diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 4680a944350bce..9daccad34a4cf7 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -39,34 +39,21 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { return true } match expr { - ast.Ident { - return expr.or_expr.kind != .absent - } - ast.StringInterLiteral { - for e in expr.exprs { - if g.need_tmp_var_in_expr(e) { - return true - } - } - } - ast.IfExpr { - if g.need_tmp_var_in_if(expr) { + ast.ArrayInit { + if g.need_tmp_var_in_expr(expr.len_expr) { return true } - } - ast.IfGuardExpr { - return true - } - ast.InfixExpr { - if g.need_tmp_var_in_expr(expr.left) { + if g.need_tmp_var_in_expr(expr.cap_expr) { return true } - if g.need_tmp_var_in_expr(expr.right) { + if g.need_tmp_var_in_expr(expr.init_expr) { return true } - } - ast.MatchExpr { - return true + for elem_expr in expr.exprs { + if g.need_tmp_var_in_expr(elem_expr) { + return true + } + } } ast.CallExpr { if expr.is_method { @@ -90,9 +77,6 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { ast.CastExpr { return g.need_tmp_var_in_expr(expr.expr) } - ast.ParExpr { - return g.need_tmp_var_in_expr(expr.expr) - } ast.ConcatExpr { for val in expr.vals { if val is ast.CallExpr { @@ -102,6 +86,17 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { } } } + ast.Ident { + return expr.or_expr.kind != .absent + } + ast.IfExpr { + if g.need_tmp_var_in_if(expr) { + return true + } + } + ast.IfGuardExpr { + return true + } ast.IndexExpr { if expr.or_expr.kind != .absent { return true @@ -110,21 +105,13 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { return true } } - ast.ArrayInit { - if g.need_tmp_var_in_expr(expr.len_expr) { - return true - } - if g.need_tmp_var_in_expr(expr.cap_expr) { + ast.InfixExpr { + if g.need_tmp_var_in_expr(expr.left) { return true } - if g.need_tmp_var_in_expr(expr.init_expr) { + if g.need_tmp_var_in_expr(expr.right) { return true } - for elem_expr in expr.exprs { - if g.need_tmp_var_in_expr(elem_expr) { - return true - } - } } ast.MapInit { for key in expr.keys { @@ -138,6 +125,28 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { } } } + ast.MatchExpr { + return true + } + ast.ParExpr { + return g.need_tmp_var_in_expr(expr.expr) + } + ast.PrefixExpr { + return g.need_tmp_var_in_expr(expr.right) + } + ast.SelectorExpr { + if g.need_tmp_var_in_expr(expr.expr) { + return true + } + return expr.or_block.kind != .absent + } + ast.StringInterLiteral { + for e in expr.exprs { + if g.need_tmp_var_in_expr(e) { + return true + } + } + } ast.StructInit { if g.need_tmp_var_in_expr(expr.update_expr) { return true @@ -148,12 +157,6 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { } } } - ast.SelectorExpr { - if g.need_tmp_var_in_expr(expr.expr) { - return true - } - return expr.or_block.kind != .absent - } else {} } return false diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 529039b85b557a..9f4f0a239791c7 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -959,47 +959,7 @@ fn (mut g Gen) need_tmp_var_in_array_call(node ast.Expr) bool { // infix_expr_and_or_op generates code for `&&` and `||` fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { - if node.right is ast.IfExpr { - // `b := a && if true { a = false ...} else {...}` - prev_inside_ternary := g.inside_ternary - g.inside_ternary = 0 - if g.need_tmp_var_in_if(node.right) { - tmp := g.new_tmp_var() - cur_line := g.go_before_last_stmt().trim_space() - g.empty_line = true - g.write('bool ${tmp} = (') - g.expr(node.left) - g.writeln(');') - g.set_current_pos_as_last_stmt_pos() - g.write('${cur_line} ${tmp} ${node.op.str()} ') - g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' } - g.expr(node.right) - g.infix_left_var_name = '' - g.inside_ternary = prev_inside_ternary - return - } - g.inside_ternary = prev_inside_ternary - } else if node.right is ast.MatchExpr { - // `b := a && match true { true { a = false ...} else {...}}` - prev_inside_ternary := g.inside_ternary - g.inside_ternary = 0 - if g.need_tmp_var_in_match(node.right) { - tmp := g.new_tmp_var() - cur_line := g.go_before_last_stmt().trim_space() - g.empty_line = true - g.write('bool ${tmp} = (') - g.expr(node.left) - g.writeln(');') - g.set_current_pos_as_last_stmt_pos() - g.write('${cur_line} ${tmp} ${node.op.str()} ') - g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' } - g.expr(node.right) - g.infix_left_var_name = '' - g.inside_ternary = prev_inside_ternary - return - } - g.inside_ternary = prev_inside_ternary - } else if g.need_tmp_var_in_array_call(node.right) { + 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_last_stmt().trim_space() @@ -1016,48 +976,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' } g.expr(node.right) g.infix_left_var_name = '' - return - } else if node.right is ast.CallExpr { - if node.right.or_block.kind != .absent { - prev_inside_ternary := g.inside_ternary - g.inside_ternary = 0 - tmp := g.new_tmp_var() - cur_line := g.go_before_last_stmt().trim_space() - g.empty_line = true - g.write('bool ${tmp} = (') - g.expr(node.left) - g.writeln(');') - g.set_current_pos_as_last_stmt_pos() - g.write('${cur_line} ${tmp} ${node.op.str()} ') - g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' } - g.expr(node.right) - g.infix_left_var_name = '' - g.inside_ternary = prev_inside_ternary - return - } - } else if node.right is ast.PrefixExpr && g.inside_ternary == 0 { - prefix := node.right - if prefix.op == .not && prefix.right is ast.CallExpr { - call_expr := prefix.right as ast.CallExpr - if call_expr.or_block.kind != .absent { - prev_inside_ternary := g.inside_ternary - g.inside_ternary = 0 - tmp := g.new_tmp_var() - cur_line := g.go_before_last_stmt().trim_space() - g.empty_line = true - g.write('bool ${tmp} = (') - g.expr(node.left) - g.writeln(');') - g.set_current_pos_as_last_stmt_pos() - g.write('${cur_line} ${tmp} ${node.op.str()} ') - g.infix_left_var_name = '!${tmp}' - g.expr(node.right) - g.infix_left_var_name = '' - g.inside_ternary = prev_inside_ternary - return - } - } - } else if node.right is ast.InfixExpr && g.need_tmp_var_in_expr(node.right) { + } else if g.need_tmp_var_in_expr(node.right) && g.inside_ternary == 0 { prev_inside_ternary := g.inside_ternary g.inside_ternary = 0 tmp := g.new_tmp_var() @@ -1072,9 +991,9 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { g.expr(node.right) g.infix_left_var_name = '' g.inside_ternary = prev_inside_ternary - return + } else { + g.gen_plain_infix_expr(node) } - g.gen_plain_infix_expr(node) } fn (mut g Gen) gen_is_none_check(node ast.InfixExpr) {