From 1de39ba1ffdacf6841f925537b31b32a4101b632 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 14 Jan 2020 20:00:19 -0500 Subject: extern variable declarations --- decls_cgen.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'decls_cgen.c') diff --git a/decls_cgen.c b/decls_cgen.c index 32bc3f1..278971f 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -101,6 +101,37 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { case EXPR_CAST: if (!cgen_decls_type(g, &e->cast.type)) return false; + break; + case EXPR_BINARY_OP: { + Type *lhs_type = &e->binary.lhs->type; + if (lhs_type->kind == TYPE_PTR) + lhs_type = lhs_type->ptr; + if (e->binary.op == BINARY_DOT && type_is_builtin(lhs_type, BUILTIN_PKG)) { + assert(e->binary.lhs->kind == EXPR_VAL); + Identifier ident = e->binary.dot.pkg_ident; + IdentDecl *idecl = ident_decl(ident); + assert(idecl); + + if (idecl->kind == IDECL_DECL) { + Declaration *d = idecl->decl; + if (((d->flags & DECL_FOUND_VAL) && e->type.kind == TYPE_FN) + || (d->expr.kind == EXPR_FN)) { + /* extern function declaration */ + break; + } + } + /* extern variable declaration */ + cgen_write(g, "extern "); + if (!cgen_type_pre(g, &e->type, e->where)) + return false; + cgen_write(g, " %s__", e->binary.lhs->val.pkg->c.prefix); + cgen_ident(g, ident); + if (!cgen_type_post(g, &e->type, e->where)) + return false; + cgen_write(g, ";"); + cgen_nl(g); + } + } break; default: break; } -- cgit v1.2.3