'while' and 'do-while' loop generator fixes.
Generate 'do-while' loop body as IrComposite, because variables declared in loop body should be visible in loop condition. Wrap 'do-while' loop in IrBlock so that variables declared in loop body are not visible outside of the loop. Generate 'while' and 'do-while' loops as expressions of type Unit.
This commit is contained in:
+1
-1
@@ -72,7 +72,7 @@ class FunctionGenerator(val function: IrFunction) {
|
||||
return body.process()
|
||||
}
|
||||
|
||||
override fun visitBlock(expression: IrBlock, data: Boolean): IrStatement? {
|
||||
override fun visitContainerExpression(expression: IrContainerExpression, data: Boolean): IrStatement? {
|
||||
return expression.process() ?: expression
|
||||
}
|
||||
|
||||
|
||||
+33
-4
@@ -39,13 +39,42 @@ class LoopExpressionGenerator(statementGenerator: StatementGenerator) : Statemen
|
||||
IrDoWhileLoopImpl(ktDoWhile.startOffset, ktDoWhile.endOffset,
|
||||
context.builtIns.unitType, IrStatementOrigin.DO_WHILE_LOOP))
|
||||
|
||||
private fun generateConditionalLoop(ktLoop: KtWhileExpressionBase, irLoop: IrLoopBase): IrLoop {
|
||||
private fun generateConditionalLoop(ktLoop: KtWhileExpressionBase, irLoop: IrLoopBase): IrExpression {
|
||||
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
|
||||
irLoop.body = ktLoop.body?.let { ktLoopBody ->
|
||||
if (ktLoopBody is KtBlockExpression) {
|
||||
if (ktLoop is KtDoWhileExpression)
|
||||
generateDoWhileLoopBody(ktLoopBody)
|
||||
else
|
||||
generateWhileLoopBody(ktLoopBody)
|
||||
}
|
||||
else
|
||||
statementGenerator.generateExpression(ktLoopBody)
|
||||
}
|
||||
irLoop.label = getLoopLabel(ktLoop)
|
||||
|
||||
return if (ktLoop is KtDoWhileExpression) {
|
||||
IrBlockImpl(ktLoop.startOffset, ktLoop.endOffset, context.builtIns.unitType).apply {
|
||||
statements.add(irLoop)
|
||||
}
|
||||
}
|
||||
else
|
||||
irLoop
|
||||
}
|
||||
|
||||
private fun generateWhileLoopBody(ktLoopBody: KtBlockExpression): IrExpression =
|
||||
IrBlockImpl(
|
||||
ktLoopBody.startOffset, ktLoopBody.endOffset, context.builtIns.unitType, null,
|
||||
ktLoopBody.statements.map { statementGenerator.generateStatement(it) }
|
||||
)
|
||||
|
||||
|
||||
private fun generateDoWhileLoopBody(ktLoopBody: KtBlockExpression): IrExpression =
|
||||
IrCompositeImpl(
|
||||
ktLoopBody.startOffset, ktLoopBody.endOffset, context.builtIns.unitType, null,
|
||||
ktLoopBody.statements.map { statementGenerator.generateStatement(it) }
|
||||
)
|
||||
|
||||
fun generateBreak(ktBreak: KtBreakExpression): IrExpression {
|
||||
val parentLoop = findParentLoop(ktBreak) ?:
|
||||
|
||||
@@ -7,7 +7,7 @@ FILE /badBreakContinue.kt
|
||||
BLOCK_BODY
|
||||
WHILE label=L1 origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
ERROR_EXPR 'Loop not found for break expression: break@ERROR' type=kotlin.Nothing
|
||||
ERROR_EXPR 'Loop not found for continue expression: continue@ERROR' type=kotlin.Nothing
|
||||
FUN public fun test3(): kotlin.Unit
|
||||
|
||||
+16
-14
@@ -3,37 +3,39 @@ FILE /breakContinue.kt
|
||||
BLOCK_BODY
|
||||
WHILE label=null origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
BREAK label=null loop.label=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
BREAK label=null loop.label=null
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
BREAK label=null loop.label=null
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
WHILE label=null origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
CONTINUE label=null loop.label=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
CONTINUE label=null loop.label=null
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
CONTINUE label=null loop.label=null
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
FUN public fun test2(): kotlin.Unit
|
||||
BLOCK_BODY
|
||||
WHILE label=OUTER origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHILE label=INNER origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
BREAK label=INNER loop.label=INNER
|
||||
BREAK label=OUTER loop.label=OUTER
|
||||
BREAK label=OUTER loop.label=OUTER
|
||||
WHILE label=OUTER origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHILE label=INNER origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
CONTINUE label=INNER loop.label=INNER
|
||||
CONTINUE label=OUTER loop.label=OUTER
|
||||
CONTINUE label=OUTER loop.label=OUTER
|
||||
@@ -41,17 +43,17 @@ FILE /breakContinue.kt
|
||||
BLOCK_BODY
|
||||
WHILE label=L origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHILE label=L origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
BREAK label=L loop.label=L
|
||||
BREAK label=L loop.label=L
|
||||
WHILE label=L origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHILE label=L origin=WHILE_LOOP
|
||||
condition: CONST Boolean type=kotlin.Boolean value='true'
|
||||
body: BLOCK type=kotlin.Nothing origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
CONTINUE label=L loop.label=L
|
||||
CONTINUE label=L loop.label=L
|
||||
|
||||
@@ -26,8 +26,8 @@ FILE /whileDoWhile.kt
|
||||
arg0: CALL 'compareTo(Int): Int' type=kotlin.Int origin=LT
|
||||
$this: GET_VAR 'x: Int' type=kotlin.Int origin=null
|
||||
other: CONST Int type=kotlin.Int value='10'
|
||||
body: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=POSTFIX_INCR
|
||||
VAR IR_TEMPORARY_VARIABLE val tmp1: kotlin.Int
|
||||
GET_VAR 'x: Int' type=kotlin.Int origin=POSTFIX_INCR
|
||||
@@ -35,11 +35,13 @@ FILE /whileDoWhile.kt
|
||||
CALL 'inc(): Int' type=kotlin.Int origin=POSTFIX_INCR
|
||||
$this: GET_VAR 'tmp1: Int' type=kotlin.Int origin=null
|
||||
GET_VAR 'tmp1: Int' type=kotlin.Int origin=null
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
condition: CALL 'LT0(Int): Boolean' type=kotlin.Boolean origin=LT
|
||||
arg0: CALL 'compareTo(Int): Int' type=kotlin.Int origin=LT
|
||||
$this: GET_VAR 'x: Int' type=kotlin.Int origin=null
|
||||
other: CONST Int type=kotlin.Int value='0'
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=POSTFIX_INCR
|
||||
@@ -53,8 +55,9 @@ FILE /whileDoWhile.kt
|
||||
arg0: CALL 'compareTo(Int): Int' type=kotlin.Int origin=LT
|
||||
$this: GET_VAR 'x: Int' type=kotlin.Int origin=null
|
||||
other: CONST Int type=kotlin.Int value='15'
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=POSTFIX_INCR
|
||||
VAR IR_TEMPORARY_VARIABLE val tmp3: kotlin.Int
|
||||
@@ -80,7 +83,8 @@ FILE /whileDoWhile.kt
|
||||
condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean
|
||||
GET_VAR 'a: Any?' type=kotlin.Any? origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean
|
||||
GET_VAR 'a: Any?' type=kotlin.Any? origin=null
|
||||
|
||||
@@ -12,8 +12,7 @@ FILE /coercionInLoop.kt
|
||||
WHILE label=null origin=WHILE_LOOP
|
||||
condition: CALL 'hasNext(): Boolean' type=kotlin.Boolean origin=null
|
||||
$this: GET_VAR 'x: DoubleIterator' type=kotlin.collections.DoubleIterator origin=null
|
||||
body: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHEN type=kotlin.Unit origin=null
|
||||
BRANCH
|
||||
if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
|
||||
@@ -27,6 +26,7 @@ FILE /coercionInLoop.kt
|
||||
STRING_CONCATENATION type=kotlin.String
|
||||
CONST String type=kotlin.String value='Fail '
|
||||
GET_VAR 'i: Int' type=kotlin.Int origin=null
|
||||
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
BLOCK type=kotlin.Int origin=POSTFIX_INCR
|
||||
VAR IR_TEMPORARY_VARIABLE val tmp0: kotlin.Int
|
||||
GET_VAR 'i: Int' type=kotlin.Int origin=POSTFIX_INCR
|
||||
|
||||
Reference in New Issue
Block a user