Skip to content

Commit 4ed62e3

Browse files
committed
AST: Introduce Decl::getAvailableAttrForPlatformIntroduction().
It replaces `AvailabilityInference::attrForAnnotatedAvailableRange()`.
1 parent 33bff48 commit 4ed62e3

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

include/swift/AST/AvailabilityInference.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ class AvailabilityInference {
6262
static AvailabilityRange availableRange(const AvailableAttr *attr,
6363
ASTContext &C);
6464

65-
/// Returns the attribute that should be used to determine the availability
66-
/// range of the given declaration, or nullptr if there is none.
67-
static const AvailableAttr *attrForAnnotatedAvailableRange(const Decl *D);
68-
6965
/// Returns the context for which the declaration
7066
/// is annotated as available, or None if the declaration
7167
/// has no availability annotation.

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14301430
std::optional<SemanticAvailableAttr> getActiveAvailableAttrForCurrentPlatform(
14311431
bool ignoreAppExtensions = false) const;
14321432

1433+
/// Returns the active platform-specific `@available` attribute that should be
1434+
/// used to determine the platform introduction version of the decl.
1435+
std::optional<SemanticAvailableAttr>
1436+
getAvailableAttrForPlatformIntroduction() const;
1437+
14331438
/// Returns true if the declaration is deprecated at the current deployment
14341439
/// target.
14351440
bool isDeprecated() const { return getDeprecatedAttr().has_value(); }

lib/AST/Availability.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ bool AvailabilityInference::updateBeforePlatformForFallback(
430430
return false;
431431
}
432432

433-
const AvailableAttr *
434-
AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D) {
433+
std::optional<SemanticAvailableAttr>
434+
Decl::getAvailableAttrForPlatformIntroduction() const {
435435
std::optional<SemanticAvailableAttr> bestAvailAttr;
436436

437-
D = abstractSyntaxDeclForAvailableAttribute(D);
437+
auto D = abstractSyntaxDeclForAvailableAttribute(this);
438438

439439
for (auto attr : D->getSemanticAvailableAttrs(/*includingInactive=*/false)) {
440440
if (!attr.isPlatformSpecific() || !attr.getIntroduced())
@@ -444,16 +444,16 @@ AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D) {
444444
bestAvailAttr.emplace(attr);
445445
}
446446

447-
return bestAvailAttr ? bestAvailAttr->getParsedAttr() : nullptr;
447+
return bestAvailAttr;
448448
}
449449

450450
std::optional<AvailabilityRange>
451451
AvailabilityInference::annotatedAvailableRange(const Decl *D) {
452-
auto bestAvailAttr = attrForAnnotatedAvailableRange(D);
452+
auto bestAvailAttr = D->getAvailableAttrForPlatformIntroduction();
453453
if (!bestAvailAttr)
454454
return std::nullopt;
455455

456-
return availableRange(bestAvailAttr, D->getASTContext());
456+
return availableRange(bestAvailAttr->getParsedAttr(), D->getASTContext());
457457
}
458458

459459
bool Decl::isAvailableAsSPI() const {
@@ -790,8 +790,9 @@ AvailabilityRange AvailabilityInference::annotatedAvailableRangeForAttr(
790790
return AvailabilityRange::alwaysAvailable();
791791
}
792792

793-
static const AvailableAttr *attrForAvailableRange(const Decl *D) {
794-
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(D))
793+
static std::optional<SemanticAvailableAttr>
794+
attrForAvailableRange(const Decl *D) {
795+
if (auto attr = D->getAvailableAttrForPlatformIntroduction())
795796
return attr;
796797

797798
// Unlike other declarations, extensions can be used without referring to them
@@ -804,16 +805,17 @@ static const AvailableAttr *attrForAvailableRange(const Decl *D) {
804805

805806
DeclContext *DC = D->getDeclContext();
806807
if (auto *ED = dyn_cast<ExtensionDecl>(DC)) {
807-
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(ED))
808+
if (auto attr = ED->getAvailableAttrForPlatformIntroduction())
808809
return attr;
809810
}
810811

811-
return nullptr;
812+
return std::nullopt;
812813
}
813814

814815
std::pair<AvailabilityRange, const AvailableAttr *>
815816
AvailabilityInference::availableRangeAndAttr(const Decl *D) {
816-
if (auto attr = attrForAvailableRange(D)) {
817+
if (auto rangeAttr = attrForAvailableRange(D)) {
818+
auto attr = rangeAttr->getParsedAttr();
817819
return {availableRange(attr, D->getASTContext()), attr};
818820
}
819821

@@ -827,7 +829,7 @@ AvailabilityRange AvailabilityInference::availableRange(const Decl *D) {
827829

828830
bool AvailabilityInference::isAvailableAsSPI(const Decl *D) {
829831
if (auto attr = attrForAvailableRange(D))
830-
return attr->isSPI();
832+
return attr->getParsedAttr()->isSPI();
831833

832834
return false;
833835
}

lib/AST/AvailabilityScope.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ AvailabilityScope::getExplicitAvailabilityRange() const {
356356

357357
case Reason::Decl: {
358358
auto decl = Node.getAsDecl();
359-
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(decl))
360-
return AvailabilityInference::availableRange(attr, decl->getASTContext());
359+
if (auto attr = decl->getAvailableAttrForPlatformIntroduction())
360+
return AvailabilityInference::availableRange(attr->getParsedAttr(),
361+
decl->getASTContext());
361362

362363
return std::nullopt;
363364
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,8 @@ static Decl *getEnclosingDeclForDecl(Decl *D) {
22662266

22672267
static std::optional<std::pair<const AvailableAttr *, const Decl *>>
22682268
getSemanticAvailableRangeDeclAndAttr(const Decl *decl) {
2269-
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(decl))
2270-
return std::make_pair(attr, decl);
2269+
if (auto attr = decl->getAvailableAttrForPlatformIntroduction())
2270+
return std::make_pair(attr->getParsedAttr(), decl);
22712271

22722272
if (auto *parent =
22732273
AvailabilityInference::parentDeclForInferredAvailability(decl))

0 commit comments

Comments
 (0)