diff options
Diffstat (limited to 'decls_cgen.c')
-rw-r--r-- | decls_cgen.c | 71 |
1 files changed, 32 insertions, 39 deletions
diff --git a/decls_cgen.c b/decls_cgen.c index 82bf56f..ca127db 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -47,7 +47,36 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { fn_exit(&e->fn); } } - } break; + } break; + case EXPR_TYPE: { + Type *type = &e->typeval; + if (type->kind == TYPE_STRUCT) { + StructDef *sdef = type->struc; + if (!(sdef->flags & STRUCT_DEF_CGENERATED)) { + /* generate struct definition */ + cgen_write(g, "struct "); + if (sdef->c.name) + cgen_ident(g, sdef->c.name); + else + cgen_ident_id(g, sdef->c.id); + cgen_write(g, "{"); + cgen_nl(g); + g->indent_lvl++; + arr_foreach(sdef->fields, Field, f) { + if (!cgen_type_pre(g, f->type, e->where)) return false; + cgen_write(g, " "); + cgen_ident(g, f->name); + if (!cgen_type_post(g, f->type, e->where)) return false; + cgen_write(g, ";"); + cgen_nl(g); + } + g->indent_lvl--; + cgen_write(g, "};"); + cgen_nl(g); + sdef->flags |= STRUCT_DEF_CGENERATED; + } + } + } break; default: break; } @@ -86,44 +115,8 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d) { } cgen_recurse_subexprs(g, (&d->expr), cgen_decls_expr, cgen_decls_block, cgen_decls_decl); } else if (d->flags & DECL_HAS_EXPR) { - if (d->flags & DECL_IS_CONST) { - for (size_t idx = 0; idx < arr_len(d->idents); idx++) { - Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx] : &d->type; - if (type->kind == TYPE_TYPE) { - Value *val = d->type.kind == TYPE_TUPLE ? &d->val.tuple[idx] : &d->val; - if (val->type->kind == TYPE_STRUCT) { - StructDef *sdef = val->type->struc; - if (!(sdef->flags & STRUCT_DEF_CGENERATED)) { - /* generate struct definition */ - cgen_write(g, "struct "); - if (sdef->c.name) - cgen_ident(g, sdef->c.name); - else - cgen_ident_id(g, sdef->c.id); - cgen_write(g, "{"); - cgen_nl(g); - g->indent_lvl++; - arr_foreach(sdef->fields, Field, f) { - if (!cgen_type_pre(g, f->type, d->where)) return false; - cgen_write(g, " "); - cgen_ident(g, f->name); - if (!cgen_type_post(g, f->type, d->where)) return false; - cgen_write(g, ";"); - cgen_nl(g); - } - g->indent_lvl--; - cgen_write(g, "};"); - cgen_nl(g); - sdef->flags |= STRUCT_DEF_CGENERATED; - } - } - } - } - } - if (!(d->flags & DECL_IS_CONST) || (d->expr.kind == EXPR_FN)) { - if (!cgen_decls_expr(g, &d->expr)) - return false; - } + if (!cgen_decls_expr(g, &d->expr)) + return false; } return true; } |