Skip to content

Commit 839221a

Browse files
authored
Merge pull request #6979 from rintaro/parse-trailing-semi
[Parse] Add back TrailingSemiLoc to Expr, Stmt, and Decl
2 parents 5a6f591 + dd46acd commit 839221a

File tree

8 files changed

+183
-159
lines changed

8 files changed

+183
-159
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,8 @@ class alignas(1 << DeclAlignInBits) Decl {
744744
/// Returns the source range of the entire declaration.
745745
SourceRange getSourceRange() const;
746746

747+
SourceLoc TrailingSemiLoc;
748+
747749
LLVM_ATTRIBUTE_DEPRECATED(
748750
void dump() const LLVM_ATTRIBUTE_USED,
749751
"only for use within the debugger");

include/swift/AST/Expr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ class alignas(8) Expr {
500500
SourceLoc getLoc() const { return (SUBEXPR)->getLoc(); } \
501501
SourceRange getSourceRange() const { return (SUBEXPR)->getSourceRange(); }
502502

503+
SourceLoc TrailingSemiLoc;
504+
503505
/// getSemanticsProvidingExpr - Find the smallest subexpression
504506
/// which obeys the property that evaluating it is exactly
505507
/// equivalent to evaluating this expression.

include/swift/AST/Stmt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class alignas(8) Stmt {
7878
SourceLoc getEndLoc() const;
7979

8080
SourceRange getSourceRange() const;
81+
SourceLoc TrailingSemiLoc;
8182

8283
/// isImplicit - Determines whether this statement was implicitly-generated,
8384
/// rather than explicitly written in the AST.

include/swift/Parse/Parser.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ class Parser {
675675
return AlreadyHandledDecls.erase(D);
676676
}
677677

678-
ParserStatus parseDecl(ParseDeclOptions Flags,
679-
llvm::function_ref<void(Decl*)> Handler);
678+
ParserResult<Decl> parseDecl(ParseDeclOptions Flags,
679+
llvm::function_ref<void(Decl*)> Handler);
680680

681681
void parseDeclDelayed();
682682

@@ -736,18 +736,20 @@ class Parser {
736736
DeclAttributes &Attributes);
737737
ParserResult<EnumDecl> parseDeclEnum(ParseDeclOptions Flags,
738738
DeclAttributes &Attributes);
739-
ParserStatus parseDeclEnumCase(ParseDeclOptions Flags, DeclAttributes &Attributes,
740-
SmallVectorImpl<Decl *> &decls);
739+
ParserResult<EnumCaseDecl>
740+
parseDeclEnumCase(ParseDeclOptions Flags, DeclAttributes &Attributes,
741+
SmallVectorImpl<Decl *> &decls);
741742
ParserResult<StructDecl>
742743
parseDeclStruct(ParseDeclOptions Flags, DeclAttributes &Attributes);
743744
ParserResult<ClassDecl>
744745
parseDeclClass(SourceLoc ClassLoc,
745746
ParseDeclOptions Flags, DeclAttributes &Attributes);
746-
ParserStatus parseDeclVar(ParseDeclOptions Flags, DeclAttributes &Attributes,
747-
SmallVectorImpl<Decl *> &Decls,
748-
SourceLoc StaticLoc,
749-
StaticSpellingKind StaticSpelling,
750-
SourceLoc TryLoc);
747+
ParserResult<PatternBindingDecl>
748+
parseDeclVar(ParseDeclOptions Flags, DeclAttributes &Attributes,
749+
SmallVectorImpl<Decl *> &Decls,
750+
SourceLoc StaticLoc,
751+
StaticSpellingKind StaticSpelling,
752+
SourceLoc TryLoc);
751753

752754
void consumeGetSetBody(AbstractFunctionDecl *AFD, SourceLoc LBLoc);
753755

@@ -796,9 +798,9 @@ class Parser {
796798
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
797799
DeclAttributes &Attributes);
798800

799-
ParserStatus parseDeclSubscript(ParseDeclOptions Flags,
800-
DeclAttributes &Attributes,
801-
SmallVectorImpl<Decl *> &Decls);
801+
ParserResult<SubscriptDecl>
802+
parseDeclSubscript(ParseDeclOptions Flags, DeclAttributes &Attributes,
803+
SmallVectorImpl<Decl *> &Decls);
802804

803805
ParserResult<ConstructorDecl>
804806
parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes);

lib/AST/ASTDumper.cpp

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ namespace {
409409

410410
if (D->isImplicit())
411411
PrintWithColorRAII(OS, DeclModifierColor) << " implicit";
412+
413+
if (D->TrailingSemiLoc.isValid())
414+
PrintWithColorRAII(OS, DeclModifierColor) << " trailing_semi";
412415
}
413416

414417
void printInherited(ArrayRef<TypeLoc> Inherited) {
@@ -1297,6 +1300,20 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
12971300
}
12981301
}
12991302

1303+
raw_ostream &printCommon(Stmt *S, const char *Name) {
1304+
OS.indent(Indent);
1305+
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1306+
PrintWithColorRAII(OS, StmtColor) << Name;
1307+
1308+
if (S->isImplicit())
1309+
OS << " implicit";
1310+
1311+
if (S->TrailingSemiLoc.isValid())
1312+
OS << " trailing_semi";
1313+
1314+
return OS;
1315+
}
1316+
13001317
void visitBraceStmt(BraceStmt *S) {
13011318
printASTNodes(S->getElements(), "brace_stmt");
13021319
}
@@ -1318,9 +1335,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13181335
}
13191336

