ForLoopsLowering: Assume step == 1 for *Range (e.g., IntRange) and

handle accordingly (e.g., do not read `step` property).
This commit is contained in:
Mark Punzalan
2020-07-23 15:21:03 -07:00
committed by Alexander Udalov
parent 09e47fff7b
commit 2cfd776092
10 changed files with 143 additions and 24 deletions
@@ -104,6 +104,8 @@ open class BuiltinSymbolsBase(protected val irBuiltIns: IrBuiltIns, protected va
open val uLong = getClassOrNull(Name.identifier("ULong"), "kotlin")
val uIntProgression = progressionOrNull("UIntProgression")
val uLongProgression = progressionOrNull("ULongProgression")
val uIntRange = progressionOrNull("UIntRange")
val uLongRange = progressionOrNull("ULongRange")
val sequence = getClassOrNull(Name.identifier("Sequence"), "kotlin", "sequences")
val charProgression = progression("CharProgression")
@@ -111,6 +113,11 @@ open class BuiltinSymbolsBase(protected val irBuiltIns: IrBuiltIns, protected va
val longProgression = progression("LongProgression")
val progressionClasses = listOfNotNull(charProgression, intProgression, longProgression, uIntProgression, uLongProgression)
val charRange = progression("CharRange")
val intRange = progression("IntRange")
val longRange = progression("LongRange")
val rangeClasses = listOfNotNull(charRange, intRange, longRange, uIntRange, uLongRange)
val getProgressionLastElementByReturnType = builtInsPackage("kotlin", "internal")
.getContributedFunctions(Name.identifier("getProgressionLastElement"), NoLookupLocation.FROM_BACKEND)
.filter { it.containingDeclaration !is BuiltInsPackageFragment }
@@ -9,8 +9,10 @@ import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.loops.*
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irInt
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.util.getPropertyGetter
@@ -20,6 +22,7 @@ internal class DefaultProgressionHandler(private val context: CommonBackendConte
ExpressionHandler {
private val symbols = context.ir.symbols
private val rangeClassesTypes = symbols.rangeClasses.map { it.defaultType }.toSet()
override fun matchIterable(expression: IrExpression) = ProgressionType.fromIrType(
expression.type,
@@ -37,9 +40,17 @@ internal class DefaultProgressionHandler(private val context: CommonBackendConte
val last = irCall(progressionClass.symbol.getPropertyGetter("last")!!).apply {
dispatchReceiver = progressionExpression.deepCopyWithSymbols()
}
val step = irCall(progressionClass.symbol.getPropertyGetter("step")!!).apply {
dispatchReceiver = progressionExpression.deepCopyWithSymbols()
// *Ranges (e.g., IntRange) have step == 1 and is always increasing.
val isRange = progressionExpression.type in rangeClassesTypes
val step = if (isRange) {
irInt(1)
} else {
irCall(progressionClass.symbol.getPropertyGetter("step")!!).apply {
dispatchReceiver = progressionExpression.deepCopyWithSymbols()
}
}
val direction = if (isRange) ProgressionDirection.INCREASING else ProgressionDirection.UNKNOWN
ProgressionHeaderInfo(
ProgressionType.fromIrType(progressionExpression.type, symbols)!!,
@@ -47,7 +58,7 @@ internal class DefaultProgressionHandler(private val context: CommonBackendConte
last,
step,
additionalStatements = listOfNotNull(progressionVar),
direction = ProgressionDirection.UNKNOWN
direction = direction
)
}
}