Skip to content

Commit 6b29700

Browse files
authored
Merge pull request swiftlang#83162 from tshortli/misc-availability-cleanups
Miscellaneous availability cleanups
2 parents 6d3d51f + 634f281 commit 6b29700

12 files changed

+150
-55
lines changed

include/swift/AST/AvailabilityConstraint.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/PlatformKindUtils.h"
2424
#include "swift/Basic/LLVM.h"
2525
#include "swift/Basic/OptionSet.h"
26+
#include "llvm/Support/raw_ostream.h"
2627

2728
namespace swift {
2829

@@ -140,6 +141,8 @@ class AvailabilityConstraint {
140141
/// Some availability constraints are active for type-checking but cannot
141142
/// be translated directly into an `if #available(...)` runtime query.
142143
bool isActiveForRuntimeQueries(const ASTContext &ctx) const;
144+
145+
void print(raw_ostream &os) const;
143146
};
144147

145148
/// Represents a set of availability constraints that restrict use of a
@@ -161,6 +164,8 @@ class DeclAvailabilityConstraints {
161164
using const_iterator = Storage::const_iterator;
162165
const_iterator begin() const { return constraints.begin(); }
163166
const_iterator end() const { return constraints.end(); }
167+
168+
void print(raw_ostream &os) const;
164169
};
165170

166171
enum class AvailabilityConstraintFlag : uint8_t {
@@ -191,4 +196,21 @@ DeclAvailabilityConstraints getAvailabilityConstraintsForDecl(
191196
AvailabilityConstraintFlags flags = std::nullopt);
192197
} // end namespace swift
193198

199+
namespace llvm {
200+
201+
inline llvm::raw_ostream &
202+
operator<<(llvm::raw_ostream &os,
203+
const swift::AvailabilityConstraint &constraint) {
204+
constraint.print(os);
205+
return os;
206+
}
207+
208+
inline llvm::raw_ostream &
209+
operator<<(llvm::raw_ostream &os,
210+
const swift::DeclAvailabilityConstraints &constraints) {
211+
constraints.print(os);
212+
return os;
213+
}
214+
215+
} // end namespace llvm
194216
#endif

include/swift/AST/AvailabilityContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/PlatformKindUtils.h"
2424
#include "swift/Basic/Debug.h"
2525
#include "swift/Basic/LLVM.h"
26+
#include "llvm/Support/raw_ostream.h"
2627
#include <optional>
2728

2829
namespace swift {
@@ -148,4 +149,14 @@ class AvailabilityContext {
148149

149150
} // end namespace swift
150151

152+
namespace llvm {
153+
154+
inline llvm::raw_ostream &
155+
operator<<(llvm::raw_ostream &os, const swift::AvailabilityContext &context) {
156+
context.print(os);
157+
return os;
158+
}
159+
160+
} // end namespace llvm
161+
151162
#endif

include/swift/AST/AvailabilityDomain.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/FoldingSet.h"
2929
#include "llvm/ADT/PointerEmbeddedInt.h"
3030
#include "llvm/ADT/PointerUnion.h"
31+
#include "llvm/Support/raw_ostream.h"
3132

3233
namespace swift {
3334
class ASTContext;
@@ -521,6 +522,12 @@ struct PointerLikeTypeTraits<swift::AvailabilityDomainOrIdentifier> {
521522
};
522523
};
523524

525+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
526+
const swift::AvailabilityDomain &domain) {
527+
domain.print(os);
528+
return os;
529+
}
530+
524531
} // end namespace llvm
525532

526533
#endif

include/swift/AST/AvailabilityInference.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class SemanticAvailableAttr;
3131

