cgen: cleanup array.v (#22723)

This commit is contained in:
yuyi 2024-11-01 19:44:04 +08:00 committed by GitHub
parent 2c887e2017
commit fc6e481bc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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] { if g.table.sym(elem_typ).kind in [.interface, .sum_type] {
g.expr_with_cast(right, right_type, elem_typ) g.expr_with_cast(right, right_type, elem_typ)
} else if right is ast.ArrayInit { } else if right is ast.ArrayInit {
if g.is_cc_msvc { g.fixed_array_init_with_cast(right, right_type)
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)
}
} else { } else {
g.expr(right) g.expr(right)
} }
@ -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] { 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) g.expr_with_cast(node.args[0].expr, node.args[0].typ, elem_typ)
} else if node.args[0].expr is ast.ArrayInit { } else if node.args[0].expr is ast.ArrayInit {
if g.is_cc_msvc { g.fixed_array_init_with_cast(node.args[0].expr, node.args[0].typ)
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)
}
} else { } else {
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
} }
@ -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.writeln('${left_styp} ${tmp}_orig;')
g.write('memcpy(&${tmp}_orig, &') g.write('memcpy(&${tmp}_orig, &')
if node.left is ast.ArrayInit { if node.left is ast.ArrayInit {
if g.is_cc_msvc { g.fixed_array_init_with_cast(node.left, node.left_type)
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)
}
} else { } else {
if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() { if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() {
g.write('*') g.write('*')
@ -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) { 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) elem_sym := g.table.sym(elem_type)
if !g.inside_array_fixed_struct { if !g.inside_array_fixed_struct {