[IR] Support Collection<*>.indices in for loop lowering.
Unmute a couple of tests that are working as intended.
This commit is contained in:
committed by
Alexander Udalov
parent
76ab631214
commit
507857d452
+1
-1
@@ -213,7 +213,7 @@ internal class HeaderInfoBuilder(context: CommonBackendContext, private val scop
|
|||||||
)
|
)
|
||||||
|
|
||||||
private val progressionHandlers = listOf(
|
private val progressionHandlers = listOf(
|
||||||
ArrayIndicesHandler(context),
|
CollectionIndicesHandler(context),
|
||||||
CharSequenceIndicesHandler(context),
|
CharSequenceIndicesHandler(context),
|
||||||
UntilHandler(context, progressionElementTypes),
|
UntilHandler(context, progressionElementTypes),
|
||||||
DownToHandler(context, progressionElementTypes),
|
DownToHandler(context, progressionElementTypes),
|
||||||
|
|||||||
+2
-4
@@ -222,8 +222,6 @@ internal class UntilHandler(private val context: CommonBackendContext, private v
|
|||||||
/** Builds a [HeaderInfo] for progressions built using the `indices` extension property. */
|
/** Builds a [HeaderInfo] for progressions built using the `indices` extension property. */
|
||||||
internal abstract class IndicesHandler(protected val context: CommonBackendContext) : ProgressionHandler {
|
internal abstract class IndicesHandler(protected val context: CommonBackendContext) : ProgressionHandler {
|
||||||
|
|
||||||
// TODO: Handle Collection<*>.indices
|
|
||||||
|
|
||||||
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? =
|
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? =
|
||||||
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
|
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
|
||||||
// `last = array.size - 1` (last is inclusive) for the loop `for (i in array.indices)`.
|
// `last = array.size - 1` (last is inclusive) for the loop `for (i in array.indices)`.
|
||||||
@@ -245,9 +243,9 @@ internal abstract class IndicesHandler(protected val context: CommonBackendConte
|
|||||||
abstract val IrType.sizePropertyGetter: IrSimpleFunction
|
abstract val IrType.sizePropertyGetter: IrSimpleFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class ArrayIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) {
|
internal class CollectionIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) {
|
||||||
override val matcher = SimpleCalleeMatcher {
|
override val matcher = SimpleCalleeMatcher {
|
||||||
extensionReceiver { it != null && it.type.run { isArray() || isPrimitiveArray() } }
|
extensionReceiver { it != null && it.type.run { isArray() || isPrimitiveArray() || isCollection() } }
|
||||||
fqName { it == FqName("kotlin.collections.<get-indices>") }
|
fqName { it == FqName("kotlin.collections.<get-indices>") }
|
||||||
parameterCount { it == 0 }
|
parameterCount { it == 0 }
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
fun Collection<Int>.sumIndices(): Int {
|
fun Collection<Int>.sumIndices(): Int {
|
||||||
var sum = 0
|
var sum = 0
|
||||||
for (i in indices) {
|
for (i in indices) {
|
||||||
@@ -13,6 +12,14 @@ fun Collection<Int>.sumIndices(): Int {
|
|||||||
// 0 getFirst
|
// 0 getFirst
|
||||||
// 0 getLast
|
// 0 getLast
|
||||||
|
|
||||||
// 0 IF_ICMPGT
|
// JVM non-IR uses while.
|
||||||
// 0 IF_ICMPEQ
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
|
// JVM_TEMPLATES
|
||||||
// 1 IF_ICMPGE
|
// 1 IF_ICMPGE
|
||||||
|
// 1 IF
|
||||||
|
|
||||||
|
// JVM_IR_TEMPLATES
|
||||||
|
// 1 IF_ICMPGT
|
||||||
|
// 1 IF_ICMPLE
|
||||||
|
// 2 IF
|
||||||
|
|||||||
+10
-3
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
fun test() {
|
fun test() {
|
||||||
var sum = 0
|
var sum = 0
|
||||||
for (i in listOf(0, 0, 0, 0).indices) {
|
for (i in listOf(0, 0, 0, 0).indices) {
|
||||||
@@ -12,6 +11,14 @@ fun test() {
|
|||||||
// 0 getFirst
|
// 0 getFirst
|
||||||
// 0 getLast
|
// 0 getLast
|
||||||
|
|
||||||
// 0 IF_ICMPGT
|
// JVM non-IR uses while.
|
||||||
// 0 IF_ICMPEQ
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
|
// JVM_TEMPLATES
|
||||||
// 1 IF_ICMPGE
|
// 1 IF_ICMPGE
|
||||||
|
// 1 IF
|
||||||
|
|
||||||
|
// JVM_IR_TEMPLATES
|
||||||
|
// 1 IF_ICMPGT
|
||||||
|
// 1 IF_ICMPLE
|
||||||
|
// 2 IF
|
||||||
|
|||||||
+17
-3
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
fun test(): Int {
|
fun test(): Int {
|
||||||
val intArray = intArrayOf(1, 2, 3)
|
val intArray = intArrayOf(1, 2, 3)
|
||||||
var sum = 0
|
var sum = 0
|
||||||
@@ -8,6 +7,21 @@ fun test(): Int {
|
|||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 0 iterator
|
||||||
|
// 0 getStart
|
||||||
|
// 0 getEnd
|
||||||
|
// 0 getFirst
|
||||||
|
// 0 getLast
|
||||||
|
// 0 getStep
|
||||||
|
|
||||||
|
// JVM non-IR uses while.
|
||||||
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
|
// JVM_TEMPLATES
|
||||||
// 1 IF_ICMPGE
|
// 1 IF_ICMPGE
|
||||||
// 0 IF_ICMPGT
|
// 1 IF
|
||||||
// 0 IF_ICMPEQ
|
|
||||||
|
// JVM_IR_TEMPLATES
|
||||||
|
// 1 IF_ICMPGT
|
||||||
|
// 1 IF_ICMPNE
|
||||||
|
// 2 IF
|
||||||
|
|||||||
Reference in New Issue
Block a user