@@ -910,7 +910,16 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
910
910
fn_body.writeln ('\t string res = str_intp( ${info.fields.len * 4 + 3} , _MOV((StrIntpData[]){' )
911
911
fn_body.writeln ('\t\t {_SLIT("$clean_struct_v_type_name {\\ n"), 0, {.d_c=0}},' )
912
912
for i, field in info.fields {
913
- mut ptr_amp := if field.typ.is_ptr () { '&' } else { '' }
913
+ ftyp_noshared := if field.typ.has_flag (.shared_f) {
914
+ field.typ.deref ().clear_flag (.shared_f)
915
+ } else {
916
+ field.typ
917
+ }
918
+ mut ptr_amp := if ftyp_noshared.is_ptr () {
919
+ '&'
920
+ } else {
921
+ ''
922
+ }
914
923
base_fmt := g.type_to_fmt (g.unwrap_generic (field.typ))
915
924
916
925
// manage prefix and quote symbol for the filed
@@ -933,7 +942,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
933
942
934
943
// custom methods management
935
944
sym_has_str_method , str_method_expects_ptr , _ := sym.str_method_info ()
936
- sftyp := g.typ (field.typ )
945
+ sftyp := g.typ (ftyp_noshared )
937
946
mut field_styp := sftyp.replace ('*' , '' )
938
947
field_styp_fn_name := if sym_has_str_method {
939
948
mut field_fn_name := '${field_styp} _str'
@@ -943,7 +952,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
943
952
}
944
953
field_fn_name
945
954
} else {
946
- g.get_str_fn (field.typ )
955
+ g.get_str_fn (ftyp_noshared )
947
956
}
948
957
949
958
// manage the fact hat with float we use always the g representation
@@ -960,7 +969,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
960
969
if field.typ in ast.cptr_types {
961
970
func = '(voidptr) it.$field.name '
962
971
caller_should_free = false
963
- } else if field.typ .is_ptr () {
972
+ } else if ftyp_noshared .is_ptr () {
964
973
// reference types can be "nil"
965
974
funcprefix + = 'isnil(it.${c_name(field.name)} )'
966
975
funcprefix + = ' ? _SLIT("nil") : '
@@ -999,34 +1008,35 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
999
1008
fn_body.writeln ('\t }));' )
1000
1009
}
1001
1010
1002
- fn struct_auto_str_func (sym & ast.TypeSymbol, field_type ast.Type, fn_name string , field_name string , has_custom_str bool , expects_ptr bool ) (string , bool ) {
1011
+ fn struct_auto_str_func (sym & ast.TypeSymbol, _field_type ast.Type, fn_name string , field_name string , has_custom_str bool , expects_ptr bool ) (string , bool ) {
1003
1012
$if trace_autostr ? {
1004
1013
eprintln ('> struct_auto_str_func: $sym.name | field_type.debug() | $fn_name | $field_name | $has_custom_str | $expects_ptr ' )
1005
1014
}
1015
+ field_type := if _field_type.has_flag (.shared_f) { _field_type.deref () } else { _field_type }
1016
+ sufix := if field_type.has_flag (.shared_f) { '->val' } else { '' }
1006
1017
deref , _ := deref_kind (expects_ptr, field_type.is_ptr (), field_type)
1007
1018
if sym.kind == .enum_ {
1008
1019
return '${fn_name} (${deref} it.${c_name(field_name)} )' , true
1009
1020
} else if should_use_indent_func (sym.kind) {
1010
- obj := 'it.${c_name(field_name)} '
1021
+ obj := '${deref} it.${c_name(field_name)}$sufix '
1011
1022
if has_custom_str {
1012
- return '${fn_name} ($deref$ obj )' , true
1023
+ return '${fn_name} ($obj )' , true
1013
1024
}
1014
- return 'indent_${fn_name} ($deref$ obj , indent_count + 1)' , true
1025
+ return 'indent_${fn_name} ($obj , indent_count + 1)' , true
1015
1026
} else if sym.kind in [.array, .array_fixed, .map , .sum_type] {
1027
+ obj := '${deref} it.${c_name(field_name)}$sufix '
1016
1028
if has_custom_str {
1017
- return '${fn_name} (${deref} it. ${c_name(field_name)} )' , true
1029
+ return '${fn_name} ($obj )' , true
1018
1030
}
1019
- return 'indent_${fn_name} (${deref} it. ${c_name(field_name)} , indent_count + 1)' , true
1031
+ return 'indent_${fn_name} ($obj , indent_count + 1)' , true
1020
1032
} else if sym.kind == .function {
1021
1033
return '${fn_name} ()' , true
1034
+ } else if sym.kind == .chan {
1035
+ return '${fn_name} (${deref} it.${c_name(field_name)}$sufix )' , true
1022
1036
} else {
1023
- if sym.kind == .chan {
1024
- return '${fn_name} (${deref} it.${c_name(field_name)} )' , true
1025
- }
1026
1037
mut method_str := 'it.${c_name(field_name)} '
1027
- mut caller_should_free := false
1028
1038
if sym.kind == .bool {
1029
- method_str + = ' ? _SLIT("true") : _SLIT("false")'
1039
+ return ' $method_str ? _SLIT("true") : _SLIT("false")', false
1030
1040
} else if (field_type.is_int_valptr () || field_type.is_float_valptr ())
1031
1041
&& field_type.is_ptr () && ! expects_ptr {
1032
1042
// ptr int can be "nil", so this needs to be casted to a string
@@ -1045,6 +1055,6 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, field_type ast.Type, fn_name string
1045
1055
fmt_type := StrIntpType.si_i32
1046
1056
return 'str_intp(1, _MOV((StrIntpData[]){{_SLIT0, ${u32(fmt_type) | 0xfe00} , {.d_i32 = *$method_str }}}))' , true
1047
1057
}
1048
- return method_str, caller_should_free
1058
+ return method_str, false
1049
1059
}
1050
1060
}
0 commit comments