From d094baf1076b33dd6562bb40fb62db93443cc3c6 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Tue, 29 Dec 2020 20:25:38 +1100 Subject: [PATCH] cgen: add temp (TODO) auto str for interfaces --- vlib/v/gen/auto_str_methods.v | 13 ++++++++++++- vlib/v/gen/cgen.v | 2 +- vlib/v/gen/str.v | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/auto_str_methods.v b/vlib/v/gen/auto_str_methods.v index 945474ec3e..d7cf3f76c0 100644 --- a/vlib/v/gen/auto_str_methods.v +++ b/vlib/v/gen/auto_str_methods.v @@ -55,6 +55,9 @@ fn (mut g Gen) gen_str_for_type(typ table.Type) string { table.SumType { 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 { 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 { 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)})' } else if sym.kind == .struct_ { 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('}') } +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) { mut gen_fn_names := map[string]string{} for typ in info.variants { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 8b95fba826..1382188f8c 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -5483,7 +5483,7 @@ fn (g &Gen) type_to_fmt(typ table.Type) string { if typ.is_ptr() && (typ.is_int() || typ.is_float()) { return '%.*s\\000' } 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' } else if sym.kind == .string { return "\'%.*s\\000\'" diff --git a/vlib/v/gen/str.v b/vlib/v/gen/str.v index 80b882aeff..c0c3abc938 100644 --- a/vlib/v/gen/str.v +++ b/vlib/v/gen/str.v @@ -343,7 +343,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype table.Type) { g.write('")') } } 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() str_fn_name := g.gen_str_for_type(typ) if is_ptr {