summaryrefslogtreecommitdiff
path: root/decls_cgen.c
diff options
context:
space:
mode:
Diffstat (limited to 'decls_cgen.c')
-rw-r--r--decls_cgen.c69
1 files changed, 2 insertions, 67 deletions
diff --git a/decls_cgen.c b/decls_cgen.c
index 6b52291..1df359d 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -1,75 +1,10 @@
static bool cgen_decls_stmt(CGenerator *g, Statement *s);
static bool cgen_decls_block(CGenerator *g, Block *b);
+static bool cgen_decls_decl(CGenerator *g, Declaration *d);
static bool cgen_decls_expr(CGenerator *g, Expression *e) {
- cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block);
+ cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block, cgen_decls_decl);
switch (e->kind) {
- case EXPR_CALL: {
- e->call.c.instance = 0;
- assert(e->call.fn->type.kind == TYPE_FN);
- FnType *fn_type = &e->call.fn->type.fn;
- if (fn_type->constness) {
- Value fval;
- /* e->call.fn had better be a compile-time constant if it has compile-time arguments */
- if (!eval_expr(g->evalr, e->call.fn, &fval))
- return false;
- FnExpr *f = fval.fn;
- /* directly calling a function; might need to generate a copy of this function */
-
- /* OPTIM should we really be constructing a tuple & type every time? */
- Value *compile_time_args = NULL;
- Type *tuple_types = NULL;
- size_t nparams = arr_len(fn_type->types)-1;
- Value *which_are_const_val = arr_add(&compile_time_args);
- U64 *which_are_const = &which_are_const_val->u64;
- Type *u64t = arr_add(&tuple_types);
- u64t->kind = TYPE_BUILTIN;
- u64t->flags = TYPE_IS_RESOLVED;
- u64t->builtin = BUILTIN_U64;
- *which_are_const = 0;
- int semi_const_arg_index = 0;
- for (size_t i = 0; i < nparams; i++) {
- Expression *arg = &e->call.arg_exprs[i];
- if (arg_is_const(arg, fn_type->constness[i])) {
- if (fn_type->constness[i] == CONSTNESS_SEMI) {
- if (semi_const_arg_index >= 64) {
- err_print(e->where, "You can't have more than 64 semi-constant parameters in a function at the moment.");
- return false;
- }
- *which_are_const |= ((U64)1) << semi_const_arg_index;
- semi_const_arg_index++;
- }
- assert(arg->kind == EXPR_VAL); /* should have been evaluated by types.c */
- *(Value *)arr_adda(&compile_time_args, g->allocr) = arg->val;
- *(Type *)arr_add(&tuple_types) = arg->type;
- i++;
- }
- }
- if (compile_time_args) {
- Value tuple;
- Type tuple_type;
- tuple.tuple = compile_time_args;
- tuple_type.kind = TYPE_TUPLE;
- tuple_type.flags = TYPE_IS_RESOLVED;
- tuple_type.tuple = tuple_types;
- if (!f->c.instances) {
- f->c.instances = allocr_calloc(g->allocr, 1, sizeof *f->c.instances);
- }
- /* lookup compile time arguments */
- I64 instance_number = (I64)f->c.instances->n + 1;
- bool already_generated_decl = val_hash_table_adda(g->allocr, f->c.instances, tuple, &tuple_type, &instance_number);
- if (!already_generated_decl) {
- /* generate a copy of this function */
- if (!cgen_fn_header(g, f, e->where, instance_number, *which_are_const))
- return false;
- cgen_write(g, ";");
- cgen_nl(g);
- }
- arr_clear(&tuple_types);
- e->call.c.instance = (U32)instance_number;
- }
- }
- } break;
case EXPR_FN:
e->fn.c.name = NULL;
if (!e->fn.c.id)