Load unsigned constants from class file with the use of expected type

This commit is contained in:
Mikhail Zarechenskiy
2018-07-09 13:28:10 +03:00
parent 9b6e8fa5d1
commit 14b8ff7d71
6 changed files with 45 additions and 3 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.builtins.UnsignedTypes
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.load.java.JvmAbi
@@ -48,6 +49,8 @@ abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any,
protected abstract fun loadConstant(desc: String, initializer: Any): C?
protected abstract fun transformToUnsignedConstant(constant: C): C?
protected abstract fun loadAnnotation(
annotationClassId: ClassId,
source: SourceElement,
@@ -202,7 +205,8 @@ abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C : Any,
val specialCase = getSpecialCaseContainerClass(container, property = true, field = true, isConst = Flags.IS_CONST.get(proto.flags))
val kotlinClass = findClassWithAnnotationsAndInitializers(container, specialCase) ?: return null
return storage(kotlinClass).propertyConstants[signature]
val constant = storage(kotlinClass).propertyConstants[signature] ?: return null
return if (UnsignedTypes.isUnsignedType(expectedType)) transformToUnsignedConstant(constant) else constant
}
private fun findClassWithAnnotationsAndInitializers(
@@ -64,6 +64,16 @@ class BinaryClassAnnotationAndConstantLoaderImpl(
return ConstantValueFactory.createConstantValue(normalizedValue)
}
override fun transformToUnsignedConstant(constant: ConstantValue<*>): ConstantValue<*>? {
return when (constant) {
is ByteValue -> UByteValue(constant.value)
is ShortValue -> UShortValue(constant.value)
is IntValue -> UIntValue(constant.value)
is LongValue -> ULongValue(constant.value)
else -> constant
}
}
override fun loadPropertyAnnotations(
propertyAnnotations: List<AnnotationDescriptor>,
fieldAnnotations: List<AnnotationDescriptor>,