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

cgen: add fn_var_signature(info ast.FnType, var_name string) string (#15433)

This commit is contained in:
yuyi 2022-08-16 17:57:33 +08:00 committed by GitHub
parent 3bc01d696d
commit 66a3cdec6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 22 deletions

View File

@ -344,17 +344,8 @@ fn (mut g Gen) gen_map_equality_fn(left_type ast.Type) string {
kind := g.table.type_kind(value.typ)
if kind == .function {
func := value.sym.info as ast.FnType
ret_styp := g.typ(func.func.return_type)
fn_builder.write_string('\t\t$ret_styp (*v) (')
arg_len := func.func.params.len
for j, arg in func.func.params {
arg_styp := g.typ(arg.typ)
fn_builder.write_string('$arg_styp $arg.name')
if j < arg_len - 1 {
fn_builder.write_string(', ')
}
}
fn_builder.writeln(') = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });')
sig := g.fn_var_signature(func, 'v')
fn_builder.writeln('\t\t$sig = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });')
} else {
fn_builder.writeln('\t\t$ptr_value_styp v = *($ptr_value_styp*)map_get(&a, k, &($ptr_value_styp[]){ 0 });')
}

View File

@ -475,17 +475,8 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) {
for var in node.inherited_vars {
var_sym := g.table.sym(var.typ)
if var_sym.info is ast.FnType {
ret_styp := g.typ(var_sym.info.func.return_type)
builder.write_string('\t$ret_styp (*$var.name) (')
arg_len := var_sym.info.func.params.len
for j, arg in var_sym.info.func.params {
arg_styp := g.typ(arg.typ)
builder.write_string('$arg_styp $arg.name')
if j < arg_len - 1 {
builder.write_string(', ')
}
}
builder.writeln(');')
sig := g.fn_var_signature(var_sym.info, var.name)
builder.writeln('\t' + sig + ';')
} else {
styp := g.typ(var.typ)
builder.writeln('\t$styp $var.name;')

View File

@ -61,3 +61,18 @@ fn (mut g Gen) unwrap(typ ast.Type) Type {
unaliased_sym: g.table.sym(no_generic_sym.parent_idx)
}
}
// 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 {
ret_styp := g.typ(info.func.return_type)
mut sig := '$ret_styp (*$var_name) ('
for j, arg in info.func.params {
arg_styp := g.typ(arg.typ)
sig += '$arg_styp $arg.name'
if j < info.func.params.len - 1 {
sig += ', '
}
}
sig += ')'
return sig
}