summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-19 12:53:47 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-19 12:53:47 +0000
commit639c6ac65a1bd44b2ba0725fe7016a4920bf0950 (patch)
tree47703f960633d3d4580022583134c28b96d5f36e /src/codegen
parent526b7748cafcb5a00f8e30df88661f6059d79843 (diff)
Iterables and iterators :D
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/codegen.c2
-rw-r--r--src/codegen/codegen.h1
-rw-r--r--src/codegen/codegen_decl.c38
-rw-r--r--src/codegen/codegen_main.c5
4 files changed, 43 insertions, 3 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c
index a371548..01c8204 100644
--- a/src/codegen/codegen.c
+++ b/src/codegen/codegen.c
@@ -1699,7 +1699,7 @@ void codegen_node_single(ParserContext *ctx, ASTNode *node, FILE *out)
{
// Cleanup attribute
ASTNode *def = find_struct_def(ctx, tname);
- if (def && def->type_info && def->type_info->has_drop)
+ if (def && def->type_info && def->type_info->traits.has_drop)
{
fprintf(out, "__attribute__((cleanup(%s__Drop_glue))) ", tname);
}
diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h
index c6e2836..f8fe318 100644
--- a/src/codegen/codegen.h
+++ b/src/codegen/codegen.h
@@ -29,6 +29,7 @@ void emit_func_signature(FILE *out, ASTNode *func, const char *name_override);
void emit_preamble(ParserContext *ctx, FILE *out);
void emit_includes_and_aliases(ASTNode *node, FILE *out);
void emit_type_aliases(ASTNode *node, FILE *out);
+void emit_global_aliases(ParserContext *ctx, FILE *out);
void emit_struct_defs(ParserContext *ctx, ASTNode *node, FILE *out);
void emit_trait_defs(ASTNode *node, FILE *out);
void emit_enum_protos(ASTNode *node, FILE *out);
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index 6f9a1cb..8ada7d6 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -225,6 +225,16 @@ void emit_type_aliases(ASTNode *node, FILE *out)
}
}
+void emit_global_aliases(ParserContext *ctx, FILE *out)
+{
+ TypeAlias *ta = ctx->type_aliases;
+ while (ta)
+ {
+ fprintf(out, "typedef %s %s;\n", ta->original_type, ta->alias);
+ ta = ta->next;
+ }
+}
+
// Emit enum constructor prototypes
void emit_enum_protos(ASTNode *node, FILE *out)
{
@@ -421,6 +431,11 @@ void emit_trait_defs(ASTNode *node, FILE *out)
{
if (node->type == NODE_TRAIT)
{
+ if (node->trait.generic_param_count > 0)
+ {
+ node = node->next;
+ continue;
+ }
fprintf(out, "typedef struct %s_VTable {\n", node->trait.name);
ASTNode *m = node->trait.methods;
while (m)
@@ -751,6 +766,29 @@ void emit_impl_vtables(ParserContext *ctx, FILE *out)
if (node && node->type == NODE_IMPL_TRAIT)
{
char *trait = node->impl_trait.trait_name;
+
+ // Filter generic traits (VTables for them are not emitted)
+ int is_generic_trait = 0;
+ StructRef *search = ctx->parsed_globals_list;
+ while (search)
+ {
+ if (search->node && search->node->type == NODE_TRAIT &&
+ strcmp(search->node->trait.name, trait) == 0)
+ {
+ if (search->node->trait.generic_param_count > 0)
+ {
+ is_generic_trait = 1;
+ }
+ break;
+ }
+ search = search->next;
+ }
+ if (is_generic_trait)
+ {
+ ref = ref->next;
+ continue;
+ }
+
char *strct = node->impl_trait.target_type;
// Filter templates
diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c
index d5d6bbc..7382827 100644
--- a/src/codegen/codegen_main.c
+++ b/src/codegen/codegen_main.c
@@ -334,6 +334,8 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
print_type_defs(ctx, out, sorted);
emit_enum_protos(sorted, out);
+ emit_global_aliases(ctx, out); // Emit ALL aliases (including imports)
+ emit_type_aliases(kids, out); // Emit local aliases (redundant but safe)
emit_trait_defs(kids, out);
// First pass: emit ONLY preprocessor directives before struct defs
@@ -382,8 +384,7 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
raw_iter = raw_iter->next;
}
- // Emit type aliases after struct defs (so aliased generic types exist)
- emit_type_aliases(kids, out);
+ // Emit type aliases was here (moved up)
ASTNode *merged_globals = NULL; // Head