Skip to content

Commit 86ea1ae

Browse files
committed
AST: Introduce a Kind enum and new constructor for AvailableAttr.
AvailableAttr::Kind and AvailabilityDomain are designed to replace PlatformAgnosticAvailabilityKind, allowing AvailableAttr to more flexibly model availability for arbitrary domains. For now, the new constructor just translates its inputs into inputs for the existing constructor. Once all of the callers of the existing AvailableAttr constructor have been updated to use the new constructor, the representation of AvailableAttr will be updated to store the new properties.
1 parent 18dd091 commit 86ea1ae

File tree

5 files changed

+102
-31
lines changed

5 files changed

+102
-31
lines changed

include/swift/AST/Attr.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,20 @@ enum class PlatformAgnosticAvailabilityKind : uint8_t {
742742

743743
/// Defines the @available attribute.
744744
class AvailableAttr : public DeclAttribute {
745+
public:
746+
enum class Kind : uint8_t {
747+
/// The attribute does not specify `deprecated`, `unavailable`,
748+
/// or `noasync`. Instead, it may specify `introduced:`, `deprecated:`, or
749+
/// `obsoleted:` versions or it may simply have a `rename:` field.
750+
Default,
751+
/// The attribute specifies unconditional deprecation.
752+
Deprecated,
753+
/// The attribute specifies unconditional unavailability.
754+
Unavailable,
755+
/// The attribute specifies unavailability in asynchronous contexts.
756+
NoAsync,
757+
};
758+
745759
public:
746760
AvailableAttr(SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
747761
StringRef Message, StringRef Rename,
@@ -753,6 +767,15 @@ class AvailableAttr : public DeclAttribute {
753767
PlatformAgnosticAvailabilityKind PlatformAgnostic,
754768
bool Implicit, bool IsSPI, bool IsForEmbedded = false);
755769

770+
AvailableAttr(SourceLoc AtLoc, SourceRange Range,
771+
const AvailabilityDomain &Domain, Kind Kind, StringRef Message,
772+
StringRef Rename, const llvm::VersionTuple &Introduced,
773+
SourceRange IntroducedRange,
774+
const llvm::VersionTuple &Deprecated,
775+
SourceRange DeprecatedRange,
776+
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
777+
bool Implicit, bool IsSPI, bool IsForEmbedded = false);
778+
756779
/// The optional message.
757780
const StringRef Message;
758781

lib/AST/Attr.cpp

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,55 +2125,100 @@ AvailableAttr::AvailableAttr(
21252125
Bits.AvailableAttr.IsSPI = IsSPI;
21262126

21272127
if (IsForEmbedded) {
2128-
// FIXME: The IsForEmbedded bit should be removed when library availability
2129-
// conditions are implemented (rdar://138802876)
2128+
// FIXME: [availability] The IsForEmbedded bit should be removed when
2129+
// it can be represented with AvailabilityDomain (rdar://138802876)
21302130
Bits.AvailableAttr.IsForEmbedded = true;
21312131
assert(getPlatform() == PlatformKind::none);
21322132
}
21332133
}
21342134

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");
2161+
}
2162+
}
2163+
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+
21352177
#undef INIT_VER_TUPLE
21362178

21372179
AvailableAttr *AvailableAttr::createUniversallyUnavailable(ASTContext &C,
21382180
StringRef Message,
21392181
StringRef Rename) {
21402182
return new (C) AvailableAttr(
2141-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2183+
SourceLoc(), SourceRange(), AvailabilityDomain::forUniversal(),
2184+
Kind::Unavailable, Message, Rename,
21422185
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
21432186
/*Obsoleted=*/{}, SourceRange(),
2144-
PlatformAgnosticAvailabilityKind::Unavailable, /*Implicit=*/false,
2187+
/*Implicit=*/false,
21452188
/*SPI=*/false);
21462189
}
21472190

21482191
AvailableAttr *AvailableAttr::createUniversallyDeprecated(ASTContext &C,
21492192
StringRef Message,
21502193
StringRef Rename) {
21512194
return new (C) AvailableAttr(
2152-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2195+
SourceLoc(), SourceRange(), AvailabilityDomain::forUniversal(),
2196+
Kind::Deprecated, Message, Rename,
21532197
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
21542198
/*Obsoleted=*/{}, SourceRange(),
2155-
PlatformAgnosticAvailabilityKind::Deprecated, /*Implicit=*/false,
2199+
/*Implicit=*/false,
21562200
/*SPI=*/false);
21572201
}
21582202

21592203
AvailableAttr *AvailableAttr::createUnavailableInSwift(ASTContext &C,
21602204
StringRef Message,
21612205
StringRef Rename) {
21622206
return new (C) AvailableAttr(
2163-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2207+
SourceLoc(), SourceRange(), AvailabilityDomain::forSwiftLanguage(),
2208+
Kind::Unavailable, Message, Rename,
21642209
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
21652210
/*Obsoleted=*/{}, SourceRange(),
2166-
PlatformAgnosticAvailabilityKind::UnavailableInSwift, /*Implicit=*/false,
2211+
/*Implicit=*/false,
21672212
/*SPI=*/false);
21682213
}
21692214

21702215
AvailableAttr *AvailableAttr::createSwiftLanguageModeVersioned(
21712216
ASTContext &C, StringRef Message, StringRef Rename,
21722217
llvm::VersionTuple Introduced, llvm::VersionTuple Obsoleted) {
21732218
return new (C) AvailableAttr(
2174-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2175-
Introduced, SourceRange(), /*Deprecated=*/{}, SourceRange(), Obsoleted,
2176-
SourceRange(), PlatformAgnosticAvailabilityKind::SwiftVersionSpecific,
2219+
SourceLoc(), SourceRange(), AvailabilityDomain::forSwiftLanguage(),
2220+
Kind::Default, Message, Rename, Introduced, SourceRange(),
2221+
/*Deprecated=*/{}, SourceRange(), Obsoleted, SourceRange(),
21772222
/*Implicit=*/false,
21782223
/*SPI=*/false);
21792224
}
@@ -2182,12 +2227,12 @@ AvailableAttr *AvailableAttr::createPlatformVersioned(
21822227
ASTContext &C, PlatformKind Platform, StringRef Message, StringRef Rename,
21832228
llvm::VersionTuple Introduced, llvm::VersionTuple Deprecated,
21842229
llvm::VersionTuple Obsoleted) {
2185-
return new (C) AvailableAttr(SourceLoc(), SourceRange(), Platform, Message,
2186-
Rename, Introduced, SourceRange(), Deprecated,
2187-
SourceRange(), Obsoleted, SourceRange(),
2188-
PlatformAgnosticAvailabilityKind::None,
2189-
/*Implicit=*/false,
2190-
/*SPI=*/false);
2230+
return new (C) AvailableAttr(
2231+
SourceLoc(), SourceRange(), AvailabilityDomain::forPlatform(Platform),
2232+
Kind::Default, Message, Rename, Introduced, SourceRange(), Deprecated,
2233+
SourceRange(), Obsoleted, SourceRange(),
2234+
/*Implicit=*/false,
2235+
/*SPI=*/false);
21912236
}
21922237

21932238
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7997,13 +7997,14 @@ void addCompletionHandlerAttribute(Decl *asyncImport,
79977997
continue;
79987998

79997999
llvm::VersionTuple NoVersion;
8000-
auto *attr = new (SwiftContext)
8001-
AvailableAttr(SourceLoc(), SourceRange(), PlatformKind::none,
8002-
/*Message=*/"", /*Rename=*/"", /*Introduced=*/NoVersion,
8003-
SourceRange(), /*Deprecated=*/NoVersion, SourceRange(),
8004-
/*Obsoleted=*/NoVersion, SourceRange(),
8005-
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true,
8006-
/*SPI=*/false);
8000+
auto *attr = new (SwiftContext) AvailableAttr(
8001+
SourceLoc(), SourceRange(), AvailabilityDomain::forUniversal(),
8002+
AvailableAttr::Kind::Default,
8003+
/*Message=*/"", /*Rename=*/"", /*Introduced=*/NoVersion, SourceRange(),
8004+
/*Deprecated=*/NoVersion, SourceRange(),
8005+
/*Obsoleted=*/NoVersion, SourceRange(),
8006+
/*Implicit=*/true,
8007+
/*SPI=*/false);
80078008

80088009
afd->setRenamedDecl(attr, asyncFunc);
80098010
afd->getAttrs().add(attr);

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4462,10 +4462,11 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
44624462
if (Context.LangOpts.hasFeature(Feature::Embedded)) {
44634463
StringRef Message = "unavailable in embedded Swift", Renamed;
44644464
auto attr = new (Context) AvailableAttr(
4465-
AtLoc, SourceRange(AtLoc, attrLoc), PlatformKind::none, Message,
4466-
Renamed, llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4467-
SourceRange(), llvm::VersionTuple(), SourceRange(),
4468-
PlatformAgnosticAvailabilityKind::Unavailable,
4465+
AtLoc, SourceRange(AtLoc, attrLoc),
4466+
AvailabilityDomain::forUniversal(), AvailableAttr::Kind::Unavailable,
4467+
Message, Renamed, llvm::VersionTuple(), SourceRange(),
4468+
llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4469+
SourceRange(),
44694470
/*Implicit=*/false, /*IsSPI=*/false, /*IsForEmbedded=*/true);
44704471
Attributes.add(attr);
44714472
}

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6654,12 +6654,13 @@ static void addUnavailableAttrs(ExtensionDecl *ext, NominalTypeDecl *nominal) {
66546654
continue;
66556655

66566656
auto attr = new (ctx) AvailableAttr(
6657-
SourceLoc(), SourceRange(), available.getPlatform(),
6658-
available.getMessage(),
6657+
SourceLoc(), SourceRange(),
6658+
AvailabilityDomain::forPlatform(available.getPlatform()),
6659+
AvailableAttr::Kind::Unavailable, available.getMessage(),
66596660
/*Rename=*/"", available.getIntroduced().value_or(noVersion),
66606661
SourceRange(), available.getDeprecated().value_or(noVersion),
66616662
SourceRange(), available.getObsoleted().value_or(noVersion),
6662-
SourceRange(), PlatformAgnosticAvailabilityKind::Unavailable,
6663+
SourceRange(),
66636664
/*Implicit=*/true, available.getParsedAttr()->isSPI());
66646665
ext->getAttrs().add(attr);
66656666
anyPlatformSpecificAttrs = true;

0 commit comments

Comments
 (0)