Skip to content

Commit 818c0ba

Browse files
committed
Add APIs to mark as completed the parsing of decl specifiers
1 parent 4dae6ee commit 818c0ba

File tree

9 files changed

+275
-173
lines changed

9 files changed

+275
-173
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function new_ast_rewriter_cc({
128128
`auto ${m.name}Decl = Decl{${specsAttr}Ctx, copy->${m.name}};`
129129
);
130130
emit(
131-
`auto ${m.name}Type = getDeclaratorType(translationUnit(), copy->${m.name}, ${specsAttr}Ctx.getType());`
131+
`auto ${m.name}Type = getDeclaratorType(translationUnit(), copy->${m.name}, ${specsAttr}Ctx.type());`
132132
);
133133

134134
typeAttr = members.find(
@@ -197,6 +197,17 @@ export function new_ast_rewriter_cc({
197197
} // switch
198198

199199
emit(` }`);
200+
201+
// update the context if needed
202+
switch (m.type) {
203+
case "SpecifierAST":
204+
emit(`${m.name}Ctx.finish();`);
205+
break;
206+
207+
default:
208+
break;
209+
} // switch
210+
200211
emit();
201212
break;
202213
}

src/parser/cxx/ast_rewriter.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,8 @@ auto ASTRewriter::operator()(InitDeclaratorAST* ast, const DeclSpecs& declSpecs)
10591059

10601060
auto decl = Decl{declSpecs, copy->declarator};
10611061

1062-
auto type = getDeclaratorType(translationUnit(), copy->declarator,
1063-
declSpecs.getType());
1062+
auto type =
1063+
getDeclaratorType(translationUnit(), copy->declarator, declSpecs.type());
10641064

10651065
// ### fix scope
10661066
if (binder_.scope() && binder_.scope()->isClassScope()) {
@@ -1152,12 +1152,13 @@ auto ASTRewriter::operator()(TypeIdAST* ast) -> TypeIdAST* {
11521152
typeSpecifierList = &(*typeSpecifierList)->next;
11531153
typeSpecifierListCtx.accept(value);
11541154
}
1155+
typeSpecifierListCtx.finish();
11551156

11561157
copy->declarator = operator()(ast->declarator);
11571158

11581159
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
11591160
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
1160-
typeSpecifierListCtx.getType());
1161+
typeSpecifierListCtx.type());
11611162
copy->type = declaratorType;
11621163

11631164
return copy;
@@ -1194,8 +1195,10 @@ auto ASTRewriter::operator()(BaseSpecifierAST* ast) -> BaseSpecifierAST* {
11941195
copy->nestedNameSpecifier = operator()(ast->nestedNameSpecifier);
11951196
copy->templateLoc = ast->templateLoc;
11961197
copy->unqualifiedId = operator()(ast->unqualifiedId);
1198+
copy->ellipsisLoc = ast->ellipsisLoc;
11971199
copy->isTemplateIntroduced = ast->isTemplateIntroduced;
11981200
copy->isVirtual = ast->isVirtual;
1201+
copy->isVariadic = ast->isVariadic;
11991202
copy->accessSpecifier = ast->accessSpecifier;
12001203
copy->symbol = ast->symbol;
12011204

@@ -1403,6 +1406,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(SimpleDeclarationAST* ast)
14031406
declSpecifierList = &(*declSpecifierList)->next;
14041407
declSpecifierListCtx.accept(value);
14051408
}
1409+
declSpecifierListCtx.finish();
14061410

14071411
for (auto initDeclaratorList = &copy->initDeclaratorList;
14081412
auto node : ListView{ast->initDeclaratorList}) {
@@ -1614,6 +1618,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(OpaqueEnumDeclarationAST* ast)
16141618
typeSpecifierList = &(*typeSpecifierList)->next;
16151619
typeSpecifierListCtx.accept(value);
16161620
}
1621+
typeSpecifierListCtx.finish();
16171622

16181623
copy->emicolonLoc = ast->emicolonLoc;
16191624

@@ -1639,12 +1644,13 @@ auto ASTRewriter::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
16391644
declSpecifierList = &(*declSpecifierList)->next;
16401645
declSpecifierListCtx.accept(value);
16411646
}
1647+
declSpecifierListCtx.finish();
16421648

