Skip to content

Commit

Permalink
cgen: cleanup go_before_stmt(0) (#19308)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 8, 2023
1 parent 68cbf27 commit 3a2994b
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 54 deletions.
4 changes: 2 additions & 2 deletions vlib/v/gen/c/array.v
Expand Up @@ -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)
Expand Down Expand Up @@ -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((')
Expand Down
30 changes: 15 additions & 15 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -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

Expand Down Expand Up @@ -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} = ')
Expand Down Expand Up @@ -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)?
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 := ''
Expand Down Expand Up @@ -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 {
''
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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})'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)')
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/gen/c/comptime.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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};')
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/gen/c/fn.v
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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};')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/gen/c/if.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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(' || ')
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/gen/c/index.v
Expand Up @@ -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(')
Expand Down Expand Up @@ -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(')
Expand Down Expand Up @@ -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} = ')
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions vlib/v/gen/c/infix.v
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()} ')
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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(';')
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/match.v
Expand Up @@ -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 {
''
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/orm.v
Expand Up @@ -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('')
Expand Down

0 comments on commit 3a2994b

Please sign in to comment.