Support const-bounded for loop generation for reversed 'until'

This commit is contained in:
Dmitry Petrov
2017-12-12 15:07:31 +03:00
parent 54cceac99b
commit bf97b332cf
7 changed files with 86 additions and 10 deletions
@@ -76,7 +76,8 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
forExpression: KtForExpression,
startValue: StackValue,
endExpression: KtExpression,
step: Int
step: Int,
isStartInclusive: Boolean = true
): ForLoopGenerator? {
val endConstValue = codegen.getCompileTimeConstant(endExpression).safeAs<IntegerValueConstant<*>>() ?: return null
@@ -86,7 +87,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
if (isProhibitedIntConstEndValue(step, endIntValue))
null
else
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step)
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step, isStartInclusive)
}
is ShortValue -> {
@@ -94,7 +95,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
if (isProhibitedIntConstEndValue(step, endIntValue))
null
else
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step)
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step, isStartInclusive)
}
is IntValue -> {
@@ -102,7 +103,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
if (isProhibitedIntConstEndValue(step, endIntValue))
null
else
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step)
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endIntValue, step, isStartInclusive)
}
is CharValue -> {
@@ -110,7 +111,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
if (isProhibitedCharConstEndValue(step, endCharValue))
null
else
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endCharValue.toInt(), step)
createConstBoundedIntForLoopGenerator(codegen, forExpression, startValue, endCharValue.toInt(), step, isStartInclusive)
}
is LongValue -> {
@@ -118,7 +119,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
if (isProhibitedLongConstEndValue(step, endLongValue))
null
else
createConstBoundedLongForLoopGenerator(codegen, forExpression, startValue, endLongValue, step)
createConstBoundedLongForLoopGenerator(codegen, forExpression, startValue, endLongValue, step, isStartInclusive)
}
else -> null
@@ -130,12 +131,13 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
forExpression: KtForExpression,
startValue: StackValue,
endIntValue: Int,
step: Int
step: Int,
isStartInclusive: Boolean
): ForLoopGenerator? =
ForInDefinitelySafeSimpleProgressionLoopGenerator(
codegen, forExpression,
startValue = startValue,
isStartInclusive = true,
isStartInclusive = isStartInclusive,
endValue = StackValue.integerConstant(endIntValue, asmElementType),
isEndInclusive = true,
step = step
@@ -146,12 +148,13 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
forExpression: KtForExpression,
startValue: StackValue,
endLongValue: Long,
step: Int
step: Int,
isStartInclusive: Boolean
): ForLoopGenerator? =
ForInDefinitelySafeSimpleProgressionLoopGenerator(
codegen, forExpression,
startValue = startValue,
isStartInclusive = true,
isStartInclusive = isStartInclusive,
endValue = StackValue.constant(endLongValue, asmElementType),
isEndInclusive = true,
step = step
@@ -17,9 +17,12 @@
package org.jetbrains.kotlin.codegen.range
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.generateCallSingleArgument
import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.codegen.range.forLoop.ForLoopGenerator
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getReceiverExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall<out CallableDescriptor>) :
@@ -32,5 +35,20 @@ class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall<out CallableDescrip
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1(codegen, forExpression, getBoundedValue(codegen))
override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression) =
createConstBoundedForInReversedUntilGenerator(codegen, forExpression) ?:
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1(codegen, forExpression, getBoundedValue(codegen))
private fun createConstBoundedForInReversedUntilGenerator(
codegen: ExpressionCodegen,
forExpression: KtForExpression
): ForLoopGenerator? {
val endExpression = rangeCall.getReceiverExpression() ?: return null
return createConstBoundedForLoopGeneratorOrNull(
codegen, forExpression,
codegen.generateCallSingleArgument(rangeCall),
endExpression,
step = -1,
isStartInclusive = false
)
}
}