Skip to content

Commit 45bc496

Browse files
committed
chore: Parse gcc statements in expressions
Signed-off-by: Roberto Raggi <[email protected]>
1 parent afacf6a commit 45bc496

25 files changed

+219
-0
lines changed

packages/cxx-frontend/src/AST.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,48 @@ export class ThisExpressionAST extends ExpressionAST {
38783878
}
38793879
}
38803880

3881+
/**
3882+
* NestedStatementExpressionAST node.
3883+
*/
3884+
export class NestedStatementExpressionAST extends ExpressionAST {
3885+
/**
3886+
* Traverse this node using the given visitor.
3887+
* @param visitor the visitor.
3888+
* @param context the context.
3889+
* @returns the result of the visit.
3890+
*/
3891+
accept<Context, Result>(
3892+
visitor: ASTVisitor<Context, Result>,
3893+
context: Context,
3894+
): Result {
3895+
return visitor.visitNestedStatementExpression(this, context);
3896+
}
3897+
3898+
/**
3899+
* Returns the location of the lparen token in this node
3900+
*/
3901+
getLparenToken(): Token | undefined {
3902+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
3903+
}
3904+
3905+
/**
3906+
* Returns the statement of this node
3907+
*/
3908+
getStatement(): CompoundStatementAST | undefined {
3909+
return AST.from<CompoundStatementAST>(
3910+
cxx.getASTSlot(this.getHandle(), 1),
3911+
this.parser,
3912+
);
3913+
}
3914+
3915+
/**
3916+
* Returns the location of the rparen token in this node
3917+
*/
3918+
getRparenToken(): Token | undefined {
3919+
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
3920+
}
3921+
}
3922+
38813923
/**
38823924
* NestedExpressionAST node.
38833925
*/
@@ -12752,6 +12794,7 @@ const AST_CONSTRUCTORS: Array<
1275212794
StringLiteralExpressionAST,
1275312795
UserDefinedStringLiteralExpressionAST,
1275412796
ThisExpressionAST,
12797+
NestedStatementExpressionAST,
1275512798
NestedExpressionAST,
1275612799
IdExpressionAST,
1275712800
LambdaExpressionAST,

