Skip to content

Commit

Permalink
cgen: cleanup array.v (vlang#22723)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Nov 1, 2024
1 parent 2c887e2 commit fc6e481
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -1174,15 +1174,7 @@ fn (mut g Gen) gen_array_contains(left_type ast.Type, left ast.Expr, right_type
if g.table.sym(elem_typ).kind in [.interface, .sum_type] {
g.expr_with_cast(right, right_type, elem_typ)
} else if right is ast.ArrayInit {
if g.is_cc_msvc {
stmts := g.go_before_last_stmt().trim_space()
tmp_var := g.new_tmp_var()
g.write2('${g.styp(right_type)} ${tmp_var} = ${g.expr_string(right)};', stmts)
g.write(tmp_var)
} else {
g.write('(${g.styp(right.typ)})')
g.expr(right)
}
g.fixed_array_init_with_cast(right, right_type)
} else {
g.expr(right)
}
Expand Down Expand Up @@ -1329,16 +1321,7 @@ fn (mut g Gen) gen_array_index(node ast.CallExpr) {
if g.table.sym(elem_typ).kind in [.interface, .sum_type] {
g.expr_with_cast(node.args[0].expr, node.args[0].typ, elem_typ)
} else if node.args[0].expr is ast.ArrayInit {
if g.is_cc_msvc {
stmts := g.go_before_last_stmt().trim_space()
tmp_var := g.new_tmp_var()
g.write2('${g.styp(node.args[0].typ)} ${tmp_var} = ${g.expr_string(node.args[0].expr)};',
stmts)
g.write(tmp_var)
} else {
g.write('(${g.styp(node.args[0].typ)})')
g.expr(node.args[0].expr)
}
g.fixed_array_init_with_cast(node.args[0].expr, node.args[0].typ)
} else {
g.expr(node.args[0].expr)
}
Expand Down Expand Up @@ -1586,15 +1569,7 @@ fn (mut g Gen) write_prepared_tmp_value(tmp string, node &ast.CallExpr, tmp_styp
g.writeln('${left_styp} ${tmp}_orig;')
g.write('memcpy(&${tmp}_orig, &')
if node.left is ast.ArrayInit {
if g.is_cc_msvc {
stmts := g.go_before_last_stmt().trim_space()
tmp_var := g.new_tmp_var()
g.write2('${left_styp} ${tmp_var} = ${g.expr_string(node.left)};', stmts)
g.write(tmp_var)
} else {
g.write('(${left_styp})')
g.expr(node.left)
}
g.fixed_array_init_with_cast(node.left, node.left_type)
} else {
if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() {
g.write('*')
Expand Down Expand Up @@ -1634,6 +1609,20 @@ fn (mut g Gen) write_prepared_var(var_name string, elem_type ast.Type, inp_elem_
}
}

fn (mut g Gen) fixed_array_init_with_cast(expr ast.ArrayInit, typ ast.Type) {
if g.is_cc_msvc {
stmts := g.go_before_last_stmt().trim_space()
tmp_var := g.new_tmp_var()
g.write('${g.styp(typ)} ${tmp_var} = ')
g.expr(expr)
g.writeln(';')
g.write2(stmts, tmp_var)
} else {
g.write('(${g.styp(typ)})')
g.expr(expr)
}
}

fn (mut g Gen) fixed_array_var_init(expr_str string, is_auto_deref bool, elem_type ast.Type, size int) {
elem_sym := g.table.sym(elem_type)
if !g.inside_array_fixed_struct {
Expand Down

0 comments on commit fc6e481

Please sign in to comment.