cgen: simplify int(bool_var) casts; support [c_extern] tag for global declarations

master
Delyan Angelov 2022-06-03 18:41:12 +03:00
parent daa94de93f
commit dbaecdc058
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 5 additions and 3 deletions

View File

@ -3961,9 +3961,9 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
g.expr(node.expr) g.expr(node.expr)
} else if node.expr_type == ast.bool_type && node.typ.is_int() { } else if node.expr_type == ast.bool_type && node.typ.is_int() {
styp := g.typ(node.typ) styp := g.typ(node.typ)
g.write('($styp[]){(') g.write('($styp)(')
g.expr(node.expr) g.expr(node.expr)
g.write(')?1:0}[0]') g.write(')')
} else { } else {
styp := g.typ(node.typ) styp := g.typ(node.typ)
if (g.pref.translated || g.file.is_translated) && sym.kind == .function { 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()` // should the global be initialized now, not later in `vinit()`
cinit := node.attrs.contains('cinit') cinit := node.attrs.contains('cinit')
cextern := node.attrs.contains('c_extern')
should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module) should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module)
|| (g.pref.build_mode == .build_module && g.module_built == node.mod) || (g.pref.build_mode == .build_module && g.module_built == node.mod)
mut attributes := '' 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') g.definitions.writeln('$fn_type_name = ${g.table.sym(field.typ).name}; // global2')
continue continue
} }
extern := if cextern { 'extern ' } else { '' }
modifier := if field.is_volatile { ' volatile ' } 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 field.has_expr || cinit {
if g.pref.translated { if g.pref.translated {
g.definitions.write_string(' = ${g.expr_string(field.expr)}') g.definitions.write_string(' = ${g.expr_string(field.expr)}')