Skip to content

Commit 36230cd

Browse files
committed
AST: Use an accessor to get the PlatformKind from an AvailableAttr.
1 parent 8a78496 commit 36230cd

19 files changed

+68
-57
lines changed

include/swift/AST/Attr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,14 @@ class AvailableAttr : public DeclAttribute {
768768
/// Indicates where the Obsoleted version was specified.
769769
const SourceRange ObsoletedRange;
770770

771+
private:
771772
/// Indicates if the declaration has platform-agnostic availability.
772773
const PlatformAgnosticAvailabilityKind PlatformAgnostic;
773774

774-
/// The platform of the availability.
775+
/// The platform that the attribute applies to (may be `none`).
775776
const PlatformKind Platform;
776777

778+
public:
777779
/// Whether this is a language-version-specific entity.
778780
bool isLanguageVersionSpecific() const;
779781

@@ -795,6 +797,9 @@ class AvailableAttr : public DeclAttribute {
795797
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
796798
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
797799

800+
/// Returns the platform that the attribute applies to (may be `none`).
801+
PlatformKind getPlatform() const { return Platform; }
802+
798803
/// Returns the platform-agnostic availability.
799804
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
800805
return PlatformAgnostic;

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ class Parser {
20422042
};
20432043

20442044
/// Parse a comma-separated list of availability specifications. Try to
2045-
/// expand availability macros when /p Source is not a command line macro.
2045+
/// expand availability macros when \p Source is not a command line macro.
20462046
ParserStatus
20472047
parseAvailabilitySpecList(SmallVectorImpl<AvailabilitySpec *> &Specs,
20482048
AvailabilitySpecSource Source);

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,9 @@ static bool isABIPlaceHolder(Decl *D) {
13411341
llvm::SmallSet<PlatformKind, 4> Platforms;
13421342
for (auto *ATT: D->getAttrs()) {
13431343
if (auto *AVA = dyn_cast<AvailableAttr>(ATT)) {
1344-
if (AVA->Platform != PlatformKind::none && AVA->Introduced &&
1344+
if (AVA->getPlatform() != PlatformKind::none && AVA->Introduced &&
13451345
AVA->Introduced->getMajor() == 9999) {
1346-
Platforms.insert(AVA->Platform);
1346+
Platforms.insert(AVA->getPlatform());
13471347
}
13481348
}
13491349
}
@@ -1363,7 +1363,7 @@ StringRef SDKContext::getPlatformIntroVersion(Decl *D, PlatformKind Kind) {
13631363
return StringRef();
13641364
for (auto *ATT: D->getAttrs()) {
13651365
if (auto *AVA = dyn_cast<AvailableAttr>(ATT)) {
1366-
if (AVA->Platform == Kind && AVA->Introduced) {
1366+
if (AVA->getPlatform() == Kind && AVA->Introduced) {
13671367
return buffer(AVA->Introduced->getAsString());
13681368
}
13691369
}

lib/AST/Attr.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,14 @@ DeclAttributes::findMostSpecificActivePlatform(const ASTContext &ctx,
413413
if (!avAttr->isActivePlatform(ctx))
414414
continue;
415415

416-
if (ignoreAppExtensions && isApplicationExtensionPlatform(avAttr->Platform))
416+
if (ignoreAppExtensions &&
417+
isApplicationExtensionPlatform(avAttr->getPlatform()))
417418
continue;
418419

419420
// We have an attribute that is active for the platform, but
420421
// is it more specific than our current best?
421-
if (!bestAttr || inheritsAvailabilityFromPlatform(avAttr->Platform,
422-
bestAttr->Platform)) {
422+
if (!bestAttr || inheritsAvailabilityFromPlatform(
423+
avAttr->getPlatform(), bestAttr->getPlatform())) {
423424
bestAttr = avAttr;
424425
}
425426
}
@@ -452,7 +453,7 @@ DeclAttributes::getUnavailable(const ASTContext &ctx,
452453
continue;
453454

454455
if (ignoreAppExtensions &&
455-
isApplicationExtensionPlatform(AvAttr->Platform))
456+
isApplicationExtensionPlatform(AvAttr->getPlatform()))
456457
continue;
457458

458459
// Unconditional unavailable.
@@ -582,8 +583,8 @@ const AvailableAttr *DeclAttributes::getNoAsync(const ASTContext &ctx) const {
582583
bestAttr = avAttr;
583584
} else if (bestAttr && avAttr->hasPlatform() &&
584585
bestAttr->hasPlatform() &&
585-
inheritsAvailabilityFromPlatform(avAttr->Platform,
586-
bestAttr->Platform)) {
586+
inheritsAvailabilityFromPlatform(avAttr->getPlatform(),
587+
bestAttr->getPlatform())) {
587588
// if they both have a viable platform, use the better one
588589
bestAttr = avAttr;
589590
} else if (avAttr->hasPlatform() && !bestAttr->hasPlatform()) {
@@ -655,7 +656,7 @@ static bool isShortAvailable(const DeclAttribute *DA) {
655656
if (!AvailAttr->Rename.empty())
656657
return false;
657658

658-
switch (AvailAttr->PlatformAgnostic) {
659+
switch (AvailAttr->getPlatformAgnosticAvailability()) {
659660
case PlatformAgnosticAvailabilityKind::Deprecated:
660661
case PlatformAgnosticAvailabilityKind::Unavailable:
661662
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
@@ -682,10 +683,11 @@ static bool isShortFormAvailabilityImpliedByOther(const AvailableAttr *Attr,
682683

683684
for (auto *DA : Others) {
684685
auto *Other = cast<AvailableAttr>(DA);
685-
if (Attr->Platform == Other->Platform)
686+
if (Attr->getPlatform() == Other->getPlatform())
686687
continue;
687688

688-
if (!inheritsAvailabilityFromPlatform(Attr->Platform, Other->Platform))
689+
if (!inheritsAvailabilityFromPlatform(Attr->getPlatform(),
690+
Other->getPlatform()))
689691
continue;
690692

691693
if (Attr->Introduced == Other->Introduced)
@@ -730,7 +732,7 @@ static void printShortFormAvailable(ArrayRef<const DeclAttribute *> Attrs,
730732
if (!Options.IsForSwiftInterface &&
731733
isShortFormAvailabilityImpliedByOther(AvailAttr, Attrs))
732734
continue;
733-
Printer << platformString(AvailAttr->Platform) << " "
735+
Printer << platformString(AvailAttr->getPlatform()) << " "
734736
<< AvailAttr->Introduced.value().getAsString() << ", ";
735737
}
736738
Printer << "*";
@@ -959,7 +961,7 @@ static std::optional<PlatformKind>
959961
referencedPlatform(const DeclAttribute *attr) {
960962
switch (attr->getKind()) {
961963
case DeclAttrKind::Available:
962-
return static_cast<const AvailableAttr *>(attr)->Platform;
964+
return static_cast<const AvailableAttr *>(attr)->getPlatform();
963965
case DeclAttrKind::BackDeployed:
964966
return static_cast<const BackDeployedAttr *>(attr)->Platform;
965967
case DeclAttrKind::OriginallyDefinedIn:

lib/AST/Availability.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ AvailabilityRange AvailabilityRange::forRuntimeTarget(const ASTContext &Ctx) {
6666
}
6767

6868
PlatformKind AvailabilityConstraint::getPlatform() const {
69-
return attr->Platform;
69+
return attr->getPlatform();
7070
}
7171

7272
std::optional<AvailabilityRange>
@@ -94,10 +94,10 @@ bool AvailabilityConstraint::isConditionallySatisfiable() const {
9494
}
9595

9696
bool AvailabilityConstraint::isActiveForRuntimeQueries(ASTContext &ctx) const {
97-
if (attr->Platform == PlatformKind::none)
97+
if (attr->getPlatform() == PlatformKind::none)
9898
return true;
9999

100-
return swift::isPlatformActive(attr->Platform, ctx.LangOpts,
100+
return swift::isPlatformActive(attr->getPlatform(), ctx.LangOpts,
101101
/*forTargetVariant=*/false,
102102
/*forRuntimeQuery=*/true);
103103
}
@@ -220,14 +220,14 @@ void AvailabilityInference::applyInferredAvailableAttrs(
220220
// Skip an attribute from an outer declaration if it is for a platform
221221
// that was already handled implicitly by an attribute from an inner
222222
// declaration.
223-
if (llvm::any_of(MergedAttrs,
224-
[&AvAttr](const AvailableAttr *MergedAttr) {
225-
return inheritsAvailabilityFromPlatform(
226-
AvAttr->Platform, MergedAttr->Platform);
227-
}))
223+
if (llvm::any_of(
224+
MergedAttrs, [&AvAttr](const AvailableAttr *MergedAttr) {
225+
return inheritsAvailabilityFromPlatform(
226+
AvAttr->getPlatform(), MergedAttr->getPlatform());
227+
}))
228228
continue;
229229

230-
mergeWithInferredAvailability(AvAttr, Inferred[AvAttr->Platform]);
230+
mergeWithInferredAvailability(AvAttr, Inferred[AvAttr->getPlatform()]);
231231
PendingAttrs.push_back(AvAttr);
232232

233233
if (Message.empty() && !AvAttr->Message.empty())
@@ -302,12 +302,12 @@ static bool isBetterThan(const AvailableAttr *newAttr,
302302
return true;
303303

304304
// If they belong to the same platform, the one that introduces later wins.
305-
if (prevAttr->Platform == newAttr->Platform)
305+
if (prevAttr->getPlatform() == newAttr->getPlatform())
306306
return prevAttr->Introduced.value() < newAttr->Introduced.value();
307307

308308
// If the new attribute's platform inherits from the old one, it wins.
309-
return inheritsAvailabilityFromPlatform(newAttr->Platform,
310-
prevAttr->Platform);
309+
return inheritsAvailabilityFromPlatform(newAttr->getPlatform(),
310+
prevAttr->getPlatform());
311311
}
312312

313313
static const clang::DarwinSDKInfo::RelatedTargetVersionMapping *
@@ -348,7 +348,8 @@ bool AvailabilityInference::updateIntroducedPlatformForFallback(
348348
const AvailableAttr *attr, const ASTContext &Ctx, llvm::StringRef &Platform,
349349
llvm::VersionTuple &PlatformVer) {
350350
std::optional<llvm::VersionTuple> IntroducedVersion = attr->Introduced;
351-
if (attr->Platform == PlatformKind::iOS && IntroducedVersion.has_value() &&
351+
if (attr->getPlatform() == PlatformKind::iOS &&
352+
IntroducedVersion.has_value() &&
352353
isPlatformActive(PlatformKind::visionOS, Ctx.LangOpts)) {
353354
// We re-map the iOS introduced version to the corresponding visionOS version
354355
auto PotentiallyRemappedIntroducedVersion =
@@ -367,7 +368,8 @@ bool AvailabilityInference::updateDeprecatedPlatformForFallback(
367368
const AvailableAttr *attr, const ASTContext &Ctx, llvm::StringRef &Platform,
368369
llvm::VersionTuple &PlatformVer) {
369370
std::optional<llvm::VersionTuple> DeprecatedVersion = attr->Deprecated;
370-
if (attr->Platform == PlatformKind::iOS && DeprecatedVersion.has_value() &&
371+
if (attr->getPlatform() == PlatformKind::iOS &&
372+
DeprecatedVersion.has_value() &&
371373
isPlatformActive(PlatformKind::visionOS, Ctx.LangOpts)) {
372374
// We re-map the iOS deprecated version to the corresponding visionOS version
373375
auto PotentiallyRemappedDeprecatedVersion =
@@ -386,7 +388,8 @@ bool AvailabilityInference::updateObsoletedPlatformForFallback(
386388
const AvailableAttr *attr, const ASTContext &Ctx, llvm::StringRef &Platform,
387389
llvm::VersionTuple &PlatformVer) {
388390
std::optional<llvm::VersionTuple> ObsoletedVersion = attr->Obsoleted;
389-
if (attr->Platform == PlatformKind::iOS && ObsoletedVersion.has_value() &&
391+
if (attr->getPlatform() == PlatformKind::iOS &&
392+
ObsoletedVersion.has_value() &&
390393
isPlatformActive(PlatformKind::visionOS, Ctx.LangOpts)) {
391394
// We re-map the iOS obsoleted version to the corresponding visionOS version
392395
auto PotentiallyRemappedObsoletedVersion =
@@ -403,7 +406,7 @@ bool AvailabilityInference::updateObsoletedPlatformForFallback(
403406

404407
void AvailabilityInference::updatePlatformStringForFallback(
405408
const AvailableAttr *attr, const ASTContext &Ctx, llvm::StringRef &Platform) {
406-
if (attr->Platform == PlatformKind::iOS &&
409+
if (attr->getPlatform() == PlatformKind::iOS &&
407410
isPlatformActive(PlatformKind::visionOS, Ctx.LangOpts)) {
408411
Platform = swift::prettyPlatformString(PlatformKind::visionOS);
409412
}
@@ -525,7 +528,7 @@ bool Decl::isUnreachableAtRuntime() const {
525528
return false;
526529

527530
// Universally unavailable declarations are always unreachable.
528-
if (unavailableAttr->Platform == PlatformKind::none)
531+
if (unavailableAttr->getPlatform() == PlatformKind::none)
529532
return true;
530533

531534
// FIXME: Support zippered frameworks (rdar://125371621)

lib/AST/AvailabilityContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
5858
isConstrained |= constrainRange(Range, *range);
5959

6060
if (auto *attr = decl->getAttrs().getUnavailable(ctx)) {
61-
isConstrained |= constrainUnavailability(attr->Platform);
61+
isConstrained |= constrainUnavailability(attr->getPlatform());
6262
isConstrained |=
6363
CONSTRAIN_BOOL(IsUnavailableInEmbedded, attr->isForEmbedded());
6464
}

lib/AST/AvailabilityScope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ getAvailabilityConditionVersionSourceRange(const DeclAttributes &DeclAttrs,
301301
for (auto *Attr : DeclAttrs) {
302302
if (auto *AA = dyn_cast<AvailableAttr>(Attr)) {
303303
if (AA->Introduced.has_value() && AA->Introduced.value() == Version &&
304-
AA->Platform == Platform) {
304+
AA->getPlatform() == Platform) {
305305

306306
// More than one: return invalid range.
307307
if (Range.isValid())

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ std::optional<llvm::VersionTuple>
557557
Decl::getIntroducedOSVersion(PlatformKind Kind) const {
558558
for (auto *attr: getAttrs()) {
559559
if (auto *ava = dyn_cast<AvailableAttr>(attr)) {
560-
if (ava->Platform == Kind && ava->Introduced) {
560+
if (ava->getPlatform() == Kind && ava->Introduced) {
561561
return ava->Introduced;
562562
}
563563
}
@@ -9253,7 +9253,8 @@ AbstractFunctionDecl *AbstractFunctionDecl::getAsyncAlternative() const {
92539253
// `getAttrs` is in reverse source order, so the last attribute is the
92549254
// first in source
92559255
if (!attr->Rename.empty() &&
9256-
(attr->Platform == PlatformKind::none || !avAttr) && !attr->isNoAsync()) {
9256+
(attr->getPlatform() == PlatformKind::none || !avAttr) &&
9257+
!attr->isNoAsync()) {
92579258
avAttr = attr;
92589259
}
92599260
}

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ class InheritedProtocolCollector {
456456
// attributes for the same platform; formally they'd need to be merged.
457457
bool alreadyHasMoreSpecificAttrForThisPlatform =
458458
llvm::any_of(*cache, [nextAttr](const AvailableAttr *existingAttr) {
459-
return existingAttr->Platform == nextAttr->Platform;
460-
});
459+
return existingAttr->getPlatform() == nextAttr->getPlatform();
460+
});
461461
if (alreadyHasMoreSpecificAttrForThisPlatform)
462462
continue;
463463
cache->push_back(nextAttr);

lib/IRGen/TBDGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,11 @@ class APIGenRecorder final : public APIRecorder {
773773
auto platform = targetPlatform(module->getASTContext().LangOpts);
774774
for (auto *attr : decl->getAttrs()) {
775775
if (auto *ava = dyn_cast<AvailableAttr>(attr)) {
776-
if (ava->Platform == PlatformKind::none) {
776+
if (ava->getPlatform() == PlatformKind::none) {
777777
hasFallbackUnavailability = ava->isUnconditionallyUnavailable();
778778
continue;
779779
}
780-
if (ava->Platform != platform)
780+
if (ava->getPlatform() != platform)
781781
continue;
782782
unavailable = ava->isUnconditionallyUnavailable();
783783
if (ava->Introduced)

0 commit comments

Comments
 (0)