Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: simplify in infix_expr_and_or_op() (related #21740) #21745

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 45 additions & 42 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
89 changes: 4 additions & 85 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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) {
Expand Down
Loading