Skip to content

Commit b20d16e

Browse files
committed
chore: Replaced dynamic_cast with ast_cast
1 parent 8168623 commit b20d16e

File tree

3 files changed

+663
-261
lines changed

3 files changed

+663
-261
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
5454
nodes.forEach(({ name, base, members }) => {
5555
emit(`class ${name} final : public ${base} {`);
5656
emit(`public:`);
57-
emit(` ${name}(): ${base}(ASTKind::${enumName(name)}) {}`);
57+
emit(` static constexpr ASTKind Kind = ASTKind::${enumName(name)};`);
58+
emit();
59+
emit(` ${name}(): ${base}(Kind) {}`);
5860
emit();
5961

6062
members.forEach((m) => {
@@ -88,7 +90,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
8890
}
8991

9092
emit(
91-
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`,
93+
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`
9294
);
9395
emit();
9496
emit(` auto firstSourceLocation() -> SourceLocation override;`);
@@ -180,6 +182,11 @@ inline auto lastSourceLocation(List<T>* nodes) -> SourceLocation {
180182
181183
${code.join("\n")}
182184
185+
template <typename T>
186+
auto ast_cast(AST* ast) -> T* {
187+
return ast && ast->kind() == T::Kind ? static_cast<T*>(ast) : nullptr;
188+
}
189+
183190
} // namespace cxx
184191
`;
185192

0 commit comments

Comments
 (0)