Skip to content

Commit 018234f

Browse files
committed
AST: Remove IsSPI from AvailabilityContext.
It only existed to make `Decl::isAvailableAsSPI()` convenient to implement. NFC.
1 parent cfb0d35 commit 018234f

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

include/swift/AST/Availability.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,10 @@ class UnavailabilityReason {
230230
/// to create an \c AvailabilityContext, rather than creating one directly.
231231
class AvailabilityContext {
232232
VersionRange OSVersion;
233-
std::optional<bool> SPI;
234233

235234
public:
236235
/// Creates a context that requires certain versions of the target OS.
237-
explicit AvailabilityContext(VersionRange OSVersion,
238-
std::optional<bool> SPI = std::nullopt)
239-
: OSVersion(OSVersion), SPI(SPI) {}
236+
explicit AvailabilityContext(VersionRange OSVersion) : OSVersion(OSVersion) {}
240237

241238
/// Creates a context that imposes the constraints of the ASTContext's
242239
/// deployment target.
@@ -332,12 +329,9 @@ class AvailabilityContext {
332329
OSVersion.unionWith(other.getOSVersion());
333330
}
334331

335-
bool isAvailableAsSPI() const { return SPI && *SPI; }
336-
337332
/// Returns a representation of this range as a string for debugging purposes.
338333
std::string getAsString() const {
339-
return "AvailabilityContext(" + OSVersion.getAsString() +
340-
(isAvailableAsSPI() ? ", spi" : "") + ")";
334+
return "AvailabilityContext(" + OSVersion.getAsString() + ")";
341335
}
342336
};
343337

@@ -358,9 +352,12 @@ class AvailabilityInference {
358352
static AvailabilityContext inferForType(Type t);
359353

360354
/// Returns the context where a declaration is available
361-
/// We assume a declaration without an annotation is always available.
355+
/// We assume a declaration without an annotation is always available.
362356
static AvailabilityContext availableRange(const Decl *D, ASTContext &C);
363357

358+
/// Returns true is the declaration is `@_spi_available`.
359+
static bool isAvailableAsSPI(const Decl *D, ASTContext &C);
360+
364361
/// Returns the availability context for a declaration with the given
365362
/// @available attribute.
366363
///

lib/AST/Availability.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ AvailabilityInference::annotatedAvailableRange(const Decl *D, ASTContext &Ctx) {
423423
}
424424

425425
bool Decl::isAvailableAsSPI() const {
426-
return AvailabilityInference::availableRange(this, getASTContext())
427-
.isAvailableAsSPI();
426+
return AvailabilityInference::isAvailableAsSPI(this, getASTContext());
428427
}
429428

430429
std::optional<AvailableAttrDeclPair>
@@ -550,13 +549,10 @@ AvailabilityInference::annotatedAvailableRangeForAttr(const SpecializeAttr* attr
550549
return AvailabilityContext::alwaysAvailable();
551550
}
552551

553-
AvailabilityContext AvailabilityInference::availableRange(const Decl *D,
554-
ASTContext &Ctx) {
555-
std::optional<AvailabilityContext> AnnotatedRange =
556-
annotatedAvailableRange(D, Ctx);
557-
if (AnnotatedRange.has_value()) {
558-
return AnnotatedRange.value();
559-
}
552+
static const AvailableAttr *attrForAvailableRange(const Decl *D,
553+
ASTContext &Ctx) {
554+
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(D, Ctx))
555+
return attr;
560556

561557
// Unlike other declarations, extensions can be used without referring to them
562558
// by name (they don't have one) in the source. For this reason, when checking
@@ -568,16 +564,30 @@ AvailabilityContext AvailabilityInference::availableRange(const Decl *D,
568564

569565
DeclContext *DC = D->getDeclContext();
570566
if (auto *ED = dyn_cast<ExtensionDecl>(DC)) {
571-
AnnotatedRange = annotatedAvailableRange(ED, Ctx);
572-
if (AnnotatedRange.has_value()) {
573-
return AnnotatedRange.value();
574-
}
567+
if (auto attr =
568+
AvailabilityInference::attrForAnnotatedAvailableRange(ED, Ctx))
569+
return attr;
575570
}
576571

572+
return nullptr;
573+
}
574+
575+
AvailabilityContext AvailabilityInference::availableRange(const Decl *D,
576+
ASTContext &Ctx) {
577+
if (auto attr = attrForAvailableRange(D, Ctx))
578+
return availableRange(attr, Ctx);
579+
577580
// Treat unannotated declarations as always available.
578581
return AvailabilityContext::alwaysAvailable();
579582
}
580583

584+
bool AvailabilityInference::isAvailableAsSPI(const Decl *D, ASTContext &Ctx) {
585+
if (auto attr = attrForAvailableRange(D, Ctx))
586+
return attr->IsSPI;
587+
588+
return false;
589+
}
590+
581591
AvailabilityContext
582592
AvailabilityInference::availableRange(const AvailableAttr *attr,
583593
ASTContext &Ctx) {
@@ -590,8 +600,7 @@ AvailabilityInference::availableRange(const AvailableAttr *attr,
590600
attr, Ctx, Platform, RemappedIntroducedVersion))
591601
IntroducedVersion = RemappedIntroducedVersion;
592602

593-
return AvailabilityContext{VersionRange::allGTE(IntroducedVersion),
594-
attr->IsSPI};
603+
return AvailabilityContext{VersionRange::allGTE(IntroducedVersion)};
595604
}
596605

597606
namespace {

0 commit comments

Comments
 (0)