@@ -3224,12 +3224,11 @@ open class KotlinFileExtractor(
32243224 (cond.dispatchReceiver as ? IrGetValue )?.symbol?.owner != iteratorVariable ||
32253225 body == null ||
32263226 body.origin != IrStatementOrigin .FOR_LOOP_INNER_WHILE ||
3227- body.statements.size != 2 ) {
3227+ body.statements.size < 2 ) {
32283228 return false
32293229 }
32303230
32313231 val loopVar = body.statements[0 ] as ? IrVariable
3232- val block = body.statements[1 ] as ? IrBlock
32333232 val nextCall = loopVar?.initializer as ? IrCall
32343233
32353234 if (loopVar == null ||
@@ -3240,14 +3239,22 @@ open class KotlinFileExtractor(
32403239 return false
32413240 }
32423241
3243- val id = extractLoop(innerWhile, block, 2 , parent, callable) { p, idx ->
3242+ val id = extractLoop(innerWhile, null , parent, callable) { p, idx ->
32443243 val loopId = tw.getFreshIdLabel<DbEnhancedforstmt >()
32453244 tw.writeStmts_enhancedforstmt(loopId, p, idx, callable)
32463245 loopId
32473246 }
32483247
3249- extractExpressionExpr(expr, callable, id, 1 , id)
32503248 extractVariableExpr(loopVar, callable, id, 0 , id, extractInitializer = false )
3249+ extractExpressionExpr(expr, callable, id, 1 , id)
3250+ val block = body.statements[1 ] as ? IrBlock
3251+ if (body.statements.size == 2 && block != null ) {
3252+ // Extract the body that was given to us by the compiler
3253+ extractExpressionStmt(block, callable, id, 2 )
3254+ } else {
3255+ // Extract a block with all but the first (loop variable declaration) statement
3256+ extractBlock(body, body.statements.takeLast(body.statements.size - 1 ), id, 2 , callable)
3257+ }
32513258
32523259 return true
32533260 }
@@ -3487,14 +3494,7 @@ open class KotlinFileExtractor(
34873494 if (! tryExtractArrayUpdate(e, callable, parent) &&
34883495 ! tryExtractForLoop(e, callable, parent)) {
34893496
3490- val stmtParent = parent.stmt(e, callable)
3491- val id = tw.getFreshIdLabel<DbBlock >()
3492- val locId = tw.getLocation(e)
3493- tw.writeStmts_block(id, stmtParent.parent, stmtParent.idx, callable)
3494- tw.writeHasLocation(id, locId)
3495- e.statements.forEachIndexed { i, s ->
3496- extractStatement(s, callable, id, i)
3497- }
3497+ extractBlock(e, e.statements, parent, callable)
34983498 }
34993499 }
35003500 is IrWhileLoop -> {
@@ -3966,6 +3966,32 @@ open class KotlinFileExtractor(
39663966 }
39673967 }
39683968
3969+ private fun extractBlock (
3970+ e : IrContainerExpression ,
3971+ statements : List <IrStatement >,
3972+ parent : StmtExprParent ,
3973+ callable : Label <out DbCallable >
3974+ ) {
3975+ val stmtParent = parent.stmt(e, callable)
3976+ extractBlock(e, statements, stmtParent.parent, stmtParent.idx, callable)
3977+ }
3978+
3979+ private fun extractBlock (
3980+ e : IrElement ,
3981+ statements : List <IrStatement >,
3982+ parent : Label <out DbStmtparent >,
3983+ idx : Int ,
3984+ callable : Label <out DbCallable >
3985+ ) {
3986+ val id = tw.getFreshIdLabel<DbBlock >()
3987+ val locId = tw.getLocation(e)
3988+ tw.writeStmts_block(id, parent, idx, callable)
3989+ tw.writeHasLocation(id, locId)
3990+ statements.forEachIndexed { i, s ->
3991+ extractStatement(s, callable, id, i)
3992+ }
3993+ }
3994+
39693995 private inline fun <D : DeclarationDescriptor , reified B : IrSymbolOwner > getBoundSymbolOwner (symbol : IrBindableSymbol <D , B >, e : IrExpression ): B ? {
39703996 if (symbol.isBound) {
39713997 return symbol.owner
@@ -4074,8 +4100,7 @@ open class KotlinFileExtractor(
40744100
40754101 private fun extractLoop (
40764102 loop : IrLoop ,
4077- body : IrExpression ? ,
4078- bodyIdx : Int ,
4103+ bodyIdx : Int? ,
40794104 stmtExprParent : StmtExprParent ,
40804105 callable : Label <out DbCallable >,
40814106 getId : (Label <out DbStmtparent >, Int ) -> Label <out DbStmt >
@@ -4103,7 +4128,8 @@ open class KotlinFileExtractor(
41034128 val id = getId(parent, idx)
41044129 tw.writeHasLocation(id, locId)
41054130
4106- if (body != null ) {
4131+ val body = loop.body
4132+ if (body != null && bodyIdx != null ) {
41074133 extractExpressionStmt(body, callable, id, bodyIdx)
41084134 }
41094135
@@ -4115,7 +4141,7 @@ open class KotlinFileExtractor(
41154141 stmtExprParent : StmtExprParent ,
41164142 callable : Label <out DbCallable >
41174143 ) {
4118- val id = extractLoop(loop, loop.body, 1 , stmtExprParent, callable) { parent, idx ->
4144+ val id = extractLoop(loop, 1 , stmtExprParent, callable) { parent, idx ->
41194145 if (loop is IrWhileLoop ) {
41204146 val id = tw.getFreshIdLabel<DbWhilestmt >()
41214147 tw.writeStmts_whilestmt(id, parent, idx, callable)
0 commit comments