mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: cleanup in fn.v (#14938)
This commit is contained in:
parent
e18c5c7908
commit
8b8667dd9a
@ -52,15 +52,7 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
|
|||||||
mut skip := false
|
mut skip := false
|
||||||
pos := g.out.len
|
pos := g.out.len
|
||||||
should_bundle_module := util.should_bundle_module(node.mod)
|
should_bundle_module := util.should_bundle_module(node.mod)
|
||||||
/*
|
|
||||||
if node.name.contains('i_error') {
|
|
||||||
println(g.table.type_str(node.params[0].typ))
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if g.pref.build_mode == .build_module {
|
if g.pref.build_mode == .build_module {
|
||||||
// if node.name.contains('parse_text') {
|
|
||||||
// println('!!! $node.name mod=$node.mod, built=$g.module_built')
|
|
||||||
// }
|
|
||||||
// TODO true for not just "builtin"
|
// TODO true for not just "builtin"
|
||||||
// TODO: clean this up
|
// TODO: clean this up
|
||||||
mod := if g.is_builtin_mod { 'builtin' } else { node.name.all_before_last('.') }
|
mod := if g.is_builtin_mod { 'builtin' } else { node.name.all_before_last('.') }
|
||||||
@ -120,7 +112,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
// as it's only informative, comment it for now
|
// as it's only informative, comment it for now
|
||||||
// g.gen_attrs(it.attrs)
|
// g.gen_attrs(it.attrs)
|
||||||
if node.language == .c {
|
if node.language == .c {
|
||||||
// || node.no_body {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +146,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
g.returned_var_name = ''
|
g.returned_var_name = ''
|
||||||
//
|
|
||||||
old_g_autofree := g.is_autofree
|
old_g_autofree := g.is_autofree
|
||||||
if node.is_manualfree {
|
if node.is_manualfree {
|
||||||
g.is_autofree = false
|
g.is_autofree = false
|
||||||
@ -163,11 +153,8 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
defer {
|
defer {
|
||||||
g.is_autofree = old_g_autofree
|
g.is_autofree = old_g_autofree
|
||||||
}
|
}
|
||||||
//
|
if node.generic_names.len > 0 && g.cur_concrete_types.len == 0 {
|
||||||
// if g.fileis('vweb.v') {
|
// need the cur_concrete_type check to avoid inf. recursion
|
||||||
// println('\ngen_fn_decl() $node.name $node.is_generic $g.cur_generic_type')
|
|
||||||
// }
|
|
||||||
if node.generic_names.len > 0 && g.cur_concrete_types.len == 0 { // need the cur_concrete_type check to avoid inf. recursion
|
|
||||||
// loop thru each generic type and generate a function
|
// loop thru each generic type and generate a function
|
||||||
nkey := node.fkey()
|
nkey := node.fkey()
|
||||||
generic_types_by_fn := g.table.fn_generic_types[nkey]
|
generic_types_by_fn := g.table.fn_generic_types[nkey]
|
||||||
@ -215,7 +202,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
if is_livefn && !is_livemode {
|
if is_livefn && !is_livemode {
|
||||||
eprintln('INFO: compile with `v -live $g.pref.path `, if you want to use the [live] function $node.name .')
|
eprintln('INFO: compile with `v -live $g.pref.path `, if you want to use the [live] function $node.name .')
|
||||||
}
|
}
|
||||||
//
|
|
||||||
mut name := g.c_fn_name(node) or { return }
|
mut name := g.c_fn_name(node) or { return }
|
||||||
mut type_name := g.typ(g.unwrap_generic(node.return_type))
|
mut type_name := g.typ(g.unwrap_generic(node.return_type))
|
||||||
|
|
||||||
@ -231,10 +218,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
panic('cgen: fn_decl: obf name "$key" not found, this should never happen')
|
panic('cgen: fn_decl: obf name "$key" not found, this should never happen')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if g.pref.show_cc && it.is_builtin {
|
|
||||||
// println(name)
|
|
||||||
// }
|
|
||||||
// type_name := g.ast.Type_to_str(it.return_type)
|
|
||||||
// Live functions are protected by a mutex, because otherwise they
|
// Live functions are protected by a mutex, because otherwise they
|
||||||
// can be changed by the live reload thread, *while* they are
|
// can be changed by the live reload thread, *while* they are
|
||||||
// running, with unpredictable results (usually just crashing).
|
// running, with unpredictable results (usually just crashing).
|
||||||
@ -268,7 +251,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
// Private functions need to marked as static so that they are not exportable in the
|
// Private functions need to marked as static so that they are not exportable in the
|
||||||
// binaries
|
// binaries
|
||||||
if g.pref.build_mode != .build_module && !g.pref.use_cache {
|
if g.pref.build_mode != .build_module && !g.pref.use_cache {
|
||||||
// if !(g.pref.build_mode == .build_module && g.is_builtin_mod) {
|
|
||||||
// If we are building vlib/builtin, we need all private functions like array_get
|
// If we are building vlib/builtin, we need all private functions like array_get
|
||||||
// to be public, so that all V programs can access them.
|
// to be public, so that all V programs can access them.
|
||||||
g.write('VV_LOCAL_SYMBOL ')
|
g.write('VV_LOCAL_SYMBOL ')
|
||||||
@ -297,7 +279,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
if node.no_body || ((g.pref.use_cache && g.pref.build_mode != .build_module) && node.is_builtin
|
if node.no_body || ((g.pref.use_cache && g.pref.build_mode != .build_module) && node.is_builtin
|
||||||
&& !g.pref.is_test) || skip {
|
&& !g.pref.is_test) || skip {
|
||||||
// Just a function header. Builtin function bodies are defined in builtin.o
|
// Just a function header. Builtin function bodies are defined in builtin.o
|
||||||
g.definitions.writeln(');') // // NO BODY')
|
g.definitions.writeln(');') // NO BODY')
|
||||||
g.writeln(');')
|
g.writeln(');')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -395,14 +377,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
|
|||||||
default_expr := g.type_default(node.return_type)
|
default_expr := g.type_default(node.return_type)
|
||||||
// TODO: perf?
|
// TODO: perf?
|
||||||
if default_expr == '{0}' {
|
if default_expr == '{0}' {
|
||||||
// if node.return_type.idx() == 1 && node.return_type.has_flag(.optional) {
|
|
||||||
// // The default return for anonymous functions that return `?,
|
|
||||||
// // should have .ok = true set, otherwise calling them with
|
|
||||||
// // optfn() or { panic(err) } will cause a panic:
|
|
||||||
// g.writeln('\treturn (Option_void){0};')
|
|
||||||
// } else {
|
|
||||||
g.writeln('\treturn ($type_name)$default_expr;')
|
g.writeln('\treturn ($type_name)$default_expr;')
|
||||||
// }
|
|
||||||
} else {
|
} else {
|
||||||
g.writeln('\treturn $default_expr;')
|
g.writeln('\treturn $default_expr;')
|
||||||
}
|
}
|
||||||
@ -437,7 +412,6 @@ fn (mut g Gen) c_fn_name(node &ast.FnDecl) ?string {
|
|||||||
return none
|
return none
|
||||||
}
|
}
|
||||||
name = g.cc_type(node.receiver.typ, false) + '_' + name
|
name = g.cc_type(node.receiver.typ, false) + '_' + name
|
||||||
// name = g.table.sym(node.receiver.typ).name + '_' + name
|
|
||||||
}
|
}
|
||||||
if node.language == .c {
|
if node.language == .c {
|
||||||
name = util.no_dots(name)
|
name = util.no_dots(name)
|
||||||
@ -615,7 +589,6 @@ fn (mut g Gen) get_anon_fn_type_name(mut node ast.AnonFn, var_name string) strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) call_expr(node ast.CallExpr) {
|
fn (mut g Gen) call_expr(node ast.CallExpr) {
|
||||||
// g.write('/*call expr*/')
|
|
||||||
// NOTE: everything could be done this way
|
// NOTE: everything could be done this way
|
||||||
// see my comment in parser near anon_fn
|
// see my comment in parser near anon_fn
|
||||||
if node.left is ast.AnonFn {
|
if node.left is ast.AnonFn {
|
||||||
@ -643,7 +616,6 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
|||||||
// cut everything that has been generated to prepend optional variable creation
|
// cut everything that has been generated to prepend optional variable creation
|
||||||
line := g.go_before_stmt(0)
|
line := g.go_before_stmt(0)
|
||||||
g.out.write_string(util.tabs(g.indent))
|
g.out.write_string(util.tabs(g.indent))
|
||||||
// g.write('/*is_gen_or_and_assign_rhs*/')
|
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
''
|
''
|
||||||
@ -668,10 +640,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
|||||||
} else {
|
} else {
|
||||||
g.fn_call(node)
|
g.fn_call(node)
|
||||||
}
|
}
|
||||||
if gen_or { // && !g.autofree {
|
if gen_or {
|
||||||
// if !g.is_autofree {
|
|
||||||
g.or_block(tmp_opt, node.or_block, node.return_type)
|
g.or_block(tmp_opt, node.or_block, node.return_type)
|
||||||
//}
|
|
||||||
unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result)
|
unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result)
|
||||||
unwrapped_styp := g.typ(unwrapped_typ)
|
unwrapped_styp := g.typ(unwrapped_typ)
|
||||||
if unwrapped_typ == ast.void_type {
|
if unwrapped_typ == ast.void_type {
|
||||||
@ -932,7 +902,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if node.left is ast.None {
|
} else if node.left is ast.None {
|
||||||
// none.str()
|
|
||||||
g.gen_expr_to_string(node.left, ast.none_type)
|
g.gen_expr_to_string(node.left, ast.none_type)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1013,11 +982,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||||||
name = g.generic_fn_name(node.concrete_types, name, false)
|
name = g.generic_fn_name(node.concrete_types, name, false)
|
||||||
// TODO2
|
// TODO2
|
||||||
// g.generate_tmp_autofree_arg_vars(node, name)
|
// g.generate_tmp_autofree_arg_vars(node, name)
|
||||||
//
|
|
||||||
// if node.receiver_type != 0 {
|
|
||||||
// g.write('/*${g.typ(node.receiver_type)}*/')
|
|
||||||
// g.write('/*expr_type=${g.typ(node.left_type)} rec type=${g.typ(node.receiver_type)}*/')
|
|
||||||
// }
|
|
||||||
if !node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str' {
|
if !node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str' {
|
||||||
g.write('ptr_str(')
|
g.write('ptr_str(')
|
||||||
} else if node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str'
|
} else if node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str'
|
||||||
@ -1065,10 +1029,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if node.left_type.idx() != node.receiver_type.idx() {
|
|
||||||
// println('${g.typ(node.left_type)} ${g.typ(node.receiver_type)}')
|
|
||||||
// }
|
|
||||||
|
|
||||||
if g.is_autofree && node.free_receiver && !g.inside_lambda && !g.is_builtin_mod {
|
if g.is_autofree && node.free_receiver && !g.inside_lambda && !g.is_builtin_mod {
|
||||||
// The receiver expression needs to be freed, use the temp var.
|
// The receiver expression needs to be freed, use the temp var.
|
||||||
fn_name := node.name.replace('.', '_')
|
fn_name := node.name.replace('.', '_')
|
||||||
@ -1113,18 +1073,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||||||
if node.args.len > 0 || is_variadic {
|
if node.args.len > 0 || is_variadic {
|
||||||
g.write(', ')
|
g.write(', ')
|
||||||
}
|
}
|
||||||
// /////////
|
|
||||||
/*
|
|
||||||
if name.contains('subkeys') {
|
|
||||||
println('call_args $name $node.arg_types.len')
|
|
||||||
for t in node.arg_types {
|
|
||||||
sym := g.table.sym(t)
|
|
||||||
print('$sym.name ')
|
|
||||||
}
|
|
||||||
println('')
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
// ///////
|
|
||||||
g.call_args(node)
|
g.call_args(node)
|
||||||
if array_depth >= 0 {
|
if array_depth >= 0 {
|
||||||
g.write(', $array_depth')
|
g.write(', $array_depth')
|
||||||
@ -1191,7 +1139,6 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||||||
g.gen_json_for_type(node.args[0].typ)
|
g.gen_json_for_type(node.args[0].typ)
|
||||||
json_type_str = g.typ(node.args[0].typ)
|
json_type_str = g.typ(node.args[0].typ)
|
||||||
// `json__encode` => `json__encode_User`
|
// `json__encode` => `json__encode_User`
|
||||||
// encode_name := c_name(name) + '_' + util.no_dots(json_type_str)
|
|
||||||
encode_name := js_enc_name(json_type_str)
|
encode_name := js_enc_name(json_type_str)
|
||||||
g.empty_line = true
|
g.empty_line = true
|
||||||
g.writeln('// json.encode')
|
g.writeln('// json.encode')
|
||||||
@ -1294,7 +1241,6 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||||||
if g.is_autofree && !typ.has_flag(.optional) {
|
if g.is_autofree && !typ.has_flag(.optional) {
|
||||||
// Create a temporary variable so that the value can be freed
|
// Create a temporary variable so that the value can be freed
|
||||||
tmp := g.new_tmp_var()
|
tmp := g.new_tmp_var()
|
||||||
// tmps << tmp
|
|
||||||
g.write('string $tmp = ')
|
g.write('string $tmp = ')
|
||||||
g.gen_expr_to_string(expr, typ)
|
g.gen_expr_to_string(expr, typ)
|
||||||
g.writeln('; ${c_name(print_method)}($tmp); string_free(&$tmp);')
|
g.writeln('; ${c_name(print_method)}($tmp); string_free(&$tmp);')
|
||||||
@ -1439,7 +1385,6 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
args << node.args
|
args << node.args
|
||||||
// for i, arg in node.args {
|
|
||||||
for i, arg in args {
|
for i, arg in args {
|
||||||
if !arg.is_tmp_autofree {
|
if !arg.is_tmp_autofree {
|
||||||
continue
|
continue
|
||||||
@ -1450,11 +1395,8 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
|
|||||||
g.autofree_call_pregen(arg.expr)
|
g.autofree_call_pregen(arg.expr)
|
||||||
}
|
}
|
||||||
free_tmp_arg_vars = true
|
free_tmp_arg_vars = true
|
||||||
// t := g.new_tmp_var() + '_arg_expr_${name}_$i'
|
|
||||||
fn_name := node.name.replace('.', '_') // can't use name...
|
fn_name := node.name.replace('.', '_') // can't use name...
|
||||||
// t := '_tt${g.tmp_count_af}_arg_expr_${fn_name}_$i'
|
|
||||||
t := '_arg_expr_${fn_name}_${i}_$node.pos.pos'
|
t := '_arg_expr_${fn_name}_${i}_$node.pos.pos'
|
||||||
// g.called_fn_name = name
|
|
||||||
used := false // scope.known_var(t)
|
used := false // scope.known_var(t)
|
||||||
mut s := '$t = '
|
mut s := '$t = '
|
||||||
if used {
|
if used {
|
||||||
@ -1479,9 +1421,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
|
|||||||
})
|
})
|
||||||
s = 'string $t = '
|
s = 'string $t = '
|
||||||
}
|
}
|
||||||
// g.expr(arg.expr)
|
|
||||||
s += g.expr_string(arg.expr)
|
s += g.expr_string(arg.expr)
|
||||||
// g.writeln(';// new af pre')
|
|
||||||
s += ';// new af2 pre'
|
s += ';// new af2 pre'
|
||||||
g.strs_to_free0 << s
|
g.strs_to_free0 << s
|
||||||
// This tmp arg var will be freed with the rest of the vars at the end of the scope.
|
// This tmp arg var will be freed with the rest of the vars at the end of the scope.
|
||||||
@ -1614,7 +1554,6 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
|
|||||||
if use_tmp_var_autofree {
|
if use_tmp_var_autofree {
|
||||||
// TODO copypasta, move to an inline fn
|
// TODO copypasta, move to an inline fn
|
||||||
fn_name := node.name.replace('.', '_')
|
fn_name := node.name.replace('.', '_')
|
||||||
// name := '_tt${g.tmp_count_af}_arg_expr_${fn_name}_$i'
|
|
||||||
name := '_arg_expr_${fn_name}_${i + 1}_$node.pos.pos'
|
name := '_arg_expr_${fn_name}_${i + 1}_$node.pos.pos'
|
||||||
g.write('/*af arg2*/' + name)
|
g.write('/*af arg2*/' + name)
|
||||||
} else {
|
} else {
|
||||||
@ -1661,7 +1600,6 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
|
|||||||
if (g.pref.translated || g.file.is_translated) && args.len == 1 {
|
if (g.pref.translated || g.file.is_translated) && args.len == 1 {
|
||||||
// Handle `foo(c'str')` for `fn foo(args ...&u8)`
|
// Handle `foo(c'str')` for `fn foo(args ...&u8)`
|
||||||
// TODOC2V handle this in a better place
|
// TODOC2V handle this in a better place
|
||||||
// println(g.table.type_to_str(args[0].typ))
|
|
||||||
g.expr(args[0].expr)
|
g.expr(args[0].expr)
|
||||||
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose {
|
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose {
|
||||||
g.expr(args[args.len - 1].expr)
|
g.expr(args[args.len - 1].expr)
|
||||||
@ -1741,12 +1679,6 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
|
|||||||
g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));')
|
g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));')
|
||||||
if expr.is_method {
|
if expr.is_method {
|
||||||
g.write('$arg_tmp_var->arg0 = ')
|
g.write('$arg_tmp_var->arg0 = ')
|
||||||
// TODO is this needed?
|
|
||||||
/*
|
|
||||||
if false && !expr.return_type.is_ptr() {
|
|
||||||
g.write('&')
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
g.expr(expr.left)
|
g.expr(expr.left)
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
}
|
}
|
||||||
@ -1974,7 +1906,6 @@ fn (mut g Gen) keep_alive_call_pregen(node ast.CallExpr) int {
|
|||||||
expected_type := node.expected_arg_types[i]
|
expected_type := node.expected_arg_types[i]
|
||||||
typ := g.table.sym(expected_type).cname
|
typ := g.table.sym(expected_type).cname
|
||||||
g.write('$typ __tmp_arg_${tmp_cnt_save + i} = ')
|
g.write('$typ __tmp_arg_${tmp_cnt_save + i} = ')
|
||||||
// g.expr(arg.expr)
|
|
||||||
g.ref_or_deref_arg(arg, expected_type, node.language)
|
g.ref_or_deref_arg(arg, expected_type, node.language)
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user