1 // This file defines how to compile structs
6 #include "../environment.h"
8 #include "../stdlib/tables.h"
9 #include "../stdlib/text.h"
10 #include "../typecheck.h"
11 #include "compilation.h"
14 Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_t *fields, bool is_secret,
16 Text_t typeinfo_name = namespace_name(env, env->namespace, Texts(name, "$$info"));
17 Text_t type_code = Match(t, StructType)->external
19 : Texts("struct ", namespace_name(env, env->namespace, Texts(name, "$$struct")));
21 int64_t num_fields = 0;
22 for (arg_ast_t *f = fields; f; f = f->next)
24 const char *short_name = name;
25 if (strchr(short_name, '$')) short_name = strrchr(short_name, '$') + 1;
27 const char *metamethods = is_packed_data(t) ? "PackedData$metamethods" : "Struct$metamethods";
28 Text_t typeinfo = Texts(
29 "public const TypeInfo_t ", typeinfo_name, " = {.size=sizeof(", type_code, "), .align=__alignof__(", type_code,
34 ".tag=StructInfo, .StructInfo.name=\"",
35 short_name, "\"", is_secret ? Text(", .StructInfo.is_secret=true") : EMPTY_TEXT,
36 is_opaque ? Text(", .StructInfo.is_opaque=true") : EMPTY_TEXT, ", .StructInfo.num_fields=", num_fields);
38 typeinfo = Texts(typeinfo, ", .StructInfo.fields=(NamedType_t[", num_fields, "]){");
39 for (arg_ast_t *f = fields; f; f = f->next) {
40 type_t *field_type = get_arg_ast_type(env, f);
41 typeinfo = Texts(typeinfo, "{\"", f->name, "\", ", compile_type_info(field_type), "}");
42 if (f->next) typeinfo = Texts(typeinfo, ", ");
44 typeinfo = Texts(typeinfo, "}");
46 return Texts(typeinfo, "};\n");
50 Text_t compile_struct_header(env_t *env, ast_t *ast) {
51 DeclareMatch(def, ast, StructDef);
52 Text_t typeinfo_name = namespace_name(env, env->namespace, Texts(def->name, "$$info"));
53 Text_t type_code = def->external
54 ? Text$from_str(def->name)
55 : Texts("struct ", namespace_name(env, env->namespace, Texts(def->name, "$$struct")));
57 Text_t fields = EMPTY_TEXT;
58 for (arg_ast_t *field = def->fields; field; field = field->next) {
59 type_t *field_t = get_arg_ast_type(env, field);
60 type_t *check_for_opaque = non_optional(field_t);
61 if (check_for_opaque->tag == StructType && Match(check_for_opaque, StructType)->opaque) {
63 code_err(field->type, "This is an opaque type, so it can't be used as a struct field type");
64 else if (field->value)
65 code_err(field->value, "This is an opaque type, so it can't be used as a struct field type");
67 fields = Texts(fields, compile_declaration(field_t, valid_c_name(field->name)),
68 field_t->tag == BoolType ? Text(":1") : EMPTY_TEXT, ";\n");
70 Text_t struct_code = def->external ? EMPTY_TEXT : Texts(type_code, " {\n", fields, "};\n");
71 type_t *t = Table$str_get(*env->types, def->name);
73 Text_t typeinfo_code = Texts("extern const TypeInfo_t ", typeinfo_name, ";\n");
74 Text_t optional_code = EMPTY_TEXT;
76 optional_code = Texts("typedef struct {\n", compile_type(t),
80 namespace_name(env, env->namespace, Texts("$Optional", def->name, "$$type")), ";\n");
82 return Texts(struct_code, optional_code, typeinfo_code);
86 Text_t compile_empty_struct(type_t *t) {
87 DeclareMatch(struct_, t, StructType);
88 Text_t code = Texts("((", compile_type(t), "){");
89 for (arg_t *field = struct_->fields; field; field = field->next) {
91 field->default_val ? compile(struct_->env, field->default_val) : compile_empty(field->type);
92 if (empty_field.length == 0) return EMPTY_TEXT;
94 code = Texts(code, empty_field);
95 if (field->next) code = Texts(code, ", ");
97 return Texts(code, "})");
101 Text_t compile_struct_field_access(env_t *env, ast_t *ast) {
102 DeclareMatch(f, ast, FieldAccess);
103 type_t *fielded_t = get_type(env, f->fielded);
104 type_t *value_t = value_type(fielded_t);
105 for (arg_t *field = Match(value_t, StructType)->fields; field; field = field->next) {
106 if (streq(field->name, f->field)) {
107 if (fielded_t->tag == PointerType) {
108 Text_t fielded = compile_to_pointer_depth(env, f->fielded, 1, false);
109 return Texts("(", fielded, ")->", valid_c_name(f->field));
111 Text_t fielded = compile(env, f->fielded);
112 return Texts("(", fielded, ").", valid_c_name(f->field));
116 code_err(ast, "The field '", f->field, "' is not a valid field name of ", type_to_text(value_t));
120 Text_t compile_struct_literal(env_t *env, ast_t *ast, type_t *t, arg_ast_t *args) {
121 DeclareMatch(struct_, t, StructType);
122 if (struct_->opaque) code_err(ast, "This struct is opaque, so I don't know what's inside it!");
124 call_opts_t constructor_opts = {
126 .underscores = (env->current_type != NULL && type_eq(env->current_type, t)),
128 if (is_valid_call(env, struct_->fields, args, constructor_opts)) {
129 return Texts("((", compile_type(t), "){", compile_arguments(env, ast, struct_->fields, args), "})");
130 } else if (!constructor_opts.underscores
131 && is_valid_call(env, struct_->fields, args, (call_opts_t){.promotion = true, .underscores = true})) {
132 code_err(ast, "This constructor is passing private fields (those starting with underscores) as positional "
133 "arguments, which is not allowed. \n"
134 " If you need to pass these fields, use a keyword argument.");
136 code_err(ast, "I could not find a constructor matching these arguments for the struct ", type_to_text(t));