cgen: cleanup in if_expr() (#13754)
parent
7a4715288c
commit
136f39a2d4
|
@ -115,89 +115,84 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
|
||||||
cvar_name := guard_vars[guard_idx]
|
cvar_name := guard_vars[guard_idx]
|
||||||
g.writeln('\tIError err = ${cvar_name}.err;')
|
g.writeln('\tIError err = ${cvar_name}.err;')
|
||||||
}
|
}
|
||||||
} else {
|
} else if branch.cond is ast.IfGuardExpr {
|
||||||
match branch.cond {
|
mut var_name := guard_vars[i]
|
||||||
ast.IfGuardExpr {
|
mut short_opt := false
|
||||||
mut var_name := guard_vars[i]
|
if var_name == '' {
|
||||||
mut short_opt := false
|
short_opt = true // we don't need a further tmp, so use the one we'll get later
|
||||||
if var_name == '' {
|
var_name = g.new_tmp_var()
|
||||||
short_opt = true // we don't need a further tmp, so use the one we'll get later
|
guard_vars[i] = var_name // for `else`
|
||||||
var_name = g.new_tmp_var()
|
g.tmp_count--
|
||||||
guard_vars[i] = var_name // for `else`
|
g.writeln('if (${var_name}.state == 0) {')
|
||||||
g.tmp_count--
|
} else {
|
||||||
g.writeln('if (${var_name}.state == 0) {')
|
g.write('if ($var_name = ')
|
||||||
|
g.expr(branch.cond.expr)
|
||||||
|
g.writeln(', ${var_name}.state == 0) {')
|
||||||
|
}
|
||||||
|
if short_opt || branch.cond.vars[0].name != '_' {
|
||||||
|
base_type := g.base_type(branch.cond.expr_type)
|
||||||
|
if short_opt {
|
||||||
|
cond_var_name := if branch.cond.vars[0].name == '_' {
|
||||||
|
'_dummy_${g.tmp_count + 1}'
|
||||||
} else {
|
} else {
|
||||||
g.write('if ($var_name = ')
|
branch.cond.vars[0].name
|
||||||
g.expr(branch.cond.expr)
|
|
||||||
g.writeln(', ${var_name}.state == 0) {')
|
|
||||||
}
|
}
|
||||||
if short_opt || branch.cond.vars[0].name != '_' {
|
g.write('\t$base_type $cond_var_name = ')
|
||||||
base_type := g.base_type(branch.cond.expr_type)
|
g.expr(branch.cond.expr)
|
||||||
if short_opt {
|
g.writeln(';')
|
||||||
cond_var_name := if branch.cond.vars[0].name == '_' {
|
} else {
|
||||||
'_dummy_${g.tmp_count + 1}'
|
mut is_auto_heap := false
|
||||||
} else {
|
if branch.stmts.len > 0 {
|
||||||
branch.cond.vars[0].name
|
scope := g.file.scope.innermost(ast.Node(branch.stmts[branch.stmts.len - 1]).pos().pos)
|
||||||
}
|
if v := scope.find_var(branch.cond.vars[0].name) {
|
||||||
g.write('\t$base_type $cond_var_name = ')
|
is_auto_heap = v.is_auto_heap
|
||||||
g.expr(branch.cond.expr)
|
}
|
||||||
g.writeln(';')
|
}
|
||||||
|
if branch.cond.vars.len == 1 {
|
||||||
|
left_var_name := c_name(branch.cond.vars[0].name)
|
||||||
|
if is_auto_heap {
|
||||||
|
g.writeln('\t$base_type* $left_var_name = HEAP($base_type, *($base_type*)${var_name}.data);')
|
||||||
} else {
|
} else {
|
||||||
mut is_auto_heap := false
|
g.writeln('\t$base_type $left_var_name = *($base_type*)${var_name}.data;')
|
||||||
if branch.stmts.len > 0 {
|
}
|
||||||
scope := g.file.scope.innermost(ast.Node(branch.stmts[branch.stmts.len - 1]).pos().pos)
|
} else if branch.cond.vars.len > 1 {
|
||||||
if v := scope.find_var(branch.cond.vars[0].name) {
|
for vi, var in branch.cond.vars {
|
||||||
is_auto_heap = v.is_auto_heap
|
left_var_name := c_name(var.name)
|
||||||
}
|
sym := g.table.sym(branch.cond.expr_type)
|
||||||
}
|
if sym.kind == .multi_return {
|
||||||
if branch.cond.vars.len == 1 {
|
mr_info := sym.info as ast.MultiReturn
|
||||||
left_var_name := c_name(branch.cond.vars[0].name)
|
if mr_info.types.len == branch.cond.vars.len {
|
||||||
if is_auto_heap {
|
var_typ := g.typ(mr_info.types[vi])
|
||||||
g.writeln('\t$base_type* $left_var_name = HEAP($base_type, *($base_type*)${var_name}.data);')
|
if is_auto_heap {
|
||||||
} else {
|
g.writeln('\t$var_typ* $left_var_name = (HEAP($base_type, *($base_type*)${var_name}.data).arg$vi);')
|
||||||
g.writeln('\t$base_type $left_var_name = *($base_type*)${var_name}.data;')
|
} else {
|
||||||
}
|
g.writeln('\t$var_typ $left_var_name = (*($base_type*)${var_name}.data).arg$vi;')
|
||||||
} else if branch.cond.vars.len > 1 {
|
|
||||||
for vi, var in branch.cond.vars {
|
|
||||||
left_var_name := c_name(var.name)
|
|
||||||
sym := g.table.sym(branch.cond.expr_type)
|
|
||||||
if sym.kind == .multi_return {
|
|
||||||
mr_info := sym.info as ast.MultiReturn
|
|
||||||
if mr_info.types.len == branch.cond.vars.len {
|
|
||||||
var_typ := g.typ(mr_info.types[vi])
|
|
||||||
if is_auto_heap {
|
|
||||||
g.writeln('\t$var_typ* $left_var_name = (HEAP($base_type, *($base_type*)${var_name}.data).arg$vi);')
|
|
||||||
} else {
|
|
||||||
g.writeln('\t$var_typ $left_var_name = (*($base_type*)${var_name}.data).arg$vi;')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
mut no_needs_par := false
|
} else {
|
||||||
if branch.cond is ast.InfixExpr {
|
mut no_needs_par := false
|
||||||
if branch.cond.op == .key_in && branch.cond.left !is ast.InfixExpr
|
if branch.cond is ast.InfixExpr {
|
||||||
&& branch.cond.right is ast.ArrayInit {
|
if branch.cond.op == .key_in && branch.cond.left !is ast.InfixExpr
|
||||||
no_needs_par = true
|
&& branch.cond.right is ast.ArrayInit {
|
||||||
}
|
no_needs_par = true
|
||||||
}
|
|
||||||
if no_needs_par {
|
|
||||||
g.write('if ')
|
|
||||||
} else {
|
|
||||||
g.write('if (')
|
|
||||||
}
|
|
||||||
g.expr(branch.cond)
|
|
||||||
if no_needs_par {
|
|
||||||
g.writeln(' {')
|
|
||||||
} else {
|
|
||||||
g.writeln(') {')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if no_needs_par {
|
||||||
|
g.write('if ')
|
||||||
|
} else {
|
||||||
|
g.write('if (')
|
||||||
|
}
|
||||||
|
g.expr(branch.cond)
|
||||||
|
if no_needs_par {
|
||||||
|
g.writeln(' {')
|
||||||
|
} else {
|
||||||
|
g.writeln(') {')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if needs_tmp_var {
|
if needs_tmp_var {
|
||||||
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
|
if node.is_expr && g.table.sym(node.typ).kind == .sum_type {
|
||||||
|
|
Loading…
Reference in New Issue