Skip to content

Commit

Permalink
BooleanLiteralExpr now is lowered directly into SIL.
Browse files Browse the repository at this point in the history
Instead of constructing calls to ExpressibleByBooleanLiteral.init(booleanLiteral: ...) in CSApply.cpp, just
annotate BooleanLiteralExpr with the selected constructor and do the actual construction during SILGen.

For context, StringLiteralExpr and NilLiteralExpr already behave this way.
  • Loading branch information
pschuh committed Jan 31, 2019
1 parent ebed597 commit d8bff8d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
30 changes: 29 additions & 1 deletion include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ class FloatLiteralExpr : public NumberLiteralExpr {
///
class BooleanLiteralExpr : public LiteralExpr {
SourceLoc Loc;
ConcreteDeclRef BuiltinInitializer;
ConcreteDeclRef Initializer;

public:
BooleanLiteralExpr(bool Value, SourceLoc Loc, bool Implicit = false)
Expand All @@ -848,7 +850,33 @@ class BooleanLiteralExpr : public LiteralExpr {
SourceRange getSourceRange() const {
return Loc;
}


/// Retrieve the builtin initializer that will be used to construct the
/// boolean literal.
///
/// Any type-checked boolean literal will have a builtin initializer, which is
/// called first to form a concrete Swift type.
ConcreteDeclRef getBuiltinInitializer() const { return BuiltinInitializer; }

/// Set the builtin initializer that will be used to construct the boolean
/// literal.
void setBuiltinInitializer(ConcreteDeclRef builtinInitializer) {
BuiltinInitializer = builtinInitializer;
}

/// Retrieve the initializer that will be used to construct the boolean
/// literal from the result of the initializer.
///
/// Only boolean literals that have no builtin literal conformance will have
/// this initializer, which will be called on the result of the builtin
/// initializer.
ConcreteDeclRef getInitializer() const { return Initializer; }

/// Set the initializer that will be used to construct the boolean literal.
void setInitializer(ConcreteDeclRef initializer) {
Initializer = initializer;
}

static bool classof(const Expr *E) {
return E->getKind() == ExprKind::BooleanLiteral;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,12 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
void visitBooleanLiteralExpr(BooleanLiteralExpr *E) {
printCommon(E, "boolean_literal_expr");
PrintWithColorRAII(OS, LiteralValueColor)
<< " value=" << (E->getValue() ? "true" : "false");
<< " value=" << (E->getValue() ? "true" : "false")
<< " builtin_initializer=";
E->getBuiltinInitializer().dump(
PrintWithColorRAII(OS, LiteralValueColor).getOS());
PrintWithColorRAII(OS, LiteralValueColor) << " initializer=";
E->getInitializer().dump(PrintWithColorRAII(OS, LiteralValueColor).getOS());
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

Expand Down
9 changes: 9 additions & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5523,6 +5523,15 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
} else if (auto nilLiteral = dyn_cast<NilLiteralExpr>(literal)) {
builtinLiteralArgs = emitEmptyTupleRValue(literal, C);
builtinInit = nilLiteral->getInitializer();
} else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal)) {
auto i1Ty = SILType::getBuiltinIntegerType(1, getASTContext());
SILValue boolValue = B.createIntegerLiteral(booleanLiteral, i1Ty,
booleanLiteral->getValue());
ManagedValue boolManaged = ManagedValue::forUnmanaged(boolValue);
CanType ty = boolManaged.getType().getASTType()->getCanonicalType();
builtinLiteralArgs = RValue(*this, {boolManaged}, ty);
builtinInit = booleanLiteral->getBuiltinInitializer();
init = booleanLiteral->getInitializer();
} else {
ASTContext &ctx = getASTContext();
SourceLoc loc = literal->getStartLoc();
Expand Down
4 changes: 1 addition & 3 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,9 +963,7 @@ RValue RValueEmitter::visitFloatLiteralExpr(FloatLiteralExpr *E,

RValue RValueEmitter::visitBooleanLiteralExpr(BooleanLiteralExpr *E,
SGFContext C) {
auto i1Ty = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
SILValue boolValue = SGF.B.createIntegerLiteral(E, i1Ty, E->getValue());
return RValue(SGF, E, ManagedValue::forUnmanaged(boolValue));
return SGF.emitLiteral(E, C);
}

RValue RValueEmitter::visitStringLiteralExpr(StringLiteralExpr *E,
Expand Down
10 changes: 5 additions & 5 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1999,18 +1999,14 @@ namespace {
{ tc.Context.Id_booleanLiteral });
DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinBooleanLiteral });
return convertLiteral(
return convertLiteralInPlace(
expr,
type,
cs.getType(expr),
protocol,
tc.Context.Id_BooleanLiteralType,
initName,
builtinProtocol,
Type(BuiltinIntegerType::get(BuiltinIntegerWidth::fixed(1),
tc.Context)),
builtinInitName,
nullptr,
diag::boolean_literal_broken_proto,
diag::builtin_boolean_literal_broken_proto);
}
Expand Down Expand Up @@ -7053,6 +7049,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
// Set the builtin initializer.
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
stringLiteral->setBuiltinInitializer(witness);
else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal))
booleanLiteral->setBuiltinInitializer(witness);
else {
cast<MagicIdentifierLiteralExpr>(literal)
->setBuiltinInitializer(witness);
Expand Down Expand Up @@ -7100,6 +7098,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
nilLiteral->setInitializer(witness);
else if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
stringLiteral->setInitializer(witness);
else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal))
booleanLiteral->setInitializer(witness);
else
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(witness);

Expand Down

0 comments on commit d8bff8d

Please sign in to comment.