Skip to content

Commit 5143382

Browse files
committed
AST: Introduce Decl::isUnavailableInCurrentSwiftVersion().
1 parent c5398e1 commit 5143382

File tree

6 files changed

+23
-42
lines changed

6 files changed

+23
-42
lines changed

include/swift/AST/Attr.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,11 +2925,6 @@ class DeclAttributes {
29252925
return UnaryOperatorKind::None;
29262926
}
29272927

2928-
/// Determine whether there is a swiftVersionSpecific attribute that's
2929-
/// unavailable relative to the provided language version.
2930-
bool
2931-
isUnavailableInSwiftVersion(const version::Version &effectiveVersion) const;
2932-
29332928
/// Finds the most-specific platform-specific attribute that is
29342929
/// active for the current platform.
29352930
const AvailableAttr *

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14021402
/// will be deprecated in the future, or `nullptr` otherwise.
14031403
const AvailableAttr *getSoftDeprecatedAttr() const;
14041404

1405+
/// Returns true if the decl has been marked unavailable in the Swift language
1406+
/// version that is currently active.
1407+
bool isUnavailableInCurrentSwiftVersion() const;
1408+
14051409
/// Returns true if the decl is always unavailable in the current compilation
14061410
/// context. For example, the decl could be marked explicitly unavailable on
14071411
/// either the current platform or in the current language mode. Returns false

lib/AST/Attr.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -370,29 +370,6 @@ DeclAttribute *DeclAttribute::clone(ASTContext &ctx) const {
370370
}
371371
}
372372

373-
bool
374-
DeclAttributes::isUnavailableInSwiftVersion(
375-
const version::Version &effectiveVersion) const {
376-
llvm::VersionTuple vers = effectiveVersion;
377-
for (auto attr : *this) {
378-
if (auto available = dyn_cast<AvailableAttr>(attr)) {
379-
if (available->isInvalid())
380-
continue;
381-
382-
if (available->isLanguageVersionSpecific()) {
383-
if (available->Introduced.has_value() &&
384-
available->Introduced.value() > vers)
385-
return true;
386-
if (available->Obsoleted.has_value() &&
387-
available->Obsoleted.value() <= vers)
388-
return true;
389-
}
390-
}
391-
}
392-
393-
return false;
394-
}
395-
396373
const AvailableAttr *
397374
DeclAttributes::findMostSpecificActivePlatform(const ASTContext &ctx,
398375
bool ignoreAppExtensions) const {

lib/AST/Availability.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,21 @@ const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
554554
return result;
555555
}
556556

557+
bool Decl::isUnavailableInCurrentSwiftVersion() const {
558+
llvm::VersionTuple vers = getASTContext().LangOpts.EffectiveLanguageVersion;
559+
for (auto attr :
560+
getAttrs().getAttributes<AvailableAttr, /*AllowInvalid=*/false>()) {
561+
if (attr->isLanguageVersionSpecific()) {
562+
if (attr->Introduced.has_value() && attr->Introduced.value() > vers)
563+
return true;
564+
if (attr->Obsoleted.has_value() && attr->Obsoleted.value() <= vers)
565+
return true;
566+
}
567+
}
568+
569+
return false;
570+
}
571+
557572
static const AvailableAttr *
558573
getDeclUnavailableAttr(const Decl *D, bool ignoreAppExtensions) {
559574
auto &ctx = D->getASTContext();

lib/AST/UnqualifiedLookup.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,7 @@ void UnqualifiedLookupFactory::setAsideUnavailableResults(
453453
// Predicate that determines whether a lookup result should
454454
// be unavailable except as a last-ditch effort.
455455
auto unavailableLookupResult = [&](const LookupResultEntry &result) {
456-
auto &effectiveVersion = Ctx.LangOpts.EffectiveLanguageVersion;
457-
return result.getValueDecl()->getAttrs().isUnavailableInSwiftVersion(
458-
effectiveVersion);
456+
return result.getValueDecl()->isUnavailableInCurrentSwiftVersion();
459457
};
460458

461459
// If all of the results we found are unavailable, keep looking.

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5079,15 +5079,12 @@ namespace {
50795079
bool hasKnownSwiftName, ModuleDecl *module,
50805080
bool allowObjCMismatchFallback,
50815081
bool cacheResult) {
5082-
const auto &languageVersion =
5083-
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
5084-
50855082
auto isMatch = [&](const T *singleResult, bool baseNameMatches,
50865083
bool allowObjCMismatch) -> bool {
50875084
const DeclAttributes &attrs = singleResult->getAttrs();
50885085

50895086
// Skip versioned variants.
5090-
if (attrs.isUnavailableInSwiftVersion(languageVersion))
5087+
if (singleResult->isUnavailableInCurrentSwiftVersion())
50915088
return false;
50925089

50935090
// If Clang decl has a custom Swift name, then we know that the name we
@@ -7692,11 +7689,9 @@ void SwiftDeclConverter::importMirroredProtocolMembers(
76927689
if (classImplementsProtocol(superInterface, clangProto, true))
76937690
continue;
76947691

7695-
const auto &languageVersion =
7696-
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
76977692
auto importProtocolRequirement = [&](Decl *member) {
76987693
// Skip compatibility stubs; there's no reason to mirror them.
7699-
if (member->getAttrs().isUnavailableInSwiftVersion(languageVersion))
7694+
if (member->isUnavailableInCurrentSwiftVersion())
77007695
return;
77017696

77027697
if (auto prop = dyn_cast<VarDecl>(member)) {
@@ -8002,9 +7997,6 @@ void SwiftDeclConverter::importInheritedConstructors(
80027997
if (curObjCClass->hasDesignatedInitializers())
80037998
kind = CtorInitializerKind::Convenience;
80047999

8005-
const auto &languageVersion =
8006-
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
8007-
80088000
auto members = superclassDecl->lookupDirect(
80098001
DeclBaseName::createConstructor());
80108002

@@ -8014,7 +8006,7 @@ void SwiftDeclConverter::importInheritedConstructors(
80148006
continue;
80158007

80168008
// Don't inherit compatibility stubs.
8017-
if (ctor->getAttrs().isUnavailableInSwiftVersion(languageVersion))
8009+
if (ctor->isUnavailableInCurrentSwiftVersion())
80188010
continue;
80198011

80208012
// Don't inherit (non-convenience) factory initializers.

0 commit comments

Comments
 (0)