cgen: split up array_init() (#14178)

yuyi 2022-04-27 01:58:47 +08:00 committed by Jef Roosens
parent 07d465cbaa
commit 8a75d68421
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 189 additions and 178 deletions

View File

@ -22,7 +22,55 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
array_styp = g.typ(array_type.typ)
g.write('HEAP($array_styp, ')
}
len := node.exprs.len
if array_type.unaliased_sym.kind == .array_fixed {
g.fixed_array_init(node, array_type)
} else if len == 0 {
// `[]int{len: 6, cap:10, init:22}`
g.array_init_with_fields(node, elem_type, is_amp, shared_styp)
} else {
// `[1, 2, 3]`
elem_styp := g.typ(elem_type.typ)
noscan := g.check_noscan(elem_type.typ)
if elem_type.unaliased_sym.kind == .function {
g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){')
} else if g.is_empty_struct(elem_type) {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof(voidptr), _MOV(($elem_styp[$len]){')
} else {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof($elem_styp), _MOV(($elem_styp[$len]){')
}
if len > 8 {
g.writeln('')
g.write('\t\t')
}
for i, expr in node.exprs {
if node.expr_types[i] == ast.string_type && expr !is ast.StringLiteral
&& expr !is ast.StringInterLiteral {
g.write('string_clone(')
g.expr(expr)
g.write(')')
} else {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
}
if i != len - 1 {
if i > 0 && i & 7 == 0 { // i > 0 && i % 8 == 0
g.writeln(',')
g.write('\t\t')
} else {
g.write(', ')
}
}
}
g.write('}))')
if g.is_shared {
g.write('}, sizeof($shared_styp))')
} else if is_amp {
g.write(')')
}
}
}
fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type) {
if node.has_it {
g.inside_lambda = true
tmp := g.new_tmp_var()
@ -115,11 +163,12 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
g.write(stmt_str)
g.write(tmp_var)
}
return
}
// `[]int{len: 6, cap: 10, init: it * it}`
fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp bool, shared_styp string) {
elem_styp := g.typ(elem_type.typ)
noscan := g.check_noscan(elem_type.typ)
if node.exprs.len == 0 {
is_default_array := elem_type.unaliased_sym.kind == .array && node.has_default
is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default
if node.has_it { // []int{len: 6, init: it * it} when variable it is used in init expression
@ -246,44 +295,6 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
} else if is_amp {
g.write(')')
}
return
}
len := node.exprs.len
if elem_type.unaliased_sym.kind == .function {
g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){')
} else if g.is_empty_struct(elem_type) {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof(voidptr), _MOV(($elem_styp[$len]){')
} else {
g.write('new_array_from_c_array${noscan}($len, $len, sizeof($elem_styp), _MOV(($elem_styp[$len]){')
}
if len > 8 {
g.writeln('')
g.write('\t\t')
}
for i, expr in node.exprs {
if node.expr_types[i] == ast.string_type && expr !is ast.StringLiteral
&& expr !is ast.StringInterLiteral {
g.write('string_clone(')
g.expr(expr)
g.write(')')
} else {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
}
if i != len - 1 {
if i > 0 && i & 7 == 0 { // i > 0 && i % 8 == 0
g.writeln(',')
g.write('\t\t')
} else {
g.write(', ')
}
}
}
g.write('}))')
if g.is_shared {
g.write('}, sizeof($shared_styp))')
} else if is_amp {
g.write(')')
}
}
// `nums.map(it % 2 == 0)`