Skip to content

Commit 69f2aba

Browse files
authored
Merge pull request #22158 from pschuh/s-2
NilLiteralExpr now is lowered directly into SIL.
2 parents 18a59bc + 6ca70c6 commit 69f2aba

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

include/swift/AST/Expr.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ class LiteralExpr : public Expr {
711711
///
712712
class NilLiteralExpr : public LiteralExpr {
713713
SourceLoc Loc;
714+
ConcreteDeclRef Initializer;
714715
public:
715716
NilLiteralExpr(SourceLoc Loc, bool Implicit = false)
716717
: LiteralExpr(ExprKind::NilLiteral, Implicit), Loc(Loc) {
@@ -719,6 +720,15 @@ class NilLiteralExpr : public LiteralExpr {
719720
SourceRange getSourceRange() const {
720721
return Loc;
721722
}
723+
724+
/// Retrieve the initializer that will be used to construct the 'nil'
725+
/// literal from the result of the initializer.
726+
ConcreteDeclRef getInitializer() const { return Initializer; }
727+
728+
/// Set the initializer that will be used to construct the 'nil' literal.
729+
void setInitializer(ConcreteDeclRef initializer) {
730+
Initializer = initializer;
731+
}
722732

723733
static bool classof(const Expr *E) {
724734
return E->getKind() == ExprKind::NilLiteral;

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,8 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
18051805

18061806
void visitNilLiteralExpr(NilLiteralExpr *E) {
18071807
printCommon(E, "nil_literal_expr");
1808+
PrintWithColorRAII(OS, LiteralValueColor) << " initializer=";
1809+
E->getInitializer().dump(PrintWithColorRAII(OS, LiteralValueColor).getOS());
18081810
PrintWithColorRAII(OS, ParenthesisColor) << ')';
18091811
}
18101812

lib/SILGen/SILGenApply.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5520,6 +5520,9 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55205520
stringLiteral->getEncoding());
55215521
builtinInit = stringLiteral->getBuiltinInitializer();
55225522
init = stringLiteral->getInitializer();
5523+
} else if (auto nilLiteral = dyn_cast<NilLiteralExpr>(literal)) {
5524+
builtinLiteralArgs = emitEmptyTupleRValue(literal, C);
5525+
builtinInit = nilLiteral->getInitializer();
55235526
} else {
55245527
ASTContext &ctx = getASTContext();
55255528
SourceLoc loc = literal->getStartLoc();

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ RValue RValueEmitter::visitOtherConstructorDeclRefExpr(
947947
}
948948

949949
RValue RValueEmitter::visitNilLiteralExpr(NilLiteralExpr *E, SGFContext C) {
950-
llvm_unreachable("NilLiteralExpr not lowered?");
950+
return SGF.emitLiteral(E, C);
951951
}
952952

953953
RValue RValueEmitter::visitIntegerLiteralExpr(IntegerLiteralExpr *E,

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,15 +1905,12 @@ namespace {
19051905

19061906
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
19071907
{ tc.Context.Id_nilLiteral });
1908-
return convertLiteral(expr, type, cs.getType(expr), protocol,
1909-
Identifier(), initName,
1910-
nullptr, Identifier(),
1911-
Identifier(),
1912-
[] (Type type) -> bool {
1913-
return false;
1914-
},
1915-
diag::nil_literal_broken_proto,
1916-
diag::nil_literal_broken_proto);
1908+
return convertLiteralInPlace(expr, type, protocol,
1909+
Identifier(), initName,
1910+
nullptr,
1911+
Identifier(),
1912+
diag::nil_literal_broken_proto,
1913+
diag::nil_literal_broken_proto);
19171914
}
19181915

19191916

@@ -7099,7 +7096,9 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
70997096
return nullptr;
71007097

71017098
// Set the initializer.
7102-
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
7099+
if (auto nilLiteral = dyn_cast<NilLiteralExpr>(literal))
7100+
nilLiteral->setInitializer(witness);
7101+
else if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
71037102
stringLiteral->setInitializer(witness);
71047103
else
71057104
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(witness);

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,11 +1294,11 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
12941294
///
12951295
/// Or like this if it is any other ExpressibleByNilLiteral type:
12961296
///
1297-
/// (dot_syntax_call_expr implicit type='Int?'
1298-
/// (declref_expr implicit decl=Optional.none)
1299-
/// (type_expr type=Int?))
1297+
/// (nil_literal_expr)
13001298
///
13011299
bool isTypeCheckedOptionalNil(Expr *E) {
1300+
if (dyn_cast<NilLiteralExpr>(E)) return true;
1301+
13021302
auto CE = dyn_cast<ApplyExpr>(E->getSemanticsProvidingExpr());
13031303
if (!CE || !CE->isImplicit())
13041304
return false;
@@ -1307,16 +1307,6 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
13071307
if (auto DRE = dyn_cast<DeclRefExpr>(CE->getSemanticFn()))
13081308
return DRE->getDecl() == TC.Context.getOptionalNoneDecl();
13091309

1310-
// Second case -- init(nilLiteral:)
1311-
auto CRCE = dyn_cast<ConstructorRefCallExpr>(CE->getSemanticFn());
1312-
if (!CRCE || !CRCE->isImplicit()) return false;
1313-
1314-
if (auto DRE = dyn_cast<DeclRefExpr>(CRCE->getSemanticFn())) {
1315-
SmallString<32> NameBuffer;
1316-
auto name = DRE->getDecl()->getFullName().getString(NameBuffer);
1317-
return name == "init(nilLiteral:)";
1318-
}
1319-
13201310
return false;
13211311
}
13221312

0 commit comments

Comments
 (0)