From bf97b332cfef994370ae9236e1ae6053a34ae65e Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 12 Dec 2017 15:07:31 +0300 Subject: [PATCH] Support const-bounded for loop generation for reversed 'until' --- ...PrimitiveNumberRangeIntrinsicRangeValue.kt | 23 ++++++++------ .../range/PrimitiveNumberUntilRangeValue.kt | 18 +++++++++++ .../forInReversedUntilWithNonConstBounds.kt | 31 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 6 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 6 ++++ 7 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/box/ranges/forInReversed/forInReversedUntilWithNonConstBounds.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeIntrinsicRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeIntrinsicRangeValue.kt index 33816be2624..05eaa393a6c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeIntrinsicRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberRangeIntrinsicRangeValue.kt @@ -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>() ?: 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 diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt index 806ce5031c0..9eccd23bed2 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/PrimitiveNumberUntilRangeValue.kt @@ -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) : @@ -32,5 +35,20 @@ class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall