Skip to content

Commit 93bb87f

Browse files
authored
cgen: simplify auto_str_methods (#10544)
1 parent 66bf963 commit 93bb87f

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -335,41 +335,25 @@ fn (mut g Gen) gen_str_for_enum(info ast.Enum, styp string, str_fn_name string)
335335
}
336336

337337
fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, str_fn_name string) {
338-
mut gen_fn_names := map[string]string{}
339-
for typ in info.types {
340-
sym := g.table.get_type_symbol(typ)
341-
if !sym.has_method('str') {
342-
field_styp := g.typ(typ)
343-
field_fn_name := g.gen_str_for_type(typ)
344-
gen_fn_names[field_styp] = field_fn_name
345-
}
346-
}
338+
// _str() functions should have a single argument, the indenting ones take 2:
339+
g.type_definitions.writeln('static string ${str_fn_name}($styp x); // auto')
340+
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp x) { return indent_${str_fn_name}(x, 0); }')
341+
g.type_definitions.writeln('static string indent_${str_fn_name}($styp x, int indent_count); // auto')
342+
mut fn_builder := strings.new_builder(512)
347343
mut clean_interface_v_type_name := styp.replace('__', '.')
348344
if styp.ends_with('*') {
349345
clean_interface_v_type_name = '&' + clean_interface_v_type_name.replace('*', '')
350346
}
351347
clean_interface_v_type_name = util.strip_main_name(clean_interface_v_type_name)
352-
353-
g.type_definitions.writeln('static string ${str_fn_name}($styp x); // auto')
354-
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp x) { return indent_${str_fn_name}(x, 0); }')
355-
g.type_definitions.writeln('static string indent_${str_fn_name}($styp x, int indent_count); // auto')
356-
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp x, int indent_count) { /* gen_str_for_interface */')
348+
fn_builder.writeln('static string indent_${str_fn_name}($styp x, int indent_count) { /* gen_str_for_interface */')
357349
for typ in info.types {
358-
typ_str := g.typ(typ)
359350
subtype := g.table.get_type_symbol(typ)
360-
361-
mut func_name := if typ_str in gen_fn_names {
362-
gen_fn_names[typ_str]
363-
} else {
364-
g.gen_str_for_type(typ)
365-
}
366-
351+
mut func_name := g.gen_str_for_type(typ)
367352
sym_has_str_method, str_method_expects_ptr, _ := subtype.str_method_info()
368353
if should_use_indent_func(subtype.kind) && !sym_has_str_method {
369354
func_name = 'indent_$func_name'
370355
}
371356

372-
//------------------------------------------
373357
// str_intp
374358
deref := if sym_has_str_method && str_method_expects_ptr { ' ' } else { '*' }
375359
if typ == ast.string_type {
@@ -382,8 +366,8 @@ fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, str_fn_nam
382366
{_SLIT("${clean_interface_v_type_name}(\'"), $c.si_s_code, {.d_s = $val}},
383367
{_SLIT("\')"), 0, {.d_c = 0 }}
384368
}))'
385-
g.auto_str_funcs.write_string('\tif (x._typ == _${styp}_${subtype.cname}_index)')
386-
g.auto_str_funcs.write_string(' return $res;')
369+
fn_builder.write_string('\tif (x._typ == _${styp}_${subtype.cname}_index)')
370+
fn_builder.write_string(' return $res;')
387371
} else {
388372
mut val := '${func_name}(${deref}($subtype.cname*)x._$subtype.cname'
389373
if should_use_indent_func(subtype.kind) && !sym_has_str_method {
@@ -394,50 +378,38 @@ fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, str_fn_nam
394378
{_SLIT("${clean_interface_v_type_name}("), $c.si_s_code, {.d_s = $val}},
395379
{_SLIT(")"), 0, {.d_c = 0 }}
396380
}))'
397-
g.auto_str_funcs.write_string('\tif (x._typ == _${styp}_${subtype.cname}_index)')
398-
g.auto_str_funcs.write_string(' return $res;\n')
381+
fn_builder.write_string('\tif (x._typ == _${styp}_${subtype.cname}_index)')
382+
fn_builder.write_string(' return $res;\n')
399383
}
400384
}
401-
g.auto_str_funcs.writeln('\treturn _SLIT("unknown interface value");')
402-
g.auto_str_funcs.writeln('}')
385+
fn_builder.writeln('\treturn _SLIT("unknown interface value");')
386+
fn_builder.writeln('}')
387+
g.auto_fn_definitions << fn_builder.str()
403388
}
404389

