Skip to content

Commit 111661e

Browse files
committed
AST: Move several queries to SemanticAvailableAttr.
1 parent 17dfbf5 commit 111661e

File tree

8 files changed

+83
-88
lines changed

8 files changed

+83
-88
lines changed

include/swift/AST/Attr.h

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,6 @@ class AvailableAttr : public DeclAttribute {
783783
/// Indicates where the Obsoleted version was specified.
784784
const SourceRange ObsoletedRange;
785785

786-
/// Whether this is a language-version-specific entity.
787-
bool isLanguageVersionSpecific() const;
788-
789-
/// Whether this is a PackageDescription version specific entity.
790-
bool isPackageDescriptionVersionSpecific() const;
791-
792786
/// Whether this is an unconditionally unavailable entity.
793787
bool isUnconditionallyUnavailable() const;
794788

@@ -832,17 +826,6 @@ class AvailableAttr : public DeclAttribute {
832826
/// Returns true if this attribute is active given the current platform.
833827
bool isActivePlatform(const ASTContext &ctx) const;
834828

835-
/// Returns the active version from the AST context corresponding to
836-
/// the available kind. For example, this will return the effective language
837-
/// version for swift version-specific availability kind, PackageDescription
838-
/// version for PackageDescription version-specific availability.
839-
llvm::VersionTuple getActiveVersion(const ASTContext &ctx) const;
840-
841-
/// Compare this attribute's version information against the platform or
842-
/// language version (assuming the this attribute pertains to the active
843-
/// platform).
844-
AvailableVersionComparison getVersionAvailability(const ASTContext &ctx) const;
845-
846829
/// Create an AvailableAttr that indicates specific availability
847830
/// for all platforms.
848831
static AvailableAttr *
@@ -3214,6 +3197,34 @@ class SemanticAvailableAttr final {
32143197
/// `PlatformKind::none` if the attribute is not platform specific.
32153198
PlatformKind getPlatformKind() const { return domain.getPlatformKind(); }
32163199

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+
32173228
/// Returns true if this attribute is considered active in the current
32183229
/// compilation context.
32193230
bool isActive(ASTContext &ctx) const;

lib/AST/Attr.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,10 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
830830
if (!semanticAttr)
831831
continue;
832832

833-
auto domain = semanticAttr->getDomain();
834833
if (isShortAvailable(*semanticAttr)) {
835-
if (domain.isSwiftLanguage())
834+
if (semanticAttr->isSwiftLanguageModeSpecific())
836835
swiftVersionAvailableAttribute.emplace(*semanticAttr);
837-
else if (domain.isPackageDescription())
836+
else if (semanticAttr->isPackageDescriptionVersionSpecific())
838837
packageDescriptionVersionAvailableAttribute.emplace(*semanticAttr);
839838
else
840839
shortAvailableAttributes.push_back(*semanticAttr);
@@ -2169,32 +2168,6 @@ OriginallyDefinedInAttr *OriginallyDefinedInAttr::clone(ASTContext &C,
21692168
OriginalModuleName, Platform, MovedVersion, implicit);
21702169
}
21712170

2172-
bool AvailableAttr::isLanguageVersionSpecific() const {
2173-
if (getPlatformAgnosticAvailability() ==
2174-
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific)
2175-
{
2176-
assert(getPlatform() == PlatformKind::none &&
2177-
(Introduced.has_value() ||
2178-
Deprecated.has_value() ||
2179-
Obsoleted.has_value()));
2180-
return true;
2181-
}
2182-
return false;
2183-
}
2184-
2185-
bool AvailableAttr::isPackageDescriptionVersionSpecific() const {
2186-
if (getPlatformAgnosticAvailability() ==
2187-
PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific)
2188-
{
2189-
assert(getPlatform() == PlatformKind::none &&
2190-
(Introduced.has_value() ||
2191-
Deprecated.has_value() ||
2192-
Obsoleted.has_value()));
2193-
return true;
2194-
}
2195-
return false;
2196-
}
2197-
21982171
bool AvailableAttr::isUnconditionallyUnavailable() const {
21992172
switch (getPlatformAgnosticAvailability()) {
22002173
case PlatformAgnosticAvailabilityKind::None:
@@ -2234,8 +2207,9 @@ bool AvailableAttr::isNoAsync() const {
22342207
PlatformAgnosticAvailabilityKind::NoAsync;
22352208
}
22362209

2237-
llvm::VersionTuple AvailableAttr::getActiveVersion(const ASTContext &ctx) const {
2238-
if (isLanguageVersionSpecific()) {
2210+
llvm::VersionTuple
2211+
SemanticAvailableAttr::getActiveVersion(const ASTContext &ctx) const {
2212+
if (isSwiftLanguageModeSpecific()) {
22392213
return ctx.LangOpts.EffectiveLanguageVersion;
22402214
} else if (isPackageDescriptionVersionSpecific()) {
22412215
return ctx.LangOpts.PackageDescriptionVersion;
@@ -2244,32 +2218,32 @@ llvm::VersionTuple AvailableAttr::getActiveVersion(const ASTContext &ctx) const
22442218
}
22452219
}
22462220

2247-
AvailableVersionComparison AvailableAttr::getVersionAvailability(
2248-
const ASTContext &ctx) const {
2221+
AvailableVersionComparison
2222+
SemanticAvailableAttr::getVersionAvailability(const ASTContext &ctx) const {
22492223

22502224
// Unconditional unavailability.
2251-
if (isUnconditionallyUnavailable())
2225+
if (attr->isUnconditionallyUnavailable())
22522226
return AvailableVersionComparison::Unavailable;
22532227

22542228
llvm::VersionTuple queryVersion = getActiveVersion(ctx);
2255-
std::optional<llvm::VersionTuple> ObsoletedVersion = Obsoleted;
2229+
std::optional<llvm::VersionTuple> ObsoletedVersion = attr->Obsoleted;
22562230

2257-
StringRef ObsoletedPlatform = prettyPlatformString();
2231+
StringRef ObsoletedPlatform;
22582232
llvm::VersionTuple RemappedObsoletedVersion;
22592233
if (AvailabilityInference::updateObsoletedPlatformForFallback(
2260-
this, ctx, ObsoletedPlatform, RemappedObsoletedVersion))
2234+
attr, ctx, ObsoletedPlatform, RemappedObsoletedVersion))
22612235
ObsoletedVersion = RemappedObsoletedVersion;
22622236

22632237
// If this entity was obsoleted before or at the query platform version,
22642238
// consider it obsolete.
22652239
if (ObsoletedVersion && *ObsoletedVersion <= queryVersion)
22662240
return AvailableVersionComparison::Obsoleted;
22672241

2268-
std::optional<llvm::VersionTuple> IntroducedVersion = Introduced;
2269-
StringRef IntroducedPlatform = prettyPlatformString();
2242+
std::optional<llvm::VersionTuple> IntroducedVersion = attr->Introduced;
2243+
StringRef IntroducedPlatform;
22702244
llvm::VersionTuple RemappedIntroducedVersion;
22712245
if (AvailabilityInference::updateIntroducedPlatformForFallback(
2272-
this, ctx, IntroducedPlatform, RemappedIntroducedVersion))
2246+
attr, ctx, IntroducedPlatform, RemappedIntroducedVersion))
22732247
IntroducedVersion = RemappedIntroducedVersion;
22742248

22752249
// If this entity was introduced after the query version and we're doing a
@@ -2278,7 +2252,7 @@ AvailableVersionComparison AvailableAttr::getVersionAvailability(
22782252
// static requirement, so we treat "introduced later" as just plain
22792253
// unavailable.
22802254
if (IntroducedVersion && *IntroducedVersion > queryVersion) {
2281-
if (isLanguageVersionSpecific() || isPackageDescriptionVersionSpecific())
2255+
if (isSwiftLanguageModeSpecific() || isPackageDescriptionVersionSpecific())
22822256
return AvailableVersionComparison::Unavailable;
22832257
else
22842258
return AvailableVersionComparison::PotentiallyUnavailable;

lib/AST/Availability.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ const AvailableAttr *Decl::getDeprecatedAttr() const {
537537

538538
std::optional<llvm::VersionTuple> deprecatedVersion = attr->Deprecated;
539539

540-
StringRef deprecatedPlatform = attr->prettyPlatformString();
540+
StringRef deprecatedPlatform;
541541
llvm::VersionTuple remappedDeprecatedVersion;
542542
if (AvailabilityInference::updateDeprecatedPlatformForFallback(
543543
attr, ctx, deprecatedPlatform, remappedDeprecatedVersion))
@@ -546,7 +546,7 @@ const AvailableAttr *Decl::getDeprecatedAttr() const {
546546
if (!deprecatedVersion.has_value())
547547
continue;
548548

549-
llvm::VersionTuple minVersion = attr->getActiveVersion(ctx);
549+
llvm::VersionTuple minVersion = semanticAttr.getActiveVersion(ctx);
550550

551551
// We treat the declaration as deprecated if it is deprecated on
552552
// all deployment targets.
@@ -573,7 +573,7 @@ const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
573573
if (!deprecatedVersion.has_value())
574574
continue;
575575

576-
llvm::VersionTuple activeVersion = attr->getActiveVersion(ctx);
576+
llvm::VersionTuple activeVersion = semanticAttr.getActiveVersion(ctx);
577577

578578
if (deprecatedVersion.value() > activeVersion)
579579
result = attr;
@@ -618,10 +618,8 @@ bool Decl::isUnavailableInCurrentSwiftVersion() const {
618618
llvm::VersionTuple vers = getASTContext().LangOpts.EffectiveLanguageVersion;
619619
for (auto semanticAttr :
620620
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
621-
auto attr = semanticAttr.getParsedAttr();
622-
auto domain = semanticAttr.getDomain();
623-
624-
if (domain.isSwiftLanguage()) {
621+
if (semanticAttr.isSwiftLanguageModeSpecific()) {
622+
auto attr = semanticAttr.getParsedAttr();
625623
if (attr->Introduced.has_value() && attr->Introduced.value() > vers)
626624
return true;
627625
if (attr->Obsoleted.has_value() && attr->Obsoleted.value() <= vers)
@@ -656,7 +654,7 @@ getDeclUnavailableAttr(const Decl *D, bool ignoreAppExtensions) {
656654
if (attr->isUnconditionallyUnavailable())
657655
return attr;
658656

659-
switch (attr->getVersionAvailability(ctx)) {
657+
switch (semanticAttr.getVersionAvailability(ctx)) {
660658
case AvailableVersionComparison::Available:
661659
case AvailableVersionComparison::PotentiallyUnavailable:
662660
break;

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,10 +2171,12 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
21712171
if (SF && SF->Kind == SourceFileKind::Interface)
21722172
return;
21732173

2174-
// The remaining diagnostics are only for attributes that are active for the
2175-
// current target triple.
2176-
if (!attr->isActivePlatform(Ctx) && !attr->isLanguageVersionSpecific() &&
2177-
!attr->isPackageDescriptionVersionSpecific())
2174+
// The remaining diagnostics are only for attributes that are active.
2175+
auto semanticAttr = D->getSemanticAvailableAttr(attr);
2176+
if (!semanticAttr)
2177+
return;
2178+
2179+
if (!semanticAttr->isActive(Ctx))
21782180
return;
21792181

21802182
// Make sure there isn't a more specific attribute we should be using instead.
@@ -2195,7 +2197,7 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
21952197
}
21962198

21972199
SourceLoc attrLoc = attr->getLocation();
2198-
auto versionAvailability = attr->getVersionAvailability(Ctx);
2200+
auto versionAvailability = semanticAttr->getVersionAvailability(Ctx);
21992201
if (versionAvailability == AvailableVersionComparison::Obsoleted ||
22002202
versionAvailability == AvailableVersionComparison::Unavailable) {
22012203
if (auto cannotBeUnavailable =

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,14 +3067,14 @@ getExplicitUnavailabilityDiagnosticInfo(const Decl *decl,
30673067

30683068
ASTContext &ctx = decl->getASTContext();
30693069

3070-
switch (attr->getVersionAvailability(ctx)) {
3070+
switch (semanticAttr->getVersionAvailability(ctx)) {
30713071
case AvailableVersionComparison::Available:
30723072
case AvailableVersionComparison::PotentiallyUnavailable:
30733073
llvm_unreachable("These aren't considered unavailable");
30743074

30753075
case AvailableVersionComparison::Unavailable:
3076-
if ((attr->isLanguageVersionSpecific() ||
3077-
attr->isPackageDescriptionVersionSpecific()) &&
3076+
if ((semanticAttr->isSwiftLanguageModeSpecific() ||
3077+
semanticAttr->isPackageDescriptionVersionSpecific()) &&
30783078
attr->Introduced.has_value()) {
30793079
return UnavailabilityDiagnosticInfo(
30803080
UnavailabilityDiagnosticInfo::Status::IntroducedInVersion, attr,
@@ -3161,18 +3161,22 @@ swift::getUnsatisfiedAvailabilityConstraint(
31613161
return std::nullopt;
31623162

31633163
if (auto attr = decl->getUnavailableAttr()) {
3164+
auto semanticAttr = decl->getSemanticAvailableAttr(attr);
3165+
if (!semanticAttr)
3166+
return std::nullopt;
3167+
31643168
if (isInsideCompatibleUnavailableDeclaration(decl, availabilityContext,
31653169
attr))
31663170
return std::nullopt;
31673171

3168-
switch (attr->getVersionAvailability(ctx)) {
3172+
switch (semanticAttr->getVersionAvailability(ctx)) {
31693173
case AvailableVersionComparison::Available:
31703174
case AvailableVersionComparison::PotentiallyUnavailable:
31713175
llvm_unreachable("Decl should be unavailable");
31723176

31733177
case AvailableVersionComparison::Unavailable:
3174-
if ((attr->isLanguageVersionSpecific() ||
3175-
attr->isPackageDescriptionVersionSpecific()) &&
3178+
if ((semanticAttr->isSwiftLanguageModeSpecific() ||
3179+
semanticAttr->isPackageDescriptionVersionSpecific()) &&
31763180
attr->Introduced.has_value())
31773181
return AvailabilityConstraint::forRequiresVersion(attr);
31783182

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,18 @@ bool swift::isOverrideBasedOnType(const ValueDecl *decl, Type declTy,
243243
static bool isUnavailableInAllVersions(ValueDecl *decl) {
244244
ASTContext &ctx = decl->getASTContext();
245245
auto *attr = decl->getUnavailableAttr();
246-
247246
if (!attr)
248247
return false;
248+
249+
auto semanticAttr = decl->getSemanticAvailableAttr(attr);
250+
if (!semanticAttr)
251+
return false;
252+
249253
if (attr->isUnconditionallyUnavailable())
250254
return true;
251255

252-
return attr->getVersionAvailability(ctx)
253-
== AvailableVersionComparison::Unavailable;
256+
return semanticAttr->getVersionAvailability(ctx) ==
257+
AvailableVersionComparison::Unavailable;
254258
}
255259

256260
/// Perform basic checking to determine whether a declaration can override a

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -916,13 +916,11 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current,
916916
static AvailabilityRange from(const ValueDecl *VD) {
917917
AvailabilityRange result;
918918
for (auto semanticAttr : VD->getSemanticAvailableAttrs()) {
919-
auto attr = semanticAttr.getParsedAttr();
920-
auto domain = semanticAttr.getDomain();
921-
if (domain.isSwiftLanguage()) {
922-
if (attr->Introduced)
923-
result.introduced = attr->Introduced;
924-
if (attr->Obsoleted)
925-
result.obsoleted = attr->Obsoleted;
919+
if (semanticAttr.isSwiftLanguageModeSpecific()) {
920+
if (auto introduced = semanticAttr.getParsedAttr()->Introduced)
921+
result.introduced = introduced;
922+
if (auto obsoleted = semanticAttr.getParsedAttr()->Obsoleted)
923+
result.obsoleted = obsoleted;
926924
}
927925
}
928926
return result;

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,10 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30443044

30453045
assert(theAttr->Rename.empty() || !theAttr->hasCachedRenamedDecl());
30463046

3047+
bool isPackageDescriptionVersionSpecific =
3048+
theAttr->getPlatformAgnosticAvailability() ==
3049+
PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific;
3050+
30473051
llvm::SmallString<32> blob;
30483052
blob.append(theAttr->Message);
30493053
blob.append(theAttr->Rename);
@@ -3054,7 +3058,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30543058
theAttr->isUnconditionallyUnavailable(),
30553059
theAttr->isUnconditionallyDeprecated(),
30563060
theAttr->isNoAsync(),
3057-
theAttr->isPackageDescriptionVersionSpecific(),
3061+
isPackageDescriptionVersionSpecific,
30583062
theAttr->isSPI(),
30593063
theAttr->isForEmbedded(),
30603064
LIST_VER_TUPLE_PIECES(Introduced),

0 commit comments

Comments
 (0)