Skip to content

Commit 08f19f7

Browse files
committed
Add expression kind to missing types warning
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 7e741fc commit 08f19f7

36 files changed

+408
-229
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@
1919
// SOFTWARE.
2020

2121
import { cpy_header } from "./cpy_header.js";
22+
import { groupNodesByBaseType } from "./groupNodesByBaseType.js";
2223
import { AST } from "./parseAST.js";
2324
import * as fs from "fs";
2425

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

30+
const by_bases = groupNodesByBaseType(ast);
31+
32+
const toKebapName = (name: string) =>
33+
name.replace(/([A-Z]+)/g, "-$1").toLocaleLowerCase();
34+
35+
const astName = (name: string) => toKebapName(name.slice(0, -3)).slice(1);
36+
2937
ast.nodes.forEach(({ name, members }) => {
3038
emit();
3139
emit(`auto ${name}::firstSourceLocation() -> SourceLocation {`);
@@ -46,6 +54,23 @@ export function gen_ast_cc({ ast, output }: { ast: AST; output: string }) {
4654
emit(`}`);
4755
});
4856

57+
emit();
58+
emit(`namespace {`);
59+
emit(`std::string_view kASTKindNames[] = {`);
60+
by_bases.forEach((nodes, base) => {
61+
emit();
62+
emit(` // ${base}`);
63+
nodes.forEach(({ name }) => {
64+
emit(` "${astName(name)}",`);
65+
});
66+
});
67+
emit(`};`);
68+
emit(`} // namespace`);
69+
70+
emit(`auto to_string(ASTKind kind) -> std::string_view {`);
71+
emit(` return kASTKindNames[int(kind)];`);
72+
emit(`}`);
73+
4974
const out = `${cpy_header}
5075
#include <cxx/ast.h>
5176

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ enum class ImplicitCastKind {
8888
8989
${code.join("\n")}
9090
91+
enum class ASTKind;
92+
auto to_string(ASTKind kind) -> std::string_view;
9193
auto to_string(ValueCategory valueCategory) -> std::string_view;
9294
auto to_string(ImplicitCastKind implicitCastKind) -> std::string_view;
9395

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function gen_ast_kind_h({ ast, output }: { ast: AST; output: string }) {
3131

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

34-
emit(`enum struct ASTKind {`);
34+
emit(`enum class ASTKind {`);
3535

3636
by_bases.forEach((nodes, base) => {
3737
emit();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
4444
emit(`};`);
4545
emit(`} // namespace`);
4646

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

5151
by_base.forEach((nodes) => {
5252
nodes.forEach(({ name, members }) => {
5353
const memberSlots = members.filter(
54-
(m) => classifyMemberSlot(m) !== undefined,
54+
(m) => classifyMemberSlot(m) !== undefined
5555
);
5656

5757
emit();
@@ -87,7 +87,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
8787
case MemberSlotClassification.IdentifierAttribute:
8888
emit(` case ${slotCount}: // ${m.name}`);
8989
emit(
90-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
90+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
9191
);
9292
emit(` slotKind_ = ASTSlotKind::kIdentifierAttribute;`);
9393
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -96,7 +96,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
9696
case MemberSlotClassification.LiteralAttribute:
9797
emit(` case ${slotCount}: // ${m.name}`);
9898
emit(
99-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
99+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
100100
);
101101
emit(` slotKind_ = ASTSlotKind::kLiteralAttribute;`);
102102
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -112,7 +112,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
112112
case MemberSlotClassification.Node:
113113
emit(` case ${slotCount}: // ${m.name}`);
114114
emit(
115-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
115+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
116116
);
117117
emit(` slotKind_ = ASTSlotKind::kNode;`);
118118
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -121,7 +121,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
121121
case MemberSlotClassification.NodeList:
122122
emit(` case ${slotCount}: // ${m.name}`);
123123
emit(
124-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
124+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
125125
);
126126
emit(` slotKind_ = ASTSlotKind::kNodeList;`);
127127
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
3030

3131
emit("#define FOR_EACH_BASE_TOKEN(V) \\");
3232
tokens.BASE_TOKENS.forEach((tk) =>
33-
emit(` V(${tk}, "${baseTokenId(tk)}") \\`),
33+
emit(` V(${tk}, "${baseTokenId(tk)}") \\`)
3434
);
3535

3636
emit();
3737
emit("#define FOR_EACH_OPERATOR(V) \\");
3838
tokens.OPERATORS.forEach(([tk, spelling]) =>
39-
emit(` V(${tk}, "${spelling}") \\`),
39+
emit(` V(${tk}, "${spelling}") \\`)
4040
);
4141

4242
emit();
@@ -46,13 +46,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
4646
emit();
4747
emit("#define FOR_EACH_BUILTIN_TYPE_TRAIT(V) \\");
4848
tokens.BUILTIN_TYPE_TRAITS.forEach((tk) =>
49-
emit(` V(${tk.toUpperCase()}, "${tk}") \\`),
49+
emit(` V(${tk.toUpperCase()}, "${tk}") \\`)
5050
);
5151

5252
emit();
5353
emit("#define FOR_EACH_TOKEN_ALIAS(V) \\");
5454
tokens.TOKEN_ALIASES.forEach(([tk, other]) =>
55-
emit(` V(${tk.toUpperCase()}, ${other}) \\`),
55+
emit(` V(${tk.toUpperCase()}, ${other}) \\`)
5656
);
5757

5858
const out = `${cpy_header}
@@ -76,12 +76,12 @@ ${code.join("\n")}
7676
// clang-format off
7777
#define TOKEN_ENUM(tk, _) T_##tk,
7878
#define TOKEN_ALIAS_ENUM(tk, other) T_##tk = T_##other,
79-
enum struct TokenKind : std::uint8_t {
79+
enum class TokenKind : std::uint8_t {
8080
FOR_EACH_TOKEN(TOKEN_ENUM)
8181
FOR_EACH_TOKEN_ALIAS(TOKEN_ALIAS_ENUM)
8282
};
8383
84-
enum struct BuiltinTypeTraitKind {
84+
enum class BuiltinTypeTraitKind {
8585
T_NONE,
8686
FOR_EACH_BUILTIN_TYPE_TRAIT(TOKEN_ENUM)
8787
};

src/frontend/cxx/frontend.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727
#include <cxx/lexer.h>
2828
#include <cxx/lsp/lsp_server.h>
2929
#include <cxx/macos_toolchain.h>
30-
#include <cxx/name_printer.h>
3130
#include <cxx/preprocessor.h>
3231
#include <cxx/private/path.h>
3332
#include <cxx/scope.h>
34-
#include <cxx/symbol_printer.h>
3533
#include <cxx/symbols.h>
3634
#include <cxx/translation_unit.h>
37-
#include <cxx/type_printer.h>
3835
#include <cxx/types.h>
3936
#include <cxx/wasm32_wasi_toolchain.h>
4037
#include <cxx/windows_toolchain.h>
@@ -85,8 +82,11 @@ struct CheckExpressionTypes {
8582
continue;
8683
}
8784

88-
auto loc = expression->firstSourceLocation();
89-
unit->warning(loc, std::format("missing type for expression"));
85+
const auto loc = expression->firstSourceLocation();
86+
87+
unit->warning(loc, std::format("untyped expression of kind '{}'",
88+
to_string(expression->kind())));
89+
9090
++missingTypes;
9191
}
9292

src/lsp/cxx/lsp/cxx_document.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
#include <cxx/lsp/enums.h>
2727
#include <cxx/lsp/types.h>
2828
#include <cxx/macos_toolchain.h>
29-
#include <cxx/name_printer.h>
3029
#include <cxx/preprocessor.h>
3130
#include <cxx/private/path.h>
3231
#include <cxx/scope.h>
33-
#include <cxx/symbol_printer.h>
3432
#include <cxx/symbols.h>
3533
#include <cxx/translation_unit.h>
36-
#include <cxx/type_printer.h>
3734
#include <cxx/types.h>
3835
#include <cxx/wasm32_wasi_toolchain.h>
3936
#include <cxx/windows_toolchain.h>

src/lsp/cxx/lsp/lsp_server.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
#include <cxx/lsp/enums.h>
2525
#include <cxx/lsp/requests.h>
2626
#include <cxx/lsp/types.h>
27-
#include <cxx/name_printer.h>
2827
#include <cxx/preprocessor.h>
2928
#include <cxx/symbols.h>
30-
#include <cxx/type_printer.h>
3129
#include <utf8/unchecked.h>
3230

3331
#include <format>

src/mlir/cxx/mlir/codegen.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
#include <cxx/control.h>
2727
#include <cxx/literals.h>
2828
#include <cxx/mlir/cxx_dialect.h>
29-
#include <cxx/name_printer.h>
3029
#include <cxx/names.h>
3130
#include <cxx/symbols.h>
3231
#include <cxx/translation_unit.h>
33-
#include <cxx/type_printer.h>
3432
#include <cxx/types.h>
3533
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
3634
#include <mlir/Dialect/Func/IR/FuncOps.h>

0 commit comments

Comments
 (0)