From 4a12546971254f7f68a95c12578788b56d211e68 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 19 Mar 2021 22:49:46 +0200 Subject: [PATCH] cgen,fmt: improve sizeof(), fix `type PPType = &&Type` --- vlib/v/fmt/fmt.v | 17 ++++---- vlib/v/gen/c/cgen.v | 22 ++++++----- .../tests/type_alias_of_pointer_types_test.v | 39 +++++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 vlib/v/tests/type_alias_of_pointer_types_test.v diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index db76147507..3e9f818eec 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2186,16 +2186,7 @@ pub fn (mut f Fmt) selector_expr(node ast.SelectorExpr) { pub fn (mut f Fmt) size_of(node ast.SizeOf) { f.write('sizeof(') if node.is_type { - sym := f.table.get_type_symbol(node.typ) - if sym.name != '' { - if f.is_external_name(sym.name) { - f.write(sym.name) - } else { - f.write(f.short_module(sym.name)) - } - } else { - f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) - } + f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) } else { f.expr(node.expr) } @@ -2350,3 +2341,9 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(node ast.Expr) { f.expr(node) } } + +fn (mut f Fmt) trace(fbase string, message string) { + if f.file.path_base == fbase { + println('> f.trace | ${fbase:-10s} | $message') + } +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index cb333112d4..4170fa2e49 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -727,13 +727,13 @@ fn (g &Gen) type_sidx(t table.Type) string { // pub fn (mut g Gen) write_typedef_types() { - for typ in g.table.types { + for typ in g.table.type_symbols { if typ.name in c.builtins { continue } match typ.kind { .alias { - parent := unsafe { &g.table.types[typ.parent_idx] } + parent := unsafe { &g.table.type_symbols[typ.parent_idx] } is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.` mut is_typedef := false if parent.info is table.Struct { @@ -746,6 +746,10 @@ pub fn (mut g Gen) write_typedef_types() { } else { parent_styp = parent.cname[3..] } + } else { + if typ.info is table.Alias { + parent_styp = g.typ(typ.info.parent_type) + } } g.type_definitions.writeln('typedef $parent_styp $typ.cname;') } @@ -822,7 +826,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym table.TypeSymbol) { pub fn (mut g Gen) write_multi_return_types() { g.typedefs.writeln('\n// BEGIN_multi_return_typedefs') g.type_definitions.writeln('\n// BEGIN_multi_return_structs') - for sym in g.table.types { + for sym in g.table.type_symbols { if sym.kind != .multi_return { continue } @@ -2745,9 +2749,9 @@ fn (mut g Gen) map_fn_ptrs(key_typ table.TypeSymbol) (string, string, string, st } .voidptr { ts := if g.pref.m64 { - &g.table.types[table.u64_type_idx] + &g.table.type_symbols[table.u64_type_idx] } else { - &g.table.types[table.u32_type_idx] + &g.table.type_symbols[table.u32_type_idx] } return g.map_fn_ptrs(ts) } @@ -5233,7 +5237,7 @@ fn (mut g Gen) write_builtin_types() { // builtin types need to be on top // everything except builtin will get sorted for builtin_name in c.builtins { - sym := g.table.types[g.table.type_idxs[builtin_name]] + sym := g.table.type_symbols[g.table.type_idxs[builtin_name]] if sym.kind == .interface_ { g.write_interface_typesymbol_declaration(sym) } else { @@ -5248,7 +5252,7 @@ fn (mut g Gen) write_builtin_types() { // are added before them. fn (mut g Gen) write_sorted_types() { mut types := []table.TypeSymbol{} // structs that need to be sorted - for typ in g.table.types { + for typ in g.table.type_symbols { if typ.name !in c.builtins { types << typ } @@ -5447,7 +5451,7 @@ fn (g &Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol { // sort types mut types_sorted := []table.TypeSymbol{} for node in dep_graph_sorted.nodes { - types_sorted << g.table.types[g.table.type_idxs[node.name]] + types_sorted << g.table.type_symbols[g.table.type_idxs[node.name]] } return types_sorted } @@ -6026,7 +6030,7 @@ fn (mut g Gen) is_expr(node ast.InfixExpr) { // Generates interface table and interface indexes fn (mut g Gen) interface_table() string { mut sb := strings.new_builder(100) - for ityp in g.table.types { + for ityp in g.table.type_symbols { if ityp.kind != .interface_ { continue } diff --git a/vlib/v/tests/type_alias_of_pointer_types_test.v b/vlib/v/tests/type_alias_of_pointer_types_test.v new file mode 100644 index 0000000000..0ae00ec9f8 --- /dev/null +++ b/vlib/v/tests/type_alias_of_pointer_types_test.v @@ -0,0 +1,39 @@ +struct MyStructInt { + x int +} + +type ZZInt = int +type ZZMyStructInt = MyStructInt + +type PZZInt = &int +type PZZMyStructInt = &MyStructInt + +type PPZZInt = &&int +type PPZZMyStructInt = &&MyStructInt + +type PPPZZInt = &&&int +type PPPZZMyStructInt = &&&MyStructInt + +fn test_alias_of_pointer_types() { + size_of_int := int(sizeof(int)) + size_of_int_ptr := int(sizeof(&int)) + dump(size_of_int) + dump(size_of_int_ptr) + eprintln('--------------------------') + dump(sizeof(ZZInt)) + dump(sizeof(ZZMyStructInt)) + dump(sizeof(PZZInt)) + dump(sizeof(PZZMyStructInt)) + dump(sizeof(PPZZInt)) + dump(sizeof(PPZZMyStructInt)) + dump(sizeof(PPPZZInt)) + dump(sizeof(PPPZZMyStructInt)) + // + assert sizeof(ZZInt) == sizeof(int) + assert sizeof(ZZMyStructInt) == sizeof(int) + // + assert sizeof(PZZInt) == sizeof(voidptr) + assert sizeof(PZZMyStructInt) == sizeof(voidptr) + assert sizeof(PPZZInt) == sizeof(voidptr) + assert sizeof(PPZZMyStructInt) == sizeof(voidptr) +}