1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix fixed array of function (#8490)

This commit is contained in:
yuyi 2021-02-01 21:50:10 +08:00 committed by GitHub
parent 8755f40430
commit 53a5aad855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 9 deletions

View File

@ -1694,6 +1694,12 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
if elem_typ.info is table.FnType { if elem_typ.info is table.FnType {
return elem_typ.info.func.return_type return elem_typ.info.func.return_type
} }
} else if sym.kind == .array_fixed {
info := sym.info as table.ArrayFixed
elem_typ := c.table.get_type_symbol(info.elem_type)
if elem_typ.info is table.FnType {
return elem_typ.info.func.return_type
}
} }
found = true found = true
return table.string_type return table.string_type

View File

@ -4198,6 +4198,28 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('\n$cur_line*($elem_type_str*)${tmp_opt}.data') g.write('\n$cur_line*($elem_type_str*)${tmp_opt}.data')
} }
} }
} else if sym.kind == .array_fixed {
info := sym.info as table.ArrayFixed
elem_type := info.elem_type
elem_sym := g.table.get_type_symbol(elem_type)
is_fn_index_call := g.is_fn_index_call && elem_sym.info is table.FnType
if is_fn_index_call {
g.write('(*')
}
if node.left_type.is_ptr() {
g.write('(*')
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
g.write('[')
g.expr(node.index)
g.write(']')
if is_fn_index_call {
g.write(')')
}
} else if sym.kind == .map { } else if sym.kind == .map {
info := sym.info as table.Map info := sym.info as table.Map
key_type_str := g.typ(info.key_type) key_type_str := g.typ(info.key_type)
@ -4328,13 +4350,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.expr(node.index) g.expr(node.index)
g.write(')') g.write(')')
} else { } else {
if sym.kind == .array_fixed && node.left_type.is_ptr() { g.expr(node.left)
g.write('(*')
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
g.write('[') g.write('[')
g.expr(node.index) g.expr(node.index)
g.write(']') g.write(']')
@ -5140,8 +5156,19 @@ 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..]
} }
g.type_definitions.writeln('typedef $fixed $styp [$len];') 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];')
}
} }
else {} else {}
} }

View File

@ -0,0 +1,9 @@
fn foo(a string) int {
return 10 + a.len
}
fn test_fixed_array_fn_index() {
a := [foo]!
println(a[0]('hello'))
assert a[0]('hello') == 15
}