@@ -335,41 +335,25 @@ fn (mut g Gen) gen_str_for_enum(info ast.Enum, styp string, str_fn_name string)
335
335
}
336
336
337
337
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 )
347
343
mut clean_interface_v_type_name := styp.replace ('__' , '.' )
348
344
if styp.ends_with ('*' ) {
349
345
clean_interface_v_type_name = '&' + clean_interface_v_type_name.replace ('*' , '' )
350
346
}
351
347
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 */' )
357
349
for typ in info.types {
358
- typ_str := g.typ (typ)
359
350
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)
367
352
sym_has_str_method , str_method_expects_ptr , _ := subtype.str_method_info ()
368
353
if should_use_indent_func (subtype.kind) && ! sym_has_str_method {
369
354
func_name = 'indent_$func_name '
370
355
}
371
356
372
- // ------------------------------------------
373
357
// str_intp
374
358
deref := if sym_has_str_method && str_method_expects_ptr { ' ' } else { '*' }
375
359
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
382
366
{_SLIT("${clean_interface_v_type_name} (\' "), $c.si_s_code , {.d_s = $val }},
383
367
{_SLIT("\' )"), 0, {.d_c = 0 }}
384
368
}))'
385
- g.auto_str_funcs .write_string ('\t if (x._typ == _${styp} _${subtype.cname} _index)' )
386
- g.auto_str_funcs .write_string (' return $res ;' )
369
+ fn_builder .write_string ('\t if (x._typ == _${styp} _${subtype.cname} _index)' )
370
+ fn_builder .write_string (' return $res ;' )
387
371
} else {
388
372
mut val := '${func_name} (${deref} ($subtype.cname *)x._$subtype.cname '
389
373
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
394
378
{_SLIT("${clean_interface_v_type_name} ("), $c.si_s_code , {.d_s = $val }},
395
379
{_SLIT(")"), 0, {.d_c = 0 }}
396
380
}))'
397
- g.auto_str_funcs .write_string ('\t if (x._typ == _${styp} _${subtype.cname} _index)' )
398
- g.auto_str_funcs .write_string (' return $res ;\n ' )
381
+ fn_builder .write_string ('\t if (x._typ == _${styp} _${subtype.cname} _index)' )
382
+ fn_builder .write_string (' return $res ;\n ' )
399
383
}
400
384
}
401
- g.auto_str_funcs.writeln ('\t return _SLIT("unknown interface value");' )
402
- g.auto_str_funcs.writeln ('}' )
385
+ fn_builder.writeln ('\t return _SLIT("unknown interface value");' )
386
+ fn_builder.writeln ('}' )
387
+ g.auto_fn_definitions << fn_builder.str ()
403
388
}
404
389
405
390
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
- }
415
391
// _str() functions should have a single argument, the indenting ones take 2:
416
392
g.type_definitions.writeln ('static string ${str_fn_name} ($styp x); // auto' )
417
393
g.auto_str_funcs.writeln ('static string ${str_fn_name} ($styp x) { return indent_${str_fn_name} (x, 0); }' )
418
394
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) {' )
420
397
mut clean_sum_type_v_type_name := styp.replace ('__' , '.' )
421
398
if styp.ends_with ('*' ) {
422
399
clean_sum_type_v_type_name = '&' + clean_sum_type_v_type_name.replace ('*' , '' )
423
400
}
424
401
clean_sum_type_v_type_name = util.strip_main_name (clean_sum_type_v_type_name)
425
- g.auto_str_funcs .writeln ('\t switch(x._typ) {' )
402
+ fn_builder .writeln ('\t switch(x._typ) {' )
426
403
for typ in info.variants {
427
404
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)
433
406
sym := g.table.get_type_symbol (typ)
434
407
sym_has_str_method , str_method_expects_ptr , _ := sym.str_method_info ()
435
408
deref := if sym_has_str_method && str_method_expects_ptr { ' ' } else { '*' }
436
409
if should_use_indent_func (sym.kind) && ! sym_has_str_method {
437
410
func_name = 'indent_$func_name '
438
411
}
439
412
440
- // ------------------------------------------
441
413
// str_intp
442
414
if typ == ast.string_type {
443
415
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_
449
421
{_SLIT("${clean_sum_type_v_type_name} (\' "), $c.si_s_code , {.d_s = $val }},
450
422
{_SLIT("\' )"), 0, {.d_c = 0 }}
451
423
}))'
452
- g.auto_str_funcs .write_string ('\t\t case $typ : return $res ;' )
424
+ fn_builder .write_string ('\t\t case $typ : return $res ;' )
453
425
} else {
454
426
mut val := '${func_name} (${deref} ($typ_str *)x._$sym.cname '
455
427
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_
460
432
{_SLIT("${clean_sum_type_v_type_name} ("), $c.si_s_code , {.d_s = $val }},
461
433
{_SLIT(")"), 0, {.d_c = 0 }}
462
434
}))'
463
- g.auto_str_funcs .write_string ('\t\t case $typ : return $res ;' )
435
+ fn_builder .write_string ('\t\t case $typ : return $res ;' )
464
436
}
465
437
}
466
- g.auto_str_funcs.writeln ('\t\t default: return _SLIT("unknown sum type value");' )
467
- g.auto_str_funcs.writeln ('\t }' )
468
- g.auto_str_funcs.writeln ('}' )
438
+ fn_builder.writeln ('\t\t default: return _SLIT("unknown sum type value");' )
439
+ fn_builder.writeln ('\t }' )
440
+ fn_builder.writeln ('}' )
441
+ g.auto_fn_definitions << fn_builder.str ()
469
442
}
470
443
471
444
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
811
784
if info.fields.len == 0 {
812
785
fn_builder.writeln ('\t return _SLIT("$clean_struct_v_type_name {}");' )
813
786
fn_builder.writeln ('}' )
814
- fn_builder.writeln ('' )
815
787
return
816
788
}
817
789
0 commit comments