Improve codegen for optional struct types
This commit is contained in:
parent
d0efc4ae5a
commit
cab9ea0aad
@ -80,4 +80,14 @@ Text_t Type$as_text(const void *typeinfo, bool colorize, const TypeInfo_t *type)
|
||||
.tag=TypeInfoInfo, .TypeInfoInfo.type_str=typestr, \
|
||||
.metamethods={.serialize=cannot_serialize, .deserialize=cannot_deserialize, .as_text=Type$as_text}})
|
||||
|
||||
#define DEFINE_OPTIONAL_TYPE(t, unpadded_size, name) \
|
||||
typedef struct { \
|
||||
union { \
|
||||
t value; \
|
||||
struct { \
|
||||
char _padding[unpadded_size]; \
|
||||
Bool_t is_none:1; \
|
||||
}; \
|
||||
}; \
|
||||
} name
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|
||||
|
11
structs.c
11
structs.c
@ -61,15 +61,8 @@ CORD compile_struct_header(env_t *env, ast_t *ast)
|
||||
"struct ", full_name, "$$struct {\n",
|
||||
fields,
|
||||
"};\n",
|
||||
"typedef struct {\n",
|
||||
"union {\n",
|
||||
full_name, "$$type value;\n"
|
||||
"struct {\n"
|
||||
"char _padding[", heap_strf("%zu", unpadded_struct_size(t)), "];\n",
|
||||
"Bool_t is_none:1;\n"
|
||||
"};\n"
|
||||
"};\n"
|
||||
"} ", namespace_prefix(env, env->namespace), "$Optional", def->name, "$$type;\n"
|
||||
"DEFINE_OPTIONAL_TYPE(", full_name, "$$type, ", heap_strf("%zu", unpadded_struct_size(t)),
|
||||
", ", namespace_prefix(env, env->namespace), "$Optional", def->name, "$$type);\n"
|
||||
// Constructor macro:
|
||||
"#define ", namespace_prefix(env, env->namespace), def->name,
|
||||
"(...) ((", namespace_prefix(env, env->namespace), def->name, "$$type){__VA_ARGS__})\n"
|
||||
|
Loading…
Reference in New Issue
Block a user