Skip to content

Commit 14bbab4

Browse files
authored
Merge pull request swiftlang#22259 from pschuh/s-3
BooleanLiteralExpr now is lowered directly into SIL.
2 parents b607717 + d8bff8d commit 14bbab4

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

include/swift/AST/Expr.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ class FloatLiteralExpr : public NumberLiteralExpr {
835835
///
836836
class BooleanLiteralExpr : public LiteralExpr {
837837
SourceLoc Loc;
838+
ConcreteDeclRef BuiltinInitializer;
839+
ConcreteDeclRef Initializer;
838840

839841
public:
840842
BooleanLiteralExpr(bool Value, SourceLoc Loc, bool Implicit = false)
@@ -848,7 +850,33 @@ class BooleanLiteralExpr : public LiteralExpr {
848850
SourceRange getSourceRange() const {
849851
return Loc;
850852
}
851-
853+
854+
/// Retrieve the builtin initializer that will be used to construct the
855+
/// boolean literal.
856+
///
857+
/// Any type-checked boolean literal will have a builtin initializer, which is
858+
/// called first to form a concrete Swift type.
859+
ConcreteDeclRef getBuiltinInitializer() const { return BuiltinInitializer; }
860+
861+
/// Set the builtin initializer that will be used to construct the boolean
862+
/// literal.
863+
void setBuiltinInitializer(ConcreteDeclRef builtinInitializer) {
864+
BuiltinInitializer = builtinInitializer;
865+
}
866+
867+
/// Retrieve the initializer that will be used to construct the boolean
868+
/// literal from the result of the initializer.
869+
///
870+
/// Only boolean literals that have no builtin literal conformance will have
871+
/// this initializer, which will be called on the result of the builtin
872+
/// initializer.
873+
ConcreteDeclRef getInitializer() const { return Initializer; }
874+
875+
/// Set the initializer that will be used to construct the boolean literal.
876+
void setInitializer(ConcreteDeclRef initializer) {
877+
Initializer = initializer;
878+
}
879+
852880
static bool classof(const Expr *E) {
853881
return E->getKind() == ExprKind::BooleanLiteral;
854882
}

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,12 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
18321832
void visitBooleanLiteralExpr(BooleanLiteralExpr *E) {
18331833
printCommon(E, "boolean_literal_expr");
18341834
PrintWithColorRAII(OS, LiteralValueColor)
1835-
<< " value=" << (E->getValue() ? "true" : "false");
1835+
<< " value=" << (E->getValue() ? "true" : "false")
1836+
<< " builtin_initializer=";
1837+
E->getBuiltinInitializer().dump(
1838+
PrintWithColorRAII(OS, LiteralValueColor).getOS());
1839+
PrintWithColorRAII(OS, LiteralValueColor) << " initializer=";
1840+
E->getInitializer().dump(PrintWithColorRAII(OS, LiteralValueColor).getOS());
18361841
PrintWithColorRAII(OS, ParenthesisColor) << ')';
18371842
}
18381843

lib/SILGen/SILGenApply.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5523,6 +5523,15 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55235523
} else if (auto nilLiteral = dyn_cast<NilLiteralExpr>(literal)) {
55245524
builtinLiteralArgs = emitEmptyTupleRValue(literal, C);
55255525
builtinInit = nilLiteral->getInitializer();
5526+
} else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal)) {
5527+
auto i1Ty = SILType::getBuiltinIntegerType(1, getASTContext());
5528+
SILValue boolValue = B.createIntegerLiteral(booleanLiteral, i1Ty,
5529+
booleanLiteral->getValue());
5530+
ManagedValue boolManaged = ManagedValue::forUnmanaged(boolValue);
5531+
CanType ty = boolManaged.getType().getASTType()->getCanonicalType();
5532+
builtinLiteralArgs = RValue(*this, {boolManaged}, ty);
5533+
builtinInit = booleanLiteral->getBuiltinInitializer();
5534+
init = booleanLiteral->getInitializer();
55265535
} else {
55275536
ASTContext &ctx = getASTContext();
55285537
SourceLoc loc = literal->getStartLoc();

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,7 @@ RValue RValueEmitter::visitFloatLiteralExpr(FloatLiteralExpr *E,
963963

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

971969
RValue RValueEmitter::visitStringLiteralExpr(StringLiteralExpr *E,

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,18 +1999,14 @@ namespace {
19991999
{ tc.Context.Id_booleanLiteral });
20002000
DeclName builtinInitName(tc.Context, DeclBaseName::createConstructor(),
20012001
{ tc.Context.Id_builtinBooleanLiteral });
2002-
return convertLiteral(
2002+
return convertLiteralInPlace(
20032003
expr,
20042004
type,
2005-
cs.getType(expr),
20062005
protocol,
20072006
tc.Context.Id_BooleanLiteralType,
20082007
initName,
20092008
builtinProtocol,
2010-
Type(BuiltinIntegerType::get(BuiltinIntegerWidth::fixed(1),
2011-
tc.Context)),
20122009
builtinInitName,
2013-
nullptr,
20142010
diag::boolean_literal_broken_proto,
20152011
diag::builtin_boolean_literal_broken_proto);
20162012
}
@@ -7053,6 +7049,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
70537049
// Set the builtin initializer.
70547050
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
70557051
stringLiteral->setBuiltinInitializer(witness);
7052+
else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal))
7053+
booleanLiteral->setBuiltinInitializer(witness);
70567054
else {
70577055
cast<MagicIdentifierLiteralExpr>(literal)
70587056
->setBuiltinInitializer(witness);
@@ -7100,6 +7098,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
71007098
nilLiteral->setInitializer(witness);
71017099
else if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
71027100
stringLiteral->setInitializer(witness);
7101+
else if (auto booleanLiteral = dyn_cast<BooleanLiteralExpr>(literal))
7102+
booleanLiteral->setInitializer(witness);
71037103
else
71047104
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(witness);
71057105

0 commit comments

Comments
 (0)