13201337
void visitReturnStmt(ReturnStmt *S) {
1321-
OS.indent(Indent);
1322-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1323-
PrintWithColorRAII(OS, StmtColor) << "return_stmt";
1338+
printCommon(S, "return_stmt");
13241339
if (S->hasResult()) {
13251340
OS << '\n';
13261341
printRec(S->getResult());
@@ -1329,19 +1344,15 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13291344
}
13301345

13311346
void visitDeferStmt(DeferStmt *S) {
1332-
OS.indent(Indent);
1333-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1334-
PrintWithColorRAII(OS, StmtColor) << "defer_stmt\n";
1347+
printCommon(S, "defer_stmt") << '\n';
13351348
printRec(S->getTempDecl());
13361349
OS << '\n';
13371350
printRec(S->getCallExpr());
13381351
PrintWithColorRAII(OS, ParenthesisColor) << ')';
13391352
}
13401353

13411354
void visitIfStmt(IfStmt *S) {
1342-
OS.indent(Indent);
1343-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1344-
PrintWithColorRAII(OS, StmtColor) << "if_stmt\n";
1355+
printCommon(S, "if_stmt") << '\n';
13451356
for (auto elt : S->getCond())
13461357
printRec(elt);
13471358
OS << '\n';
@@ -1354,9 +1365,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13541365
}
13551366

13561367
void visitGuardStmt(GuardStmt *S) {
1357-
OS.indent(Indent);
1358-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1359-
PrintWithColorRAII(OS, StmtColor) << "guard_stmt\n";
1368+
printCommon(S, "guard_stmt") << '\n';
13601369
for (auto elt : S->getCond())
13611370
printRec(elt);
13621371
OS << '\n';
@@ -1365,11 +1374,10 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13651374
}
13661375

13671376
void visitIfConfigStmt(IfConfigStmt *S) {
1368-
OS.indent(Indent);
1369-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1370-
PrintWithColorRAII(OS, StmtColor) << "#if_stmt\n";
1377+
printCommon(S, "#if_stmt");
13711378
Indent += 2;
13721379
for (auto &Clause : S->getClauses()) {
1380+
OS << '\n';
13731381
OS.indent(Indent);
13741382
if (Clause.Cond) {
13751383
PrintWithColorRAII(OS, ParenthesisColor) << '(';
@@ -1390,17 +1398,13 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
13901398
}
13911399

13921400
void visitDoStmt(DoStmt *S) {
1393-
OS.indent(Indent);
1394-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1395-
PrintWithColorRAII(OS, StmtColor) << "do_stmt\n";
1401+
printCommon(S, "do_stmt") << '\n';
13961402
printRec(S->getBody());
13971403
PrintWithColorRAII(OS, ParenthesisColor) << ')';
13981404
}
13991405

14001406
void visitWhileStmt(WhileStmt *S) {
1401-
OS.indent(Indent);
1402-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1403-
PrintWithColorRAII(OS, StmtColor) << "while_stmt\n";
1407+
printCommon(S, "while_stmt") << '\n';
14041408
for (auto elt : S->getCond())
14051409
printRec(elt);
14061410
OS << '\n';
@@ -1409,18 +1413,14 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14091413
}
14101414

14111415
void visitRepeatWhileStmt(RepeatWhileStmt *S) {
1412-
OS.indent(Indent);
1413-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1414-
PrintWithColorRAII(OS, StmtColor) << "do_while_stmt\n";
1416+
printCommon(S, "repeat_while_stmt") << '\n';
14151417
printRec(S->getBody());
14161418
OS << '\n';
14171419
printRec(S->getCond());
14181420
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14191421
}
14201422
void visitForStmt(ForStmt *S) {
1421-
OS.indent(Indent);
1422-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1423-
PrintWithColorRAII(OS, StmtColor) << "for_stmt\n";
1423+
printCommon(S, "for_stmt") << '\n';
14241424
if (!S->getInitializerVarDecls().empty()) {
14251425
for (auto D : S->getInitializerVarDecls()) {
14261426
printRec(D);
@@ -1449,9 +1449,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14491449
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14501450
}
14511451
void visitForEachStmt(ForEachStmt *S) {
1452-
OS.indent(Indent);
1453-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1454-
OS << "for_each_stmt\n";
1452+
printCommon(S, "for_each_stmt") << '\n';
14551453
printRec(S->getPattern());
14561454
OS << '\n';
14571455
if (S->getWhere()) {
@@ -1477,27 +1475,19 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14771475
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14781476
}
14791477
void visitBreakStmt(BreakStmt *S) {
1480-
OS.indent(Indent);
1481-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1482-
PrintWithColorRAII(OS, StmtColor) << "break_stmt";
1478+
printCommon(S, "break_stmt");
14831479
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14841480
}
14851481
void visitContinueStmt(ContinueStmt *S) {
1486-
OS.indent(Indent);
1487-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1488-
PrintWithColorRAII(OS, StmtColor) << "continue_stmt";
1482+
printCommon(S, "continue_stmt");
14891483
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14901484
}
14911485
void visitFallthroughStmt(FallthroughStmt *S) {
1492-
OS.indent(Indent);
1493-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1494-
PrintWithColorRAII(OS, StmtColor) << "fallthrough_stmt";
1486+
printCommon(S, "fallthrough_stmt");
14951487
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14961488
}
14971489
void visitSwitchStmt(SwitchStmt *S) {
1498-
OS.indent(Indent);
1499-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1500-
PrintWithColorRAII(OS, StmtColor) << "switch_stmt\n";
1490+
printCommon(S, "switch_stmt") << '\n';
15011491
printRec(S->getSubjectExpr());
15021492
for (CaseStmt *C : S->getCases()) {
15031493
OS << '\n';
@@ -1506,9 +1496,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15061496
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15071497
}
15081498
void visitCaseStmt(CaseStmt *S) {
1509-
OS.indent(Indent);
1510-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1511-
PrintWithColorRAII(OS, StmtColor) << "case_stmt";
1499+
printCommon(S, "case_stmt");
15121500
for (const auto &LabelItem : S->getCaseLabelItems()) {
15131501
OS << '\n';
15141502
OS.indent(Indent + 2);
@@ -1529,24 +1517,18 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15291517
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15301518
}
15311519
void visitFailStmt(FailStmt *S) {
1532-
OS.indent(Indent);
1533-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1534-
PrintWithColorRAII(OS, StmtColor) << "fail_stmt";
1520+
printCommon(S, "fail_stmt");
15351521
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15361522
}
15371523

15381524
void visitThrowStmt(ThrowStmt *S) {
1539-
OS.indent(Indent);
1540-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1541-
PrintWithColorRAII(OS, StmtColor) << "throw_stmt\n";
1525+
printCommon(S, "throw_stmt") << '\n';
15421526
printRec(S->getSubExpr());
15431527
PrintWithColorRAII(OS, ParenthesisColor) << ')';
15441528
}
15451529

15461530
void visitDoCatchStmt(DoCatchStmt *S) {
1547-
OS.indent(Indent);
1548-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1549-
PrintWithColorRAII(OS, StmtColor) << "do_catch_stmt\n";
1531+
printCommon(S, "do_catch_stmt") << '\n';
15501532
printRec(S->getBody());
15511533
OS << '\n';
15521534
Indent += 2;
@@ -1560,9 +1542,7 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15601542
}
15611543
}
15621544
void visitCatchStmt(CatchStmt *clause) {
1563-
OS.indent(Indent);
1564-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1565-
PrintWithColorRAII(OS, StmtColor) << "catch\n";
1545+
printCommon(clause, "catch") << '\n';
15661546
printRec(clause->getErrorPattern());
15671547
if (auto guard = clause->getGuardExpr()) {
15681548
OS << '\n';
@@ -1670,6 +1650,9 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16701650
}
16711651
}
16721652

1653+
if (E->TrailingSemiLoc.isValid())
1654+
OS << " trailing_semi";
1655+
16731656
return OS;
16741657
}
16751658

0 commit comments

Comments
 (0)