Skip to content

Commit e357dbf

Browse files
committed
Parse C object literal expressions
Signed-off-by: Roberto Raggi <[email protected]>
1 parent e3a6492 commit e357dbf

26 files changed

+296
-6
lines changed

packages/cxx-frontend/src/AST.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,6 +3854,58 @@ export class UserDefinedStringLiteralExpressionAST extends ExpressionAST {
38543854
}
38553855
}
38563856

3857+
/**
3858+
* ObjectLiteralExpressionAST node.
3859+
*/
3860+
export class ObjectLiteralExpressionAST extends ExpressionAST {
3861+
/**
3862+
* Traverse this node using the given visitor.
3863+
* @param visitor the visitor.
3864+
* @param context the context.
3865+
* @returns the result of the visit.
3866+
*/
3867+
accept<Context, Result>(
3868+
visitor: ASTVisitor<Context, Result>,
3869+
context: Context,
3870+
): Result {
3871+
return visitor.visitObjectLiteralExpression(this, context);
3872+
}
3873+
3874+
/**
3875+
* Returns the location of the lparen token in this node
3876+
*/
3877+
getLparenToken(): Token | undefined {
3878+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
3879+
}
3880+
3881+
/**
3882+
* Returns the typeId of this node
3883+
*/
3884+
getTypeId(): TypeIdAST | undefined {
3885+
return AST.from<TypeIdAST>(
3886+
cxx.getASTSlot(this.getHandle(), 1),
3887+
this.parser,
3888+
);
3889+
}
3890+
3891+
/**
3892+
* Returns the location of the rparen token in this node
3893+
*/
3894+
getRparenToken(): Token | undefined {
3895+
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
3896+
}
3897+
3898+
/**
3899+
* Returns the bracedInitList of this node
3900+
*/
3901+
getBracedInitList(): BracedInitListAST | undefined {
3902+
return AST.from<BracedInitListAST>(
3903+
cxx.getASTSlot(this.getHandle(), 3),
3904+
this.parser,
3905+
);
3906+
}
3907+
}
3908+
38573909
/**
38583910
* ThisExpressionAST node.
38593911
*/
@@ -12976,6 +13028,7 @@ const AST_CONSTRUCTORS: Array<
1297613028
NullptrLiteralExpressionAST,
1297713029
StringLiteralExpressionAST,
1297813030
UserDefinedStringLiteralExpressionAST,
13031+
ObjectLiteralExpressionAST,
1297913032
ThisExpressionAST,
1298013033
NestedStatementExpressionAST,
1298113034
NestedExpressionAST,

