From dbaecdc058d654c2303c01e2ef70ddf66d14d077 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 3 Jun 2022 18:41:12 +0300 Subject: [PATCH] cgen: simplify int(bool_var) casts; support [c_extern] tag for global declarations --- vlib/v/gen/c/cgen.v | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f7a19c3391..2beaa1d251 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3961,9 +3961,9 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.expr(node.expr) } else if node.expr_type == ast.bool_type && node.typ.is_int() { styp := g.typ(node.typ) - g.write('($styp[]){(') + g.write('($styp)(') g.expr(node.expr) - g.write(')?1:0}[0]') + g.write(')') } else { styp := g.typ(node.typ) if (g.pref.translated || g.file.is_translated) && sym.kind == .function { @@ -4633,6 +4633,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { } // should the global be initialized now, not later in `vinit()` cinit := node.attrs.contains('cinit') + cextern := node.attrs.contains('c_extern') should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module) || (g.pref.build_mode == .build_module && g.module_built == node.mod) mut attributes := '' @@ -4656,8 +4657,9 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { g.definitions.writeln('$fn_type_name = ${g.table.sym(field.typ).name}; // global2') continue } + extern := if cextern { 'extern ' } else { '' } modifier := if field.is_volatile { ' volatile ' } else { '' } - g.definitions.write_string('$visibility_kw$modifier$styp $attributes $field.name') + g.definitions.write_string('$extern$visibility_kw$modifier$styp $attributes $field.name') if field.has_expr || cinit { if g.pref.translated { g.definitions.write_string(' = ${g.expr_string(field.expr)}')