cgen: clean up struct_init (#12815)
parent
b303588491
commit
be5823069a
|
@ -5911,29 +5911,29 @@ const (
|
||||||
skip_struct_init = ['struct stat', 'struct addrinfo']
|
skip_struct_init = ['struct stat', 'struct addrinfo']
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
fn (mut g Gen) struct_init(node ast.StructInit) {
|
||||||
styp := g.typ(struct_init.typ)
|
styp := g.typ(node.typ)
|
||||||
mut shared_styp := '' // only needed for shared x := St{...
|
mut shared_styp := '' // only needed for shared x := St{...
|
||||||
if styp in c.skip_struct_init {
|
if styp in c.skip_struct_init {
|
||||||
// needed for c++ compilers
|
// needed for c++ compilers
|
||||||
g.go_back_out(3)
|
g.go_back_out(3)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mut sym := g.table.get_final_type_symbol(g.unwrap_generic(struct_init.typ))
|
mut sym := g.table.get_final_type_symbol(g.unwrap_generic(node.typ))
|
||||||
is_amp := g.is_amp
|
is_amp := g.is_amp
|
||||||
is_multiline := struct_init.fields.len > 5
|
is_multiline := node.fields.len > 5
|
||||||
g.is_amp = false // reset the flag immediately so that other struct inits in this expr are handled correctly
|
g.is_amp = false // reset the flag immediately so that other struct inits in this expr are handled correctly
|
||||||
if is_amp {
|
if is_amp {
|
||||||
g.out.go_back(1) // delete the `&` already generated in `prefix_expr()
|
g.out.go_back(1) // delete the `&` already generated in `prefix_expr()
|
||||||
}
|
}
|
||||||
if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set {
|
if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set {
|
||||||
mut shared_typ := struct_init.typ.set_flag(.shared_f)
|
mut shared_typ := node.typ.set_flag(.shared_f)
|
||||||
shared_styp = g.typ(shared_typ)
|
shared_styp = g.typ(shared_typ)
|
||||||
g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.mtx = {0}, .val =($styp){')
|
g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.mtx = {0}, .val =($styp){')
|
||||||
} else if is_amp || g.inside_cast_in_heap > 0 {
|
} else if is_amp || g.inside_cast_in_heap > 0 {
|
||||||
g.write('($styp*)memdup(&($styp){')
|
g.write('($styp*)memdup(&($styp){')
|
||||||
} else if struct_init.typ.is_ptr() {
|
} else if node.typ.is_ptr() {
|
||||||
basetyp := g.typ(struct_init.typ.set_nr_muls(0))
|
basetyp := g.typ(node.typ.set_nr_muls(0))
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
g.writeln('&($basetyp){')
|
g.writeln('&($basetyp){')
|
||||||
} else {
|
} else {
|
||||||
|
@ -5949,13 +5949,13 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
// mut fields := []string{}
|
// mut fields := []string{}
|
||||||
mut inited_fields := map[string]int{} // TODO this is done in checker, move to ast node
|
mut inited_fields := map[string]int{} // TODO this is done in checker, move to ast node
|
||||||
/*
|
/*
|
||||||
if struct_init.fields.len == 0 && struct_init.exprs.len > 0 {
|
if node.fields.len == 0 && node.exprs.len > 0 {
|
||||||
// Get fields for {a,b} short syntax. Fields array wasn't set in the parser.
|
// Get fields for {a,b} short syntax. Fields array wasn't set in the parser.
|
||||||
for f in info.fields {
|
for f in info.fields {
|
||||||
fields << f.name
|
fields << f.name
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fields = struct_init.fields
|
fields = node.fields
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
|
@ -5964,7 +5964,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
// User set fields
|
// User set fields
|
||||||
mut initialized := false
|
mut initialized := false
|
||||||
mut old_is_shared := g.is_shared
|
mut old_is_shared := g.is_shared
|
||||||
for i, field in struct_init.fields {
|
for i, field in node.fields {
|
||||||
if !field.typ.has_flag(.shared_f) {
|
if !field.typ.has_flag(.shared_f) {
|
||||||
g.is_shared = false
|
g.is_shared = false
|
||||||
}
|
}
|
||||||
|
@ -5990,7 +5990,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
}
|
}
|
||||||
g.expr_with_cast(field.expr, field.typ, field.expected_type)
|
g.expr_with_cast(field.expr, field.typ, field.expected_type)
|
||||||
}
|
}
|
||||||
if i != struct_init.fields.len - 1 {
|
if i != node.fields.len - 1 {
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
g.writeln(',')
|
g.writeln(',')
|
||||||
} else {
|
} else {
|
||||||
|
@ -6008,7 +6008,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
if sym.kind == .struct_ {
|
if sym.kind == .struct_ {
|
||||||
mut info := sym.info as ast.Struct
|
mut info := sym.info as ast.Struct
|
||||||
nr_fields = info.fields.len
|
nr_fields = info.fields.len
|
||||||
if info.is_union && struct_init.fields.len > 1 {
|
if info.is_union && node.fields.len > 1 {
|
||||||
verror('union must not have more than 1 initializer')
|
verror('union must not have more than 1 initializer')
|
||||||
}
|
}
|
||||||
if !info.is_union {
|
if !info.is_union {
|
||||||
|
@ -6016,7 +6016,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
mut used_embed_fields := []string{}
|
mut used_embed_fields := []string{}
|
||||||
init_field_names := info.fields.map(it.name)
|
init_field_names := info.fields.map(it.name)
|
||||||
// fields that are initialized but belong to the embedding
|
// fields that are initialized but belong to the embedding
|
||||||
init_fields_to_embed := struct_init.fields.filter(it.name !in init_field_names)
|
init_fields_to_embed := node.fields.filter(it.name !in init_field_names)
|
||||||
for embed in info.embeds {
|
for embed in info.embeds {
|
||||||
embed_sym := g.table.get_type_symbol(embed)
|
embed_sym := g.table.get_type_symbol(embed)
|
||||||
embed_name := embed_sym.embed_name()
|
embed_name := embed_sym.embed_name()
|
||||||
|
@ -6027,7 +6027,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
&& it.name in embed_field_names)
|
&& it.name in embed_field_names)
|
||||||
used_embed_fields << fields_to_embed.map(it.name)
|
used_embed_fields << fields_to_embed.map(it.name)
|
||||||
default_init := ast.StructInit{
|
default_init := ast.StructInit{
|
||||||
...struct_init
|
...node
|
||||||
typ: embed
|
typ: embed
|
||||||
fields: init_fields_to_embed
|
fields: init_fields_to_embed
|
||||||
}
|
}
|
||||||
|
@ -6062,7 +6062,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if field.name in inited_fields {
|
if field.name in inited_fields {
|
||||||
sfield := struct_init.fields[inited_fields[field.name]]
|
sfield := node.fields[inited_fields[field.name]]
|
||||||
field_name := if sym.language == .v { c_name(field.name) } else { field.name }
|
field_name := if sym.language == .v { c_name(field.name) } else { field.name }
|
||||||
if sfield.typ == 0 {
|
if sfield.typ == 0 {
|
||||||
continue
|
continue
|
||||||
|
@ -6118,9 +6118,9 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
||||||
if field.typ in info.embeds {
|
if field.typ in info.embeds {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if struct_init.has_update_expr {
|
if node.has_update_expr {
|
||||||
g.expr(struct_init.update_expr)
|
g.expr(node.update_expr)
|
||||||
if struct_init.update_expr_type.is_ptr() {
|
if node.update_expr_type.is_ptr() {
|
||||||
g.write('->')
|
g.write('->')
|
||||||
} else {
|
} else {
|
||||||
g.write('.')
|
g.write('.')
|
||||||
|
|
Loading…
Reference in New Issue