diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 15:10:41 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 15:10:41 -0400 |
| commit | 634d1ad756fca46e1e8aec93a265998232dd58a6 (patch) | |
| tree | be05e440db02d4c76890026982aa6978f212f9bd /src/compile | |
| parent | e30c0f06d55cd7b27c5d012a96989920f586e4a1 (diff) | |
Move structs/enums into the right folder
Diffstat (limited to 'src/compile')
| -rw-r--r-- | src/compile/enums.c | 143 | ||||
| -rw-r--r-- | src/compile/enums.h | 13 | ||||
| -rw-r--r-- | src/compile/structs.c | 80 | ||||
| -rw-r--r-- | src/compile/structs.h | 12 |
4 files changed, 248 insertions, 0 deletions
diff --git a/src/compile/enums.c b/src/compile/enums.c new file mode 100644 index 00000000..3abe0307 --- /dev/null +++ b/src/compile/enums.c @@ -0,0 +1,143 @@ +// Logic for compiling tagged unions (enums) +#include <gc.h> +#include <stdio.h> + +#include "../ast.h" +#include "../compile.h" +#include "../environment.h" +#include "../naming.h" +#include "../stdlib/tables.h" +#include "../stdlib/text.h" +#include "../typecheck.h" +#include "structs.h" + +Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { + DeclareMatch(def, ast, EnumDef); + + // Compile member types and constructors: + Text_t member_typeinfos = EMPTY_TEXT; + for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + if (!tag->fields) continue; + + const char *tag_name = String(def->name, "$", tag->name); + type_t *tag_type = Table$str_get(*env->types, tag_name); + assert(tag_type && tag_type->tag == StructType); + member_typeinfos = + Texts(member_typeinfos, compile_struct_typeinfo(env, tag_type, tag_name, tag->fields, tag->secret, false)); + } + + int num_tags = 0; + for (tag_ast_t *t = def->tags; t; t = t->next) + num_tags += 1; + + type_t *t = Table$str_get(*env->types, def->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 typeinfo = Texts("public const TypeInfo_t ", info, " = {", String((int64_t)type_size(t)), "u, ", + String((int64_t)type_align(t)), "u, .metamethods=", metamethods, + ", {.tag=EnumInfo, .EnumInfo={.name=\"", def->name, + "\", " + ".num_tags=", + String((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); + 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), "}, "); + else typeinfo = Texts(typeinfo, "{\"", tag->name, "\"}, "); + } + typeinfo = Texts(typeinfo, "}}}};\n"); + return Texts(member_typeinfos, typeinfo); +} + +Text_t compile_enum_constructors(env_t *env, ast_t *ast) { + DeclareMatch(def, ast, EnumDef); + Text_t constructors = EMPTY_TEXT; + for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + if (!tag->fields) continue; + + Text_t arg_sig = EMPTY_TEXT; + for (arg_ast_t *field = tag->fields; field; field = field->next) { + type_t *field_t = get_arg_ast_type(env, field); + arg_sig = Texts(arg_sig, compile_declaration(field_t, Texts("$", field->name))); + 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 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) { + constructor_impl = Texts(constructor_impl, "$", field->name); + if (field->next) constructor_impl = Texts(constructor_impl, ", "); + } + constructor_impl = Texts(constructor_impl, "}}; }\n"); + constructors = Texts(constructors, constructor_impl); + } + return constructors; +} + +Text_t compile_enum_header(env_t *env, ast_t *ast) { + DeclareMatch(def, ast, EnumDef); + 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 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)); + 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); + } + enum_tags = Texts(enum_tags, " }"); + + 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")); + 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 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) { + 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)); + all_defs = Texts(all_defs, field_def); + Text_t tag_type = namespace_name(env, env->namespace, Texts(def->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")); + all_defs = Texts(all_defs, "extern const TypeInfo_t ", info, ";\n"); + for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { + if (!tag->fields) continue; + + Text_t arg_sig = EMPTY_TEXT; + for (arg_ast_t *field = tag->fields; field; field = field->next) { + type_t *field_t = get_arg_ast_type(env, field); + arg_sig = Texts(arg_sig, compile_declaration(field_t, Texts("$", field->name))); + 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 constructor_def = Texts(enum_type, " ", tagged_name, "(", arg_sig, ");\n"); + all_defs = Texts(all_defs, constructor_def); + } + return all_defs; +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/compile/enums.h b/src/compile/enums.h new file mode 100644 index 00000000..6a42bece --- /dev/null +++ b/src/compile/enums.h @@ -0,0 +1,13 @@ +#pragma once + +// Compilation of tagged unions (enums) + +#include "../ast.h" +#include "../environment.h" +#include "../stdlib/datatypes.h" + +Text_t compile_enum_typeinfo(env_t *env, ast_t *ast); +Text_t compile_enum_constructors(env_t *env, ast_t *ast); +Text_t compile_enum_header(env_t *env, ast_t *ast); + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/compile/structs.c b/src/compile/structs.c new file mode 100644 index 00000000..d4bb346c --- /dev/null +++ b/src/compile/structs.c @@ -0,0 +1,80 @@ +// Logic for compiling new struct types defined in code +#include <gc.h> + +#include "../ast.h" +#include "../compile.h" +#include "../environment.h" +#include "../naming.h" +#include "../stdlib/tables.h" +#include "../stdlib/text.h" +#include "../typecheck.h" + +Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_t *fields, bool is_secret, + bool is_opaque) { + Text_t typeinfo_name = namespace_name(env, env->namespace, Texts(name, "$$info")); + Text_t type_code = Match(t, StructType)->external + ? Text$from_str(name) + : Texts("struct ", namespace_name(env, env->namespace, Texts(name, "$$struct"))); + + int num_fields = 0; + for (arg_ast_t *f = fields; f; f = f->next) + num_fields += 1; + const char *short_name = name; + if (strchr(short_name, '$')) short_name = strrchr(short_name, '$') + 1; + + const char *metamethods = is_packed_data(t) ? "PackedData$metamethods" : "Struct$metamethods"; + Text_t typeinfo = Texts( + "public const TypeInfo_t ", typeinfo_name, " = {.size=sizeof(", type_code, "), .align=__alignof__(", type_code, + "), " + ".metamethods=", + metamethods, + ", " + ".tag=StructInfo, .StructInfo.name=\"", + short_name, "\"", is_secret ? Text(", .StructInfo.is_secret=true") : EMPTY_TEXT, + is_opaque ? Text(", .StructInfo.is_opaque=true") : EMPTY_TEXT, ", .StructInfo.num_fields=", String(num_fields)); + if (fields) { + typeinfo = Texts(typeinfo, ", .StructInfo.fields=(NamedType_t[", String(num_fields), "]){"); + for (arg_ast_t *f = fields; f; f = f->next) { + type_t *field_type = get_arg_ast_type(env, f); + typeinfo = Texts(typeinfo, "{\"", f->name, "\", ", compile_type_info(field_type), "}"); + if (f->next) typeinfo = Texts(typeinfo, ", "); + } + typeinfo = Texts(typeinfo, "}"); + } + return Texts(typeinfo, "};\n"); +} + +Text_t compile_struct_header(env_t *env, ast_t *ast) { + DeclareMatch(def, ast, StructDef); + Text_t typeinfo_name = namespace_name(env, env->namespace, Texts(def->name, "$$info")); + Text_t type_code = def->external + ? Text$from_str(def->name) + : Texts("struct ", namespace_name(env, env->namespace, Texts(def->name, "$$struct"))); + + Text_t fields = EMPTY_TEXT; + for (arg_ast_t *field = def->fields; field; field = field->next) { + type_t *field_t = get_arg_ast_type(env, field); + type_t *check_for_opaque = non_optional(field_t); + if (check_for_opaque->tag == StructType && Match(check_for_opaque, StructType)->opaque) { + if (field->type) + code_err(field->type, "This is an opaque type, so it can't be used as a struct field type"); + else if (field->value) + code_err(field->value, "This is an opaque type, so it can't be used as a struct field type"); + } + fields = Texts(fields, compile_declaration(field_t, valid_c_name(field->name)), + field_t->tag == BoolType ? Text(":1") : EMPTY_TEXT, ";\n"); + } + Text_t struct_code = def->external ? EMPTY_TEXT : Texts(type_code, " {\n", fields, "};\n"); + type_t *t = Table$str_get(*env->types, def->name); + + Text_t unpadded_size = + def->opaque ? Texts("sizeof(", type_code, ")") : Text$from_str(String((int64_t)unpadded_struct_size(t))); + Text_t typeinfo_code = Texts("extern const TypeInfo_t ", typeinfo_name, ";\n"); + Text_t optional_code = EMPTY_TEXT; + if (!def->opaque) { + optional_code = Texts("DEFINE_OPTIONAL_TYPE(", compile_type(t), ", ", unpadded_size, ", ", + namespace_name(env, env->namespace, Texts("$Optional", def->name, "$$type")), ");\n"); + } + return Texts(struct_code, optional_code, typeinfo_code); +} +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/compile/structs.h b/src/compile/structs.h new file mode 100644 index 00000000..f24ed15a --- /dev/null +++ b/src/compile/structs.h @@ -0,0 +1,12 @@ +#pragma once + +// Compilation of user-defined structs + +#include "../ast.h" +#include "../environment.h" + +Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_t *fields, bool is_secret, + bool is_opaque); +Text_t compile_struct_header(env_t *env, ast_t *ast); + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
