Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgen: cleanup in index.v
Browse files Browse the repository at this point in the history
yuyi98 committed Dec 22, 2023
1 parent 06a536e commit c8e3890
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
@@ -48,18 +48,16 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write(')')
}
}
} else if sym.info is ast.Aggregate
&& sym.info.types.filter(g.table.type_kind(it) !in [.array, .array_fixed, .string, .map]).len == 0 {
// treating sumtype of array types
unwrapped_got_type := sym.info.types[g.aggregate_type_idx]
g.index_expr(ast.IndexExpr{ ...node, left_type: unwrapped_got_type })
} else {
if sym.kind == .aggregate
&& (sym.info as ast.Aggregate).types.filter(g.table.type_kind(it) !in [.array, .array_fixed, .string, .map]).len == 0 {
// treating sumtype of array types
unwrapped_got_type := (sym.info as ast.Aggregate).types[g.aggregate_type_idx]
g.index_expr(ast.IndexExpr{ ...node, left_type: unwrapped_got_type })
} else {
g.expr(node.left)
g.write('[')
g.expr(node.index)
g.write(']')
}
g.expr(node.left)
g.write('[')
g.expr(node.index)
g.write(']')
}
}
}
@@ -97,18 +95,17 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
g.write('*')
}
g.expr(node.left)
} else if sym.kind == .array_fixed {
} else if sym.info is ast.ArrayFixed {
// Convert a fixed array to V array when doing `fixed_arr[start..end]`
info := sym.info as ast.ArrayFixed
noscan := g.check_noscan(info.elem_type)
noscan := g.check_noscan(sym.info.elem_type)
if node.is_gated {
g.write('array_slice_ni(')
} else {
g.write('array_slice(')
}
g.write('new_array_from_c_array${noscan}(')
ctype := g.typ(info.elem_type)
g.write('${info.size}, ${info.size}, sizeof(${ctype}), ')
ctype := g.typ(sym.info.elem_type)
g.write('${sym.info.size}, ${sym.info.size}, sizeof(${ctype}), ')
if node.left_type.is_ptr() {
g.write('*')
}
@@ -138,9 +135,8 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
g.write(', ')
if range.has_high {
g.expr(range.high)
} else if sym.kind == .array_fixed {
info := sym.info as ast.ArrayFixed
g.write('${info.size}')
} else if sym.info is ast.ArrayFixed {
g.write('${sym.info.size}')
} else {
g.write('2147483647') // max_int
}

0 comments on commit c8e3890

Please sign in to comment.