[JS IR BE] process empty loops correctly in BlockDecomposerLowering

This commit is contained in:
Zalim Bashorov
2018-10-10 14:23:51 +03:00
parent 6d592ae66b
commit 8013a56286
6 changed files with 70 additions and 6 deletions
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.ir.util.transformFlat
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.utils.addIfNotNull
class BlockDecomposerLowering(context: JsIrBackendContext) : DeclarationContainerLoweringPass {
@@ -265,7 +266,7 @@ class BlockDecomposerTransformer(context: JsIrBackendContext) : IrElementTransfo
}
newLoopBody.statements += JsIrBuilder.buildIfElse(unitType, breakCond, thenBlock)
newLoopBody.statements += newBody!!
newLoopBody.statements.addIfNotNull(newBody)
return loop.apply {
condition = constTrue
@@ -293,16 +294,22 @@ class BlockDecomposerTransformer(context: JsIrBackendContext) : IrElementTransfo
// } while (cond)
//
override fun visitDoWhileLoop(loop: IrDoWhileLoop): IrExpression {
val newBody = loop.body?.transform(statementTransformer, null)!!
val newBody = loop.body?.transform(statementTransformer, null)
val newCondition = loop.condition.transform(expressionTransformer, null)
if (newCondition is IrComposite) {
val innerLoop = IrDoWhileLoopImpl(loop.startOffset, loop.endOffset, unitType, loop.origin, newBody, constFalse).apply {
val innerLoop = IrDoWhileLoopImpl(loop.startOffset, loop.endOffset, unitType, loop.origin).apply {
condition = constFalse
body = newBody
label = makeLoopLabel()
}
val newLoopCondition = newCondition.statements.last() as IrExpression
val newLoopBody = IrBlockImpl(newCondition.startOffset, newBody.endOffset, newBody.type).apply {
val newLoopBody = IrBlockImpl(
newCondition.startOffset,
newBody?.endOffset ?: newCondition.endOffset,
newBody?.type ?: unitType
).apply {
statements += innerLoop
statements += newCondition.statements.run { subList(0, lastIndex) }
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JS_IR
fun box(): String {
do while (false);
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JS_IR
fun box(): String {
while (false);