aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-03-11 13:44:02 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-03-11 13:44:02 -0400
commitcab9ea0aad4c7fb0fe512f4003a2c160cdd3ddef (patch)
tree3f596b231fc2a1afadfad41b02cdf2cecef910f3
parentd0efc4ae5aeb321128c210d762036d4a8ed2c9d1 (diff)
Improve codegen for optional struct types
-rw-r--r--stdlib/types.h10
-rw-r--r--structs.c11
2 files changed, 12 insertions, 9 deletions
diff --git a/stdlib/types.h b/stdlib/types.h
index 2c9abcfc..f74048cc 100644
--- a/stdlib/types.h
+++ b/stdlib/types.h
@@ -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
diff --git a/structs.c b/structs.c
index 080791e5..5b8aafd3 100644
--- a/structs.c
+++ b/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"