Partial body resolve: more efficient handling if-statements

This commit is contained in:
Valentin Kipyatkov
2014-11-18 15:48:03 +03:00
parent 0a7d73ef6b
commit e724af4b4e
6 changed files with 66 additions and 2 deletions
@@ -208,7 +208,6 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J
return result
}
//TODO: more precise analysis
private fun collectAlwaysExitPoints(expression: JetExpression?): Collection<JetExpression> {
val result = ArrayList<JetExpression>()
expression?.accept(object : ControlFlowVisitor() {
@@ -222,6 +221,23 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J
result.add(expression)
}
override fun visitIfExpression(expression: JetIfExpression) {
expression.getCondition().accept(this)
val thenBranch = expression.getThen()
val elseBranch = expression.getElse()
if (thenBranch != null && elseBranch != null) { // if we have only one branch it makes no sense to search exits in it
val thenExits = collectAlwaysExitPoints(thenBranch)
if (thenExits.isNotEmpty()) {
val elseExits = collectAlwaysExitPoints(elseBranch)
if (elseExits.isNotEmpty()) {
result.addAll(thenExits)
result.addAll(elseExits)
}
}
}
}
override fun visitForExpression(loop: JetForExpression) {
loop.getLoopRange()?.accept(this)
// do not make sense to search exits inside for as not necessary enter it at all