16431649
copy->declarator = rewrite(ast->declarator);
16441650

16451651
auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
16461652
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
1647-
declSpecifierListCtx.getType());
1653+
declSpecifierListCtx.type());
16481654
copy->requiresClause = rewrite(ast->requiresClause);
16491655
copy->functionBody = rewrite(ast->functionBody);
16501656
copy->symbol = ast->symbol;
@@ -1878,12 +1884,13 @@ auto ASTRewriter::DeclarationVisitor::operator()(ParameterDeclarationAST* ast)
18781884
typeSpecifierList = &(*typeSpecifierList)->next;
18791885
typeSpecifierListCtx.accept(value);
18801886
}
1887+
typeSpecifierListCtx.finish();
18811888

18821889
copy->declarator = rewrite(ast->declarator);
18831890

18841891
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
18851892
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
1886-
typeSpecifierListCtx.getType());
1893+
typeSpecifierListCtx.type());
18871894
copy->type = declaratorType;
18881895
copy->equalLoc = ast->equalLoc;
18891896
copy->expression = rewrite(ast->expression);
@@ -1931,6 +1938,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(
19311938
declSpecifierList = &(*declSpecifierList)->next;
19321939
declSpecifierListCtx.accept(value);
19331940
}
1941+
declSpecifierListCtx.finish();
19341942

19351943
copy->refQualifierLoc = ast->refQualifierLoc;
19361944
copy->lbracketLoc = ast->lbracketLoc;
@@ -3034,12 +3042,13 @@ auto ASTRewriter::ExpressionVisitor::operator()(NewExpressionAST* ast)
30343042
typeSpecifierList = &(*typeSpecifierList)->next;
30353043
typeSpecifierListCtx.accept(value);
30363044
}
3045+
typeSpecifierListCtx.finish();
30373046

30383047
copy->declarator = rewrite(ast->declarator);
30393048

30403049
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
30413050
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
3042-
typeSpecifierListCtx.getType());
3051+
typeSpecifierListCtx.type());
30433052
copy->rparenLoc = ast->rparenLoc;
30443053
copy->newInitalizer = rewrite(ast->newInitalizer);
30453054

@@ -3224,12 +3233,13 @@ auto ASTRewriter::ExpressionVisitor::operator()(ConditionExpressionAST* ast)
32243233
declSpecifierList = &(*declSpecifierList)->next;
32253234
declSpecifierListCtx.accept(value);
32263235
}
3236+
declSpecifierListCtx.finish();
32273237

32283238
copy->declarator = rewrite(ast->declarator);
32293239

32303240
auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
32313241
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
3232-
declSpecifierListCtx.getType());
3242+
declSpecifierListCtx.type());
32333243
copy->initializer = rewrite(ast->initializer);
32343244
copy->symbol = ast->symbol;
32353245

@@ -3734,6 +3744,7 @@ auto ASTRewriter::SpecifierVisitor::operator()(EnumSpecifierAST* ast)
37343744
typeSpecifierList = &(*typeSpecifierList)->next;
37353745
typeSpecifierListCtx.accept(value);
37363746
}
3747+
typeSpecifierListCtx.finish();
37373748

37383749
copy->lbraceLoc = ast->lbraceLoc;
37393750

@@ -4498,12 +4509,13 @@ auto ASTRewriter::ExceptionDeclarationVisitor::operator()(
44984509
typeSpecifierList = &(*typeSpecifierList)->next;
44994510
typeSpecifierListCtx.accept(value);
45004511
}
4512+
typeSpecifierListCtx.finish();
45014513

45024514
copy->declarator = rewrite(ast->declarator);
45034515

45044516
auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
45054517
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
4506-
typeSpecifierListCtx.getType());
4518+
typeSpecifierListCtx.type());
45074519

45084520
return copy;
45094521
}

src/parser/cxx/binder.cc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ auto Binder::enterBlock(SourceLocation loc) -> BlockSymbol* {
104104
}
105105

