Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 16, 2024
1 parent 5e1530f commit 5c7a6cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2561,20 +2561,26 @@ fn (mut g Gen) expr_with_var(expr ast.Expr, got_type_raw ast.Type, expected_type
styp := g.styp(expected_type)

g.writeln('${styp} ${tmp_var};')
if expr is ast.ArrayInit && expr.is_fixed && expr.exprs.len > 0
&& expr.exprs.any(it is ast.CallExpr) {
// [ foo(), foo() ]!
val_typ := g.table.value_type(got_type_raw)
for i, item_expr in expr.exprs {
g.write('memcpy(${tmp_var}[${i}], ')
g.expr(item_expr)
g.writeln(', sizeof(${g.styp(val_typ)}));')
}
} else {
// regular init
g.write('memcpy(&${tmp_var}, ')
g.expr(expr)
g.writeln(', sizeof(${styp}));')
g.write('memcpy(&${tmp_var}, ')
g.expr(expr)
g.writeln(', sizeof(${styp}));')
g.write(stmt_str)
return tmp_var
}

// expr_with_fixed_array generates code for fixed array initialization with expr which requires tmp var
fn (mut g Gen) expr_with_fixed_array(expr ast.ArrayInit, got_type_raw ast.Type, expected_type ast.Type) string {
stmt_str := g.go_before_last_stmt().trim_space()
g.empty_line = true
tmp_var := g.new_tmp_var()
styp := g.styp(expected_type)
g.writeln('${styp} ${tmp_var};')
// [ foo(), foo() ]!
val_typ := g.table.value_type(got_type_raw)
for i, item_expr in expr.exprs {
g.write('memcpy(${tmp_var}[${i}], ')
g.expr(item_expr)
g.writeln(', sizeof(${g.styp(val_typ)}));')
}
g.write(stmt_str)
return tmp_var
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
ast.ArrayInit {
if sfield.expr.has_index
|| (sfield.expr.exprs.len > 0 && sfield.expr.exprs.any(it is ast.CallExpr)) {
tmp_var := g.expr_with_var(sfield.expr, sfield.typ, sfield.expected_type)
tmp_var := g.expr_with_fixed_array(sfield.expr, sfield.typ, sfield.expected_type)
g.fixed_array_var_init(tmp_var, false, field_unwrap_sym.info.elem_type,
field_unwrap_sym.info.size)
} else {
Expand Down

0 comments on commit 5c7a6cf

Please sign in to comment.