diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 13:38:27 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 13:38:27 -0400 |
| commit | 7204970fccd5f1903c38b97c9c0778892c44fdfe (patch) | |
| tree | db3e4afa89834c9387ac686f49ab5f777f63ed28 /src/compile | |
| parent | e419a527a1212123946e53adaaea01df0c5605c3 (diff) | |
Got inlne enums working in most places
Diffstat (limited to 'src/compile')
| -rw-r--r-- | src/compile/enums.c | 64 | ||||
| -rw-r--r-- | src/compile/enums.h | 6 | ||||
| -rw-r--r-- | src/compile/files.c | 25 | ||||
| -rw-r--r-- | src/compile/headers.c | 41 |
4 files changed, 96 insertions, 40 deletions
diff --git a/src/compile/enums.c b/src/compile/enums.c index f5500831..f8a3994e 100644 --- a/src/compile/enums.c +++ b/src/compile/enums.c @@ -8,15 +8,13 @@ #include "../typecheck.h" #include "compilation.h" -Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { - DeclareMatch(def, ast, EnumDef); - +Text_t compile_enum_typeinfo(env_t *env, const char *name, tag_ast_t *tags) { // Compile member types and constructors: Text_t member_typeinfos = EMPTY_TEXT; - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + for (tag_ast_t *tag = tags; tag; tag = tag->next) { if (!tag->fields) continue; - const char *tag_name = String(def->name, "$", tag->name); + const char *tag_name = String(name, "$", tag->name); type_t *tag_type = Table$str_get(*env->types, tag_name); assert(tag_type && tag_type->tag == StructType); member_typeinfos = @@ -24,21 +22,21 @@ Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { } int num_tags = 0; - for (tag_ast_t *t = def->tags; t; t = t->next) + for (tag_ast_t *t = tags; t; t = t->next) num_tags += 1; - type_t *t = Table$str_get(*env->types, def->name); + type_t *t = Table$str_get(*env->types, name); const char *metamethods = is_packed_data(t) ? "PackedDataEnum$metamethods" : "Enum$metamethods"; - Text_t info = namespace_name(env, env->namespace, Texts(def->name, "$$info")); + Text_t info = namespace_name(env, env->namespace, Texts(name, "$$info")); Text_t typeinfo = Texts("public const TypeInfo_t ", info, " = {", (int64_t)type_size(t), "u, ", (int64_t)type_align(t), - "u, .metamethods=", metamethods, ", {.tag=EnumInfo, .EnumInfo={.name=\"", def->name, + "u, .metamethods=", metamethods, ", {.tag=EnumInfo, .EnumInfo={.name=\"", name, "\", " ".num_tags=", (int64_t)num_tags, ", .tags=(NamedType_t[]){"); - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - const char *tag_type_name = String(def->name, "$", tag->name); + for (tag_ast_t *tag = tags; tag; tag = tag->next) { + const char *tag_type_name = String(name, "$", tag->name); type_t *tag_type = Table$str_get(*env->types, tag_type_name); if (tag_type && Match(tag_type, StructType)->fields) typeinfo = Texts(typeinfo, "{\"", tag->name, "\", ", compile_type_info(tag_type), "}, "); @@ -48,10 +46,9 @@ Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { return Texts(member_typeinfos, typeinfo); } -Text_t compile_enum_constructors(env_t *env, ast_t *ast) { - DeclareMatch(def, ast, EnumDef); +Text_t compile_enum_constructors(env_t *env, const char *name, tag_ast_t *tags) { Text_t constructors = EMPTY_TEXT; - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + for (tag_ast_t *tag = tags; tag; tag = tag->next) { if (!tag->fields) continue; Text_t arg_sig = EMPTY_TEXT; @@ -61,9 +58,9 @@ Text_t compile_enum_constructors(env_t *env, ast_t *ast) { if (field->next) arg_sig = Texts(arg_sig, ", "); } if (arg_sig.length == 0) arg_sig = Text("void"); - Text_t type_name = namespace_name(env, env->namespace, Texts(def->name, "$$type")); - Text_t tagged_name = namespace_name(env, env->namespace, Texts(def->name, "$tagged$", tag->name)); - Text_t tag_name = namespace_name(env, env->namespace, Texts(def->name, "$tag$", tag->name)); + Text_t type_name = namespace_name(env, env->namespace, Texts(name, "$$type")); + Text_t tagged_name = namespace_name(env, env->namespace, Texts(name, "$tagged$", tag->name)); + Text_t tag_name = namespace_name(env, env->namespace, Texts(name, "$tag$", tag->name)); Text_t constructor_impl = Texts("public inline ", type_name, " ", tagged_name, "(", arg_sig, ") { return (", type_name, "){.$tag=", tag_name, ", .", valid_c_name(tag->name), "={"); for (arg_ast_t *field = tag->fields; field; field = field->next) { @@ -76,16 +73,15 @@ Text_t compile_enum_constructors(env_t *env, ast_t *ast) { return constructors; } -Text_t compile_enum_header(env_t *env, ast_t *ast) { - DeclareMatch(def, ast, EnumDef); +Text_t compile_enum_header(env_t *env, const char *name, tag_ast_t *tags) { Text_t all_defs = EMPTY_TEXT; - Text_t none_name = namespace_name(env, env->namespace, Texts(def->name, "$none")); - Text_t enum_name = namespace_name(env, env->namespace, Texts(def->name, "$$enum")); + Text_t none_name = namespace_name(env, env->namespace, Texts(name, "$none")); + Text_t enum_name = namespace_name(env, env->namespace, Texts(name, "$$enum")); Text_t enum_tags = Texts("{ ", none_name, "=0, "); bool has_any_tags_with_fields = false; - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - Text_t tag_name = namespace_name(env, env->namespace, Texts(def->name, "$tag$", tag->name)); + for (tag_ast_t *tag = tags; tag; tag = tag->next) { + Text_t tag_name = namespace_name(env, env->namespace, Texts(name, "$tag$", tag->name)); enum_tags = Texts(enum_tags, tag_name); if (tag->next) enum_tags = Texts(enum_tags, ", "); has_any_tags_with_fields = has_any_tags_with_fields || (tag->fields != NULL); @@ -94,32 +90,32 @@ Text_t compile_enum_header(env_t *env, ast_t *ast) { if (!has_any_tags_with_fields) { Text_t enum_def = Texts("enum ", enum_name, " ", enum_tags, ";\n"); - Text_t info = namespace_name(env, env->namespace, Texts(def->name, "$$info")); + Text_t info = namespace_name(env, env->namespace, Texts(name, "$$info")); return Texts(enum_def, "extern const TypeInfo_t ", info, ";\n"); } - Text_t struct_name = namespace_name(env, env->namespace, Texts(def->name, "$$struct")); + Text_t struct_name = namespace_name(env, env->namespace, Texts(name, "$$struct")); Text_t enum_def = Texts("struct ", struct_name, " {\n" "enum ", enum_tags, " $tag;\n" "union {\n"); - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + for (tag_ast_t *tag = tags; tag; tag = tag->next) { if (!tag->fields) continue; - Text_t field_def = compile_struct_header( - env, - WrapAST(ast, StructDef, .name = Text$as_c_string(Texts(def->name, "$", tag->name)), .fields = tag->fields)); + Text_t field_def = compile_struct_header(env, NewAST(tag->file, tag->start, tag->end, StructDef, + .name = Text$as_c_string(Texts(name, "$", tag->name)), + .fields = tag->fields)); all_defs = Texts(all_defs, field_def); - Text_t tag_type = namespace_name(env, env->namespace, Texts(def->name, "$", tag->name, "$$type")); + Text_t tag_type = namespace_name(env, env->namespace, Texts(name, "$", tag->name, "$$type")); enum_def = Texts(enum_def, tag_type, " ", valid_c_name(tag->name), ";\n"); } enum_def = Texts(enum_def, "};\n};\n"); all_defs = Texts(all_defs, enum_def); - Text_t info = namespace_name(env, env->namespace, Texts(def->name, "$$info")); + Text_t info = namespace_name(env, env->namespace, Texts(name, "$$info")); all_defs = Texts(all_defs, "extern const TypeInfo_t ", info, ";\n"); - for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + for (tag_ast_t *tag = tags; tag; tag = tag->next) { if (!tag->fields) continue; Text_t arg_sig = EMPTY_TEXT; @@ -129,8 +125,8 @@ Text_t compile_enum_header(env_t *env, ast_t *ast) { if (field->next) arg_sig = Texts(arg_sig, ", "); } if (arg_sig.length == 0) arg_sig = Text("void"); - Text_t enum_type = namespace_name(env, env->namespace, Texts(def->name, "$$type")); - Text_t tagged_name = namespace_name(env, env->namespace, Texts(def->name, "$tagged$", tag->name)); + Text_t enum_type = namespace_name(env, env->namespace, Texts(name, "$$type")); + Text_t tagged_name = namespace_name(env, env->namespace, Texts(name, "$tagged$", tag->name)); Text_t constructor_def = Texts(enum_type, " ", tagged_name, "(", arg_sig, ");\n"); all_defs = Texts(all_defs, constructor_def); } diff --git a/src/compile/enums.h b/src/compile/enums.h index 888fc294..9fadb345 100644 --- a/src/compile/enums.h +++ b/src/compile/enums.h @@ -8,7 +8,7 @@ #include "../types.h" Text_t compile_empty_enum(type_t *t); -Text_t compile_enum_constructors(env_t *env, ast_t *ast); +Text_t compile_enum_constructors(env_t *env, const char *name, tag_ast_t *tags); Text_t compile_enum_field_access(env_t *env, ast_t *ast); -Text_t compile_enum_header(env_t *env, ast_t *ast); -Text_t compile_enum_typeinfo(env_t *env, ast_t *ast); +Text_t compile_enum_header(env_t *env, const char *name, tag_ast_t *tags); +Text_t compile_enum_typeinfo(env_t *env, const char *name, tag_ast_t *tags); diff --git a/src/compile/files.c b/src/compile/files.c index 4d6fb1a8..7ff252c2 100644 --- a/src/compile/files.c +++ b/src/compile/files.c @@ -125,8 +125,8 @@ Text_t compile_top_level_code(env_t *env, ast_t *ast) { } case EnumDef: { DeclareMatch(def, ast, EnumDef); - Text_t code = compile_enum_typeinfo(env, ast); - code = Texts(code, compile_enum_constructors(env, ast)); + Text_t code = compile_enum_typeinfo(env, def->name, def->tags); + code = Texts(code, compile_enum_constructors(env, def->name, def->tags)); return Texts(code, compile_namespace(env, def->name, def->namespace)); } case LangDef: { @@ -162,9 +162,30 @@ Text_t compile_top_level_code(env_t *env, ast_t *ast) { } } +typedef struct { + env_t *env; + Text_t *code; +} compile_info_t; + +static void add_type_infos(type_ast_t *type_ast, void *userdata) { + if (type_ast && type_ast->tag == EnumTypeAST) { + compile_info_t *info = (compile_info_t *)userdata; + *info->code = Texts( + *info->code, + compile_enum_typeinfo(info->env, String("enum$", (int64_t)(type_ast->start - type_ast->file->text)), + Match(type_ast, EnumTypeAST)->tags), + compile_enum_constructors(info->env, String("enum$", (int64_t)(type_ast->start - type_ast->file->text)), + Match(type_ast, EnumTypeAST)->tags)); + } +} + public Text_t compile_file(env_t *env, ast_t *ast) { Text_t top_level_code = compile_top_level_code(env, ast); + + compile_info_t info = {.env = env, .code = &top_level_code}; + type_ast_visit(ast, add_type_infos, &info); + Text_t includes = EMPTY_TEXT; Text_t use_imports = EMPTY_TEXT; diff --git a/src/compile/headers.c b/src/compile/headers.c index 33a979cf..77040445 100644 --- a/src/compile/headers.c +++ b/src/compile/headers.c @@ -146,6 +146,41 @@ static void _define_types_and_funcs(compile_typedef_info_t *info, ast_t *ast) { compile_statement_namespace_header(info->env, info->header_path, ast)); } +static void add_type_headers(type_ast_t *type_ast, void *userdata) { + if (!type_ast) return; + + if (type_ast->tag == EnumTypeAST) { + compile_typedef_info_t *info = (compile_typedef_info_t *)userdata; + DeclareMatch(enum_, type_ast, EnumTypeAST); + bool has_any_tags_with_fields = false; + for (tag_ast_t *tag = enum_->tags; tag; tag = tag->next) { + has_any_tags_with_fields = has_any_tags_with_fields || (tag->fields != NULL); + } + + const char *name = String("enum$", (int64_t)(type_ast->start - type_ast->file->text)); + if (has_any_tags_with_fields) { + Text_t struct_name = namespace_name(info->env, info->env->namespace, Texts(name, "$$struct")); + Text_t type_name = namespace_name(info->env, info->env->namespace, Texts(name, "$$type")); + *info->header = Texts(*info->header, "typedef struct ", struct_name, " ", type_name, ";\n"); + + for (tag_ast_t *tag = enum_->tags; tag; tag = tag->next) { + if (!tag->fields) continue; + Text_t tag_struct = + namespace_name(info->env, info->env->namespace, Texts(name, "$", tag->name, "$$struct")); + Text_t tag_type = + namespace_name(info->env, info->env->namespace, Texts(name, "$", tag->name, "$$type")); + *info->header = Texts(*info->header, "typedef struct ", tag_struct, " ", tag_type, ";\n"); + } + } else { + Text_t enum_name = namespace_name(info->env, info->env->namespace, Texts(name, "$$enum")); + Text_t type_name = namespace_name(info->env, info->env->namespace, Texts(name, "$$type")); + *info->header = Texts(*info->header, "typedef enum ", enum_name, " ", type_name, ";\n"); + } + + *info->header = Texts(*info->header, compile_enum_header(info->env, name, enum_->tags)); + } +} + public Text_t compile_file_header(env_t *env, Path_t header_path, ast_t *ast) { Text_t header = @@ -155,6 +190,9 @@ Text_t compile_file_header(env_t *env, Path_t header_path, ast_t *ast) { compile_typedef_info_t info = {.env = env, .header = &header, .header_path = header_path}; visit_topologically(Match(ast, Block)->statements, (Closure_t){.fn = (void *)_make_typedefs, &info}); + + type_ast_visit(ast, add_type_headers, &info); + visit_topologically(Match(ast, Block)->statements, (Closure_t){.fn = (void *)_define_types_and_funcs, &info}); header = Texts(header, "void ", namespace_name(env, env->namespace, Text("$initialize")), "(void);\n"); @@ -211,7 +249,8 @@ Text_t compile_statement_type_header(env_t *env, Path_t header_path, ast_t *ast) return compile_struct_header(env, ast); } case EnumDef: { - return compile_enum_header(env, ast); + DeclareMatch(def, ast, EnumDef); + return compile_enum_header(env, def->name, def->tags); } case LangDef: { DeclareMatch(def, ast, LangDef); |
