Inject builtins in constants

This commit is contained in:
Pavel V. Talanov
2015-07-07 13:34:32 +03:00
parent ea1a85e78c
commit 5dc5d77e60
33 changed files with 111 additions and 96 deletions
@@ -274,7 +274,7 @@ public abstract class AnnotationCodegen {
@Override @Override
public Void visitEnumValue(EnumValue value, Void data) { public Void visitEnumValue(EnumValue value, Void data) {
String propertyName = value.getValue().getName().asString(); String propertyName = value.getValue().getName().asString();
annotationVisitor.visitEnum(name, typeMapper.mapType(value.getType(KotlinBuiltIns.getInstance())).getDescriptor(), propertyName); annotationVisitor.visitEnum(name, typeMapper.mapType(value.getType()).getDescriptor(), propertyName);
return null; return null;
} }
@@ -282,7 +282,7 @@ public abstract class AnnotationCodegen {
public Void visitArrayValue(ArrayValue value, Void data) { public Void visitArrayValue(ArrayValue value, Void data) {
AnnotationVisitor visitor = annotationVisitor.visitArray(name); AnnotationVisitor visitor = annotationVisitor.visitArray(name);
for (CompileTimeConstant<?> argument : value.getValue()) { for (CompileTimeConstant<?> argument : value.getValue()) {
genCompileTimeValue(null, argument, value.getType(KotlinBuiltIns.getInstance()), visitor); genCompileTimeValue(null, argument, value.getType(), visitor);
} }
visitor.visitEnd(); visitor.visitEnd();
return null; return null;
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen;
import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.codegen.state.JetTypeMapper; import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
@@ -45,6 +46,7 @@ import static org.jetbrains.kotlin.codegen.JvmSerializationBindings.*;
public class JvmSerializerExtension extends SerializerExtension { public class JvmSerializerExtension extends SerializerExtension {
private final JvmSerializationBindings bindings; private final JvmSerializationBindings bindings;
private final JetTypeMapper typeMapper; private final JetTypeMapper typeMapper;
private final AnnotationSerializer annotationSerializer = new AnnotationSerializer(KotlinBuiltIns.getInstance());
public JvmSerializerExtension(@NotNull JvmSerializationBindings bindings, @NotNull JetTypeMapper typeMapper) { public JvmSerializerExtension(@NotNull JvmSerializationBindings bindings, @NotNull JetTypeMapper typeMapper) {
this.bindings = bindings; this.bindings = bindings;
@@ -77,7 +79,7 @@ public class JvmSerializerExtension extends SerializerExtension {
public void serializeType(@NotNull JetType type, @NotNull ProtoBuf.Type.Builder proto, @NotNull StringTable stringTable) { public void serializeType(@NotNull JetType type, @NotNull ProtoBuf.Type.Builder proto, @NotNull StringTable stringTable) {
// TODO: don't store type annotations in our binary metadata on Java 8, use *TypeAnnotations attributes instead // TODO: don't store type annotations in our binary metadata on Java 8, use *TypeAnnotations attributes instead
for (AnnotationDescriptor annotation : type.getAnnotations()) { for (AnnotationDescriptor annotation : type.getAnnotations()) {
proto.addExtension(JvmProtoBuf.typeAnnotation, AnnotationSerializer.INSTANCE$.serializeAnnotation(annotation, stringTable)); proto.addExtension(JvmProtoBuf.typeAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable));
} }
} }
@@ -113,7 +113,7 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
// TODO: perform some kind of validation? At the moment not possible because DescriptorValidator is in compiler-tests // TODO: perform some kind of validation? At the moment not possible because DescriptorValidator is in compiler-tests
// DescriptorValidator.validate(packageView) // DescriptorValidator.validate(packageView)
val serializer = DescriptorSerializer.createTopLevel(BuiltInsSerializerExtension) val serializer = DescriptorSerializer.createTopLevel(BuiltInsSerializerExtension(module))
val classifierDescriptors = DescriptorSerializer.sort(packageView.memberScope.getDescriptors(DescriptorKindFilter.CLASSIFIERS)) val classifierDescriptors = DescriptorSerializer.sort(packageView.memberScope.getDescriptors(DescriptorKindFilter.CLASSIFIERS))
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaPropertyInitializerEvaluator
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstantFactory; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstantFactory;
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
public class JavaPropertyInitializerEvaluatorImpl implements JavaPropertyInitializerEvaluator { public class JavaPropertyInitializerEvaluatorImpl implements JavaPropertyInitializerEvaluator {
@Nullable @Nullable
@@ -40,7 +41,7 @@ public class JavaPropertyInitializerEvaluatorImpl implements JavaPropertyInitial
ConstantExpressionEvaluator.isPropertyCompileTimeConstant(descriptor), ConstantExpressionEvaluator.isPropertyCompileTimeConstant(descriptor),
false, false,
true true
)); ), DescriptorUtilPackage.getBuiltIns(descriptor));
return factory.createCompileTimeConstant(evaluatedExpression, descriptor.getType()); return factory.createCompileTimeConstant(evaluatedExpression, descriptor.getType());
} }
return null; return null;
@@ -61,11 +61,9 @@ import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
public class CallExpressionResolver { public class CallExpressionResolver {
private final CallResolver callResolver; private final CallResolver callResolver;
private final KotlinBuiltIns builtIns;
public CallExpressionResolver(@NotNull CallResolver callResolver, @NotNull KotlinBuiltIns builtIns) { public CallExpressionResolver(@NotNull CallResolver callResolver) {
this.callResolver = callResolver; this.callResolver = callResolver;
this.builtIns = builtIns;
} }
private ExpressionTypingServices expressionTypingServices; private ExpressionTypingServices expressionTypingServices;
@@ -372,7 +370,7 @@ public class CallExpressionResolver {
CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate(expression, context.trace, context.expectedType); CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate(expression, context.trace, context.expectedType);
if (value instanceof IntegerValueConstant && ((IntegerValueConstant) value).isPure()) { if (value instanceof IntegerValueConstant && ((IntegerValueConstant) value).isPure()) {
return ExpressionTypingUtils.createCompileTimeConstantTypeInfo(value, expression, context, builtIns); return ExpressionTypingUtils.createCompileTimeConstantTypeInfo(value, expression, context);
} }
JetTypeInfo typeInfo; JetTypeInfo typeInfo;
@@ -89,7 +89,7 @@ public class CompileTimeConstantChecker {
} }
if (!noExpectedTypeOrError(expectedType)) { if (!noExpectedTypeOrError(expectedType)) {
JetType valueType = value.getType(builtIns); JetType valueType = value.getType();
if (!JetTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) { if (!JetTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) {
return reportError(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, "integer", expectedType)); return reportError(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, "integer", expectedType));
} }
@@ -106,7 +106,7 @@ public class CompileTimeConstantChecker {
return reportError(FLOAT_LITERAL_OUT_OF_RANGE.on(expression)); return reportError(FLOAT_LITERAL_OUT_OF_RANGE.on(expression));
} }
if (!noExpectedTypeOrError(expectedType)) { if (!noExpectedTypeOrError(expectedType)) {
JetType valueType = value.getType(builtIns); JetType valueType = value.getType();
if (!JetTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) { if (!JetTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) {
return reportError(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, "floating-point", expectedType)); return reportError(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, "floating-point", expectedType));
} }
@@ -42,6 +42,8 @@ import kotlin.platform.platformStatic
public class ConstantExpressionEvaluator private constructor(val trace: BindingTrace) : JetVisitor<CompileTimeConstant<*>, JetType>() { public class ConstantExpressionEvaluator private constructor(val trace: BindingTrace) : JetVisitor<CompileTimeConstant<*>, JetType>() {
private val builtIns = KotlinBuiltIns.getInstance()
companion object { companion object {
platformStatic public fun evaluate(expression: JetExpression, trace: BindingTrace, expectedType: JetType? = TypeUtils.NO_EXPECTED_TYPE): CompileTimeConstant<*>? { platformStatic public fun evaluate(expression: JetExpression, trace: BindingTrace, expectedType: JetType? = TypeUtils.NO_EXPECTED_TYPE): CompileTimeConstant<*>? {
val evaluator = ConstantExpressionEvaluator(trace) val evaluator = ConstantExpressionEvaluator(trace)
@@ -86,7 +88,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT
} }
private val stringExpressionEvaluator = object : JetVisitor<StringValue, Nothing>() { private val stringExpressionEvaluator = object : JetVisitor<StringValue, Nothing>() {
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false)) private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false), builtIns)
fun evaluate(entry: JetStringTemplateEntry): StringValue? { fun evaluate(entry: JetStringTemplateEntry): StringValue? {
return entry.accept(this, null) return entry.accept(this, null)
@@ -108,7 +110,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT
val text = expression.getText() ?: return null val text = expression.getText() ?: return null
val nodeElementType = expression.getNode().getElementType() val nodeElementType = expression.getNode().getElementType()
if (nodeElementType == JetNodeTypes.NULL) return NullValue if (nodeElementType == JetNodeTypes.NULL) return NullValue(builtIns)
val result: Any? = when (nodeElementType) { val result: Any? = when (nodeElementType) {
JetNodeTypes.INTEGER_CONSTANT -> parseLong(text) JetNodeTypes.INTEGER_CONSTANT -> parseLong(text)
@@ -249,7 +251,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT
val canBeUsedInAnnotation = canBeUsedInAnnotation(argumentForReceiver.expression) && canBeUsedInAnnotation(argumentForParameter.expression) val canBeUsedInAnnotation = canBeUsedInAnnotation(argumentForReceiver.expression) && canBeUsedInAnnotation(argumentForParameter.expression)
val usesVariableAsConstant = usesVariableAsConstant(argumentForReceiver.expression) || usesVariableAsConstant(argumentForParameter.expression) val usesVariableAsConstant = usesVariableAsConstant(argumentForReceiver.expression) || usesVariableAsConstant(argumentForParameter.expression)
val parameters = CompileTimeConstant.Parameters.Impl(canBeUsedInAnnotation, areArgumentsPure, usesVariableAsConstant) val parameters = CompileTimeConstant.Parameters.Impl(canBeUsedInAnnotation, areArgumentsPure, usesVariableAsConstant)
val factory = CompileTimeConstantFactory(parameters) val factory = CompileTimeConstantFactory(parameters, builtIns)
return when (resultingDescriptorName) { return when (resultingDescriptorName) {
OperatorConventions.COMPARE_TO -> createCompileTimeConstantForCompareTo(result, callExpression, factory) OperatorConventions.COMPARE_TO -> createCompileTimeConstantForCompareTo(result, callExpression, factory)
OperatorConventions.EQUALS -> createCompileTimeConstantForEquals(result, callExpression, factory) OperatorConventions.EQUALS -> createCompileTimeConstantForEquals(result, callExpression, factory)
@@ -493,12 +495,12 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT
expectedType: JetType?, expectedType: JetType?,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters
): CompileTimeConstant<*>? { ): CompileTimeConstant<*>? {
return CompileTimeConstantFactory(parameters).createCompileTimeConstant(value, if (parameters.isPure) expectedType ?: TypeUtils.NO_EXPECTED_TYPE else null) return CompileTimeConstantFactory(parameters, builtIns).createCompileTimeConstant(value, if (parameters.isPure) expectedType ?: TypeUtils.NO_EXPECTED_TYPE else null)
} }
} }
public fun IntegerValueTypeConstant.createCompileTimeConstantWithType(expectedType: JetType): CompileTimeConstant<*>? public fun IntegerValueTypeConstant.createCompileTimeConstantWithType(expectedType: JetType): CompileTimeConstant<*>?
= CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(this.canBeUsedInAnnotations(), true, false)).createCompileTimeConstant(this.getValue(expectedType)) = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(this.canBeUsedInAnnotations(), true, false), KotlinBuiltIns.getInstance()).createCompileTimeConstant(this.getValue(expectedType))
private fun hasLongSuffix(text: String) = text.endsWith('l') || text.endsWith('L') private fun hasLongSuffix(text: String) = text.endsWith('l') || text.endsWith('L')
@@ -594,13 +596,13 @@ private fun createCompileTimeConstantForCompareTo(result: Any?, operationReferen
private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? { private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? {
return when (value) { return when (value) {
is IntegerValueTypeConstant -> CompileTimeConstantFactory(value.parameters).createStringValue(value.getValue(TypeUtils.NO_EXPECTED_TYPE).toString()) is IntegerValueTypeConstant -> CompileTimeConstantFactory(value.parameters, KotlinBuiltIns.getInstance()).createStringValue(value.getValue(TypeUtils.NO_EXPECTED_TYPE).toString())
is StringValue -> value is StringValue -> value
is IntValue, is ByteValue, is ShortValue, is LongValue, is IntValue, is ByteValue, is ShortValue, is LongValue,
is CharValue, is CharValue,
is DoubleValue, is FloatValue, is DoubleValue, is FloatValue,
is BooleanValue, is BooleanValue,
is NullValue -> CompileTimeConstantFactory(value.parameters).createStringValue("${value.value}") is NullValue -> CompileTimeConstantFactory(value.parameters, KotlinBuiltIns.getInstance()).createStringValue("${value.value}")
else -> null else -> null
} }
} }
@@ -133,7 +133,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
} }
assert value != null : "CompileTimeConstant should be evaluated for constant expression or an error should be recorded " + expression.getText(); assert value != null : "CompileTimeConstant should be evaluated for constant expression or an error should be recorded " + expression.getText();
return createCompileTimeConstantTypeInfo(value, expression, context, components.builtIns); return createCompileTimeConstantTypeInfo(value, expression, context);
} }
@NotNull @NotNull
@@ -942,7 +942,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate(expression, contextWithExpectedType.trace, CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate(expression, contextWithExpectedType.trace,
contextWithExpectedType.expectedType); contextWithExpectedType.expectedType);
if (value != null) { if (value != null) {
return createCompileTimeConstantTypeInfo(value, expression, contextWithExpectedType, components.builtIns); return createCompileTimeConstantTypeInfo(value, expression, contextWithExpectedType);
} }
return DataFlowUtils.checkType(typeInfo.replaceType(result), return DataFlowUtils.checkType(typeInfo.replaceType(result),
@@ -1138,7 +1138,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
expression, contextWithExpectedType.trace, contextWithExpectedType.expectedType expression, contextWithExpectedType.trace, contextWithExpectedType.expectedType
); );
if (value != null) { if (value != null) {
return createCompileTimeConstantTypeInfo(value, expression, contextWithExpectedType, components.builtIns); return createCompileTimeConstantTypeInfo(value, expression, contextWithExpectedType);
} }
return DataFlowUtils.checkType(result, expression, contextWithExpectedType); return DataFlowUtils.checkType(result, expression, contextWithExpectedType);
} }
@@ -246,10 +246,9 @@ public class ExpressionTypingUtils {
public static JetTypeInfo createCompileTimeConstantTypeInfo( public static JetTypeInfo createCompileTimeConstantTypeInfo(
@NotNull CompileTimeConstant<?> value, @NotNull CompileTimeConstant<?> value,
@NotNull JetExpression expression, @NotNull JetExpression expression,
@NotNull ExpressionTypingContext context, @NotNull ExpressionTypingContext context
@NotNull KotlinBuiltIns kotlinBuiltIns
) { ) {
JetType expressionType = value.getType(kotlinBuiltIns); JetType expressionType = value.getType();
if (value instanceof IntegerValueTypeConstant && context.contextDependency == INDEPENDENT) { if (value instanceof IntegerValueTypeConstant && context.contextDependency == INDEPENDENT) {
expressionType = ((IntegerValueTypeConstant) value).getType(context.expectedType); expressionType = ((IntegerValueTypeConstant) value).getType(context.expectedType);
ArgumentTypeResolver.updateNumberType(expressionType, expression, context); ArgumentTypeResolver.updateNumberType(expressionType, expression, context);
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor 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.AnnotationArgumentVisitor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.resolve.constants.* import org.jetbrains.kotlin.resolve.constants.*
@@ -26,9 +27,9 @@ import org.jetbrains.kotlin.serialization.ProtoBuf.Annotation.Argument.Value.Typ
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
public object AnnotationSerializer { public class AnnotationSerializer(builtIns: KotlinBuiltIns) {
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException) private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, builtIns)
public fun serializeAnnotation(annotation: AnnotationDescriptor, stringTable: StringTable): ProtoBuf.Annotation { public fun serializeAnnotation(annotation: AnnotationDescriptor, stringTable: StringTable): ProtoBuf.Annotation {
return with(ProtoBuf.Annotation.newBuilder()) { return with(ProtoBuf.Annotation.newBuilder()) {
@@ -17,11 +17,7 @@
package org.jetbrains.kotlin.serialization.builtins package org.jetbrains.kotlin.serialization.builtins
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.resolve.constants.NullValue import org.jetbrains.kotlin.resolve.constants.NullValue
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
@@ -31,10 +27,13 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.SerializerExtension import org.jetbrains.kotlin.serialization.SerializerExtension
import org.jetbrains.kotlin.serialization.StringTable import org.jetbrains.kotlin.serialization.StringTable
public object BuiltInsSerializerExtension : SerializerExtension() { public class BuiltInsSerializerExtension(module: ModuleDescriptor) : SerializerExtension() {
private val annotationSerializer = AnnotationSerializer(module.builtIns)
override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder, stringTable: StringTable) { override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder, stringTable: StringTable) {
for (annotation in descriptor.getAnnotations()) { for (annotation in descriptor.getAnnotations()) {
proto.addExtension(BuiltInsProtoBuf.classAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(BuiltInsProtoBuf.classAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
} }
@@ -58,13 +57,13 @@ public object BuiltInsSerializerExtension : SerializerExtension() {
stringTable: StringTable stringTable: StringTable
) { ) {
for (annotation in callable.getAnnotations()) { for (annotation in callable.getAnnotations()) {
proto.addExtension(BuiltInsProtoBuf.callableAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(BuiltInsProtoBuf.callableAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
val propertyDescriptor = callable as? PropertyDescriptor ?: return val propertyDescriptor = callable as? PropertyDescriptor ?: return
val compileTimeConstant = propertyDescriptor.getCompileTimeInitializer() val compileTimeConstant = propertyDescriptor.getCompileTimeInitializer()
if (compileTimeConstant != null && compileTimeConstant !is NullValue) { if (compileTimeConstant != null && compileTimeConstant !is NullValue) {
val type = compileTimeConstant.getType(propertyDescriptor.builtIns) val valueProto = annotationSerializer.valueProto(compileTimeConstant, compileTimeConstant.type, stringTable)
proto.setExtension(BuiltInsProtoBuf.compileTimeValue, AnnotationSerializer.valueProto(compileTimeConstant, type, stringTable).build()) proto.setExtension(BuiltInsProtoBuf.compileTimeValue, valueProto.build())
} }
} }
@@ -74,7 +73,7 @@ public object BuiltInsSerializerExtension : SerializerExtension() {
stringTable: StringTable stringTable: StringTable
) { ) {
for (annotation in descriptor.getAnnotations()) { for (annotation in descriptor.getAnnotations()) {
proto.addExtension(BuiltInsProtoBuf.parameterAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(BuiltInsProtoBuf.parameterAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
} }
} }
@@ -64,7 +64,7 @@ class LazyJavaAnnotationDescriptor(
annotationClass?.getDefaultType() ?: ErrorUtils.createErrorType(fqName.asString()) annotationClass?.getDefaultType() ?: ErrorUtils.createErrorType(fqName.asString())
} }
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false)) private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false), c.module.builtIns)
override fun getType(): JetType = type() override fun getType(): JetType = type()
@@ -46,7 +46,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl(
storageManager, kotlinClassFinder, errorReporter storageManager, kotlinClassFinder, errorReporter
) { ) {
private val annotationDeserializer = AnnotationDeserializer(module) private val annotationDeserializer = AnnotationDeserializer(module)
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException) private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, module.builtIns)
override fun loadTypeAnnotation(proto: ProtoBuf.Annotation, nameResolver: NameResolver): AnnotationDescriptor = override fun loadTypeAnnotation(proto: ProtoBuf.Annotation, nameResolver: NameResolver): AnnotationDescriptor =
annotationDeserializer.deserializeAnnotation(proto, nameResolver) annotationDeserializer.deserializeAnnotation(proto, nameResolver)
@@ -23,7 +23,8 @@ import org.jetbrains.kotlin.types.JetType
public class AnnotationValue(value: AnnotationDescriptor) : public class AnnotationValue(value: AnnotationDescriptor) :
CompileTimeConstant<AnnotationDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) { CompileTimeConstant<AnnotationDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = value.getType() override val type: JetType
get() = value.getType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitAnnotationValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitAnnotationValue(this, data)
@@ -23,7 +23,7 @@ import java.util.*
public class ArrayValue( public class ArrayValue(
value: List<CompileTimeConstant<*>>, value: List<CompileTimeConstant<*>>,
private val type: JetType, override val type: JetType,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<List<CompileTimeConstant<*>>>(value, parameters) { ) : CompileTimeConstant<List<CompileTimeConstant<*>>>(value, parameters) {
@@ -40,8 +40,6 @@ public class ArrayValue(
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value } assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value }
} }
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = type
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitArrayValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitArrayValue(this, data)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
@@ -22,11 +22,12 @@ import org.jetbrains.kotlin.types.JetType
public class BooleanValue( public class BooleanValue(
value: Boolean, value: Boolean,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : CompileTimeConstant<Boolean>(value, parameters) { ) : CompileTimeConstant<Boolean>(value, parameters) {
override fun isPure(): Boolean = false override fun isPure(): Boolean = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getBooleanType() override val type = builtIns.getBooleanType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitBooleanValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitBooleanValue(this, data)
} }
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.types.JetType
public class ByteValue( public class ByteValue(
value: Byte, value: Byte,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Byte>(value, parameters) { ) : IntegerValueConstant<Byte>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getByteType() override val type = builtIns.getByteType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitByteValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitByteValue(this, data)
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.types.JetType
public class CharValue( public class CharValue(
value: Char, value: Char,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Char>(value, parameters) { ) : IntegerValueConstant<Char>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getCharType() override val type = builtIns.getCharType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitCharValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitCharValue(this, data)
@@ -30,7 +30,7 @@ public abstract class CompileTimeConstant<T> protected constructor(
public open fun usesVariableAsConstant(): Boolean = parameters.usesVariableAsConstant public open fun usesVariableAsConstant(): Boolean = parameters.usesVariableAsConstant
public abstract fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType public abstract val type: JetType
public abstract fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R public abstract fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D): R
@@ -22,28 +22,31 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.TypeUtils
public class CompileTimeConstantFactory(private val parameters: CompileTimeConstant.Parameters) { public class CompileTimeConstantFactory(
fun createLongValue(value: Long) = LongValue(value, parameters) private val parameters: CompileTimeConstant.Parameters,
private val builtins: KotlinBuiltIns
) {
fun createLongValue(value: Long) = LongValue(value, parameters, builtins)
fun createIntValue(value: Int) = IntValue(value, parameters) fun createIntValue(value: Int) = IntValue(value, parameters, builtins)
fun createErrorValue(message: String) = ErrorValue.create(message) fun createErrorValue(message: String) = ErrorValue.create(message)
fun createShortValue(value: Short) = ShortValue(value, parameters) fun createShortValue(value: Short) = ShortValue(value, parameters, builtins)
fun createByteValue(value: Byte) = ByteValue(value, parameters) fun createByteValue(value: Byte) = ByteValue(value, parameters, builtins)
fun createDoubleValue(value: Double) = DoubleValue(value, parameters) fun createDoubleValue(value: Double) = DoubleValue(value, parameters, builtins)
fun createFloatValue(value: Float) = FloatValue(value, parameters) fun createFloatValue(value: Float) = FloatValue(value, parameters, builtins)
fun createBooleanValue(value: Boolean) = BooleanValue(value, parameters) fun createBooleanValue(value: Boolean) = BooleanValue(value, parameters, builtins)
fun createCharValue(value: Char) = CharValue(value, parameters) fun createCharValue(value: Char) = CharValue(value, parameters, builtins)
fun createStringValue(value: String) = StringValue(value, parameters) fun createStringValue(value: String) = StringValue(value, parameters, builtins)
fun createNullValue() = NullValue fun createNullValue() = NullValue(builtins)
fun createEnumValue(enumEntryClass: ClassDescriptor): EnumValue = EnumValue(enumEntryClass) fun createEnumValue(enumEntryClass: ClassDescriptor): EnumValue = EnumValue(enumEntryClass)
@@ -22,12 +22,13 @@ import org.jetbrains.kotlin.types.JetType
public class DoubleValue( public class DoubleValue(
value: Double, value: Double,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : CompileTimeConstant<Double>(value, parameters) { ) : CompileTimeConstant<Double>(value, parameters) {
override fun isPure() = false override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getDoubleType() override val type = builtIns.getDoubleType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitDoubleValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitDoubleValue(this, data)
@@ -27,13 +27,12 @@ public class EnumValue(
value: ClassDescriptor value: ClassDescriptor
) : CompileTimeConstant<ClassDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) { ) : CompileTimeConstant<ClassDescriptor>(value, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = getType() override val type: JetType
get() = value.classObjectType.sure { "Enum entry must have a class object type: " + value }
private fun getType() = value.classObjectType.sure { "Enum entry must have a class object type: " + value }
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitEnumValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitEnumValue(this, data)
override fun toString() = "${getType()}.${value.getName()}" override fun toString() = "$type.${value.getName()}"
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
@@ -31,7 +31,7 @@ public abstract class ErrorValue : CompileTimeConstant<Unit>(Unit, CompileTimeCo
public class ErrorValueWithMessage(public val message: String) : ErrorValue() { public class ErrorValueWithMessage(public val message: String) : ErrorValue() {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = ErrorUtils.createErrorType(message) override val type = ErrorUtils.createErrorType(message)
override fun toString() = message override fun toString() = message
} }
@@ -22,11 +22,12 @@ import org.jetbrains.kotlin.types.JetType
public class FloatValue( public class FloatValue(
value: Float, value: Float,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : CompileTimeConstant<Float>(value, parameters) { ) : CompileTimeConstant<Float>(value, parameters) {
override fun isPure() = false override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getFloatType() override val type = builtIns.getFloatType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitFloatValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitFloatValue(this, data)
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.types.JetType
public class IntValue( public class IntValue(
value: Int, value: Int,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Int>(value, parameters) { ) : IntegerValueConstant<Int>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getIntType() override val type = builtIns.getIntType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitIntValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitIntValue(this, data)
@@ -32,12 +32,10 @@ public class IntegerValueTypeConstant(
private val typeConstructor = IntegerValueTypeConstructor(value.toLong()) private val typeConstructor = IntegerValueTypeConstructor(value.toLong())
override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { override val type = JetTypeImpl(
return JetTypeImpl( Annotations.EMPTY, typeConstructor, false, emptyList<TypeProjection>()
Annotations.EMPTY, typeConstructor, false, emptyList<TypeProjection>() , ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)
, ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true) )
)
}
deprecated("") deprecated("")
override val value: Number override val value: Number
@@ -20,9 +20,8 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
public class KClassValue(private val type: JetType) : public class KClassValue(override val type: JetType) :
CompileTimeConstant<JetType>(type, CompileTimeConstant.Parameters.Impl(true, false, false)) { CompileTimeConstant<JetType>(type, CompileTimeConstant.Parameters.Impl(true, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = type
override val value: JetType override val value: JetType
get() = type.getArguments().single().getType() get() = type.getArguments().single().getType()
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.types.JetType
public class LongValue( public class LongValue(
value: Long, value: Long,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Long>(value, parameters) { ) : IntegerValueConstant<Long>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getLongType() override val type = builtIns.getLongType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitLongValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitLongValue(this, data)
@@ -20,9 +20,11 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
public object NullValue : CompileTimeConstant<Void?>(null, CompileTimeConstant.Parameters.Impl(false, false, false)) { public class NullValue(
builtIns: KotlinBuiltIns
) : CompileTimeConstant<Void?>(null, CompileTimeConstant.Parameters.Impl(false, false, false)) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getNullableNothingType() override val type = builtIns.getNullableNothingType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitNullValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitNullValue(this, data)
@@ -22,10 +22,11 @@ import org.jetbrains.kotlin.types.JetType
public class ShortValue( public class ShortValue(
value: Short, value: Short,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : IntegerValueConstant<Short>(value, parameters) { ) : IntegerValueConstant<Short>(value, parameters) {
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getShortType() override val type = builtIns.getShortType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitShortValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitShortValue(this, data)
@@ -22,11 +22,12 @@ import org.jetbrains.kotlin.types.JetType
public class StringValue( public class StringValue(
value: String, value: String,
parameters: CompileTimeConstant.Parameters parameters: CompileTimeConstant.Parameters,
builtIns: KotlinBuiltIns
) : CompileTimeConstant<String>(value, parameters) { ) : CompileTimeConstant<String>(value, parameters) {
override fun isPure() = false override fun isPure() = false
override fun getType(kotlinBuiltIns: KotlinBuiltIns) = kotlinBuiltIns.getStringType() override val type = builtIns.getStringType()
override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitStringValue(this, data) override fun <R, D> accept(visitor: AnnotationArgumentVisitor<R, D>, data: D) = visitor.visitStringValue(this, data)
@@ -39,7 +39,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) {
private val builtIns: KotlinBuiltIns private val builtIns: KotlinBuiltIns
get() = module.builtIns get() = module.builtIns
private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException) private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, builtIns)
public fun deserializeAnnotation(proto: Annotation, nameResolver: NameResolver): AnnotationDescriptor { public fun deserializeAnnotation(proto: Annotation, nameResolver: NameResolver): AnnotationDescriptor {
val annotationClass = resolveClass(nameResolver.getClassId(proto.getId())) val annotationClass = resolveClass(nameResolver.getClassId(proto.getId()))
@@ -121,7 +121,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) {
else -> error("Unsupported annotation argument type: ${value.getType()} (expected $expectedType)") else -> error("Unsupported annotation argument type: ${value.getType()} (expected $expectedType)")
} }
if (result.getType(builtIns) isSubtypeOf expectedType) { if (result.type isSubtypeOf expectedType) {
return result return result
} }
else { else {
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.serialization.js package org.jetbrains.kotlin.serialization.js
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor
@@ -29,9 +30,12 @@ import org.jetbrains.kotlin.serialization.StringTable
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
public object KotlinJavascriptSerializerExtension : SerializerExtension() { public object KotlinJavascriptSerializerExtension : SerializerExtension() {
private val annotationSerializer = AnnotationSerializer(KotlinBuiltIns.getInstance())
override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder, stringTable: StringTable) { override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder, stringTable: StringTable) {
for (annotation in descriptor.getAnnotations()) { for (annotation in descriptor.getAnnotations()) {
proto.addExtension(JsProtoBuf.classAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(JsProtoBuf.classAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
} }
@@ -41,13 +45,13 @@ public object KotlinJavascriptSerializerExtension : SerializerExtension() {
stringTable: StringTable stringTable: StringTable
) { ) {
for (annotation in callable.getAnnotations()) { for (annotation in callable.getAnnotations()) {
proto.addExtension(JsProtoBuf.callableAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(JsProtoBuf.callableAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
val propertyDescriptor = callable as? PropertyDescriptor ?: return val propertyDescriptor = callable as? PropertyDescriptor ?: return
val compileTimeConstant = propertyDescriptor.getCompileTimeInitializer() val compileTimeConstant = propertyDescriptor.getCompileTimeInitializer()
if (compileTimeConstant != null && compileTimeConstant !is NullValue) { if (compileTimeConstant != null && compileTimeConstant !is NullValue) {
val type = compileTimeConstant.getType(propertyDescriptor.builtIns) val type = compileTimeConstant.type
proto.setExtension(JsProtoBuf.compileTimeValue, AnnotationSerializer.valueProto(compileTimeConstant, type, stringTable).build()) proto.setExtension(JsProtoBuf.compileTimeValue, annotationSerializer.valueProto(compileTimeConstant, type, stringTable).build())
} }
} }
@@ -57,13 +61,13 @@ public object KotlinJavascriptSerializerExtension : SerializerExtension() {
stringTable: StringTable stringTable: StringTable
) { ) {
for (annotation in descriptor.getAnnotations()) { for (annotation in descriptor.getAnnotations()) {
proto.addExtension(JsProtoBuf.parameterAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(JsProtoBuf.parameterAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
} }
override fun serializeType(type: JetType, proto: ProtoBuf.Type.Builder, stringTable: StringTable) { override fun serializeType(type: JetType, proto: ProtoBuf.Type.Builder, stringTable: StringTable) {
for (annotation in type.getAnnotations()) { for (annotation in type.getAnnotations()) {
proto.addExtension(JsProtoBuf.typeAnnotation, AnnotationSerializer.serializeAnnotation(annotation, stringTable)) proto.addExtension(JsProtoBuf.typeAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable))
} }
} }
} }