Skip to content

Commit

Permalink
fix: Add reference to the else token in the AST
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Sep 5, 2023
1 parent 2e7041b commit e365784
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2777,9 +2777,12 @@ export class IfStatementAST extends StatementAST {
this.parser,
);
}
getElseToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 7), this.parser);
}
getElseStatement(): StatementAST | undefined {
return AST.from<StatementAST>(
cxx.getASTSlot(this.getHandle(), 7),
cxx.getASTSlot(this.getHandle(), 8),
this.parser,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/cxx/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1592,12 +1592,14 @@ auto IfStatementAST::firstSourceLocation() -> SourceLocation {
if (auto loc = cxx::firstSourceLocation(condition)) return loc;
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(statement)) return loc;
if (auto loc = cxx::firstSourceLocation(elseLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(elseStatement)) return loc;
return {};
}

auto IfStatementAST::lastSourceLocation() -> SourceLocation {
if (auto loc = cxx::lastSourceLocation(elseStatement)) return loc;
if (auto loc = cxx::lastSourceLocation(elseLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(statement)) return loc;
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(condition)) return loc;
Expand Down
1 change: 1 addition & 0 deletions src/parser/cxx/ast.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ table IfStatement /* StatementAST */ {
constexpr_loc: SourceLocation;
lparen_loc: SourceLocation;
rparen_loc: SourceLocation;
else_loc: SourceLocation;
}

table SwitchStatement /* StatementAST */ {
Expand Down
1 change: 1 addition & 0 deletions src/parser/cxx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ class IfStatementAST final : public StatementAST {
ExpressionAST* condition = nullptr;
SourceLocation rparenLoc;
StatementAST* statement = nullptr;
SourceLocation elseLoc;
StatementAST* elseStatement = nullptr;

void accept(ASTVisitor* visitor) override { visitor->visit(this); }
Expand Down
2 changes: 2 additions & 0 deletions src/parser/cxx/ast_cloner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,8 @@ void ASTCloner::visit(IfStatementAST* ast) {

copy->statement = accept(ast->statement);

copy->elseLoc = ast->elseLoc;

copy->elseStatement = accept(ast->elseStatement);
}

Expand Down
3 changes: 3 additions & 0 deletions src/parser/cxx/ast_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,8 @@ void ASTEncoder::visit(IfStatementAST* ast) {

const auto [statement, statementType] = acceptStatement(ast->statement);

auto elseLoc = encodeSourceLocation(ast->elseLoc);

const auto [elseStatement, elseStatementType] =
acceptStatement(ast->elseStatement);

Expand All @@ -2887,6 +2889,7 @@ void ASTEncoder::visit(IfStatementAST* ast) {
builder.add_rparen_loc(rparenLoc.o);
builder.add_statement(statement);
builder.add_statement_type(static_cast<io::Statement>(statementType));
builder.add_else_loc(elseLoc.o);
builder.add_else_statement(elseStatement);
builder.add_else_statement_type(
static_cast<io::Statement>(elseStatementType));
Expand Down
8 changes: 6 additions & 2 deletions src/parser/cxx/ast_slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2301,13 +2301,17 @@ void ASTSlot::visit(IfStatementAST* ast) {
value_ = reinterpret_cast<std::intptr_t>(ast->statement);
slotKind_ = ASTSlotKind::kNode;
break;
case 7: // elseStatement
case 7: // elseLoc
value_ = ast->elseLoc.index();
slotKind_ = ASTSlotKind::kToken;
break;
case 8: // elseStatement
value_ = reinterpret_cast<std::intptr_t>(ast->elseStatement);
slotKind_ = ASTSlotKind::kNode;
break;
} // switch

slotCount_ = 8;
slotCount_ = 9;
}

void ASTSlot::visit(SwitchStatementAST* ast) {
Expand Down
6 changes: 1 addition & 5 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3074,9 +3074,7 @@ auto Parser::parse_if_statement(StatementAST*& yyast) -> bool {

parse_statement(ast->statement);

SourceLocation elseLoc;

if (!match(TokenKind::T_ELSE, elseLoc)) return true;
if (!match(TokenKind::T_ELSE, ast->elseLoc)) return true;

parse_statement(ast->elseStatement);

Expand Down Expand Up @@ -5662,8 +5660,6 @@ auto Parser::parse_enum_specifier(SpecifierAST*& yyast) -> bool {

if (!match(TokenKind::T_LBRACE, lbraceLoc)) return false;

const Name* enumName = name ? name->name : nullptr;

auto ast = new (pool) EnumSpecifierAST();
yyast = ast;

Expand Down

0 comments on commit e365784

Please sign in to comment.