Skip to content

Commit c5398e1

Browse files
committed
AST: Introduce Decl::getDeprecatedAttr().
It replaces `DeclAttr::getDeprecated()` as the designated way to query for the attribute that makes a decl deprecated.
1 parent 3e50a90 commit c5398e1

File tree

12 files changed

+93
-113
lines changed

12 files changed

+93
-113
lines changed

include/swift/AST/Attr.h

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

2928-
bool isDeprecated(const ASTContext &ctx) const {
2929-
return getDeprecated(ctx) != nullptr;
2930-
}
2931-
29322928
/// Determine whether there is a swiftVersionSpecific attribute that's
29332929
/// unavailable relative to the provided language version.
29342930
bool
@@ -2940,14 +2936,6 @@ class DeclAttributes {
29402936
findMostSpecificActivePlatform(const ASTContext &ctx,
29412937
bool ignoreAppExtensions = false) const;
29422938

2943-
/// Returns the first @available attribute that indicates
2944-
/// a declaration is deprecated on all deployment targets, or null otherwise.
2945-
const AvailableAttr *getDeprecated(const ASTContext &ctx) const;
2946-
2947-
/// Returns the first @available attribute that indicates
2948-
/// a declaration will be deprecated in the future, or null otherwise.
2949-
const AvailableAttr *getSoftDeprecated(const ASTContext &ctx) const;
2950-
29512939
/// Returns the first @available attribute that indicates
29522940
/// a declaration is unavailable from asynchronous contexts, or null
29532941
/// otherwise.

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,18 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
13901390
/// and its DeclContext does not.
13911391
bool isOutermostPrivateOrFilePrivateScope() const;
13921392

1393+
/// Returns true if the declaration is deprecated at the current deployment
1394+
/// target.
1395+
bool isDeprecated() const { return getDeprecatedAttr() != nullptr; }
1396+
1397+
/// Returns the first `@available` attribute that indicates that this decl
1398+
/// is deprecated on current deployment target, or `nullptr` otherwise.
1399+
const AvailableAttr *getDeprecatedAttr() const;
1400+
1401+
/// Returns the first `@available` attribute that indicates that this decl
1402+
/// will be deprecated in the future, or `nullptr` otherwise.
1403+
const AvailableAttr *getSoftDeprecatedAttr() const;
1404+
13931405
/// Returns true if the decl is always unavailable in the current compilation
13941406
/// context. For example, the decl could be marked explicitly unavailable on
13951407
/// either the current platform or in the current language mode. Returns false

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
14731473
ObjCName(Ctx.getObjcName(D)),
14741474
InitKind(Ctx.getInitKind(D)),
14751475
IsImplicit(D->isImplicit()),
1476-
IsDeprecated(D->getAttrs().isDeprecated(D->getASTContext())),
1476+
IsDeprecated(D->isDeprecated()),
14771477
IsABIPlaceholder(isABIPlaceholderRecursive(D)),
14781478
IsFromExtension(isDeclaredInExtension(D)),
14791479
DeclAttrs(collectDeclAttributes(D)) {

lib/AST/Attr.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -427,87 +427,6 @@ DeclAttributes::findMostSpecificActivePlatform(const ASTContext &ctx,
427427
return bestAttr;
428428
}
429429

430-
const AvailableAttr *
431-
DeclAttributes::getDeprecated(const ASTContext &ctx) const {
432-
const AvailableAttr *conditional = nullptr;
433-
const AvailableAttr *bestActive = findMostSpecificActivePlatform(ctx);
434-
for (auto Attr : *this) {
435-
if (auto AvAttr = dyn_cast<AvailableAttr>(Attr)) {
436-
if (AvAttr->isInvalid())
437-
continue;
438-
439-
if (AvAttr->hasPlatform() &&
440-
(!bestActive || AvAttr != bestActive))
441-
continue;
442-
443-
if (!AvAttr->isActivePlatform(ctx) &&
444-
!AvAttr->isLanguageVersionSpecific() &&
445-
!AvAttr->isPackageDescriptionVersionSpecific())
446-
continue;
447-
448-
// Unconditional deprecated.
449-
if (AvAttr->isUnconditionallyDeprecated())
450-
return AvAttr;
451-
452-
std::optional<llvm::VersionTuple> DeprecatedVersion = AvAttr->Deprecated;
453-
454-
StringRef DeprecatedPlatform = AvAttr->prettyPlatformString();
455-
llvm::VersionTuple RemappedDeprecatedVersion;
456-
if (AvailabilityInference::updateDeprecatedPlatformForFallback(
457-
AvAttr, ctx, DeprecatedPlatform, RemappedDeprecatedVersion))
458-
DeprecatedVersion = RemappedDeprecatedVersion;
459-
460-
if (!DeprecatedVersion.has_value())
461-
continue;
462-
463-
llvm::VersionTuple MinVersion = AvAttr->getActiveVersion(ctx);
464-
465-
// We treat the declaration as deprecated if it is deprecated on
466-
// all deployment targets.
467-
// Once availability checking is enabled by default, we should
468-
// query the availability scope tree to determine
469-
// whether a declaration is deprecated on all versions
470-
// allowed by the context containing the reference.
471-
if (DeprecatedVersion.value() <= MinVersion) {
472-
conditional = AvAttr;
473-
}
474-
}
475-
}
476-
return conditional;
477-
}
478-
479-
const AvailableAttr *
480-
DeclAttributes::getSoftDeprecated(const ASTContext &ctx) const {
481-
const AvailableAttr *conditional = nullptr;
482-
const AvailableAttr *bestActive = findMostSpecificActivePlatform(ctx);
483-
for (auto Attr : *this) {
484-
if (auto AvAttr = dyn_cast<AvailableAttr>(Attr)) {
485-
if (AvAttr->isInvalid())
486-
continue;
487-
488-
if (AvAttr->hasPlatform() &&
489-
(!bestActive || AvAttr != bestActive))
490-
continue;
491-
492-
if (!AvAttr->isActivePlatform(ctx) &&
493-
!AvAttr->isLanguageVersionSpecific() &&
494-
!AvAttr->isPackageDescriptionVersionSpecific())
495-
continue;
496-
497-
std::optional<llvm::VersionTuple> DeprecatedVersion = AvAttr->Deprecated;
498-
if (!DeprecatedVersion.has_value())
499-
continue;
500-
501-
llvm::VersionTuple ActiveVersion = AvAttr->getActiveVersion(ctx);
502-
503-
if (DeprecatedVersion.value() > ActiveVersion) {
504-
conditional = AvAttr;
505-
}
506-
}
507-
}
508-
return conditional;
509-
}
510-
511430
const AvailableAttr *DeclAttributes::getNoAsync(const ASTContext &ctx) const {
512431
const AvailableAttr *bestAttr = nullptr;
513432
for (const DeclAttribute *attr : *this) {

lib/AST/Availability.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,74 @@ bool Decl::isAvailableAsSPI() const {
486486
return AvailabilityInference::isAvailableAsSPI(this);
487487
}
488488

489+
const AvailableAttr *Decl::getDeprecatedAttr() const {
490+
auto &ctx = getASTContext();
491+
auto attrs = getAttrs();
492+
const AvailableAttr *result = nullptr;
493+
const AvailableAttr *bestActive = attrs.findMostSpecificActivePlatform(ctx);
494+
495+
for (auto attr :
496+
attrs.getAttributes<AvailableAttr, /*AllowInvalid=*/false>()) {
497+
if (attr->hasPlatform() && (!bestActive || attr != bestActive))
498+
continue;
499+
500+
if (!attr->isActivePlatform(ctx) && !attr->isLanguageVersionSpecific() &&
501+
!attr->isPackageDescriptionVersionSpecific())
502+
continue;
503+
504+
// Unconditional deprecated.
505+
if (attr->isUnconditionallyDeprecated())
506+
return attr;
507+
508+
std::optional<llvm::VersionTuple> deprecatedVersion = attr->Deprecated;
509+
510+
StringRef deprecatedPlatform = attr->prettyPlatformString();
511+
llvm::VersionTuple remappedDeprecatedVersion;
512+
if (AvailabilityInference::updateDeprecatedPlatformForFallback(
513+
attr, ctx, deprecatedPlatform, remappedDeprecatedVersion))
514+
deprecatedVersion = remappedDeprecatedVersion;
515+
516+
if (!deprecatedVersion.has_value())
517+
continue;
518+
519+
llvm::VersionTuple minVersion = attr->getActiveVersion(ctx);
520+
521+
// We treat the declaration as deprecated if it is deprecated on
522+
// all deployment targets.
523+
if (deprecatedVersion.value() <= minVersion) {
524+
result = attr;
525+
}
526+
}
527+
return result;
528+
}
529+
530+
const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
531+
auto &ctx = getASTContext();
532+
auto attrs = getAttrs();
533+
const AvailableAttr *result = nullptr;
534+
const AvailableAttr *bestActive = attrs.findMostSpecificActivePlatform(ctx);
535+
536+
for (auto attr :
537+
attrs.getAttributes<AvailableAttr, /*AllowInvalid=*/false>()) {
538+
if (attr->hasPlatform() && (!bestActive || attr != bestActive))
539+
continue;
540+
541+
if (!attr->isActivePlatform(ctx) && !attr->isLanguageVersionSpecific() &&
542+
!attr->isPackageDescriptionVersionSpecific())
543+
continue;
544+
545+
std::optional<llvm::VersionTuple> deprecatedVersion = attr->Deprecated;
546+
if (!deprecatedVersion.has_value())
547+
continue;
548+
549+
llvm::VersionTuple activeVersion = attr->getActiveVersion(ctx);
550+
551+
if (deprecatedVersion.value() > activeVersion)
552+
result = attr;
553+
}
554+
return result;
555+
}
556+
489557
static const AvailableAttr *
490558
getDeclUnavailableAttr(const Decl *D, bool ignoreAppExtensions) {
491559
auto &ctx = D->getASTContext();

lib/AST/AvailabilityContext.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ bool AvailabilityContext::PlatformInfo::constrainWith(
5252

5353
bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
5454
bool isConstrained = false;
55-
auto &ctx = decl->getASTContext();
5655

5756
if (auto range = AvailabilityInference::annotatedAvailableRange(decl))
5857
isConstrained |= constrainRange(Range, *range);
@@ -63,8 +62,7 @@ bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
6362
CONSTRAIN_BOOL(IsUnavailableInEmbedded, attr->isForEmbedded());
6463
}
6564

66-
isConstrained |=
67-
CONSTRAIN_BOOL(IsDeprecated, decl->getAttrs().isDeprecated(ctx));
65+
isConstrained |= CONSTRAIN_BOOL(IsDeprecated, decl->isDeprecated());
6866

6967
return isConstrained;
7068
}

lib/IDE/CodeCompletionDiagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ bool CodeCompletionDiagnostics::getDiagnosticForDeprecated(
7878
const ValueDecl *D, CodeCompletionDiagnosticSeverity &severity,
7979
llvm::raw_ostream &Out) {
8080
bool isSoftDeprecated = false;
81-
const AvailableAttr *Attr = D->getAttrs().getDeprecated(Ctx);
81+
const AvailableAttr *Attr = D->getDeprecatedAttr();
8282
if (!Attr) {
83-
Attr = D->getAttrs().getSoftDeprecated(Ctx);
83+
Attr = D->getSoftDeprecatedAttr();
8484
isSoftDeprecated = true;
8585
}
8686
if (!Attr)

lib/IDE/CodeCompletionResultBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
218218
CurrentModule = MD;
219219
}
220220

221-
if (D->getAttrs().isDeprecated(D->getASTContext()))
221+
if (D->isDeprecated())
222222
setContextFreeNotRecommended(ContextFreeNotRecommendedReason::Deprecated);
223-
else if (D->getAttrs().getSoftDeprecated(D->getASTContext()))
223+
else if (D->getSoftDeprecatedAttr())
224224
setContextFreeNotRecommended(
225225
ContextFreeNotRecommendedReason::SoftDeprecated);
226226

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,11 +4243,10 @@ findImportedCaseWithMatchingSuffix(Type instanceTy, DeclNameRef name) {
42434243
WORSE(== nullptr);
42444244

42454245
assert((a && b) && "neither should be null here");
4246-
ASTContext &ctx = a->getASTContext();
42474246

42484247
// Is one more available than the other?
42494248
WORSE(->isUnavailable());
4250-
WORSE(->getAttrs().isDeprecated(ctx));
4249+
WORSE(->isDeprecated());
42514250

42524251
// Does one have a shorter name (so the non-matching prefix is shorter)?
42534252
WORSE(->getName().getBaseName().userFacingName().size());

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,13 +2308,13 @@ diagnosePotentialUnavailability(const RootProtocolConformance *rootConf,
23082308
/// declaration is deprecated or null otherwise.
23092309
static const AvailableAttr *getDeprecated(const Decl *D) {
23102310
auto &Ctx = D->getASTContext();
2311-
if (auto *Attr = D->getAttrs().getDeprecated(Ctx))
2311+
if (auto *Attr = D->getDeprecatedAttr())
23122312
return Attr;
23132313

23142314
if (Ctx.LangOpts.WarnSoftDeprecated) {
23152315
// When -warn-soft-deprecated is specified, treat any declaration that is
23162316
// deprecated in the future as deprecated.
2317-
if (auto *Attr = D->getAttrs().getSoftDeprecated(Ctx))
2317+
if (auto *Attr = D->getSoftDeprecatedAttr())
23182318
return Attr;
23192319
}
23202320

0 commit comments

Comments
 (0)