Skip to content

Commit 09f8622

Browse files
committed
AST: Simplify AvailabilitySpec subclasses further.
Remove methods that are specific to AvailabilitySpec subclasses and replace them with methods on the superclass if necessary. NFC.
1 parent 735000f commit 09f8622

File tree

6 files changed

+25
-62
lines changed

6 files changed

+25
-62
lines changed

include/swift/AST/AvailabilitySpec.h

+6-27
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
7676
AvailabilitySpecKind getKind() const { return Kind; }
7777

7878
SourceRange getSourceRange() const { return SrcRange; }
79+
SourceLoc getStartLoc() const { return SrcRange.Start; }
7980

8081
std::optional<AvailabilityDomain> getDomain() const { return Domain; }
8182

82-
std::optional<PlatformKind> getPlatform() const;
83+
PlatformKind getPlatform() const {
84+
if (auto domain = getDomain())
85+
return domain->getPlatformKind();
86+
return PlatformKind::none;
87+
}
8388

8489
// The platform version to compare against.
8590
llvm::VersionTuple getVersion() const;
@@ -114,22 +119,6 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
114119
getDomainForPlatform(Platform),
115120
SourceRange(PlatformLoc, VersionSrcRange.End), Version,
116121
VersionSrcRange.Start) {}
117-
118-
/// The required platform.
119-
PlatformKind getPlatform() const {
120-
if (auto domain = getDomain())
121-
return domain->getPlatformKind();
122-
return PlatformKind::none;
123-
}
124-
SourceLoc getPlatformLoc() const { return getSourceRange().Start; }
125-
126-
/// Returns true when the constraint is for a platform that was not
127-
/// recognized. This enables better recovery during parsing but should never
128-
/// be true after parsing is completed.
129-
bool isUnrecognizedPlatform() const {
130-
return getPlatform() == PlatformKind::none;
131-
}
132-
133122
// The version to be used in codegen for version comparisons at run time.
134123
// This is required to support beta versions of macOS Big Sur that
135124
// report 10.16 at run time.
@@ -180,14 +169,6 @@ class PlatformAgnosticVersionConstraintAvailabilitySpec
180169
AvailabilitySpecKind == AvailabilitySpecKind::PackageDescriptionVersionConstraint);
181170
}
182171

183-
SourceLoc getPlatformAgnosticNameLoc() const {
184-
return getSourceRange().Start;
185-
}
186-
187-
bool isLanguageVersionSpecific() const {
188-
return getKind() == AvailabilitySpecKind::LanguageVersionConstraint;
189-
}
190-
191172
void print(raw_ostream &OS, unsigned Indent) const;
192173

