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
4 changes: 2 additions & 2 deletions packages/cxx-gen-ast/src/gen_ast_dump_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export function gen_ast_dump_cc({ ast, output }: { ast: AST; output: string }) {
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(` out_ << std::format("{}\\n", "${fieldName}");`);
emit(` for (auto it = ast->${member.name}; it; it = it->next) {`);
emit(` accept(it->value);`);
emit(` for (auto node: ListView{ast->${member.name}}) {`);
emit(` accept(node);`);
emit(` }`);
emit(` --indent_;`);
emit(` }`);
Expand Down
12 changes: 6 additions & 6 deletions packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ export function gen_ast_encoder_cc({
const className = makeClassName(m.type);
emit(` std::vector<flatbuffers::Offset<io::${className}>>`);
emit(` ${m.name}Offsets;`);
emit(` for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(` if (!it->value) continue;`);
emit(` ${m.name}Offsets.emplace_back(accept(it->value).o);`);
emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` if (!node) continue;`);
emit(` ${m.name}Offsets.emplace_back(accept(node).o);`);
emit(` }`);
emit();
emit(` auto ${m.name}OffsetsVector = fbb_.CreateVector(`);
Expand All @@ -130,10 +130,10 @@ export function gen_ast_encoder_cc({
emit(` std::vector<std::underlying_type_t<io::${className}>>`);
emit(` ${m.name}Types;`);
emit();
emit(` for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(` if (!it->value) continue;`);
emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` if (!node) continue;`);
emit(` const auto [offset, type] = accept${className}(`);
emit(` it->value);`);
emit(` node);`);
emit(` ${m.name}Offsets.push_back(offset);`);
emit(` ${m.name}Types.push_back(type);`);
emit(` }`);
Expand Down
75 changes: 58 additions & 17 deletions packages/cxx-gen-ast/src/gen_ast_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
}

emit(
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`,
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`
);
emit();
emit(` auto firstSourceLocation() -> SourceLocation override;`);
Expand All @@ -110,7 +110,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
emit(` switch (ast->kind()) {`);
nodes.forEach(({ name }) => {
emit(
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`,
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`
);
});
emit(` default: cxx_runtime_error("unexpected ${variantName}");`);
Expand All @@ -130,35 +130,76 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
#include <cxx/const_value.h>
#include <cxx/symbols_fwd.h>
#include <optional>
#include <ranges>

namespace cxx {

template <typename T>
class List final : public Managed {
public:
T value;
List* next;
T value;
List* next;

explicit List(const T& value, List* next = nullptr)
: value(value), next(next) {}
explicit List(const T& value, List* next = nullptr)
: value(value), next(next) {}
};

template <typename T>
class ListIterator {
public:
using value_type = T;
using difference_type = std::ptrdiff_t;

ListIterator() = default;
explicit ListIterator(List<T>* list) : list_(list) {}

auto operator<=>(const ListIterator&) const = default;

auto operator*() const -> const T& { return list_->value; }

auto operator++() -> ListIterator& {
list_ = list_->next;
return *this;
}

auto operator++(int) -> ListIterator {
auto it = *this;
++*this;
return it;
}

private:
List<T>* list_{};
};

template <typename T>
class ListView : std::ranges::view_interface<ListView<T>> {
public:
explicit ListView(List<T>* list) : list_(list) {}

auto begin() const { return ListIterator<T>(list_); }
auto end() const { return ListIterator<T>(); }

private:
List<T>* list_;
};

class AST : public Managed {
public:
explicit AST(ASTKind kind): kind_(kind) {}
explicit AST(ASTKind kind): kind_(kind) {}

virtual ~AST();
virtual ~AST();

[[nodiscard]] auto kind() const -> ASTKind { return kind_; }
[[nodiscard]] auto kind() const -> ASTKind { return kind_; }

virtual void accept(ASTVisitor* visitor) = 0;
virtual void accept(ASTVisitor* visitor) = 0;

virtual auto firstSourceLocation() -> SourceLocation = 0;
virtual auto lastSourceLocation() -> SourceLocation = 0;
virtual auto firstSourceLocation() -> SourceLocation = 0;
virtual auto lastSourceLocation() -> SourceLocation = 0;

[[nodiscard]] auto sourceLocationRange() -> SourceLocationRange {
return SourceLocationRange(firstSourceLocation(), lastSourceLocation());
}
[[nodiscard]] auto sourceLocationRange() -> SourceLocationRange {
return SourceLocationRange(firstSourceLocation(), lastSourceLocation());
}

private:
ASTKind kind_;
Expand All @@ -173,8 +214,8 @@ template <typename T>

template <typename T>
[[nodiscard]] inline auto firstSourceLocation(List<T>* nodes) -> SourceLocation {
for (auto it = nodes; it; it = it->next) {
if (auto loc = firstSourceLocation(it->value)) return loc;
for (auto node : ListView{nodes}) {
if (auto loc = firstSourceLocation(node)) return loc;
}
return {};
}
Expand Down
10 changes: 5 additions & 5 deletions packages/cxx-gen-ast/src/new_ast_op_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export function new_ast_op_cc({
}
case "node-list": {
emit();
emit(` for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(` auto value = operator()(it->value);`);
emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` auto value = operator()(node);`);
emit(` }`);
emit();
break;
Expand All @@ -90,7 +90,7 @@ export function new_ast_op_cc({
const resultTy = `${chopAST(base)}Result`;
emit();
emit(
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`,
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`
);
members.forEach((m) => {
switch (m.kind) {
Expand All @@ -100,8 +100,8 @@ export function new_ast_op_cc({
}
case "node-list": {
emit();
emit(` for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(` auto value = accept(it->value);`);
emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` auto value = accept(node);`);
emit(` }`);
emit();
break;
Expand Down
8 changes: 4 additions & 4 deletions packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function new_ast_rewriter_cc({
emit(` copy->${m.name} = ${visitor}(ast->${m.name});`);
} else {
emit(
` copy->${m.name} = ast_cast<${m.type}>(${visitor}(ast->${m.name}));`,
` copy->${m.name} = ast_cast<${m.type}>(${visitor}(ast->${m.name}));`
);
}
break;
Expand All @@ -72,8 +72,8 @@ export function new_ast_rewriter_cc({
emit();
emit(` if (auto it = ast->${m.name}) {`);
emit(` auto out = &copy->${m.name};`);
emit(` for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(` auto value = ${visitor}(it->value);`);
emit(` for (auto node : ListView{ast->${m.name}}) {`);
emit(` auto value = ${visitor}(node);`);
if (isBase(m.type)) {
emit(`*out = new (arena()) List(value);`);
} else {
Expand Down Expand Up @@ -126,7 +126,7 @@ export function new_ast_rewriter_cc({
nodes.forEach(({ name, members }) => {
emit();
emit(
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${base}* {`,
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${base}* {`
);
emit(` auto copy = new (arena()) ${name}{};`);
emit();
Expand Down
Loading