Handle break/continue in loop header (condition for 'while', range for 'for').

This commit is contained in:
Dmitry Petrov
2016-09-05 10:30:09 +03:00
committed by Dmitry Petrov
parent b3866d53cd
commit d623c70778
7 changed files with 139 additions and 32 deletions
@@ -39,8 +39,8 @@ class LoopExpressionGenerator(statementGenerator: StatementGenerator) : Statemen
context.builtIns.unitType, IrOperator.DO_WHILE_LOOP))
private fun generateConditionalLoop(ktLoop: KtWhileExpressionBase, irLoop: IrLoopBase): IrLoop {
statementGenerator.bodyGenerator.putLoop(ktLoop, irLoop)
irLoop.condition = statementGenerator.generateExpression(ktLoop.condition!!)
statementGenerator.bodyGenerator.putLoop(ktLoop, irLoop)
irLoop.body = ktLoop.body?.let { statementGenerator.generateExpression(ktLoop.body!!) }
irLoop.label = getLoopLabel(ktLoop)
return irLoop
@@ -74,14 +74,14 @@ class LoopExpressionGenerator(statementGenerator: StatementGenerator) : Statemen
break
}
if (targetLabel == null) {
return getLoop(finger)
return getLoop(finger) ?: continue
}
else {
val parent = finger.parent
if (parent is KtLabeledExpression) {
val label = parent.getLabelName()!!
if (targetLabel == label) {
return getLoop(finger)
return getLoop(finger) ?: continue
}
}
}
@@ -89,9 +89,8 @@ class LoopExpressionGenerator(statementGenerator: StatementGenerator) : Statemen
throw AssertionError("No parent loop for break/continue @$targetLabel")
}
private fun getLoop(ktLoop: KtLoopExpression): IrLoop {
return statementGenerator.bodyGenerator.getLoop(ktLoop) ?:
throw AssertionError("Loop was not visited:\n${ktLoop.text}")
private fun getLoop(ktLoop: KtLoopExpression): IrLoop? {
return statementGenerator.bodyGenerator.getLoop(ktLoop)
}
fun generateForLoop(ktFor: KtForExpression): IrExpression {
@@ -162,10 +162,10 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
"DO_WHILE label=${loop.label} operator=${loop.operator}"
override fun visitBreak(jump: IrBreak, data: Nothing?): String =
"BREAK label=${jump.label} depth=${jump.getDepth()}"
"BREAK label=${jump.label} loop.label=${jump.loop.label} depth=${jump.getDepth()}"
override fun visitContinue(jump: IrContinue, data: Nothing?): String =
"CONTINUE label=${jump.label} depth=${jump.getDepth()}"
"CONTINUE label=${jump.label} loop.label=${jump.loop.label} depth=${jump.getDepth()}"
override fun visitThrow(expression: IrThrow, data: Nothing?): String =
"THROW type=${expression.type.render()}"