packages/cxx-frontend/src/ASTKind.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export enum ASTKind {
8484
NullptrLiteralExpression,
8585
StringLiteralExpression,
8686
UserDefinedStringLiteralExpression,
87+
ObjectLiteralExpression,
8788
ThisExpression,
8889
NestedStatementExpression,
8990
NestedExpression,

packages/cxx-frontend/src/ASTVisitor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,18 @@ export abstract class ASTVisitor<Context, Result> {
709709
context: Context,
710710
): Result;
711711

712+
/**
713+
* Visit ObjectLiteralExpression node.
714+
*
715+
* @param node The node to visit.
716+
* @param context The context.
717+
* @returns The result of the visit.
718+
*/
719+
abstract visitObjectLiteralExpression(
720+
node: ast.ObjectLiteralExpressionAST,
721+
context: Context,
722+
): Result;
723+
712724
/**
713725
* Visit ThisExpression node.
714726
*

packages/cxx-frontend/src/RecursiveASTVisitor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,20 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
822822
context: Context,
823823
): void {}
824824

825+
/**
826+
* Visit a ObjectLiteralExpression node.
827+
*
828+
* @param node The node to visit.
829+
* @param context The context.
830+
*/
831+
visitObjectLiteralExpression(
832+
node: ast.ObjectLiteralExpressionAST,
833+
context: Context,
834+
): void {
835+
this.accept(node.getTypeId(), context);
836+
this.accept(node.getBracedInitList(), context);
837+
}
838+
825839
/**
826840
* Visit a ThisExpression node.
827841
*

src/mlir/cxx/mlir/codegen.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ struct Codegen::ExpressionVisitor {
174174
[[nodiscard]] auto operator()(UserDefinedStringLiteralExpressionAST* ast)
175175
-> ExpressionResult;
176176

177+
[[nodiscard]] auto operator()(ObjectLiteralExpressionAST* ast)
178+
-> ExpressionResult;
179+
177180
[[nodiscard]] auto operator()(ThisExpressionAST* ast) -> ExpressionResult;
178181

179182
[[nodiscard]] auto operator()(NestedStatementExpressionAST* ast)
@@ -1619,6 +1622,13 @@ auto Codegen::ExpressionVisitor::operator()(
16191622
return {op};
16201623
}
16211624

1625+
auto Codegen::ExpressionVisitor::operator()(ObjectLiteralExpressionAST* ast)
1626+
-> ExpressionResult {
1627+
auto op = gen.emitTodoExpr(ast->firstSourceLocation(),
1628+
"ObjectLiteralExpressionAST");
1629+
return {op};
1630+
}
1631+
16221632
auto Codegen::ExpressionVisitor::operator()(ThisExpressionAST* ast)
16231633
-> ExpressionResult {
16241634
auto op = gen.emitTodoExpr(ast->firstSourceLocation(), "ThisExpressionAST");

src/parser/cxx/ast.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,22 @@ auto UserDefinedStringLiteralExpressionAST::lastSourceLocation()
948948
return {};
949949
}
950950

951+
auto ObjectLiteralExpressionAST::firstSourceLocation() -> SourceLocation {
952+
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
953+
if (auto loc = cxx::firstSourceLocation(typeId)) return loc;
954+
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
955+
if (auto loc = cxx::firstSourceLocation(bracedInitList)) return loc;
956+
return {};
957+
}
958+
959+
auto ObjectLiteralExpressionAST::lastSourceLocation() -> SourceLocation {
960+
if (auto loc = cxx::lastSourceLocation(bracedInitList)) return loc;
961+
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
962+
if (auto loc = cxx::lastSourceLocation(typeId)) return loc;
963+
if (auto loc = cxx::lastSourceLocation(lparenLoc)) return loc;
964+
return {};
965+
}
966+
951967
auto ThisExpressionAST::firstSourceLocation() -> SourceLocation {
952968
if (auto loc = cxx::firstSourceLocation(thisLoc)) return loc;
953969
return {};
@@ -3530,6 +3546,7 @@ std::string_view kASTKindNames[] = {
35303546
"nullptr-literal-expression",
35313547
"string-literal-expression",
35323548
"user-defined-string-literal-expression",
3549+
"object-literal-expression",
35333550
"this-expression",
35343551
"nested-statement-expression",
35353552
"nested-expression",

src/parser/cxx/ast.fbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ union Expression {
134134
NullptrLiteralExpression,
135135
StringLiteralExpression,
136136
UserDefinedStringLiteralExpression,
137+
ObjectLiteralExpression,
137138
ThisExpression,
138139
NestedStatementExpression,
139140
NestedExpression,
@@ -895,6 +896,13 @@ table UserDefinedStringLiteralExpression /* ExpressionAST */ {
895896
literal_loc: uint32;
896897
}
897898

899+
table ObjectLiteralExpression /* ExpressionAST */ {
900+
type_id: TypeId;
901+
braced_init_list: BracedInitList;
902+
lparen_loc: uint32;
903+
rparen_loc: uint32;
904+
}
905+
898906
table ThisExpression /* ExpressionAST */ {
899907
this_loc: uint32;
900908
}

src/parser/cxx/ast.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,23 @@ class UserDefinedStringLiteralExpressionAST final : public ExpressionAST {
12931293
auto lastSourceLocation() -> SourceLocation override;
12941294
};
12951295

1296+
class ObjectLiteralExpressionAST final : public ExpressionAST {
1297+
public:
1298+
static constexpr ASTKind Kind = ASTKind::ObjectLiteralExpression;
1299+
1300+
ObjectLiteralExpressionAST() : ExpressionAST(Kind) {}
1301+
1302+
SourceLocation lparenLoc;
1303+
TypeIdAST* typeId = nullptr;
1304+
SourceLocation rparenLoc;
1305+
BracedInitListAST* bracedInitList = nullptr;
1306+
1307+
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
1308+
1309+
auto firstSourceLocation() -> SourceLocation override;
1310+
auto lastSourceLocation() -> SourceLocation override;
1311+
};
1312+
12961313
class ThisExpressionAST final : public ExpressionAST {
12971314
public:
12981315
static constexpr ASTKind Kind = ASTKind::ThisExpression;
@@ -4484,6 +4501,9 @@ auto visit(Visitor&& visitor, ExpressionAST* ast) {
44844501
return std::invoke(
44854502
std::forward<Visitor>(visitor),
44864503
static_cast<UserDefinedStringLiteralExpressionAST*>(ast));
4504+
case ObjectLiteralExpressionAST::Kind:
4505+
return std::invoke(std::forward<Visitor>(visitor),
4506+
static_cast<ObjectLiteralExpressionAST*>(ast));
44874507
case ThisExpressionAST::Kind:
44884508
return std::invoke(std::forward<Visitor>(visitor),
44894509
static_cast<ThisExpressionAST*>(ast));
@@ -4654,6 +4674,7 @@ template <>
46544674
case NullptrLiteralExpressionAST::Kind:
46554675
case StringLiteralExpressionAST::Kind:
46564676
case UserDefinedStringLiteralExpressionAST::Kind:
4677+
case ObjectLiteralExpressionAST::Kind:
46574678
case ThisExpressionAST::Kind:
46584679
case NestedStatementExpressionAST::Kind:
46594680
case NestedExpressionAST::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 FloatLiteralExpressionAST;
143143
class NullptrLiteralExpressionAST;
144144
class StringLiteralExpressionAST;
145145
class UserDefinedStringLiteralExpressionAST;
146+
class ObjectLiteralExpressionAST;
146147
class ThisExpressionAST;
147148
class NestedStatementExpressionAST;
148149
class NestedExpressionAST;

src/parser/cxx/ast_interpreter.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ struct ASTInterpreter::ExpressionVisitor {
299299
[[nodiscard]] auto operator()(UserDefinedStringLiteralExpressionAST* ast)
300300
-> ExpressionResult;
301301

302+
[[nodiscard]] auto operator()(ObjectLiteralExpressionAST* ast)
303+
-> ExpressionResult;
304+
302305
[[nodiscard]] auto operator()(ThisExpressionAST* ast) -> ExpressionResult;
303306

304307
[[nodiscard]] auto operator()(NestedStatementExpressionAST* ast)
@@ -1623,6 +1626,11 @@ auto ASTInterpreter::ExpressionVisitor::operator()(
16231626
return ConstValue(ast->literal);
16241627
}
16251628

1629+
auto ASTInterpreter::ExpressionVisitor::operator()(
1630+
ObjectLiteralExpressionAST* ast) -> ExpressionResult {
1631+
return std::nullopt;
1632+
}
1633+
16261634
auto ASTInterpreter::ExpressionVisitor::operator()(ThisExpressionAST* ast)
16271635
-> ExpressionResult {
16281636
return std::nullopt;

0 commit comments

Comments
 (0)