Skip to content

Commit fae776c

Browse files
committed
[NFC] Fix ImplementsAttr::clone()
1 parent c0ba520 commit fae776c

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
@@ -1752,14 +1752,16 @@ class StorageRestrictionsAttr final
17521752
/// The @_implements attribute, which treats a decl as the implementation for
17531753
/// some named protocol requirement (but otherwise not-visible by that name).
17541754
class ImplementsAttr : public DeclAttribute {
1755-
TypeRepr *TyR;
1755+
/// If constructed by the \c create() variant with a TypeRepr, the TypeRepr;
1756+
/// if constructed by the \c create() variant with a DeclContext and
1757+
/// ProtocolDecl, the DeclContext.
1758+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC;
17561759
DeclName MemberName;
17571760
DeclNameLoc MemberNameLoc;
17581761

17591762
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
1760-
TypeRepr *TyR,
1761-
DeclName MemberName,
1762-
DeclNameLoc MemberNameLoc);
1763+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
1764+
DeclName MemberName, DeclNameLoc MemberNameLoc);
17631765

17641766
public:
17651767
static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
@@ -1779,7 +1781,9 @@ class ImplementsAttr : public DeclAttribute {
17791781
/// otherwise `nullopt`. This should only be used for dumping.
17801782
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;
17811783

1782-
TypeRepr *getProtocolTypeRepr() const { return TyR; }
1784+
TypeRepr *getProtocolTypeRepr() const {
1785+
return TyROrDC.dyn_cast<TypeRepr *>();
1786+
}
17831787

17841788
DeclName getMemberName() const { return MemberName; }
17851789
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }
@@ -1790,8 +1794,12 @@ class ImplementsAttr : public DeclAttribute {
17901794

17911795
/// Create a copy of this attribute.
17921796
ImplementsAttr *clone(ASTContext &ctx) const {
1793-
return new (ctx) ImplementsAttr(
1794-
AtLoc, Range, TyR, getMemberName(), getMemberNameLoc());
1797+
if (auto tyR = getProtocolTypeRepr()) {
1798+
return create(ctx, AtLoc, Range, tyR, getMemberName(),
1799+
getMemberNameLoc());
1800+
}
1801+
auto dc = TyROrDC.dyn_cast<DeclContext *>();
1802+
return create(dc, getProtocol(dc), getMemberName());
17951803
}
17961804
};
17971805

lib/AST/Attr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,11 +2674,12 @@ StorageRestrictionsAttr::create(
26742674
/*implicit=*/false);
26752675
}
26762676

2677-
ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
2678-
TypeRepr *TyR, DeclName MemberName,
2679-
DeclNameLoc MemberNameLoc)
2677+
ImplementsAttr::
2678+
ImplementsAttr(SourceLoc atLoc, SourceRange range,
2679+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
2680+
DeclName MemberName, DeclNameLoc MemberNameLoc)
26802681
: DeclAttribute(DeclAttrKind::Implements, atLoc, range, /*Implicit=*/false),
2681-
TyR(TyR), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
2682+
TyROrDC(TyROrDC), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
26822683

26832684
ImplementsAttr *ImplementsAttr::create(ASTContext &Ctx, SourceLoc atLoc,
26842685
SourceRange range,
@@ -2695,9 +2696,8 @@ ImplementsAttr *ImplementsAttr::create(DeclContext *DC,
26952696
DeclName MemberName) {
26962697
auto &ctx = DC->getASTContext();
26972698
void *mem = ctx.Allocate(sizeof(ImplementsAttr), alignof(ImplementsAttr));
2698-
auto *attr = new (mem) ImplementsAttr(
2699-
SourceLoc(), SourceRange(), nullptr,
2700-
MemberName, DeclNameLoc());
2699+
auto *attr = new (mem) ImplementsAttr(SourceLoc(), SourceRange(), DC,
2700+
MemberName, DeclNameLoc());
27012701
ctx.evaluator.cacheOutput(ImplementsAttrProtocolRequest{attr, DC},
27022702
std::move(Proto));
27032703
return attr;

0 commit comments

Comments
 (0)