From 735535dd5a9c2ad94b41748784931d7b23dd0364 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Fri, 1 Nov 2019 13:01:23 -0700 Subject: [PATCH] Extract HeaderInfo base class and add NumericHeaderInfo class, in preparation for handling withIndex(). --- .../backend/common/lower/loops/HeaderInfo.kt | 24 ++++++++++--------- .../common/lower/loops/HeaderProcessor.kt | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt index 8fc6b4583d8..194de40cbea 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt @@ -67,7 +67,16 @@ internal enum class ProgressionDirection { } /** Information about a loop that is required by [HeaderProcessor] to build a [ForLoopHeader]. */ -internal sealed class HeaderInfo( +internal sealed class HeaderInfo { + /** + * Returns a copy of this [HeaderInfo] with the values reversed. + * I.e., first and last are swapped, step is negated. + * Returns null if the iterable cannot be iterated in reverse. + */ + abstract fun asReversed(): HeaderInfo? +} + +internal sealed class NumericHeaderInfo( val progressionType: ProgressionType, val first: IrExpression, val last: IrExpression, @@ -76,14 +85,7 @@ internal sealed class HeaderInfo( val isReversed: Boolean, val direction: ProgressionDirection, val additionalNotEmptyCondition: IrExpression? -) { - /** - * Returns a copy of this [HeaderInfo] with the values reversed. - * I.e., first and last are swapped, step is negated. - * Returns null if the iterable cannot be iterated in reverse. - */ - abstract fun asReversed(): HeaderInfo? -} +) : HeaderInfo() /** Information about a for-loop over a progression. */ internal class ProgressionHeaderInfo( @@ -96,7 +98,7 @@ internal class ProgressionHeaderInfo( direction: ProgressionDirection, additionalNotEmptyCondition: IrExpression? = null, val additionalVariables: List = listOf() -) : HeaderInfo( +) : NumericHeaderInfo( progressionType, first, last, step, canCacheLast = true, isReversed = isReversed, @@ -175,7 +177,7 @@ internal class IndexedGetHeaderInfo( canCacheLast: Boolean = true, val objectVariable: IrVariable, val expressionHandler: IndexedGetIterationHandler -) : HeaderInfo( +) : NumericHeaderInfo( ProgressionType.INT_PROGRESSION, first, last, diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt index d1b4d84d52a..a596e1eda4b 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt @@ -53,7 +53,7 @@ internal interface ForLoopHeader { } internal abstract class NumericForLoopHeader( - headerInfo: HeaderInfo, + headerInfo: NumericHeaderInfo, builder: DeclarationIrBuilder, protected val isLastInclusive: Boolean ) : ForLoopHeader { @@ -123,7 +123,7 @@ internal abstract class NumericForLoopHeader( // This cannot be declared and initialized directly in the constructor. At the time of the base class (ForLoopHeader) constructor // execution, the `headerInfo` property overridden in the derived class (e.g., NumericForLoopHeader) is not yet initialized. // Therefore, we must use the `headerInfo` constructor parameter in the "init" block above instead of using the property. - protected open val headerInfo: HeaderInfo = headerInfo + protected open val headerInfo: NumericHeaderInfo = headerInfo private fun DeclarationIrBuilder.ensureNotNullable(expression: IrExpression) = if (expression.type is IrSimpleType && expression.type.isNullable()) {