cgen: cleanup g.type_default

pull/9890/head
Delyan Angelov 2021-04-26 11:54:32 +03:00
parent 212b4fa089
commit 5e0ff5c524
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 76 additions and 98 deletions

View File

@ -5852,8 +5852,26 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
if typ.is_ptr() && !typ.has_flag(.shared_f) {
return '0'
}
if typ.idx() < ast.string_type_idx {
// Default values for other types are not needed because of mandatory initialization
return '0'
}
sym := g.table.get_type_symbol(typ)
if sym.kind == .array {
match sym.kind {
.string {
return '(string){.str=(byteptr)"", .is_lit=1}'
}
.interface_, .sum_type, .array_fixed, .multi_return {
return '{0}'
}
.alias {
return g.type_default((sym.info as ast.Alias).parent_type)
}
.chan {
elemtypstr := g.typ(sym.chan_info().elem_type)
return 'sync__new_channel_st(0, sizeof($elemtypstr))'
}
.array {
elem_typ := sym.array_info().elem_type
elem_sym := g.typ(elem_typ)
mut elem_type_str := util.no_dots(elem_sym)
@ -5869,7 +5887,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
return init_str
}
}
if sym.kind == .map {
.map {
info := sym.map_info()
key_typ := g.table.get_type_symbol(info.key_type)
hash_fn, key_eq_fn, clone_fn, free_fn := g.map_fn_ptrs(key_typ)
@ -5881,9 +5899,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
return init_str
}
}
// User struct defined in another module.
// if typ.contains('__') {
if sym.kind == .struct_ {
.struct_ {
mut has_none_zero := false
mut init_str := '{'
info := sym.info as ast.Struct
@ -5913,48 +5929,10 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
return init_str
}
}
// if typ.ends_with('Fn') { // TODO
// return '0'
// }
// Default values for other types are not needed because of mandatory initialization
idx := int(typ)
if idx >= 1 && idx <= 17 {
else {
return '0'
}
/*
match idx {
ast.bool_type_idx {
return '0'
}
else {}
}
*/
match sym.name {
'string' { return '(string){.str=(byteptr)"", .is_lit=1}' }
'rune' { return '0' }
else {}
}
if sym.kind == .chan {
elemtypstr := g.typ(sym.chan_info().elem_type)
return 'sync__new_channel_st(0, sizeof($elemtypstr))'
}
return match sym.kind {
.interface_, .sum_type, .array_fixed, .multi_return { '{0}' }
.alias { g.type_default((sym.info as ast.Alias).parent_type) }
else { '0' }
}
// TODO this results in
// error: expected a field designator, such as '.field = 4'
// - Empty ee= (Empty) { . = {0} } ;
/*
return match typ {
'bool', 'i8', 'i16', 'i64', 'u16', 'u32', 'u64', 'byte', 'int', 'rune', 'byteptr', 'voidptr' {'0'}
'string'{ '_SLIT("")'}
'f32'{ '0.0'}
'f64'{ '0.0'}
else { '{0} '}
}
*/
}
fn (g &Gen) get_all_test_function_names() []string {