Skip to content

Commit 9981bfa

Browse files
authored
Merge pull request #79554 from tshortli/canonicalize-available-attr-on-demand
AST: Canonicalize `AvailableAttr` versions on-demand
2 parents 11027b8 + b2c57f8 commit 9981bfa

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

include/swift/AST/Attr.h

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,11 @@ class AvailableAttr : public DeclAttribute {
746746
const StringRef Message;
747747
const StringRef Rename;
748748

749-
llvm::VersionTuple Introduced;
749+
const llvm::VersionTuple Introduced;
750750
const SourceRange IntroducedRange;
751-
llvm::VersionTuple Deprecated;
751+
const llvm::VersionTuple Deprecated;
752752
const SourceRange DeprecatedRange;
753-
llvm::VersionTuple Obsoleted;
753+
const llvm::VersionTuple Obsoleted;
754754
const SourceRange ObsoletedRange;
755755

756756
public:
@@ -909,12 +909,6 @@ class AvailableAttr : public DeclAttribute {
909909
private:
910910
friend class SemanticAvailableAttrRequest;
911911

912-
void setRawIntroduced(llvm::VersionTuple version) { Introduced = version; }
913-
914-
void setRawDeprecated(llvm::VersionTuple version) { Deprecated = version; }
915-
916-
void setRawObsoleted(llvm::VersionTuple version) { Obsoleted = version; }
917-
918912
void setCachedDomain(AvailabilityDomain domain) {
919913
assert(!DomainOrIdentifier.isDomain());
920914
DomainOrIdentifier.setDomain(domain);
@@ -3310,10 +3304,8 @@ class SemanticAvailableAttr final {
33103304
return attr->getCachedDomain().value();
33113305
}
33123306

3313-
/// The version tuple written in source for the `introduced:` component.
3314-
std::optional<llvm::VersionTuple> getIntroduced() const {
3315-
return attr->getRawIntroduced();
3316-
}
3307+
/// The version tuple for the `introduced:` component.
3308+
std::optional<llvm::VersionTuple> getIntroduced() const;
33173309

33183310
/// The source range of the `introduced:` version component.
33193311
SourceRange getIntroducedSourceRange() const { return attr->IntroducedRange; }
@@ -3322,18 +3314,14 @@ class SemanticAvailableAttr final {
33223314
/// was introduced.
33233315
AvailabilityRange getIntroducedRange(const ASTContext &Ctx) const;
33243316

3325-
/// The version tuple written in source for the `deprecated:` component.
3326-
std::optional<llvm::VersionTuple> getDeprecated() const {
3327-
return attr->getRawDeprecated();
3328-
}
3317+
/// The version tuple for the `deprecated:` component.
3318+
std::optional<llvm::VersionTuple> getDeprecated() const;
33293319

33303320
/// The source range of the `deprecated:` version component.
33313321
SourceRange getDeprecatedSourceRange() const { return attr->DeprecatedRange; }
33323322

3333-
/// The version tuple written in source for the `obsoleted:` component.
3334-
std::optional<llvm::VersionTuple> getObsoleted() const {
3335-
return attr->getRawObsoleted();
3336-
}
3323+
/// The version tuple for the `obsoleted:` component.
3324+
std::optional<llvm::VersionTuple> getObsoleted() const;
33373325

33383326
/// The source range of the `obsoleted:` version component.
33393327
SourceRange getObsoletedSourceRange() const { return attr->ObsoletedRange; }

lib/AST/Availability.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,6 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
824824
auto attrLoc = attr->getLocation();
825825
auto attrName = attr->getAttrName();
826826
auto domainLoc = attr->getDomainLoc();
827-
auto introducedVersion = attr->getRawIntroduced();
828-
auto deprecatedVersion = attr->getRawDeprecated();
829-
auto obsoletedVersion = attr->getRawObsoleted();
830827
auto mutableAttr = const_cast<AvailableAttr *>(attr);
831828
auto domain = attr->getCachedDomain();
832829

@@ -867,16 +864,18 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
867864
auto domainName = domain->getNameForAttributePrinting();
868865
auto semanticAttr = SemanticAvailableAttr(attr);
869866

870-
bool hasVersionSpec =
871-
(introducedVersion || deprecatedVersion || obsoletedVersion);
867+
bool hasIntroduced = attr->getRawIntroduced().has_value();
868+
bool hasDeprecated = attr->getRawDeprecated().has_value();
869+
auto hasObsoleted = attr->getRawObsoleted().has_value();
870+
bool hasVersionSpec = (hasIntroduced || hasDeprecated || hasObsoleted);
872871

873872
if (!domain->isVersioned() && hasVersionSpec) {
874873
SourceRange versionSourceRange;
875-
if (introducedVersion)
874+
if (hasIntroduced)
876875
versionSourceRange = semanticAttr.getIntroducedSourceRange();
877-
else if (deprecatedVersion)
876+
else if (hasDeprecated)
878877
versionSourceRange = semanticAttr.getDeprecatedSourceRange();
879-
else if (obsoletedVersion)
878+
else if (hasObsoleted)
880879
versionSourceRange = semanticAttr.getObsoletedSourceRange();
881880

882881
diags
@@ -915,25 +914,15 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
915914
}
916915
}
917916

918-
// Canonicalize platform versions.
919-
// FIXME: [availability] This should be done when remapping versions instead.
920-
if (domain->isPlatform()) {
921-
auto canonicalizeVersion = [&](llvm::VersionTuple version) {
922-
return canonicalizePlatformVersion(domain->getPlatformKind(), version);
923-
};
924-
if (introducedVersion)
925-
mutableAttr->setRawIntroduced(canonicalizeVersion(*introducedVersion));
926-
927-
if (deprecatedVersion)
928-
mutableAttr->setRawDeprecated(canonicalizeVersion(*deprecatedVersion));
929-
930-
if (obsoletedVersion)
931-
mutableAttr->setRawObsoleted(canonicalizeVersion(*obsoletedVersion));
932-
}
933-
934917
return semanticAttr;
935918
}
936919

920+
std::optional<llvm::VersionTuple> SemanticAvailableAttr::getIntroduced() const {
921+
if (auto version = attr->getRawIntroduced())
922+
return canonicalizePlatformVersion(getPlatform(), *version);
923+
return std::nullopt;
924+
}
925+
937926
AvailabilityRange
938927
SemanticAvailableAttr::getIntroducedRange(const ASTContext &Ctx) const {
939928
assert(getDomain().isActive(Ctx));
@@ -942,7 +931,7 @@ SemanticAvailableAttr::getIntroducedRange(const ASTContext &Ctx) const {
942931
if (!attr->getRawIntroduced().has_value())
943932
return AvailabilityRange::alwaysAvailable();
944933

945-
llvm::VersionTuple IntroducedVersion = attr->getRawIntroduced().value();
934+
llvm::VersionTuple IntroducedVersion = getIntroduced().value();
946935
StringRef Platform;
947936
llvm::VersionTuple RemappedIntroducedVersion;
948937
if (AvailabilityInference::updateIntroducedPlatformForFallback(
@@ -952,6 +941,18 @@ SemanticAvailableAttr::getIntroducedRange(const ASTContext &Ctx) const {
952941
return AvailabilityRange{VersionRange::allGTE(IntroducedVersion)};
953942
}
954943

944+
std::optional<llvm::VersionTuple> SemanticAvailableAttr::getDeprecated() const {
945+
if (auto version = attr->getRawDeprecated())
946+
return canonicalizePlatformVersion(getPlatform(), *version);
947+
return std::nullopt;
948+
}
949+
950+
std::optional<llvm::VersionTuple> SemanticAvailableAttr::getObsoleted() const {
951+
if (auto version = attr->getRawObsoleted())
952+
return canonicalizePlatformVersion(getPlatform(), *version);
953+
return std::nullopt;
954+
}
955+
955956
namespace {
956957
/// Infers the availability required to access a type.
957958
class AvailabilityInferenceTypeWalker : public TypeWalker {

0 commit comments

Comments
 (0)