Refactor: Pass buitlins into IntegerValueTypeConstant and IntegerValueTypeConstructor

This commit is contained in:
Pavel V. Talanov
2015-07-23 18:38:26 +03:00
parent a1a38e2eba
commit 273e69c02c
4 changed files with 14 additions and 9 deletions
@@ -680,7 +680,7 @@ private class ConstantExpressionEvaluatorVisitor(
expectedType: JetType expectedType: JetType
): CompileTimeConstant<*>? { ): CompileTimeConstant<*>? {
if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) { if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) {
return IntegerValueTypeConstant(value, parameters) return IntegerValueTypeConstant(value, constantExpressionEvaluator.builtIns, parameters)
} }
val integerValue = factory.createIntegerConstantValue(value, expectedType) val integerValue = factory.createIntegerConstantValue(value, expectedType)
if (integerValue != null) { if (integerValue != null) {
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.scopes.JetScope import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import java.util.regex.Pattern import java.util.regex.Pattern
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor
import org.jetbrains.kotlin.types.JetTypeImpl import org.jetbrains.kotlin.types.JetTypeImpl
@@ -59,7 +60,7 @@ public class ConstraintSystemTestData(
val matcher = INTEGER_VALUE_TYPE_PATTERN.matcher(name) val matcher = INTEGER_VALUE_TYPE_PATTERN.matcher(name)
if (matcher.find()) { if (matcher.find()) {
val number = matcher.group(1)!! val number = matcher.group(1)!!
return JetTypeImpl(Annotations.EMPTY, IntegerValueTypeConstructor(number.toLong()), false, listOf(), JetScope.Empty) return JetTypeImpl(Annotations.EMPTY, IntegerValueTypeConstructor(number.toLong(), KotlinBuiltIns.getInstance()), false, listOf(), JetScope.Empty)
} }
return typeResolver.resolveType( return typeResolver.resolveType(
scopeToResolveTypeParameters, JetPsiFactory(project).createType(name), scopeToResolveTypeParameters, JetPsiFactory(project).createType(name),
@@ -57,12 +57,13 @@ public class TypedCompileTimeConstant<out T>(
public class IntegerValueTypeConstant( public class IntegerValueTypeConstant(
private val value: Number, private val value: Number,
private val builtIns: KotlinBuiltIns,
override val parameters: CompileTimeConstant.Parameters override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Number> { ) : CompileTimeConstant<Number> {
private val typeConstructor = IntegerValueTypeConstructor(value.toLong()) private val typeConstructor = IntegerValueTypeConstructor(value.toLong(), builtIns)
override fun toConstantValue(expectedType: JetType): ConstantValue<Number> { override fun toConstantValue(expectedType: JetType): ConstantValue<Number> {
val factory = ConstantValueFactory(KotlinBuiltIns.getInstance()) val factory = ConstantValueFactory(builtIns)
val type = getType(expectedType) val type = getType(expectedType)
return when { return when {
KotlinBuiltIns.isInt(type) -> { KotlinBuiltIns.isInt(type) -> {
@@ -26,17 +26,20 @@ import org.jetbrains.kotlin.types.TypeConstructor
import java.util.ArrayList import java.util.ArrayList
import java.util.Collections import java.util.Collections
public class IntegerValueTypeConstructor(private val value: Long) : TypeConstructor { public class IntegerValueTypeConstructor(
private val value: Long,
private val builtIns: KotlinBuiltIns
) : TypeConstructor {
private val supertypes = ArrayList<JetType>(4) private val supertypes = ArrayList<JetType>(4)
init { init {
// order of types matters // order of types matters
// 'getPrimitiveNumberType' returns first of supertypes that is a subtype of expected type // 'getPrimitiveNumberType' returns first of supertypes that is a subtype of expected type
// for expected type 'Any' result type 'Int' should be returned // for expected type 'Any' result type 'Int' should be returned
checkBoundsAndAddSuperType(value, Integer.MIN_VALUE.toLong(), Integer.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getIntType()) checkBoundsAndAddSuperType(value, Integer.MIN_VALUE.toLong(), Integer.MAX_VALUE.toLong(), builtIns.getIntType())
checkBoundsAndAddSuperType(value, java.lang.Byte.MIN_VALUE.toLong(), java.lang.Byte.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getByteType()) checkBoundsAndAddSuperType(value, java.lang.Byte.MIN_VALUE.toLong(), java.lang.Byte.MAX_VALUE.toLong(), builtIns.getByteType())
checkBoundsAndAddSuperType(value, java.lang.Short.MIN_VALUE.toLong(), java.lang.Short.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getShortType()) checkBoundsAndAddSuperType(value, java.lang.Short.MIN_VALUE.toLong(), java.lang.Short.MAX_VALUE.toLong(), builtIns.getShortType())
supertypes.add(KotlinBuiltIns.getInstance().getLongType()) supertypes.add(builtIns.getLongType())
} }
private fun checkBoundsAndAddSuperType(value: Long, minValue: Long, maxValue: Long, kotlinType: JetType) { private fun checkBoundsAndAddSuperType(value: Long, minValue: Long, maxValue: Long, kotlinType: JetType) {