Refactor compile constants to reduce boolean parameter hell

This commit is contained in:
Pavel V. Talanov
2015-07-03 16:04:41 +03:00
parent f97767e159
commit b0a4520710
25 changed files with 195 additions and 180 deletions
@@ -100,7 +100,7 @@ class LazyJavaAnnotationDescriptor(
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? {
return when (argument) {
is JavaLiteralAnnotationArgument -> createCompileTimeConstant(argument.value, true, false, false, null)
is JavaLiteralAnnotationArgument -> createCompileTimeConstant(argument.value, CompileTimeConstant.Parameters.Impl(true, false, false))
is JavaEnumValueAnnotationArgument -> resolveFromEnumValue(argument.resolve())
is JavaArrayAnnotationArgument -> resolveFromArray(argument.name ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
@@ -125,7 +125,7 @@ class LazyJavaAnnotationDescriptor(
val values = elements.map {
argument -> resolveAnnotationArgument(argument) ?: NullValue
}
return ArrayValue(values, valueParameter.getType(), values.any { it.usesVariableAsConstant() })
return ArrayValue(values, valueParameter.getType(), CompileTimeConstant.Parameters.Impl(true, false, values.any { it.usesVariableAsConstant() }))
}
private fun resolveFromEnumValue(element: JavaField?): CompileTimeConstant<*>? {
@@ -140,7 +140,7 @@ class LazyJavaAnnotationDescriptor(
val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(element.getName())
if (classifier !is ClassDescriptor) return null
return EnumValue(classifier, false)
return EnumValue(classifier)
}
private fun resolveFromJavaClassObjectType(javaType: JavaType): CompileTimeConstant<*>? {
@@ -66,8 +66,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl(
}
val compileTimeConstant = createCompileTimeConstant(
normalizedValue, canBeUsedInAnnotation = true, isPureIntConstant = true,
usesVariableAsConstant = true, expectedType = null
normalizedValue, CompileTimeConstant.Parameters.ThrowException
)
return compileTimeConstant
}
@@ -107,7 +106,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl(
val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass)
if (parameter != null) {
elements.trimToSize()
arguments[parameter] = ArrayValue(elements, parameter.getType(), false)
arguments[parameter] = ArrayValue(elements, parameter.getType(), CompileTimeConstant.Parameters.ThrowException)
}
}
}
@@ -130,7 +129,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl(
if (enumClass.getKind() == ClassKind.ENUM_CLASS) {
val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(name)
if (classifier is ClassDescriptor) {
return EnumValue(classifier, false)
return EnumValue(classifier)
}
}
return ErrorValue.create("Unresolved enum entry: $enumClassId.$name")
@@ -141,9 +140,10 @@ public class BinaryClassAnnotationAndConstantLoaderImpl(
}
private fun createConstant(name: Name?, value: Any?): CompileTimeConstant<*> {
return createCompileTimeConstant(value, canBeUsedInAnnotation = true, isPureIntConstant = false,
usesVariableAsConstant = false, expectedType = null)
?: ErrorValue.create("Unsupported annotation argument: $name")
return createCompileTimeConstant(
value,
CompileTimeConstant.Parameters.ThrowException
) ?: ErrorValue.create("Unsupported annotation argument: $name")
}
private fun setArgumentValueByName(name: Name, argumentValue: CompileTimeConstant<*>) {
@@ -21,7 +21,8 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.types.JetType
public class AnnotationValue(value: AnnotationDescriptor) : CompileTimeConstant<AnnotationDescriptor>(value, true, false, false) {
public class AnnotationValue(value: AnnotationDescriptor) :
CompileTimeConstant<AnnotationDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = value.getType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitAnnotationValue(this, data)
@@ -24,8 +24,17 @@ import java.util.*
public class ArrayValue(
value: List<CompileTimeConstant<*>>,
private val type: JetType,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<List<CompileTimeConstant<*>>>(value, true, false, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<List<CompileTimeConstant<*>>>(value, parameters) {
public constructor(
value: List<CompileTimeConstant<*>>,
type: JetType,
usesVariableAsConstant: Boolean
) : this(value, type, CompileTimeConstant.Parameters.Impl(true, false, usesVariableAsConstant))
override fun canBeUsedInAnnotations() = true
override fun isPure() = false
init {
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value }
@@ -22,9 +22,10 @@ import org.jetbrains.kotlin.types.JetType
public class BooleanValue(
value: Boolean,
canBeUseInAnnotation: Boolean,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<Boolean>(value, canBeUseInAnnotation, false, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Boolean>(value, parameters) {
override fun isPure(): Boolean = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getBooleanType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitBooleanValue(this, data)
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.types.JetType
public class ByteValue(
value: Byte,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVaraiableAsConstant: Boolean
) : IntegerValueConstant<Byte>(value, canBeUsedInAnnotations, pure, usesVaraiableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Byte>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getByteType()
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.types.JetType
public class CharValue(
value: Char,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVariableAsConstant: Boolean
) : IntegerValueConstant<Char>(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Char>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getCharType()
@@ -20,24 +20,15 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.JetType
public abstract class CompileTimeConstant<T> protected constructor(public open val value: T, canBeUsedInAnnotations: Boolean, isPure: Boolean, usesVariableAsConstant: Boolean) {
private val flags: Int
public abstract class CompileTimeConstant<T> protected constructor(
public open val value: T,
public val parameters: CompileTimeConstant.Parameters
) {
public open fun canBeUsedInAnnotations(): Boolean = parameters.canBeUsedInAnnotation
init {
flags = (if (isPure) IS_PURE_MASK else 0) or (if (canBeUsedInAnnotations) CAN_BE_USED_IN_ANNOTATIONS_MASK else 0) or (if (usesVariableAsConstant) USES_VARIABLE_AS_CONSTANT_MASK else 0)
}
public open fun isPure(): Boolean = parameters.isPure
public fun canBeUsedInAnnotations(): Boolean {
return (flags and CAN_BE_USED_IN_ANNOTATIONS_MASK) != 0
}
public fun isPure(): Boolean {
return (flags and IS_PURE_MASK) != 0
}
public fun usesVariableAsConstant(): Boolean {
return (flags and USES_VARIABLE_AS_CONSTANT_MASK) != 0
}
public open fun usesVariableAsConstant(): Boolean = parameters.usesVariableAsConstant
public abstract fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType
@@ -45,16 +36,24 @@ public abstract class CompileTimeConstant<T> protected constructor(public open v
override fun toString() = value.toString()
companion object {
public interface Parameters {
public open val canBeUsedInAnnotation: Boolean
public open val isPure: Boolean
public open val usesVariableAsConstant: Boolean
/*
* if is pure is false then constant type cannot be changed
* ex1. val a: Long = 1.toInt() (TYPE_MISMATCH error, 1.toInt() isn't pure)
* ex2. val b: Int = a (TYPE_MISMATCH error, a isn't pure)
*
*/
private val IS_PURE_MASK = 1
private val CAN_BE_USED_IN_ANNOTATIONS_MASK = 1 shl 1
private val USES_VARIABLE_AS_CONSTANT_MASK = 1 shl 2
public class Impl(
override val canBeUsedInAnnotation: Boolean,
override val isPure: Boolean,
override val usesVariableAsConstant: Boolean
) : Parameters
public object ThrowException : Parameters {
override val canBeUsedInAnnotation: Boolean
get() = error("Should not be called")
override val isPure: Boolean
get() = error("Should not be called")
override val usesVariableAsConstant: Boolean
get() = error("Should not be called")
}
}
}
}
@@ -22,27 +22,25 @@ import org.jetbrains.kotlin.types.TypeUtils
public fun createCompileTimeConstant(
value: Any?,
canBeUsedInAnnotation: Boolean,
isPureIntConstant: Boolean,
usesVariableAsConstant: Boolean = false,
parameters: CompileTimeConstant.Parameters,
expectedType: JetType? = null
): CompileTimeConstant<*>? {
// TODO: primitive arrays
if (expectedType == null) {
when(value) {
is Byte -> return ByteValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
is Short -> return ShortValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
is Int -> return IntValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
is Long -> return LongValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
is Byte -> return ByteValue(value, parameters)
is Short -> return ShortValue(value, parameters)
is Int -> return IntValue(value, parameters)
is Long -> return LongValue(value, parameters)
}
}
return when(value) {
is Byte, is Short, is Int, is Long -> getIntegerValue((value as Number).toLong(), canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant, expectedType)
is Char -> CharValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
is Float -> FloatValue(value, canBeUsedInAnnotation, usesVariableAsConstant)
is Double -> DoubleValue(value, canBeUsedInAnnotation, usesVariableAsConstant)
is Boolean -> BooleanValue(value, canBeUsedInAnnotation, usesVariableAsConstant)
is String -> StringValue(value, canBeUsedInAnnotation, usesVariableAsConstant)
is Byte, is Short, is Int, is Long -> getIntegerValue((value as Number).toLong(), parameters, expectedType)
is Char -> CharValue(value, parameters)
is Float -> FloatValue(value, parameters)
is Double -> DoubleValue(value, parameters)
is Boolean -> BooleanValue(value, parameters)
is String -> StringValue(value, parameters)
null -> NullValue
else -> null
}
@@ -50,32 +48,37 @@ public fun createCompileTimeConstant(
private fun getIntegerValue(
value: Long,
canBeUsedInAnnotation: Boolean,
isPureIntConstant: Boolean,
usesVariableAsConstant: Boolean,
parameters: CompileTimeConstant.Parameters,
expectedType: JetType
): CompileTimeConstant<*>? {
fun defaultIntegerValue(value: Long) = when (value) {
value.toInt().toLong() -> IntValue(value.toInt(), canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
else -> LongValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
value.toInt().toLong() -> IntValue(value.toInt(), parameters)
else -> LongValue(value, parameters)
}
if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) {
return IntegerValueTypeConstant(value, canBeUsedInAnnotation, usesVariableAsConstant)
return IntegerValueTypeConstant(value, parameters)
}
val notNullExpected = TypeUtils.makeNotNullable(expectedType)
return when {
KotlinBuiltIns.isLong(notNullExpected) -> LongValue(value, canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
KotlinBuiltIns.isShort(notNullExpected) -> when (value) {
value.toShort().toLong() -> ShortValue(value.toShort(), canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
else -> defaultIntegerValue(value)
}
KotlinBuiltIns.isByte(notNullExpected) -> when (value) {
value.toByte().toLong() -> ByteValue(value.toByte(), canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
else -> defaultIntegerValue(value)
}
KotlinBuiltIns.isChar(notNullExpected) -> IntValue(value.toInt(), canBeUsedInAnnotation, isPureIntConstant, usesVariableAsConstant)
KotlinBuiltIns.isLong(notNullExpected) -> LongValue(value, parameters)
KotlinBuiltIns.isShort(notNullExpected) ->
if (value == value.toShort().toLong())
ShortValue(value.toShort(), parameters)
else
defaultIntegerValue(value)
KotlinBuiltIns.isByte(notNullExpected) ->
if (value == value.toByte().toLong())
ByteValue(value.toByte(), parameters)
else
defaultIntegerValue(value)
KotlinBuiltIns.isChar(notNullExpected) ->
IntValue(value.toInt(), parameters)
else -> defaultIntegerValue(value)
}
}
@@ -22,9 +22,10 @@ import org.jetbrains.kotlin.types.JetType
public class DoubleValue(
value: Double,
canBeUsedInAnnotations: Boolean,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<Double>(value, canBeUsedInAnnotations, false, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Double>(value, parameters) {
override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getDoubleType()
@@ -24,9 +24,8 @@ import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.utils.sure
public class EnumValue(
value: ClassDescriptor,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<ClassDescriptor>(value, true, false, usesVariableAsConstant) {
value: ClassDescriptor
) : CompileTimeConstant<ClassDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = getType()
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.JetType
public abstract class ErrorValue : CompileTimeConstant<Unit>(Unit, true, false, false) {
public abstract class ErrorValue : CompileTimeConstant<Unit>(Unit, CompileTimeConstant.Parameters.Impl(true, false, false)) {
deprecated("Should not be called, for this is not a real value, but a indication of an error")
override val value: Unit
@@ -22,9 +22,9 @@ import org.jetbrains.kotlin.types.JetType
public class FloatValue(
value: Float,
canBeUsedInAnnotations: Boolean,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<Float>(value, canBeUsedInAnnotations, false, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Float>(value, parameters) {
override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getFloatType()
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.types.JetType
public class IntValue(
value: Int,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVariableAsConstant: Boolean
) : IntegerValueConstant<Int>(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Int>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getIntType()
@@ -18,7 +18,5 @@ package org.jetbrains.kotlin.resolve.constants
public abstract class IntegerValueConstant<T> protected constructor(
value: T,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<T>(value, canBeUsedInAnnotations, pure, usesVariableAsConstant)
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<T>(value, parameters)
@@ -25,9 +25,10 @@ import java.util.Collections
public class IntegerValueTypeConstant(
value: Number,
canBeUsedInAnnotations: Boolean,
usesVariableAsConstant: Boolean
) : IntegerValueConstant<Number>(value, canBeUsedInAnnotations, true, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Number>(value, parameters) {
override fun isPure() = true
private val typeConstructor = IntegerValueTypeConstructor(value.toLong())
@@ -20,8 +20,8 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.JetType
public class KClassValue(private val type: JetType) : CompileTimeConstant<JetType>(type, true, false, false) {
public class KClassValue(private val type: JetType) :
CompileTimeConstant<JetType>(type, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = type
override val value: JetType
get() = type.getArguments().single().getType()
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.types.JetType
public class LongValue(
value: Long,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVariableAsConstant: Boolean
) : IntegerValueConstant<Long>(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Long>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getLongType()
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.JetType
public object NullValue : CompileTimeConstant<Void?>(null, false, false, false) {
public object NullValue : CompileTimeConstant<Void?>(null, CompileTimeConstant.Parameters.Impl(false, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getNullableNothingType()
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.types.JetType
public class ShortValue(
value: Short,
canBeUsedInAnnotations: Boolean,
pure: Boolean,
usesVariableAsConstant: Boolean
) : IntegerValueConstant<Short>(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : IntegerValueConstant<Short>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getShortType()
@@ -22,9 +22,9 @@ import org.jetbrains.kotlin.types.JetType
public class StringValue(
value: String,
canBeUsedInAnnotations: Boolean,
usesVariableAsConstant: Boolean
) : CompileTimeConstant<String>(value, canBeUsedInAnnotations, false, usesVariableAsConstant) {
parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<String>(value, parameters) {
override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getStringType()
@@ -68,17 +68,18 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) {
value: Value,
nameResolver: NameResolver
): CompileTimeConstant<*> {
val parameters = CompileTimeConstant.Parameters.ThrowException
val result = when (value.getType()) {
Type.BYTE -> ByteValue(value.getIntValue().toByte(), true, true, true)
Type.CHAR -> CharValue(value.getIntValue().toChar(), true, true, true)
Type.SHORT -> ShortValue(value.getIntValue().toShort(), true, true, true)
Type.INT -> IntValue(value.getIntValue().toInt(), true, true, true)
Type.LONG -> LongValue(value.getIntValue(), true, true, true)
Type.FLOAT -> FloatValue(value.getFloatValue(), true, true)
Type.DOUBLE -> DoubleValue(value.getDoubleValue(), true, true)
Type.BOOLEAN -> BooleanValue(value.getIntValue() != 0L, true, true)
Type.BYTE -> ByteValue(value.getIntValue().toByte(), parameters)
Type.CHAR -> CharValue(value.getIntValue().toChar(), parameters)
Type.SHORT -> ShortValue(value.getIntValue().toShort(), parameters)
Type.INT -> IntValue(value.getIntValue().toInt(), parameters)
Type.LONG -> LongValue(value.getIntValue(), parameters)
Type.FLOAT -> FloatValue(value.getFloatValue(), parameters)
Type.DOUBLE -> DoubleValue(value.getDoubleValue(), parameters)
Type.BOOLEAN -> BooleanValue(value.getIntValue() != 0L, parameters)
Type.STRING -> {
StringValue(nameResolver.getString(value.getStringValue()), true, true)
StringValue(nameResolver.getString(value.getStringValue()), parameters)
}
Type.CLASS -> {
// TODO: support class literals
@@ -114,7 +115,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) {
resolveValue(expectedElementType, it, nameResolver)
},
actualArrayType,
true
parameters
)
}
else -> error("Unsupported annotation argument type: ${value.getType()} (expected $expectedType)")
@@ -135,7 +136,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) {
if (enumClass.getKind() == ClassKind.ENUM_CLASS) {
val enumEntry = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(enumEntryName)
if (enumEntry is ClassDescriptor) {
return EnumValue(enumEntry, true)
return EnumValue(enumEntry)
}
}
return ErrorValue.create("Unresolved enum entry: $enumClassId.$enumEntryName")