From 051cc732bb5352f9710dc72e0a646acecd340b0d Mon Sep 17 00:00:00 2001 From: Enzo Baldisserri Date: Thu, 16 Apr 2020 21:04:27 +0200 Subject: [PATCH] cgen: generate typeof for functions --- cmd/tools/vtest-fixed.v | 1 - vlib/v/gen/cgen.v | 19 +++++++++++++++++-- vlib/v/table/table.v | 7 +++++++ vlib/v/tests/typeof_simple_types_test.v | 4 ++++ vlib/v/tests/typeof_test.v | 11 ++++++++--- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index 6547340872..29c11101b6 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -32,7 +32,6 @@ const ( 'vlib/v/tests/string_interpolation_struct_test.v', 'vlib/v/tests/string_interpolation_variadic_test.v', 'vlib/v/tests/type_test.v', - 'vlib/v/tests/typeof_test.v', 'vlib/v/tests/valgrind/valgrind_test.v', // ubuntu-musl only 'vlib/v/tests/pointers_str_test.v', 'vlib/net/http/cookie_test.v', diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index cff9fb987a..9940443718 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1103,8 +1103,23 @@ fn (var g Gen) typeof_expr(node ast.TypeOf) { g.write(').typ ))') } else if sym.kind == .array_fixed { fixed_info := sym.info as table.ArrayFixed - elem_sym := g.table.get_type_symbol(fixed_info.elem_type) - g.write('tos3("[$fixed_info.size]${elem_sym.name}")') + typ_name := g.table.get_type_name(fixed_info.elem_type) + g.write('tos3("[$fixed_info.size]${typ_name}")') + } else if sym.kind == .function { + info := sym.info as table.FnType + fn_info := info.func + mut repr := 'fn (' + for i, arg in fn_info.args { + if i > 0 { + repr += ', ' + } + repr += g.table.get_type_name(arg.typ) + } + repr += ')' + if fn_info.return_type != table.void_type { + repr += ' ${g.table.get_type_name(fn_info.return_type)}' + } + g.write('tos3("$repr")') } else { g.write('tos3("${sym.name}")') } diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 3d3d69beca..a04e4b084b 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -204,6 +204,13 @@ pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol { panic('get_type_symbol: invalid type (typ=$typ idx=${idx}). This should never happen') } +[inline] +pub fn (t &Table) get_type_name(typ Type) string { + typ_sym := t.get_type_symbol(typ) + return typ_sym.name +} + + // this will override or register builtin type // allows prexisitng types added in register_builtins // to be overriden with their real type info diff --git a/vlib/v/tests/typeof_simple_types_test.v b/vlib/v/tests/typeof_simple_types_test.v index e1f2a8701d..8b06d8390b 100644 --- a/vlib/v/tests/typeof_simple_types_test.v +++ b/vlib/v/tests/typeof_simple_types_test.v @@ -17,8 +17,12 @@ type AnotherSumType = XxYyZz | int type SuperSumType = MySumType | AnotherSumType | string fn test_typeof_for_builtin_int_types() { + assert typeof(i8(1)) == 'i8' + assert typeof(i16(1)) == 'i16' assert typeof(1) == 'int' assert typeof(i64(1)) == 'i64' + assert typeof(byte(1)) == 'byte' + assert typeof(u16(1)) == 'u16' assert typeof(u32(1)) == 'u32' assert typeof(u64(1)) == 'u64' } diff --git a/vlib/v/tests/typeof_test.v b/vlib/v/tests/typeof_test.v index 796e02f9aa..f1d7563f78 100644 --- a/vlib/v/tests/typeof_test.v +++ b/vlib/v/tests/typeof_test.v @@ -73,15 +73,20 @@ fn test_typeof_on_sumtypes_of_structs() { assert typeof(d) == 'UnaryExpr' } -type MyFn fn(int) int -type MyFn2 fn() - fn myfn(i int) int { return i } fn myfn2() {} +fn myfn3(i int, s string) byte { + return byte(0) +} +fn myfn4() i8 { + return -1 +} fn test_typeof_on_fn() { assert typeof(myfn) == 'fn (int) int' assert typeof(myfn2) == 'fn ()' + assert typeof(myfn3) == 'fn (int, string) byte' + assert typeof(myfn4) == 'fn () i8' }