1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: add temp (TODO) auto str for interfaces

This commit is contained in:
Joe Conigliaro 2020-12-29 20:25:38 +11:00
parent 9728abf4bc
commit d094baf107
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 14 additions and 3 deletions

View File

@ -55,6 +55,9 @@ fn (mut g Gen) gen_str_for_type(typ table.Type) string {
table.SumType { table.SumType {
g.gen_str_for_union_sum_type(sym.info, styp, str_fn_name) g.gen_str_for_union_sum_type(sym.info, styp, str_fn_name)
} }
table.Interface {
g.gen_str_for_interface(sym.info, styp, str_fn_name)
}
else { else {
verror("could not generate string method $str_fn_name for type '$styp'") verror("could not generate string method $str_fn_name for type '$styp'")
} }
@ -449,7 +452,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st
fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string { fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string {
has_custom_str := sym.has_method('str') has_custom_str := sym.has_method('str')
if sym.kind == .enum_ { if sym.kind in [.enum_, .interface_] {
return '${fn_name}(it.${c_name(field_name)})' return '${fn_name}(it.${c_name(field_name)})'
} else if sym.kind == .struct_ { } else if sym.kind == .struct_ {
mut obj := 'it.${c_name(field_name)}' mut obj := 'it.${c_name(field_name)}'
@ -506,6 +509,14 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp string, str_fn_name string
g.auto_str_funcs.writeln('}') g.auto_str_funcs.writeln('}')
} }
fn (mut g Gen) gen_str_for_interface(info table.Interface, styp string, str_fn_name string) {
// TODO
g.type_definitions.writeln('static string ${str_fn_name}($styp it); // auto')
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp it) { /* gen_str_for_interface */')
g.auto_str_funcs.writeln('\treturn _SLIT("$styp{ /* TODO: Interface str */ }");')
g.auto_str_funcs.writeln('}')
}
fn (mut g Gen) gen_str_for_union_sum_type(info table.SumType, styp string, str_fn_name string) { fn (mut g Gen) gen_str_for_union_sum_type(info table.SumType, styp string, str_fn_name string) {
mut gen_fn_names := map[string]string{} mut gen_fn_names := map[string]string{}
for typ in info.variants { for typ in info.variants {

View File

@ -5483,7 +5483,7 @@ fn (g &Gen) type_to_fmt(typ table.Type) string {
if typ.is_ptr() && (typ.is_int() || typ.is_float()) { if typ.is_ptr() && (typ.is_int() || typ.is_float()) {
return '%.*s\\000' return '%.*s\\000'
} else if sym.kind in } else if sym.kind in
[.struct_, .array, .array_fixed, .map, .bool, .enum_, .sum_type, .function] { [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type, .function] {
return '%.*s\\000' return '%.*s\\000'
} else if sym.kind == .string { } else if sym.kind == .string {
return "\'%.*s\\000\'" return "\'%.*s\\000\'"

View File

@ -343,7 +343,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype table.Type) {
g.write('")') g.write('")')
} }
} else if sym_has_str_method || sym.kind in } else if sym_has_str_method || sym.kind in
[.array, .array_fixed, .map, .struct_, .multi_return, .sum_type] { [.array, .array_fixed, .map, .struct_, .multi_return, .sum_type, .interface_] {
is_ptr := typ.is_ptr() is_ptr := typ.is_ptr()
str_fn_name := g.gen_str_for_type(typ) str_fn_name := g.gen_str_for_type(typ)
if is_ptr { if is_ptr {