Rename "lowerBound/upperBound" to "first/last" and move loopVariable to

ForLoopHeader base class.

These terms are less loaded and less confusing, especially with "downTo"
progressions, and once we start handling "reversed()".
This commit is contained in:
Mark Punzalan
2019-03-19 23:24:58 -07:00
committed by max-kammerer
parent 8e1be5424f
commit 93deff5e57
4 changed files with 26 additions and 28 deletions
@@ -8,17 +8,17 @@ package org.jetbrains.kotlin.backend.common.lower.loops
import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irIfThen import org.jetbrains.kotlin.backend.common.lower.irIfThen
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.irCallOp
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irSetVar
import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.types.toKotlinType import org.jetbrains.kotlin.ir.types.toKotlinType
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
@@ -178,9 +178,7 @@ private class RangeLoopTransformer(
forLoopInfo.inductionVariable.type.toKotlinType(), forLoopInfo.inductionVariable.type.toKotlinType(),
forLoopInfo.step.type.toKotlinType() forLoopInfo.step.type.toKotlinType()
) )
if (forLoopInfo is ProgressionLoopHeader) { forLoopInfo.loopVariable = variable
forLoopInfo.loopVariable = variable
}
return with(context.createIrBuilder(getScopeOwnerSymbol(), initializer.startOffset, initializer.endOffset)) { return with(context.createIrBuilder(getScopeOwnerSymbol(), initializer.startOffset, initializer.endOffset)) {
variable.initializer = forLoopInfo.initializeLoopVariable(symbols, this) variable.initializer = forLoopInfo.initializeLoopVariable(symbols, this)
val increment = irSetVar( val increment = irSetVar(
@@ -58,8 +58,8 @@ internal enum class ProgressionDirection { DECREASING, INCREASING, UNKNOWN }
// Information about loop that is required by HeaderProcessor to build ForLoopHeader // Information about loop that is required by HeaderProcessor to build ForLoopHeader
internal sealed class HeaderInfo( internal sealed class HeaderInfo(
val progressionType: ProgressionType, val progressionType: ProgressionType,
val lowerBound: IrExpression, val first: IrExpression,
val upperBound: IrExpression, val last: IrExpression,
val step: IrExpression, val step: IrExpression,
val closed: Boolean val closed: Boolean
) { ) {
@@ -78,22 +78,22 @@ internal sealed class HeaderInfo(
internal class ProgressionHeaderInfo( internal class ProgressionHeaderInfo(
progressionType: ProgressionType, progressionType: ProgressionType,
lowerBound: IrExpression, first: IrExpression,
upperBound: IrExpression, last: IrExpression,
step: IrExpression, step: IrExpression,
closed: Boolean = true, closed: Boolean = true,
val additionalNotEmptyCondition: IrExpression? = null val additionalNotEmptyCondition: IrExpression? = null
) : HeaderInfo(progressionType, lowerBound, upperBound, step, closed) ) : HeaderInfo(progressionType, first, last, step, closed)
internal class ArrayHeaderInfo( internal class ArrayHeaderInfo(
lowerBound: IrExpression, first: IrExpression,
upperBound: IrExpression, last: IrExpression,
step: IrExpression, step: IrExpression,
val arrayVariable: IrVariable val arrayVariable: IrVariable
) : HeaderInfo( ) : HeaderInfo(
ProgressionType.INT_PROGRESSION, ProgressionType.INT_PROGRESSION,
lowerBound, first,
upperBound, last,
step, step,
closed = false closed = false
) )
@@ -32,7 +32,8 @@ internal sealed class ForLoopHeader(
val last: IrVariable, val last: IrVariable,
val step: IrVariable, val step: IrVariable,
val progressionType: ProgressionType, val progressionType: ProgressionType,
val needsEmptinessCheck: Boolean val needsEmptinessCheck: Boolean,
var loopVariable: IrVariable? = null
) { ) {
abstract fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder): IrExpression abstract fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder): IrExpression
@@ -48,8 +49,7 @@ internal class ProgressionLoopHeader(
inductionVariable: IrVariable, inductionVariable: IrVariable,
bound: IrVariable, bound: IrVariable,
last: IrVariable, last: IrVariable,
step: IrVariable, step: IrVariable
var loopVariable: IrVariable? = null
) : ForLoopHeader(inductionVariable, bound, last, step, headerInfo.progressionType, needsEmptinessCheck = true) { ) : ForLoopHeader(inductionVariable, bound, last, step, headerInfo.progressionType, needsEmptinessCheck = true) {
override fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder) = with(builder) { override fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder) = with(builder) {
@@ -200,14 +200,14 @@ internal class HeaderProcessor(
// Due to features of PSI2IR we can obtain nullable arguments here while actually // Due to features of PSI2IR we can obtain nullable arguments here while actually
// they are non-nullable (the frontend takes care about this). So we need to cast them to non-nullable. // they are non-nullable (the frontend takes care about this). So we need to cast them to non-nullable.
val inductionVariable = scope.createTemporaryVariable( val inductionVariable = scope.createTemporaryVariable(
lowerBound.castIfNecessary(progressionType), first.castIfNecessary(progressionType),
nameHint = "inductionVariable", nameHint = "inductionVariable",
isMutable = true, isMutable = true,
origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE
) )
val upperBoundTmpVariable = scope.createTemporaryVariable( val upperBoundTmpVariable = scope.createTemporaryVariable(
ensureNotNullable(upperBound.castIfNecessary(progressionType)), ensureNotNullable(last.castIfNecessary(progressionType)),
nameHint = "upperBound", nameHint = "upperBound",
origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE
) )
@@ -38,8 +38,8 @@ internal class RangeToHandler(private val context: CommonBackendContext, private
with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) { with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) {
ProgressionHeaderInfo( ProgressionHeaderInfo(
data, data,
lowerBound = call.dispatchReceiver!!, first = call.dispatchReceiver!!,
upperBound = call.getValueArgument(0)!!, last = call.getValueArgument(0)!!,
step = irInt(1) step = irInt(1)
) )
} }
@@ -57,8 +57,8 @@ internal class DownToHandler(private val context: CommonBackendContext, private
with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) { with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) {
ProgressionHeaderInfo( ProgressionHeaderInfo(
data, data,
lowerBound = call.extensionReceiver!!, first = call.extensionReceiver!!,
upperBound = call.getValueArgument(0)!!, last = call.getValueArgument(0)!!,
step = irInt(-1) step = irInt(-1)
) )
} }
@@ -98,8 +98,8 @@ internal class UntilHandler(private val context: CommonBackendContext, private v
// TODO: Do not add additionalEmptinessCondition if "bound" is const and > MIN_VALUE // TODO: Do not add additionalEmptinessCondition if "bound" is const and > MIN_VALUE
ProgressionHeaderInfo( ProgressionHeaderInfo(
data, data,
lowerBound = call.extensionReceiver!!, first = call.extensionReceiver!!,
upperBound = call.getValueArgument(0)!!, last = call.getValueArgument(0)!!,
step = irInt(1), step = irInt(1),
closed = false, closed = false,
additionalNotEmptyCondition = buildMinValueConditionIfNecessary(data, call.getValueArgument(0)!!) additionalNotEmptyCondition = buildMinValueConditionIfNecessary(data, call.getValueArgument(0)!!)
@@ -194,8 +194,8 @@ internal class ArrayIterationHandler(val context: CommonBackendContext) : Header
dispatchReceiver = irGet(arrayReference) dispatchReceiver = irGet(arrayReference)
} }
ArrayHeaderInfo( ArrayHeaderInfo(
lowerBound = irInt(0), first = irInt(0),
upperBound = upperBound, last = upperBound,
step = irInt(1), step = irInt(1),
arrayVariable = arrayReference arrayVariable = arrayReference
) )