RAW FIR: flatten if chains

This commit is contained in:
Jinseong Jeon
2022-02-13 01:00:30 -08:00
committed by teamcity
parent 5cec068d36
commit bb766a5235
6 changed files with 186 additions and 209 deletions
@@ -1909,19 +1909,30 @@ open class RawFirBuilder(
override fun visitIfExpression(expression: KtIfExpression, data: Unit): FirElement {
return buildWhenExpression {
source = expression.toFirSourceElement()
val ktCondition = expression.condition
branches += buildWhenBranch {
source = ktCondition?.toFirSourceElement(KtFakeSourceElementKind.WhenCondition)
condition = ktCondition.toFirExpression("If statement should have condition")
result = expression.then.toFirBlock()
}
if (expression.elseKeyword != null) {
var ktLastIf: KtIfExpression = expression
whenBranches@ while (true) {
val ktCondition = ktLastIf.condition
branches += buildWhenBranch {
source = expression.elseKeyword?.toKtPsiSourceElement()
condition = buildElseIfTrueCondition()
result = expression.`else`.toFirBlock()
source = ktCondition?.toFirSourceElement(KtFakeSourceElementKind.WhenCondition)
condition = ktCondition.toFirExpression("If statement should have condition")
result = ktLastIf.then.toFirBlock()
}
when (val ktElse = ktLastIf.`else`) {
null -> break@whenBranches
is KtIfExpression -> ktLastIf = ktElse
else -> {
branches += buildWhenBranch {
source = ktLastIf.elseKeyword?.toKtPsiSourceElement()
condition = buildElseIfTrueCondition()
result = ktLastIf.`else`.toFirBlock()
}
break@whenBranches
}
}
}
usedAsExpression = expression.usedAsExpression
}
}