Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: cleanup in array.v (related #19422) #19429

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.expr_with_default(node)
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down Expand Up @@ -171,11 +167,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
} else if node.has_default {
info := array_type.unaliased_sym.info as ast.ArrayFixed
for i in 0 .. info.size {
if info.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, info.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, info.elem_type)
}
g.expr_with_default(node)
if i != info.size - 1 {
g.write(', ')
}
Expand Down Expand Up @@ -244,6 +236,14 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
}
}

fn (mut g Gen) expr_with_default(node ast.ArrayInit) {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
}

fn (mut g Gen) struct_has_array_or_map_field(elem_typ ast.Type) bool {
unaliased_sym := g.table.final_sym(elem_typ)
if unaliased_sym.kind == .struct_ {
Expand Down Expand Up @@ -342,11 +342,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.indent++
g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden
g.write('*pelem = ')
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.expr_with_default(node)
g.writeln(';')
g.indent--
g.writeln('}')
Expand Down Expand Up @@ -409,11 +405,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.writeln('; ${ind}++) {')
g.write('\t${tmp}[${ind}] = ')
if node.has_default {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.expr_with_default(node)
} else {
if node.elem_type.has_flag(.option) {
g.expr_with_opt(ast.None{}, ast.none_type, node.elem_type)
Expand All @@ -427,11 +419,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
g.write(' (voidptr)${tmp})')
} else if node.has_default {
g.write('&(${elem_styp}[]){')
if node.elem_type.has_flag(.option) {
g.expr_with_opt(node.default_expr, node.default_type, node.elem_type)
} else {
g.expr_with_cast(node.default_expr, node.default_type, node.elem_type)
}
g.expr_with_default(node)
g.write('})')
} else if node.has_len && node.elem_type.has_flag(.option) {
g.write('&')
Expand Down
Loading