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

cgen: fix fn_var_signature() when param type is function (#15436)

This commit is contained in:
yuyi 2022-08-17 01:21:58 +08:00 committed by GitHub
parent dc37386bcc
commit b08f500c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 15 deletions

View File

@ -155,12 +155,8 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
} }
// if it's a decl assign (`:=`) or a blank assignment `_ =`/`_ :=` then generate `void (*ident) (args) =` // if it's a decl assign (`:=`) or a blank assignment `_ =`/`_ :=` then generate `void (*ident) (args) =`
if (is_decl || blank_assign) && left is ast.Ident { if (is_decl || blank_assign) && left is ast.Ident {
ret_styp := g.typ(val.decl.return_type) sig := g.fn_var_signature(val.decl.return_type, val.decl.params, ident.name)
g.write('$ret_styp (*$ident.name) (') g.write(sig + ' = ')
def_pos := g.definitions.len
g.fn_decl_params(val.decl.params, unsafe { nil }, false)
g.definitions.go_back(g.definitions.len - def_pos)
g.write(') = ')
} else { } else {
g.is_assign_lhs = true g.is_assign_lhs = true
g.assign_op = node.op g.assign_op = node.op

View File

@ -343,8 +343,8 @@ fn (mut g Gen) gen_map_equality_fn(left_type ast.Type) string {
fn_builder.writeln('\t\tif (!map_exists(&b, k)) return false;') fn_builder.writeln('\t\tif (!map_exists(&b, k)) return false;')
kind := g.table.type_kind(value.typ) kind := g.table.type_kind(value.typ)
if kind == .function { if kind == .function {
func := value.sym.info as ast.FnType info := value.sym.info as ast.FnType
sig := g.fn_var_signature(func, 'v') sig := g.fn_var_signature(info.func.return_type, info.func.params, 'v')
fn_builder.writeln('\t\t$sig = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });') fn_builder.writeln('\t\t$sig = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });')
} else { } else {
fn_builder.writeln('\t\t$ptr_value_styp v = *($ptr_value_styp*)map_get(&a, k, &($ptr_value_styp[]){ 0 });') fn_builder.writeln('\t\t$ptr_value_styp v = *($ptr_value_styp*)map_get(&a, k, &($ptr_value_styp[]){ 0 });')

View File

@ -475,7 +475,8 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) {
for var in node.inherited_vars { for var in node.inherited_vars {
var_sym := g.table.sym(var.typ) var_sym := g.table.sym(var.typ)
if var_sym.info is ast.FnType { if var_sym.info is ast.FnType {
sig := g.fn_var_signature(var_sym.info, var.name) sig := g.fn_var_signature(var_sym.info.func.return_type, var_sym.info.func.params,
var.name)
builder.writeln('\t' + sig + ';') builder.writeln('\t' + sig + ';')
} else { } else {
styp := g.typ(var.typ) styp := g.typ(var.typ)

View File

@ -63,13 +63,20 @@ fn (mut g Gen) unwrap(typ ast.Type) Type {
} }
// generate function variable definition, e.g. `void (*var_name) (int, string)` // generate function variable definition, e.g. `void (*var_name) (int, string)`
fn (mut g Gen) fn_var_signature(info ast.FnType, var_name string) string { fn (mut g Gen) fn_var_signature(return_type ast.Type, params []ast.Param, var_name string) string {
ret_styp := g.typ(info.func.return_type) ret_styp := g.typ(return_type)
mut sig := '$ret_styp (*$var_name) (' mut sig := '$ret_styp (*$var_name) ('
for j, arg in info.func.params { for j, arg in params {
arg_styp := g.typ(arg.typ) arg_sym := g.table.sym(arg.typ)
sig += '$arg_styp $arg.name' if arg_sym.info is ast.FnType {
if j < info.func.params.len - 1 { arg_sig := g.fn_var_signature(arg_sym.info.func.return_type, arg_sym.info.func.params,
'')
sig += arg_sig
} else {
arg_styp := g.typ(arg.typ)
sig += '$arg_styp'
}
if j < params.len - 1 {
sig += ', ' sig += ', '
} }
} }

View File

@ -0,0 +1,13 @@
fn test_anon_fn_decl_with_anon_fn_params() {
a := fn (p1 fn () int, p2 string) string {
n := p1()
return '$n' + p2
}
ret := a(aaa, 'hello')
println(ret)
assert ret == '22hello'
}
fn aaa() int {
return 22
}