106106
void Binder::bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpecs) {
107-
const auto underlyingType = underlyingTypeSpecs.getType();
107+
const Type* underlyingType = control()->getIntType();
108+
109+
if (underlyingTypeSpecs.hasTypeOrSizeSpecifier())
110+
underlyingType = underlyingTypeSpecs.type();
108111

109112
const auto location = ast->unqualifiedId
110113
? ast->unqualifiedId->firstSourceLocation()
@@ -133,12 +136,13 @@ void Binder::bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpecs) {
133136
}
134137
}
135138

136-
void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs) {
139+
void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs,
140+
bool isDeclaration) {
141+
const auto _ = ScopeGuard{this};
142+
137143
auto className = get_name(control(), ast->unqualifiedId);
138144
const auto location = ast->unqualifiedId->firstSourceLocation();
139145

140-
const auto _ = ScopeGuard{this};
141-
142146
if (ast->nestedNameSpecifier) {
143147
auto parent = ast->nestedNameSpecifier->symbol;
144148

@@ -171,8 +175,8 @@ void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs) {
171175

172176
ast->symbol = classSymbol;
173177

174-
declSpecs.type = ast->symbol->type();
175178
declSpecs.setTypeSpecifier(ast);
179+
declSpecs.setType(ast->symbol->type());
176180
}
177181

178182
void Binder::bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs) {
@@ -250,7 +254,7 @@ void Binder::bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs) {
250254
ast->symbol = classSymbol;
251255

252256
declSpecs.setTypeSpecifier(ast);
253-
declSpecs.type = classSymbol->type();
257+
declSpecs.setType(classSymbol->type());
254258
}
255259

256260
void Binder::complete(ClassSpecifierAST* ast) {
@@ -266,7 +270,7 @@ void Binder::complete(ClassSpecifierAST* ast) {
266270

267271
void Binder::bind(ParameterDeclarationAST* ast, const Decl& decl,
268272
bool inTemplateParameters) {
269-
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.getType());
273+
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.type());
270274

271275
if (auto declId = decl.declaratorId; declId && declId->unqualifiedId) {
272276
auto paramName = get_name(control(), declId->unqualifiedId);
@@ -503,13 +507,13 @@ void Binder::bind(UsingDirectiveAST* ast) {
503507
}
504508

505509
void Binder::bind(TypeIdAST* ast, const Decl& decl) {
506-
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.getType());
510+
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.type());
507511
}
508512

509513
auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
510514
-> TypeAliasSymbol* {
511515
auto name = decl.getName();
512-
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
516+
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
513517
auto symbol = control()->newTypeAliasSymbol(scope(), decl.location());
514518
symbol->setName(name);
515519
symbol->setType(type);
@@ -520,7 +524,7 @@ auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
520524
auto Binder::declareFunction(DeclaratorAST* declarator, const Decl& decl)
521525
-> FunctionSymbol* {
522526
auto name = decl.getName();
523-
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
527+
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
524528

525529
auto parentScope = scope();
526530

@@ -573,7 +577,7 @@ auto Binder::declareFunction(DeclaratorAST* declarator, const Decl& decl)
573577
auto Binder::declareField(DeclaratorAST* declarator, const Decl& decl)
574578
-> FieldSymbol* {
575579
auto name = decl.getName();
576-
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
580+
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
577581
auto fieldSymbol = control()->newFieldSymbol(scope(), decl.location());
578582
applySpecifiers(fieldSymbol, decl.specs);
579583
fieldSymbol->setName(name);
@@ -590,7 +594,7 @@ auto Binder::declareVariable(DeclaratorAST* declarator, const Decl& decl)
590594
-> VariableSymbol* {
591595
auto name = decl.getName();
592596
auto symbol = control()->newVariableSymbol(scope(), decl.location());
593-
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
597+
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
594598
applySpecifiers(symbol, decl.specs);
595599
symbol->setName(name);
596600
symbol->setType(type);

src/parser/cxx/binder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class Binder {
8989

9090
void bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpec);
9191

92-
void bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs);
92+
void bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs,
93+
bool isDeclaration);
9394

9495
void bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs);
9596

0 commit comments

Comments
 (0)