Skip to content

Commit 223de71

Browse files
committed
[Syntax] Create UnknownDecl node for orphan #else, et al. and SIL nodes
1 parent 8fc2634 commit 223de71

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -224,43 +224,32 @@ bool Parser::parseTopLevel() {
224224
// allows type declarations and other things to be parsed, name bound, and
225225
// type checked in batches, similar to immediate mode. This also enforces
226226
// that SIL bodies can only be at the top level.
227-
if (Tok.is(tok::kw_sil)) {
228-
assert(isInSILMode() && "'sil' should only be a keyword in SIL mode");
229-
SIL->parseDeclSIL(*this);
230-
} else if (Tok.is(tok::kw_sil_stage)) {
231-
assert(isInSILMode() && "'sil_stage' should only be a keyword in SIL mode");
232-
SIL->parseDeclSILStage(*this);
233-
} else if (Tok.is(tok::kw_sil_vtable)) {
234-
assert(isInSILMode() &&
235-
"'sil_vtable' should only be a keyword in SIL mode");
236-
SIL->parseSILVTable(*this);
237-
} else if (Tok.is(tok::kw_sil_global)) {
238-
assert(isInSILMode() &&
239-
"'sil_global' should only be a keyword in SIL mode");
240-
SIL->parseSILGlobal(*this);
241-
} else if (Tok.is(tok::kw_sil_witness_table)) {
242-
assert(isInSILMode() &&
243-
"'sil_witness_table' should only be a keyword in SIL mode");
244-
SIL->parseSILWitnessTable(*this);
245-
} else if (Tok.is(tok::kw_sil_default_witness_table)) {
246-
assert(isInSILMode() &&
247-
"'sil_default_witness_table' should only be a keyword in SIL mode");
248-
SIL->parseSILDefaultWitnessTable(*this);
249-
} else if (Tok.is(tok::kw_sil_coverage_map)) {
250-
assert(isInSILMode() &&
251-
"'sil_coverage_map' should only be a keyword in SIL mode");
252-
SIL->parseSILCoverageMap(*this);
253-
} else if (Tok.is(tok::kw_sil_property)) {
254-
assert(isInSILMode() &&
255-
"'sil_property' should only be a keyword in SIL mode");
256-
SIL->parseSILProperty(*this);
257-
} else if (Tok.is(tok::kw_sil_scope)) {
258-
assert(isInSILMode() && "'sil_scope' should only be a keyword in SIL mode");
259-
SIL->parseSILScope(*this);
260-
} else {
261-
parseBraceItems(Items,
262-
allowTopLevelCode() ? BraceItemListKind::TopLevelCode
263-
: BraceItemListKind::TopLevelLibrary);
227+
switch (Tok.getKind()) {
228+
default:
229+
parseBraceItems(Items, allowTopLevelCode()
230+
? BraceItemListKind::TopLevelCode
231+
: BraceItemListKind::TopLevelLibrary);
232+
break;
233+
234+
// For now, create 'UnknownDecl' for all SIL declarations.
235+
#define CASE_SIL(KW, NAME) \
236+
case tok::kw_##KW: { \
237+
assert(isInSILMode() && "'" #KW "' should only be a keyword in SIL mode"); \
238+
SyntaxParsingContext itemCtxt(SyntaxContext, SyntaxKind::CodeBlockItem); \
239+
SyntaxParsingContext declCtxt(SyntaxContext, SyntaxContextKind::Decl); \
240+
SIL->parse##NAME(*this); \
241+
break; \
242+
}
243+
CASE_SIL(sil, DeclSIL)
244+
CASE_SIL(sil_stage, DeclSILStage)
245+
CASE_SIL(sil_vtable, SILVTable)
246+
CASE_SIL(sil_global, SILGlobal)
247+
CASE_SIL(sil_witness_table, SILWitnessTable)
248+
CASE_SIL(sil_default_witness_table, SILDefaultWitnessTable)
249+
CASE_SIL(sil_coverage_map, SILCoverageMap)
250+
CASE_SIL(sil_property, SILProperty)
251+
CASE_SIL(sil_scope, SILScope)
252+
#undef CASE_SIL
264253
}
265254

266255
// In the case of a catastrophic parse error, consume any trailing
@@ -270,6 +259,10 @@ bool Parser::parseTopLevel() {
270259
Tok.is(tok::pound_endif)) {
271260
diagnose(Tok.getLoc(),
272261
diag::unexpected_conditional_compilation_block_terminator);
262+
// Create 'UnknownDecl' for orphan directives.
263+
SyntaxParsingContext itemCtxt(SyntaxContext, SyntaxKind::CodeBlockItem);
264+
SyntaxParsingContext declCtxt(SyntaxContext, SyntaxContextKind::Decl);
265+
273266
consumeToken();
274267
}
275268

test/Syntax/round_trip_misc.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class C {
1414
// Orphan '}' at top level
1515
}
1616

17+
// Orphan #elseif, #else, #endif at top level.
18+
#elseif foobar
19+
#else
20+
#endif
21+
1722
// Compound name.
1823
foo(x:y:)()
1924

0 commit comments

Comments
 (0)