Skip to content

Commit 423d855

Browse files
committed
Move DeclSpecs out of Parser
DeclSpecs is needed during template instantiation.
1 parent 3ad65e7 commit 423d855

File tree

12 files changed

+616
-257
lines changed

12 files changed

+616
-257
lines changed

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

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export function new_ast_rewriter_cc({
6262
emit();
6363
emit(` struct ASTRewriter::${className}Visitor {`);
6464
emit(` ${opName}& rewrite;`);
65+
emit(
66+
` [[nodiscard]] auto translationUnit() const -> TranslationUnit* { return rewrite.unit_; }`
67+
);
6568
emit();
6669
emit(
6770
` [[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }`
@@ -88,8 +91,20 @@ export function new_ast_rewriter_cc({
8891
}
8992
case "node-list": {
9093
emit();
94+
95+
// check the base type has a context that must be passed
96+
switch (m.type) {
97+
case "SpecifierAST":
98+
emit(` DeclSpecs ${m.name}Ctx{translationUnit()};`);
99+
break;
100+
101+
default:
102+
break;
103+
} // switch
104+
91105
emit(` if (auto it = ast->${m.name}) {`);
92106
emit(` auto out = &copy->${m.name};`);
107+
93108
emit(` for (auto node : ListView{ast->${m.name}}) {`);
94109
emit(` auto value = ${visitor}(node);`);
95110
if (isBase(m.type)) {
@@ -98,6 +113,17 @@ export function new_ast_rewriter_cc({
98113
emit(`*out = new (arena()) List(ast_cast<${m.type}>(value));`);
99114
}
100115
emit(` out = &(*out)->next;`);
116+
117+
// update the context if needed
118+
switch (m.type) {
119+
case "SpecifierAST":
120+
emit(`${m.name}Ctx.accept(value);`);
121+
break;
122+
123+
default:
124+
break;
125+
} // switch
126+
101127
emit(` }`);
102128
emit(` }`);
103129
emit();
@@ -118,21 +144,34 @@ export function new_ast_rewriter_cc({
118144
by_base.forEach((nodes, base) => {
119145
if (base === "AST") return;
120146
emit();
121-
if (base == "ExpressionAST") {
122-
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
123-
emit(` if (!ast) return {};`);
124-
emit(` auto expr = visit(${chopAST(base)}Visitor{*this}, ast);`);
125-
emit(` if (expr) typeChecker_->check(expr);`);
126-
emit(` return expr;`);
127-
emit(`}`);
128-
} else {
129-
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
130-
emit(` if (ast)`);
131-
emit(` return visit(${chopAST(base)}Visitor{*this}, ast);`);
132-
emit(` return {};`);
133-
emit(`}`);
134-
}
147+
148+
switch (base) {
149+
case "ExpressionAST":
150+
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
151+
emit(` if (!ast) return {};`);
152+
emit(` auto expr = visit(${chopAST(base)}Visitor{*this}, ast);`);
153+
emit(` if (expr) typeChecker_->check(expr);`);
154+
emit(` return expr;`);
155+
emit(`}`);
156+
break;
157+
158+
case "SpecifierAST":
159+
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
160+
emit(` if (!ast) return {};`);
161+
emit(` auto specifier = visit(${chopAST(base)}Visitor{*this}, ast);`);
162+
emit(` return specifier;`);
163+
emit(`}`);
164+
break;
165+
166+
default:
167+
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
168+
emit(` if (!ast) return {};`);
169+
emit(` return visit(${chopAST(base)}Visitor{*this}, ast);`);
170+
emit(`}`);
171+
break;
172+
} // switch
135173
});
174+
136175
by_base.get("AST")?.forEach(({ name, members }) => {
137176
emit();
138177
emit(`auto ${opName}::operator()(${name}* ast) -> ${name}* {`);
@@ -176,6 +215,7 @@ export function new_ast_rewriter_cc({
176215
#include <cxx/control.h>
177216
#include <cxx/translation_unit.h>
178217
#include <cxx/type_checker.h>
218+
#include <cxx/decl_specs.h>
179219
180220
namespace cxx {
181221

src/parser/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ file(GLOB CXX_INCLUDE_HEADER_FILES cxx/*.h)
2121
file(GLOB CXX_VIEWS_INCLUDE_HEADER_FILES cxx/views/*.h)
2222

2323
add_library(cxx-parser
24-
cxx/ast.cc
2524
cxx/ast_cursor.cc
2625
cxx/ast_interpreter.cc
2726
cxx/ast_rewriter.cc
2827
cxx/ast_slot.cc
28+
cxx/ast.cc
2929
cxx/base_classes.cc
3030
cxx/cli.cc
3131
cxx/const_expression_evaluator.cc
3232
cxx/const_value.cc
3333
cxx/control.cc
3434
cxx/cxx.cc
35+
cxx/decl_specs.cc
3536
cxx/diagnostic.cc
3637
cxx/diagnostics_client.cc
3738
cxx/external_name_encoder.cc

src/parser/cxx/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,6 +2998,7 @@ class NamedTypeSpecifierAST final : public SpecifierAST {
29982998
SourceLocation templateLoc;
29992999
UnqualifiedIdAST* unqualifiedId = nullptr;
30003000
bool isTemplateIntroduced = false;
3001+
Symbol* symbol = nullptr;
30013002

30023003
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
30033004

0 commit comments

Comments
 (0)