Refactor: SimpleBoundedValue, AbstractBoundedValue -> BoundedValue class

This commit is contained in:
Dmitry Petrov
2019-06-20 14:36:16 +03:00
parent bfeb9537ee
commit 0b580b2741
14 changed files with 27 additions and 73 deletions
@@ -1,26 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen.range
import org.jetbrains.org.objectweb.asm.Type
abstract class AbstractBoundedValue(
override val instanceType: Type,
override val isLowInclusive: Boolean = true,
override val isHighInclusive: Boolean = true
) : BoundedValue {
}
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.generateCallReceiver
import org.jetbrains.kotlin.codegen.range.comparison.IntComparisonGenerator
import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType
import org.jetbrains.kotlin.codegen.range.forLoop.ForInDefinitelySafeSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.codegen.range.forLoop.ForLoopGenerator
@@ -36,8 +35,7 @@ class ArrayIndicesRangeValue(rangeCall: ResolvedCall<out CallableDescriptor>) :
private val expectedReceiverType: KotlinType = ExpressionCodegen.getExpectedReceiverType(rangeCall)
override fun getBoundedValue(codegen: ExpressionCodegen) =
SimpleBoundedValue(
codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
BoundedValue(
StackValue.constant(0, codegen.asmType(elementKotlinType), elementKotlinType),
true,
StackValue.operation(Type.INT_TYPE) { v ->
@@ -24,15 +24,16 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
/**
* Low level abstraction for bounded range that is used to generate contains checks and for loops.
*/
class SimpleBoundedValue(
instanceType: Type,
class BoundedValue(
val lowBound: StackValue,
isLowInclusive: Boolean = true,
val isLowInclusive: Boolean = true,
val highBound: StackValue,
isHighInclusive: Boolean = true
) : AbstractBoundedValue(instanceType, isLowInclusive, isHighInclusive) {
val isHighInclusive: Boolean = true
) {
override fun putHighLow(v: InstructionAdapter, type: Type) {
// It is necessary to maintain the proper evaluation order as of Kotlin 1.0 and 1.1
// to evaluate range bounds left to right and put them on stack as 'high; low'.
fun putHighLow(v: InstructionAdapter, type: Type) {
if (!lowBound.canHaveSideEffects() || !highBound.canHaveSideEffects()) {
highBound.put(type, v)
lowBound.put(type, v)
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.generateCallReceiver
import org.jetbrains.kotlin.codegen.range.comparison.IntComparisonGenerator
import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType
import org.jetbrains.kotlin.codegen.range.forLoop.ForInDefinitelySafeSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.descriptors.CallableDescriptor
@@ -35,8 +34,7 @@ class CharSequenceIndicesRangeValue(rangeCall: ResolvedCall<out CallableDescript
private val expectedReceiverType: KotlinType = ExpressionCodegen.getExpectedReceiverType(rangeCall)
override fun getBoundedValue(codegen: ExpressionCodegen) =
SimpleBoundedValue(
codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
BoundedValue(
StackValue.constant(0, codegen.asmType(elementKotlinType), elementKotlinType),
true,
StackValue.operation(Type.INT_TYPE) { v ->
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.generateCallReceiver
import org.jetbrains.kotlin.codegen.range.comparison.IntComparisonGenerator
import org.jetbrains.kotlin.codegen.range.comparison.getComparisonGeneratorForKotlinType
import org.jetbrains.kotlin.codegen.range.forLoop.ForInDefinitelySafeSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.codegen.range.forLoop.ForInSimpleProgressionLoopGenerator
import org.jetbrains.kotlin.descriptors.CallableDescriptor
@@ -35,8 +34,7 @@ class CollectionIndicesRangeValue(rangeCall: ResolvedCall<out CallableDescriptor
private val expectedReceiverType: KotlinType = ExpressionCodegen.getExpectedReceiverType(rangeCall)
override fun getBoundedValue(codegen: ExpressionCodegen) =
SimpleBoundedValue(
codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
BoundedValue(
StackValue.constant(0, codegen.asmType(elementKotlinType), elementKotlinType),
true,
StackValue.operation(Type.INT_TYPE) { v ->
@@ -30,8 +30,7 @@ class ComparableRangeLiteralRangeValue(
codegen: ExpressionCodegen,
rangeCall: ResolvedCall<out CallableDescriptor>
) : CallIntrinsicRangeValue(rangeCall) {
private val boundedValue = SimpleBoundedValue(
instanceType = codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
private val boundedValue = BoundedValue(
lowBound = codegen.generateCallReceiver(rangeCall),
highBound = codegen.generateCallSingleArgument(rangeCall)
)
@@ -32,8 +32,7 @@ class DownToProgressionRangeValue(rangeCall: ResolvedCall<out CallableDescriptor
PrimitiveNumberRangeIntrinsicRangeValue(rangeCall), ReversableRangeValue {
override fun getBoundedValue(codegen: ExpressionCodegen) =
SimpleBoundedValue(
instanceType = codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
BoundedValue(
lowBound = codegen.generateCallSingleArgument(rangeCall),
highBound = codegen.generateCallReceiver(rangeCall)
)
@@ -63,7 +63,7 @@ abstract class PrimitiveNumberRangeIntrinsicRangeValue(
comparisonGenerator == null -> CallBasedInExpressionGenerator(codegen, operatorReference)
comparedType == Type.DOUBLE_TYPE || comparedType == Type.FLOAT_TYPE -> {
val rangeLiteral = getBoundedValue(codegen) as? SimpleBoundedValue
val rangeLiteral = getBoundedValue(codegen) as? BoundedValue
?: throw AssertionError("Floating point intrinsic range value should be a range literal")
InFloatingPointRangeLiteralExpressionGenerator(operatorReference, rangeLiteral, comparisonGenerator, codegen.frameMap)
}
@@ -36,15 +36,14 @@ class PrimitiveNumberRangeLiteralRangeValue(
) : PrimitiveNumberRangeIntrinsicRangeValue(rangeCall),
ReversableRangeValue {
override fun getBoundedValue(codegen: ExpressionCodegen): SimpleBoundedValue {
override fun getBoundedValue(codegen: ExpressionCodegen): BoundedValue {
val instanceType = codegen.asmType(rangeCall.resultingDescriptor.returnType!!)
val lowBound = codegen.generateCallReceiver(rangeCall)
if (codegen.canBeSpecializedByExcludingHighBound(rangeCall)) {
val highBound = (rangeCall.getFirstArgumentExpression() as KtBinaryExpression).left
return SimpleBoundedValue(instanceType, lowBound, true, codegen.gen(highBound), false)
return BoundedValue(lowBound, true, codegen.gen(highBound), false)
}
return SimpleBoundedValue(
instanceType = instanceType,
return BoundedValue(
lowBound = lowBound,
highBound = codegen.generateCallSingleArgument(rangeCall)
)
@@ -31,8 +31,7 @@ class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall<out CallableDescrip
PrimitiveNumberRangeIntrinsicRangeValue(rangeCall), ReversableRangeValue {
override fun getBoundedValue(codegen: ExpressionCodegen) =
SimpleBoundedValue(
codegen.asmType(rangeCall.resultingDescriptor.returnType!!),
BoundedValue(
codegen.generateCallReceiver(rangeCall),
true,
codegen.generateCallSingleArgument(rangeCall),
@@ -24,14 +24,3 @@ interface ReversableRangeValue : RangeValue {
fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator
}
interface BoundedValue {
val instanceType: Type
// It is necessary to maintain the proper evaluation order as of Kotlin 1.0 and 1.1
// to evaluate range bounds left to right and put them on stack as 'high; low'.
fun putHighLow(v: InstructionAdapter, type: Type)
val isLowInclusive: Boolean
val isHighInclusive: Boolean
}
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.codegen.range.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.range.SimpleBoundedValue
import org.jetbrains.kotlin.codegen.range.BoundedValue
import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.org.objectweb.asm.Label
@@ -52,7 +52,7 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator(
constructor(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
comparisonGenerator: ComparisonGenerator,
step: Int
) : this(
@@ -69,7 +69,7 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator(
fun fromBoundedValueWithStep1(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
comparisonGenerator: ComparisonGenerator
) =
ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, comparisonGenerator, 1)
@@ -77,7 +77,7 @@ class ForInDefinitelySafeSimpleProgressionLoopGenerator(
fun fromBoundedValueWithStepMinus1(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
comparisonGenerator: ComparisonGenerator
) =
ForInDefinitelySafeSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, comparisonGenerator, -1)
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.codegen.range.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.range.SimpleBoundedValue
import org.jetbrains.kotlin.codegen.range.BoundedValue
import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.org.objectweb.asm.Label
@@ -38,7 +38,7 @@ class ForInSimpleProgressionLoopGenerator(
constructor(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
inverseBoundsEvaluationOrder: Boolean,
comparisonGenerator: ComparisonGenerator,
step: Int
@@ -57,7 +57,7 @@ class ForInSimpleProgressionLoopGenerator(
fun fromBoundedValueWithStep1(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
comparisonGenerator: ComparisonGenerator,
inverseBoundsEvaluationOrder: Boolean = false
) =
@@ -66,7 +66,7 @@ class ForInSimpleProgressionLoopGenerator(
fun fromBoundedValueWithStepMinus1(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
boundedValue: SimpleBoundedValue,
boundedValue: BoundedValue,
comparisonGenerator: ComparisonGenerator,
inverseBoundsEvaluationOrder: Boolean = false
) =
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.codegen.range.inExpression
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.range.SimpleBoundedValue
import org.jetbrains.kotlin.codegen.range.BoundedValue
import org.jetbrains.kotlin.codegen.range.comparison.ComparisonGenerator
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
@@ -28,7 +28,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
class InFloatingPointRangeLiteralExpressionGenerator(
operatorReference: KtSimpleNameExpression,
private val rangeLiteral: SimpleBoundedValue,
private val rangeLiteral: BoundedValue,
private val comparisonGenerator: ComparisonGenerator,
private val frameMap: FrameMap
) : InExpressionGenerator {