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
20 changes: 17 additions & 3 deletions packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7667,25 +7667,39 @@ export class BaseSpecifierAST extends AST {
);
}

/**
* Returns the location of the ellipsis token in this node
*/
getEllipsisToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 6), this.parser);
}

/**
* Returns the isTemplateIntroduced attribute of this node
*/
getIsTemplateIntroduced(): boolean {
return cxx.getASTSlot(this.getHandle(), 6) !== 0;
return cxx.getASTSlot(this.getHandle(), 7) !== 0;
}

/**
* Returns the isVirtual attribute of this node
*/
getIsVirtual(): boolean {
return cxx.getASTSlot(this.getHandle(), 7) !== 0;
return cxx.getASTSlot(this.getHandle(), 8) !== 0;
}

/**
* Returns the isVariadic attribute of this node
*/
getIsVariadic(): boolean {
return cxx.getASTSlot(this.getHandle(), 9) !== 0;
}

/**
* Returns the accessSpecifier attribute of this node
*/
getAccessSpecifier(): TokenKind {
return cxx.getASTSlot(this.getHandle(), 8);
return cxx.getASTSlot(this.getHandle(), 10);
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/cxx-gen-ast/src/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { gen_ast_slot_cc } from "./gen_ast_slot_cc.js";
import { gen_ast_slot_h } from "./get_ast_slot_h.js";
import { gen_ast_ts } from "./gen_ast_ts.js";
import { gen_ast_visitor_h } from "./gen_ast_visitor_h.js";
import { gen_ast_visitor_cc } from "./gen_ast_visitor_cc.js";
import { gen_ast_visitor_ts } from "./gen_ast_visitor_ts.js";
import { parseAST } from "./parseAST.js";
import { gen_ast_kind_ts } from "./gen_ast_kind_ts.js";
Expand Down Expand Up @@ -67,6 +68,10 @@ gen_ast_visitor_h({
ast,
output: path.join(outdir, "src/parser/cxx/ast_visitor.h"),
});
gen_ast_visitor_cc({
ast,
output: path.join(outdir, "src/parser/cxx/ast_visitor.cc"),
});
gen_ast_printer_h({
ast,
output: path.join(outdir, "src/parser/cxx/ast_printer.h"),
Expand Down
91 changes: 91 additions & 0 deletions packages/cxx-gen-ast/src/gen_ast_visitor_cc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2024 Roberto Raggi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { groupNodesByBaseType } from "./groupNodesByBaseType.js";
import { AST, getASTNodes } from "./parseAST.js";
import { cpy_header } from "./cpy_header.js";
import * as fs from "fs";

export function gen_ast_visitor_cc({
ast,
output,
}: {
ast: AST;
output: string;
}) {
const code: string[] = [];
const emit = (line = "") => code.push(line);

const by_base = groupNodesByBaseType(ast);

const types = new Set<string>();

ast.nodes.forEach((node) => {
node.members.forEach((m) => {
if (m.kind === "node" || m.kind === "node-list") types.add(m.type);
});
});

by_base.forEach((nodes) => {
nodes.forEach(({ name, members }) => {
members = getASTNodes(members);

emit();
emit(`void ASTVisitor::visit(${name}* ast) {`);
members.forEach((m) => {
if (m.kind === "node") {
emit(`accept(ast->${m.name});`);
} else if (m.kind === "node-list") {
emit(`for (auto node : ListView{ast->${m.name}}) {`);
emit(`accept(node);`);
emit(`}`);
}
});
emit(`}`);
});
});

const out = `${cpy_header}
#include <cxx/ast_visitor.h>

// cxx
#include <cxx/ast.h>

namespace cxx {

auto ASTVisitor::preVisit(AST*) -> bool {
return true;
}

void ASTVisitor::postVisit(AST*) {}

void ASTVisitor::accept(AST* ast) {
if (!ast) return;
if (preVisit(ast)) ast->accept(this);
postVisit(ast);
}

${code.join("\n")}

} // namespace cxx
`;

fs.writeFileSync(output, out);
}
8 changes: 7 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_visitor_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function gen_ast_visitor_h({
emit();
emit(` // ${base}`);
nodes.forEach(({ name }) => {
emit(` virtual void visit(${name}* ast) = 0;`);
emit(` virtual void visit(${name}* ast);`);
});
});

Expand All @@ -55,6 +55,12 @@ namespace cxx {
class ASTVisitor {
public:
virtual ~ASTVisitor() = default;

void accept(AST* ast);

[[nodiscard]] virtual bool preVisit(AST* ast);
virtual void postVisit(AST* ast);

${code.join("\n")}
};

Expand Down
Loading