From 63f835c4cecefce4a4df5e57c535493c3429e7b7 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 1 Apr 2021 00:51:03 +0800 Subject: [PATCH] cgen: fix the alias of fixed_array (fix #9537) (#9544) --- vlib/v/gen/c/cgen.v | 69 +++++++++++++++++++-------- vlib/v/tests/alias_fixed_array_test.v | 12 +++++ 2 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 vlib/v/tests/alias_fixed_array_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 8fb1f8f6c5..13fe401838 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -760,6 +760,32 @@ pub fn (mut g Gen) write_typedef_types() { .array { g.type_definitions.writeln('typedef array $typ.cname;') } + .array_fixed { + info := typ.info as table.ArrayFixed + elem_sym := g.table.get_type_symbol(info.elem_type) + if elem_sym.is_builtin() { + // .array_fixed { + styp := typ.cname + // array_fixed_char_300 => char x[300] + mut fixed := styp[12..] + len := styp.after('_') + fixed = fixed[..fixed.len - len.len - 1] + if fixed.starts_with('C__') { + fixed = fixed[3..] + } + if elem_sym.info is table.FnType { + pos := g.out.len + g.write_fn_ptr_decl(&elem_sym.info, '') + fixed = g.out.after(pos) + g.out.go_back(fixed.len) + mut def_str := 'typedef $fixed;' + def_str = def_str.replace_once('(*)', '(*$styp[$len])') + g.type_definitions.writeln(def_str) + } else { + g.type_definitions.writeln('typedef $fixed $styp [$len];') + } + } + } .interface_ { g.write_interface_typesymbol_declaration(typ) } @@ -5419,27 +5445,28 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) { g.type_definitions.writeln('') } table.ArrayFixed { - // .array_fixed { - styp := typ.cname - // array_fixed_char_300 => char x[300] - mut fixed := styp[12..] - len := styp.after('_') - fixed = fixed[..fixed.len - len.len - 1] - if fixed.starts_with('C__') { - fixed = fixed[3..] - } - elem_type := typ.info.elem_type - elem_sym := g.table.get_type_symbol(elem_type) - if elem_sym.info is table.FnType { - pos := g.out.len - g.write_fn_ptr_decl(&elem_sym.info, '') - fixed = g.out.after(pos) - g.out.go_back(fixed.len) - mut def_str := 'typedef $fixed;' - def_str = def_str.replace_once('(*)', '(*$styp[$len])') - g.type_definitions.writeln(def_str) - } else { - g.type_definitions.writeln('typedef $fixed $styp [$len];') + elem_sym := g.table.get_type_symbol(typ.info.elem_type) + if !elem_sym.is_builtin() { + // .array_fixed { + styp := typ.cname + // array_fixed_char_300 => char x[300] + mut fixed := styp[12..] + len := styp.after('_') + fixed = fixed[..fixed.len - len.len - 1] + if fixed.starts_with('C__') { + fixed = fixed[3..] + } + if elem_sym.info is table.FnType { + pos := g.out.len + g.write_fn_ptr_decl(&elem_sym.info, '') + fixed = g.out.after(pos) + g.out.go_back(fixed.len) + mut def_str := 'typedef $fixed;' + def_str = def_str.replace_once('(*)', '(*$styp[$len])') + g.type_definitions.writeln(def_str) + } else { + g.type_definitions.writeln('typedef $fixed $styp [$len];') + } } } else {} diff --git a/vlib/v/tests/alias_fixed_array_test.v b/vlib/v/tests/alias_fixed_array_test.v new file mode 100644 index 0000000000..f2acb606f3 --- /dev/null +++ b/vlib/v/tests/alias_fixed_array_test.v @@ -0,0 +1,12 @@ +type Block = [8]byte + +fn test_alias_fixed_array() { + a := [8]byte{init: 22} + ret := get(Block(a)) + println(ret) + assert ret == 'Block([22, 22, 22, 22, 22, 22, 22, 22])' +} + +fn get(b Block) string { + return '$b' +}