packages/cxx-frontend/src/ASTKind.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export enum ASTKind {
8585
StringLiteralExpression,
8686
UserDefinedStringLiteralExpression,
8787
ThisExpression,
88+
NestedStatementExpression,
8889
NestedExpression,
8990
IdExpression,
9091
LambdaExpression,

packages/cxx-frontend/src/ASTVisitor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,18 @@ export abstract class ASTVisitor<Context, Result> {
721721
context: Context,
722722
): Result;
723723

724+
/**
725+
* Visit NestedStatementExpression node.
726+
*
727+
* @param node The node to visit.
728+
* @param context The context.
729+
* @returns The result of the visit.
730+
*/
731+
abstract visitNestedStatementExpression(
732+
node: ast.NestedStatementExpressionAST,
733+
context: Context,
734+
): Result;
735+
724736
/**
725737
* Visit NestedExpression node.
726738
*

packages/cxx-frontend/src/RecursiveASTVisitor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,19 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
830830
*/
831831
visitThisExpression(node: ast.ThisExpressionAST, context: Context): void {}
832832

833+
/**
834+
* Visit a NestedStatementExpression node.
835+
*
836+
* @param node The node to visit.
837+
* @param context The context.
838+
*/
839+
visitNestedStatementExpression(
840+
node: ast.NestedStatementExpressionAST,
841+
context: Context,
842+
): void {
843+
this.accept(node.getStatement(), context);
844+
}
845+
833846
/**
834847
* Visit a NestedExpression node.
835848
*

src/frontend/cxx/ast_printer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,11 @@ void ASTPrinter::visit(ThisExpressionAST* ast) {
783783
out_ << std::format("{}\n", "this-expression");
784784
}
785785

786+
void ASTPrinter::visit(NestedStatementExpressionAST* ast) {
787+
out_ << std::format("{}\n", "nested-statement-expression");
788+
accept(ast->statement, "statement");
789+
}
790+
786791
void ASTPrinter::visit(NestedExpressionAST* ast) {
787792
out_ << std::format("{}\n", "nested-expression");
788793
accept(ast->expression, "expression");

src/frontend/cxx/ast_printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class ASTPrinter : ASTVisitor {
104104
void visit(StringLiteralExpressionAST* ast) override;
105105
void visit(UserDefinedStringLiteralExpressionAST* ast) override;
106106
void visit(ThisExpressionAST* ast) override;
107+
void visit(NestedStatementExpressionAST* ast) override;
107108
void visit(NestedExpressionAST* ast) override;
108109
void visit(IdExpressionAST* ast) override;
109110
void visit(LambdaExpressionAST* ast) override;

src/parser/cxx/ast.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,20 @@ auto ThisExpressionAST::lastSourceLocation() -> SourceLocation {
958958
return {};
959959
}
960960

961+
auto NestedStatementExpressionAST::firstSourceLocation() -> SourceLocation {
962+
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
963+
if (auto loc = cxx::firstSourceLocation(statement)) return loc;
964+
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
965+
return {};
966+
}
967+
968+
auto NestedStatementExpressionAST::lastSourceLocation() -> SourceLocation {
969+
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
970+
if (auto loc = cxx::lastSourceLocation(statement)) return loc;
971+
if (auto loc = cxx::lastSourceLocation(lparenLoc)) return loc;
972+
return {};
973+
}
974+
961975
auto NestedExpressionAST::firstSourceLocation() -> SourceLocation {
962976
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
963977
if (auto loc = cxx::firstSourceLocation(expression)) return loc;

src/parser/cxx/ast.fbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ union Expression {
130130
StringLiteralExpression,
131131
UserDefinedStringLiteralExpression,
132132
ThisExpression,
133+
NestedStatementExpression,
133134
NestedExpression,
134135
IdExpression,
135136
LambdaExpression,
@@ -876,6 +877,12 @@ table ThisExpression /* ExpressionAST */ {
876877
this_loc: uint32;
877878
}
878879

880+
table NestedStatementExpression /* ExpressionAST */ {
881+
statement: CompoundStatement;
882+
lparen_loc: uint32;
883+
rparen_loc: uint32;
884+
}
885+
879886
table NestedExpression /* ExpressionAST */ {
880887
expression: Expression;
881888
lparen_loc: uint32;

src/parser/cxx/ast.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,22 @@ class ThisExpressionAST final : public ExpressionAST {
12811281
auto lastSourceLocation() -> SourceLocation override;
12821282
};
12831283

1284+
class NestedStatementExpressionAST final : public ExpressionAST {
1285+
public:
1286+
static constexpr ASTKind Kind = ASTKind::NestedStatementExpression;
1287+
1288+
NestedStatementExpressionAST() : ExpressionAST(Kind) {}
1289+
1290+
SourceLocation lparenLoc;
1291+
CompoundStatementAST* statement = nullptr;
1292+
SourceLocation rparenLoc;
1293+
1294+
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
1295+
1296+
auto firstSourceLocation() -> SourceLocation override;
1297+
auto lastSourceLocation() -> SourceLocation override;
1298+
};
1299+
12841300
class NestedExpressionAST final : public ExpressionAST {
12851301
public:
12861302
static constexpr ASTKind Kind = ASTKind::NestedExpression;
@@ -4363,6 +4379,9 @@ auto visit(Visitor&& visitor, ExpressionAST* ast) {
43634379
case ThisExpressionAST::Kind:
43644380
return std::invoke(std::forward<Visitor>(visitor),
43654381
static_cast<ThisExpressionAST*>(ast));
4382+
case NestedStatementExpressionAST::Kind:
4383+
return std::invoke(std::forward<Visitor>(visitor),
4384+
static_cast<NestedStatementExpressionAST*>(ast));
43664385
case NestedExpressionAST::Kind:
43674386
return std::invoke(std::forward<Visitor>(visitor),
43684387
static_cast<NestedExpressionAST*>(ast));
@@ -4527,6 +4546,7 @@ auto visit(Visitor&& visitor, ExpressionAST* ast) {
45274546
case StringLiteralExpressionAST::Kind:
45284547
case UserDefinedStringLiteralExpressionAST::Kind:
45294548
case ThisExpressionAST::Kind:
4549+
case NestedStatementExpressionAST::Kind:
45304550
case NestedExpressionAST::Kind:
45314551
case IdExpressionAST::Kind:
45324552
case LambdaExpressionAST::Kind:

src/parser/cxx/ast_fwd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class NullptrLiteralExpressionAST;
143143
class StringLiteralExpressionAST;
144144
class UserDefinedStringLiteralExpressionAST;
145145
class ThisExpressionAST;
146+
class NestedStatementExpressionAST;
146147
class NestedExpressionAST;
147148
class IdExpressionAST;
148149
class LambdaExpressionAST;

0 commit comments

Comments
 (0)