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
59 changes: 53 additions & 6 deletions packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export function new_ast_rewriter_cc({
` [[nodiscard]] auto translationUnit() const -> TranslationUnit* { return rewrite.unit_; }`
);
emit();
emit(
` [[nodiscard]] auto control() const -> Control* { return rewrite.control(); }`
);
emit(
` [[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }`
);
Expand All @@ -77,7 +80,25 @@ export function new_ast_rewriter_cc({
});

const emitRewriterBody = (members: Member[], visitor: string = "rewrite") => {
const blockSymbol = members.find(
(m) => m.kind === "attribute" && m.type === "BlockSymbol"
);

if (blockSymbol) {
emit(`auto _ = Binder::ScopeGuard(&rewrite.binder_);

if (ast->${blockSymbol.name}) {
copy->${blockSymbol.name} = control()->newBlockSymbol(rewrite.binder_.scope(),
ast->${blockSymbol.name}->location());

rewrite.binder_.setScope(copy->${blockSymbol.name});
}
`);
}

members.forEach((m) => {
if (m === blockSymbol) return;

switch (m.kind) {
case "node": {
if (isBase(m.type)) {
Expand Down Expand Up @@ -106,11 +127,23 @@ export function new_ast_rewriter_cc({
emit(` auto out = &copy->${m.name};`);

emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` auto value = ${visitor}(node);`);

switch (m.type) {
case "InitDeclaratorAST":
emit(
` auto value = ${visitor}(node, declSpecifierListCtx);`
);
break;

default:
emit(` auto value = ${visitor}(node);`);
break;
} // switch

if (isBase(m.type)) {
emit(`*out = new (arena()) List(value);`);
emit(`*out = make_list_node(arena(), value);`);
} else {
emit(`*out = new (arena()) List(ast_cast<${m.type}>(value));`);
emit(`*out = make_list_node(arena(), ast_cast<${m.type}>(value));`);
}
emit(` out = &(*out)->next;`);

Expand Down Expand Up @@ -174,10 +207,20 @@ export function new_ast_rewriter_cc({

by_base.get("AST")?.forEach(({ name, members }) => {
emit();
emit(`auto ${opName}::operator()(${name}* ast) -> ${name}* {`);
switch (name) {
case "InitDeclaratorAST":
emit(
`auto ${opName}::operator()(${name}* ast, const DeclSpecs& declSpecs) -> ${name}* {`
);
break;
default:
emit(`auto ${opName}::operator()(${name}* ast) -> ${name}* {`);
break;
} // switch

emit(` if (!ast) return {};`);
emit();
emit(` auto copy = new (arena()) ${name}{};`);
emit(` auto copy = make_node<${name}>(arena());`);
emit();
emitRewriterBody(members, "operator()");
emit();
Expand All @@ -194,7 +237,7 @@ export function new_ast_rewriter_cc({
emit(
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${base}* {`
);
emit(` auto copy = new (arena()) ${name}{};`);
emit(` auto copy = make_node<${name}>(arena());`);
emit();
ast.baseMembers.get(base)?.forEach((m) => {
emit(` copy->${m.name} = ast->${m.name};`);
Expand All @@ -216,6 +259,10 @@ export function new_ast_rewriter_cc({
#include <cxx/translation_unit.h>
#include <cxx/type_checker.h>
#include <cxx/decl_specs.h>
#include <cxx/decl.h>
#include <cxx/symbols.h>
#include <cxx/types.h>
#include <cxx/binder.h>

namespace cxx {

Expand Down
11 changes: 10 additions & 1 deletion packages/cxx-gen-ast/src/new_ast_rewriter_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ export function new_ast_rewriter_h({
emit();
emit(` // run on the misc nodes`);
by_base.get("AST")?.forEach(({ name }) => {
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${name}*;`);
switch (name) {
case "InitDeclaratorAST":
emit(
` [[nodiscard]] auto operator()(${name}* ast, const DeclSpecs& declSpecs) -> ${name}*;`
);
break;
default:
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${name}*;`);
break;
} // switch
});

emit();
Expand Down
5 changes: 5 additions & 0 deletions src/parser/cxx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ class IfStatementAST final : public StatementAST {
StatementAST* statement = nullptr;
SourceLocation elseLoc;
StatementAST* elseStatement = nullptr;
BlockSymbol* symbol = nullptr;

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

Expand Down Expand Up @@ -967,6 +968,7 @@ class SwitchStatementAST final : public StatementAST {
ExpressionAST* condition = nullptr;
SourceLocation rparenLoc;
StatementAST* statement = nullptr;
BlockSymbol* symbol = nullptr;

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

Expand All @@ -985,6 +987,7 @@ class WhileStatementAST final : public StatementAST {
ExpressionAST* condition = nullptr;
SourceLocation rparenLoc;
StatementAST* statement = nullptr;
BlockSymbol* symbol = nullptr;

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

Expand Down Expand Up @@ -1026,6 +1029,7 @@ class ForRangeStatementAST final : public StatementAST {
ExpressionAST* rangeInitializer = nullptr;
SourceLocation rparenLoc;
StatementAST* statement = nullptr;
BlockSymbol* symbol = nullptr;

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

Expand All @@ -1047,6 +1051,7 @@ class ForStatementAST final : public StatementAST {
ExpressionAST* expression = nullptr;
SourceLocation rparenLoc;
StatementAST* statement = nullptr;
BlockSymbol* symbol = nullptr;

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

Expand Down
Loading