Skip to content

Commit c2ef734

Browse files
authored
Merge pull request swiftlang#84134 from tshortli/print-available-and-has-symbol-conds
AST: Add printing support for `#available` and `#_hasSymbol` conditions
2 parents 89c3f47 + e0eba4e commit c2ef734

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
11411141

11421142
void printArgumentList(ArgumentList *args, bool forSubscript = false);
11431143

1144+
void printAvailabilitySpec(AvailabilitySpec *spec);
1145+
11441146
void printStmtCondition(StmtCondition stmt);
11451147

11461148
#define DECL(Name,Parent) void visit##Name##Decl(Name##Decl *decl);
@@ -5697,19 +5699,58 @@ void PrintAST::visitRepeatWhileStmt(RepeatWhileStmt *stmt) {
56975699
visit(stmt->getCond());
56985700
}
56995701

5702+
void PrintAST::printAvailabilitySpec(AvailabilitySpec *spec) {
5703+
auto domainOrIdentifier = spec->getDomainOrIdentifier();
5704+
if (auto domain = domainOrIdentifier.getAsDomain()) {
5705+
Printer << domain->getNameForAttributePrinting();
5706+
} else {
5707+
Printer << domainOrIdentifier.getAsIdentifier().value();
5708+
}
5709+
5710+
if (!spec->getRawVersion().empty())
5711+
Printer << " " << spec->getRawVersion().getAsString();
5712+
}
5713+
57005714
void PrintAST::printStmtCondition(StmtCondition condition) {
57015715
interleave(
57025716
condition,
57035717
[&](StmtConditionElement &elt) {
5704-
if (auto pattern = elt.getPatternOrNull()) {
5705-
printPattern(pattern);
5706-
auto initializer = elt.getInitializer();
5707-
if (initializer) {
5718+
switch (elt.getKind()) {
5719+
case StmtConditionElement::ConditionKind::CK_Boolean:
5720+
visit(elt.getBoolean());
5721+
break;
5722+
5723+
case StmtConditionElement::ConditionKind::CK_PatternBinding:
5724+
printPattern(elt.getPattern());
5725+
if (auto initializer = elt.getInitializer()) {
57085726
Printer << " = ";
57095727
visit(initializer);
57105728
}
5711-
} else if (auto boolean = elt.getBooleanOrNull()) {
5712-
visit(boolean);
5729+
break;
5730+
5731+
case StmtConditionElement::ConditionKind::CK_Availability:
5732+
if (auto availability = elt.getAvailability()) {
5733+
Printer << (availability->isUnavailability()
5734+
? tok::pound_unavailable
5735+
: tok::pound_available)
5736+
<< "(";
5737+
5738+
interleave(
5739+
availability->getQueries(),
5740+
[&](AvailabilitySpec *spec) { printAvailabilitySpec(spec); },
5741+
[&] { Printer << ", "; });
5742+
5743+
Printer << ")";
5744+
}
5745+
break;
5746+
5747+
case StmtConditionElement::ConditionKind::CK_HasSymbol:
5748+
if (auto hasSymbolInfo = elt.getHasSymbolInfo()) {
5749+
Printer << tok::pound__hasSymbol << "(";
5750+
visit(hasSymbolInfo->getSymbolExpr());
5751+
Printer << ")";
5752+
}
5753+
break;
57135754
}
57145755
},
57155756
[&] { Printer << ", "; });

test/expr/print/conditions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ if (5 + 5) == 9 {
1414
// CHECK: } else {
1515
// CHECK: }
1616

17+
if #available(macOS 11, iOS 17, *) {
18+
} else if #unavailable(watchOS 11) {
19+
}
20+
// CHECK: if #available(macOS 11, iOS 17, *) {
21+
// CHECK: } else if #unavailable(watchOS 11) {
22+
// CHECK: }
23+
24+
if #_hasSymbol(Int.self) {
25+
}
26+
// CHECK: if #_hasSymbol(Int.self) {
27+
// CHECK: }
28+
1729
guard (5 + 5) == 10 else {
1830
}
1931
// CHECK: guard (5 + 5) == 10 else {

0 commit comments

Comments
 (0)