Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 54 additions & 14 deletions packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export function new_ast_rewriter_cc({
emit();
emit(` struct ASTRewriter::${className}Visitor {`);
emit(` ${opName}& rewrite;`);
emit(
` [[nodiscard]] auto translationUnit() const -> TranslationUnit* { return rewrite.unit_; }`
);
emit();
emit(
` [[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }`
Expand All @@ -88,8 +91,20 @@ export function new_ast_rewriter_cc({
}
case "node-list": {
emit();

// check the base type has a context that must be passed
switch (m.type) {
case "SpecifierAST":
emit(` DeclSpecs ${m.name}Ctx{translationUnit()};`);
break;

default:
break;
} // switch

emit(` if (auto it = ast->${m.name}) {`);
emit(` auto out = &copy->${m.name};`);

emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` auto value = ${visitor}(node);`);
if (isBase(m.type)) {
Expand All @@ -98,6 +113,17 @@ export function new_ast_rewriter_cc({
emit(`*out = new (arena()) List(ast_cast<${m.type}>(value));`);
}
emit(` out = &(*out)->next;`);

// update the context if needed
switch (m.type) {
case "SpecifierAST":
emit(`${m.name}Ctx.accept(value);`);
break;

default:
break;
} // switch

emit(` }`);
emit(` }`);
emit();
Expand All @@ -118,21 +144,34 @@ export function new_ast_rewriter_cc({
by_base.forEach((nodes, base) => {
if (base === "AST") return;
emit();
if (base == "ExpressionAST") {
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
emit(` if (!ast) return {};`);
emit(` auto expr = visit(${chopAST(base)}Visitor{*this}, ast);`);
emit(` if (expr) typeChecker_->check(expr);`);
emit(` return expr;`);
emit(`}`);
} else {
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
emit(` if (ast)`);
emit(` return visit(${chopAST(base)}Visitor{*this}, ast);`);
emit(` return {};`);
emit(`}`);
}

switch (base) {
case "ExpressionAST":
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
emit(` if (!ast) return {};`);
emit(` auto expr = visit(${chopAST(base)}Visitor{*this}, ast);`);
emit(` if (expr) typeChecker_->check(expr);`);
emit(` return expr;`);
emit(`}`);
break;

case "SpecifierAST":
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
emit(` if (!ast) return {};`);
emit(` auto specifier = visit(${chopAST(base)}Visitor{*this}, ast);`);
emit(` return specifier;`);
emit(`}`);
break;

default:
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
emit(` if (!ast) return {};`);
emit(` return visit(${chopAST(base)}Visitor{*this}, ast);`);
emit(`}`);
break;
} // switch
});

by_base.get("AST")?.forEach(({ name, members }) => {
emit();
emit(`auto ${opName}::operator()(${name}* ast) -> ${name}* {`);
Expand Down Expand Up @@ -176,6 +215,7 @@ export function new_ast_rewriter_cc({
#include <cxx/control.h>
#include <cxx/translation_unit.h>
#include <cxx/type_checker.h>
#include <cxx/decl_specs.h>

namespace cxx {

Expand Down
3 changes: 2 additions & 1 deletion src/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ file(GLOB CXX_INCLUDE_HEADER_FILES cxx/*.h)
file(GLOB CXX_VIEWS_INCLUDE_HEADER_FILES cxx/views/*.h)

add_library(cxx-parser
cxx/ast.cc
cxx/ast_cursor.cc
cxx/ast_interpreter.cc
cxx/ast_rewriter.cc
cxx/ast_slot.cc
cxx/ast.cc
cxx/base_classes.cc
cxx/cli.cc
cxx/const_expression_evaluator.cc
cxx/const_value.cc
cxx/control.cc
cxx/cxx.cc
cxx/decl_specs.cc
cxx/diagnostic.cc
cxx/diagnostics_client.cc
cxx/external_name_encoder.cc
Expand Down
1 change: 1 addition & 0 deletions src/parser/cxx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,7 @@ class NamedTypeSpecifierAST final : public SpecifierAST {
SourceLocation templateLoc;
UnqualifiedIdAST* unqualifiedId = nullptr;
bool isTemplateIntroduced = false;
Symbol* symbol = nullptr;

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

Expand Down
Loading