Skip to content

Commit 8b8667d

Browse files
authored
cgen: cleanup in fn.v (#14938)
1 parent e18c5c7 commit 8b8667d

File tree

1 file changed

+5
-74
lines changed

1 file changed

+5
-74
lines changed

vlib/v/gen/c/fn.v

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
5252
mut skip := false
5353
pos := g.out.len
5454
should_bundle_module := util.should_bundle_module(node.mod)
55-
/*
56-
if node.name.contains('i_error') {
57-
println(g.table.type_str(node.params[0].typ))
58-
}
59-
*/
6055
if g.pref.build_mode == .build_module {
61-
// if node.name.contains('parse_text') {
62-
// println('!!! $node.name mod=$node.mod, built=$g.module_built')
63-
// }
6456
// TODO true for not just "builtin"
6557
// TODO: clean this up
6658
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) {
120112
// as it's only informative, comment it for now
121113
// g.gen_attrs(it.attrs)
122114
if node.language == .c {
123-
// || node.no_body {
124115
return
125116
}
126117

@@ -155,19 +146,15 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
155146
*/
156147

157148
g.returned_var_name = ''
158-
//
159149
old_g_autofree := g.is_autofree
160150
if node.is_manualfree {
161151
g.is_autofree = false
162152
}
163153
defer {
164154
g.is_autofree = old_g_autofree
165155
}
166-
//
167-
// if g.fileis('vweb.v') {
168-
// println('\ngen_fn_decl() $node.name $node.is_generic $g.cur_generic_type')
169-
// }
170-
if node.generic_names.len > 0 && g.cur_concrete_types.len == 0 { // need the cur_concrete_type check to avoid inf. recursion
156+
if node.generic_names.len > 0 && g.cur_concrete_types.len == 0 {
157+
// need the cur_concrete_type check to avoid inf. recursion
171158
// loop thru each generic type and generate a function
172159
nkey := node.fkey()
173160
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) {
215202
if is_livefn && !is_livemode {
216203
eprintln('INFO: compile with `v -live $g.pref.path `, if you want to use the [live] function $node.name .')
217204
}
218-
//
205+
219206
mut name := g.c_fn_name(node) or { return }
220207
mut type_name := g.typ(g.unwrap_generic(node.return_type))
221208

@@ -231,10 +218,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
231218
panic('cgen: fn_decl: obf name "$key" not found, this should never happen')
232219
}
233220
}
234-
// if g.pref.show_cc && it.is_builtin {
235-
// println(name)
236-
// }
237-
// type_name := g.ast.Type_to_str(it.return_type)
238221
// Live functions are protected by a mutex, because otherwise they
239222
// can be changed by the live reload thread, *while* they are
240223
// running, with unpredictable results (usually just crashing).
@@ -268,7 +251,6 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
268251
// Private functions need to marked as static so that they are not exportable in the
269252
// binaries
270253
if g.pref.build_mode != .build_module && !g.pref.use_cache {
271-
// if !(g.pref.build_mode == .build_module && g.is_builtin_mod) {
272254
// If we are building vlib/builtin, we need all private functions like array_get
273255
// to be public, so that all V programs can access them.
274256
g.write('VV_LOCAL_SYMBOL ')
@@ -297,7 +279,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
297279
if node.no_body || ((g.pref.use_cache && g.pref.build_mode != .build_module) && node.is_builtin
298280
&& !g.pref.is_test) || skip {
299281
// Just a function header. Builtin function bodies are defined in builtin.o
300-
g.definitions.writeln(');') // // NO BODY')
282+
g.definitions.writeln(');') // NO BODY')
301283
g.writeln(');')
302284
return
303285
}
@@ -395,14 +377,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
395377
default_expr := g.type_default(node.return_type)
396378
// TODO: perf?
397379
if default_expr == '{0}' {
398-
// if node.return_type.idx() == 1 && node.return_type.has_flag(.optional) {
399-
// // The default return for anonymous functions that return `?,
400-
// // should have .ok = true set, otherwise calling them with
401-
// // optfn() or { panic(err) } will cause a panic:
402-
// g.writeln('\treturn (Option_void){0};')
403-
// } else {
404380
g.writeln('\treturn ($type_name)$default_expr;')
405-
// }
406381
} else {
407382
g.writeln('\treturn $default_expr;')
408383
}
@@ -437,7 +412,6 @@ fn (mut g Gen) c_fn_name(node &ast.FnDecl) ?string {
437412
return none
438413
}
439414
name = g.cc_type(node.receiver.typ, false) + '_' + name
440-
// name = g.table.sym(node.receiver.typ).name + '_' + name
441415
}
442416
if node.language == .c {
443417
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
615589
}
616590

617591
fn (mut g Gen) call_expr(node ast.CallExpr) {
618-
// g.write('/*call expr*/')
619592
// NOTE: everything could be done this way
620593
// see my comment in parser near anon_fn
621594
if node.left is ast.AnonFn {
@@ -643,7 +616,6 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
643616
// cut everything that has been generated to prepend optional variable creation
644617
line := g.go_before_stmt(0)
645618
g.out.write_string(util.tabs(g.indent))
646-
// g.write('/*is_gen_or_and_assign_rhs*/')
647619
line
648620
} else {
649621
''
@@ -668,10 +640,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
668640
} else {
669641
g.fn_call(node)
670642
}
671-
if gen_or { // && !g.autofree {
672-
// if !g.is_autofree {
643+
if gen_or {
673644
g.or_block(tmp_opt, node.or_block, node.return_type)
674-
//}
675645
unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result)
676646
unwrapped_styp := g.typ(unwrapped_typ)
677647
if unwrapped_typ == ast.void_type {
@@ -932,7 +902,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
932902
}
933903
}
934904
} else if node.left is ast.None {
935-
// none.str()
936905
g.gen_expr_to_string(node.left, ast.none_type)
937906
return
938907
}
@@ -1013,11 +982,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
1013982
name = g.generic_fn_name(node.concrete_types, name, false)
1014983
// TODO2
1015984
// g.generate_tmp_autofree_arg_vars(node, name)
1016-
//
1017-
// if node.receiver_type != 0 {
1018-
// g.write('/*${g.typ(node.receiver_type)}*/')
1019-
// g.write('/*expr_type=${g.typ(node.left_type)} rec type=${g.typ(node.receiver_type)}*/')
1020-
// }
1021985
if !node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str' {
1022986
g.write('ptr_str(')
1023987
} 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) {
10651029
}
10661030
}
10671031

