@@ -58,6 +58,7 @@ class ClassDecl;
5858class GenericFunctionType ;
5959class LazyConformanceLoader ;
6060class LazyMemberLoader ;
61+ class ModuleDecl ;
6162class PatternBindingInitializer ;
6263class TrailingWhereClause ;
6364class TypeExpr ;
@@ -2581,6 +2582,59 @@ class DeclAttributes {
25812582 SourceLoc getStartLoc (bool forModifiers = false ) const ;
25822583};
25832584
2585+ // / Attributes applied directly to the declaration.
2586+ // /
2587+ // / We should really just have \c DeclAttributes and \c SemanticDeclAttributes,
2588+ // / but currently almost all callers expect the latter. Instead of changing all
2589+ // / callers of \c getAttrs, instead provide a way to retrieive the original
2590+ // / attributes.
2591+ class OrigDeclAttributes {
2592+ SmallVector<DeclAttribute *> attributes;
2593+ using AttrListTy = SmallVectorImpl<DeclAttribute *>;
2594+
2595+ public:
2596+ OrigDeclAttributes () = default ;
2597+
2598+ OrigDeclAttributes (DeclAttributes semanticAttrs, ModuleDecl *mod);
2599+
2600+
2601+ using iterator = AttrListTy::iterator;
2602+ using const_iterator = AttrListTy::const_iterator;
2603+
2604+ iterator begin () { return attributes.begin (); }
2605+ iterator end () { return attributes.end (); }
2606+ const_iterator begin () const { return attributes.begin (); }
2607+ const_iterator end () const { return attributes.end (); }
2608+
2609+ template <typename AttrType, bool AllowInvalid>
2610+ using AttributeKindRange =
2611+ OptionalTransformRange<iterator_range<const_iterator>,
2612+ ToAttributeKind<AttrType, AllowInvalid>, const_iterator>;
2613+
2614+ template <typename AttrType, bool AllowInvalid = false >
2615+ AttributeKindRange<AttrType, AllowInvalid> getAttributes () const {
2616+ return AttributeKindRange<AttrType, AllowInvalid>(make_range (begin (), end ()), ToAttributeKind<AttrType, AllowInvalid>());
2617+ }
2618+
2619+ // / Retrieve the first attribute of the given attribute class.
2620+ template <typename AttrType>
2621+ const AttrType *getAttribute (bool allowInvalid = false ) const {
2622+ for (auto *attr : attributes) {
2623+ if (auto *specificAttr = dyn_cast<AttrType>(attr)) {
2624+ if (specificAttr->isValid () || allowInvalid)
2625+ return specificAttr;
2626+ }
2627+ }
2628+ return nullptr ;
2629+ }
2630+
2631+ // / Determine whether there is an attribute with the given attribute class.
2632+ template <typename AttrType>
2633+ bool hasAttribute (bool allowInvalid = false ) const {
2634+ return getAttribute<AttrType>(allowInvalid) != nullptr ;
2635+ }
2636+ };
2637+
25842638// / TypeAttributes - These are attributes that may be applied to types.
25852639class TypeAttributes {
25862640 // Get a SourceLoc for every possible attribute that can be parsed in source.
0 commit comments