193174
static bool classof(const AvailabilitySpec *Spec) {
@@ -220,8 +201,6 @@ class OtherPlatformAvailabilitySpec : public AvailabilitySpec {
220201
/*Version=*/{},
221202
/*VersionStartLoc=*/{}) {}
222203

223-
SourceLoc getStarLoc() const { return getSourceRange().Start; }
224-
225204
void print(raw_ostream &OS, unsigned Indent) const;
226205

227206
static bool classof(const AvailabilitySpec *Spec) {

lib/AST/ASTDumper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ namespace {
11181118
printHead("platform_agnostic_version_constraint_"
11191119
"availability_spec",
11201120
PatternColor, label);
1121-
printField(agnostic->isLanguageVersionSpecific()
1121+
printField(agnostic->getDomain()->isSwiftLanguage()
11221122
? "swift"
11231123
: "package_description",
11241124
Label::always("kind"));

lib/AST/AvailabilitySpec.cpp

+1-15
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,6 @@
2121

2222
using namespace swift;
2323

24-
std::optional<PlatformKind> AvailabilitySpec::getPlatform() const {
25-
switch (getKind()) {
26-
case AvailabilitySpecKind::PlatformVersionConstraint: {
27-
auto spec = cast<PlatformVersionConstraintAvailabilitySpec>(this);
28-
return spec->getPlatform();
29-
}
30-
case AvailabilitySpecKind::LanguageVersionConstraint:
31-
case AvailabilitySpecKind::PackageDescriptionVersionConstraint:
32-
case AvailabilitySpecKind::OtherPlatform:
33-
return std::nullopt;
34-
}
35-
llvm_unreachable("bad AvailabilitySpecKind");
36-
}
37-
3824
llvm::VersionTuple AvailabilitySpec::getVersion() const {
3925
switch (getKind()) {
4026
case AvailabilitySpecKind::PlatformVersionConstraint: {
@@ -77,7 +63,7 @@ void PlatformAgnosticVersionConstraintAvailabilitySpec::print(raw_ostream &OS,
7763
OS.indent(Indent) << '('
7864
<< "platform_agnostic_version_constraint_availability_spec"
7965
<< " kind='"
80-
<< (isLanguageVersionSpecific() ?
66+
<< (getDomain()->isSwiftLanguage() ?
8167
"swift" : "package_description")
8268
<< "'"
8369
<< " version='" << getVersion() << "'"

lib/AST/Bridging/AvailabilityBridging.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ BridgedAvailabilitySpec_getDomain(BridgedAvailabilitySpec spec) {
101101

102102
BridgedPlatformKind
103103
BridgedAvailabilitySpec_getPlatform(BridgedAvailabilitySpec spec) {
104-
auto platform = spec.unbridged()->getPlatform();
105-
if (platform)
106-
return bridge(*platform);
107-
return BridgedPlatformKind_None;
104+
return bridge(spec.unbridged()->getPlatform());
108105
}
109106

110107
BridgedVersionTuple

lib/Parse/ParseDecl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1877,15 +1877,15 @@ ParserStatus Parser::parsePlatformVersionInList(StringRef AttrName,
18771877
auto Platform = Spec->getPlatform();
18781878
// Since peekAvailabilityMacroName() only matches defined availability
18791879
// macros, we only expect platform specific constraints here.
1880-
assert(Platform && "Unexpected AvailabilitySpec kind");
1880+
DEBUG_ASSERT(Platform != PlatformKind::none);
18811881

18821882
auto Version = Spec->getVersion();
18831883
if (Version.getSubminor().has_value() || Version.getBuild().has_value()) {
18841884
diagnose(Spec->getVersionSrcRange().Start,
18851885
diag::attr_availability_platform_version_major_minor_only,
18861886
AttrName);
18871887
}
1888-
PlatformAndVersions.emplace_back(*Platform, Version);
1888+
PlatformAndVersions.emplace_back(Platform, Version);
18891889
}
18901890

18911891
return makeParserSuccess();

lib/Parse/ParseStmt.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -1306,24 +1306,25 @@ validateAvailabilitySpecList(Parser &P,
13061306
for (auto *Spec : Specs) {
13071307
RecognizedSpecs.push_back(Spec);
13081308
if (auto *OtherPlatSpec = dyn_cast<OtherPlatformAvailabilitySpec>(Spec)) {
1309-
OtherPlatformSpecLoc = OtherPlatSpec->getStarLoc();
1309+
OtherPlatformSpecLoc = OtherPlatSpec->getStartLoc();
13101310
continue;
13111311
}
13121312

13131313
if (auto *PlatformAgnosticSpec =
13141314
dyn_cast<PlatformAgnosticVersionConstraintAvailabilitySpec>(Spec)) {
1315-
P.diagnose(PlatformAgnosticSpec->getPlatformAgnosticNameLoc(),
1315+
bool isLanguageVersionSpecific = PlatformAgnosticSpec->getDomain()->isSwiftLanguage();
1316+
P.diagnose(PlatformAgnosticSpec->getStartLoc(),
13161317
diag::availability_must_occur_alone,
1317-
PlatformAgnosticSpec->isLanguageVersionSpecific()
1318-
? "swift"
1319-
: "_PackageDescription");
1318+
isLanguageVersionSpecific
1319+
? "swift"
1320+
: "_PackageDescription");
13201321
continue;
13211322
}
13221323

13231324
auto *VersionSpec = cast<PlatformVersionConstraintAvailabilitySpec>(Spec);
13241325
// We keep specs for unrecognized platforms around for error recovery
13251326
// during parsing but remove them once parsing is completed.
1326-
if (VersionSpec->isUnrecognizedPlatform()) {
1327+
if (!VersionSpec->getDomain().has_value()) {
13271328
RecognizedSpecs.pop_back();
13281329
continue;
13291330
}
@@ -1334,7 +1335,7 @@ validateAvailabilitySpecList(Parser &P,
13341335
// For example, we emit an error for
13351336
/// #available(OSX 10.10, OSX 10.11, *)
13361337
PlatformKind Platform = VersionSpec->getPlatform();
1337-
P.diagnose(VersionSpec->getPlatformLoc(),
1338+
P.diagnose(VersionSpec->getStartLoc(),
13381339
diag::availability_query_repeated_platform,
13391340
platformString(Platform));
13401341
}
@@ -1404,8 +1405,9 @@ ParserResult<PoundAvailableInfo> Parser::parseStmtConditionPoundAvailable() {
14041405
for (auto *Spec : Specs) {
14051406
if (auto *PlatformAgnostic =
14061407
dyn_cast<PlatformAgnosticVersionConstraintAvailabilitySpec>(Spec)) {
1407-
diagnose(PlatformAgnostic->getPlatformAgnosticNameLoc(),
1408-
PlatformAgnostic->isLanguageVersionSpecific()
1408+
bool isLanguageVersionSpecific = PlatformAgnostic->getDomain()->isSwiftLanguage();
1409+
diagnose(PlatformAgnostic->getStartLoc(),
1410+
isLanguageVersionSpecific
14091411
? diag::pound_available_swift_not_allowed
14101412
: diag::pound_available_package_description_not_allowed,
14111413
getTokenText(MainToken));
@@ -1551,11 +1553,10 @@ Parser::parseAvailabilitySpecList(SmallVectorImpl<AvailabilitySpec *> &Specs,
15511553
auto *PlatformSpec =
15521554
cast<PlatformVersionConstraintAvailabilitySpec>(Previous);
15531555

1554-
auto PlatformNameEndLoc =
1555-
Lexer::getLocForEndOfToken(SourceManager,
1556-
PlatformSpec->getPlatformLoc());
1556+
auto PlatformNameEndLoc = Lexer::getLocForEndOfToken(
1557+
SourceManager, PlatformSpec->getStartLoc());
15571558

1558-
diagnose(PlatformSpec->getPlatformLoc(),
1559+
diagnose(PlatformSpec->getStartLoc(),
15591560
diag::avail_query_meant_introduced)
15601561
.fixItInsert(PlatformNameEndLoc, ", introduced:");
15611562
}

0 commit comments

Comments
 (0)