1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: simplify struct_init()

This commit is contained in:
Alexander Medvednikov 2020-05-07 13:00:42 +02:00
parent 78efe72c4c
commit 6ff93f270c
2 changed files with 15 additions and 24 deletions

View File

@ -2121,15 +2121,7 @@ fn (mut g Gen) const_decl_init_later(name, val string, typ table.Type) {
} }
fn (mut g Gen) struct_init(struct_init ast.StructInit) { fn (mut g Gen) struct_init(struct_init ast.StructInit) {
mut info := &table.Struct{}
mut is_struct := false
sym := g.table.get_type_symbol(struct_init.typ) sym := g.table.get_type_symbol(struct_init.typ)
if sym.kind == .struct_ {
is_struct = true
info = sym.info as table.Struct
}
// info := g.table.get_type_symbol(it.typ).info as table.Struct
// println(info.fields.len)
styp := g.typ(struct_init.typ) styp := g.typ(struct_init.typ)
is_amp := g.is_amp is_amp := g.is_amp
if is_amp { if is_amp {
@ -2159,7 +2151,8 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
g.writeln(',') g.writeln(',')
} }
// The rest of the fields are zeroed. // The rest of the fields are zeroed.
if is_struct { if sym.kind == .struct_ {
info := sym.info as table.Struct
for field in info.fields { for field in info.fields {
if field.name in inited_fields { if field.name in inited_fields {
continue continue
@ -2177,10 +2170,10 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
} }
g.writeln(',') g.writeln(',')
} }
}
if struct_init.fields.len == 0 && info.fields.len == 0 { if struct_init.fields.len == 0 && info.fields.len == 0 {
g.write('0') g.write('0')
} }
}
g.write('}') g.write('}')
if is_amp { if is_amp {
g.write(', sizeof($styp))') g.write(', sizeof($styp))')
@ -2539,11 +2532,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('\\000') g.write('\\000')
} }
} }
num_string_parts := if end_string { num_string_parts := if end_string { node.exprs.len + 1 } else { node.exprs.len }
node.exprs.len+1
} else {
node.exprs.len
}
g.write('", $num_string_parts, ') g.write('", $num_string_parts, ')
// Build args // Build args
for i, expr in node.exprs { for i, expr in node.exprs {

View File

@ -188,10 +188,12 @@ fn (mut g Gen) write_autofree_stmts_when_needed(r ast.Return) {
// TODO: write_autofree_stmts_when_needed should not free the returned variables. // TODO: write_autofree_stmts_when_needed should not free the returned variables.
// It may require rewriting g.return_statement to assign the expressions // It may require rewriting g.return_statement to assign the expressions
// to temporary variables, then protecting *them* from autofreeing ... // to temporary variables, then protecting *them* from autofreeing ...
/*
g.writeln('/* autofreeings before return: -------') g.writeln('/* autofreeings before return: -------')
//g.write( g.autofree_scope_vars(r.pos.pos) ) //g.write( g.autofree_scope_vars(r.pos.pos) )
g.write( g.autofree_scope_vars(g.fn_decl.body_pos.pos) ) g.write( g.autofree_scope_vars(g.fn_decl.body_pos.pos) )
g.writeln('--------------------------------------------------- */') g.writeln('--------------------------------------------------- */')
*/
} }
fn (mut g Gen) write_defer_stmts_when_needed() { fn (mut g Gen) write_defer_stmts_when_needed() {