Skip to content

Commit e131682

Browse files
authored
Merge pull request #77886 from tshortli/consolidate-availability-queries-on-decl
AST: Consolidate queries for `@available` attributes on `Decl`
2 parents 7e05f1d + d8c946d commit e131682

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+376
-469
lines changed

include/swift/AST/Attr.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,6 @@ class AvailableAttr : public DeclAttribute {
809809
Bits.AvailableAttr.PlatformAgnostic);
810810
}
811811

812-
/// Determine if a given declaration should be considered unavailable given
813-
/// the current settings.
814-
///
815-
/// \returns The attribute responsible for making the declaration unavailable.
816-
static const AvailableAttr *isUnavailable(const Decl *D);
817-
818812
/// Returns true if the availability applies to a specific
819813
/// platform.
820814
bool hasPlatform() const { return getPlatform() != PlatformKind::none; }
@@ -2931,43 +2925,6 @@ class DeclAttributes {
29312925
return UnaryOperatorKind::None;
29322926
}
29332927

2934-
bool isUnavailable(const ASTContext &ctx) const {
2935-
return getUnavailable(ctx) != nullptr;
2936-
}
2937-
2938-
bool isDeprecated(const ASTContext &ctx) const {
2939-
return getDeprecated(ctx) != nullptr;
2940-
}
2941-
2942-
/// Determine whether there is a swiftVersionSpecific attribute that's
2943-
/// unavailable relative to the provided language version.
2944-
bool
2945-
isUnavailableInSwiftVersion(const version::Version &effectiveVersion) const;
2946-
2947-
/// Finds the most-specific platform-specific attribute that is
2948-
/// active for the current platform.
2949-
const AvailableAttr *
2950-
findMostSpecificActivePlatform(const ASTContext &ctx,
2951-
bool ignoreAppExtensions = false) const;
2952-
2953-
/// Returns the first @available attribute that indicates
2954-
/// a declaration is unavailable, or null otherwise.
2955-
const AvailableAttr *getUnavailable(const ASTContext &ctx,
2956-
bool ignoreAppExtensions = false) const;
2957-
2958-
/// Returns the first @available attribute that indicates
2959-
/// a declaration is deprecated on all deployment targets, or null otherwise.
2960-
const AvailableAttr *getDeprecated(const ASTContext &ctx) const;
2961-
2962-
/// Returns the first @available attribute that indicates
2963-
/// a declaration will be deprecated in the future, or null otherwise.
2964-
const AvailableAttr *getSoftDeprecated(const ASTContext &ctx) const;
2965-
2966-
/// Returns the first @available attribute that indicates
2967-
/// a declaration is unavailable from asynchronous contexts, or null
2968-
/// otherwise.
2969-
const AvailableAttr *getNoAsync(const ASTContext &ctx) const;
2970-
29712928
/// Returns the `@backDeployed` attribute that is active for the current
29722929
/// platform.
29732930
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,

include/swift/AST/Decl.h

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,16 +1378,61 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
13781378
/// @_originalDefinedIn attribute, this function returns this module name.
13791379
StringRef getAlternateModuleName() const;
13801380

1381-
// Is this Decl an SPI? It can be directly marked with @_spi or is defined in
1382-
// an @_spi context.
1381+
/// Is this Decl an SPI? It can be directly marked with @_spi or is defined in
1382+
/// an @_spi context.
13831383
bool isSPI() const;
13841384

1385+
/// Returns true if the attribute providing the platform availability
1386+
/// introduction for this decl is an `@_spi_available` attribute.
13851387
bool isAvailableAsSPI() const;
13861388

13871389
/// Determine whether this Decl has either Private or FilePrivate access,
13881390
/// and its DeclContext does not.
13891391
bool isOutermostPrivateOrFilePrivateScope() const;
13901392

