Skip to content

Commit ad73834

Browse files
authored
Merge pull request #77758 from tshortli/available-attr-conveniences
AST: `AvailableAttr` cleanup
2 parents f19c131 + afad02e commit ad73834

19 files changed

+95
-85
lines changed

include/swift/AST/Attr.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ class DeclAttribute : public AttributeBase {
147147
Value : 32
148148
);
149149

150-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 1+1,
150+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 8+8+1+1,
151+
/// A `PlatformKind` value.
152+
Platform : 8,
153+
154+
/// A `PlatformAgnosticAvailabilityKind` value.
155+
PlatformAgnostic : 8,
156+
151157
/// Whether this attribute was spelled `@_spi_available`.
152158
IsSPI : 1,
153159

@@ -700,7 +706,7 @@ enum class AvailableVersionComparison {
700706
};
701707

702708
/// Describes the platform-agnostic availability of a declaration.
703-
enum class PlatformAgnosticAvailabilityKind {
709+
enum class PlatformAgnosticAvailabilityKind : uint8_t {
704710
/// The associated availability attribute is not platform-agnostic.
705711
None,
706712
/// The declaration is deprecated, but can still be used.
@@ -768,12 +774,6 @@ class AvailableAttr : public DeclAttribute {
768774
/// Indicates where the Obsoleted version was specified.
769775
const SourceRange ObsoletedRange;
770776

771-
/// Indicates if the declaration has platform-agnostic availability.
772-
const PlatformAgnosticAvailabilityKind PlatformAgnostic;
773-
774-
/// The platform of the availability.
775-
const PlatformKind Platform;
776-
777777
/// Whether this is a language-version-specific entity.
778778
bool isLanguageVersionSpecific() const;
779779

@@ -795,9 +795,15 @@ class AvailableAttr : public DeclAttribute {
795795
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
796796
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
797797

798+
/// Returns the platform that the attribute applies to (may be `none`).
799+
PlatformKind getPlatform() const {
800+
return static_cast<PlatformKind>(Bits.AvailableAttr.Platform);
801+
}
802+
798803
/// Returns the platform-agnostic availability.
799804
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
800-
return PlatformAgnostic;
805+
return static_cast<PlatformAgnosticAvailabilityKind>(
806+
Bits.AvailableAttr.PlatformAgnostic);
801807
}
802808

803809
/// Determine if a given declaration should be considered unavailable given
@@ -808,18 +814,16 @@ class AvailableAttr : public DeclAttribute {
808814

809815
/// Returns true if the availability applies to a specific
810816
/// platform.
811-
bool hasPlatform() const {
812-
return Platform != PlatformKind::none;
813-
}
817+
bool hasPlatform() const { return getPlatform() != PlatformKind::none; }
814818

815819
/// Returns the string for the platform of the attribute.
816820
StringRef platformString() const {
817-
return swift::platformString(Platform);
821+
return swift::platformString(getPlatform());
818822
}
819823

820824
/// Returns the human-readable string for the platform of the attribute.
821825
StringRef prettyPlatformString() const {
822-
return swift::prettyPlatformString(Platform);
826+
return swift::prettyPlatformString(getPlatform());
823827
}
824828

825829
/// Returns true if this attribute is active given the current platform.

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: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ DeclAttributes::isUnavailableInSwiftVersion(
379379
if (available->isInvalid())
380380
continue;
381381

382-
if (available->getPlatformAgnosticAvailability() ==
383-
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific) {
382+
if (available->isLanguageVersionSpecific()) {
384383
if (available->Introduced.has_value() &&
385384
available->Introduced.value() > vers)
386385
return true;
@@ -413,13 +412,14 @@ DeclAttributes::findMostSpecificActivePlatform(const ASTContext &ctx,
413412
if (!avAttr->isActivePlatform(ctx))
414413
continue;
415414

416-
if (ignoreAppExtensions && isApplicationExtensionPlatform(avAttr->Platform))
415+
if (ignoreAppExtensions &&
416+
isApplicationExtensionPlatform(avAttr->getPlatform()))
417417
continue;
418418

419419
// We have an attribute that is active for the platform, but
420420
// is it more specific than our current best?
421-
if (!bestAttr || inheritsAvailabilityFromPlatform(avAttr->Platform,
422-
bestAttr->Platform)) {
421+
if (!bestAttr || inheritsAvailabilityFromPlatform(
422+
avAttr->getPlatform(), bestAttr->getPlatform())) {
423423
bestAttr = avAttr;
424424
}
425425
}
@@ -452,7 +452,7 @@ DeclAttributes::getUnavailable(const ASTContext &ctx,
452452
continue;
453453

454454
if (ignoreAppExtensions &&
455-
isApplicationExtensionPlatform(AvAttr->Platform))
455+
isApplicationExtensionPlatform(AvAttr->getPlatform()))
456456
continue;
457457

458458
// Unconditional unavailable.
@@ -582,8 +582,8 @@ const AvailableAttr *DeclAttributes::getNoAsync(const ASTContext &ctx) const {
582582
bestAttr = avAttr;
583583
} else if (bestAttr && avAttr->hasPlatform() &&
584584
bestAttr->hasPlatform() &&
585-
inheritsAvailabilityFromPlatform(avAttr->Platform,
586-
bestAttr->Platform)) {
585+
inheritsAvailabilityFromPlatform(avAttr->getPlatform(),
586+
bestAttr->getPlatform())) {
587587
// if they both have a viable platform, use the better one
588588
bestAttr = avAttr;
589589
} else if (avAttr->hasPlatform() && !bestAttr->hasPlatform()) {
@@ -655,7 +655,7 @@ static bool isShortAvailable(const DeclAttribute *DA) {
655655
if (!AvailAttr->Rename.empty())
656656
return false;
657657

658-
switch (AvailAttr->PlatformAgnostic) {
658+
switch (AvailAttr->getPlatformAgnosticAvailability()) {
659659
case PlatformAgnosticAvailabilityKind::Deprecated:
660660
case PlatformAgnosticAvailabilityKind::Unavailable:
661661
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
@@ -682,10 +682,11 @@ static bool isShortFormAvailabilityImpliedByOther(const AvailableAttr *Attr,
682682

683683
for (auto *DA : Others) {
684684
auto *Other = cast<AvailableAttr>(DA);
685-
if (Attr->Platform == Other->Platform)
685+
if (Attr->getPlatform() == Other->getPlatform())
686686
continue;
687687

688-
if (!inheritsAvailabilityFromPlatform(Attr->Platform, Other->Platform))
688+
if (!inheritsAvailabilityFromPlatform(Attr->getPlatform(),
689+
Other->getPlatform()))
689690
continue;
690691

691692
if (Attr->Introduced == Other->Introduced)
@@ -730,7 +731,7 @@ static void printShortFormAvailable(ArrayRef<const DeclAttribute *> Attrs,
730731
if (!Options.IsForSwiftInterface &&
731732
isShortFormAvailabilityImpliedByOther(AvailAttr, Attrs))
732733
continue;
733-
Printer << platformString(AvailAttr->Platform) << " "
734+
Printer << platformString(AvailAttr->getPlatform()) << " "
734735
<< AvailAttr->Introduced.value().getAsString() << ", ";
735736
}
736737
Printer << "*";
@@ -959,7 +960,7 @@ static std::optional<PlatformKind>
959960
referencedPlatform(const DeclAttribute *attr) {
960961
switch (attr->getKind()) {
961962
case DeclAttrKind::Available:
962-
return static_cast<const AvailableAttr *>(attr)->Platform;
963+
return static_cast<const AvailableAttr *>(attr)->getPlatform();
963964
case DeclAttrKind::BackDeployed:
964965
return static_cast<const BackDeployedAttr *>(attr)->Platform;
965966
case DeclAttrKind::OriginallyDefinedIn:
@@ -2232,15 +2233,16 @@ AvailableAttr::AvailableAttr(
22322233
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
22332234
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
22342235
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
2235-
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
2236-
PlatformAgnostic(PlatformAgnostic), Platform(Platform) {
2236+
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange) {
2237+
Bits.AvailableAttr.Platform = static_cast<uint8_t>(Platform);
2238+
Bits.AvailableAttr.PlatformAgnostic = static_cast<uint8_t>(PlatformAgnostic);
22372239
Bits.AvailableAttr.IsSPI = IsSPI;
22382240

22392241
if (IsForEmbedded) {
22402242
// FIXME: The IsForEmbedded bit should be removed when library availability
22412243
// conditions are implemented (rdar://138802876)
22422244
Bits.AvailableAttr.IsForEmbedded = true;
2243-
assert(Platform == PlatformKind::none);
2245+
assert(getPlatform() == PlatformKind::none);
22442246
}
22452247
}
22462248

@@ -2277,7 +2279,7 @@ AvailableAttr *AvailableAttr::createForAlternative(
22772279
}
22782280

22792281
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
2280-
return isPlatformActive(Platform, ctx.LangOpts);
2282+
return isPlatformActive(getPlatform(), ctx.LangOpts);
22812283
}
22822284

22832285
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
@@ -2288,14 +2290,14 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
22882290
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
22892291
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
22902292
implicit ? SourceRange() : getRange(),
2291-
Platform, Message, Rename, RenameDecl,
2293+
getPlatform(), Message, Rename, RenameDecl,
22922294
Introduced ? *Introduced : llvm::VersionTuple(),
22932295
implicit ? SourceRange() : IntroducedRange,
22942296
Deprecated ? *Deprecated : llvm::VersionTuple(),
22952297
implicit ? SourceRange() : DeprecatedRange,
22962298
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
22972299
implicit ? SourceRange() : ObsoletedRange,
2298-
PlatformAgnostic,
2300+
getPlatformAgnosticAvailability(),
22992301
implicit,
23002302
isSPI(),
23012303
isForEmbedded());
@@ -2330,10 +2332,10 @@ OriginallyDefinedInAttr *OriginallyDefinedInAttr::clone(ASTContext &C,
23302332
}
23312333

23322334
bool AvailableAttr::isLanguageVersionSpecific() const {
2333-
if (PlatformAgnostic ==
2335+
if (getPlatformAgnosticAvailability() ==
23342336
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific)
23352337
{
2336-
assert(Platform == PlatformKind::none &&
2338+
assert(getPlatform() == PlatformKind::none &&
23372339
(Introduced.has_value() ||
23382340
Deprecated.has_value() ||
23392341
Obsoleted.has_value()));
@@ -2343,10 +2345,10 @@ bool AvailableAttr::isLanguageVersionSpecific() const {
23432345
}
23442346

23452347
bool AvailableAttr::isPackageDescriptionVersionSpecific() const {
2346-
if (PlatformAgnostic ==
2348+
if (getPlatformAgnosticAvailability() ==
23472349
PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific)
23482350
{
2349-
assert(Platform == PlatformKind::none &&
2351+
assert(getPlatform() == PlatformKind::none &&
23502352
(Introduced.has_value() ||
23512353
Deprecated.has_value() ||
23522354
Obsoleted.has_value()));
@@ -2356,7 +2358,7 @@ bool AvailableAttr::isPackageDescriptionVersionSpecific() const {
23562358
}
23572359

23582360
bool AvailableAttr::isUnconditionallyUnavailable() const {
2359-
switch (PlatformAgnostic) {
2361+
switch (getPlatformAgnosticAvailability()) {
23602362
case PlatformAgnosticAvailabilityKind::None:
23612363
case PlatformAgnosticAvailabilityKind::Deprecated:
23622364
case PlatformAgnosticAvailabilityKind::SwiftVersionSpecific:
@@ -2373,7 +2375,7 @@ bool AvailableAttr::isUnconditionallyUnavailable() const {
23732375
}
23742376

23752377
bool AvailableAttr::isUnconditionallyDeprecated() const {
2376-
switch (PlatformAgnostic) {
2378+
switch (getPlatformAgnosticAvailability()) {
23772379
case PlatformAgnosticAvailabilityKind::None:
23782380
case PlatformAgnosticAvailabilityKind::Unavailable:
23792381
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
@@ -2390,7 +2392,8 @@ bool AvailableAttr::isUnconditionallyDeprecated() const {
23902392
}
23912393

23922394
bool AvailableAttr::isNoAsync() const {
2393-
return PlatformAgnostic == PlatformAgnosticAvailabilityKind::NoAsync;
2395+
return getPlatformAgnosticAvailability() ==
2396+
PlatformAgnosticAvailabilityKind::NoAsync;
23942397
}
23952398

23962399
llvm::VersionTuple AvailableAttr::getActiveVersion(const ASTContext &ctx) const {

0 commit comments

Comments
 (0)