Skip to content

Commit

Permalink
cgen: fix the alias of fixed_array (fix #9537) (#9544)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Mar 31, 2021
1 parent f1797a0 commit 63f835c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
69 changes: 48 additions & 21 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {}
Expand Down
12 changes: 12 additions & 0 deletions 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'
}

0 comments on commit 63f835c

Please sign in to comment.