1
0
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:
yuyi 2022-07-05 11:40:57 +08:00 committed by GitHub
parent e18c5c7908
commit 8b8667dd9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,15 +52,7 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
mut skip := false
pos := g.out.len
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 node.name.contains('parse_text') {
// println('!!! $node.name mod=$node.mod, built=$g.module_built')
// }
// TODO true for not just "builtin"
// TODO: clean this up
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
// g.gen_attrs(it.attrs)
if node.language == .c {
// || node.no_body {
return
}
@ -155,7 +146,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
*/
g.returned_var_name = ''
//
old_g_autofree := g.is_autofree
if node.is_manualfree {
g.is_autofree = false
@ -163,11 +153,8 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
defer {
g.is_autofree = old_g_autofree
}
//
// if g.fileis('vweb.v') {
// 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
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
nkey := node.fkey()
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 {
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 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')
}
}
// 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
// can be changed by the live reload thread, *while* they are
// 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
// binaries
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
// to be public, so that all V programs can access them.
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
&& !g.pref.is_test) || skip {
// Just a function header. Builtin function bodies are defined in builtin.o
g.definitions.writeln(');') // // NO BODY')
g.definitions.writeln(');') // NO BODY')
g.writeln(');')
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)
// TODO: perf?
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;')
// }
} else {
g.writeln('\treturn $default_expr;')
}
@ -437,7 +412,6 @@ fn (mut g Gen) c_fn_name(node &ast.FnDecl) ?string {
return none
}
name = g.cc_type(node.receiver.typ, false) + '_' + name
// name = g.table.sym(node.receiver.typ).name + '_' + name
}
if node.language == .c {
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) {
// g.write('/*call expr*/')
// NOTE: everything could be done this way
// see my comment in parser near anon_fn
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
line := g.go_before_stmt(0)
g.out.write_string(util.tabs(g.indent))
// g.write('/*is_gen_or_and_assign_rhs*/')
line
} else {
''
@ -668,10 +640,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
} else {
g.fn_call(node)
}
if gen_or { // && !g.autofree {
// if !g.is_autofree {
if gen_or {
g.or_block(tmp_opt, node.or_block, node.return_type)
//}
unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result)
unwrapped_styp := g.typ(unwrapped_typ)
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 {
// none.str()
g.gen_expr_to_string(node.left, ast.none_type)
return
}
@ -1013,11 +982,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
name = g.generic_fn_name(node.concrete_types, name, false)
// TODO2
// 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' {
g.write('ptr_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 {
// The receiver expression needs to be freed, use the temp var.
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 {
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)
if array_depth >= 0 {
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)
json_type_str = g.typ(node.args[0].typ)
// `json__encode` => `json__encode_User`
// encode_name := c_name(name) + '_' + util.no_dots(json_type_str)
encode_name := js_enc_name(json_type_str)
g.empty_line = true
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) {
// Create a temporary variable so that the value can be freed
tmp := g.new_tmp_var()
// tmps << tmp
g.write('string $tmp = ')
g.gen_expr_to_string(expr, typ)
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
// for i, arg in node.args {
for i, arg in args {
if !arg.is_tmp_autofree {
continue
@ -1450,11 +1395,8 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
g.autofree_call_pregen(arg.expr)
}
free_tmp_arg_vars = true
// t := g.new_tmp_var() + '_arg_expr_${name}_$i'
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'
// g.called_fn_name = name
used := false // scope.known_var(t)
mut s := '$t = '
if used {
@ -1479,9 +1421,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
})
s = 'string $t = '
}
// g.expr(arg.expr)
s += g.expr_string(arg.expr)
// g.writeln(';// new af pre')
s += ';// new af2 pre'
g.strs_to_free0 << s
// 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 {
// TODO copypasta, move to an inline fn
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'
g.write('/*af arg2*/' + name)
} 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 {
// Handle `foo(c'str')` for `fn foo(args ...&u8)`
// TODOC2V handle this in a better place
// println(g.table.type_to_str(args[0].typ))
g.expr(args[0].expr)
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose {
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));')
if expr.is_method {
g.write('$arg_tmp_var->arg0 = ')
// TODO is this needed?
/*
if false && !expr.return_type.is_ptr() {
g.write('&')
}
*/
g.expr(expr.left)
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]
typ := g.table.sym(expected_type).cname
g.write('$typ __tmp_arg_${tmp_cnt_save + i} = ')
// g.expr(arg.expr)
g.ref_or_deref_arg(arg, expected_type, node.language)
g.writeln(';')
}