Skip to content

Commit 2a16797

Browse files
committed
AST: Use AvailabilityDomain and Kind directly in the AvailableAttr representation.
1 parent 58a1c23 commit 2a16797

File tree

2 files changed

+58
-98
lines changed

2 files changed

+58
-98
lines changed

include/swift/AST/Attr.h

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,9 @@ class DeclAttribute : public AttributeBase {
151151
Value : 32
152152
);
153153

154-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 8+8+1+1+1+1,
155-
/// A `PlatformKind` value.
156-
Platform : 8,
157-
158-
/// A `PlatformAgnosticAvailabilityKind` value.
159-
PlatformAgnostic : 8,
154+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 4+1+1+1+1,
155+
/// An `AvailableAttr::Kind` value.
156+
Kind : 4,
160157

161158
/// State storage for `RenamedDeclRequest`.
162159
HasComputedRenamedDecl : 1,
@@ -742,6 +739,8 @@ enum class PlatformAgnosticAvailabilityKind : uint8_t {
742739

743740
/// Defines the @available attribute.
744741
class AvailableAttr : public DeclAttribute {
742+
AvailabilityDomain Domain;
743+
745744
public:
746745
enum class Kind : uint8_t {
747746
/// The attribute does not specify `deprecated`, `unavailable`,
@@ -756,17 +755,6 @@ class AvailableAttr : public DeclAttribute {
756755
NoAsync,
757756
};
758757

759-
public:
760-
AvailableAttr(SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
761-
StringRef Message, StringRef Rename,
762-
const llvm::VersionTuple &Introduced,
763-
SourceRange IntroducedRange,
764-
const llvm::VersionTuple &Deprecated,
765-
SourceRange DeprecatedRange,
766-
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
767-
PlatformAgnosticAvailabilityKind PlatformAgnostic,
768-
bool Implicit, bool IsSPI, bool IsForEmbedded = false);
769-
770758
AvailableAttr(SourceLoc AtLoc, SourceRange Range,
771759
const AvailabilityDomain &Domain, Kind Kind, StringRef Message,
772760
StringRef Rename, const llvm::VersionTuple &Introduced,
@@ -826,31 +814,47 @@ class AvailableAttr : public DeclAttribute {
826814
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
827815

828816
/// Returns the platform that the attribute applies to (may be `none`).
829-
PlatformKind getPlatform() const {
830-
return static_cast<PlatformKind>(Bits.AvailableAttr.Platform);
831-
}
817+
PlatformKind getPlatform() const { return Domain.getPlatformKind(); }
832818

833-
/// Returns the platform-agnostic availability.
834-
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
835-
return static_cast<PlatformAgnosticAvailabilityKind>(
836-
Bits.AvailableAttr.PlatformAgnostic);
837-
}
819+
/// Returns the `AvailabilityDomain` associated with the attribute, or
820+
/// `std::nullopt` if it has either not yet been resolved or could not be
821+
/// resolved successfully.
822+
std::optional<AvailabilityDomain> getCachedDomain() const { return Domain; }
838823

839824
/// Returns the kind of availability the attribute specifies.
840-
Kind getKind() const {
841-
switch (getPlatformAgnosticAvailability()) {
842-
case PlatformAgnosticAvailabilityKind::None:
843-
case PlatformAgnosticAvailabilityKind::SwiftVersionSpecific:
844-
case PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific:
845-
return Kind::Default;
846-
case PlatformAgnosticAvailabilityKind::Deprecated:
847-
return Kind::Deprecated;
848-
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
849-
case PlatformAgnosticAvailabilityKind::Unavailable:
850-
return Kind::Unavailable;
851-
case PlatformAgnosticAvailabilityKind::NoAsync:
852-
return Kind::NoAsync;
825+
Kind getKind() const { return static_cast<Kind>(Bits.AvailableAttr.Kind); }
826+
827+
/// Returns the platform-agnostic availability.
828+
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
829+
// FIXME: [availability] Remove this method entirely.
830+
switch (getKind()) {
831+
case Kind::Default:
832+
if (Domain.isSwiftLanguage())
833+
return PlatformAgnosticAvailabilityKind::SwiftVersionSpecific;
834+
else if (Domain.isPackageDescription())
835+
return PlatformAgnosticAvailabilityKind::
836+
PackageDescriptionVersionSpecific;
837+
else if (Domain.isUniversal() || Domain.isPlatform())
838+
return PlatformAgnosticAvailabilityKind::None;
839+
840+
break;
841+
842+
case Kind::Deprecated:
843+
return PlatformAgnosticAvailabilityKind::Deprecated;
844+
845+
case Kind::Unavailable:
846+
if (Domain.isSwiftLanguage())
847+
return PlatformAgnosticAvailabilityKind::UnavailableInSwift;
848+
else if (Domain.isUniversal() || Domain.isPlatform())
849+
return PlatformAgnosticAvailabilityKind::Unavailable;
850+
851+
break;
852+
853+
case Kind::NoAsync:
854+
return PlatformAgnosticAvailabilityKind::NoAsync;
853855
}
856+
857+
llvm_unreachable("unsupported AvailabilityDomain and Kind");
854858
}
855859

856860
/// Create an `AvailableAttr` that specifies universal unavailability, e.g.

lib/AST/Attr.cpp

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,20 +2106,18 @@ Type RawLayoutAttr::getResolvedCountType(StructDecl *sd) const {
21062106
#define INIT_VER_TUPLE(X) X(X.empty() ? std::optional<llvm::VersionTuple>() : X)
21072107

21082108
AvailableAttr::AvailableAttr(
2109-
SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
2110-
StringRef Message, StringRef Rename,
2109+
SourceLoc AtLoc, SourceRange Range, const AvailabilityDomain &Domain,
2110+
Kind Kind, StringRef Message, StringRef Rename,
21112111
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
21122112
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
21132113
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
2114-
PlatformAgnosticAvailabilityKind PlatformAgnostic, bool Implicit,
2115-
bool IsSPI, bool IsForEmbedded)
2114+
bool Implicit, bool IsSPI, bool IsForEmbedded)
21162115
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
2117-
Message(Message), Rename(Rename),
2116+
Domain(Domain), Message(Message), Rename(Rename),
21182117
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
21192118
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
21202119
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange) {
2121-
Bits.AvailableAttr.Platform = static_cast<uint8_t>(Platform);
2122-
Bits.AvailableAttr.PlatformAgnostic = static_cast<uint8_t>(PlatformAgnostic);
2120+
Bits.AvailableAttr.Kind = static_cast<uint8_t>(Kind);
21232121
Bits.AvailableAttr.HasComputedRenamedDecl = false;
21242122
Bits.AvailableAttr.HasRenamedDecl = false;
21252123
Bits.AvailableAttr.IsSPI = IsSPI;
@@ -2128,52 +2126,10 @@ AvailableAttr::AvailableAttr(
21282126
// FIXME: [availability] The IsForEmbedded bit should be removed when
21292127
// it can be represented with AvailabilityDomain (rdar://138802876)
21302128
Bits.AvailableAttr.IsForEmbedded = true;
2131-
assert(getPlatform() == PlatformKind::none);
2132-
}
2133-
}
2134-
2135-
static PlatformAgnosticAvailabilityKind
2136-
platformAgnosticFromDomainAndKind(const AvailabilityDomain &Domain,
2137-
AvailableAttr::Kind Kind) {
2138-
switch (Kind) {
2139-
case swift::AvailableAttr::Kind::NoAsync:
2140-
return PlatformAgnosticAvailabilityKind::NoAsync;
2141-
2142-
case swift::AvailableAttr::Kind::Default:
2143-
if (Domain.isSwiftLanguage()) {
2144-
return PlatformAgnosticAvailabilityKind::SwiftVersionSpecific;
2145-
} else if (Domain.isPackageDescription()) {
2146-
return PlatformAgnosticAvailabilityKind::
2147-
PackageDescriptionVersionSpecific;
2148-
}
2149-
return PlatformAgnosticAvailabilityKind::None;
2150-
2151-
case swift::AvailableAttr::Kind::Deprecated:
2152-
return PlatformAgnosticAvailabilityKind::Deprecated;
2153-
2154-
case swift::AvailableAttr::Kind::Unavailable:
2155-
if (Domain.isSwiftLanguage()) {
2156-
return PlatformAgnosticAvailabilityKind::UnavailableInSwift;
2157-
} else if (Domain.isUniversal() || Domain.isPlatform()) {
2158-
return PlatformAgnosticAvailabilityKind::Unavailable;
2159-
}
2160-
llvm_unreachable("unexpected domain for unavailable attr");
2129+
assert(Domain.isUniversal());
21612130
}
21622131
}
21632132

2164-
AvailableAttr::AvailableAttr(
2165-
SourceLoc AtLoc, SourceRange Range, const AvailabilityDomain &Domain,
2166-
Kind Kind, StringRef Message, StringRef Rename,
2167-
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
2168-
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
2169-
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
2170-
bool Implicit, bool IsSPI, bool IsForEmbedded)
2171-
: AvailableAttr(AtLoc, Range, Domain.getPlatformKind(), Message, Rename,
2172-
Introduced, IntroducedRange, Deprecated, DeprecatedRange,
2173-
Obsoleted, ObsoletedRange,
2174-
platformAgnosticFromDomainAndKind(Domain, Kind), Implicit,
2175-
IsSPI, IsForEmbedded) {}
2176-
21772133
#undef INIT_VER_TUPLE
21782134

21792135
AvailableAttr *AvailableAttr::createUniversallyUnavailable(ASTContext &C,
@@ -2241,16 +2197,16 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
22412197
}
22422198

22432199
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
2244-
return new (C) AvailableAttr(
2245-
implicit ? SourceLoc() : AtLoc, implicit ? SourceRange() : getRange(),
2246-
getPlatform(), Message, Rename,
2247-
Introduced ? *Introduced : llvm::VersionTuple(),
2248-
implicit ? SourceRange() : IntroducedRange,
2249-
Deprecated ? *Deprecated : llvm::VersionTuple(),
2250-
implicit ? SourceRange() : DeprecatedRange,
2251-
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2252-
implicit ? SourceRange() : ObsoletedRange,
2253-
getPlatformAgnosticAvailability(), implicit, isSPI(), isForEmbedded());
2200+
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
2201+
implicit ? SourceRange() : getRange(), Domain,
2202+
getKind(), Message, Rename,
2203+
Introduced ? *Introduced : llvm::VersionTuple(),
2204+
implicit ? SourceRange() : IntroducedRange,
2205+
Deprecated ? *Deprecated : llvm::VersionTuple(),
2206+
implicit ? SourceRange() : DeprecatedRange,
2207+
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2208+
implicit ? SourceRange() : ObsoletedRange,
2209+
implicit, isSPI(), isForEmbedded());
22542210
}
22552211

22562212
std::optional<OriginallyDefinedInAttr::ActiveVersion>

0 commit comments

Comments
 (0)