Split CompileTimeConstant into two entities

1. ConstantValue
	* just holds some value and its type
	* implementations for concrete constants
2. CompileTimeConstant
	* is only produced by ConstantExpressionEvaluator
	* has additional flags (canBeUsedInAnnotation etc)
	* has two implementations TypedCompileTimeConstant containing a constant value
		and IntegerValueConstant which does not have exact type
	* can be converted to ConstantValue

Adjustt usages to use ConstantValue if flags are not needed
Add tests for some uncovered cases
This commit is contained in:
Pavel V. Talanov
2015-07-07 14:56:19 +03:00
parent 155f00578d
commit c313887641
134 changed files with 791 additions and 907 deletions
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.serialization
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.resolve.constants.*
@@ -29,8 +28,6 @@ import org.jetbrains.kotlin.types.JetType
public class AnnotationSerializer(builtIns: KotlinBuiltIns) {
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, builtIns)
public fun serializeAnnotation(annotation: AnnotationDescriptor, stringTable: StringTable): ProtoBuf.Annotation {
return with(ProtoBuf.Annotation.newBuilder()) {
val annotationClass = annotation.getType().getConstructor().getDeclarationDescriptor() as? ClassDescriptor
@@ -52,7 +49,7 @@ public class AnnotationSerializer(builtIns: KotlinBuiltIns) {
}
}
fun valueProto(constant: CompileTimeConstant<*>, type: JetType, nameTable: StringTable): Value.Builder = with(Value.newBuilder()) {
fun valueProto(constant: ConstantValue<*>, type: JetType, nameTable: StringTable): Value.Builder = with(Value.newBuilder()) {
constant.accept(object : AnnotationArgumentVisitor<Unit, Unit> {
override fun visitAnnotationValue(value: AnnotationValue, data: Unit) {
setType(Type.ANNOTATION)
@@ -121,22 +118,6 @@ public class AnnotationSerializer(builtIns: KotlinBuiltIns) {
throw UnsupportedOperationException("Null should not appear in annotation arguments")
}
override fun visitNumberTypeValue(constant: IntegerValueTypeConstant, data: Unit) {
// TODO: IntegerValueTypeConstant should not occur in annotation arguments
val number = constant.getValue(type)
val specificConstant = with(KotlinBuiltIns.getInstance()) {
when (type) {
getLongType() -> factory.createLongValue(number.toLong())
getIntType() -> factory.createIntValue(number.toInt())
getShortType() -> factory.createShortValue(number.toShort())
getByteType() -> factory.createByteValue(number.toByte())
else -> throw IllegalStateException("Integer constant $constant has non-integer type $type")
}
}
specificConstant.accept(this, data)
}
override fun visitShortValue(value: ShortValue, data: Unit) {
setType(Type.SHORT)
setIntValue(value.value.toLong())
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotated;
import org.jetbrains.kotlin.resolve.DescriptorFactory;
import org.jetbrains.kotlin.resolve.MemberComparator;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.constants.NullValue;
import org.jetbrains.kotlin.types.*;
@@ -188,7 +188,7 @@ public class DescriptorSerializer {
}
}
CompileTimeConstant<?> compileTimeConstant = propertyDescriptor.getCompileTimeInitializer();
ConstantValue<?> compileTimeConstant = propertyDescriptor.getCompileTimeInitializer();
hasConstant = !(compileTimeConstant == null || compileTimeConstant instanceof NullValue);
}