diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 0b023061fa9dfb..629f4e080c3b1a 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -138,7 +138,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st mut tmp_var := '' if need_tmp_var { tmp_var = g.new_tmp_var() - stmt_str = g.go_before_stmt(0) + stmt_str = g.go_before_last_stmt() g.empty_line = true ret_typ := g.typ(node.typ) @@ -376,7 +376,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.write('}[0])') } else if needs_more_defaults { tmp := g.new_tmp_var() - line := g.go_before_stmt(0).trim_space() + line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('${elem_styp}* ${tmp} = (${elem_styp}*) _v_malloc((') diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 59ceb54dbea487..e899929044247a 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1924,7 +1924,7 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T panic('cgen: parameter `ret_typ` of function `expr_with_tmp_var()` must be an Option or Result') } - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() mut styp := g.base_type(ret_typ) g.empty_line = true @@ -2468,7 +2468,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ fname := g.get_sumtype_casting_fn(unwrapped_got_type, unwrapped_expected_type) if expr is ast.ArrayInit && got_sym.kind == .array_fixed { - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() g.empty_line = true tmp_var := g.new_tmp_var() g.write('${got_styp} ${tmp_var} = ') @@ -3362,7 +3362,7 @@ fn (mut g Gen) expr(node_ ast.Expr) { g.expr(node.expr) g.write(')') } else if node.op == .question { - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() mut expr_str := '' if mut node.expr is ast.ComptimeSelector && node.expr.left is ast.Ident { // val.$(field.name)? @@ -3403,7 +3403,7 @@ fn (mut g Gen) expr(node_ ast.Expr) { elem_type := right_inf.elem_type is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) line } else { @@ -3602,7 +3602,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { if node.or_block.kind != .absent && !g.is_assign_lhs && g.table.sym(node.typ).kind != .chan { is_ptr := sym.kind in [.interface_, .sum_type] - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() styp := g.typ(g.unwrap_generic(node.typ)) g.empty_line = true tmp_var := g.new_tmp_var() @@ -3935,7 +3935,7 @@ fn (mut g Gen) lock_expr(node ast.LockExpr) { mut cur_line := '' if node.is_expr { styp := g.typ(node.typ) - cur_line = g.go_before_stmt(0) + cur_line = g.go_before_last_stmt() g.writeln('${styp} ${tmp_result};') } mut mtxs := '' @@ -4109,7 +4109,7 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) { is_expr := node.is_expr || g.inside_ternary > 0 cur_line := if is_expr { g.empty_line = true - g.go_before_stmt(0) + g.go_before_last_stmt() } else { '' } @@ -4324,7 +4324,7 @@ fn (mut g Gen) ident(node ast.Ident) { } if node.or_expr.kind != .absent && !(g.inside_opt_or_res && g.inside_assign && !g.is_assign_lhs) { - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() g.empty_line = true var_name := if !g.is_assign_lhs && is_auto_heap { '(*${name})' } else { name } g.or_block(var_name, node.or_expr, comptime_type) @@ -4368,7 +4368,7 @@ fn (mut g Gen) ident(node ast.Ident) { } if node.or_expr.kind != .absent && !(g.inside_opt_or_res && g.inside_assign && !g.is_assign_lhs) { - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() g.empty_line = true var_name := if !g.is_assign_lhs && is_auto_heap { '(*${name})' @@ -4465,7 +4465,7 @@ fn (mut g Gen) ident(node ast.Ident) { } } if node.or_expr.kind != .absent && !(g.inside_opt_or_res && g.inside_assign && !g.is_assign_lhs) { - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() g.empty_line = true var_opt := if is_auto_heap { '(*${name})' } else { name } g.or_block(var_opt, node.or_expr, node.obj.typ) @@ -4528,7 +4528,7 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { } else if node.typ.has_flag(.option) { if sym.kind == .alias { if (sym.info as ast.Alias).parent_type.has_flag(.option) { - cur_stmt := g.go_before_stmt(0) + cur_stmt := g.go_before_last_stmt() g.empty_line = true parent_type := (sym.info as ast.Alias).parent_type tmp_var := g.new_tmp_var() @@ -4970,18 +4970,18 @@ fn (mut g Gen) return_stmt(node ast.Return) { mut tmp := g.new_tmp_var() if !call_expr.return_type.has_flag(.option) && !call_expr.return_type.has_flag(.result) { - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() expr_styp := g.typ(call_expr.return_type) g.write('${expr_styp} ${tmp}=') g.expr(expr) g.writeln(';') - multi_unpack += g.go_before_stmt(0) + multi_unpack += g.go_before_last_stmt() g.write(line) } else { - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.tmp_count-- g.expr(expr) - multi_unpack += g.go_before_stmt(0) + multi_unpack += g.go_before_last_stmt() g.write(line) expr_styp := g.base_type(call_expr.return_type) tmp = ('(*(${expr_styp}*)${tmp}.data)') diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 72ea10277f356a..7491fd3fc31625 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -94,7 +94,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { mut cur_line := '' if !is_html { - cur_line = g.go_before_stmt(0) + cur_line = g.go_before_last_stmt() } for stmt in node.vweb_tmpl.stmts { @@ -239,7 +239,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { if node.or_block.kind != .absent && (m.return_type.has_flag(.option) || m.return_type.has_flag(.result)) { if !g.inside_assign { - cur_line := g.go_before_stmt(0) + cur_line := g.go_before_last_stmt() tmp_var := g.new_tmp_var() g.write('${g.typ(m.return_type)} ${tmp_var} = ') g.write(cur_line) @@ -326,7 +326,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { tmp_var := g.new_tmp_var() is_opt_or_result := node.typ.has_flag(.option) || node.typ.has_flag(.result) line := if node.is_expr { - stmt_str := g.go_before_stmt(0) + stmt_str := g.go_before_last_stmt() g.write(util.tabs(g.indent)) styp := g.typ(node.typ) g.writeln('${styp} ${tmp_var};') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 825206734177a5..2824457bc43f1f 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -691,7 +691,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { tmp_var := g.new_tmp_var() fn_type := g.fn_var_signature(node.left.decl.return_type, node.left.decl.params.map(it.typ), tmp_var) - line := g.go_before_stmt(0).trim_space() + line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('${fn_type} = ') g.expr(node.left) @@ -713,7 +713,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { g.inside_curry_call = true ret_typ := node.return_type - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.empty_line = true tmp_res := g.new_tmp_var() @@ -741,7 +741,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { mut cur_line := if !g.inside_curry_call && (is_gen_or_and_assign_rhs || gen_keep_alive) { // && !g.is_autofree { // `x := foo() or { ...}` // cut everything that has been generated to prepend option variable creation - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) line } else { @@ -767,7 +767,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } mut styp := g.typ(ret_typ) if gen_or && !is_gen_or_and_assign_rhs { - cur_line = g.go_before_stmt(0) + cur_line = g.go_before_last_stmt() } if gen_or && g.infix_left_var_name.len > 0 { g.writeln('${styp} ${tmp_opt};') @@ -1561,7 +1561,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { g.is_json_fn = true json_obj = g.new_tmp_var() mut tmp2 := '' - cur_line := g.go_before_stmt(0) + cur_line := g.go_before_last_stmt() if is_json_encode || is_json_encode_pretty { g.gen_json_for_type(node.args[0].typ) json_type_str = g.typ(node.args[0].typ) @@ -1803,7 +1803,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { } else { if node.is_keep_alive && g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt] { - cur_line := g.go_before_stmt(0) + cur_line := g.go_before_last_stmt() tmp_cnt_save = g.keep_alive_call_pregen(node) g.write(cur_line) for i in 0 .. node.args.len { diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 6e807e1a6026db..a8a754ef427d0a 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -201,7 +201,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { g.inside_if_result = true styp = styp.replace('*', '_ptr') } - cur_line = g.go_before_stmt(0) + cur_line = g.go_before_last_stmt() g.empty_line = true g.writeln('${styp} ${tmp}; /* if prepend */') if g.infix_left_var_name.len > 0 { @@ -340,7 +340,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { } else { if i == 0 && node.branches.len > 1 && !needs_tmp_var && needs_conds_order { cond_var_name := g.new_tmp_var() - line := g.go_before_stmt(0).trim_space() + line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('bool ${cond_var_name} = ') g.expr(branch.cond) @@ -351,7 +351,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { g.writeln('if (${cond_var_name}) {') } else if i > 0 && branch_cond_var_names.len > 0 && !needs_tmp_var && needs_conds_order { cond_var_name := g.new_tmp_var() - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.empty_line = true g.writeln('bool ${cond_var_name};') branch_cond := branch_cond_var_names.join(' || ') diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 99795f0815e29a..64dbd6bfca6bd4 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -21,7 +21,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { gen_or := node.or_expr.kind != .absent || node.is_option if gen_or { tmp_opt := g.new_tmp_var() - cur_line := g.go_before_stmt(0) + cur_line := g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) opt_elem_type := g.typ(ast.u8_type.set_flag(.option)) g.write('${opt_elem_type} ${tmp_opt} = string_at_with_check(') @@ -77,7 +77,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) { } else { if gen_or { tmp_opt = g.new_tmp_var() - cur_line = g.go_before_stmt(0) + cur_line = g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) opt_elem_type := g.typ(ast.string_type.set_flag(.result)) g.write('${opt_elem_type} ${tmp_opt} = string_substr_with_check(') @@ -129,7 +129,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) { } if node.left is ast.ArrayInit { var := g.new_tmp_var() - line := g.go_before_stmt(0).trim_space() + line := g.go_before_last_stmt().trim_space() styp := g.typ(node.left_type) g.empty_line = true g.write('${styp} ${var} = ') @@ -266,7 +266,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { && !g.is_assign_lhs is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) line } else { @@ -470,7 +470,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { zero := g.type_default(info.value_type) is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() g.out.write_string(util.tabs(g.indent)) line } else { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index e18b269313b88d..597446a5689a17 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -755,7 +755,7 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) { mut right_var := '' if node.right is ast.Ident && node.right.or_expr.kind != .absent { - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() right_var = g.new_tmp_var() g.write('${g.typ(right.typ)} ${right_var} = ') g.op_arg(node.right, method.params[1].typ, right.typ) @@ -910,7 +910,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { g.inside_ternary = 0 if g.need_tmp_var_in_if(node.right) { tmp := g.new_tmp_var() - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('bool ${tmp} = (') g.expr(node.left) @@ -930,7 +930,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { g.inside_ternary = 0 if g.need_tmp_var_in_match(node.right) { tmp := g.new_tmp_var() - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('bool ${tmp} = (') g.expr(node.left) @@ -947,7 +947,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { } else if g.need_tmp_var_in_array_call(node.right) { // `if a == 0 || arr.any(it.is_letter()) {...}` tmp := g.new_tmp_var() - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() g.empty_line = true if g.infix_left_var_name.len > 0 { g.write('bool ${tmp} = ((${g.infix_left_var_name}) ${node.op.str()} ') @@ -967,7 +967,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { prev_inside_ternary := g.inside_ternary g.inside_ternary = 0 tmp := g.new_tmp_var() - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('bool ${tmp} = (') g.expr(node.left) @@ -988,7 +988,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { prev_inside_ternary := g.inside_ternary g.inside_ternary = 0 tmp := g.new_tmp_var() - cur_line := g.go_before_stmt(0).trim_space() + cur_line := g.go_before_last_stmt().trim_space() g.empty_line = true g.write('bool ${tmp} = (') g.expr(node.left) @@ -1014,7 +1014,7 @@ fn (mut g Gen) gen_is_none_check(node ast.InfixExpr) { g.inside_opt_or_res = old_inside_opt_or_res g.write('.state') } else { - stmt_str := g.go_before_stmt(0).trim_space() + stmt_str := g.go_before_last_stmt().trim_space() g.empty_line = true left_var := g.expr_with_opt(node.left, node.left_type, node.left_type) g.writeln(';') diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index c1b5c9a5fd8726..3b9aa3a17e57f7 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -76,7 +76,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { } else { line := if is_expr { g.empty_line = true - g.go_before_stmt(0) + g.go_before_last_stmt() } else { '' } @@ -89,7 +89,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { } if need_tmp_var { g.empty_line = true - cur_line = g.go_before_stmt(0).trim_left(' \t') + cur_line = g.go_before_last_stmt().trim_left(' \t') tmp_var = g.new_tmp_var() mut func_decl := '' if g.table.final_sym(node.return_type).kind == .function { diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index e720edf088dd9d..991841c8834e44 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -29,7 +29,7 @@ enum SqlExprSide { // from the database, which is used by the `select` query. fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { // users := - left := g.go_before_stmt(0) + left := g.go_before_last_stmt() connection_var_name := g.new_tmp_var() g.writeln('') diff --git a/vlib/v/gen/c/past_tmp_var.v b/vlib/v/gen/c/past_tmp_var.v index ebd7c08007c1cc..3a489d7d9a0037 100644 --- a/vlib/v/gen/c/past_tmp_var.v +++ b/vlib/v/gen/c/past_tmp_var.v @@ -10,7 +10,7 @@ mut: fn (mut g Gen) past_tmp_var_new() PastTmpVar { tmp_var := g.new_tmp_var() - mut s := g.go_before_stmt(0) + mut s := g.go_before_last_stmt() s_ends_with_ln := s.ends_with('\n') s = s.trim_space() g.empty_line = true @@ -27,7 +27,7 @@ fn (mut g Gen) past_tmp_var_from_var_name(var_name string) PastTmpVar { if var_name.len != 0 { tmp_var = var_name } else { - s = g.go_before_stmt(0) + s = g.go_before_last_stmt() } s_ends_with_ln := s.ends_with('\n') s = s.trim_space() diff --git a/vlib/v/gen/c/spawn_and_go.v b/vlib/v/gen/c/spawn_and_go.v index 7c620e0f886de7..a1de7fe779307b 100644 --- a/vlib/v/gen/c/spawn_and_go.v +++ b/vlib/v/gen/c/spawn_and_go.v @@ -19,7 +19,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) { } else { g.writeln('/*go (coroutine) */') } - line := g.go_before_stmt(0) + line := g.go_before_last_stmt() mut handle := '' tmp := g.new_tmp_var() mut expr := node.call_expr diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index b5819f2023b3db..770d3cb03e1f0f 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -14,7 +14,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) { is_update_tmp_var = true tmp_update_var = g.new_tmp_var() - s := g.go_before_stmt(0) + s := g.go_before_last_stmt() g.empty_line = true styp := g.typ(node.update_expr_type) diff --git a/vlib/v/gen/c/text_manipulation.v b/vlib/v/gen/c/text_manipulation.v index f0bba212d1705c..f37d00fa1945fe 100644 --- a/vlib/v/gen/c/text_manipulation.v +++ b/vlib/v/gen/c/text_manipulation.v @@ -62,19 +62,18 @@ fn (mut g Gen) set_current_pos_as_last_stmt_pos() { g.stmt_path_pos << g.out.len } -fn (mut g Gen) go_before_stmt(n int) string { - stmt_pos := g.nth_stmt_pos(n) - // g.out_parallel[g.out_idx].cut_to(stmt_pos) - return g.out.cut_to(stmt_pos) +[inline] +fn (mut g Gen) go_before_last_stmt() string { + return g.out.cut_to(g.nth_stmt_pos(0)) } [inline] fn (mut g Gen) go_before_ternary() string { - return g.go_before_stmt(g.inside_ternary) + return g.out.cut_to(g.nth_stmt_pos(g.inside_ternary)) } fn (mut g Gen) insert_before_stmt(s string) { - cur_line := g.go_before_stmt(g.inside_ternary) + cur_line := g.out.cut_to(g.nth_stmt_pos(g.inside_ternary)) g.writeln(s) g.write(cur_line) }