Skip to content

Commit efa1aff

Browse files
committed
AST: Introduce SemanticAvailableAttr::getIntroducedRange().
It replaces the overload of AvailabilityInference::availableRange() that takes an AvailableAttr.
1 parent f1d0885 commit efa1aff

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

include/swift/AST/Attr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/AttrKind.h"
2222
#include "swift/AST/AutoDiff.h"
2323
#include "swift/AST/AvailabilityDomain.h"
24+
#include "swift/AST/AvailabilityRange.h"
2425
#include "swift/AST/ConcreteDeclRef.h"
2526
#include "swift/AST/DeclNameLoc.h"
2627
#include "swift/AST/Identifier.h"
@@ -3231,14 +3232,21 @@ class SemanticAvailableAttr final {
32313232
const AvailableAttr *getParsedAttr() const { return attr; }
32323233
const AvailabilityDomain getDomain() const { return domain; }
32333234

3235+
/// The version tuple written in source for the `introduced:` component.
32343236
std::optional<llvm::VersionTuple> getIntroduced() const {
32353237
return attr->Introduced;
32363238
}
32373239

3240+
/// Returns the effective range in which the declaration with this attribute
3241+
/// was introduced.
3242+
AvailabilityRange getIntroducedRange(ASTContext &Ctx) const;
3243+
3244+
/// The version tuple written in source for the `deprecated:` component.
32383245
std::optional<llvm::VersionTuple> getDeprecated() const {
32393246
return attr->Deprecated;
32403247
}
32413248

3249+
/// The version tuple written in source for the `obsoleted:` component.
32423250
std::optional<llvm::VersionTuple> getObsoleted() const {
32433251
return attr->Obsoleted;
32443252
}

include/swift/AST/AvailabilityInference.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ class AvailabilityInference {
5555
/// Returns true is the declaration is `@_spi_available`.
5656
static bool isAvailableAsSPI(const Decl *D);
5757

58-
/// Returns the range of platform versions in which a declaration with the
59-
/// given `@available` attribute is available.
60-
///
61-
/// NOTE: The attribute must be active on the current platform.
62-
static AvailabilityRange availableRange(const AvailableAttr *attr,
63-
ASTContext &C);
64-
6558
/// Returns the context for which the declaration
6659
/// is annotated as available, or None if the declaration
6760
/// has no availability annotation.

lib/AST/Availability.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ AvailabilityConstraint::getRequiredNewerAvailabilityRange(
8080
case Kind::Obsoleted:
8181
return std::nullopt;
8282
case Kind::IntroducedInNewerVersion:
83-
return AvailabilityInference::availableRange(attr.getParsedAttr(), ctx);
83+
return attr.getIntroducedRange(ctx);
8484
}
8585
}
8686

@@ -453,7 +453,7 @@ AvailabilityInference::annotatedAvailableRange(const Decl *D) {
453453
if (!bestAvailAttr)
454454
return std::nullopt;
455455

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

459459
bool Decl::isAvailableAsSPI() const {
@@ -785,7 +785,7 @@ AvailabilityRange AvailabilityInference::annotatedAvailableRangeForAttr(
785785
}
786786

787787
if (bestAvailAttr)
788-
return availableRange(bestAvailAttr->getParsedAttr(), ctx);
788+
return bestAvailAttr->getIntroducedRange(ctx);
789789

790790
return AvailabilityRange::alwaysAvailable();
791791
}
@@ -814,10 +814,9 @@ attrForAvailableRange(const Decl *D) {
814814

815815
std::pair<AvailabilityRange, const AvailableAttr *>
816816
AvailabilityInference::availableRangeAndAttr(const Decl *D) {
817-
if (auto rangeAttr = attrForAvailableRange(D)) {
818-
auto attr = rangeAttr->getParsedAttr();
819-
return {availableRange(attr, D->getASTContext()), attr};
820-
}
817+
if (auto rangeAttr = attrForAvailableRange(D))
818+
return {rangeAttr->getIntroducedRange(D->getASTContext()),
819+
rangeAttr->getParsedAttr()};
821820

822821
// Treat unannotated declarations as always available.
823822
return {AvailabilityRange::alwaysAvailable(), nullptr};
@@ -835,8 +834,8 @@ bool AvailabilityInference::isAvailableAsSPI(const Decl *D) {
835834
}
836835

837836
AvailabilityRange
838-
AvailabilityInference::availableRange(const AvailableAttr *attr,
839-
ASTContext &Ctx) {
837+
SemanticAvailableAttr::getIntroducedRange(ASTContext &Ctx) const {
838+
auto *attr = getParsedAttr();
840839
assert(attr->isActivePlatform(Ctx));
841840

842841
llvm::VersionTuple IntroducedVersion = attr->Introduced.value();

lib/AST/AvailabilityScope.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ AvailabilityScope::getExplicitAvailabilityRange() const {
357357
case Reason::Decl: {
358358
auto decl = Node.getAsDecl();
359359
if (auto attr = decl->getAvailableAttrForPlatformIntroduction())
360-
return AvailabilityInference::availableRange(attr->getParsedAttr(),
361-
decl->getASTContext());
360+
return attr->getIntroducedRange(decl->getASTContext());
362361

363362
return std::nullopt;
364363
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,16 +2378,17 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
23782378
// is fully contained within that declaration's range. If there is no such
23792379
// enclosing declaration, then there is nothing to check.
23802380
std::optional<AvailabilityRange> EnclosingAnnotatedRange;
2381-
AvailabilityRange AttrRange =
2382-
AvailabilityInference::availableRange(attr, Ctx);
2381+
AvailabilityRange AttrRange = semanticAttr->getIntroducedRange(Ctx);
23832382

23842383
if (auto *parent = getEnclosingDeclForDecl(D)) {
23852384
if (auto enclosingAvailable =
23862385
getSemanticAvailableRangeDeclAndAttr(parent)) {
2387-
const AvailableAttr *enclosingAttr = enclosingAvailable.value().first;
23882386
const Decl *enclosingDecl = enclosingAvailable.value().second;
2389-
EnclosingAnnotatedRange.emplace(
2390-
AvailabilityInference::availableRange(enclosingAttr, Ctx));
2387+
SemanticAvailableAttr enclosingAttr =
2388+
enclosingDecl
2389+
->getSemanticAvailableAttr(enclosingAvailable.value().first)
2390+
.value();
2391+
EnclosingAnnotatedRange.emplace(enclosingAttr.getIntroducedRange(Ctx));
23912392
if (!AttrRange.isContainedIn(*EnclosingAnnotatedRange)) {
23922393
auto limit = DiagnosticBehavior::Unspecified;
23932394
if (D->isImplicit()) {

0 commit comments

Comments
 (0)