3232
class AvailabilityInference {
3333
public:
34-
/// Returns the decl that should be considered the parent decl of the given
35-
/// decl when looking for inherited availability annotations.
36-
static const Decl *parentDeclForInferredAvailability(const Decl *D);
37-
3834
/// Infers the common availability required to access an array of
3935
/// declarations and adds attributes reflecting that availability
4036
/// to ToDecl.
@@ -47,9 +43,6 @@ class AvailabilityInference {
4743
/// Returns the range of platform versions in which the decl is available.
4844
static AvailabilityRange availableRange(const Decl *D);
4945

50-
/// Returns true is the declaration is `@_spi_available`.
51-
static bool isAvailableAsSPI(const Decl *D);
52-
5346
/// Returns the context for which the declaration
5447
/// is annotated as available, or None if the declaration
5548
/// has no availability annotation.

include/swift/AST/AvailabilityRange.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "swift/Basic/LLVM.h"
2222
#include "llvm/ADT/FoldingSet.h"
2323
#include "llvm/Support/VersionTuple.h"
24-
#include <optional>
24+
#include "llvm/Support/raw_ostream.h"
2525

2626
namespace swift {
2727
class ASTContext;
@@ -317,11 +317,20 @@ class AvailabilityRange {
317317

318318
/// Returns a representation of this range as a string for debugging purposes.
319319
std::string getAsString() const {
320-
return "AvailabilityRange(" + getVersionString() + ")";
320+
return "AvailabilityRange(" + getVersionDescription() + ")";
321321
}
322322

323-
/// Returns a representation of the raw version range as a string for
323+
/// Returns a representation of the version range as a string for
324324
/// debugging purposes.
325+
std::string getVersionDescription() const {
326+
if (Range.hasLowerEndpoint())
327+
return Range.getLowerEndpoint().getAsString();
328+
else
329+
return Range.isEmpty() ? "never" : "always";
330+
}
331+
332+
/// Returns a string representation of the raw version range. It is an error
333+
/// to call this if the range is "always" or "never".
325334
std::string getVersionString() const {
326335
ASSERT(Range.hasLowerEndpoint());
327336
return Range.getLowerEndpoint().getAsString();
@@ -330,4 +339,20 @@ class AvailabilityRange {
330339

331340
} // end namespace swift
332341

342+
namespace llvm {
343+
344+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
345+
const swift::VersionRange &range) {
346+
os << range.getAsString();
347+
return os;
348+
}
349+
350+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
351+
const swift::AvailabilityRange &range) {
352+
os << range.getAsString();
353+
return os;
354+
}
355+
356+
} // namespace llvm
357+
333358
#endif

include/swift/AST/AvailabilitySpec.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ADT/StringMap.h"
2828
#include "llvm/ADT/iterator_range.h"
2929
#include "llvm/Support/VersionTuple.h"
30+
#include "llvm/Support/raw_ostream.h"
3031

3132
namespace swift {
3233
class ASTContext;
@@ -241,4 +242,20 @@ class AvailabilityMacroMap {
241242

242243
} // end namespace swift
243244

245+
namespace llvm {
246+
247+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
248+
const swift::AvailabilitySpec *spec) {
249+
spec->print(os);
250+
return os;
251+
}
252+
253+
inline llvm::raw_ostream &
254+
operator<<(llvm::raw_ostream &os, const swift::SemanticAvailabilitySpec &spec) {
255+
spec.print(os);
256+
return os;
257+
}
258+
259+
} // end namespace llvm
260+
244261
#endif

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
15211521
/// compatibility mode.
15221522
bool requiresUnavailableDeclABICompatibilityStubs() const;
15231523

1524+
/// Returns the decl that should be considered the parent decl when looking
1525+
/// for inherited availability annotations.
1526+
const Decl *parentDeclForAvailability() const;
1527+
15241528
// List the SPI groups declared with @_spi or inherited by this decl.
15251529
//
15261530
// SPI groups are inherited from the parent contexts only if the local decl

lib/AST/Availability.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void AvailabilityInference::applyInferredAvailableAttrs(
197197

198198
// Walk up the enclosing declaration hierarchy to make sure we aren't
199199
// missing any inherited attributes.
200-
D = AvailabilityInference::parentDeclForInferredAvailability(D);
200+
D = D->parentDeclForAvailability();
201201
} while (D);
202202
}
203203

@@ -213,32 +213,31 @@ void AvailabilityInference::applyInferredAvailableAttrs(
213213

214214
/// Returns the decl that should be considered the parent decl of the given decl
215215
/// when looking for inherited availability annotations.
216-
const Decl *
217-
AvailabilityInference::parentDeclForInferredAvailability(const Decl *D) {
218-
if (auto *AD = dyn_cast<AccessorDecl>(D))
216+
const Decl *Decl::parentDeclForAvailability() const {
217+
if (auto *AD = dyn_cast<AccessorDecl>(this))
219218
return AD->getStorage();
220219

221-
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
220+
if (auto *ED = dyn_cast<ExtensionDecl>(this)) {
222221
if (auto *NTD = ED->getExtendedNominal())
223222
return NTD;
224223
}
225224

226-
if (auto *PBD = dyn_cast<PatternBindingDecl>(D)) {
225+
if (auto *PBD = dyn_cast<PatternBindingDecl>(this)) {
227226
if (PBD->getNumPatternEntries() < 1)
228227
return nullptr;
229228

230229
return PBD->getAnchoringVarDecl(0);
231230
}
232231

233-
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(D))
232+
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(this))
234233
return OTD->getNamingDecl();
235234

236235
// Clang decls may be inaccurately parented rdar://53956555
237-
if (D->hasClangNode())
236+
if (hasClangNode())
238237
return nullptr;
239238

240239
// Availability is inherited from the enclosing context.
241-
return D->getDeclContext()->getInnermostDeclarationDeclContext();
240+
return getDeclContext()->getInnermostDeclarationDeclContext();
242241
}
243242

244243
/// Returns true if the introduced version in \p newAttr should be used instead
@@ -408,7 +407,10 @@ AvailabilityInference::annotatedAvailableRange(const Decl *D) {
408407
}
409408

410409
bool Decl::isAvailableAsSPI() const {
411-
return AvailabilityInference::isAvailableAsSPI(this);
410+
if (auto attr = getAvailableAttrForPlatformIntroduction())
411+
return attr->isSPI();
412+
413+
return false;
412414
}
413415

414416
SemanticAvailableAttributes
@@ -696,10 +698,8 @@ DeclRuntimeAvailability
696698
DeclRuntimeAvailabilityRequest::evaluate(Evaluator &evaluator,
697699
const Decl *decl) const {
698700
auto inherited = DeclRuntimeAvailability::PotentiallyAvailable;
699-
if (auto *parent =
700-
AvailabilityInference::parentDeclForInferredAvailability(decl)) {
701+
if (auto *parent = decl->parentDeclForAvailability())
701702
inherited = getDeclRuntimeAvailability(parent);
702-
}
703703

704704
// If the inherited runtime availability is already maximally unavailable
705705
// then skip computing unavailability for this declaration.
@@ -785,8 +785,7 @@ Decl::getAvailableAttrForPlatformIntroduction(bool checkExtension) const {
785785
if (!checkExtension)
786786
return std::nullopt;
787787

788-
if (auto parent =
789-
AvailabilityInference::parentDeclForInferredAvailability(this)) {
788+
if (auto parent = parentDeclForAvailability()) {
790789
if (auto *ED = dyn_cast<ExtensionDecl>(parent)) {
791790
if (auto attr = getDeclAvailableAttrForPlatformIntroduction(ED))
792791
return attr;
@@ -804,13 +803,6 @@ AvailabilityRange AvailabilityInference::availableRange(const Decl *D) {
804803
return AvailabilityRange::alwaysAvailable();
805804
}
806805

807-
bool AvailabilityInference::isAvailableAsSPI(const Decl *D) {
808-
if (auto attr = D->getAvailableAttrForPlatformIntroduction())
809-
return attr->isSPI();
810-
811-
return false;
812-
}
813-
814806
std::optional<SemanticAvailableAttr>
815807
SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
816808
const AvailableAttr *attr,

lib/AST/AvailabilityConstraint.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "swift/AST/AvailabilityConstraint.h"
1414
#include "swift/AST/ASTContext.h"
1515
#include "swift/AST/AvailabilityContext.h"
16-
#include "swift/AST/AvailabilityInference.h"
1716
#include "swift/AST/Decl.h"
1817

1918
using namespace swift;
@@ -45,6 +44,27 @@ bool AvailabilityConstraint::isActiveForRuntimeQueries(
4544
/*forRuntimeQuery=*/true);
4645
}
4746

47+
void AvailabilityConstraint::print(llvm::raw_ostream &os) const {
48+
os << "AvailabilityConstraint(";
49+
getAttr().getDomain().print(os);
50+
os << ", ";
51+
52+
switch (getReason()) {
53+
case Reason::UnconditionallyUnavailable:
54+
os << "unavailable";
55+
break;
56+
case Reason::Obsoleted:
57+
os << "obsoleted: " << getAttr().getObsoleted().value();
58+
break;
59+
case Reason::UnavailableForDeployment:
60+
case Reason::PotentiallyUnavailable:
61+
os << "introduced: " << getAttr().getIntroduced().value();
62+
break;
63+
}
64+
65+
os << ")";
66+
}
67+
4868
static bool constraintIsStronger(const AvailabilityConstraint &lhs,
4969
const AvailabilityConstraint &rhs) {
5070
DEBUG_ASSERT(lhs.getDomain() == rhs.getDomain());
@@ -117,6 +137,17 @@ DeclAvailabilityConstraints::getPrimaryConstraint() const {
117137
return result;
118138
}
119139

140+
void DeclAvailabilityConstraints::print(llvm::raw_ostream &os) const {
141+
os << "{\n";
142+
llvm::interleave(
143+
constraints,
144+
[&os](const AvailabilityConstraint &constraint) {
145+
os << " " << constraint;
146+
},
147+
[&os] { os << ",\n"; });
148+
os << "\n}";
149+
}
150+
120151
static bool canIgnoreConstraintInUnavailableContexts(
121152
const Decl *decl, const AvailabilityConstraint &constraint,
122153
const AvailabilityConstraintFlags flags) {
@@ -293,9 +324,10 @@ swift::getAvailabilityConstraintsForDecl(const Decl *decl,
293324
if (decl->getClangNode())
294325
return constraints;
295326

296-
auto parent = AvailabilityInference::parentDeclForInferredAvailability(decl);
327+
auto parent = decl->parentDeclForAvailability();
297328
if (auto extension = dyn_cast_or_null<ExtensionDecl>(parent))
298329
getAvailabilityConstraintsForDecl(constraints, extension, context, flags);
299330

300331
return constraints;
301332
}
333+

lib/AST/Decl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ const Decl *Decl::getInnermostDeclWithAvailability() const {
591591
if (getAttrs().hasAttribute<AvailableAttr>())
592592
return this;
593593

594-
if (auto parent =
595-
AvailabilityInference::parentDeclForInferredAvailability(this))
594+
if (auto parent = parentDeclForAvailability())
596595
return parent->getInnermostDeclWithAvailability();
597596

598597
return nullptr;
@@ -1492,7 +1491,7 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
14921491
return *containingContext;
14931492
}
14941493

1495-
// FIXME: Adopt AvailabilityInference::parentDeclForInferredAvailability()
1494+
// FIXME: Adopt Decl::parentDeclForAvailability()
14961495
// here instead of duplicating the logic.
14971496
if (auto *accessor = dyn_cast<AccessorDecl>(this))
14981497
return accessor->getStorage()->getAvailabilityForLinkage();

0 commit comments

Comments
 (0)