diff options
Diffstat (limited to 'decls_cgen.c')
-rw-r--r-- | decls_cgen.c | 128 |
1 files changed, 45 insertions, 83 deletions
diff --git a/decls_cgen.c b/decls_cgen.c index 9776547..ebf6ed1 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -3,13 +3,13 @@ This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>. */ -static Status cgen_decls_stmt(CGenerator *g, Statement *s); -static Status cgen_decls_block(CGenerator *g, Block *b); -static Status cgen_decls_decl(CGenerator *g, Declaration *d); +static void cgen_decls_stmt(CGenerator *g, Statement *s); +static void cgen_decls_block(CGenerator *g, Block *b); +static void cgen_decls_decl(CGenerator *g, Declaration *d); -static Status cgen_decls_type(CGenerator *g, Type *type) { +static void cgen_decls_type(CGenerator *g, Type *type) { if (!(type->flags & TYPE_IS_RESOLVED)) /* non-instance constant fn parameter type */ - return true; + return; if (type->kind == TYPE_STRUCT) { StructDef *sdef = type->struc; if (!(sdef->flags & STRUCT_DEF_CGEN_DEFINED)) { @@ -20,10 +20,10 @@ static Status cgen_decls_type(CGenerator *g, Type *type) { cgen_nl(g); ++g->indent_lvl; arr_foreach(sdef->fields, Field, f) { - if (!cgen_type_pre(g, &f->type, sdef->where)) return false; + cgen_type_pre(g, &f->type, sdef->where); cgen_write(g, " "); cgen_ident_simple(g, f->name); - if (!cgen_type_post(g, &f->type, sdef->where)) return false; + cgen_type_post(g, &f->type, sdef->where); cgen_write(g, ";"); cgen_nl(g); } @@ -34,51 +34,43 @@ static Status cgen_decls_type(CGenerator *g, Type *type) { } } cgen_recurse_subtypes(cgen_decls_type, g, type); - return true; } -static Status cgen_single_fn_decl(CGenerator *g, FnExpr *f, U64 instance, U64 which_are_const) { +static void cgen_single_fn_decl(CGenerator *g, FnExpr *f, U64 instance, U64 which_are_const) { if (cgen_should_gen_fn(f)) { - if (!cgen_fn_header(g, f, instance, which_are_const)) - return false; + cgen_fn_header(g, f, instance, which_are_const); cgen_write(g, ";"); cgen_nl(g); } - return true; } -static Status cgen_decls_fn_instances(CGenerator *g, FnExpr *f) { +static void cgen_decls_fn_instances(CGenerator *g, FnExpr *f) { Instance **data = f->instances.data; for (U64 i = 0; i < f->instances.cap; ++i) { if (f->instances.occupied[i]) { if (cgen_should_gen_fn((*data)->fn)) { (*data)->fn->c.name = f->c.name; (*data)->fn->c.id = f->c.id; - if (!cgen_single_fn_decl(g, (*data)->fn, (*data)->c.id, (*data)->val.tuple[0].u64)) - return false; + cgen_single_fn_decl(g, (*data)->fn, (*data)->c.id, (*data)->val.tuple[0].u64); cgen_write(g, ";"); cgen_nl(g); } } ++data; } - return true; } -static Status cgen_fn_decl(CGenerator *g, FnExpr *f, Type *t) { +static void cgen_fn_decl(CGenerator *g, FnExpr *f, Type *t) { FnType *fn_type = &t->fn; if (fn_type->constness) { - if (!cgen_decls_fn_instances(g, f)) - return false; + cgen_decls_fn_instances(g, f); } else { - if (!cgen_single_fn_decl(g, f, 0, 0)) - return false; + cgen_single_fn_decl(g, f, 0, 0); } - return true; } -static Status cgen_decls_expr(CGenerator *g, Expression *e) { +static void cgen_decls_expr(CGenerator *g, Expression *e) { assert(e->flags & EXPR_FOUND_TYPE); cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block, cgen_decls_decl); switch (e->kind) { @@ -87,17 +79,14 @@ static Status cgen_decls_expr(CGenerator *g, Expression *e) { f->c.name = NULL; if (!f->c.id) f->c.id = ++g->ident_counter; - if (!cgen_fn_decl(g, e->fn, &e->type)) - return false; + cgen_fn_decl(g, e->fn, &e->type); } break; case EXPR_TYPE: { Type *type = &e->typeval; - if (!cgen_decls_type(g, type)) - return false; + cgen_decls_type(g, type); } break; case EXPR_CAST: - if (!cgen_decls_type(g, &e->cast.type)) - return false; + cgen_decls_type(g, &e->cast.type); break; case EXPR_BINARY_OP: { Type *lhs_type = &e->binary.lhs->type; @@ -107,23 +96,18 @@ static Status cgen_decls_expr(CGenerator *g, Expression *e) { default: break; } - - return true; } -static Status cgen_decls_block(CGenerator *g, Block *b) { +static void cgen_decls_block(CGenerator *g, Block *b) { Block *prev_block = g->block; g->block = b; arr_foreach(b->stmts, Statement, s) - if (!cgen_decls_stmt(g, s)) - return false; - if (b->ret_expr && !cgen_decls_expr(g, b->ret_expr)) - return false; + cgen_decls_stmt(g, s); + if (b->ret_expr) cgen_decls_expr(g, b->ret_expr); g->block = prev_block; - return true; } -static Status cgen_decls_decl(CGenerator *g, Declaration *d) { +static void cgen_decls_decl(CGenerator *g, Declaration *d) { if (d->flags & DECL_FOREIGN) { cgen_write(g, "extern "); if ((d->flags & DECL_IS_CONST) && (d->type.kind == TYPE_FN) && arr_len(d->idents) == 1) { @@ -132,60 +116,49 @@ static Status cgen_decls_decl(CGenerator *g, Declaration *d) { const char *foreign_name = (d->flags & DECL_FOUND_VAL) ? d->val.fn->foreign.name : d->foreign.name_str; - if (!cgen_type_pre(g, &fn_types[0], d->where)) - return false; + cgen_type_pre(g, &fn_types[0], d->where); cgen_write(g, " %s", foreign_name); cgen_write(g, "("); arr_foreach(fn_types, Type, t) { if (t == fn_types) continue; if (t != fn_types+1) cgen_write(g, ", "); - if (!cgen_type_pre(g, t, d->where)) - return false; - if (!cgen_type_post(g, t, d->where)) - return false; + cgen_type_pre(g, t, d->where); + cgen_type_post(g, t, d->where); } cgen_write(g, ")"); - if (!cgen_type_post(g, &fn_types[0], d->where)) - return false; + cgen_type_post(g, &fn_types[0], d->where); cgen_write(g, ";"); if (!ident_eq_str(d->idents[0], foreign_name)) { cgen_write(g, "static "); - if (!cgen_type_pre(g, &d->type, d->where)) - return false; + cgen_type_pre(g, &d->type, d->where); cgen_write(g, " const "); cgen_ident(g, d->idents[0]); - if (!cgen_type_post(g, &d->type, d->where)) - return false; + cgen_type_post(g, &d->type, d->where); cgen_write(g, " = %s;", foreign_name); } cgen_nl(g); if (d->flags & DECL_FOUND_VAL) d->val.fn->c.name = d->idents[0]; - return true; + return; } else { /* foreign non-function */ const char *foreign_name = d->foreign.name_str; - if (!cgen_type_pre(g, &d->type, d->where)) - return false; + cgen_type_pre(g, &d->type, d->where); cgen_write(g, " %s", foreign_name); - if (!cgen_type_post(g, &d->type, d->where)) - return false; + cgen_type_post(g, &d->type, d->where); cgen_write(g, ";"); cgen_nl(g); } - return true; + return; } - if (!cgen_decls_type(g, &d->type)) - return false; + cgen_decls_type(g, &d->type); if (cgen_fn_is_direct(g, d)) { - if (!cgen_fn_decl(g, d->expr.fn, &d->expr.type)) - return false; + cgen_fn_decl(g, d->expr.fn, &d->expr.type); cgen_recurse_subexprs(g, (&d->expr), cgen_decls_expr, cgen_decls_block, cgen_decls_decl); } else { if (d->flags & DECL_HAS_EXPR) { - if (!cgen_decls_expr(g, &d->expr)) - return false; + cgen_decls_expr(g, &d->expr); } if ((g->block == NULL || (g->block->flags & BLOCK_IS_NMS)) && g->fn == NULL) { for (int i = 0, n_idents = (int)arr_len(d->idents); i < n_idents; ++i) { @@ -194,17 +167,14 @@ static Status cgen_decls_decl(CGenerator *g, Declaration *d) { if (!type_is_compileonly(type)) { if (!(d->flags & DECL_EXPORT)) cgen_write(g, "static "); - if (!cgen_type_pre(g, type, d->where)) - return false; + cgen_type_pre(g, type, d->where); cgen_write(g, " "); cgen_ident(g, ident); - if (!cgen_type_post(g, type, d->where)) - return false; + cgen_type_post(g, type, d->where); if (d->flags & DECL_HAS_EXPR) { Value *val = decl_val_at_index(d, i); cgen_write(g, " = "); - if (!cgen_val(g, *val, type, d->where)) - return false; + cgen_val(g, *val, type, d->where); } else { cgen_write(g, " = "); cgen_zero_value(g, type); @@ -215,38 +185,30 @@ static Status cgen_decls_decl(CGenerator *g, Declaration *d) { } } } - return true; } -static Status cgen_decls_stmt(CGenerator *g, Statement *s) { +static void cgen_decls_stmt(CGenerator *g, Statement *s) { switch (s->kind) { case STMT_DECL: - if (!cgen_decls_decl(g, s->decl)) - return false; + cgen_decls_decl(g, s->decl); break; case STMT_EXPR: - if (!cgen_decls_expr(g, &s->expr)) - return false; + cgen_decls_expr(g, &s->expr); break; case STMT_RET: if (s->ret.flags & RET_HAS_EXPR) - if (!cgen_decls_expr(g, &s->ret.expr)) - return false; + cgen_decls_expr(g, &s->ret.expr); break; case STMT_INCLUDE: arr_foreach(s->inc.stmts, Statement, sub) - if (!cgen_decls_stmt(g, sub)) - return false; + cgen_decls_stmt(g, sub); break; } - return true; } -static Status cgen_decls_file(CGenerator *g, ParsedFile *f) { +static void cgen_decls_file(CGenerator *g, ParsedFile *f) { cgen_write(g, "/* declarations */\n"); arr_foreach(f->stmts, Statement, s) { - if (!cgen_decls_stmt(g, s)) - return false; + cgen_decls_stmt(g, s); } - return true; } |