Skip to content

Commit 8ebf2aa

Browse files
committed
AST: Add printing utilities for AvailabilityDomain and AvailabilitySpec.
1 parent 9c9ba68 commit 8ebf2aa

File tree

7 files changed

+52
-23
lines changed

7 files changed

+52
-23
lines changed

include/swift/AST/Attr.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,20 +765,17 @@ class AvailableAttr : public DeclAttribute {
765765
/// has been resolved successfully.
766766
bool hasCachedDomain() const { return DomainOrIdentifier.isDomain(); }
767767

768+
AvailabilityDomainOrIdentifier getDomainOrIdentifier() const {
769+
return DomainOrIdentifier;
770+
}
771+
768772
/// Returns the `AvailabilityDomain` associated with the attribute, or
769773
/// `std::nullopt` if it has either not yet been resolved or could not be
770774
/// resolved successfully.
771775
std::optional<AvailabilityDomain> getCachedDomain() const {
772776
return DomainOrIdentifier.getAsDomain();
773777
}
774778

775-
/// If the attribute does not already have a cached `AvailabilityDomain`, this
776-
/// returns the domain identifier that was written in source, from which an
777-
/// `AvailabilityDomain` can be resolved.
778-
std::optional<Identifier> getDomainIdentifier() const {
779-
return DomainOrIdentifier.getAsIdentifier();
780-
}
781-
782779
SourceLoc getDomainLoc() const { return DomainLoc; }
783780

784781
/// Returns the parsed version for `introduced:`.

include/swift/AST/AvailabilityDomain.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,19 @@ class AvailabilityDomain final {
248248
ID.AddPointer(getOpaqueValue());
249249
}
250250

251+
void print(llvm::raw_ostream &os) const;
252+
251253
private:
252254
friend class AvailabilityDomainOrIdentifier;
253255

254256
AvailabilityDomain copy(ASTContext &ctx) const;
255257
};
256258

259+
inline void simple_display(llvm::raw_ostream &os,
260+
const AvailabilityDomain &domain) {
261+
domain.print(os);
262+
}
263+
257264
/// Represents an availability domain that has been defined in a module.
258265
class CustomAvailabilityDomain : public ASTAllocated<CustomAvailabilityDomain> {
259266
public:
@@ -327,6 +334,8 @@ class AvailabilityDomainOrIdentifier {
327334
/// members of the original into the given `ASTContext` in case it is
328335
/// different than the context that the original was created for.
329336
AvailabilityDomainOrIdentifier copy(ASTContext &ctx) const;
337+
338+
void print(llvm::raw_ostream &os) const;
330339
};
331340

332341
} // end namespace swift

include/swift/AST/AvailabilitySpec.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
5656
: Storage(Storage), SrcRange(SrcRange), Version(Version),
5757
VersionStartLoc(VersionStartLoc) {}
5858

59-
AvailabilityDomainOrIdentifier getDomainOrIdentifier() const {
60-
return Storage.getPointer();
61-
}
6259

6360
public:
6461
/// Creates a wildcard availability specification that guards execution
@@ -101,6 +98,10 @@ class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
10198
return false;
10299
}
103100

101+
AvailabilityDomainOrIdentifier getDomainOrIdentifier() const {
102+
return Storage.getPointer();
103+
}
104+
104105
std::optional<AvailabilityDomain> getDomain() const {
105106
return getDomainOrIdentifier().getAsDomain();
106107
}
@@ -123,8 +124,15 @@ class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
123124
// Location of the macro expanded to create this spec.
124125
SourceLoc getMacroLoc() const { return MacroLoc; }
125126
void setMacroLoc(SourceLoc loc) { MacroLoc = loc; }
127+
128+
void print(llvm::raw_ostream &os) const;
126129
};
127130

