FIR2IR: wrap do-while loop in IrBlock

"so that variables declared in loop body are not visible outside of the
loop" (from commit d096f1d)
This commit is contained in:
Jinseong Jeon
2021-03-18 00:29:09 -07:00
committed by TeamCityServer
parent 08670114c8
commit 7898d167f3
13 changed files with 197 additions and 161 deletions
@@ -728,7 +728,7 @@ class Fir2IrVisitor(
private val loopMap = mutableMapOf<FirLoop, IrLoop>() private val loopMap = mutableMapOf<FirLoop, IrLoop>()
override fun visitDoWhileLoop(doWhileLoop: FirDoWhileLoop, data: Any?): IrElement { override fun visitDoWhileLoop(doWhileLoop: FirDoWhileLoop, data: Any?): IrElement {
return doWhileLoop.convertWithOffsets { startOffset, endOffset -> val irLoop = doWhileLoop.convertWithOffsets { startOffset, endOffset ->
IrDoWhileLoopImpl( IrDoWhileLoopImpl(
startOffset, endOffset, irBuiltIns.unitType, startOffset, endOffset, irBuiltIns.unitType,
IrStatementOrigin.DO_WHILE_LOOP IrStatementOrigin.DO_WHILE_LOOP
@@ -742,6 +742,9 @@ class Fir2IrVisitor(
}.also { }.also {
doWhileLoop.accept(implicitCastInserter, it) doWhileLoop.accept(implicitCastInserter, it)
} }
return IrBlockImpl(irLoop.startOffset, irLoop.endOffset, irBuiltIns.unitType).apply {
statements.add(irLoop)
}
} }
override fun visitWhileLoop(whileLoop: FirWhileLoop, data: Any?): IrElement { override fun visitWhileLoop(whileLoop: FirWhileLoop, data: Any?): IrElement {
@@ -1,5 +1,7 @@
fun foo() { fun foo() {
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
val x: Int = 42 val x: Int = 42
// } while (EQEQ(arg0 = x, arg1 = 42).not()) // } while (EQEQ(arg0 = x, arg1 = 42).not())
}
} }
@@ -1,6 +1,7 @@
FILE fqName:<root> fileName:/localVarInDoWhile.kt FILE fqName:<root> fileName:/localVarInDoWhile.kt
FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Unit FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY BLOCK_BODY
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP
VAR name:x type:kotlin.Int [val] VAR name:x type:kotlin.Int [val]
@@ -1,8 +1,12 @@
fun test1() { fun test1() {
while (true) break while (true) break
{ // BLOCK
dobreak while (true) dobreak while (true)
}
while (true) continue while (true) continue
{ // BLOCK
docontinue while (true) docontinue while (true)
}
} }
fun test2() { fun test2() {
@@ -4,12 +4,14 @@ FILE fqName:<root> fileName:/breakContinue.kt
WHILE label=null origin=WHILE_LOOP WHILE label=null origin=WHILE_LOOP
condition: CONST Boolean type=kotlin.Boolean value=true condition: CONST Boolean type=kotlin.Boolean value=true
body: BREAK label=null loop.label=null body: BREAK label=null loop.label=null
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: BREAK label=null loop.label=null body: BREAK label=null loop.label=null
condition: CONST Boolean type=kotlin.Boolean value=true condition: CONST Boolean type=kotlin.Boolean value=true
WHILE label=null origin=WHILE_LOOP WHILE label=null origin=WHILE_LOOP
condition: CONST Boolean type=kotlin.Boolean value=true condition: CONST Boolean type=kotlin.Boolean value=true
body: CONTINUE label=null loop.label=null body: CONTINUE label=null loop.label=null
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: CONTINUE label=null loop.label=null body: CONTINUE label=null loop.label=null
condition: CONST Boolean type=kotlin.Boolean value=true condition: CONST Boolean type=kotlin.Boolean value=true
@@ -64,6 +64,7 @@ fun test5() {
i = i.inc() i = i.inc()
i /*~> Unit */ i /*~> Unit */
var j: Int = 0 var j: Int = 0
{ // BLOCK
Inner@ do// COMPOSITE { Inner@ do// COMPOSITE {
j = j.inc() j = j.inc()
j j
@@ -71,6 +72,7 @@ fun test5() {
greaterOrEqual(arg0 = j, arg1 = 3) -> false greaterOrEqual(arg0 = j, arg1 = 3) -> false
else -> break@Outer else -> break@Outer
}) })
}
when { when {
EQEQ(arg0 = i, arg1 = 3) -> break@Outer EQEQ(arg0 = i, arg1 = 3) -> break@Outer
} }
@@ -109,6 +109,7 @@ FILE fqName:<root> fileName:/breakContinueInLoopHeader.kt
GET_VAR 'var i: kotlin.Int [var] declared in <root>.test5' type=kotlin.Int origin=null GET_VAR 'var i: kotlin.Int [var] declared in <root>.test5' type=kotlin.Int origin=null
VAR name:j type:kotlin.Int [var] VAR name:j type:kotlin.Int [var]
CONST Int type=kotlin.Int value=0 CONST Int type=kotlin.Int value=0
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=Inner origin=DO_WHILE_LOOP DO_WHILE label=Inner origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP
SET_VAR 'var j: kotlin.Int [var] declared in <root>.test5' type=kotlin.Unit origin=EQ SET_VAR 'var j: kotlin.Int [var] declared in <root>.test5' type=kotlin.Unit origin=EQ
@@ -24,9 +24,11 @@ fun testBreakWhile() {
fun testBreakDoWhile() { fun testBreakDoWhile() {
var k: Int = 0 var k: Int = 0
{ // BLOCK
dowhen { dowhen {
greater(arg0 = k, arg1 = 2) -> break greater(arg0 = k, arg1 = 2) -> break
} while (less(arg0 = k, arg1 = 10)) } while (less(arg0 = k, arg1 = 10))
}
} }
fun testContinueFor() { fun testContinueFor() {
@@ -56,6 +58,7 @@ fun testContinueWhile() {
fun testContinueDoWhile() { fun testContinueDoWhile() {
var k: Int = 0 var k: Int = 0
var s: String = "" var s: String = ""
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
k = k.inc() k = k.inc()
k /*~> Unit */ k /*~> Unit */
@@ -64,6 +67,7 @@ fun testContinueDoWhile() {
} }
s = s.plus(other = k.toString() + ";") s = s.plus(other = k.toString() + ";")
// } while (less(arg0 = k, arg1 = 10)) // } while (less(arg0 = k, arg1 = 10))
}
when { when {
EQEQ(arg0 = s, arg1 = "1;2;").not() -> throw AssertionError(p0 = s) EQEQ(arg0 = s, arg1 = "1;2;").not() -> throw AssertionError(p0 = s)
} }
@@ -47,6 +47,7 @@ FILE fqName:<root> fileName:/breakContinueInWhen.kt
BLOCK_BODY BLOCK_BODY
VAR name:k type:kotlin.Int [var] VAR name:k type:kotlin.Int [var]
CONST Int type=kotlin.Int value=0 CONST Int type=kotlin.Int value=0
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: WHEN type=kotlin.Unit origin=WHEN body: WHEN type=kotlin.Unit origin=WHEN
BRANCH BRANCH
@@ -107,6 +108,7 @@ FILE fqName:<root> fileName:/breakContinueInWhen.kt
CONST Int type=kotlin.Int value=0 CONST Int type=kotlin.Int value=0
VAR name:s type:kotlin.String [var] VAR name:s type:kotlin.String [var]
CONST String type=kotlin.String value="" CONST String type=kotlin.String value=""
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP
SET_VAR 'var k: kotlin.Int [var] declared in <root>.testContinueDoWhile' type=kotlin.Unit origin=EQ SET_VAR 'var k: kotlin.Int [var] declared in <root>.testContinueDoWhile' type=kotlin.Unit origin=EQ
@@ -12,18 +12,24 @@ fun test() {
x = <unary>.inc() x = <unary>.inc()
<unary> <unary>
} }
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
// } while (less(arg0 = x, arg1 = 0)) // } while (less(arg0 = x, arg1 = 0))
}
{ // BLOCK
do{ // BLOCK do{ // BLOCK
val <unary>: Int = x val <unary>: Int = x
x = <unary>.inc() x = <unary>.inc()
<unary> <unary>
} while (less(arg0 = x, arg1 = 15)) } while (less(arg0 = x, arg1 = 15))
}
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
val <unary>: Int = x val <unary>: Int = x
x = <unary>.inc() x = <unary>.inc()
<unary> <unary>
// } while (less(arg0 = x, arg1 = 20)) // } while (less(arg0 = x, arg1 = 20))
}
} }
fun testSmartcastInCondition() { fun testSmartcastInCondition() {
@@ -32,8 +38,10 @@ fun testSmartcastInCondition() {
a is Boolean -> { // BLOCK a is Boolean -> { // BLOCK
while (a /*as Boolean */) { // BLOCK while (a /*as Boolean */) { // BLOCK
} }
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
// } while (a /*as Boolean */) // } while (a /*as Boolean */)
} }
} }
}
} }
@@ -30,11 +30,13 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
CALL 'public final fun inc (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null CALL 'public final fun inc (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
$this: GET_VAR 'val tmp_1: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null $this: GET_VAR 'val tmp_1: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null
GET_VAR 'val tmp_1: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null GET_VAR 'val tmp_1: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
arg0: GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null arg0: GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null
arg1: CONST Int type=kotlin.Int value=0 arg1: CONST Int type=kotlin.Int value=0
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: BLOCK type=kotlin.Int origin=null body: BLOCK type=kotlin.Int origin=null
VAR IR_TEMPORARY_VARIABLE name:tmp_2 type:kotlin.Int [val] VAR IR_TEMPORARY_VARIABLE name:tmp_2 type:kotlin.Int [val]
@@ -46,6 +48,7 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
arg0: GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null arg0: GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null
arg1: CONST Int type=kotlin.Int value=15 arg1: CONST Int type=kotlin.Int value=15
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP
VAR IR_TEMPORARY_VARIABLE name:tmp_3 type:kotlin.Int [val] VAR IR_TEMPORARY_VARIABLE name:tmp_3 type:kotlin.Int [val]
@@ -70,6 +73,7 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean
GET_VAR 'val a: kotlin.Any? [val] declared in <root>.testSmartcastInCondition' type=kotlin.Any? origin=null GET_VAR 'val a: kotlin.Any? [val] declared in <root>.testSmartcastInCondition' type=kotlin.Any? origin=null
body: BLOCK type=kotlin.Unit origin=null body: BLOCK type=kotlin.Unit origin=null
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Unit origin=DO_WHILE_LOOP
condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean condition: TYPE_OP type=kotlin.Boolean origin=IMPLICIT_CAST typeOperand=kotlin.Boolean
@@ -223,6 +223,7 @@ internal class ArrayMapImpl<T : Any> : ArrayMap<T> {
private set private set
protected override fun computeNext() { protected override fun computeNext() {
{ // BLOCK
do// COMPOSITE { do// COMPOSITE {
val <unary>: Int = <this>.<get-index>() val <unary>: Int = <this>.<get-index>()
<this>.<set-index>(<set-?> = <unary>.inc()) <this>.<set-index>(<set-?> = <unary>.inc())
@@ -231,6 +232,7 @@ internal class ArrayMapImpl<T : Any> : ArrayMap<T> {
less(arg0 = <this>.<get-index>(), arg1 = <this>.<get-data>().<get-size>()) -> EQEQ(arg0 = <this>.<get-data>().get(index = <this>.<get-index>()), arg1 = null) less(arg0 = <this>.<get-index>(), arg1 = <this>.<get-data>().<get-size>()) -> EQEQ(arg0 = <this>.<get-data>().get(index = <this>.<get-index>()), arg1 = null)
else -> false else -> false
}) })
}
when { when {
greaterOrEqual(arg0 = <this>.<get-index>(), arg1 = <this>.<get-data>().<get-size>()) -> <this>.done() greaterOrEqual(arg0 = <this>.<get-index>(), arg1 = <this>.<get-data>().<get-size>()) -> <this>.done()
else -> <this>.setNext(value = <this>.<get-data>().get(index = <this>.<get-index>()) as T) else -> <this>.setNext(value = <this>.<get-data>().get(index = <this>.<get-index>()) as T)
@@ -535,6 +535,7 @@ FILE fqName:<root> fileName:/ArrayMap.kt
protected abstract fun computeNext (): kotlin.Unit declared in kotlin.collections.AbstractIterator protected abstract fun computeNext (): kotlin.Unit declared in kotlin.collections.AbstractIterator
$this: VALUE_PARAMETER name:<this> type:<root>.ArrayMapImpl.iterator.<no name provided><T of <root>.ArrayMapImpl> $this: VALUE_PARAMETER name:<this> type:<root>.ArrayMapImpl.iterator.<no name provided><T of <root>.ArrayMapImpl>
BLOCK_BODY BLOCK_BODY
BLOCK type=kotlin.Unit origin=null
DO_WHILE label=null origin=DO_WHILE_LOOP DO_WHILE label=null origin=DO_WHILE_LOOP
body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP body: COMPOSITE type=kotlin.Int origin=DO_WHILE_LOOP
VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Int [val] VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Int [val]