|
20 | 20 | #include "swift/AST/ASTAllocated.h"
|
21 | 21 | #include "swift/AST/AttrKind.h"
|
22 | 22 | #include "swift/AST/AutoDiff.h"
|
| 23 | +#include "swift/AST/AvailabilityDomain.h" |
23 | 24 | #include "swift/AST/ConcreteDeclRef.h"
|
24 | 25 | #include "swift/AST/DeclNameLoc.h"
|
25 | 26 | #include "swift/AST/Identifier.h"
|
@@ -782,12 +783,6 @@ class AvailableAttr : public DeclAttribute {
|
782 | 783 | /// Indicates where the Obsoleted version was specified.
|
783 | 784 | const SourceRange ObsoletedRange;
|
784 | 785 |
|
785 |
| - /// Whether this is a language-version-specific entity. |
786 |
| - bool isLanguageVersionSpecific() const; |
787 |
| - |
788 |
| - /// Whether this is a PackageDescription version specific entity. |
789 |
| - bool isPackageDescriptionVersionSpecific() const; |
790 |
| - |
791 | 786 | /// Whether this is an unconditionally unavailable entity.
|
792 | 787 | bool isUnconditionallyUnavailable() const;
|
793 | 788 |
|
@@ -831,17 +826,6 @@ class AvailableAttr : public DeclAttribute {
|
831 | 826 | /// Returns true if this attribute is active given the current platform.
|
832 | 827 | bool isActivePlatform(const ASTContext &ctx) const;
|
833 | 828 |
|
834 |
| - /// Returns the active version from the AST context corresponding to |
835 |
| - /// the available kind. For example, this will return the effective language |
836 |
| - /// version for swift version-specific availability kind, PackageDescription |
837 |
| - /// version for PackageDescription version-specific availability. |
838 |
| - llvm::VersionTuple getActiveVersion(const ASTContext &ctx) const; |
839 |
| - |
840 |
| - /// Compare this attribute's version information against the platform or |
841 |
| - /// language version (assuming the this attribute pertains to the active |
842 |
| - /// platform). |
843 |
| - AvailableVersionComparison getVersionAvailability(const ASTContext &ctx) const; |
844 |
| - |
845 | 829 | /// Create an AvailableAttr that indicates specific availability
|
846 | 830 | /// for all platforms.
|
847 | 831 | static AvailableAttr *
|
@@ -3190,6 +3174,95 @@ class ParsedDeclAttributes {
|
3190 | 3174 | }
|
3191 | 3175 | };
|
3192 | 3176 |
|
| 3177 | +/// A wrapper for `AvailableAttr` that is enriched with additional semantic |
| 3178 | +/// informaton, like its corresponding `AvailabilityDomain`. |
| 3179 | +class SemanticAvailableAttr final { |
| 3180 | + const AvailableAttr *attr; |
| 3181 | + const AvailabilityDomain domain; |
| 3182 | + |
| 3183 | +public: |
| 3184 | + SemanticAvailableAttr(const AvailableAttr *attr, AvailabilityDomain domain) |
| 3185 | + : attr(attr), domain(domain) { |
| 3186 | + assert(attr); |
| 3187 | + } |
| 3188 | + |
| 3189 | + const AvailableAttr *getParsedAttr() const { return attr; } |
| 3190 | + const AvailabilityDomain getDomain() const { return domain; } |
| 3191 | + |
| 3192 | + /// Returns the platform kind that the attribute applies to, or |
| 3193 | + /// `PlatformKind::none` if the attribute is not platform specific. |
| 3194 | + bool isPlatformSpecific() const { return domain.isPlatform(); } |
| 3195 | + |
| 3196 | + /// Returns the platform kind that the attribute applies to, or |
| 3197 | + /// `PlatformKind::none` if the attribute is not platform specific. |
| 3198 | + PlatformKind getPlatformKind() const { return domain.getPlatformKind(); } |
| 3199 | + |
| 3200 | + /// Whether this attribute has an introduced, deprecated, or obsoleted |
| 3201 | + /// version. |
| 3202 | + bool isVersionSpecific() const { |
| 3203 | + return attr->Introduced || attr->Deprecated || attr->Obsoleted; |
| 3204 | + } |
| 3205 | + |
| 3206 | + /// Whether this is a language mode specific attribute. |
| 3207 | + bool isSwiftLanguageModeSpecific() const { |
| 3208 | + return domain.isSwiftLanguage() && isVersionSpecific(); |
| 3209 | + } |
| 3210 | + |
| 3211 | + /// Whether this is a PackageDescription version specific attribute. |
| 3212 | + bool isPackageDescriptionVersionSpecific() const { |
| 3213 | + return domain.isPackageDescription() && isVersionSpecific(); |
| 3214 | + } |
| 3215 | + |
| 3216 | + /// Returns the active version from the AST context corresponding to |
| 3217 | + /// the available kind. For example, this will return the effective language |
| 3218 | + /// version for swift version-specific availability kind, PackageDescription |
| 3219 | + /// version for PackageDescription version-specific availability. |
| 3220 | + llvm::VersionTuple getActiveVersion(const ASTContext &ctx) const; |
| 3221 | + |
| 3222 | + /// Compare this attribute's version information against the platform or |
| 3223 | + /// language version (assuming the this attribute pertains to the active |
| 3224 | + /// platform). |
| 3225 | + AvailableVersionComparison |
| 3226 | + getVersionAvailability(const ASTContext &ctx) const; |
| 3227 | + |
| 3228 | + /// Returns true if this attribute is considered active in the current |
| 3229 | + /// compilation context. |
| 3230 | + bool isActive(ASTContext &ctx) const; |
| 3231 | +}; |
| 3232 | + |
| 3233 | +/// An iterable range of `SemanticAvailableAttr`s. |
| 3234 | +class SemanticAvailableAttributes { |
| 3235 | +public: |
| 3236 | + class Filter final { |
| 3237 | + const Decl *decl; |
| 3238 | + bool includeInactive; |
| 3239 | + |
| 3240 | + public: |
| 3241 | + Filter(const Decl *decl, bool includeInactive) |
| 3242 | + : decl(decl), includeInactive(includeInactive) {} |
| 3243 | + |
| 3244 | + std::optional<SemanticAvailableAttr> |
| 3245 | + operator()(const DeclAttribute *attr) const; |
| 3246 | + }; |
| 3247 | + |
| 3248 | + using Range = |
| 3249 | + OptionalTransformRange<iterator_range<DeclAttributes::const_iterator>, |
| 3250 | + Filter>; |
| 3251 | + |
| 3252 | +private: |
| 3253 | + Range attrRange; |
| 3254 | + |
| 3255 | +public: |
| 3256 | + SemanticAvailableAttributes(const DeclAttributes &attrs, const Decl *decl, |
| 3257 | + bool includeInactive = false) |
| 3258 | + : attrRange(make_range(attrs.begin(), attrs.end()), |
| 3259 | + Filter(decl, includeInactive)) {} |
| 3260 | + |
| 3261 | + Range::iterator begin() const { return attrRange.begin(); } |
| 3262 | + Range::iterator end() const { return attrRange.end(); } |
| 3263 | + bool empty() const { return attrRange.empty(); } |
| 3264 | +}; |
| 3265 | + |
3193 | 3266 | class alignas(1 << AttrAlignInBits) TypeAttribute
|
3194 | 3267 | : public ASTAllocated<TypeAttribute> {
|
3195 | 3268 | protected:
|
|
0 commit comments