summaryrefslogtreecommitdiff
path: root/cgen.c
diff options
context:
space:
mode:
Diffstat (limited to 'cgen.c')
-rw-r--r--cgen.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/cgen.c b/cgen.c
index 6c6f0b4..2dc187d 100644
--- a/cgen.c
+++ b/cgen.c
@@ -6,7 +6,6 @@ static void cgen_create(CGenerator *g, FILE *out, Identifiers *ids, Evaluator *e
g->evalr = ev;
g->will_indent = true;
g->indent_lvl = 0;
- g->anon_fns = NULL;
g->idents = ids;
g->allocr = allocr;
}
@@ -1121,9 +1120,23 @@ static bool cgen_expr(CGenerator *g, Expression *e) {
case EXPR_LITERAL_CHAR:
cgen_write(g, "((char)%d)", e->charl);
break;
- case EXPR_IDENT:
- cgen_ident(g, e->ident);
- break;
+ case EXPR_IDENT: {
+ bool handled = false;
+ if (e->type.kind == TYPE_FN) {
+ /* generate the right function name, because it might be anonymous */
+ IdentDecl *idecl = ident_decl(e->ident);
+ if (idecl && idecl->kind == IDECL_DECL) {
+ Declaration *d = idecl->decl;
+ Value fn_val = d->val;
+ FnExpr *fn = fn_val.fn;
+ cgen_fn_name(g, fn);
+ handled = true;
+ }
+ }
+ if (!handled) {
+ cgen_ident(g, e->ident);
+ }
+ } break;
case EXPR_BINARY_OP: {
const char *s = "";
bool handled = false;
@@ -1379,13 +1392,9 @@ static bool cgen_expr(CGenerator *g, Expression *e) {
case EXPR_TYPE:
assert(0);
break;
- case EXPR_FN: {
- if (g->block != NULL) {
- Expression **eptr = arr_add(&g->anon_fns);
- *eptr = e;
- }
+ case EXPR_FN:
cgen_fn_name(g, &e->fn);
- } break;
+ break;
case EXPR_SLICE:
cgen_ident_id(g, e->slice.c.id);
break;
@@ -1634,9 +1643,7 @@ static bool cgen_val(CGenerator *g, Value v, Type *t, Location where) {
static bool cgen_decl(CGenerator *g, Declaration *d) {
int has_expr = d->flags & DECL_HAS_EXPR;
bool is_tuple = d->type.kind == TYPE_TUPLE;
- if (cgen_fn_is_direct(g, d)) {
- /* definition already generated by cgen_defs_decl */
- } else if ((d->flags & DECL_IS_CONST) || g->block == NULL) {
+ if ((d->flags & DECL_IS_CONST) || g->block == NULL) {
/* declarations where we use a value */
for (size_t idx = 0; idx < arr_len(d->idents); idx++) {
Identifier i = d->idents[idx];
@@ -1666,6 +1673,9 @@ static bool cgen_decl(CGenerator *g, Declaration *d) {
cgen_nl(g);
}
continue;
+ } else if (type->kind == TYPE_FN && (d->flags & DECL_IS_CONST)) {
+ /* don't generate function pointer declaration for constant fns */
+ continue;
}
if (!cgen_val_pre(g, *val, type, d->where))
return false;
@@ -1881,11 +1891,6 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) {
if (!cgen_stmt(g, s))
return false;
}
- typedef Expression *ExprPtr;
- arr_foreach(g->anon_fns, ExprPtr, eptr) {
- Expression *e = *eptr;
- if (!cgen_fn(g, &e->fn, e->where, 0, NULL)) return false;
- }
return true;
}