Skip to content

Commit c57132c

Browse files
committed
AST: Introduce new conveniences for synthesizing AvailabilityAttrs.
This makes intent clearer at the call site and removes a lot of explicit uses of PlatformAgnosticAvailabilityKind, which is going away.
1 parent 9edd9ee commit c57132c

File tree

6 files changed

+125
-94
lines changed

6 files changed

+125
-94
lines changed

include/swift/AST/Attr.h

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,36 @@ class AvailableAttr : public DeclAttribute {
813813
Bits.AvailableAttr.PlatformAgnostic);
814814
}
815815

816-
/// Create an AvailableAttr that indicates specific availability
817-
/// for all platforms.
818-
static AvailableAttr *
819-
createPlatformAgnostic(ASTContext &C, StringRef Message, StringRef Rename = "",
820-
PlatformAgnosticAvailabilityKind Reason
821-
= PlatformAgnosticAvailabilityKind::Unavailable,
822-
llvm::VersionTuple Obsoleted
823-
= llvm::VersionTuple());
816+
/// Create an `AvailableAttr` that specifies universal unavailability, e.g.
817+
/// `@available(*, unavailable)`.
818+
static AvailableAttr *createUniversallyUnavailable(ASTContext &C,
819+
StringRef Message,
820+
StringRef Rename = "");
821+
822+
/// Create an `AvailableAttr` that specifies universal deprecation, e.g.
823+
/// `@available(*, deprecated)`.
824+
static AvailableAttr *createUniversallyDeprecated(ASTContext &C,
825+
StringRef Message,
826+
StringRef Rename = "");
827+
828+
/// Create an `AvailableAttr` that specifies unavailability in Swift, e.g.
829+
/// `@available(swift, unavailable)`.
830+
static AvailableAttr *createUnavailableInSwift(ASTContext &C,
831+
StringRef Message,
832+
StringRef Rename = "");
833+
834+
/// Create an `AvailableAttr` that specifies availability associated with
835+
/// Swift language modes, e.g. `@available(swift, obsoleted: 6)`.
836+
static AvailableAttr *createSwiftLanguageModeVersioned(
837+
ASTContext &C, StringRef Message, StringRef Rename,
838+
llvm::VersionTuple Introduced, llvm::VersionTuple Obsoleted);
839+
840+
/// Create an `AvailableAttr` that specifies versioned availability for a
841+
/// particular platform, e.g. `@available(macOS, introduced: 13)`.
842+
static AvailableAttr *createPlatformVersioned(
843+
ASTContext &C, PlatformKind Platform, StringRef Message, StringRef Rename,
844+
llvm::VersionTuple Introduced, llvm::VersionTuple Deprecated,
845+
llvm::VersionTuple Obsoleted);
824846

