Skip to content

Commit debe239

Browse files
committed
Add APIs to get the id of a declarator
1 parent cfea692 commit debe239

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export function new_ast_rewriter_cc({
7474
emit(
7575
`[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }`
7676
);
77+
emit(
78+
`[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }`
79+
);
7780
nodes.forEach(({ name }) => {
7881
emit();
7982
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${base}*;`);
@@ -95,10 +98,10 @@ export function new_ast_rewriter_cc({
9598
);
9699

97100
if (blockSymbol) {
98-
emit(`auto _ = Binder::ScopeGuard(&rewrite.binder_);
101+
emit(`auto _ = Binder::ScopeGuard(binder());
99102
100103
if (ast->${blockSymbol.name}) {
101-
copy->${blockSymbol.name} = rewrite.binder_.enterBlock(ast->${blockSymbol.name}->location());
104+
copy->${blockSymbol.name} = binder()->enterBlock(ast->${blockSymbol.name}->location());
102105
}
103106
`);
104107
}
@@ -121,6 +124,9 @@ export function new_ast_rewriter_cc({
121124
members.find((m) => m.name == "declSpecifierList")?.name;
122125
if (specsAttr) {
123126
emit();
127+
emit(
128+
`auto ${m.name}Decl = Decl{${specsAttr}Ctx, copy->${m.name}};`
129+
);
124130
emit(
125131
`auto ${m.name}Type = getDeclaratorType(translationUnit(), copy->${m.name}, ${specsAttr}Ctx.getType());`
126132
);

src/parser/cxx/ast_rewriter.cc

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct ASTRewriter::UnitVisitor {
8989
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
9090
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
9191
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
92+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
9293

9394
[[nodiscard]] auto operator()(TranslationUnitAST* ast) -> UnitAST*;
9495

@@ -104,6 +105,7 @@ struct ASTRewriter::DeclarationVisitor {
104105
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
105106
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
106107
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
108+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
107109

108110
[[nodiscard]] auto operator()(SimpleDeclarationAST* ast) -> DeclarationAST*;
109111

@@ -184,6 +186,7 @@ struct ASTRewriter::StatementVisitor {
184186
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
185187
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
186188
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
189+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
187190

188191
[[nodiscard]] auto operator()(LabeledStatementAST* ast) -> StatementAST*;
189192

@@ -234,6 +237,7 @@ struct ASTRewriter::ExpressionVisitor {
234237
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
235238
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
236239
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
240+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
237241

238242
[[nodiscard]] auto operator()(GeneratedLiteralExpressionAST* ast)
239243
-> ExpressionAST*;
@@ -384,6 +388,7 @@ struct ASTRewriter::TemplateParameterVisitor {
384388
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
385389
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
386390
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
391+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
387392

388393
[[nodiscard]] auto operator()(TemplateTypeParameterAST* ast)
389394
-> TemplateParameterAST*;
@@ -407,6 +412,7 @@ struct ASTRewriter::SpecifierVisitor {
407412
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
408413
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
409414
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
415+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
410416

411417
[[nodiscard]] auto operator()(GeneratedTypeSpecifierAST* ast)
412418
-> SpecifierAST*;
@@ -495,6 +501,7 @@ struct ASTRewriter::PtrOperatorVisitor {
495501
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
496502
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
497503
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
504+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
498505

499506
[[nodiscard]] auto operator()(PointerOperatorAST* ast) -> PtrOperatorAST*;
500507

@@ -512,6 +519,7 @@ struct ASTRewriter::CoreDeclaratorVisitor {
512519
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
513520
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
514521
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
522+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
515523

516524
[[nodiscard]] auto operator()(BitfieldDeclaratorAST* ast)
517525
-> CoreDeclaratorAST*;
@@ -532,6 +540,7 @@ struct ASTRewriter::DeclaratorChunkVisitor {
532540
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
533541
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
534542
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
543+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
535544

536545
[[nodiscard]] auto operator()(FunctionDeclaratorChunkAST* ast)
537546
-> DeclaratorChunkAST*;
@@ -549,6 +558,7 @@ struct ASTRewriter::UnqualifiedIdVisitor {
549558
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
550559
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
551560
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
561+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
552562

553563
[[nodiscard]] auto operator()(NameIdAST* ast) -> UnqualifiedIdAST*;
554564

@@ -582,6 +592,7 @@ struct ASTRewriter::NestedNameSpecifierVisitor {
582592
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
583593
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
584594
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
595+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
585596

586597
[[nodiscard]] auto operator()(GlobalNestedNameSpecifierAST* ast)
587598
-> NestedNameSpecifierAST*;
@@ -605,6 +616,7 @@ struct ASTRewriter::FunctionBodyVisitor {
605616
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
606617
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
607618
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
619+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
608620

609621
[[nodiscard]] auto operator()(DefaultFunctionBodyAST* ast)
610622
-> FunctionBodyAST*;
@@ -627,6 +639,7 @@ struct ASTRewriter::TemplateArgumentVisitor {
627639
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
628640
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
629641
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
642+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
630643

631644
[[nodiscard]] auto operator()(TypeTemplateArgumentAST* ast)
632645
-> TemplateArgumentAST*;
@@ -644,6 +657,7 @@ struct ASTRewriter::ExceptionSpecifierVisitor {
644657
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
645658
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
646659
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
660+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
647661

648662
[[nodiscard]] auto operator()(ThrowExceptionSpecifierAST* ast)
649663
-> ExceptionSpecifierAST*;
@@ -661,6 +675,7 @@ struct ASTRewriter::RequirementVisitor {
661675
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
662676
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
663677
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
678+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
664679

665680
[[nodiscard]] auto operator()(SimpleRequirementAST* ast) -> RequirementAST*;
666681

@@ -680,6 +695,7 @@ struct ASTRewriter::NewInitializerVisitor {
680695
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
681696
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
682697
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
698+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
683699

684700
[[nodiscard]] auto operator()(NewParenInitializerAST* ast)
685701
-> NewInitializerAST*;
@@ -697,6 +713,7 @@ struct ASTRewriter::MemInitializerVisitor {
697713
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
698714
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
699715
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
716+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
700717

701718
[[nodiscard]] auto operator()(ParenMemInitializerAST* ast)
702719
-> MemInitializerAST*;
@@ -714,6 +731,7 @@ struct ASTRewriter::LambdaCaptureVisitor {
714731
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
715732
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
716733
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
734+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
717735

718736
[[nodiscard]] auto operator()(ThisLambdaCaptureAST* ast) -> LambdaCaptureAST*;
719737

@@ -740,6 +758,7 @@ struct ASTRewriter::ExceptionDeclarationVisitor {
740758
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
741759
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
742760
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
761+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
743762

744763
[[nodiscard]] auto operator()(EllipsisExceptionDeclarationAST* ast)
745764
-> ExceptionDeclarationAST*;
@@ -757,6 +776,7 @@ struct ASTRewriter::AttributeSpecifierVisitor {
757776
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
758777
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
759778
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
779+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
760780

761781
[[nodiscard]] auto operator()(CxxAttributeAST* ast) -> AttributeSpecifierAST*;
762782

@@ -780,6 +800,7 @@ struct ASTRewriter::AttributeTokenVisitor {
780800
[[nodiscard]] auto control() const -> Control* { return rewrite.control(); }
781801
[[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }
782802
[[nodiscard]] auto rewriter() const -> ASTRewriter* { return &rewrite; }
803+
[[nodiscard]] auto binder() const -> Binder* { return &rewrite.binder_; }
783804

784805
[[nodiscard]] auto operator()(ScopedAttributeTokenAST* ast)
785806
-> AttributeTokenAST*;
@@ -1118,6 +1139,7 @@ auto ASTRewriter::operator()(TypeIdAST* ast) -> TypeIdAST* {
11181139

11191140
copy->declarator = operator()(ast->declarator);
11201141

1142+
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
11211143
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
11221144
typeSpecifierListCtx.getType());
11231145
copy->type = declaratorType;
@@ -1602,6 +1624,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
16021624

16031625
copy->declarator = rewrite(ast->declarator);
16041626

1627+
auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
16051628
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
16061629
declSpecifierListCtx.getType());
16071630
copy->requiresClause = rewrite(ast->requiresClause);
@@ -1840,6 +1863,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(ParameterDeclarationAST* ast)
18401863

18411864
copy->declarator = rewrite(ast->declarator);
18421865

1866+
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
18431867
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
18441868
typeSpecifierListCtx.getType());
18451869
copy->type = declaratorType;
@@ -2000,10 +2024,10 @@ auto ASTRewriter::StatementVisitor::operator()(CompoundStatementAST* ast)
20002024
-> StatementAST* {
20012025
auto copy = make_node<CompoundStatementAST>(arena());
20022026

2003-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2027+
auto _ = Binder::ScopeGuard(binder());
20042028

20052029
if (ast->symbol) {
2006-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2030+
copy->symbol = binder()->enterBlock(ast->symbol->location());
20072031
}
20082032

20092033
copy->lbraceLoc = ast->lbraceLoc;
@@ -2024,10 +2048,10 @@ auto ASTRewriter::StatementVisitor::operator()(IfStatementAST* ast)
20242048
-> StatementAST* {
20252049
auto copy = make_node<IfStatementAST>(arena());
20262050

2027-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2051+
auto _ = Binder::ScopeGuard(binder());
20282052

20292053
if (ast->symbol) {
2030-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2054+
copy->symbol = binder()->enterBlock(ast->symbol->location());
20312055
}
20322056

20332057
copy->ifLoc = ast->ifLoc;
@@ -2062,10 +2086,10 @@ auto ASTRewriter::StatementVisitor::operator()(SwitchStatementAST* ast)
20622086
-> StatementAST* {
20632087
auto copy = make_node<SwitchStatementAST>(arena());
20642088

2065-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2089+
auto _ = Binder::ScopeGuard(binder());
20662090

20672091
if (ast->symbol) {
2068-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2092+
copy->symbol = binder()->enterBlock(ast->symbol->location());
20692093
}
20702094

20712095
copy->switchLoc = ast->switchLoc;
@@ -2082,10 +2106,10 @@ auto ASTRewriter::StatementVisitor::operator()(WhileStatementAST* ast)
20822106
-> StatementAST* {
20832107
auto copy = make_node<WhileStatementAST>(arena());
20842108

2085-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2109+
auto _ = Binder::ScopeGuard(binder());
20862110

20872111
if (ast->symbol) {
2088-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2112+
copy->symbol = binder()->enterBlock(ast->symbol->location());
20892113
}
20902114

20912115
copy->whileLoc = ast->whileLoc;
@@ -2116,10 +2140,10 @@ auto ASTRewriter::StatementVisitor::operator()(ForRangeStatementAST* ast)
21162140
-> StatementAST* {
21172141
auto copy = make_node<ForRangeStatementAST>(arena());
21182142

2119-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2143+
auto _ = Binder::ScopeGuard(binder());
21202144

21212145
if (ast->symbol) {
2122-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2146+
copy->symbol = binder()->enterBlock(ast->symbol->location());
21232147
}
21242148

21252149
copy->forLoc = ast->forLoc;
@@ -2138,10 +2162,10 @@ auto ASTRewriter::StatementVisitor::operator()(ForStatementAST* ast)
21382162
-> StatementAST* {
21392163
auto copy = make_node<ForStatementAST>(arena());
21402164

2141-
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2165+
auto _ = Binder::ScopeGuard(binder());
21422166

21432167
if (ast->symbol) {
2144-
copy->symbol = rewrite.binder_.enterBlock(ast->symbol->location());
2168+
copy->symbol = binder()->enterBlock(ast->symbol->location());
21452169
}
21462170

21472171
copy->forLoc = ast->forLoc;
@@ -2995,6 +3019,7 @@ auto ASTRewriter::ExpressionVisitor::operator()(NewExpressionAST* ast)
29953019

29963020
copy->declarator = rewrite(ast->declarator);
29973021

3022+
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
29983023
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
29993024
typeSpecifierListCtx.getType());
30003025
copy->rparenLoc = ast->rparenLoc;
@@ -3184,6 +3209,7 @@ auto ASTRewriter::ExpressionVisitor::operator()(ConditionExpressionAST* ast)
31843209

31853210
copy->declarator = rewrite(ast->declarator);
31863211

3212+
auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
31873213
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
31883214
declSpecifierListCtx.getType());
31893215
copy->initializer = rewrite(ast->initializer);
@@ -4439,6 +4465,7 @@ auto ASTRewriter::ExceptionDeclarationVisitor::operator()(
44394465

44404466
copy->declarator = rewrite(ast->declarator);
44414467

4468+
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
44424469
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
44434470
typeSpecifierListCtx.getType());
44444471

src/parser/cxx/decl.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,24 @@ struct GetDeclaratorType {
334334
return getDeclaratorType(declarator, type);
335335
}
336336

337-
Decl::Decl(const DeclSpecs& specs) : specs{specs} {}
337+
[[nodiscard]] auto getDeclaratorId(DeclaratorAST* declarator)
338+
-> IdDeclaratorAST* {
339+
if (!declarator) return nullptr;
340+
341+
if (auto id = ast_cast<IdDeclaratorAST>(declarator->coreDeclarator)) {
342+
return id;
343+
}
344+
345+
if (auto nested = ast_cast<NestedDeclaratorAST>(declarator->coreDeclarator)) {
346+
return getDeclaratorId(nested->declarator);
347+
}
348+
349+
return nullptr;
350+
}
351+
352+
Decl::Decl(const DeclSpecs& specs, DeclaratorAST* declarator) : specs{specs} {
353+
declaratorId = getDeclaratorId(declarator);
354+
}
338355

339356
auto Decl::location() const -> SourceLocation {
340357
if (declaratorId) return declaratorId->firstSourceLocation();

src/parser/cxx/decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ class Decl {
3333
IdDeclaratorAST* declaratorId = nullptr;
3434
bool isPack = false;
3535

36-
explicit Decl(const DeclSpecs& specs);
36+
explicit Decl(const DeclSpecs& specs, DeclaratorAST* declarator = nullptr);
3737

3838
[[nodiscard]] auto location() const -> SourceLocation;
3939
[[nodiscard]] auto getName() const -> const Name*;
4040
[[nodiscard]] auto getNestedNameSpecifier() const -> NestedNameSpecifierAST*;
4141
[[nodiscard]] auto getScope() const -> Scope*;
4242
};
4343

44+
[[nodiscard]] auto getDeclaratorId(DeclaratorAST* declarator)
45+
-> IdDeclaratorAST*;
46+
4447
[[nodiscard]] auto getFunctionPrototype(DeclaratorAST* declarator)
4548
-> FunctionDeclaratorChunkAST*;
4649

0 commit comments

Comments
 (0)