1068-
// if node.left_type.idx() != node.receiver_type.idx() {
1069-
// println('${g.typ(node.left_type)} ${g.typ(node.receiver_type)}')
1070-
// }
1071-
10721032
if g.is_autofree && node.free_receiver && !g.inside_lambda && !g.is_builtin_mod {
10731033
// The receiver expression needs to be freed, use the temp var.
10741034
fn_name := node.name.replace('.', '_')
@@ -1113,18 +1073,6 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
11131073
if node.args.len > 0 || is_variadic {
11141074
g.write(', ')
11151075
}
1116-
// /////////
1117-
/*
1118-
if name.contains('subkeys') {
1119-
println('call_args $name $node.arg_types.len')
1120-
for t in node.arg_types {
1121-
sym := g.table.sym(t)
1122-
print('$sym.name ')
1123-
}
1124-
println('')
1125-
}
1126-
*/
1127-
// ///////
11281076
g.call_args(node)
11291077
if array_depth >= 0 {
11301078
g.write(', $array_depth')
@@ -1191,7 +1139,6 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
11911139
g.gen_json_for_type(node.args[0].typ)
11921140
json_type_str = g.typ(node.args[0].typ)
11931141
// `json__encode` => `json__encode_User`
1194-
// encode_name := c_name(name) + '_' + util.no_dots(json_type_str)
11951142
encode_name := js_enc_name(json_type_str)
11961143
g.empty_line = true
11971144
g.writeln('// json.encode')
@@ -1294,7 +1241,6 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
12941241
if g.is_autofree && !typ.has_flag(.optional) {
12951242
// Create a temporary variable so that the value can be freed
12961243
tmp := g.new_tmp_var()
1297-
// tmps << tmp
12981244
g.write('string $tmp = ')
12991245
g.gen_expr_to_string(expr, typ)
13001246
g.writeln('; ${c_name(print_method)}($tmp); string_free(&$tmp);')
@@ -1439,7 +1385,6 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
14391385
},
14401386
]
14411387
args << node.args
1442-
// for i, arg in node.args {
14431388
for i, arg in args {
14441389
if !arg.is_tmp_autofree {
14451390
continue
@@ -1450,11 +1395,8 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
14501395
g.autofree_call_pregen(arg.expr)
14511396
}
14521397
free_tmp_arg_vars = true
1453-
// t := g.new_tmp_var() + '_arg_expr_${name}_$i'
14541398
fn_name := node.name.replace('.', '_') // can't use name...
1455-
// t := '_tt${g.tmp_count_af}_arg_expr_${fn_name}_$i'
14561399
t := '_arg_expr_${fn_name}_${i}_$node.pos.pos'
1457-
// g.called_fn_name = name
14581400
used := false // scope.known_var(t)
14591401
mut s := '$t = '
14601402
if used {
@@ -1479,9 +1421,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
14791421
})
14801422
s = 'string $t = '
14811423
}
1482-
// g.expr(arg.expr)
14831424
s += g.expr_string(arg.expr)
1484-
// g.writeln(';// new af pre')
14851425
s += ';// new af2 pre'
14861426
g.strs_to_free0 << s
14871427
// 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) {
16141554
if use_tmp_var_autofree {
16151555
// TODO copypasta, move to an inline fn
16161556
fn_name := node.name.replace('.', '_')
1617-
// name := '_tt${g.tmp_count_af}_arg_expr_${fn_name}_$i'
16181557
name := '_arg_expr_${fn_name}_${i + 1}_$node.pos.pos'
16191558
g.write('/*af arg2*/' + name)
16201559
} else {
@@ -1661,7 +1600,6 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
16611600
if (g.pref.translated || g.file.is_translated) && args.len == 1 {
16621601
// Handle `foo(c'str')` for `fn foo(args ...&u8)`
16631602
// TODOC2V handle this in a better place
1664-
// println(g.table.type_to_str(args[0].typ))
16651603
g.expr(args[0].expr)
16661604
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose {
16671605
g.expr(args[args.len - 1].expr)
@@ -1741,12 +1679,6 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
17411679
g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));')
17421680
if expr.is_method {
17431681
g.write('$arg_tmp_var->arg0 = ')
1744-
// TODO is this needed?
1745-
/*
1746-
if false && !expr.return_type.is_ptr() {
1747-
g.write('&')
1748-
}
1749-
*/
17501682
g.expr(expr.left)
17511683
g.writeln(';')
17521684
}
@@ -1974,7 +1906,6 @@ fn (mut g Gen) keep_alive_call_pregen(node ast.CallExpr) int {
19741906
expected_type := node.expected_arg_types[i]
19751907
typ := g.table.sym(expected_type).cname
19761908
g.write('$typ __tmp_arg_${tmp_cnt_save + i} = ')
1977-
// g.expr(arg.expr)
19781909
g.ref_or_deref_arg(arg, expected_type, node.language)
19791910
g.writeln(';')
19801911
}

0 commit comments

Comments
 (0)