Refactor ConstantValue implementations

Remove KotlinBuiltIns and take a ModuleDescriptor instance in getType
instead. This will allow to create constant values in contexts where
there's no module or built-ins accessible (such as, deserialization of
module annotations during indexing of .kotlin_module files in IDE).

Note that some values (KClassValue, EnumValue, AnnotationValue) still
take module-dependent objects (KotlinType, ClassDescriptor and
AnnotationDescriptor respectively). This is to be refactored later
This commit is contained in:
Alexander Udalov
2017-12-29 13:04:36 +01:00
parent 5e97517c8b
commit 907f53e539
22 changed files with 207 additions and 247 deletions
@@ -114,8 +114,7 @@ class JavaDeprecatedAnnotationDescriptor(
c: LazyJavaResolverContext
): JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.deprecated) {
override val allValueArguments: Map<Name, ConstantValue<*>> by c.storageManager.createLazyValue {
mapOf(JavaAnnotationMapper.DEPRECATED_ANNOTATION_MESSAGE to
ConstantValueFactory(c.module.builtIns).createStringValue("Deprecated in Java"))
mapOf(JavaAnnotationMapper.DEPRECATED_ANNOTATION_MESSAGE to ConstantValueFactory.createStringValue("Deprecated in Java"))
}
}
@@ -170,7 +169,7 @@ object JavaAnnotationTargetMapper {
JavaAnnotationMapper.TARGET_ANNOTATION_ALLOWED_TARGETS,
builtIns.getBuiltInClassByFqName(KotlinBuiltIns.FQ_NAMES.target)
)
return ArrayValue(kotlinTargets, parameterDescriptor?.type ?: ErrorUtils.createErrorType("Error: AnnotationTarget[]"), builtIns)
return ArrayValue(kotlinTargets) { parameterDescriptor?.type ?: ErrorUtils.createErrorType("Error: AnnotationTarget[]") }
}
private val retentionNameList = mapOf(
@@ -57,8 +57,6 @@ class LazyJavaAnnotationDescriptor(
override val source = c.components.sourceElementFactory.source(javaAnnotation)
private val factory = ConstantValueFactory(c.module.builtIns)
override val allValueArguments by c.storageManager.createLazyValue {
javaAnnotation.arguments.mapNotNull { arg ->
val name = arg.name ?: DEFAULT_ANNOTATION_MEMBER_NAME
@@ -68,7 +66,7 @@ class LazyJavaAnnotationDescriptor(
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): ConstantValue<*>? {
return when (argument) {
is JavaLiteralAnnotationArgument -> factory.createConstantValue(argument.value)
is JavaLiteralAnnotationArgument -> ConstantValueFactory.createConstantValue(argument.value)
is JavaEnumValueAnnotationArgument -> resolveFromEnumValue(argument.resolve(), argument.entryName)
is JavaArrayAnnotationArgument -> resolveFromArray(argument.name ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
@@ -78,7 +76,7 @@ class LazyJavaAnnotationDescriptor(
}
private fun resolveFromAnnotation(javaAnnotation: JavaAnnotation): ConstantValue<*> {
return factory.createAnnotationValue(LazyJavaAnnotationDescriptor(c, javaAnnotation))
return ConstantValueFactory.createAnnotationValue(LazyJavaAnnotationDescriptor(c, javaAnnotation))
}
private fun resolveFromArray(argumentName: Name, elements: List<JavaAnnotationArgument>): ConstantValue<*>? {
@@ -93,16 +91,16 @@ class LazyJavaAnnotationDescriptor(
)
val values = elements.map {
argument -> resolveAnnotationArgument(argument) ?: factory.createNullValue()
argument -> resolveAnnotationArgument(argument) ?: ConstantValueFactory.createNullValue()
}
return factory.createArrayValue(values, arrayType)
return ConstantValueFactory.createArrayValue(values, arrayType)
}
private fun resolveFromEnumValue(element: JavaField?, entryName: Name?): ConstantValue<*>? {
if (element == null || !element.isEnumEntry) {
if (entryName == null) return null
return factory.createEnumValue(ErrorUtils.createErrorClassWithExactName(entryName))
return ConstantValueFactory.createEnumValue(ErrorUtils.createErrorClassWithExactName(entryName))
}
val containingJavaClass = element.containingClass
@@ -112,7 +110,7 @@ class LazyJavaAnnotationDescriptor(
val classifier = enumClass.unsubstitutedInnerClassesScope.getContributedClassifier(element.name, NoLookupLocation.FROM_JAVA_LOADER)
as? ClassDescriptor ?: return null
return factory.createEnumValue(classifier)
return ConstantValueFactory.createEnumValue(classifier)
}
private fun resolveFromJavaClassObjectType(javaType: JavaType): ConstantValue<*>? {
@@ -128,7 +126,7 @@ class LazyJavaAnnotationDescriptor(
val javaClassObjectType = KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, jlClass, arguments)
return factory.createKClassValue(javaClassObjectType)
return ConstantValueFactory.createKClassValue(javaClassObjectType)
}
override fun toString(): String {
@@ -45,7 +45,6 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
storageManager, kotlinClassFinder
) {
private val annotationDeserializer = AnnotationDeserializer(module, notFoundClasses)
private val factory = ConstantValueFactory(module.builtIns)
override fun loadTypeAnnotation(proto: ProtoBuf.Annotation, nameResolver: NameResolver): AnnotationDescriptor =
annotationDeserializer.deserializeAnnotation(proto, nameResolver)
@@ -65,7 +64,7 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
initializer
}
return factory.createConstantValue(normalizedValue)
return ConstantValueFactory.createConstantValue(normalizedValue)
}
override fun loadPropertyAnnotations(
@@ -116,7 +115,7 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
override fun visitEnd() {
val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass)
if (parameter != null) {
arguments[name] = factory.createArrayValue(elements.compact(), parameter.type)
arguments[name] = ConstantValueFactory.createArrayValue(elements.compact(), parameter.type)
}
}
}
@@ -139,10 +138,10 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
if (enumClass.kind == ClassKind.ENUM_CLASS) {
val classifier = enumClass.unsubstitutedInnerClassesScope.getContributedClassifier(name, NoLookupLocation.FROM_JAVA_LOADER)
if (classifier is ClassDescriptor) {
return factory.createEnumValue(classifier)
return ConstantValueFactory.createEnumValue(classifier)
}
}
return factory.createErrorValue("Unresolved enum entry: $enumClassId.$name")
return ConstantValueFactory.createErrorValue("Unresolved enum entry: $enumClassId.$name")
}
override fun visitEnd() {
@@ -150,8 +149,8 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
}
private fun createConstant(name: Name?, value: Any?): ConstantValue<*> {
return factory.createConstantValue(value) ?:
factory.createErrorValue("Unsupported annotation argument: $name")
return ConstantValueFactory.createConstantValue(value) ?:
ConstantValueFactory.createErrorValue("Unsupported annotation argument: $name")
}
}
}
@@ -169,7 +169,7 @@ fun getFunctionTypeArgumentProjections(
val parameterNameAnnotation = BuiltInAnnotationDescriptor(
builtIns,
KotlinBuiltIns.FQ_NAMES.parameterName,
mapOf(Name.identifier("name") to ConstantValueFactory(builtIns).createStringValue(name.asString()))
mapOf(Name.identifier("name") to ConstantValueFactory.createStringValue(name.asString()))
)
type.replaceAnnotations(AnnotationsImpl(type.annotations + parameterNameAnnotation))
}
@@ -39,8 +39,10 @@ fun KotlinBuiltIns.createDeprecatedAnnotation(
this,
KotlinBuiltIns.FQ_NAMES.replaceWith,
mapOf(
REPLACE_WITH_EXPRESSION_NAME to StringValue(replaceWith, this),
REPLACE_WITH_IMPORTS_NAME to ArrayValue(emptyList(), getArrayType(Variance.INVARIANT, stringType), this)
REPLACE_WITH_EXPRESSION_NAME to StringValue(replaceWith),
REPLACE_WITH_IMPORTS_NAME to ArrayValue(emptyList()) { module ->
module.builtIns.getArrayType(Variance.INVARIANT, stringType)
}
)
)
@@ -48,7 +50,7 @@ fun KotlinBuiltIns.createDeprecatedAnnotation(
this,
KotlinBuiltIns.FQ_NAMES.deprecated,
mapOf(
DEPRECATED_MESSAGE_NAME to StringValue(message, this),
DEPRECATED_MESSAGE_NAME to StringValue(message),
DEPRECATED_REPLACE_WITH_NAME to AnnotationValue(replaceWithAnnotation),
DEPRECATED_LEVEL_NAME to EnumValue(getDeprecationLevelEnumEntry(level) ?: error("Deprecation level $level not found"))
)
@@ -17,8 +17,12 @@
package org.jetbrains.kotlin.resolve.constants
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeUtils
interface CompileTimeConstant<out T> {
val isError: Boolean
@@ -48,12 +52,13 @@ interface CompileTimeConstant<out T> {
class TypedCompileTimeConstant<out T>(
val constantValue: ConstantValue<T>,
module: ModuleDescriptor,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<T> {
override val isError: Boolean
get() = constantValue is ErrorValue
val type: KotlinType = constantValue.type
val type: KotlinType = constantValue.getType(module)
override fun toConstantValue(expectedType: KotlinType): ConstantValue<T> = constantValue
@@ -75,32 +80,24 @@ class TypedCompileTimeConstant<out T>(
class IntegerValueTypeConstant(
private val value: Number,
private val builtIns: KotlinBuiltIns,
builtIns: KotlinBuiltIns,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Number> {
private val typeConstructor = IntegerValueTypeConstructor(value.toLong(), builtIns)
override fun toConstantValue(expectedType: KotlinType): ConstantValue<Number> {
val factory = ConstantValueFactory(builtIns)
val type = getType(expectedType)
return when {
KotlinBuiltIns.isInt(type) -> {
factory.createIntValue(value.toInt())
}
KotlinBuiltIns.isByte(type) -> {
factory.createByteValue(value.toByte())
}
KotlinBuiltIns.isShort(type) -> {
factory.createShortValue(value.toShort())
}
else -> {
factory.createLongValue(value.toLong())
}
KotlinBuiltIns.isInt(type) -> ConstantValueFactory.createIntValue(value.toInt())
KotlinBuiltIns.isByte(type) -> ConstantValueFactory.createByteValue(value.toByte())
KotlinBuiltIns.isShort(type) -> ConstantValueFactory.createShortValue(value.toShort())
else -> ConstantValueFactory.createLongValue(value.toLong())
}
}
val unknownIntegerType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(Annotations.EMPTY, typeConstructor, emptyList<TypeProjection>(),
false, ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)
val unknownIntegerType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
Annotations.EMPTY, typeConstructor, emptyList(), false,
ErrorUtils.createErrorScope("Scope for number value type ($typeConstructor)", true)
)
fun getType(expectedType: KotlinType): KotlinType = TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType)
@@ -19,41 +19,37 @@ package org.jetbrains.kotlin.resolve.constants
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
class ConstantValueFactory(
private val builtins: KotlinBuiltIns
) {
fun createLongValue(value: Long) = LongValue(value, builtins)
object ConstantValueFactory {
fun createLongValue(value: Long) = LongValue(value)
fun createIntValue(value: Int) = IntValue(value, builtins)
fun createIntValue(value: Int) = IntValue(value)
fun createErrorValue(message: String) = ErrorValue.create(message)
fun createShortValue(value: Short) = ShortValue(value, builtins)
fun createShortValue(value: Short) = ShortValue(value)
fun createByteValue(value: Byte) = ByteValue(value, builtins)
fun createByteValue(value: Byte) = ByteValue(value)
fun createDoubleValue(value: Double) = DoubleValue(value, builtins)
fun createDoubleValue(value: Double) = DoubleValue(value)
fun createFloatValue(value: Float) = FloatValue(value, builtins)
fun createFloatValue(value: Float) = FloatValue(value)
fun createBooleanValue(value: Boolean) = BooleanValue(value, builtins)
fun createBooleanValue(value: Boolean) = BooleanValue(value)
fun createCharValue(value: Char) = CharValue(value, builtins)
fun createCharValue(value: Char) = CharValue(value)
fun createStringValue(value: String) = StringValue(value, builtins)
fun createStringValue(value: String) = StringValue(value)
fun createNullValue() = NullValue(builtins)
fun createNullValue() = NullValue()
fun createEnumValue(enumEntryClass: ClassDescriptor): EnumValue = EnumValue(enumEntryClass)
fun createArrayValue(
value: List<ConstantValue<*>>,
type: KotlinType
) = ArrayValue(value, type, builtins)
fun createArrayValue(value: List<ConstantValue<*>>, type: KotlinType) = createArrayValue(value) { type }
fun createAnnotationValue(value: AnnotationDescriptor) = AnnotationValue(value)
@@ -83,11 +79,14 @@ class ConstantValueFactory(
}
}
private fun createArrayValue(value: List<ConstantValue<*>>, computeType: (ModuleDescriptor) -> KotlinType): ArrayValue =
ArrayValue(value, computeType)
private fun List<*>.arrayToList(): List<ConstantValue<*>> =
this.toList().mapNotNull { createConstantValue(it) }
private fun PrimitiveType.arrayType(): KotlinType =
builtins.getPrimitiveArrayKotlinType(this)
private fun PrimitiveType.arrayType(): (ModuleDescriptor) -> KotlinType =
{ module -> module.builtIns.getPrimitiveArrayKotlinType(this) }
fun createIntegerConstantValue(
value: Long,
@@ -104,4 +103,3 @@ class ConstantValueFactory(
}
}
}
@@ -18,14 +18,19 @@ package org.jetbrains.kotlin.resolve.constants
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.descriptorUtil.classValueType
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
abstract class ConstantValue<out T>(open val value: T) {
abstract val type: KotlinType
abstract fun getType(module: ModuleDescriptor): KotlinType
abstract fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R
@@ -36,8 +41,7 @@ abstract class IntegerValueConstant<out T> protected constructor(value: T) : Con
class AnnotationValue(value: AnnotationDescriptor) : ConstantValue<AnnotationDescriptor>(value) {
override val type: KotlinType
get() = value.type
override fun getType(module: ModuleDescriptor): KotlinType = value.type
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitAnnotationValue(this, data)
override fun toString() = value.toString()
@@ -45,12 +49,10 @@ class AnnotationValue(value: AnnotationDescriptor) : ConstantValue<AnnotationDes
class ArrayValue(
value: List<ConstantValue<*>>,
override val type: KotlinType,
private val builtIns: KotlinBuiltIns
private val computeType: (ModuleDescriptor) -> KotlinType
) : ConstantValue<List<ConstantValue<*>>>(value) {
init {
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value }
override fun getType(module: ModuleDescriptor): KotlinType = computeType(module).also { type ->
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was $type: $value" }
}
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitArrayValue(this, data)
@@ -60,33 +62,20 @@ class ArrayValue(
override fun hashCode() = value.hashCode()
}
class BooleanValue(
value: Boolean,
builtIns: KotlinBuiltIns
) : ConstantValue<Boolean>(value) {
override val type = builtIns.booleanType
class BooleanValue(value: Boolean) : ConstantValue<Boolean>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.booleanType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitBooleanValue(this, data)
}
class ByteValue(
value: Byte,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Byte>(value) {
override val type = builtIns.byteType
class ByteValue(value: Byte) : IntegerValueConstant<Byte>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.byteType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitByteValue(this, data)
override fun toString(): String = "$value.toByte()"
}
class CharValue(
value: Char,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Char>(value) {
override val type = builtIns.charType
class CharValue(value: Char) : IntegerValueConstant<Char>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.charType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitCharValue(this, data)
@@ -96,7 +85,7 @@ class CharValue(
'\b' -> "\\b"
'\t' -> "\\t"
'\n' -> "\\n"
//TODO: KT-8507
//TODO: KT-8507
12.toChar() -> "\\f"
'\r' -> "\\r"
else -> if (isPrintableUnicode(c)) Character.toString(c) else "?"
@@ -114,27 +103,26 @@ class CharValue(
}
}
class DoubleValue(
value: Double,
builtIns: KotlinBuiltIns
) : ConstantValue<Double>(value) {
override val type = builtIns.doubleType
class DoubleValue(value: Double) : ConstantValue<Double>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.doubleType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitDoubleValue(this, data)
override fun toString() = "$value.toDouble()"
}
class EnumValue(
value: ClassDescriptor
) : ConstantValue<ClassDescriptor>(value) {
class EnumValue(value: ClassDescriptor) : ConstantValue<ClassDescriptor>(value) {
val enumClassId: ClassId get() = (value.containingDeclaration as ClassDescriptor).classId!!
override val type: KotlinType
get() = value.classValueType ?: ErrorUtils.createErrorType("Containing class for error-class based enum entry $value")
val enumEntryName: Name get() = value.name
override fun getType(module: ModuleDescriptor): KotlinType =
value.classValueType
?: ErrorUtils.createErrorType("Containing class for error-class based enum entry ${value.fqNameUnsafe}")
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitEnumValue(this, data)
override fun toString() = "$type.${value.name}"
override fun toString() = "${enumClassId.shortClassName}.$enumEntryName"
override fun equals(other: Any?): Boolean = this === other || value == (other as? EnumValue)?.value
@@ -151,7 +139,7 @@ abstract class ErrorValue : ConstantValue<Unit>(Unit) {
class ErrorValueWithMessage(val message: String) : ErrorValue() {
override val type = ErrorUtils.createErrorType(message)
override fun getType(module: ModuleDescriptor) = ErrorUtils.createErrorType(message)
override fun toString() = message
}
@@ -163,23 +151,16 @@ abstract class ErrorValue : ConstantValue<Unit>(Unit) {
}
}
class FloatValue(
value: Float,
builtIns: KotlinBuiltIns
) : ConstantValue<Float>(value) {
override val type = builtIns.floatType
class FloatValue(value: Float) : ConstantValue<Float>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.floatType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitFloatValue(this, data)
override fun toString() = "$value.toFloat()"
}
class IntValue(
value: Int,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Int>(value) {
override val type = builtIns.intType
class IntValue(value: Int) : IntegerValueConstant<Int>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.intType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitIntValue(this, data)
@@ -188,54 +169,41 @@ class IntValue(
override fun hashCode() = value
}
class KClassValue(override val type: KotlinType) :
ConstantValue<KotlinType>(type) {
class KClassValue(private val type: KotlinType) : ConstantValue<KotlinType>(type) {
override fun getType(module: ModuleDescriptor): KotlinType = type
override val value: KotlinType
get() = type.arguments.single().type
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitKClassValue(this, data)
}
class LongValue(
value: Long,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Long>(value) {
override val type = builtIns.longType
class LongValue(value: Long) : IntegerValueConstant<Long>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.longType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitLongValue(this, data)
override fun toString() = "$value.toLong()"
}
class NullValue(
builtIns: KotlinBuiltIns
) : ConstantValue<Void?>(null) {
override val type = builtIns.nullableNothingType
class NullValue : ConstantValue<Void?>(null) {
override fun getType(module: ModuleDescriptor) = module.builtIns.nullableNothingType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitNullValue(this, data)
override fun toString() = "null"
}
class ShortValue(
value: Short,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Short>(value) {
override val type = builtIns.shortType
class ShortValue(value: Short) : IntegerValueConstant<Short>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.shortType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitShortValue(this, data)
override fun toString() = "$value.toShort()"
}
class StringValue(
value: String,
builtIns: KotlinBuiltIns
) : ConstantValue<String>(value) {
override val type = builtIns.stringType
class StringValue(value: String) : ConstantValue<String>(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.stringType
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitStringValue(this, data)
@@ -40,8 +40,6 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
private val builtIns: KotlinBuiltIns
get() = module.builtIns
private val factory = ConstantValueFactory(builtIns)
fun deserializeAnnotation(proto: Annotation, nameResolver: NameResolver): AnnotationDescriptor {
val annotationClass = resolveClass(nameResolver.getClassId(proto.id))
@@ -72,16 +70,16 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
nameResolver: NameResolver
): ConstantValue<*> {
val result: ConstantValue<*> = when (value.type) {
Type.BYTE -> factory.createByteValue(value.intValue.toByte())
Type.CHAR -> factory.createCharValue(value.intValue.toChar())
Type.SHORT -> factory.createShortValue(value.intValue.toShort())
Type.INT -> factory.createIntValue(value.intValue.toInt())
Type.LONG -> factory.createLongValue(value.intValue)
Type.FLOAT -> factory.createFloatValue(value.floatValue)
Type.DOUBLE -> factory.createDoubleValue(value.doubleValue)
Type.BOOLEAN -> factory.createBooleanValue(value.intValue != 0L)
Type.BYTE -> ConstantValueFactory.createByteValue(value.intValue.toByte())
Type.CHAR -> ConstantValueFactory.createCharValue(value.intValue.toChar())
Type.SHORT -> ConstantValueFactory.createShortValue(value.intValue.toShort())
Type.INT -> ConstantValueFactory.createIntValue(value.intValue.toInt())
Type.LONG -> ConstantValueFactory.createLongValue(value.intValue)
Type.FLOAT -> ConstantValueFactory.createFloatValue(value.floatValue)
Type.DOUBLE -> ConstantValueFactory.createDoubleValue(value.doubleValue)
Type.BOOLEAN -> ConstantValueFactory.createBooleanValue(value.intValue != 0L)
Type.STRING -> {
factory.createStringValue(nameResolver.getString(value.stringValue))
ConstantValueFactory.createStringValue(nameResolver.getString(value.stringValue))
}
Type.CLASS -> {
resolveClassLiteralValue(nameResolver.getClassId(value.classId))
@@ -111,7 +109,7 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
val expectedElementType = builtIns.getArrayElementType(if (expectedIsArray) expectedType else actualArrayType)
factory.createArrayValue(
ConstantValueFactory.createArrayValue(
arrayElements.map {
resolveValue(expectedElementType, it, nameResolver)
},
@@ -121,12 +119,12 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
else -> error("Unsupported annotation argument type: ${value.type} (expected $expectedType)")
}
return if (result.type.isSubtypeOf(expectedType)) {
return if (result.getType(module).isSubtypeOf(expectedType)) {
result
}
else {
// This means that an annotation class has been changed incompatibly without recompiling clients
factory.createErrorValue("Unexpected argument value")
ConstantValueFactory.createErrorValue("Unexpected argument value")
}
}
@@ -136,7 +134,7 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
val starProjectedType = resolveClass(classId).defaultType.replaceArgumentsWithStarProjections()
val kClass = resolveClass(ClassId.topLevel(KotlinBuiltIns.FQ_NAMES.kClass.toSafe()))
val type = KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, kClass, listOf(TypeProjectionImpl(starProjectedType)))
return factory.createKClassValue(type)
return ConstantValueFactory.createKClassValue(type)
}
// NOTE: see analogous code in BinaryClassAnnotationAndConstantLoaderImpl
@@ -145,10 +143,10 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
if (enumClass.kind == ClassKind.ENUM_CLASS) {
val enumEntry = enumClass.unsubstitutedInnerClassesScope.getContributedClassifier(enumEntryName, NoLookupLocation.FROM_DESERIALIZATION)
if (enumEntry is ClassDescriptor) {
return factory.createEnumValue(enumEntry)
return ConstantValueFactory.createEnumValue(enumEntry)
}
}
return factory.createErrorValue("Unresolved enum entry: $enumClassId.$enumEntryName")
return ConstantValueFactory.createErrorValue("Unresolved enum entry: $enumClassId.$enumEntryName")
}
private fun resolveArrayElementType(value: Value, nameResolver: NameResolver): SimpleType =