summaryrefslogtreecommitdiff
path: root/cgen.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-07-12 15:03:36 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-07-12 15:03:36 -0400
commit07982dd332c739a8135305d22994caaa2fe340ca (patch)
tree3cfab9779868d825f5c9c6f175c44c17e4a93c42 /cgen.c
parenta7d209081a9f59f9d347ab87f76d0b58dc7cc106 (diff)
fixed local #foreign functions
Diffstat (limited to 'cgen.c')
-rw-r--r--cgen.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/cgen.c b/cgen.c
index 16b8e9a..07ed12e 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1415,15 +1415,20 @@ static void cgen_fn(CGenerator *g, FnExpr *f) {
static void cgen_decl(CGenerator *g, Declaration *d) {
if (!g->block || (g->block->kind == BLOCK_NMS))
return; // already dealt with
- int has_expr = d->flags & DECL_HAS_EXPR;
- if (cgen_fn_is_direct(g, d))
- return; // dealt with in the loop that defines all the functions in cgen_file
- if (d->flags & DECL_FOUND_VAL) {
+ DeclFlags flags = d->flags;
+ DeclFlags has_expr = flags & DECL_HAS_EXPR,
+ is_const = flags & DECL_IS_CONST;
+
+ if (flags & DECL_FOUND_VAL) {
// declarations where we use a value
for (int idx = 0, nidents = (int)arr_len(d->idents); idx < nidents; ++idx) {
Identifier i = d->idents[idx];
if (ident_eq_str(i, "_")) continue;
Type *type = decl_type_at_index(d, idx);
+ if (type->kind == TYPE_FN) {
+ // we don't need to generate this, because we can just use the function's name
+ continue;
+ }
if (type_is_compileonly(&d->type)) {
continue;
}
@@ -1431,10 +1436,10 @@ static void cgen_decl(CGenerator *g, Declaration *d) {
if (has_expr) {
cgen_val_pre(g, val, type);
}
- if (d->flags & DECL_IS_CONST)
+ if (is_const)
cgen_write(g, "static ");
cgen_type_pre(g, type);
- cgen_write(g, " ");
+ cgen_write(g, is_const ? " const " : " ");
cgen_ident(g, i);
cgen_type_post(g, type);
if (has_expr) {
@@ -1467,7 +1472,7 @@ static void cgen_decl(CGenerator *g, Declaration *d) {
}
if (has_expr) {
Expression *expr = &d->expr;
- assert((g->block || g->fn) && !(d->flags & DECL_IS_CONST));
+ assert((g->block || g->fn) && !is_const);
if (expr->type.kind == TYPE_TUPLE) {
cgen_set_tuple(g, NULL, d->idents, NULL, expr);
} else {