Skip to content

Commit 7da1bfd

Browse files
committed
Regenerate the AST rewriter
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 275f945 commit 7da1bfd

File tree

8 files changed

+1210
-846
lines changed

8 files changed

+1210
-846
lines changed

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ export function new_ast_rewriter_cc({
5555
return name;
5656
};
5757

58+
by_base.forEach((nodes, base) => {
59+
if (!Array.isArray(nodes)) throw new Error("not an array");
60+
if (base === "AST") return;
61+
const className = chopAST(base);
62+
emit();
63+
emit(` struct ASTRewriter::${className}Visitor {`);
64+
emit(` ${opName}& rewrite;`);
65+
emit();
66+
emit(
67+
` [[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }`
68+
);
69+
nodes.forEach(({ name }) => {
70+
emit();
71+
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${base}*;`);
72+
});
73+
emit(` };`);
74+
});
75+
5876
const emitRewriterBody = (members: Member[], visitor: string = "rewrite") => {
5977
members.forEach((m) => {
6078
switch (m.kind) {
@@ -100,11 +118,20 @@ export function new_ast_rewriter_cc({
100118
by_base.forEach((nodes, base) => {
101119
if (base === "AST") return;
102120
emit();
103-
emit(`auto ${opName}::operator()(${base}* ast) -> ${base}* {`);
104-
emit(` if (ast)`);
105-
emit(` return visit(${chopAST(base)}Visitor{*this}, ast);`);
106-
emit(` return {};`);
107-
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+
}
108135
});
109136
by_base.get("AST")?.forEach(({ name, members }) => {
110137
emit();
@@ -146,12 +173,17 @@ export function new_ast_rewriter_cc({
146173
147174
// cxx
148175
#include <cxx/ast.h>
149-
#include <cxx/translation_unit.h>
150176
#include <cxx/control.h>
177+
#include <cxx/translation_unit.h>
178+
#include <cxx/type_checker.h>
151179
152180
namespace cxx {
153181
154-
${opName}::${opName}(TranslationUnit* unit) : unit_(unit) {}
182+
${opName}::${opName}(TypeChecker* typeChcker,
183+
const std::vector<TemplateArgument>& templateArguments)
184+
: typeChecker_(typeChcker)
185+
, unit_(typeChcker->translationUnit())
186+
, templateArguments_(templateArguments) {}
155187
156188
${opName}::~${opName}() {}
157189

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,28 @@ export function new_ast_rewriter_h({
6161
if (!Array.isArray(nodes)) throw new Error("not an array");
6262
if (base === "AST") return;
6363
const className = chopAST(base);
64-
emit();
65-
emit(` struct ${className}Visitor {`);
66-
emit(` ${opName}& rewrite;`);
67-
emit();
68-
emit(
69-
` [[nodiscard]] auto arena() const -> Arena* { return rewrite.arena(); }`,
70-
);
71-
nodes.forEach(({ name }) => {
72-
emit();
73-
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${base}*;`);
74-
});
75-
emit(` };`);
64+
emit(` struct ${className}Visitor;`);
7665
});
7766

7867
const out = `${cpy_header}
7968
8069
#pragma once
8170
8271
#include <cxx/ast_fwd.h>
72+
#include <cxx/names_fwd.h>
73+
74+
#include <vector>
8375
8476
namespace cxx {
8577
8678
class TranslationUnit;
79+
class TypeChecker;
8780
class Control;
8881
class Arena;
8982
9083
class ${opName} {
9184
public:
92-
explicit ${opName}(TranslationUnit* unit);
85+
explicit ${opName}(TypeChecker* typeChecker, const std::vector<TemplateArgument>& templateArguments);
9386
~${opName}();
9487
9588
[[nodiscard]] auto translationUnit() const -> TranslationUnit* { return unit_; }
@@ -99,6 +92,9 @@ public:
9992
10093
${code.join("\n")}
10194
95+
private:
96+
TypeChecker* typeChecker_ = nullptr;
97+
const std::vector<TemplateArgument>& templateArguments_;
10298
TranslationUnit* unit_ = nullptr;
10399
};
104100

src/lsp/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919

2020
file(GLOB CXX_LSP_INCLUDE_HEADER_FILES cxx/lsp/*.h)
2121

22-
aux_source_directory(cxx/lsp LSP_SOURCES)
23-
2422
add_library(cxx-lsp
25-
${LSP_SOURCES}
23+
cxx/lsp/cxx_document.cc
24+
cxx/lsp/enums.cc
25+
cxx/lsp/lsp.cc
26+
cxx/lsp/lsp_server.cc
27+
cxx/lsp/requests.cc
28+
cxx/lsp/sync_queue.cc
29+
cxx/lsp/types.cc
2630
${CXX_LSP_INCLUDE_HEADER_FILES}
2731
)
2832

src/parser/CMakeLists.txt

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,49 @@
2020
file(GLOB CXX_INCLUDE_HEADER_FILES cxx/*.h)
2121
file(GLOB CXX_VIEWS_INCLUDE_HEADER_FILES cxx/views/*.h)
2222

23-
aux_source_directory(cxx SOURCES)
23+
add_library(cxx-parser
24+
cxx/ast.cc
25+
cxx/ast_cursor.cc
26+
cxx/ast_interpreter.cc
27+
cxx/ast_rewriter.cc
28+
cxx/ast_slot.cc
29+
cxx/base_classes.cc
30+
cxx/cli.cc
31+
cxx/const_expression_evaluator.cc
32+
cxx/const_value.cc
33+
cxx/control.cc
34+
cxx/cxx.cc
35+
cxx/diagnostic.cc
36+
cxx/diagnostics_client.cc
37+
cxx/external_name_encoder.cc
38+
cxx/gcc_linux_toolchain.cc
39+
cxx/lexer.cc
40+
cxx/literals.cc
41+
cxx/macos_toolchain.cc
42+
cxx/memory_layout.cc
43+
cxx/name_lookup.cc
44+
cxx/name_printer.cc
45+
cxx/names.cc
46+
cxx/parser.cc
47+
cxx/path.cc
48+
cxx/preprocessor.cc
49+
cxx/scope.cc
50+
cxx/source_location.cc
51+
cxx/symbol_chain_view.cc
52+
cxx/symbol_instantiation.cc
53+
cxx/symbol_printer.cc
54+
cxx/symbols.cc
55+
cxx/token.cc
56+
cxx/toolchain.cc
57+
cxx/translation_unit.cc
58+
cxx/type_checker.cc
59+
cxx/type_printer.cc
60+
cxx/type_traits.cc
61+
cxx/types.cc
62+
cxx/util.cc
63+
cxx/wasm32_wasi_toolchain.cc
64+
cxx/windows_toolchain.cc
2465

25-
add_library(cxx-parser ${SOURCES}
2666
# generated files
2767
keywords-priv.h
2868
pp_keywords-priv.h
@@ -63,8 +103,10 @@ add_custom_command(OUTPUT pp_keywords-priv.h
63103
COMMENT "Generate pp_keywords-priv.h")
64104

65105
if (CXX_ENABLE_FLATBUFFERS)
66-
aux_source_directory(cxx/flatbuffers FLATBUFFERS_SOURCES)
67-
target_sources(cxx-parser PRIVATE ${FLATBUFFERS_SOURCES})
106+
target_sources(cxx-parser PRIVATE
107+
cxx/flatbuffers/ast_decoder.cc
108+
cxx/flatbuffers/ast_encoder.cc
109+
)
68110

69111
# generate flatbuffers decoder for ast.fbs
70112
flatbuffers_generate_headers(

0 commit comments

Comments
 (0)