Add lowering on switch in loop
^KT-42262 fixed
This commit is contained in:
@@ -341,6 +341,12 @@ private val rangeContainsLoweringPhase = makeBodyLoweringPhase(
|
||||
description = "[Optimization] Optimizes calls to contains() for ClosedRanges"
|
||||
)
|
||||
|
||||
private val loopWithBreakingSwitchLowering = makeBodyLoweringPhase(
|
||||
{ LoopWithBreakingSwitchLowering() },
|
||||
name = "LoopWithBreakingSwitchLowering",
|
||||
description = "Transform statement-like-expression nodes into pure-statement to make it easily transform into JS",
|
||||
)
|
||||
|
||||
private val forLoopsLoweringPhase = makeBodyLoweringPhase(
|
||||
::ForLoopsLowering,
|
||||
name = "ForLoopsLowering",
|
||||
@@ -712,6 +718,7 @@ val loweringList = listOf<Lowering>(
|
||||
interopCallableReferenceLoweringPhase,
|
||||
returnableBlockLoweringPhase,
|
||||
rangeContainsLoweringPhase,
|
||||
loopWithBreakingSwitchLowering,
|
||||
forLoopsLoweringPhase,
|
||||
primitiveCompanionLoweringPhase,
|
||||
propertyAccessorInlinerLoweringPhase,
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.ir.backend.js.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBody
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBreak
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrLoop
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import java.util.*
|
||||
|
||||
class LoopWithBreakingSwitchLowering : BodyLoweringPass {
|
||||
|
||||
private val switchInLoopTransformer = LoopWithBreakingSwitchTransformer()
|
||||
|
||||
override fun lower(irBody: IrBody, container: IrDeclaration) {
|
||||
when (container) {
|
||||
is IrFunction -> {
|
||||
container.accept(switchInLoopTransformer, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LoopWithBreakingSwitchTransformer : IrElementTransformerVoid() {
|
||||
private var counter: Int = 0
|
||||
|
||||
private val loopDeque: Deque<IrLoop> = LinkedList()
|
||||
|
||||
override fun visitBreak(jump: IrBreak): IrExpression {
|
||||
val loop = loopDeque.firstOrNull()
|
||||
loop?.label = loop?.label ?: makeLoopLabel()
|
||||
|
||||
return super.visitBreak(jump)
|
||||
}
|
||||
|
||||
override fun visitLoop(loop: IrLoop): IrExpression {
|
||||
loopDeque.push(loop)
|
||||
return super.visitLoop(loop).apply {
|
||||
loopDeque.pop()
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeLoopLabel() = "\$l\$switch\$${counter++}"
|
||||
}
|
||||
Reference in New Issue
Block a user