Skip to content

Commit

Permalink
opt
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 17, 2024
1 parent 35eeac5 commit bb6851a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
} else {
g.styp(info.elem_type)
}
left_is_shared := node.left_type.has_flag(.shared_f)
// `vals[i].field = x` is an exception and requires `array_get`:
// `(*(Val*)array_get(vals, i)).field = x;`
if g.is_assign_lhs && node.is_setter {
Expand All @@ -170,14 +171,14 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('((${elem_type_str}*)')
} else if is_op_assign {
g.write('(*(${elem_type_str}*)array_get(')
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('*')
}
} else {
g.cur_indexexpr << node.pos.pos
g.is_arraymap_set = true // special handling of assign_op and closing with '})'
g.write('array_set(')
if !left_is_ptr || node.left_type.has_flag(.shared_f) {
if !left_is_ptr || left_is_shared {
g.write('&')
}
}
Expand All @@ -189,15 +190,15 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
g.expr(node.left)
}

if node.left_type.has_flag(.shared_f) {
if left_is_shared {
if left_is_ptr {
g.write('->val')
} else {
g.write('.val')
}
}
if is_direct_array_access {
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('->')
} else {
g.write('.')
Expand Down Expand Up @@ -251,7 +252,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' }
if gen_or {
g.write('${elem_type_str}* ${tmp_opt_ptr} = (${elem_type_str}*)(array_get_with_check(')
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('*')
}
} else {
Expand All @@ -268,29 +269,29 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write(')(*(${elem_type_str}*)array_get(')
}
}
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('*')
}
} else if is_direct_array_access {
g.write('((${elem_type_str}*)')
} else {
g.write('(*(${elem_type_str}*)array_get(')
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('*')
}
}
}
g.expr(node.left)
// TODO: test direct_array_access when 'shared' is implemented
if node.left_type.has_flag(.shared_f) {
if left_is_shared {
if left_is_ptr {
g.write('->val')
} else {
g.write('.val')
}
}
if is_direct_array_access && !gen_or {
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
if left_is_ptr && !left_is_shared {
g.write('->')
} else {
g.write('.')
Expand Down

0 comments on commit bb6851a

Please sign in to comment.