cgen: fix the alias of fixed_array (fix #9537) (#9544)

pull/9546/head
yuyi 2021-04-01 00:51:03 +08:00 committed by GitHub
parent f1797a0150
commit 63f835c4ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 21 deletions

View File

@ -760,6 +760,32 @@ pub fn (mut g Gen) write_typedef_types() {
.array { .array {
g.type_definitions.writeln('typedef array $typ.cname;') 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_ { .interface_ {
g.write_interface_typesymbol_declaration(typ) g.write_interface_typesymbol_declaration(typ)
} }
@ -5419,6 +5445,8 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) {
g.type_definitions.writeln('') g.type_definitions.writeln('')
} }
table.ArrayFixed { table.ArrayFixed {
elem_sym := g.table.get_type_symbol(typ.info.elem_type)
if !elem_sym.is_builtin() {
// .array_fixed { // .array_fixed {
styp := typ.cname styp := typ.cname
// array_fixed_char_300 => char x[300] // array_fixed_char_300 => char x[300]
@ -5428,8 +5456,6 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) {
if fixed.starts_with('C__') { if fixed.starts_with('C__') {
fixed = fixed[3..] 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 { if elem_sym.info is table.FnType {
pos := g.out.len pos := g.out.len
g.write_fn_ptr_decl(&elem_sym.info, '') g.write_fn_ptr_decl(&elem_sym.info, '')
@ -5442,6 +5468,7 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) {
g.type_definitions.writeln('typedef $fixed $styp [$len];') g.type_definitions.writeln('typedef $fixed $styp [$len];')
} }
} }
}
else {} else {}
} }
} }

View File

@ -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'
}