Skip to content

Commit d47fc42

Browse files
committed
[NFC] Fix ImplementsAttr::clone()
1 parent bdf2294 commit d47fc42

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

include/swift/AST/Attr.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,14 +1725,16 @@ class StorageRestrictionsAttr final
17251725
/// The @_implements attribute, which treats a decl as the implementation for
17261726
/// some named protocol requirement (but otherwise not-visible by that name).
17271727
class ImplementsAttr : public DeclAttribute {
1728-
TypeRepr *TyR;
1728+
/// If constructed by the \c create() variant with a TypeRepr, the TypeRepr;
1729+
/// if constructed by the \c create() variant with a DeclContext and
1730+
/// ProtocolDecl, the DeclContext.
1731+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC;
17291732
DeclName MemberName;
17301733
DeclNameLoc MemberNameLoc;
17311734

17321735
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
1733-
TypeRepr *TyR,
1734-
DeclName MemberName,
1735-
DeclNameLoc MemberNameLoc);
1736+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
1737+
DeclName MemberName, DeclNameLoc MemberNameLoc);
17361738

17371739
public:
17381740
static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
@@ -1752,7 +1754,9 @@ class ImplementsAttr : public DeclAttribute {
17521754
/// otherwise `nullopt`. This should only be used for dumping.
17531755
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;
17541756

1755-
TypeRepr *getProtocolTypeRepr() const { return TyR; }
1757+
TypeRepr *getProtocolTypeRepr() const {
1758+
return TyROrDC.dyn_cast<TypeRepr *>();
1759+
}
17561760

17571761
DeclName getMemberName() const { return MemberName; }
17581762
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }
@@ -1763,8 +1767,12 @@ class ImplementsAttr : public DeclAttribute {
17631767

17641768
/// Create a copy of this attribute.
17651769
ImplementsAttr *clone(ASTContext &ctx) const {
1766-
return new (ctx) ImplementsAttr(
1767-
AtLoc, Range, TyR, getMemberName(), getMemberNameLoc());
1770+
if (auto tyR = getProtocolTypeRepr()) {
1771+
return create(ctx, AtLoc, Range, tyR, getMemberName(),
1772+
getMemberNameLoc());
1773+
}
1774+
auto dc = TyROrDC.dyn_cast<DeclContext *>();
1775+
return create(dc, getProtocol(dc), getMemberName());
17681776
}
17691777
};
17701778

lib/AST/Attr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,11 +2666,12 @@ StorageRestrictionsAttr::create(
26662666
/*implicit=*/false);
26672667
}
26682668

2669-
ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
2670-
TypeRepr *TyR, DeclName MemberName,
2671-
DeclNameLoc MemberNameLoc)
2669+
ImplementsAttr::
2670+
ImplementsAttr(SourceLoc atLoc, SourceRange range,
2671+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
2672+
DeclName MemberName, DeclNameLoc MemberNameLoc)
26722673
: DeclAttribute(DeclAttrKind::Implements, atLoc, range, /*Implicit=*/false),
2673-
TyR(TyR), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
2674+
TyROrDC(TyROrDC), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
26742675

26752676
ImplementsAttr *ImplementsAttr::create(ASTContext &Ctx, SourceLoc atLoc,
26762677
SourceRange range,
@@ -2687,9 +2688,8 @@ ImplementsAttr *ImplementsAttr::create(DeclContext *DC,
26872688
DeclName MemberName) {
26882689
auto &ctx = DC->getASTContext();
26892690
void *mem = ctx.Allocate(sizeof(ImplementsAttr), alignof(ImplementsAttr));
2690-
auto *attr = new (mem) ImplementsAttr(
2691-
SourceLoc(), SourceRange(), nullptr,
2692-
MemberName, DeclNameLoc());
2691+
auto *attr = new (mem) ImplementsAttr(SourceLoc(), SourceRange(), DC,
2692+
MemberName, DeclNameLoc());
26932693
ctx.evaluator.cacheOutput(ImplementsAttrProtocolRequest{attr, DC},
26942694
std::move(Proto));
26952695
return attr;

0 commit comments

Comments
 (0)