Kotlin: Resugar for loops with tuples as loop variables

This commit is contained in:
Tamas Vajk
2022-10-27 15:59:30 +02:00
parent 841340b266
commit 803a97df7f
5 changed files with 72 additions and 66 deletions

View File

@@ -3224,12 +3224,11 @@ open class KotlinFileExtractor(
(cond.dispatchReceiver as? IrGetValue)?.symbol?.owner != iteratorVariable ||
body == null ||
body.origin != IrStatementOrigin.FOR_LOOP_INNER_WHILE ||
body.statements.size != 2) {
body.statements.size < 2) {
return false
}
val loopVar = body.statements[0] as? IrVariable
val block = body.statements[1] as? IrBlock
val nextCall = loopVar?.initializer as? IrCall
if (loopVar == null ||
@@ -3240,14 +3239,22 @@ open class KotlinFileExtractor(
return false
}
val id = extractLoop(innerWhile, block, 2, parent, callable) { p, idx ->
val id = extractLoop(innerWhile, null, parent, callable) { p, idx ->
val loopId = tw.getFreshIdLabel<DbEnhancedforstmt>()
tw.writeStmts_enhancedforstmt(loopId, p, idx, callable)
loopId
}
extractExpressionExpr(expr, callable, id, 1, id)
extractVariableExpr(loopVar, callable, id, 0, id, extractInitializer = false)
extractExpressionExpr(expr, callable, id, 1, id)
val block = body.statements[1] as? IrBlock
if (body.statements.size == 2 && block != null) {
// Extract the body that was given to us by the compiler
extractExpressionStmt(block, callable, id, 2)
} else {
// Extract a block with all but the first (loop variable declaration) statement
extractBlock(body, body.statements.takeLast(body.statements.size - 1), id, 2, callable)
}
return true
}
@@ -3487,14 +3494,7 @@ open class KotlinFileExtractor(
if (!tryExtractArrayUpdate(e, callable, parent) &&
!tryExtractForLoop(e, callable, parent)) {
val stmtParent = parent.stmt(e, callable)
val id = tw.getFreshIdLabel<DbBlock>()
val locId = tw.getLocation(e)
tw.writeStmts_block(id, stmtParent.parent, stmtParent.idx, callable)
tw.writeHasLocation(id, locId)
e.statements.forEachIndexed { i, s ->
extractStatement(s, callable, id, i)
}
extractBlock(e, e.statements, parent, callable)
}
}
is IrWhileLoop -> {
@@ -3966,6 +3966,32 @@ open class KotlinFileExtractor(
}
}
private fun extractBlock(
e: IrContainerExpression,
statements: List<IrStatement>,
parent: StmtExprParent,
callable: Label<out DbCallable>
) {
val stmtParent = parent.stmt(e, callable)
extractBlock(e, statements, stmtParent.parent, stmtParent.idx, callable)
}
private fun extractBlock(
e: IrElement,
statements: List<IrStatement>,
parent: Label<out DbStmtparent>,
idx: Int,
callable: Label<out DbCallable>
) {
val id = tw.getFreshIdLabel<DbBlock>()
val locId = tw.getLocation(e)
tw.writeStmts_block(id, parent, idx, callable)
tw.writeHasLocation(id, locId)
statements.forEachIndexed { i, s ->
extractStatement(s, callable, id, i)
}
}
private inline fun <D: DeclarationDescriptor, reified B: IrSymbolOwner> getBoundSymbolOwner(symbol: IrBindableSymbol<D, B>, e: IrExpression): B? {
if (symbol.isBound) {
return symbol.owner
@@ -4074,8 +4100,7 @@ open class KotlinFileExtractor(
private fun extractLoop(
loop: IrLoop,
body: IrExpression?,
bodyIdx: Int,
bodyIdx: Int?,
stmtExprParent: StmtExprParent,
callable: Label<out DbCallable>,
getId: (Label<out DbStmtparent>, Int) -> Label<out DbStmt>
@@ -4103,7 +4128,8 @@ open class KotlinFileExtractor(
val id = getId(parent, idx)
tw.writeHasLocation(id, locId)
if (body != null) {
val body = loop.body
if (body != null && bodyIdx != null) {
extractExpressionStmt(body, callable, id, bodyIdx)
}
@@ -4115,7 +4141,7 @@ open class KotlinFileExtractor(
stmtExprParent: StmtExprParent,
callable: Label<out DbCallable>
) {
val id = extractLoop(loop, loop.body, 1, stmtExprParent, callable) { parent, idx ->
val id = extractLoop(loop, 1, stmtExprParent, callable) { parent, idx ->
if (loop is IrWhileLoop) {
val id = tw.getFreshIdLabel<DbWhilestmt>()
tw.writeStmts_whilestmt(id, parent, idx, callable)

View File

@@ -151,40 +151,32 @@ stmts.kt:
# 37| 0: [VarAccess] x
# 37| 1: [VarAccess] y
# 37| 1: [BreakStmt] break
# 41| 4: [BlockStmt] { ... }
# 41| 0: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] tmp2_iterator
# 41| 0: [MethodAccess] iterator(...)
# 41| -1: [MethodAccess] withIndex(...)
# 41| -2: [TypeAccess] Integer
# 41| -1: [TypeAccess] CollectionsKt
# 41| 0: [MethodAccess] rangeTo(...)
# 41| -1: [VarAccess] x
# 41| 0: [VarAccess] y
# 41| 1: [WhileStmt] while (...)
# 41| 0: [MethodAccess] hasNext(...)
# 41| -1: [VarAccess] tmp2_iterator
# 41| 1: [BlockStmt] { ... }
# 41| 0: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] tmp3_loop_parameter
# 41| 0: [MethodAccess] next(...)
# 41| -1: [VarAccess] tmp2_iterator
# 41| 1: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] v
# 41| 0: [MethodAccess] component1(...)
# 41| -1: [VarAccess] tmp3_loop_parameter
# 41| 2: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] i
# 41| 0: [MethodAccess] component2(...)
# 41| -1: [VarAccess] tmp3_loop_parameter
# 41| 3: [BlockStmt] { ... }
# 42| 0: [ExprStmt] <Expr>;
# 42| 0: [WhenExpr] when ...
# 42| 0: [WhenBranch] ... -> ...
# 42| 0: [GTExpr] ... > ...
# 42| 0: [VarAccess] x
# 42| 1: [VarAccess] y
# 42| 1: [BreakStmt] break
# 41| 4: [EnhancedForStmt] for (... : ...)
#-----| 0: (Single Local Variable Declaration)
# 41| 1: [LocalVariableDeclExpr] tmp3_loop_parameter
# 41| 1: [MethodAccess] withIndex(...)
# 41| -2: [TypeAccess] Integer
# 41| -1: [TypeAccess] CollectionsKt
# 41| 0: [MethodAccess] rangeTo(...)
# 41| -1: [VarAccess] x
# 41| 0: [VarAccess] y
# 41| 2: [BlockStmt] { ... }
# 41| 0: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] v
# 41| 0: [MethodAccess] component1(...)
# 41| -1: [VarAccess] tmp3_loop_parameter
# 41| 1: [LocalVariableDeclStmt] var ...;
# 41| 1: [LocalVariableDeclExpr] i
# 41| 0: [MethodAccess] component2(...)
# 41| -1: [VarAccess] tmp3_loop_parameter
# 41| 2: [BlockStmt] { ... }
# 42| 0: [ExprStmt] <Expr>;
# 42| 0: [WhenExpr] when ...
# 42| 0: [WhenBranch] ... -> ...
# 42| 0: [GTExpr] ... > ...
# 42| 0: [VarAccess] x
# 42| 1: [VarAccess] y
# 42| 1: [BreakStmt] break
# 46| 3: [Method] exceptions
# 46| 3: [TypeAccess] int
# 46| 5: [BlockStmt] { ... }

View File

@@ -85,12 +85,6 @@
| stmts.kt:41:11:41:11 | v | LocalVariableDeclExpr |
| stmts.kt:41:13:41:13 | component2(...) | MethodAccess |
| stmts.kt:41:13:41:13 | i | LocalVariableDeclExpr |
| stmts.kt:41:19:41:36 | hasNext(...) | MethodAccess |
| stmts.kt:41:19:41:36 | iterator(...) | MethodAccess |
| stmts.kt:41:19:41:36 | next(...) | MethodAccess |
| stmts.kt:41:19:41:36 | tmp2_iterator | LocalVariableDeclExpr |
| stmts.kt:41:19:41:36 | tmp2_iterator | VarAccess |
| stmts.kt:41:19:41:36 | tmp2_iterator | VarAccess |
| stmts.kt:41:19:41:36 | tmp3_loop_parameter | LocalVariableDeclExpr |
| stmts.kt:41:19:41:36 | tmp3_loop_parameter | VarAccess |
| stmts.kt:41:19:41:36 | tmp3_loop_parameter | VarAccess |

View File

@@ -7,4 +7,4 @@ jumpTarget
| stmts.kt:29:9:29:16 | continue | stmts.kt:28:5:29:16 | while (...) |
| stmts.kt:32:20:32:24 | break | stmts.kt:31:5:33:5 | for (... : ...) |
| stmts.kt:37:24:37:36 | break | stmts.kt:35:13:39:5 | for (... : ...) |
| stmts.kt:42:20:42:24 | break | stmts.kt:41:5:43:5 | while (...) |
| stmts.kt:42:20:42:24 | break | stmts.kt:41:5:43:5 | for (... : ...) |

View File

@@ -54,13 +54,10 @@ enclosing
| stmts.kt:37:13:37:36 | ... -> ... | stmts.kt:37:13:37:36 | <Expr>; |
| stmts.kt:37:13:37:36 | <Expr>; | stmts.kt:36:13:38:9 | { ... } |
| stmts.kt:37:24:37:36 | break | stmts.kt:37:13:37:36 | ... -> ... |
| stmts.kt:41:5:43:5 | while (...) | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:41:5:43:5 | { ... } | stmts.kt:22:27:44:1 | { ... } |
| stmts.kt:41:5:43:5 | { ... } | stmts.kt:41:5:43:5 | while (...) |
| stmts.kt:41:5:43:5 | for (... : ...) | stmts.kt:22:27:44:1 | { ... } |
| stmts.kt:41:5:43:5 | { ... } | stmts.kt:41:5:43:5 | for (... : ...) |
| stmts.kt:41:11:41:11 | var ...; | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:41:13:41:13 | var ...; | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:41:19:41:36 | var ...; | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:41:19:41:36 | var ...; | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:41:39:43:5 | { ... } | stmts.kt:41:5:43:5 | { ... } |
| stmts.kt:42:9:42:24 | ... -> ... | stmts.kt:42:9:42:24 | <Expr>; |
| stmts.kt:42:9:42:24 | <Expr>; | stmts.kt:41:39:43:5 | { ... } |
@@ -131,13 +128,10 @@ enclosing
| stmts.kt:37:13:37:36 | ... -> ... | WhenBranch |
| stmts.kt:37:13:37:36 | <Expr>; | ExprStmt |
| stmts.kt:37:24:37:36 | break | BreakStmt |
| stmts.kt:41:5:43:5 | while (...) | WhileStmt |
| stmts.kt:41:5:43:5 | { ... } | BlockStmt |
| stmts.kt:41:5:43:5 | for (... : ...) | EnhancedForStmt |
| stmts.kt:41:5:43:5 | { ... } | BlockStmt |
| stmts.kt:41:11:41:11 | var ...; | LocalVariableDeclStmt |
| stmts.kt:41:13:41:13 | var ...; | LocalVariableDeclStmt |
| stmts.kt:41:19:41:36 | var ...; | LocalVariableDeclStmt |
| stmts.kt:41:19:41:36 | var ...; | LocalVariableDeclStmt |
| stmts.kt:41:39:43:5 | { ... } | BlockStmt |
| stmts.kt:42:9:42:24 | ... -> ... | WhenBranch |
| stmts.kt:42:9:42:24 | <Expr>; | ExprStmt |