Skip to content

Commit 6844cfe

Browse files
committed
AST: Retire OtherPlatformAvailabilitySpec.
1 parent da02cd0 commit 6844cfe

11 files changed

+74
-100
lines changed

include/swift/AST/ASTBridging.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,18 @@ struct BridgedAvailabilityMacroDefinition {
626626

627627
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAvailabilitySpecKind {
628628
BridgedAvailabilitySpecKindPlatformVersionConstraint,
629-
BridgedAvailabilitySpecKindOtherPlatform,
629+
BridgedAvailabilitySpecKindWildcard,
630630
BridgedAvailabilitySpecKindLanguageVersionConstraint,
631631
BridgedAvailabilitySpecKindPackageDescriptionVersionConstraint,
632632
};
633633

634634
struct BridgedAvailabilityDomain;
635635

636+
SWIFT_NAME("BridgedAvailabilitySpec.createWildcard(_:loc:)")
637+
BridgedAvailabilitySpec
638+
BridgedAvailabilitySpec_createWildcard(BridgedASTContext cContext,
639+
BridgedSourceLoc cLoc);
640+
636641
SWIFT_NAME("getter:BridgedAvailabilitySpec.sourceRange(self:)")
637642
BridgedSourceRange
638643
BridgedAvailabilitySpec_getSourceRange(BridgedAvailabilitySpec spec);
@@ -669,11 +674,6 @@ BridgedPlatformAgnosticVersionConstraintAvailabilitySpec_createParsed(
669674
BridgedSourceLoc cNameLoc, BridgedVersionTuple cVersion,
670675
BridgedSourceRange cVersionSrcRange);
671676

672-
SWIFT_NAME("BridgedOtherPlatformAvailabilitySpec.createParsed(_:loc:)")
673-
BridgedOtherPlatformAvailabilitySpec
674-
BridgedOtherPlatformAvailabilitySpec_createParsed(BridgedASTContext cContext,
675-
BridgedSourceLoc cLoc);
676-
677677
SWIFT_NAME("getter:BridgedPlatformVersionConstraintAvailabilitySpec."
678678
"asAvailabilitySpec(self:)")
679679
BridgedAvailabilitySpec
@@ -686,11 +686,6 @@ BridgedAvailabilitySpec
686686
BridgedPlatformAgnosticVersionConstraintAvailabilitySpec_asAvailabilitySpec(
687687
BridgedPlatformAgnosticVersionConstraintAvailabilitySpec spec);
688688

689-
SWIFT_NAME(
690-
"getter:BridgedOtherPlatformAvailabilitySpec.asAvailabilitySpec(self:)")
691-
BridgedAvailabilitySpec BridgedOtherPlatformAvailabilitySpec_asAvailabilitySpec(
692-
BridgedOtherPlatformAvailabilitySpec spec);
693-
694689
struct BridgedAvailabilityDomain {
695690
void *_Nullable opaque;
696691

include/swift/AST/ASTBridgingWrappers.def

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
109109
AST_BRIDGING_WRAPPER_NULLABLE(AvailabilitySpec)
110110
AST_BRIDGING_WRAPPER_NULLABLE(PlatformVersionConstraintAvailabilitySpec)
111111
AST_BRIDGING_WRAPPER_NULLABLE(PlatformAgnosticVersionConstraintAvailabilitySpec)
112-
AST_BRIDGING_WRAPPER_NULLABLE(OtherPlatformAvailabilitySpec)
113112
AST_BRIDGING_WRAPPER_CONST_NONNULL(AvailabilityMacroMap)
114113
AST_BRIDGING_WRAPPER_NONNULL(PoundAvailableInfo)
115114

include/swift/AST/AvailabilitySpec.h

+25-42
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ namespace swift {
3030
class ASTContext;
3131

3232
enum class AvailabilitySpecKind {
33-
/// A platform-version constraint of the form "PlatformName X.Y.Z"
34-
PlatformVersionConstraint,
33+
/// A platform-version constraint of the form "PlatformName X.Y.Z"
34+
PlatformVersionConstraint,
3535

36-
/// A wildcard constraint, spelled '*', that is equivalent
37-
/// to CurrentPlatformName >= MinimumDeploymentTargetVersion
38-
OtherPlatform,
36+
/// A wildcard constraint, spelled '*', that is equivalent
37+
/// to CurrentPlatformName >= MinimumDeploymentTargetVersion
38+
Wildcard,
3939

40-
/// A language-version constraint of the form "swift X.Y.Z"
41-
LanguageVersionConstraint,
40+
/// A language-version constraint of the form "swift X.Y.Z"
41+
LanguageVersionConstraint,
4242

43-
/// A PackageDescription version constraint of the form "_PackageDescription X.Y.Z"
44-
PackageDescriptionVersionConstraint,
43+
/// A PackageDescription version constraint of the form "_PackageDescription
44+
/// X.Y.Z"
45+
PackageDescriptionVersionConstraint,
4546
};
4647

4748
/// The root class for specifications of API availability in availability
@@ -65,16 +66,29 @@ class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
6566
// Location of the availability macro expanded to create this spec.
6667
SourceLoc MacroLoc;
6768

68-
public:
6969
AvailabilitySpec(AvailabilitySpecKind Kind,
7070
std::optional<AvailabilityDomain> Domain,
7171
SourceRange SrcRange, llvm::VersionTuple Version,
7272
SourceLoc VersionStartLoc)
7373
: Kind(Kind), Domain(Domain), SrcRange(SrcRange), Version(Version),
7474
VersionStartLoc(VersionStartLoc) {}
7575

76+
public:
77+
/// Creates a wildcard availability specification that guards execution
78+
/// by checking that the run-time version is greater than the minimum
79+
/// deployment target. This specification is designed to ease porting
80+
/// to new platforms. Because new platforms typically branch from
81+
/// existing platforms, the wildcard allows an #available() check to do the
82+
/// "right" thing (executing the guarded branch) on the new platform without
83+
/// requiring a modification to every availability guard in the program. Note
84+
/// that we still do compile-time availability checking with '*', so the
85+
/// compiler will still catch references to potentially unavailable symbols.
86+
static AvailabilitySpec *createWildcard(ASTContext &ctx, SourceLoc starLoc);
87+
7688
AvailabilitySpecKind getKind() const { return Kind; }
7789

90+
bool isWildcard() { return getKind() == AvailabilitySpecKind::Wildcard; }
91+
7892
SourceRange getSourceRange() const { return SrcRange; }
7993
SourceLoc getStartLoc() const { return SrcRange.Start; }
8094

@@ -147,7 +161,7 @@ class PlatformAgnosticVersionConstraintAvailabilitySpec
147161
static AvailabilityDomain getDomainForSpecKind(AvailabilitySpecKind Kind) {
148162
switch (Kind) {
149163
case AvailabilitySpecKind::PlatformVersionConstraint:
150-
case AvailabilitySpecKind::OtherPlatform:
164+
case AvailabilitySpecKind::Wildcard:
151165
llvm_unreachable("unexpected spec kind");
152166
case AvailabilitySpecKind::LanguageVersionConstraint:
153167
return AvailabilityDomain::forSwiftLanguage();
@@ -184,37 +198,6 @@ class PlatformAgnosticVersionConstraintAvailabilitySpec
184198
}
185199
};
186200

187-
/// A wildcard availability specification that guards execution
188-
/// by checking that the run-time version is greater than the minimum
189-
/// deployment target. This specification is designed to ease porting
190-
/// to new platforms. Because new platforms typically branch from
191-
/// existing platforms, the wildcard allows an #available() check to do the
192-
/// "right" thing (executing the guarded branch) on the new platform without
193-
/// requiring a modification to every availability guard in the program. Note
194-
/// that we still do compile-time availability checking with '*', so the
195-
/// compiler will still catch references to potentially unavailable symbols.
196-
class OtherPlatformAvailabilitySpec : public AvailabilitySpec {
197-
public:
198-
OtherPlatformAvailabilitySpec(SourceLoc StarLoc)
199-
: AvailabilitySpec(AvailabilitySpecKind::OtherPlatform, std::nullopt,
200-
StarLoc,
201-
/*Version=*/{},
202-
/*VersionStartLoc=*/{}) {}
203-
204-
void print(raw_ostream &OS, unsigned Indent) const;
205-
206-
static bool classof(const AvailabilitySpec *Spec) {
207-
return Spec->getKind() == AvailabilitySpecKind::OtherPlatform;
208-
}
209-
210-
void *
211-
operator new(size_t Bytes, ASTContext &C,
212-
unsigned Alignment = alignof(OtherPlatformAvailabilitySpec)) {
213-
return AvailabilitySpec::operator new(Bytes, C, AllocationArena::Permanent,
214-
Alignment);
215-
}
216-
};
217-
218201
/// Maps of macro name and version to availability specifications.
219202
/// Organized as two nested \c DenseMap keyed first on the macro name then
220203
/// the macro version. This structure allows to peek at macro names before

lib/AST/ASTDumper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1128,8 +1128,8 @@ namespace {
11281128
printFoot();
11291129
break;
11301130
}
1131-
case AvailabilitySpecKind::OtherPlatform:
1132-
printHead("other_constraint_availability_spec", PatternColor,
1131+
case AvailabilitySpecKind::Wildcard:
1132+
printHead("wildcard_constraint_availability_spec", PatternColor,
11331133
label);
11341134
printFoot();
11351135
break;

lib/AST/AvailabilitySpec.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
using namespace swift;
2323

24+
AvailabilitySpec *AvailabilitySpec::createWildcard(ASTContext &ctx,
25+
SourceLoc starLoc) {
26+
return new (ctx)
27+
AvailabilitySpec(AvailabilitySpecKind::Wildcard, std::nullopt, starLoc,
28+
/*Version=*/{},
29+
/*VersionStartLoc=*/{});
30+
}
31+
2432
llvm::VersionTuple AvailabilitySpec::getVersion() const {
2533
switch (getKind()) {
2634
case AvailabilitySpecKind::PlatformVersionConstraint: {
@@ -40,7 +48,7 @@ llvm::VersionTuple AvailabilitySpec::getVersion() const {
4048
}
4149
case AvailabilitySpecKind::LanguageVersionConstraint:
4250
case AvailabilitySpecKind::PackageDescriptionVersionConstraint:
43-
case AvailabilitySpecKind::OtherPlatform:
51+
case AvailabilitySpecKind::Wildcard:
4452
return Version;
4553
}
4654
}
@@ -69,9 +77,3 @@ void PlatformAgnosticVersionConstraintAvailabilitySpec::print(raw_ostream &OS,
6977
<< " version='" << getVersion() << "'"
7078
<< ')';
7179
}
72-
73-
void OtherPlatformAvailabilitySpec::print(raw_ostream &OS, unsigned Indent) const {
74-
OS.indent(Indent) << '(' << "other_constraint_availability_spec"
75-
<< " "
76-
<< ')';
77-
}

lib/AST/Bridging/AvailabilityBridging.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ static BridgedPlatformKind bridge(PlatformKind platform) {
8686
// MARK: AvailabilitySpec
8787
//===----------------------------------------------------------------------===//
8888

89+
BridgedAvailabilitySpec
90+
BridgedAvailabilitySpec_createWildcard(BridgedASTContext cContext,
91+
BridgedSourceLoc cLoc) {
92+
return AvailabilitySpec::createWildcard(cContext.unbridged(),
93+
cLoc.unbridged());
94+
}
95+
8996
BridgedSourceRange
9097
BridgedAvailabilitySpec_getSourceRange(BridgedAvailabilitySpec spec) {
9198
return spec.unbridged()->getSourceRange();
@@ -118,8 +125,8 @@ static AvailabilitySpecKind unbridge(BridgedAvailabilitySpecKind kind) {
118125
switch (kind) {
119126
case BridgedAvailabilitySpecKindPlatformVersionConstraint:
120127
return AvailabilitySpecKind::PlatformVersionConstraint;
121-
case BridgedAvailabilitySpecKindOtherPlatform:
122-
return AvailabilitySpecKind::OtherPlatform;
128+
case BridgedAvailabilitySpecKindWildcard:
129+
return AvailabilitySpecKind::Wildcard;
123130
case BridgedAvailabilitySpecKindLanguageVersionConstraint:
124131
return AvailabilitySpecKind::LanguageVersionConstraint;
125132
case BridgedAvailabilitySpecKindPackageDescriptionVersionConstraint:
@@ -149,13 +156,6 @@ BridgedPlatformAgnosticVersionConstraintAvailabilitySpec_createParsed(
149156
cVersionSrcRange.unbridged());
150157
}
151158

152-
BridgedOtherPlatformAvailabilitySpec
153-
BridgedOtherPlatformAvailabilitySpec_createParsed(BridgedASTContext cContext,
154-
BridgedSourceLoc cLoc) {
155-
return new (cContext.unbridged())
156-
OtherPlatformAvailabilitySpec(cLoc.unbridged());
157-
}
158-
159159
BridgedAvailabilitySpec
160160
BridgedPlatformVersionConstraintAvailabilitySpec_asAvailabilitySpec(
161161
BridgedPlatformVersionConstraintAvailabilitySpec spec) {
@@ -168,11 +168,6 @@ BridgedPlatformAgnosticVersionConstraintAvailabilitySpec_asAvailabilitySpec(
168168
return static_cast<AvailabilitySpec *>(spec.unbridged());
169169
}
170170

171-
BridgedAvailabilitySpec BridgedOtherPlatformAvailabilitySpec_asAvailabilitySpec(
172-
BridgedOtherPlatformAvailabilitySpec spec) {
173-
return static_cast<AvailabilitySpec *>(spec.unbridged());
174-
}
175-
176171
//===----------------------------------------------------------------------===//
177172
// MARK: AvailabilityDomain
178173
//===----------------------------------------------------------------------===//

lib/ASTGen/Sources/ASTGen/Availability.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ extension ASTGenVisitor {
344344
for parsed in node {
345345
switch parsed.argument {
346346
case .token(let tok) where tok.rawText == "*":
347-
let spec = BridgedOtherPlatformAvailabilitySpec.createParsed(
347+
let spec = BridgedAvailabilitySpec.createWildcard(
348348
self.ctx,
349349
loc: self.generateSourceLoc(tok)
350350
)
351-
result.append(spec.asAvailabilitySpec)
351+
result.append(spec)
352352
case .token(let tok):
353353
handle(domainNode: tok, versionNode: nil)
354354
case .availabilityVersionRestriction(let platformVersion):

lib/Parse/ParseExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3689,7 +3689,7 @@ ParserResult<AvailabilitySpec> Parser::parseAvailabilitySpec() {
36893689
SourceLoc StarLoc = Tok.getLoc();
36903690
consumeToken();
36913691

3692-
return makeParserResult(new (Context) OtherPlatformAvailabilitySpec(StarLoc));
3692+
return makeParserResult(AvailabilitySpec::createWildcard(Context, StarLoc));
36933693
}
36943694
if (Tok.isIdentifierOrUnderscore() &&
36953695
(Tok.getText() == "swift" || Tok.getText() == "_PackageDescription"))

lib/Parse/ParseStmt.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ validateAvailabilitySpecList(Parser &P,
12921292
SmallVectorImpl<AvailabilitySpec *> &Specs,
12931293
Parser::AvailabilitySpecSource Source) {
12941294
llvm::SmallSet<PlatformKind, 4> Platforms;
1295-
std::optional<SourceLoc> OtherPlatformSpecLoc = std::nullopt;
1295+
std::optional<SourceLoc> WildcardSpecLoc = std::nullopt;
12961296

12971297
if (Specs.size() == 1 &&
12981298
isa<PlatformAgnosticVersionConstraintAvailabilitySpec>(Specs[0])) {
@@ -1305,8 +1305,8 @@ validateAvailabilitySpecList(Parser &P,
13051305
SmallVector<AvailabilitySpec *, 5> RecognizedSpecs;
13061306
for (auto *Spec : Specs) {
13071307
RecognizedSpecs.push_back(Spec);
1308-
if (auto *OtherPlatSpec = dyn_cast<OtherPlatformAvailabilitySpec>(Spec)) {
1309-
OtherPlatformSpecLoc = OtherPlatSpec->getStartLoc();
1308+
if (Spec->isWildcard()) {
1309+
WildcardSpecLoc = Spec->getStartLoc();
13101310
continue;
13111311
}
13121312

@@ -1343,24 +1343,24 @@ validateAvailabilitySpecList(Parser &P,
13431343

13441344
switch (Source) {
13451345
case Parser::AvailabilitySpecSource::Available: {
1346-
if (OtherPlatformSpecLoc == std::nullopt) {
1346+
if (WildcardSpecLoc == std::nullopt) {
13471347
SourceLoc InsertWildcardLoc = P.PreviousLoc;
13481348
P.diagnose(InsertWildcardLoc, diag::availability_query_wildcard_required)
13491349
.fixItInsertAfter(InsertWildcardLoc, ", *");
13501350
}
13511351
break;
13521352
}
13531353
case Parser::AvailabilitySpecSource::Unavailable: {
1354-
if (OtherPlatformSpecLoc != std::nullopt) {
1355-
SourceLoc Loc = OtherPlatformSpecLoc.value();
1354+
if (WildcardSpecLoc != std::nullopt) {
1355+
SourceLoc Loc = WildcardSpecLoc.value();
13561356
P.diagnose(Loc, diag::unavailability_query_wildcard_not_required)
13571357
.fixItRemove(Loc);
13581358
}
13591359
break;
13601360
}
13611361
case Parser::AvailabilitySpecSource::Macro: {
1362-
if (OtherPlatformSpecLoc != std::nullopt) {
1363-
SourceLoc Loc = OtherPlatformSpecLoc.value();
1362+
if (WildcardSpecLoc != std::nullopt) {
1363+
SourceLoc Loc = WildcardSpecLoc.value();
13641364
P.diagnose(Loc, diag::attr_availability_wildcard_in_macro);
13651365
}
13661366
break;

lib/Sema/DerivedConformanceRawRepresentable.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ struct RuntimeVersionCheck {
203203
auto platformSpec = new (C) PlatformVersionConstraintAvailabilitySpec(
204204
Platform, SourceLoc(), Version, SourceLoc());
205205

206-
// otherSpec = "*"
207-
auto otherSpec = new (C) OtherPlatformAvailabilitySpec(SourceLoc());
206+
// wildcardSpec = "*"
207+
auto wildcardSpec = AvailabilitySpec::createWildcard(C, SourceLoc());
208208

209-
// availableInfo = "#available(\(platformSpec), \(otherSpec))"
209+
// availableInfo = "#available(\(platformSpec), \(wildcardSpec))"
210210
auto availableInfo = PoundAvailableInfo::create(
211-
C, SourceLoc(), SourceLoc(), { platformSpec, otherSpec }, SourceLoc(),
211+
C, SourceLoc(), SourceLoc(), {platformSpec, wildcardSpec}, SourceLoc(),
212212
false);
213213

214214
// This won't be filled in by TypeCheckAvailability because we have

lib/Sema/TypeCheckAvailability.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ class AvailabilityScopeBuilder : private ASTWalker {
12731273
Query->setVariantAvailableRange(VariantRange);
12741274
}
12751275

1276-
if (Spec->getKind() == AvailabilitySpecKind::OtherPlatform) {
1276+
if (Spec->isWildcard()) {
12771277
// The wildcard spec '*' represents the minimum deployment target, so
12781278
// there is no need to create an availability scope for this query.
12791279
// Further, we won't diagnose for useless #available() conditions
@@ -1379,12 +1379,12 @@ class AvailabilityScopeBuilder : private ASTWalker {
13791379
/// such spec exists.
13801380
AvailabilitySpec *bestActiveSpecForQuery(PoundAvailableInfo *available,
13811381
bool forTargetVariant = false) {
1382-
OtherPlatformAvailabilitySpec *FoundOtherSpec = nullptr;
1382+
AvailabilitySpec *FoundWildcardSpec = nullptr;
13831383
PlatformVersionConstraintAvailabilitySpec *BestSpec = nullptr;
13841384

13851385
for (auto *Spec : available->getQueries()) {
1386-
if (auto *OtherSpec = dyn_cast<OtherPlatformAvailabilitySpec>(Spec)) {
1387-
FoundOtherSpec = OtherSpec;
1386+
if (Spec->isWildcard()) {
1387+
FoundWildcardSpec = Spec;
13881388
continue;
13891389
}
13901390

@@ -1412,12 +1412,12 @@ class AvailabilityScopeBuilder : private ASTWalker {
14121412

14131413
// If we have reached this point, we found no spec for our target, so
14141414
// we return the other spec ('*'), if we found it, or nullptr, if not.
1415-
if (FoundOtherSpec) {
1416-
return FoundOtherSpec;
1415+
if (FoundWildcardSpec) {
1416+
return FoundWildcardSpec;
14171417
} else if (available->isUnavailability()) {
14181418
// For #unavailable, imply the presence of a wildcard.
14191419
SourceLoc Loc = available->getRParenLoc();
1420-
return new (Context) OtherPlatformAvailabilitySpec(Loc);
1420+
return AvailabilitySpec::createWildcard(Context, Loc);
14211421
} else {
14221422
return nullptr;
14231423
}
@@ -1426,7 +1426,7 @@ class AvailabilityScopeBuilder : private ASTWalker {
14261426
/// Return the availability context for the given spec.
14271427
AvailabilityRange contextForSpec(AvailabilitySpec *Spec,
14281428
bool GetRuntimeContext) {
1429-
if (isa<OtherPlatformAvailabilitySpec>(Spec)) {
1429+
if (Spec->isWildcard()) {
14301430
return AvailabilityRange::alwaysAvailable();
14311431
}
14321432

0 commit comments

Comments
 (0)