Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ class PrintAST : public ASTVisitor<PrintAST> {

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

void printAvailabilitySpec(AvailabilitySpec *spec);

void printStmtCondition(StmtCondition stmt);

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

void PrintAST::printAvailabilitySpec(AvailabilitySpec *spec) {
auto domainOrIdentifier = spec->getDomainOrIdentifier();
if (auto domain = domainOrIdentifier.getAsDomain()) {
Printer << domain->getNameForAttributePrinting();
} else {
Printer << domainOrIdentifier.getAsIdentifier().value();
}

if (!spec->getRawVersion().empty())
Printer << " " << spec->getRawVersion().getAsString();
}

void PrintAST::printStmtCondition(StmtCondition condition) {
interleave(
condition,
[&](StmtConditionElement &elt) {
if (auto pattern = elt.getPatternOrNull()) {
printPattern(pattern);
auto initializer = elt.getInitializer();
if (initializer) {
switch (elt.getKind()) {
case StmtConditionElement::ConditionKind::CK_Boolean:
visit(elt.getBoolean());
break;

case StmtConditionElement::ConditionKind::CK_PatternBinding:
printPattern(elt.getPattern());
if (auto initializer = elt.getInitializer()) {
Printer << " = ";
visit(initializer);
}
} else if (auto boolean = elt.getBooleanOrNull()) {
visit(boolean);
break;

case StmtConditionElement::ConditionKind::CK_Availability:
if (auto availability = elt.getAvailability()) {
Printer << (availability->isUnavailability()
? tok::pound_unavailable
: tok::pound_available)
<< "(";

interleave(
availability->getQueries(),
[&](AvailabilitySpec *spec) { printAvailabilitySpec(spec); },
[&] { Printer << ", "; });

Printer << ")";
}
break;

case StmtConditionElement::ConditionKind::CK_HasSymbol:
if (auto hasSymbolInfo = elt.getHasSymbolInfo()) {
Printer << tok::pound__hasSymbol << "(";
visit(hasSymbolInfo->getSymbolExpr());
Printer << ")";
}
break;
}
},
[&] { Printer << ", "; });
Expand Down
12 changes: 12 additions & 0 deletions test/expr/print/conditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ if (5 + 5) == 9 {
// CHECK: } else {
// CHECK: }

if #available(macOS 11, iOS 17, *) {
} else if #unavailable(watchOS 11) {
}
// CHECK: if #available(macOS 11, iOS 17, *) {
// CHECK: } else if #unavailable(watchOS 11) {
// CHECK: }

if #_hasSymbol(Int.self) {
}
// CHECK: if #_hasSymbol(Int.self) {
// CHECK: }

guard (5 + 5) == 10 else {
}
// CHECK: guard (5 + 5) == 10 else {
Expand Down