131+
inline void simple_display(llvm::raw_ostream &os,
132+
const AvailabilitySpec *spec) {
133+
spec->print(os);
134+
}
135+
128136
/// The type-checked representation of `AvailabilitySpec` which guaranatees that
129137
/// the spec has a valid `AvailabilityDomain`.
130138
class SemanticAvailabilitySpec {
@@ -153,6 +161,8 @@ class SemanticAvailabilitySpec {
153161
// This is required to support beta versions of macOS Big Sur that
154162
// report 10.16 at run time.
155163
llvm::VersionTuple getRuntimeVersion() const { return spec->getRawVersion(); }
164+
165+
void print(llvm::raw_ostream &os) const { spec->print(os); }
156166
};
157167

158168
/// Wraps an array of availability specs and provides an iterator for their

lib/AST/ASTDumper.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,11 +1103,11 @@ namespace {
11031103
printRecArbitrary(
11041104
[&](Label label) {
11051105
printHead("availability_spec", PatternColor, label);
1106-
StringRef domainName =
1107-
Spec->isWildcard()
1108-
? "*"
1109-
: Spec->getDomain()->getNameForAttributePrinting();
1110-
printField(domainName, Label::always("domain"));
1106+
printFieldRaw(
1107+
[&](llvm::raw_ostream &OS) {
1108+
Spec->getDomainOrIdentifier().print(OS);
1109+
},
1110+
Label::always("domain"));
11111111
if (!Spec->getRawVersion().empty())
11121112
printFieldRaw(
11131113
[&](llvm::raw_ostream &OS) { OS << Spec->getRawVersion(); },
@@ -4944,13 +4944,8 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
49444944
void visitAvailableAttr(AvailableAttr *Attr, Label label) {
49454945
printCommon(Attr, "available_attr", label);
49464946

4947-
if (auto domain = Attr->getCachedDomain()) {
4948-
printField(domain->getNameForAttributePrinting(),
4949-
Label::always("domain"));
4950-
} else {
4951-
printField(*Attr->getDomainIdentifier(),
4952-
Label::always("domainIdentifier"));
4953-
}
4947+
printFieldRaw([&](auto &out) { Attr->getDomainOrIdentifier().print(out); },
4948+
Label::always("domain"));
49544949

49554950
switch (Attr->getKind()) {
49564951
case swift::AvailableAttr::Kind::Default:

lib/AST/Availability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
828828
auto domain = attr->getCachedDomain();
829829

830830
if (!domain) {
831-
auto domainIdentifier = attr->getDomainIdentifier();
831+
auto domainIdentifier = attr->getDomainOrIdentifier().getAsIdentifier();
832832
ASSERT(domainIdentifier);
833833

834834
// Attempt to resolve the domain specified for the attribute and diagnose

lib/AST/AvailabilityDomain.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ AvailabilityDomain AvailabilityDomain::getABICompatibilityDomain() const {
168168
return *this;
169169
}
170170

171+
void AvailabilityDomain::print(llvm::raw_ostream &os) const {
172+
os << getNameForAttributePrinting();
173+
}
174+
171175
AvailabilityDomain AvailabilityDomain::copy(ASTContext &ctx) const {
172176
switch (getKind()) {
173177
case Kind::Universal:
@@ -205,3 +209,10 @@ AvailabilityDomainOrIdentifier::copy(ASTContext &ctx) const {
205209
DEBUG_ASSERT(isDomain());
206210
return getAsDomain()->copy(ctx);
207211
}
212+
213+
void AvailabilityDomainOrIdentifier::print(llvm::raw_ostream &os) const {
214+
if (auto identifier = getAsIdentifier())
215+
os << identifier->str();
216+
else
217+
getAsDomain()->print(os);
218+
}

lib/AST/AvailabilitySpec.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ AvailabilitySpec *AvailabilitySpec::clone(ASTContext &ctx) const {
5454
SrcRange, Version, VersionStartLoc);
5555
}
5656

57+
void AvailabilitySpec::print(llvm::raw_ostream &os) const {
58+
getDomainOrIdentifier().print(os);
59+
60+
if (!getRawVersion().empty())
61+
os << " " << getRawVersion().getAsString();
62+
}
63+
5764
llvm::VersionTuple SemanticAvailabilitySpec::getVersion() const {
5865
// For macOS Big Sur, we canonicalize 10.16 to 11.0 for compile-time
5966
// checking since clang canonicalizes availability markup. However, to

0 commit comments

Comments
 (0)