diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 760278248e..b9d34d83d2 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -496,7 +496,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) { if node.is_pub { f.write('pub ') } - ptype := f.type_to_str(node.parent_type) + ptype := f.table.type_to_str(node.parent_type) f.write('type $node.name = $ptype') } ast.FnTypeDecl { @@ -548,7 +548,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) { f.write('type $node.name = ') mut sum_type_names := []string{} for t in node.sub_types { - sum_type_names << f.type_to_str(t) + sum_type_names << f.table.type_to_str(t) } sum_type_names.sort() for i, name in sum_type_names { @@ -710,64 +710,6 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) { } } -pub fn (f &Fmt) type_to_str(t table.Type) string { - mut res := f.table.type_to_str(t) - cur_mod := f.cur_mod + '.' - // - map_prefix := 'map[string]' - has_map_prefix := res.starts_with(map_prefix) - if has_map_prefix { - res = res.replace(map_prefix, '') - } - // - chan_prefix := 'chan ' - has_chan_prefix := res.starts_with(chan_prefix) - if has_chan_prefix { - res = res.replace(chan_prefix, '') - } - // - no_symbols := res.trim_left('&[]') - should_shorten := no_symbols.starts_with(cur_mod) - // - for res.ends_with('_ptr') { - // type_ptr => &type - res = res[0..res.len - 4] - start_pos := 2 * res.count('[]') - res = res[0..start_pos] + '&' + res[start_pos..res.len] - } - arr_fixed_prefix := '[]fixed_' - if res.starts_with(arr_fixed_prefix) { - // res: `[]fixed_[]fixed_int_5_10` - // transforms to: `[10][5]int` - mut last_underscore_idx := 0 - mut dimensions := []string{} - for { - res = res[arr_fixed_prefix.len..] - last_underscore_idx = res.last_index('_') or { - return dimensions.join('') + '[]' + - if should_shorten { res.replace_once(cur_mod, '') } else { res } - } - limit := res[last_underscore_idx + 1..] - dimensions << '[$limit]' - res = res[..last_underscore_idx] - if !res.starts_with(arr_fixed_prefix) { - break - } - } - res = dimensions.join('') + res[..last_underscore_idx] - } - if should_shorten { - res = res.replace_once(cur_mod, '') - } - if has_chan_prefix { - res = chan_prefix + res - } - if has_map_prefix { - res = map_prefix + res - } - return res -} - pub fn (mut f Fmt) expr(node ast.Expr) { if f.is_debug { eprintln('expr: ${node.position():-42} | node: ${typeof(node):-20} | $node.str()') @@ -780,7 +722,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.array_init(node) } ast.AsCast { - type_str := f.type_to_str(node.typ) + type_str := f.table.type_to_str(node.typ) f.expr(node.expr) f.write(' as $type_str') } @@ -802,7 +744,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { } ast.CastExpr { node.typname = f.table.get_type_symbol(node.typ).name - f.write(f.type_to_str(node.typ) + '(') + f.write(f.table.type_to_str(node.typ) + '(') f.expr(node.expr) if node.has_arg { f.write(', ') @@ -991,7 +933,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.write(f.short_module(node.type_name)) } } else { - f.write(f.type_to_str(node.typ)) + f.write(f.table.type_to_str(node.typ)) } f.write(')') } else { @@ -1093,7 +1035,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.struct_init(node) } ast.Type { - f.write(f.type_to_str(node.typ)) + f.write(f.table.type_to_str(node.typ)) } ast.TypeOf { f.write('typeof(') @@ -1515,7 +1457,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { } if node.generic_type != 0 && node.generic_type != table.void_type { f.write('<') - f.write(f.type_to_str(node.generic_type)) + f.write(f.table.type_to_str(node.generic_type)) f.write('>') } f.write('(') @@ -1669,7 +1611,7 @@ pub fn (mut f Fmt) chan_init(mut it ast.ChanInit) { if is_mut { f.write('mut ') } - f.write(f.type_to_str(el_typ)) + f.write(f.table.type_to_str(el_typ)) f.write('{') if it.has_cap { f.write('cap: ') @@ -1708,7 +1650,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { f.write('}') return } - f.write(f.type_to_str(it.typ)) + f.write(f.table.type_to_str(it.typ)) f.write('{') // TODO copypasta if it.has_len { @@ -1791,7 +1733,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { f.write('!!') return } - f.write(f.type_to_str(it.elem_type)) + f.write(f.table.type_to_str(it.elem_type)) if it.has_default { f.write('{init: $it.default_expr}') } else { @@ -1909,7 +1851,7 @@ fn (mut f Fmt) global_decl(it ast.GlobalDecl) { f.write(strings.repeat(` `, max - field.name.len)) if field.has_expr { f.write('= ') - f.write(f.type_to_str(field.typ)) + f.write(f.table.type_to_str(field.typ)) f.write('(') f.expr(field.expr) f.write(')') @@ -1917,7 +1859,7 @@ fn (mut f Fmt) global_decl(it ast.GlobalDecl) { if !single && has_assign { f.write(' ') } - f.write('${f.type_to_str(field.typ)} ') + f.write('${f.table.type_to_str(field.typ)} ') } if !single { f.writeln('')