@@ -68,6 +68,7 @@ class GenericFunctionType;
6868class LazyConformanceLoader ;
6969class LazyMemberLoader ;
7070class ModuleDecl ;
71+ class NominalTypeDecl ;
7172class PatternBindingInitializer ;
7273class TrailingWhereClause ;
7374class TypeExpr ;
@@ -2272,34 +2273,63 @@ class ClangImporterSynthesizedTypeAttr : public DeclAttribute {
22722273 }
22732274};
22742275
2276+ // / The owning decl for a given custom attribute, or a DeclContext for a
2277+ // / custom attribute in e.g a closure or inheritance clause.
2278+ class CustomAttrOwner final {
2279+ llvm::PointerUnion<Decl *, DeclContext *> Owner;
2280+
2281+ public:
2282+ CustomAttrOwner () : Owner(nullptr ) {}
2283+ CustomAttrOwner (Decl *D) : Owner(D) {}
2284+ CustomAttrOwner (DeclContext *DC) : Owner(DC) {}
2285+
2286+ // / If the owner is a declaration, returns it, \c nullptr otherwise.
2287+ Decl *getAsDecl () const { return Owner.dyn_cast <Decl *>(); }
2288+
2289+ // / Retrieve the DeclContext for the CustomAttr.
2290+ DeclContext *getDeclContext () const ;
2291+ };
2292+
22752293// / Defines a custom attribute.
22762294class CustomAttr final : public DeclAttribute {
22772295 TypeExpr *typeExpr;
22782296 ArgumentList *argList;
2297+ CustomAttrOwner owner;
22792298 CustomAttributeInitializer *initContext;
22802299 Expr *semanticInit = nullptr ;
22812300
22822301 mutable unsigned isArgUnsafeBit : 1 ;
22832302
22842303 CustomAttr (SourceLoc atLoc, SourceRange range, TypeExpr *type,
2285- CustomAttributeInitializer *initContext, ArgumentList *argList ,
2286- bool implicit);
2304+ CustomAttrOwner owner, CustomAttributeInitializer *initContext ,
2305+ ArgumentList *argList, bool implicit);
22872306
22882307public:
22892308 static CustomAttr *create (ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
2290- bool implicit = false ) {
2291- return create (ctx, atLoc, type, /* initContext*/ nullptr ,
2309+ CustomAttrOwner owner, bool implicit = false ) {
2310+ return create (ctx, atLoc, type, owner, /* initContext*/ nullptr ,
22922311 /* argList*/ nullptr , implicit);
22932312 }
22942313
22952314 static CustomAttr *create (ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
2315+ CustomAttrOwner owner,
22962316 CustomAttributeInitializer *initContext,
22972317 ArgumentList *argList, bool implicit = false );
22982318
22992319 TypeExpr *getTypeExpr () const { return typeExpr; }
23002320 TypeRepr *getTypeRepr () const ;
23012321 Type getType () const ;
23022322
2323+ // / Retrieve the Decl or DeclContext owner for the attribute.
2324+ CustomAttrOwner getOwner () const { return owner; }
2325+ void setOwner (CustomAttrOwner newOwner) { owner = newOwner; }
2326+
2327+ ASTContext &getASTContext () const ;
2328+
2329+ // / Retrieve the NominalTypeDecl the CustomAttr refers to, or \c nullptr if
2330+ // / it doesn't refer to one (which can be the case for e.g macro attrs).
2331+ NominalTypeDecl *getNominalDecl () const ;
2332+
23032333 // / Destructure an attribute's type repr for a macro reference.
23042334 // /
23052335 // / For a 1-level member type repr whose base and member are both identifier
@@ -2336,7 +2366,8 @@ class CustomAttr final : public DeclAttribute {
23362366 CustomAttr *clone (ASTContext &ctx) const {
23372367 assert (argList == nullptr &&
23382368 " Cannot clone custom attribute with an argument list" );
2339- return create (ctx, AtLoc, getTypeExpr (), initContext, argList, isImplicit ());
2369+ return create (ctx, AtLoc, getTypeExpr (), owner, initContext, argList,
2370+ isImplicit ());
23402371 }
23412372
23422373 bool canClone () const { return argList == nullptr ; }
0 commit comments