405390
fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_name string) {
406-
mut gen_fn_names := map[string]string{}
407-
for typ in info.variants {
408-
sym := g.table.get_type_symbol(typ)
409-
if !sym.has_method('str') {
410-
field_styp := g.typ(typ)
411-
field_fn_name := g.gen_str_for_type(typ)
412-
gen_fn_names[field_styp] = field_fn_name
413-
}
414-
}
415391
// _str() functions should have a single argument, the indenting ones take 2:
416392
g.type_definitions.writeln('static string ${str_fn_name}($styp x); // auto')
417393
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp x) { return indent_${str_fn_name}(x, 0); }')
418394
g.type_definitions.writeln('static string indent_${str_fn_name}($styp x, int indent_count); // auto')
419-
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp x, int indent_count) {')
395+
mut fn_builder := strings.new_builder(512)
396+
fn_builder.writeln('static string indent_${str_fn_name}($styp x, int indent_count) {')
420397
mut clean_sum_type_v_type_name := styp.replace('__', '.')
421398
if styp.ends_with('*') {
422399
clean_sum_type_v_type_name = '&' + clean_sum_type_v_type_name.replace('*', '')
423400
}
424401
clean_sum_type_v_type_name = util.strip_main_name(clean_sum_type_v_type_name)
425-
g.auto_str_funcs.writeln('\tswitch(x._typ) {')
402+
fn_builder.writeln('\tswitch(x._typ) {')
426403
for typ in info.variants {
427404
typ_str := g.typ(typ)
428-
mut func_name := if typ_str in gen_fn_names {
429-
gen_fn_names[typ_str]
430-
} else {
431-
g.gen_str_for_type(typ)
432-
}
405+
mut func_name := g.gen_str_for_type(typ)
433406
sym := g.table.get_type_symbol(typ)
434407
sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
435408
deref := if sym_has_str_method && str_method_expects_ptr { ' ' } else { '*' }
436409
if should_use_indent_func(sym.kind) && !sym_has_str_method {
437410
func_name = 'indent_$func_name'
438411
}
439412

440-
//------------------------------------------
441413
// str_intp
442414
if typ == ast.string_type {
443415
mut val := '${func_name}(${deref}($typ_str*)x._$sym.cname'
@@ -449,7 +421,7 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_
449421
{_SLIT("${clean_sum_type_v_type_name}(\'"), $c.si_s_code, {.d_s = $val}},
450422
{_SLIT("\')"), 0, {.d_c = 0 }}
451423
}))'
452-
g.auto_str_funcs.write_string('\t\tcase $typ: return $res;')
424+
fn_builder.write_string('\t\tcase $typ: return $res;')
453425
} else {
454426
mut val := '${func_name}(${deref}($typ_str*)x._$sym.cname'
455427
if should_use_indent_func(sym.kind) && !sym_has_str_method {
@@ -460,12 +432,13 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_
460432
{_SLIT("${clean_sum_type_v_type_name}("), $c.si_s_code, {.d_s = $val}},
461433
{_SLIT(")"), 0, {.d_c = 0 }}
462434
}))'
463-
g.auto_str_funcs.write_string('\t\tcase $typ: return $res;')
435+
fn_builder.write_string('\t\tcase $typ: return $res;')
464436
}
465437
}
466-
g.auto_str_funcs.writeln('\t\tdefault: return _SLIT("unknown sum type value");')
467-
g.auto_str_funcs.writeln('\t}')
468-
g.auto_str_funcs.writeln('}')
438+
fn_builder.writeln('\t\tdefault: return _SLIT("unknown sum type value");')
439+
fn_builder.writeln('\t}')
440+
fn_builder.writeln('}')
441+
g.auto_fn_definitions << fn_builder.str()
469442
}
470443

471444
fn (mut g Gen) fn_decl_str(info ast.FnType) string {
@@ -811,7 +784,6 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
811784
if info.fields.len == 0 {
812785
fn_builder.writeln('\treturn _SLIT("$clean_struct_v_type_name{}");')
813786
fn_builder.writeln('}')
814-
fn_builder.writeln('')
815787
return
816788
}
817789

0 commit comments

Comments
 (0)