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
25 changes: 25 additions & 0 deletions packages/cxx-gen-ast/src/gen_ast_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@
// SOFTWARE.

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

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

const by_bases = groupNodesByBaseType(ast);

const toKebapName = (name: string) =>
name.replace(/([A-Z]+)/g, "-$1").toLocaleLowerCase();

const astName = (name: string) => toKebapName(name.slice(0, -3)).slice(1);

ast.nodes.forEach(({ name, members }) => {
emit();
emit(`auto ${name}::firstSourceLocation() -> SourceLocation {`);
Expand All @@ -46,6 +54,23 @@ export function gen_ast_cc({ ast, output }: { ast: AST; output: string }) {
emit(`}`);
});

emit();
emit(`namespace {`);
emit(`std::string_view kASTKindNames[] = {`);
by_bases.forEach((nodes, base) => {
emit();
emit(` // ${base}`);
nodes.forEach(({ name }) => {
emit(` "${astName(name)}",`);
});
});
emit(`};`);
emit(`} // namespace`);

emit(`auto to_string(ASTKind kind) -> std::string_view {`);
emit(` return kASTKindNames[int(kind)];`);
emit(`}`);

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

Expand Down
2 changes: 2 additions & 0 deletions packages/cxx-gen-ast/src/gen_ast_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ enum class ImplicitCastKind {

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

enum class ASTKind;
auto to_string(ASTKind kind) -> std::string_view;
auto to_string(ValueCategory valueCategory) -> std::string_view;
auto to_string(ImplicitCastKind implicitCastKind) -> std::string_view;

Expand Down
2 changes: 1 addition & 1 deletion packages/cxx-gen-ast/src/gen_ast_kind_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function gen_ast_kind_h({ ast, output }: { ast: AST; output: string }) {

const enumName = (name: string) => name.slice(0, -3);

emit(`enum struct ASTKind {`);
emit(`enum class ASTKind {`);

by_bases.forEach((nodes, base) => {
emit();
Expand Down
12 changes: 6 additions & 6 deletions packages/cxx-gen-ast/src/gen_ast_slot_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
emit(`};`);
emit(`} // namespace`);

emit(`std::string_view to_string(SlotNameIndex index) {`);
emit(`auto to_string(SlotNameIndex index) -> std::string_view {`);
emit(` return kMemberSlotNames[int(index)];`);
emit(`}`);

by_base.forEach((nodes) => {
nodes.forEach(({ name, members }) => {
const memberSlots = members.filter(
(m) => classifyMemberSlot(m) !== undefined,
(m) => classifyMemberSlot(m) !== undefined
);

emit();
Expand Down Expand Up @@ -87,7 +87,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.IdentifierAttribute:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
);
emit(` slotKind_ = ASTSlotKind::kIdentifierAttribute;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -96,7 +96,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.LiteralAttribute:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
);
emit(` slotKind_ = ASTSlotKind::kLiteralAttribute;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -112,7 +112,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.Node:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
);
emit(` slotKind_ = ASTSlotKind::kNode;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -121,7 +121,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.NodeList:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
);
emit(` slotKind_ = ASTSlotKind::kNodeList;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand Down
12 changes: 6 additions & 6 deletions packages/cxx-gen-ast/src/gen_token_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {

emit("#define FOR_EACH_BASE_TOKEN(V) \\");
tokens.BASE_TOKENS.forEach((tk) =>
emit(` V(${tk}, "${baseTokenId(tk)}") \\`),
emit(` V(${tk}, "${baseTokenId(tk)}") \\`)
);

emit();
emit("#define FOR_EACH_OPERATOR(V) \\");
tokens.OPERATORS.forEach(([tk, spelling]) =>
emit(` V(${tk}, "${spelling}") \\`),
emit(` V(${tk}, "${spelling}") \\`)
);

emit();
Expand All @@ -46,13 +46,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
emit();
emit("#define FOR_EACH_BUILTIN_TYPE_TRAIT(V) \\");
tokens.BUILTIN_TYPE_TRAITS.forEach((tk) =>
emit(` V(${tk.toUpperCase()}, "${tk}") \\`),
emit(` V(${tk.toUpperCase()}, "${tk}") \\`)
);

emit();
emit("#define FOR_EACH_TOKEN_ALIAS(V) \\");
tokens.TOKEN_ALIASES.forEach(([tk, other]) =>
emit(` V(${tk.toUpperCase()}, ${other}) \\`),
emit(` V(${tk.toUpperCase()}, ${other}) \\`)
);

const out = `${cpy_header}
Expand All @@ -76,12 +76,12 @@ ${code.join("\n")}
// clang-format off
#define TOKEN_ENUM(tk, _) T_##tk,
#define TOKEN_ALIAS_ENUM(tk, other) T_##tk = T_##other,
enum struct TokenKind : std::uint8_t {
enum class TokenKind : std::uint8_t {
FOR_EACH_TOKEN(TOKEN_ENUM)
FOR_EACH_TOKEN_ALIAS(TOKEN_ALIAS_ENUM)
};

enum struct BuiltinTypeTraitKind {
enum class BuiltinTypeTraitKind {
T_NONE,
FOR_EACH_BUILTIN_TYPE_TRAIT(TOKEN_ENUM)
};
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@
#include <cxx/lexer.h>
#include <cxx/lsp/lsp_server.h>
#include <cxx/macos_toolchain.h>
#include <cxx/name_printer.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>
#include <cxx/scope.h>
#include <cxx/symbol_printer.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/type_printer.h>
#include <cxx/types.h>
#include <cxx/wasm32_wasi_toolchain.h>
#include <cxx/windows_toolchain.h>
Expand Down Expand Up @@ -85,8 +82,11 @@ struct CheckExpressionTypes {
continue;
}

auto loc = expression->firstSourceLocation();
unit->warning(loc, std::format("missing type for expression"));
const auto loc = expression->firstSourceLocation();

unit->warning(loc, std::format("untyped expression of kind '{}'",
to_string(expression->kind())));

++missingTypes;
}

Expand Down
3 changes: 0 additions & 3 deletions src/lsp/cxx/lsp/cxx_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
#include <cxx/lsp/enums.h>
#include <cxx/lsp/types.h>
#include <cxx/macos_toolchain.h>
#include <cxx/name_printer.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>
#include <cxx/scope.h>
#include <cxx/symbol_printer.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/type_printer.h>
#include <cxx/types.h>
#include <cxx/wasm32_wasi_toolchain.h>
#include <cxx/windows_toolchain.h>
Expand Down
2 changes: 0 additions & 2 deletions src/lsp/cxx/lsp/lsp_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
#include <cxx/lsp/enums.h>
#include <cxx/lsp/requests.h>
#include <cxx/lsp/types.h>
#include <cxx/name_printer.h>
#include <cxx/preprocessor.h>
#include <cxx/symbols.h>
#include <cxx/type_printer.h>
#include <utf8/unchecked.h>

#include <format>
Expand Down
2 changes: 0 additions & 2 deletions src/mlir/cxx/mlir/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
#include <cxx/control.h>
#include <cxx/literals.h>
#include <cxx/mlir/cxx_dialect.h>
#include <cxx/name_printer.h>
#include <cxx/names.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/type_printer.h>
#include <cxx/types.h>
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
#include <mlir/Dialect/Func/IR/FuncOps.h>
Expand Down
Loading