diff options
Diffstat (limited to 'src/compile/enums.c')
| -rw-r--r-- | src/compile/enums.c | 64 |
1 files changed, 30 insertions, 34 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); } |