1393+
/// Returns the active platform-specific `@available` attribute for this decl.
1394+
/// There may be multiple `@available` attributes that are relevant to the
1395+
/// current platform, but the returned one has the highest priority.
1396+
const AvailableAttr *getActiveAvailableAttrForCurrentPlatform(
1397+
bool ignoreAppExtensions = false) const;
1398+
1399+
/// Returns true if the declaration is deprecated at the current deployment
1400+
/// target.
1401+
bool isDeprecated() const { return getDeprecatedAttr() != nullptr; }
1402+
1403+
/// Returns the first `@available` attribute that indicates that this decl
1404+
/// is deprecated on current deployment target, or `nullptr` otherwise.
1405+
const AvailableAttr *getDeprecatedAttr() const;
1406+
1407+
/// Returns the first `@available` attribute that indicates that this decl
1408+
/// will be deprecated in the future, or `nullptr` otherwise.
1409+
const AvailableAttr *getSoftDeprecatedAttr() const;
1410+
1411+
/// Returns the first @available attribute that indicates this decl is
1412+
/// unavailable from asynchronous contexts, or `nullptr` otherwise.
1413+
const AvailableAttr *getNoAsyncAttr() const;
1414+
1415+
/// Returns true if the decl has been marked unavailable in the Swift language
1416+
/// version that is currently active.
1417+
bool isUnavailableInCurrentSwiftVersion() const;
1418+
1419+
/// Returns true if the decl is always unavailable in the current compilation
1420+
/// context. For example, the decl could be marked explicitly unavailable on
1421+
/// either the current platform or in the current language mode. Returns false
1422+
/// for declarations that are only _potentially_ unavailable because of a
1423+
/// condition that could be satisfied at runtime (like requiring an operating
1424+
/// system version that is higher than the current deployment target).
1425+
///
1426+
/// Note that this query only considers the attributes that are attached
1427+
/// directly to this decl (or the extension it is declared in, if applicable).
1428+
bool isUnavailable() const { return getUnavailableAttr() != nullptr; }
1429+
1430+
/// If the decl is always unavailable in the current compilation
1431+
/// context, returns the attribute attached to the decl (or its parent
1432+
/// extension) that makes it unavailable.
1433+
const AvailableAttr *
1434+
getUnavailableAttr(bool ignoreAppExtensions = false) const;
1435+
13911436
/// Retrieve the @available attribute that provides the OS version range that
13921437
/// this declaration is available in.
13931438
///
@@ -1406,7 +1451,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14061451
/// the attribute.
14071452
///
14081453
/// Note that this notion of unavailability is broader than that which is
1409-
/// checked by \c AvailableAttr::isUnavailable.
1454+
/// checked by \c isUnavailable().
14101455
std::optional<std::pair<const AvailableAttr *, const Decl *>>
14111456
getSemanticUnavailableAttr(bool ignoreAppExtensions = false) const;
14121457

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
14731473
ObjCName(Ctx.getObjcName(D)),
14741474
InitKind(Ctx.getInitKind(D)),
14751475
IsImplicit(D->isImplicit()),
1476-
IsDeprecated(D->getAttrs().isDeprecated(D->getASTContext())),
1476+
IsDeprecated(D->isDeprecated()),
14771477
IsABIPlaceholder(isABIPlaceholderRecursive(D)),
14781478
IsFromExtension(isDeclaredInExtension(D)),
14791479
DeclAttrs(collectDeclAttributes(D)) {
@@ -1767,8 +1767,8 @@ SDKContext::shouldIgnore(Decl *D, const Decl* Parent) const {
17671767
if (D->isPrivateSystemDecl(false))
17681768
return true;
17691769
}
1770-
if (AvailableAttr::isUnavailable(D))
1771-
return true;
1770+
if (D->isUnavailable())
1771+
return true;
17721772
if (auto VD = dyn_cast<ValueDecl>(D)) {
17731773
switch (getAccessLevel(VD)) {
17741774
case AccessLevel::Internal:

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,10 +2150,8 @@ bool ShouldPrintChecker::shouldPrint(const Pattern *P,
21502150
}
21512151

21522152
bool isNonSendableExtension(const Decl *D) {
2153-
ASTContext &ctx = D->getASTContext();
2154-
21552153
const ExtensionDecl *ED = dyn_cast<ExtensionDecl>(D);
2156-
if (!ED || !ED->getAttrs().isUnavailable(ctx))
2154+
if (!ED || !ED->isUnavailable())
21572155
return false;
21582156

21592157
auto nonSendable =
@@ -2193,8 +2191,7 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
21932191
return false;
21942192
}
21952193

2196-
if (Options.SkipUnavailable &&
2197-
D->getAttrs().isUnavailable(D->getASTContext()))
2194+
if (Options.SkipUnavailable && D->isUnavailable())
21982195
return false;
21992196
}
22002197

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,7 @@ class Verifier : public ASTWalker {
29312931

29322932
if (auto req = dyn_cast<ValueDecl>(member)) {
29332933
if (!normal->hasWitness(req)) {
2934-
if ((req->getAttrs().isUnavailable(Ctx) ||
2934+
if ((req->isUnavailable() ||
29352935
req->getAttrs().hasAttribute<OptionalAttr>()) &&
29362936
proto->isObjC()) {
29372937
continue;

0 commit comments

Comments
 (0)