Skip to content

Commit

Permalink
cgen: fix fixed array literal in infix expr
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 18, 2024
1 parent 8054808 commit 4628aa5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
22 changes: 14 additions & 8 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.array_init_with_fields(node, elem_type, is_amp, shared_styp, var_name)
} else {
// `[1, 2, 3]`
tmp_inside_array := g.inside_array_item
g.inside_array_item = true
defer {
g.inside_array_item = tmp_inside_array
}
elem_styp := g.typ(elem_type.typ)
noscan := g.check_noscan(elem_type.typ)
if elem_type.unaliased_sym.kind == .function {
Expand Down Expand Up @@ -136,7 +141,8 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.set_current_pos_as_last_stmt_pos()
return
}
need_tmp_var := g.inside_call && !g.inside_struct_init && node.exprs.len == 0
need_tmp_var := (g.inside_call || g.inside_infix) && !g.inside_struct_init && !g.inside_index
&& !g.inside_array_item
mut stmt_str := ''
mut tmp_var := ''
if need_tmp_var {
Expand All @@ -159,12 +165,12 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
if !is_struct {
g.write('{')
}
tmp_inside_array := g.inside_array_item
g.inside_array_item = true
defer {
g.inside_array_item = tmp_inside_array
}
if node.has_val {
tmp_inside_array := g.inside_array_item
g.inside_array_item = true
defer {
g.inside_array_item = tmp_inside_array
}
for i, expr in node.exprs {
if elem_sym.kind == .array_fixed && expr in [ast.Ident, ast.SelectorExpr] {
info := elem_sym.info as ast.ArrayFixed
Expand Down Expand Up @@ -988,7 +994,7 @@ fn (mut g Gen) gen_array_contains_methods() {
fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {')
if elem_kind == .string {
fn_builder.writeln('\t\tif (fast_string_eq(((string*)a.data)[i], v)) {')
} else if elem_kind == .array && elem_is_not_ptr {
} else if elem_kind in [.array, .array_fixed] && elem_is_not_ptr {
ptr_typ := g.equality_fn(elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(((${elem_type_str}*)a.data)[i], v)) {')
} else if elem_kind == .function {
Expand Down Expand Up @@ -1027,7 +1033,7 @@ fn (mut g Gen) gen_array_contains_methods() {
fn_builder.writeln('\tfor (int i = 0; i < ${size}; ++i) {')
if elem_kind == .string {
fn_builder.writeln('\t\tif (fast_string_eq(a[i], v)) {')
} else if elem_kind == .array && elem_is_not_ptr {
} else if elem_kind in [.array, .array_fixed] && elem_is_not_ptr {
ptr_typ := g.equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(a[i], v)) {')
} else if elem_kind == .function {
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ mut:
inside_map_postfix bool // inside map++/-- postfix expr
inside_map_infix bool // inside map<</+=/-= infix expr
inside_assign bool
inside_index bool
inside_infix bool
inside_map_index bool
inside_array_index bool
inside_array_fixed_struct bool
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import v.ast
import v.util

fn (mut g Gen) index_expr(node ast.IndexExpr) {
pre_inside_index := g.inside_index
g.inside_index = true
defer {
g.inside_index = pre_inside_index
}
if node.index is ast.RangeExpr {
g.index_range_expr(node, node.index)
} else {
Expand Down
6 changes: 5 additions & 1 deletion vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import v.util

fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.expected_fixed_arr = true
pre_inside_infix := g.inside_infix
g.inside_infix = true
defer {
g.expected_fixed_arr = false
g.inside_infix = pre_inside_infix
}
if node.auto_locked != '' {
g.writeln('sync__RwMutex_lock(&${node.auto_locked}->mtx);')
Expand Down Expand Up @@ -877,7 +880,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
// push a single element
elem_type_str := g.typ(array_info.elem_type)
elem_sym := g.table.sym(array_info.elem_type)
elem_is_array_var := elem_sym.kind in [.array, .array_fixed] && node.right is ast.Ident
elem_is_array_var := (elem_sym.kind == .array && node.right is ast.Ident)
|| elem_sym.kind == .array_fixed
g.write('array_push${noscan}((array*)')
if !left.typ.is_ptr()
|| (node.left_type.has_flag(.shared_f) && !node.left_type.deref().is_ptr()) {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/tests/builtin_arrays/fixed_array_literal_infix_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn test_fixed_array_literal_infix() {
mut a := [][2]int{}
a << [0, 0]!
println([0, 0]! in a)
ret := [0, 0]! in a
println(ret)
assert ret
}

0 comments on commit 4628aa5

Please sign in to comment.