Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 16, 2024
1 parent a200c45 commit 679a6f6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
12 changes: 4 additions & 8 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
}
}
if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.writeln(' = ${anon_ctx}${c_name(left.name)};')
} else if left_is_auto_deref_var {
styp := g.styp(left_typ).trim('*')
Expand Down Expand Up @@ -1044,8 +1043,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)array_get(')
} else {
styp := g.styp(info.elem_type)
Expand All @@ -1070,8 +1068,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)')
} else {
styp := g.styp(info.elem_type)
Expand All @@ -1098,8 +1095,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if val_typ.kind == .function {
left_type := node.left_types[i]
left_sym := g.table.sym(left_type)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)map_get(')
} else {
g.write('${styp} _var_${left.pos.pos} = *(${styp}*)map_get(')
Expand Down
12 changes: 5 additions & 7 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ pub fn (mut g Gen) write_typedef_types() {
if elem_sym.info is ast.FnType {
pos := g.out.len
// pos2:=g.out_parallel[g.out_idx].len
g.write_fn_ptr_decl(&elem_sym.info, '', true)
g.write_fn_ptr_decl(&elem_sym.info, '')
fixed = g.out.cut_to(pos)
// g.out_parallel[g.out_idx].cut_to(pos2)
mut def_str := 'typedef ${fixed};'
Expand Down Expand Up @@ -2230,7 +2230,7 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
final_ret_sym := g.table.final_sym(ret_typ)
if final_ret_sym.info is ast.FnType {
g.write('(')
g.write_fn_ptr_decl(&final_ret_sym.info, '', false)
g.write_fn_ptr_decl(&final_ret_sym.info, '')
g.write(')')
}
}
Expand Down Expand Up @@ -3201,16 +3201,14 @@ fn cnewlines(s string) string {
return s.replace('\n', r'\n')
}

fn (mut g Gen) write_fn_ptr_decl(func &ast.FnType, ptr_name string, with_argname bool) {
fn (mut g Gen) write_fn_ptr_decl(func &ast.FnType, ptr_name string) {
ret_styp := g.styp(func.func.return_type)
g.write('${ret_styp} (*${ptr_name}) (')
arg_len := func.func.params.len
for i, arg in func.func.params {
arg_styp := g.styp(arg.typ)
g.write(arg_styp)
if with_argname {
g.write(' ${arg.name}')
}
g.write(' ${arg.name}')
if i < arg_len - 1 {
g.write(', ')
}
Expand Down Expand Up @@ -6637,7 +6635,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
if len > 0 {
if elem_sym.info is ast.FnType {
pos := g.out.len
g.write_fn_ptr_decl(&elem_sym.info, '', true)
g.write_fn_ptr_decl(&elem_sym.info, '')
fixed_elem_name = g.out.cut_to(pos)
mut def_str := 'typedef ${fixed_elem_name};'
def_str = def_str.replace_once('(*)', '(*${styp}[${len}])')
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/dumpexpr.v
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn (mut g Gen) dump_expr_definitions() {
fninfo := dump_sym.info as ast.FnType
str_dumparg_type = 'DumpFNType_${name}'
tdef_pos := g.out.len
g.write_fn_ptr_decl(&fninfo, str_dumparg_type, true)
g.write_fn_ptr_decl(&fninfo, str_dumparg_type)
str_tdef := g.out.after(tdef_pos)
g.go_back(str_tdef.len)
dump_typedefs['typedef ${str_tdef};'] = true
Expand Down
9 changes: 3 additions & 6 deletions vlib/v/gen/c/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
if node.val_var != '_' {
if val_sym.kind == .function {
g.write('\t')
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
g.writeln(' = ((voidptr*)${cond_var}${op_field}data)[${i}];')
} else if val_sym.kind == .array_fixed && !node.val_is_mut {
right := '((${styp}*)${cond_var}${op_field}data)[${i}]'
Expand Down Expand Up @@ -323,8 +322,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
is_fixed_array := val_sym.kind == .array_fixed && !node.val_is_mut
if val_sym.kind == .function {
g.write('\t')
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
} else if is_fixed_array {
styp := g.styp(node.val_type)
g.writeln('\t${styp} ${c_name(node.val_var)};')
Expand Down Expand Up @@ -390,8 +388,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
if node.val_var != '_' {
val_sym := g.table.sym(node.val_type)
if val_sym.kind == .function {
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
g.write(' = (*(voidptr*)')
g.writeln('DenseArray_value(&${cond_var}${dot_or_ptr}key_values, ${idx}));')
} else if val_sym.kind == .array_fixed && !node.val_is_mut {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
if is_fn_index_call {
if elem_sym.info is ast.FnType {
g.write('((')
g.write_fn_ptr_decl(&elem_sym.info, '', true)
g.write_fn_ptr_decl(&elem_sym.info, '')
if is_direct_array_access {
g.write(')((${elem_type_str}*)')
} else {
Expand Down Expand Up @@ -484,7 +484,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
if g.is_fn_index_call {
if val_sym.info is ast.FnType {
g.write('((')
g.write_fn_ptr_decl(&val_sym.info, '', true)
g.write_fn_ptr_decl(&val_sym.info, '')
g.write(')(*(voidptr*)map_get(')
is_fn_last_index_call = true
g.is_fn_index_call = false
Expand Down

0 comments on commit 679a6f6

Please sign in to comment.