diff options
-rw-r--r-- | cgen.c | 2 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | test.toc | 7 | ||||
-rw-r--r-- | toc.c | 2 | ||||
-rw-r--r-- | typedefs_cgen.c | 67 |
5 files changed, 32 insertions, 48 deletions
@@ -1987,7 +1987,7 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) { "static void free_(void *data) { extern void free(void *data); free(data); }\n" /* don't introduce free to global namespace */ "static void *e__calloc(size_t n, size_t sz) { extern void *calloc(size_t n, size_t size); extern void abort(void); void *ret = calloc(n, sz); if (!ret) { fprintf(stderr, \"Out of memory.\\n\"); abort(); } return ret; }\n\n\n"); - if (!typedefs_file(g, f)) + if (!cgen_sdecls_file(g, f)) return false; if (!cgen_decls_file(g, f)) return false; @@ -7,7 +7,7 @@ /* NOTE: Structure of the toc compiler: - tokenizer => parser => typing (types.c) => typdefs_cgen, decls_cgen, cgen + tokenizer => parser => typing (types.c) => cgen (lexing) toc tries to continue even after the first error. @@ -12,6 +12,13 @@ putf ::= fn(x: float) { // point ::= pkg "point"; Foo ::= struct { x, y : int; }; +f ::= fn(t :: Type) int { + 5 as t +} +; main ::= fn() { + i ::= int; + puti(f(i)); + }; @@ -81,7 +81,7 @@ static inline bool type_is_slicechar(Type *t) { #include "package.c" #include "types.c" static bool cgen_decls_file(CGenerator *g, ParsedFile *f); -static bool typedefs_file(CGenerator *g, ParsedFile *f); +static bool cgen_sdecls_file(CGenerator *g, ParsedFile *f); #include "cgen.c" #include "typedefs_cgen.c" #include "decls_cgen.c" diff --git a/typedefs_cgen.c b/typedefs_cgen.c index 814eb4a..1b333d6 100644 --- a/typedefs_cgen.c +++ b/typedefs_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 bool typedefs_stmt(CGenerator *g, Statement *s); -static bool typedefs_decl(CGenerator *g, Declaration *d); -static bool typedefs_expr(CGenerator *g, Expression *e); +static bool cgen_sdecls_stmt(CGenerator *g, Statement *s); +static bool cgen_sdecls_decl(CGenerator *g, Declaration *d); +static bool cgen_sdecls_expr(CGenerator *g, Expression *e); /* i is the name for this type, NULL if not available */ /* ALWAYS RETURNS TRUE. it just returns a bool for cgen_recurse_into_type to work */ -static bool typedefs_type(CGenerator *g, Type *type) { +static bool cgen_sdecls_type(CGenerator *g, Type *type) { if (!(type->flags & TYPE_IS_RESOLVED)) /* non-instance constant fn parameter type */ return true; if (type->kind == TYPE_STRUCT) { @@ -32,105 +32,82 @@ static bool typedefs_type(CGenerator *g, Type *type) { sdef->flags |= STRUCT_DEF_CGEN_DECLARED; } } - cgen_recurse_subtypes(typedefs_type, g, type); + cgen_recurse_subtypes(cgen_sdecls_type, g, type); return true; } -static bool typedefs_block(CGenerator *g, Block *b) { +static bool cgen_sdecls_block(CGenerator *g, Block *b) { Block *prev = g->block; if (!cgen_block_enter(g, b)) return false; arr_foreach(b->stmts, Statement, s) - if (!typedefs_stmt(g, s)) + if (!cgen_sdecls_stmt(g, s)) return false; - if (b->ret_expr && !typedefs_expr(g, b->ret_expr)) + if (b->ret_expr && !cgen_sdecls_expr(g, b->ret_expr)) return false; cgen_block_exit(g, prev); return true; } -static bool typedefs_expr(CGenerator *g, Expression *e) { - cgen_recurse_subexprs(g, e, typedefs_expr, typedefs_block, typedefs_decl); +static bool cgen_sdecls_expr(CGenerator *g, Expression *e) { + cgen_recurse_subexprs(g, e, cgen_sdecls_expr, cgen_sdecls_block, cgen_sdecls_decl); if (e->kind == EXPR_CAST) { - typedefs_type(g, &e->cast.type); + cgen_sdecls_type(g, &e->cast.type); } if (e->kind == EXPR_FN) { /* needs to go before decls_cgen.c... */ e->fn->c.id = ++g->ident_counter; } if (e->kind == EXPR_TYPE) { - if (!typedefs_type(g, &e->typeval)) + if (!cgen_sdecls_type(g, &e->typeval)) return false; } return true; } -static bool typedefs_decl(CGenerator *g, Declaration *d) { - typedefs_type(g, &d->type); +static bool cgen_sdecls_decl(CGenerator *g, Declaration *d) { + cgen_sdecls_type(g, &d->type); if (cgen_fn_is_direct(g, d)) { d->expr.fn->c.name = d->idents[0]; } for (int idx = 0; idx < (int)arr_len(d->idents); ++idx) { - Identifier i = d->idents[idx]; Type *type = decl_type_at_index(d, idx); Value *val = decl_val_at_index(d, idx); if (type_is_builtin(type, BUILTIN_TYPE)) { - /* generate typedef */ - if (!typedefs_type(g, val->type)) + if (!cgen_sdecls_type(g, val->type)) return false; - - if (val->type->kind != TYPE_STRUCT) { - IdentID id = 0; - if (g->block != NULL || g->fn != NULL) - id = ++g->ident_counter; - if (!type_is_compileonly(val->type)) { - cgen_write(g, "typedef "); - if (!cgen_type_pre(g, val->type, d->where)) return false; - cgen_write(g, " "); - if (id) { - cgen_ident_id(g, id); - } else { - cgen_ident(g, i); - } - if (val->type->kind != TYPE_STRUCT) { - if (!cgen_type_post(g, val->type, d->where)) return false; - } - cgen_write(g, ";"); - } - } - cgen_nl(g); } } if (d->flags & DECL_HAS_EXPR) { - if (!typedefs_expr(g, &d->expr)) + if (!cgen_sdecls_expr(g, &d->expr)) return false; } return true; } -static bool typedefs_stmt(CGenerator *g, Statement *s) { +static bool cgen_sdecls_stmt(CGenerator *g, Statement *s) { switch (s->kind) { case STMT_DECL: - if (!typedefs_decl(g, &s->decl)) + if (!cgen_sdecls_decl(g, &s->decl)) return false; break; case STMT_EXPR: - if (!typedefs_expr(g, &s->expr)) + if (!cgen_sdecls_expr(g, &s->expr)) return false; break; case STMT_RET: if (s->ret.flags & RET_HAS_EXPR) - if (!typedefs_expr(g, &s->ret.expr)) + if (!cgen_sdecls_expr(g, &s->ret.expr)) return false; break; } return true; } -static bool typedefs_file(CGenerator *g, ParsedFile *f) { +static bool cgen_sdecls_file(CGenerator *g, ParsedFile *f) { arr_foreach(f->stmts, Statement, s) { - if (!typedefs_stmt(g, s)) + if (!cgen_sdecls_stmt(g, s)) return false; } return true; |