Skip to content

Commit a4f59f9

Browse files
committed
AST: Return @backDeployed attribute along with version from Decl query.
Also, introduce a convenience on `BackDeployedAttr` for getting its associated `AvailabilityDomain`. NFC.
1 parent 27e6573 commit a4f59f9

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

include/swift/AST/Attr.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ class BackDeployedAttr : public DeclAttribute {
29252925
: DeclAttribute(DeclAttrKind::BackDeployed, AtLoc, Range, Implicit),
29262926
Platform(Platform), Version(Version) {}
29272927

2928-
/// The platform the symbol is available for back deployment on.
2928+
/// The platform the decl is available for back deployment in.
29292929
const PlatformKind Platform;
29302930

29312931
/// The earliest platform version that may use the back deployed implementation.
@@ -2934,6 +2934,12 @@ class BackDeployedAttr : public DeclAttribute {
29342934
/// Returns true if this attribute is active given the current platform.
29352935
bool isActivePlatform(const ASTContext &ctx, bool forTargetVariant) const;
29362936

2937+
/// Returns the `AvailabilityDomain` that the decl is available for back
2938+
/// deployment in.
2939+
AvailabilityDomain getAvailabilityDomain() const {
2940+
return AvailabilityDomain::forPlatform(Platform);
2941+
}
2942+
29372943
static bool classof(const DeclAttribute *DA) {
29382944
return DA->getKind() == DeclAttrKind::BackDeployed;
29392945
}

include/swift/AST/Decl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
11151115
std::optional<llvm::VersionTuple>
11161116
getIntroducedOSVersion(PlatformKind Kind) const;
11171117

1118-
/// Returns the OS version in which the decl became ABI as specified by the
1119-
/// `@backDeployed` attribute.
1120-
std::optional<llvm::VersionTuple>
1121-
getBackDeployedBeforeOSVersion(ASTContext &Ctx,
1122-
bool forTargetVariant = false) const;
1118+
/// Returns the active `@backDeployed` attribute and the `AvailabilityRange`
1119+
/// in which the decl is available as ABI.
1120+
std::optional<std::pair<const BackDeployedAttr *, AvailabilityRange>>
1121+
getBackDeployedAttrAndRange(ASTContext &Ctx,
1122+
bool forTargetVariant = false) const;
11231123

11241124
/// Returns true if the decl has an active `@backDeployed` attribute for the
11251125
/// given context.

lib/AST/Availability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ bool AvailabilityInference::updateBeforeAvailabilityDomainForFallback(
365365
const BackDeployedAttr *attr, const ASTContext &ctx,
366366
AvailabilityDomain &domain, llvm::VersionTuple &platformVer) {
367367
bool hasRemap = false;
368-
auto remappedDomain = AvailabilityDomain::forPlatform(attr->Platform)
369-
.getRemappedDomain(ctx, hasRemap);
368+
auto remappedDomain =
369+
attr->getAvailabilityDomain().getRemappedDomain(ctx, hasRemap);
370370
if (!hasRemap)
371371
return false;
372372

lib/AST/Decl.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,9 @@ Decl::getIntroducedOSVersion(PlatformKind Kind) const {
607607
return std::nullopt;
608608
}
609609

610-
std::optional<llvm::VersionTuple>
611-
Decl::getBackDeployedBeforeOSVersion(ASTContext &Ctx,
612-
bool forTargetVariant) const {
610+
std::optional<std::pair<const BackDeployedAttr *, AvailabilityRange>>
611+
Decl::getBackDeployedAttrAndRange(ASTContext &Ctx,
612+
bool forTargetVariant) const {
613613
if (auto *attr = getAttrs().getBackDeployed(Ctx, forTargetVariant)) {
614614
auto version = attr->Version;
615615
AvailabilityDomain ignoredDomain;
@@ -618,26 +618,25 @@ Decl::getBackDeployedBeforeOSVersion(ASTContext &Ctx,
618618

619619
// If the remap for fallback resulted in 1.0, then the
620620
// backdeployment prior to that is not meaningful.
621-
if (version == clang::VersionTuple(1, 0, 0, 0))
621+
if (version == llvm::VersionTuple(1, 0, 0, 0))
622622
return std::nullopt;
623623

624-
return version;
624+
return std::make_pair(attr, AvailabilityRange(version));
625625
}
626626

627627
// Accessors may inherit `@backDeployed`.
628628
if (auto *AD = dyn_cast<AccessorDecl>(this))
629-
return AD->getStorage()->getBackDeployedBeforeOSVersion(Ctx,
630-
forTargetVariant);
629+
return AD->getStorage()->getBackDeployedAttrAndRange(Ctx, forTargetVariant);
631630

632631
return std::nullopt;
633632
}
634633

635634
bool Decl::isBackDeployed(ASTContext &Ctx) const {
636-
if (getBackDeployedBeforeOSVersion(Ctx))
635+
if (getBackDeployedAttrAndRange(Ctx))
637636
return true;
638637

639638
if (Ctx.LangOpts.TargetVariant) {
640-
if (getBackDeployedBeforeOSVersion(Ctx, /*forTargetVariant=*/true))
639+
if (getBackDeployedAttrAndRange(Ctx, /*forTargetVariant=*/true))
641640
return true;
642641
}
643642

@@ -1463,8 +1462,8 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
14631462

14641463
// When computing availability for linkage, use the "before" version from
14651464
// the @backDeployed attribute, if present.
1466-
if (auto backDeployVersion = getBackDeployedBeforeOSVersion(ctx))
1467-
return AvailabilityRange{*backDeployVersion};
1465+
if (auto backDeployedAttrAndRange = getBackDeployedAttrAndRange(ctx))
1466+
return backDeployedAttrAndRange->second;
14681467

14691468
auto containingContext = AvailabilityInference::annotatedAvailableRange(this);
14701469
if (containingContext.has_value()) {

lib/SILGen/SILGenAvailability.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ static SILValue emitZipperedBackDeployIfAvailableBooleanTestValue(
228228
assert(ctx.LangOpts.TargetVariant);
229229

230230
VersionRange OSVersion = VersionRange::all();
231-
if (auto version = AFD->getBackDeployedBeforeOSVersion(ctx)) {
232-
OSVersion = VersionRange::allGTE(*version);
231+
if (auto attrAndRange = AFD->getBackDeployedAttrAndRange(ctx)) {
232+
OSVersion = attrAndRange->second.getRawVersionRange();
233233
}
234234

235235
VersionRange VariantOSVersion = VersionRange::all();
236-
if (auto version =
237-
AFD->getBackDeployedBeforeOSVersion(ctx, /*forTargetVariant=*/true)) {
238-
VariantOSVersion = VersionRange::allGTE(*version);
236+
if (auto attrAndRange =
237+
AFD->getBackDeployedAttrAndRange(ctx, /*forTargetVariant=*/true)) {
238+
VariantOSVersion = attrAndRange->second.getRawVersionRange();
239239
}
240240

241241
return emitZipperedOSVersionRangeCheck(SGF, loc, OSVersion, VariantOSVersion);
@@ -254,25 +254,26 @@ static void emitBackDeployIfAvailableCondition(SILGenFunction &SGF,
254254
SILLocation loc,
255255
SILBasicBlock *availableBB,
256256
SILBasicBlock *unavailableBB) {
257-
if (SGF.getASTContext().LangOpts.TargetVariant) {
257+
auto &ctx = SGF.getASTContext();
258+
if (ctx.LangOpts.TargetVariant) {
258259
SILValue booleanTestValue =
259260
emitZipperedBackDeployIfAvailableBooleanTestValue(
260261
SGF, AFD, loc, availableBB, unavailableBB);
261262
SGF.B.createCondBranch(loc, booleanTestValue, availableBB, unavailableBB);
262263
return;
263264
}
264265

265-
auto version = AFD->getBackDeployedBeforeOSVersion(SGF.SGM.getASTContext());
266+
auto attrAndRange = AFD->getBackDeployedAttrAndRange(ctx);
266267
VersionRange OSVersion = VersionRange::empty();
267-
if (version.has_value()) {
268-
OSVersion = VersionRange::allGTE(*version);
268+
if (attrAndRange) {
269+
OSVersion = attrAndRange->second.getRawVersionRange();
269270
}
270271

271272
SILValue booleanTestValue;
272273
if (OSVersion.isEmpty() || OSVersion.isAll()) {
273274
// If there's no check for the current platform, this condition is
274275
// trivially true.
275-
SILType i1 = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
276+
SILType i1 = SILType::getBuiltinIntegerType(1, ctx);
276277
booleanTestValue = SGF.B.createIntegerLiteral(loc, i1, 1);
277278
} else {
278279
bool isMacCatalyst =
@@ -434,8 +435,8 @@ SILGenFunction::emitIfAvailableQuery(SILLocation loc,
434435
bool SILGenModule::requiresBackDeploymentThunk(ValueDecl *decl,
435436
ResilienceExpansion expansion) {
436437
auto &ctx = getASTContext();
437-
auto backDeployBeforeVersion = decl->getBackDeployedBeforeOSVersion(ctx);
438-
if (!backDeployBeforeVersion)
438+
auto attrAndRange = decl->getBackDeployedAttrAndRange(ctx);
439+
if (!attrAndRange)
439440
return false;
440441

441442
switch (expansion) {
@@ -453,8 +454,7 @@ bool SILGenModule::requiresBackDeploymentThunk(ValueDecl *decl,
453454
// high enough that the ABI implementation of the back deployed declaration is
454455
// guaranteed to be available.
455456
auto deploymentAvailability = AvailabilityRange::forDeploymentTarget(ctx);
456-
auto declAvailability = AvailabilityRange(*backDeployBeforeVersion);
457-
if (deploymentAvailability.isContainedIn(declAvailability))
457+
if (deploymentAvailability.isContainedIn(attrAndRange->second))
458458
return false;
459459

460460
return true;

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,8 +5129,8 @@ void AttributeChecker::checkBackDeployedAttrs(
51295129
std::map<PlatformKind, SourceLoc> seenPlatforms;
51305130

51315131
const BackDeployedAttr *ActiveAttr = nullptr;
5132-
if (D->getBackDeployedBeforeOSVersion(Ctx))
5133-
ActiveAttr = D->getAttrs().getBackDeployed(Ctx, false);
5132+
if (auto AttrAndRange = D->getBackDeployedAttrAndRange(Ctx))
5133+
ActiveAttr = AttrAndRange->first;
51345134

51355135
for (auto *Attr : Attrs) {
51365136
// Back deployment only makes sense for public declarations.
@@ -5221,7 +5221,7 @@ void AttributeChecker::checkBackDeployedAttrs(
52215221
D->getLoc(), D->getInnermostDeclContext());
52225222

52235223
// Unavailable decls cannot be back deployed.
5224-
auto backDeployedDomain = AvailabilityDomain::forPlatform(Attr->Platform);
5224+
auto backDeployedDomain = Attr->getAvailabilityDomain();
52255225
if (availability.containsUnavailableDomain(backDeployedDomain)) {
52265226
auto domainForDiagnostics = backDeployedDomain;
52275227
llvm::VersionTuple ignoredVersion;
@@ -5254,7 +5254,7 @@ void AttributeChecker::checkBackDeployedAttrs(
52545254
// fallback could never be executed at runtime.
52555255
if (auto availableRangeAttrPair =
52565256
getSemanticAvailableRangeDeclAndAttr(VD)) {
5257-
auto beforeDomain = AvailabilityDomain::forPlatform(Attr->Platform);
5257+
auto beforeDomain = Attr->getAvailabilityDomain();
52585258
auto beforeVersion = Attr->Version;
52595259
auto availableAttr = availableRangeAttrPair.value().first;
52605260
auto introVersion = availableAttr.getIntroduced().value();

0 commit comments

Comments
 (0)