|
|
@ -14,11 +14,11 @@ class IfElseTransformer : Transformer() { |
|
|
|
stmt.elseStmt.ifPresent { elseStmt: Statement -> |
|
|
|
stmt.elseStmt.ifPresent { elseStmt: Statement -> |
|
|
|
val condition = stmt.condition |
|
|
|
val condition = stmt.condition |
|
|
|
val thenStmt = stmt.thenStmt |
|
|
|
val thenStmt = stmt.thenStmt |
|
|
|
if (isIf(thenStmt) && !isIf(elseStmt)) { |
|
|
|
if (thenStmt.isIf() && !elseStmt.isIf()) { |
|
|
|
stmt.condition = condition.not() |
|
|
|
stmt.condition = condition.not() |
|
|
|
stmt.thenStmt = elseStmt.clone() |
|
|
|
stmt.thenStmt = elseStmt.clone() |
|
|
|
stmt.setElseStmt(thenStmt.clone()) |
|
|
|
stmt.setElseStmt(thenStmt.clone()) |
|
|
|
} else if (!isIf(thenStmt) && isIf(elseStmt)) { |
|
|
|
} else if (!thenStmt.isIf() && elseStmt.isIf()) { |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Don't consider any more conditions for swapping the |
|
|
|
* Don't consider any more conditions for swapping the |
|
|
|
* if/else branches, as it'll introduce another level of |
|
|
|
* if/else branches, as it'll introduce another level of |
|
|
@ -45,8 +45,8 @@ class IfElseTransformer : Transformer() { |
|
|
|
|
|
|
|
|
|
|
|
unit.walk { stmt: IfStmt -> |
|
|
|
unit.walk { stmt: IfStmt -> |
|
|
|
stmt.elseStmt.ifPresent { elseStmt -> |
|
|
|
stmt.elseStmt.ifPresent { elseStmt -> |
|
|
|
if (isIf(elseStmt)) { |
|
|
|
if (elseStmt.isIf()) { |
|
|
|
stmt.setElseStmt(getIf(elseStmt)) |
|
|
|
stmt.setElseStmt(elseStmt.getIf()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -97,7 +97,7 @@ class IfElseTransformer : Transformer() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val thenStmt = ifStmt.thenStmt |
|
|
|
val thenStmt = ifStmt.thenStmt |
|
|
|
if (!isTailThrowOrReturn(thenStmt)) { |
|
|
|
if (!thenStmt.isTailThrowOrReturn()) { |
|
|
|
return@ifPresent |
|
|
|
return@ifPresent |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -112,47 +112,45 @@ class IfElseTransformer : Transformer() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
private fun Statement.isIf(): Boolean { |
|
|
|
private fun isIf(stmt: Statement): Boolean { |
|
|
|
return when { |
|
|
|
return when { |
|
|
|
isIfStmt -> true |
|
|
|
stmt.isIfStmt -> true |
|
|
|
isBlockStmt -> { |
|
|
|
stmt.isBlockStmt -> { |
|
|
|
val stmts = asBlockStmt().statements |
|
|
|
val stmts = stmt.asBlockStmt().statements |
|
|
|
stmts.size == 1 && stmts[0].isIfStmt |
|
|
|
stmts.size == 1 && stmts[0].isIfStmt |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else -> false |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else -> false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun getIf(stmt: Statement): Statement { |
|
|
|
private fun Statement.getIf(): Statement { |
|
|
|
if (stmt.isIfStmt) { |
|
|
|
if (isIfStmt) { |
|
|
|
return stmt.clone() |
|
|
|
return clone() |
|
|
|
} else if (stmt.isBlockStmt) { |
|
|
|
} else if (isBlockStmt) { |
|
|
|
val stmts = stmt.asBlockStmt().statements |
|
|
|
val stmts = asBlockStmt().statements |
|
|
|
if (stmts.size == 1) { |
|
|
|
if (stmts.size == 1) { |
|
|
|
val head = stmts[0] |
|
|
|
val head = stmts[0] |
|
|
|
if (head.isIfStmt) { |
|
|
|
if (head.isIfStmt) { |
|
|
|
return head.clone() |
|
|
|
return head.clone() |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
throw IllegalArgumentException() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
throw IllegalArgumentException() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun isTailThrowOrReturn(stmt: Statement): Boolean { |
|
|
|
private fun Statement.isTailThrowOrReturn(): Boolean { |
|
|
|
return if (stmt.isThrowStmt || stmt.isReturnStmt) { |
|
|
|
return if (isThrowStmt || isReturnStmt) { |
|
|
|
true |
|
|
|
true |
|
|
|
} else if (stmt.isBlockStmt) { |
|
|
|
} else if (isBlockStmt) { |
|
|
|
val stmts = stmt.asBlockStmt().statements |
|
|
|
val stmts = asBlockStmt().statements |
|
|
|
if (stmts.isEmpty()) { |
|
|
|
if (stmts.isEmpty()) { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val tail = stmts[stmts.size - 1] |
|
|
|
|
|
|
|
tail.isThrowStmt || tail.isReturnStmt |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
false |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val tail = stmts[stmts.size - 1] |
|
|
|
|
|
|
|
tail.isThrowStmt || tail.isReturnStmt |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
false |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|