Skip to content

Commit bca941b

Browse files
committed
[AST] NFC: Rename IfExpr -> TernaryExpr
This matches what we call it in SwiftSyntax, and is just generally clearer.
1 parent a6b5896 commit bca941b

25 files changed

+125
-133
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,12 +1342,12 @@ ERROR(legacy_object_literal,none,
13421342
ERROR(unknown_pound_expr,none,
13431343
"use of unknown directive '#%0'", (StringRef))
13441344

1345-
// If expressions
1346-
ERROR(expected_expr_after_if_question,none,
1345+
// Ternary expressions
1346+
ERROR(expected_expr_after_ternary_question,none,
13471347
"expected expression after '?' in ternary expression", ())
1348-
ERROR(expected_colon_after_if_question,none,
1348+
ERROR(expected_colon_after_ternary_question,none,
13491349
"expected ':' after '? ...' in ternary expression", ())
1350-
ERROR(expected_expr_after_if_colon,none,
1350+
ERROR(expected_expr_after_ternary_colon,none,
13511351
"expected expression after '? ... :' in ternary expression", ())
13521352
ERROR(expected_expr_after_try, none,
13531353
"expected expression after 'try'", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ ERROR(invalid_force_unwrap,none,
10701070
ERROR(invalid_optional_chain,none,
10711071
"cannot use optional chaining on non-optional value of type %0",
10721072
(Type))
1073-
ERROR(if_expr_cases_mismatch,none,
1073+
ERROR(ternary_expr_cases_mismatch,none,
10741074
"result values in '? :' expression have mismatching types %0 and %1",
10751075
(Type, Type))
10761076

include/swift/AST/Expr.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5061,25 +5061,21 @@ class RebindSelfInConstructorExpr : public Expr {
50615061
return E->getKind() == ExprKind::RebindSelfInConstructor;
50625062
}
50635063
};
5064-
5065-
/// The conditional expression 'x ? y : z'.
5066-
class IfExpr : public Expr {
5064+
5065+
/// The ternary conditional expression 'x ? y : z'.
5066+
class TernaryExpr : public Expr {
50675067
Expr *CondExpr, *ThenExpr, *ElseExpr;
50685068
SourceLoc QuestionLoc, ColonLoc;
50695069
public:
5070-
IfExpr(Expr *CondExpr,
5071-
SourceLoc QuestionLoc, Expr *ThenExpr,
5072-
SourceLoc ColonLoc, Expr *ElseExpr,
5073-
Type Ty = Type())
5074-
: Expr(ExprKind::If, /*Implicit=*/false, Ty),
5075-
CondExpr(CondExpr), ThenExpr(ThenExpr), ElseExpr(ElseExpr),
5076-
QuestionLoc(QuestionLoc), ColonLoc(ColonLoc)
5077-
{}
5078-
5079-
IfExpr(SourceLoc QuestionLoc, Expr *ThenExpr, SourceLoc ColonLoc)
5080-
: IfExpr(nullptr, QuestionLoc, ThenExpr, ColonLoc, nullptr)
5081-
{}
5082-
5070+
TernaryExpr(Expr *CondExpr, SourceLoc QuestionLoc, Expr *ThenExpr,
5071+
SourceLoc ColonLoc, Expr *ElseExpr, Type Ty = Type())
5072+
: Expr(ExprKind::Ternary, /*Implicit=*/false, Ty), CondExpr(CondExpr),
5073+
ThenExpr(ThenExpr), ElseExpr(ElseExpr), QuestionLoc(QuestionLoc),
5074+
ColonLoc(ColonLoc) {}
5075+
5076+
TernaryExpr(SourceLoc QuestionLoc, Expr *ThenExpr, SourceLoc ColonLoc)
5077+
: TernaryExpr(nullptr, QuestionLoc, ThenExpr, ColonLoc, nullptr) {}
5078+
50835079
SourceLoc getLoc() const { return QuestionLoc; }
50845080
SourceLoc getStartLoc() const {
50855081
return (isFolded() ? CondExpr->getStartLoc() : QuestionLoc);
@@ -5103,7 +5099,7 @@ class IfExpr : public Expr {
51035099
bool isFolded() const { return CondExpr && ElseExpr; }
51045100

51055101
static bool classof(const Expr *E) {
5106-
return E->getKind() == ExprKind::If;
5102+
return E->getKind() == ExprKind::Ternary;
51075103
}
51085104
};
51095105

@@ -5979,7 +5975,7 @@ class TypeJoinExpr final : public Expr,
59795975
};
59805976

59815977
inline bool Expr::isInfixOperator() const {
5982-
return isa<BinaryExpr>(this) || isa<IfExpr>(this) ||
5978+
return isa<BinaryExpr>(this) || isa<TernaryExpr>(this) ||
59835979
isa<AssignExpr>(this) || isa<ExplicitCastExpr>(this);
59845980
}
59855981

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ ABSTRACT_EXPR(ExplicitCast, Expr)
194194
EXPR(Coerce, ExplicitCastExpr)
195195
EXPR_RANGE(ExplicitCast, ForcedCheckedCast, Coerce)
196196
UNCHECKED_EXPR(Arrow, Expr)
197-
EXPR(If, Expr)
197+
EXPR(Ternary, Expr)
198198
EXPR(EnumIsCase, Expr)
199199
EXPR(Assign, Expr)
200200
EXPR(CodeCompletion, Expr)

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,8 +2714,8 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
27142714
printRec(E->getSubExpr());
27152715
PrintWithColorRAII(OS, ParenthesisColor) << ')';
27162716
}
2717-
void visitIfExpr(IfExpr *E) {
2718-
printCommon(E, "if_expr") << '\n';
2717+
void visitTernaryExpr(TernaryExpr *E) {
2718+
printCommon(E, "ternary_expr") << '\n';
27192719
printRec(E->getCondExpr());
27202720
OS << '\n';
27212721
printRec(E->getThenExpr());

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,8 +4473,7 @@ void PrintAST::visitErrorExpr(ErrorExpr *expr) {
44734473
Printer << "<error>";
44744474
}
44754475

4476-
void PrintAST::visitIfExpr(IfExpr *expr) {
4477-
}
4476+
void PrintAST::visitTernaryExpr(TernaryExpr *expr) {}
44784477

44794478
void PrintAST::visitIsExpr(IsExpr *expr) {
44804479
}

lib/AST/ASTVerifier.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,18 +2089,18 @@ class Verifier : public ASTWalker {
20892089
verifyCheckedBase(E);
20902090
}
20912091

2092-
void verifyChecked(IfExpr *E) {
2093-
PrettyStackTraceExpr debugStack(Ctx, "verifying IfExpr", E);
2092+
void verifyChecked(TernaryExpr *E) {
2093+
PrettyStackTraceExpr debugStack(Ctx, "verifying TernaryExpr", E);
20942094

20952095
auto condTy = E->getCondExpr()->getType();
20962096
if (!condTy->isBool()) {
2097-
Out << "IfExpr condition is not Bool\n";
2097+
Out << "TernaryExpr condition is not Bool\n";
20982098
abort();
20992099
}
21002100

21012101
checkSameType(E->getThenExpr()->getType(),
21022102
E->getElseExpr()->getType(),
2103-
"then and else branches of an if-expr");
2103+
"then and else branches of a TernaryExpr");
21042104
verifyCheckedBase(E);
21052105
}
21062106

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
10101010

10111011
return E;
10121012
}
1013-
1014-
1015-
Expr *visitIfExpr(IfExpr *E) {
1013+
1014+
Expr *visitTernaryExpr(TernaryExpr *E) {
10161015
if (Expr *Cond = E->getCondExpr()) {
10171016
Cond = doIt(Cond);
10181017
if (!Cond) return nullptr;

lib/AST/Expr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
450450
NO_REFERENCE(Is);
451451

452452
NO_REFERENCE(Arrow);
453-
NO_REFERENCE(If);
453+
NO_REFERENCE(Ternary);
454454
NO_REFERENCE(EnumIsCase);
455455
NO_REFERENCE(Assign);
456456
NO_REFERENCE(CodeCompletion);
@@ -805,7 +805,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
805805
return false;
806806

807807
case ExprKind::Arrow:
808-
case ExprKind::If:
808+
case ExprKind::Ternary:
809809
case ExprKind::Assign:
810810
case ExprKind::UnresolvedPattern:
811811
case ExprKind::EditorPlaceholder:
@@ -968,7 +968,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
968968
case ExprKind::Is:
969969
case ExprKind::Coerce:
970970
case ExprKind::Arrow:
971-
case ExprKind::If:
971+
case ExprKind::Ternary:
972972
case ExprKind::EnumIsCase:
973973
case ExprKind::Assign:
974974
case ExprKind::CodeCompletion:

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,8 @@ class ExprContextAnalyzer {
10161016
}
10171017
break;
10181018
}
1019-
case ExprKind::If: {
1020-
auto *IE = cast<IfExpr>(Parent);
1019+
case ExprKind::Ternary: {
1020+
auto *IE = cast<TernaryExpr>(Parent);
10211021
if (IE->isFolded() &&
10221022
SM.rangeContains(IE->getCondExpr()->getSourceRange(),
10231023
ParsedExpr->getSourceRange())) {
@@ -1291,7 +1291,7 @@ class ExprContextAnalyzer {
12911291
case ExprKind::PrefixUnary:
12921292
case ExprKind::Assign:
12931293
case ExprKind::Dictionary:
1294-
case ExprKind::If:
1294+
case ExprKind::Ternary:
12951295
return true;
12961296
case ExprKind::Array:
12971297
return (!Parent.getAsExpr() ||

0 commit comments

Comments
 (0)