825847
AvailableAttr *clone(ASTContext &C, bool implicit) const;
826848
AvailableAttr *clone(ASTContext &C) const {

lib/AST/Attr.cpp

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,24 +2134,62 @@ AvailableAttr::AvailableAttr(
21342134

21352135
#undef INIT_VER_TUPLE
21362136

2137-
AvailableAttr *
2138-
AvailableAttr::createPlatformAgnostic(ASTContext &C,
2139-
StringRef Message,
2140-
StringRef Rename,
2141-
PlatformAgnosticAvailabilityKind Kind,
2142-
llvm::VersionTuple Obsoleted) {
2143-
assert(Kind != PlatformAgnosticAvailabilityKind::None);
2144-
llvm::VersionTuple NoVersion;
2145-
if (Kind == PlatformAgnosticAvailabilityKind::SwiftVersionSpecific) {
2146-
assert(!Obsoleted.empty());
2147-
}
2137+
AvailableAttr *AvailableAttr::createUniversallyUnavailable(ASTContext &C,
2138+
StringRef Message,
2139+
StringRef Rename) {
2140+
return new (C) AvailableAttr(
2141+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2142+
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
2143+
/*Obsoleted=*/{}, SourceRange(),
2144+
PlatformAgnosticAvailabilityKind::Unavailable, /*Implicit=*/false,
2145+
/*SPI=*/false);
2146+
}
2147+
2148+
AvailableAttr *AvailableAttr::createUniversallyDeprecated(ASTContext &C,
2149+
StringRef Message,
2150+
StringRef Rename) {
2151+
return new (C) AvailableAttr(
2152+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2153+
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
2154+
/*Obsoleted=*/{}, SourceRange(),
2155+
PlatformAgnosticAvailabilityKind::Deprecated, /*Implicit=*/false,
2156+
/*SPI=*/false);
2157+
}
2158+
2159+
AvailableAttr *AvailableAttr::createUnavailableInSwift(ASTContext &C,
2160+
StringRef Message,
2161+
StringRef Rename) {
21482162
return new (C) AvailableAttr(
21492163
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2150-
/*Introduced=*/NoVersion, SourceRange(), /*Deprecated=*/NoVersion,
2151-
SourceRange(), Obsoleted, SourceRange(), Kind, /*Implicit=*/false,
2164+
/*Introduced=*/{}, SourceRange(), /*Deprecated=*/{}, SourceRange(),
2165+
/*Obsoleted=*/{}, SourceRange(),
2166+
PlatformAgnosticAvailabilityKind::UnavailableInSwift, /*Implicit=*/false,
21522167
/*SPI=*/false);
21532168
}
21542169

2170+
AvailableAttr *AvailableAttr::createSwiftLanguageModeVersioned(
2171+
ASTContext &C, StringRef Message, StringRef Rename,
2172+
llvm::VersionTuple Introduced, llvm::VersionTuple Obsoleted) {
2173+
return new (C) AvailableAttr(
2174+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2175+
Introduced, SourceRange(), /*Deprecated=*/{}, SourceRange(), Obsoleted,
2176+
SourceRange(), PlatformAgnosticAvailabilityKind::SwiftVersionSpecific,
2177+
/*Implicit=*/false,
2178+
/*SPI=*/false);
2179+
}
2180+
2181+
AvailableAttr *AvailableAttr::createPlatformVersioned(
2182+
ASTContext &C, PlatformKind Platform, StringRef Message, StringRef Rename,
2183+
llvm::VersionTuple Introduced, llvm::VersionTuple Deprecated,
2184+
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);
2191+
}
2192+
21552193
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
21562194
bool forTargetVariant) const {
21572195
return isPlatformActive(Platform, ctx.LangOpts, forTargetVariant);

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,8 @@ void swift::conformToCxxSpanIfNeeded(ClangImporter::Implementation &impl,
12691269
if (!importedConstructor)
12701270
return;
12711271

1272-
auto attr = AvailableAttr::createPlatformAgnostic(importedConstructor->getASTContext(), "use 'init(_:)' instead.", "", PlatformAgnosticAvailabilityKind::Deprecated);
1272+
auto attr = AvailableAttr::createUniversallyDeprecated(
1273+
importedConstructor->getASTContext(), "use 'init(_:)' instead.", "");
12731274
importedConstructor->getAttrs().add(attr);
12741275

12751276
decl->addMember(importedConstructor);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,9 @@ static void applyAvailableAttribute(Decl *decl, AvailabilityRange &info,
555555
return;
556556

557557
llvm::VersionTuple noVersion;
558-
auto AvAttr = new (C) AvailableAttr(
559-
SourceLoc(), SourceRange(), targetPlatform(C.LangOpts),
560-
/*Message=*/StringRef(),
561-
/*Rename=*/StringRef(), info.getRawMinimumVersion(),
562-
/*IntroducedRange=*/SourceRange(),
563-
/*Deprecated=*/noVersion,
564-
/*DeprecatedRange=*/SourceRange(),
565-
/*Obsoleted=*/noVersion,
566-
/*ObsoletedRange=*/SourceRange(), PlatformAgnosticAvailabilityKind::None,
567-
/*Implicit=*/false,
568-
/*SPI=*/false);
558+
auto AvAttr = AvailableAttr::createPlatformVersioned(
559+
C, targetPlatform(C.LangOpts), /*Message=*/"", /*Rename=*/"",
560+
info.getRawMinimumVersion(), /*Deprecated=*/{}, /*Obsoleted=*/{});
569561

570562
decl->getAttrs().add(AvAttr);
571563
}
@@ -1304,9 +1296,8 @@ namespace {
13041296
// "Raw" is the Objective-C name, which was never available in Swift.
13051297
// Variants within the active version are usually declarations that
13061298
// have been superseded, like the accessors of a property.
1307-
attr = AvailableAttr::createPlatformAgnostic(
1308-
ctx, /*Message*/StringRef(), ctx.AllocateCopy(renamed.str()),
1309-
PlatformAgnosticAvailabilityKind::UnavailableInSwift);
1299+
attr = AvailableAttr::createUnavailableInSwift(
1300+
ctx, /*Message*/ StringRef(), ctx.AllocateCopy(renamed.str()));
13101301
} else {
13111302
unsigned majorVersion = getVersion().majorVersionNumber();
13121303
unsigned minorVersion = getVersion().minorVersionNumber();
@@ -1319,26 +1310,19 @@ namespace {
13191310
(majorVersion == 4 && minorVersion < 2)
13201311
? llvm::VersionTuple(4, 2)
13211312
: llvm::VersionTuple(majorVersion + 1);
1322-
attr = AvailableAttr::createPlatformAgnostic(
1323-
ctx, /*Message*/StringRef(), ctx.AllocateCopy(renamed.str()),
1324-
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific,
1325-
obsoletedVersion);
1313+
attr = AvailableAttr::createSwiftLanguageModeVersioned(
1314+
ctx, /*Message=*/"", ctx.AllocateCopy(renamed.str()),
1315+
/*Introduced=*/{}, obsoletedVersion);
13261316
} else {
13271317
// Future names are introduced in their future version.
13281318
assert(getVersion() > getActiveSwiftVersion());
13291319
llvm::VersionTuple introducedVersion =
13301320
(majorVersion == 4 && minorVersion == 2)
13311321
? llvm::VersionTuple(4, 2)
13321322
: llvm::VersionTuple(majorVersion);
1333-
attr = new (ctx) AvailableAttr(
1334-
SourceLoc(), SourceRange(), PlatformKind::none,
1335-
/*Message=*/StringRef(), ctx.AllocateCopy(renamed.str()),
1336-
/*Introduced=*/introducedVersion, SourceRange(),
1337-
/*Deprecated=*/llvm::VersionTuple(), SourceRange(),
1338-
/*Obsoleted=*/llvm::VersionTuple(), SourceRange(),
1339-
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific,
1340-
/*Implicit=*/false,
1341-
/*SPI=*/false);
1323+
attr = AvailableAttr::createSwiftLanguageModeVersioned(
1324+
ctx, /*Message=*/"", ctx.AllocateCopy(renamed.str()),
1325+
introducedVersion, /*Obsoleted=*/{});
13421326
}
13431327
}
13441328

@@ -1525,10 +1509,8 @@ namespace {
15251509

15261510
// Make Objective-C's 'id' unavailable.
15271511
if (Impl.SwiftContext.LangOpts.EnableObjCInterop && isObjCId(Decl)) {
1528-
auto attr = AvailableAttr::createPlatformAgnostic(
1529-
Impl.SwiftContext,
1530-
"'id' is not available in Swift; use 'Any'", "",
1531-
PlatformAgnosticAvailabilityKind::UnavailableInSwift);
1512+
auto attr = AvailableAttr::createUnavailableInSwift(
1513+
Impl.SwiftContext, "'id' is not available in Swift; use 'Any'", "");
15321514
Result->getAttrs().add(attr);
15331515
}
15341516

@@ -2391,12 +2373,12 @@ namespace {
23912373
synthesizer.createDefaultConstructor(result);
23922374
ctors.push_back(defaultCtor);
23932375
if (cxxRecordDecl) {
2394-
auto attr = AvailableAttr::createPlatformAgnostic(
2376+
auto attr = AvailableAttr::createUniversallyDeprecated(
23952377
defaultCtor->getASTContext(),
23962378
"This zero-initializes the backing memory of the struct, which "
23972379
"is unsafe for some C++ structs. Consider adding an explicit "
23982380
"default initializer for this C++ struct.",
2399-
"", PlatformAgnosticAvailabilityKind::Deprecated);
2381+
"");
24002382
defaultCtor->getAttrs().add(attr);
24012383
}
24022384
}
@@ -2910,16 +2892,11 @@ namespace {
29102892
auto availability = Impl.SwiftContext.getSwift58Availability();
29112893
if (!availability.isAlwaysAvailable()) {
29122894
assert(availability.hasMinimumVersion());
2913-
auto AvAttr = new (Impl.SwiftContext)
2914-
AvailableAttr(SourceLoc(), SourceRange(),
2915-
targetPlatform(Impl.SwiftContext.LangOpts),
2916-
/*Message=*/"", /*Rename=*/"",
2917-
availability.getRawMinimumVersion(),
2918-
/*IntroducedRange=*/SourceRange(), {},
2919-
/*DeprecatedRange=*/SourceRange(), {},
2920-
/*ObsoletedRange=*/SourceRange(),
2921-
PlatformAgnosticAvailabilityKind::None,
2922-
/*Implicit=*/false, false);
2895+
auto AvAttr = AvailableAttr::createPlatformVersioned(
2896+
Impl.SwiftContext, targetPlatform(Impl.SwiftContext.LangOpts),
2897+
/*Message=*/"", /*Rename=*/"",
2898+
availability.getRawMinimumVersion(), /*Deprecated=*/{},
2899+
/*Obsoleted=*/{});
29232900
classDecl->getAttrs().add(AvAttr);
29242901
}
29252902
}
@@ -4408,7 +4385,7 @@ namespace {
44084385
decl, AccessLevel::Public, loc, name, loc, std::nullopt,
44094386
genericParamList, dc);
44104387

4411-
auto attr = AvailableAttr::createPlatformAgnostic(
4388+
auto attr = AvailableAttr::createUniversallyUnavailable(
44124389
Impl.SwiftContext, "Un-specialized class templates are not currently "
44134390
"supported. Please use a specialization of this "
44144391
"type.");
@@ -5376,8 +5353,8 @@ namespace {
53765353
message = "cannot find Swift declaration for this protocol";
53775354
else
53785355
llvm_unreachable("unknown bridged decl kind");
5379-
auto attr = AvailableAttr::createPlatformAgnostic(Impl.SwiftContext,
5380-
message);
5356+
auto attr = AvailableAttr::createUniversallyUnavailable(Impl.SwiftContext,
5357+
message);
53815358
VD->getAttrs().add(attr);
53825359
}
53835360

@@ -5430,7 +5407,7 @@ namespace {
54305407
addObjCAttribute(result,
54315408
Impl.importIdentifier(decl->getIdentifier()));
54325409
result->setImplicit();
5433-
auto attr = AvailableAttr::createPlatformAgnostic(
5410+
auto attr = AvailableAttr::createUniversallyUnavailable(
54345411
Impl.SwiftContext,
54355412
"This Objective-C protocol has only been forward-declared; "
54365413
"import its owning module to use it");
@@ -5585,7 +5562,8 @@ namespace {
55855562
auto result = createFakeClass(name, /* cacheResult */ true,
55865563
/* inheritFromNSObject */ true);
55875564
result->setImplicit();
5588-
auto attr = AvailableAttr::createPlatformAgnostic(Impl.SwiftContext,
5565+
auto attr = AvailableAttr::createUniversallyUnavailable(
5566+
Impl.SwiftContext,
55895567
"This Objective-C class has only been forward-declared; "
55905568
"import its owning module to use it");
55915569
result->getAttrs().add(attr);
@@ -6477,7 +6455,7 @@ SwiftDeclConverter::importOptionConstant(const clang::EnumConstantDecl *decl,
64776455
!CD->isUnavailable()) {
64786456
/// Create an AvailableAttr that indicates specific availability
64796457
/// for all platforms.
6480-
auto attr = AvailableAttr::createPlatformAgnostic(
6458+
auto attr = AvailableAttr::createUniversallyUnavailable(
64816459
Impl.SwiftContext, "use [] to construct an empty option set");
64826460
CD->getAttrs().add(attr);
64836461
}
@@ -7114,7 +7092,7 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
71147092
errorStr += objcMethod->getSelector().getAsString();
71157093
errorStr += ']';
71167094

7117-
auto attr = AvailableAttr::createPlatformAgnostic(
7095+
auto attr = AvailableAttr::createUniversallyUnavailable(
71187096
Impl.SwiftContext, Impl.SwiftContext.AllocateCopy(errorStr.str()));
71197097
ctor->getAttrs().add(attr);
71207098
continue;
@@ -8795,7 +8773,7 @@ void ClangImporter::Implementation::importAttributes(
87958773
//
87968774
if (auto unavailable = dyn_cast<clang::UnavailableAttr>(*AI)) {
87978775
auto Message = unavailable->getMessage();
8798-
auto attr = AvailableAttr::createPlatformAgnostic(C, Message);
8776+
auto attr = AvailableAttr::createUniversallyUnavailable(C, Message);
87998777
MappedDecl->getAttrs().add(attr);
88008778
AnyUnavailable = true;
88018779
continue;
@@ -8808,8 +8786,7 @@ void ClangImporter::Implementation::importAttributes(
88088786
//
88098787
if (auto unavailable_annot = dyn_cast<clang::AnnotateAttr>(*AI))
88108788
if (unavailable_annot->getAnnotation() == "swift1_unavailable") {
8811-
auto attr = AvailableAttr::createPlatformAgnostic(
8812-
C, "", "", PlatformAgnosticAvailabilityKind::UnavailableInSwift);
8789+
auto attr = AvailableAttr::createUnavailableInSwift(C, "", "");
88138790
MappedDecl->getAttrs().add(attr);
88148791
AnyUnavailable = true;
88158792
continue;
@@ -8822,8 +8799,7 @@ void ClangImporter::Implementation::importAttributes(
88228799
//
88238800
if (auto deprecated = dyn_cast<clang::DeprecatedAttr>(*AI)) {
88248801
auto Message = deprecated->getMessage();
8825-
auto attr = AvailableAttr::createPlatformAgnostic(C, Message, "",
8826-
PlatformAgnosticAvailabilityKind::Deprecated);
8802+
auto attr = AvailableAttr::createUniversallyDeprecated(C, Message, "");
88278803
MappedDecl->getAttrs().add(attr);
88288804
continue;
88298805
}
@@ -8845,9 +8821,8 @@ void ClangImporter::Implementation::importAttributes(
88458821
if (!replacement.empty())
88468822
swiftReplacement = getSwiftNameFromClangName(replacement);
88478823

8848-
auto attr = AvailableAttr::createPlatformAgnostic(
8849-
C, avail->getMessage(), swiftReplacement,
8850-
PlatformAgnosticAvailabilityKind::UnavailableInSwift);
8824+
auto attr = AvailableAttr::createUnavailableInSwift(
8825+
C, avail->getMessage(), swiftReplacement);
88518826
MappedDecl->getAttrs().add(attr);
88528827
AnyUnavailable = true;
88538828
continue;
@@ -8955,7 +8930,7 @@ void ClangImporter::Implementation::importAttributes(
89558930
if (auto ID = dyn_cast<clang::ObjCInterfaceDecl>(ClangDecl)) {
89568931
// Ban NSInvocation.
89578932
if (ID->getName() == "NSInvocation") {
8958-
auto attr = AvailableAttr::createPlatformAgnostic(C, "");
8933+
auto attr = AvailableAttr::createUniversallyUnavailable(C, "");
89598934
MappedDecl->getAttrs().add(attr);
89608935
return;
89618936
}
@@ -8988,8 +8963,8 @@ void ClangImporter::Implementation::importAttributes(
89888963
!FD->getAttr<clang::SwiftNameAttr>()) {
89898964
if (auto t = FD->getParamDecl(0)->getType()->getAs<clang::TypedefType>()){
89908965
if (isCFTypeDecl(t->getDecl())) {
8991-
auto attr = AvailableAttr::createPlatformAgnostic(C,
8992-
"Core Foundation objects are automatically memory managed");
8966+
auto attr = AvailableAttr::createUniversallyUnavailable(
8967+
C, "Core Foundation objects are automatically memory managed");
89938968
MappedDecl->getAttrs().add(attr);
89948969
return;
89958970
}
@@ -9677,8 +9652,8 @@ void ClangImporter::Implementation::
96779652
markUnavailable(ValueDecl *decl, StringRef unavailabilityMsgRef) {
96789653

96799654
unavailabilityMsgRef = SwiftContext.AllocateCopy(unavailabilityMsgRef);
9680-
auto ua = AvailableAttr::createPlatformAgnostic(SwiftContext,
9681-
unavailabilityMsgRef);
9655+
auto ua = AvailableAttr::createUniversallyUnavailable(SwiftContext,
9656+
unavailabilityMsgRef);
96829657
decl->getAttrs().add(ua);
96839658
}
96849659

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6672,13 +6672,8 @@ static void addUnavailableAttrs(ExtensionDecl *ext, NominalTypeDecl *nominal) {
66726672

66736673
// Add the blanket "unavailable".
66746674

6675-
auto attr = new (ctx) AvailableAttr(
6676-
SourceLoc(), SourceRange(), PlatformKind::none, /*Message*/ "",
6677-
/*Rename=*/"", /*Introduced=*/noVersion, SourceRange(),
6678-
/*Deprecated=*/noVersion, SourceRange(), /*Obsoleted=*/noVersion,
6679-
SourceRange(), PlatformAgnosticAvailabilityKind::Unavailable,
6680-
/*Implicit=*/false, /*SPI=*/false);
6681-
ext->getAttrs().add(attr);
6675+
ext->getAttrs().add(
6676+
AvailableAttr::createUniversallyUnavailable(ctx, /*Message=*/""));
66826677
}
66836678

66846679
ProtocolConformance *swift::deriveImplicitSendableConformance(

0 commit comments

Comments
 (0)