summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c51
-rw-r--r--decls_cgen.c3
-rw-r--r--main.c2
-rw-r--r--types.c27
-rw-r--r--types.h1
5 files changed, 56 insertions, 28 deletions
diff --git a/cgen.c b/cgen.c
index f75cd2c..3bdf47f 100644
--- a/cgen.c
+++ b/cgen.c
@@ -121,7 +121,7 @@ static void cgen_defs_decl(CGenerator *g, Declaration *d);
break; \
case EXPR_FN: { \
FnExpr *fn = e->fn; \
- if (e->type.fn.constness) { \
+ if (fn->instances.data) { \
Instance **data = fn->instances.data; \
for (U64 i = 0; i < fn->instances.cap; ++i) { \
if (fn->instances.occupied[i]) { \
@@ -436,20 +436,32 @@ static void cgen_full_fn_name(CGenerator *g, FnExpr *f, U64 instance) {
static void cgen_fn_args(CGenerator *g, FnExpr *f, U64 instance, U64 which_are_const) {
(void)instance; /* not needed atm */
bool out_param = cgen_uses_ptr(&f->ret_type);
- bool any_params = false;
cgen_write(g, "(");
int semi_const_idx = 0;
- bool any_args = false;
+ bool any_params = false;
arr_foreach(f->params, Declaration, d) {
- if (!(d->flags & DECL_IS_CONST) && !((d->flags & DECL_SEMI_CONST)
- && (which_are_const & (((U64)1) << semi_const_idx++)))) {
- long idx = 0;
+ if (d->flags & DECL_IS_CONST) continue;
+ if (type_is_builtin(&d->type, BUILTIN_VARARGS)) {
+ int idx = 0;
+ arr_foreach(d->val.varargs, VarArg, varg) {
+ if (any_params)
+ cgen_write(g, ", ");
+ any_params = true;
+ cgen_type_pre(g, varg->type);
+ cgen_write(g, " ");
+ cgen_ident_simple(g, d->idents[0]);
+ cgen_write(g, "%d_", idx);
+ cgen_type_post(g, varg->type);
+ ++idx;
+ }
+ } else if ((d->flags & DECL_SEMI_CONST)
+ && (which_are_const & (((U64)1) << semi_const_idx++))) {
+ int idx = 0;
arr_foreach(d->idents, Identifier, i) {
- if (any_args)
+ if (any_params)
cgen_write(g, ", ");
- any_args = true;
- Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx++] : &d->type;
any_params = true;
+ Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx++] : &d->type;
cgen_type_pre(g, type);
cgen_write(g, " ");
cgen_ident_simple(g, *i);
@@ -458,7 +470,6 @@ static void cgen_fn_args(CGenerator *g, FnExpr *f, U64 instance, U64 which_are_c
}
}
if (out_param) {
- any_args = true;
if (f->ret_type.kind == TYPE_TUPLE) {
/* multiple return variables */
for (size_t i = 0; i < arr_len(f->ret_type.tuple); ++i) {
@@ -476,8 +487,9 @@ static void cgen_fn_args(CGenerator *g, FnExpr *f, U64 instance, U64 which_are_c
cgen_write(g, " (*ret__)");
cgen_type_post(g, &f->ret_type);
}
+ any_params = true;
}
- if (!any_args)
+ if (!any_params)
cgen_write(g, "void");
cgen_write(g, ")");
}
@@ -1927,15 +1939,9 @@ static void cgen_stmt(CGenerator *g, Statement *s) {
}
}
-static void cgen_defs_fn(CGenerator *g, FnExpr *f, Type *t) {
- FnType *fn_type = &t->fn;
- bool any_const = false;
- if (fn_type->constness) {
- for (size_t i = 0; i < arr_len(fn_type->types)-1; ++i) {
- if (fn_type->constness[i] == CONSTNESS_YES)
- any_const = true;
- }
- HashTable *instances = &f->instances;
+static void cgen_defs_fn(CGenerator *g, FnExpr *f) {
+ HashTable *instances = &f->instances;
+ if (instances->data) {
/* generate each instance */
Instance **is = instances->data;
for (U64 i = 0; i < instances->cap; ++i) {
@@ -1944,15 +1950,14 @@ static void cgen_defs_fn(CGenerator *g, FnExpr *f, Type *t) {
cgen_fn(g, is[i]->fn, is[i]->c.id, is[i]->val.tuple);
}
}
- }
- if (!any_const) {
+ } else {
cgen_fn(g, f, 0, NULL);
}
}
static void cgen_defs_expr(CGenerator *g, Expression *e) {
if (e->kind == EXPR_FN) {
- cgen_defs_fn(g, e->fn, &e->type);
+ cgen_defs_fn(g, e->fn);
}
cgen_recurse_subexprs(g, e, cgen_defs_expr, cgen_defs_block, cgen_defs_decl);
}
diff --git a/decls_cgen.c b/decls_cgen.c
index 70e405c..bd97756 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -164,8 +164,7 @@ static void cgen_fn_decl(CGenerator *g, FnExpr *f, Type *t) {
cgen_nl(g);
return;
}
- FnType *fn_type = &t->fn;
- if (fn_type->constness) {
+ if (f->instances.data) {
cgen_decls_fn_instances(g, f);
} else {
cgen_single_fn_decl(g, f, 0, 0);
diff --git a/main.c b/main.c
index c75618e..4fe435d 100644
--- a/main.c
+++ b/main.c
@@ -10,6 +10,8 @@
TODO:
variadic fns
const varargs
+don't allow default varargs
+don't allow semiconst varargs
make sure you can't have a variadic function pointer
#foreign variadic fns
where
diff --git a/types.c b/types.c
index b62f539..ed5508b 100644
--- a/types.c
+++ b/types.c
@@ -1958,9 +1958,10 @@ static Status types_expr(Typer *tr, Expression *e) {
}
}
+ size_t nvarargs;
if (has_varargs) {
assert(fn_decl);
- size_t nvarargs = nargs - (size_t)order[nparams-1];
+ nvarargs = nargs - (size_t)order[nparams-1];
narg_exprs = nparams-1 + nvarargs;
} else {
narg_exprs = nparams;
@@ -1995,8 +1996,6 @@ static Status types_expr(Typer *tr, Expression *e) {
/* deal with varargs (put them at the end of arg_exprs) */
int idx = order[nparams-1];
assert(idx >= 0);
- Declaration *param = arr_last(fn_decl->params);
- param->expr.tuple = NULL;
for (; idx < (int)nargs; ++idx) {
arg_exprs[idx+(int)nparams-1] = args[idx].val;
}
@@ -2207,7 +2206,10 @@ static Status types_expr(Typer *tr, Expression *e) {
}
}
}
+
+
if (fn_type->constness || has_varargs) {
+
Type table_index_type = {0};
Value table_index = {0};
@@ -2293,6 +2295,25 @@ static Status types_expr(Typer *tr, Expression *e) {
arr_remove_lasta(&err_ctx->instance_stack, tr->allocr);
if (!success) return false;
arr_cleara(&table_index_type.tuple, tr->allocr);
+
+ if (has_varargs) {
+ /* set value of varargs param decl */
+ VarArg *varargs = NULL;
+ arr_set_lena(&varargs, nvarargs, tr->allocr);
+ Declaration *varargs_param = arr_last(fn_copy->params);
+ varargs_param->val.varargs = varargs;
+
+ for (int i = 0; i < (int)nvarargs; ++i) {
+ Expression *arg = &arg_exprs[i+order[nparams-1]];
+ VarArg *vararg = &varargs[i];
+
+ if (varargs_param->flags & DECL_IS_CONST)
+ vararg->val = arg->val;
+ vararg->type = &arg->type;
+
+ }
+
+ }
}
}
free(order);
diff --git a/types.h b/types.h
index 1d1cf4a..38614d1 100644
--- a/types.h
+++ b/types.h
@@ -704,6 +704,7 @@ typedef struct FnExpr {
HashTable instances; /* for fns with constant parameters. the key is a tuple where
the first element is a u64 value whose ith bit (1<<i) is 1
if the ith semi-constant parameter is constant.
+ cgen relies on this being here even for foreign fns.
*/
struct {
/* if name = NULL, this is an anonymous function, and id will be the ID of the fn. */