From c313887641e7a5b381b8508cab3b7b8debc2f86a Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 7 Jul 2015 14:56:19 +0300 Subject: [PATCH] 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 --- .../kotlin/codegen/AnnotationCodegen.java | 32 +-- .../kotlin/codegen/ExpressionCodegen.java | 20 +- .../kotlin/codegen/FunctionCodegen.java | 8 +- .../kotlin/codegen/MemberCodegen.java | 31 +-- .../kotlin/codegen/PropertyCodegen.java | 6 +- .../binding/CodegenAnnotatingVisitor.java | 8 +- .../kotlin/codegen/state/JetTypeMapper.java | 6 +- .../codegen/when/EnumSwitchCodegen.java | 4 +- .../when/IntegralConstantsSwitchCodegen.java | 4 +- .../codegen/when/StringSwitchCodegen.java | 4 +- .../kotlin/codegen/when/SwitchCodegen.java | 8 +- .../codegen/when/SwitchCodegenUtil.java | 26 +-- .../JavaPropertyInitializerEvaluatorImpl.java | 56 ----- .../JavaPropertyInitializerEvaluatorImpl.kt | 50 ++++ .../kotlin/resolve/AnnotationResolver.java | 35 ++- .../kotlin/resolve/BodyResolver.java | 4 +- .../resolve/CompileTimeConstantUtils.java | 12 +- .../kotlin/resolve/DescriptorResolver.java | 14 +- .../kotlin/resolve/ModifiersChecker.java | 8 +- .../resolve/calls/CallExpressionResolver.java | 3 +- .../util/FakeCallableDescriptorForObject.kt | 3 +- .../constants/CompileTimeConstantChecker.java | 8 +- .../evaluate/ConstantExpressionEvaluator.kt | 221 ++++++++++-------- .../DiagnosticsWithSuppression.java | 6 +- .../kotlin/resolve/inline/InlineUtil.java | 8 +- .../lazy/descriptors/LazyAnnotations.kt | 6 +- .../BasicExpressionTypingVisitor.java | 15 +- .../types/expressions/DataFlowUtils.java | 11 +- .../expressions/ExpressionTypingUtils.java | 19 +- .../serialization/AnnotationSerializer.kt | 21 +- .../serialization/DescriptorSerializer.java | 4 +- .../javaPropertyAsAnnotationParameter.java | 2 + .../javaPropertyAsAnnotationParameter.kt | 8 +- .../tests/annotations/AnnotatedResultType.txt | 2 +- .../tests/annotations/AnnotationOnObject.txt | 2 +- .../tests/annotations/BasicAnnotations.txt | 4 +- .../tests/annotations/ConstructorCall.txt | 4 +- .../annotations/RecursivelyAnnotated.txt | 2 +- .../RecursivelyAnnotatedParameter.txt | 4 +- .../RecursivelyAnnotatedParameterType.txt | 4 +- .../RecursivelyAnnotatedParameterWithAt.txt | 4 +- ...cursivelyIncorrectlyAnnotatedParameter.txt | 4 +- .../WrongAnnotationArgsOnObject.txt | 2 +- .../tests/annotations/atAnnotationResolve.txt | 6 +- .../primaryConstructorMissingKeyword.txt | 2 +- .../namedArguments/allowForJavaAnnotation.txt | 2 +- .../ctrsAnnotationResolve.txt | 2 +- .../annotations/annotationClassMethodCall.txt | 2 +- .../array.txt | 2 +- .../vararg.txt | 2 +- .../annotationParameters/valueArray.txt | 14 +- .../valueArrayAndOtherDefault.txt | 10 +- .../javaAnnotationWithVarargArgument.txt | 2 +- .../kotlinAnnotationWithVarargArgument.txt | 2 +- .../annotationAsArgument.txt | 4 +- .../argAndOtherDefault.txt | 2 +- .../argWithDefaultAndOther.txt | 4 +- .../valueAndOtherDefault.txt | 4 +- .../valueWithDefaultAndOther.txt | 6 +- .../kotlinAnnotation.txt | 6 +- .../prohibitPositionedArgument/withValue.txt | 8 +- .../withoutValue.txt | 6 +- .../usesVariableAsConstant/OtherTypes.kt | 10 +- .../resolveAnnotations/parameters/byte.kt | 2 +- .../parameters/expressions/char.kt | 2 +- .../parameters/expressions/divide.kt | 2 +- .../parameters/expressions/infixCallBinary.kt | 2 +- .../parameters/expressions/intrincics.kt | 2 +- .../parameters/expressions/long.kt | 2 +- .../parameters/expressions/maxValueByte.kt | 2 +- .../parameters/expressions/maxValueInt.kt | 2 +- .../parameters/expressions/miltiply.kt | 2 +- .../parameters/expressions/minus.kt | 2 +- .../parameters/expressions/mod.kt | 2 +- .../parameters/expressions/paranthesized.kt | 2 +- .../parameters/expressions/plus.kt | 2 +- .../expressions/simpleCallBinary.kt | 2 +- .../parameters/expressions/unaryMinus.kt | 2 +- .../parameters/expressions/unaryPlus.kt | 2 +- .../resolveAnnotations/parameters/int.kt | 2 +- .../resolveAnnotations/parameters/long.kt | 2 +- .../resolveAnnotations/parameters/short.kt | 2 +- .../jvm/compiler/ExpectedLoadErrorsUtil.java | 4 +- .../AnnotationDescriptorResolveTest.java | 8 +- .../AbstractEvaluateExpressionTest.kt | 29 +-- .../LazyJavaAnnotationDescriptor.kt | 19 +- .../descriptors/LazyJavaClassDescriptor.kt | 2 +- .../JavaPropertyInitializerEvaluator.java | 42 ---- .../JavaPropertyInitializerEvaluator.kt | 32 +++ ...aryClassAnnotationAndConstantLoaderImpl.kt | 24 +- .../load/kotlin/reflect/RuntimeModuleData.kt | 2 +- .../BuiltInsAnnotationAndConstantLoader.kt | 6 +- .../kotlin/builtins/KotlinBuiltIns.java | 4 +- .../descriptors/VariableDescriptor.java | 4 +- .../AnnotationArgumentVisitor.java | 2 - .../annotations/AnnotationDescriptor.java | 4 +- .../annotations/AnnotationDescriptorImpl.java | 8 +- .../DefaultAnnotationArgumentVisitor.java | 7 +- .../impl/VariableDescriptorImpl.java | 8 +- .../kotlin/renderer/DescriptorRendererImpl.kt | 24 +- .../resolve/constants/AnnotationValue.kt | 4 +- .../kotlin/resolve/constants/ArrayValue.kt | 18 +- .../kotlin/resolve/constants/BooleanValue.kt | 6 +- .../kotlin/resolve/constants/ByteValue.kt | 4 +- .../kotlin/resolve/constants/CharValue.kt | 6 +- .../resolve/constants/CompileTimeConstant.kt | 99 +++++--- .../constants/CompileTimeConstantFactory.kt | 125 ---------- .../kotlin/resolve/constants/ConstantValue.kt | 29 +++ .../resolve/constants/ConstantValueFactory.kt | 95 ++++++++ .../kotlin/resolve/constants/DoubleValue.kt | 7 +- .../kotlin/resolve/constants/EnumValue.kt | 3 +- .../kotlin/resolve/constants/ErrorValue.kt | 4 +- .../kotlin/resolve/constants/FloatValue.kt | 6 +- .../kotlin/resolve/constants/IntValue.kt | 4 +- .../resolve/constants/IntegerValueConstant.kt | 5 +- .../constants/IntegerValueTypeConstant.kt | 68 ------ .../kotlin/resolve/constants/KClassValue.kt | 3 +- .../kotlin/resolve/constants/LongValue.kt | 4 +- .../kotlin/resolve/constants/NullValue.kt | 3 +- .../kotlin/resolve/constants/ShortValue.kt | 4 +- .../kotlin/resolve/constants/StringValue.kt | 6 +- .../deserialization/AnnotationDeserializer.kt | 30 +-- .../deserialization/MemberDeserializer.kt | 16 +- .../serialization/deserialization/context.kt | 4 +- .../DeserializerForDecompilerBase.kt | 4 +- .../ConvertToStringTemplateIntention.kt | 9 +- .../js/resolve/diagnostics/JsCallChecker.kt | 23 +- .../js/translate/utils/AnnotationsUtils.java | 4 +- ...inJavascriptAnnotationAndConstantLoader.kt | 6 +- .../js/KotlinJavascriptSerializerExtension.kt | 9 +- .../expression/ExpressionVisitor.java | 10 +- .../operation/UnaryOperationTranslator.java | 1 - .../reference/CallExpressionTranslator.java | 2 +- .../js/translate/utils/BindingUtils.java | 11 +- 134 files changed, 791 insertions(+), 907 deletions(-) delete mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.java create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.kt delete mode 100644 core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.java create mode 100644 core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.kt delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantFactory.kt create mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValue.kt create mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValueFactory.kt delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java index fbfe87901d8..2f176994840 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/AnnotationCodegen.java @@ -187,9 +187,9 @@ public abstract class AnnotationCodegen { return !type.isMarkedNullable() && classifier instanceof TypeParameterDescriptor && TypeUtils.hasNullableSuperType(type); } - public void generateAnnotationDefaultValue(@NotNull CompileTimeConstant value, @NotNull JetType expectedType) { + public void generateAnnotationDefaultValue(@NotNull ConstantValue value, @NotNull JetType expectedType) { AnnotationVisitor visitor = visitAnnotation(null, false); // Parameters are unimportant - genCompileTimeValue(null, value, expectedType, visitor); + genCompileTimeValue(null, value, visitor); visitor.visitEnd(); } @@ -212,17 +212,16 @@ public abstract class AnnotationCodegen { } private void genAnnotationArguments(AnnotationDescriptor annotationDescriptor, AnnotationVisitor annotationVisitor) { - for (Map.Entry> entry : annotationDescriptor.getAllValueArguments().entrySet()) { + for (Map.Entry> entry : annotationDescriptor.getAllValueArguments().entrySet()) { ValueParameterDescriptor descriptor = entry.getKey(); String name = descriptor.getName().asString(); - genCompileTimeValue(name, entry.getValue(), descriptor.getType(), annotationVisitor); + genCompileTimeValue(name, entry.getValue(), annotationVisitor); } } private void genCompileTimeValue( @Nullable final String name, - @NotNull CompileTimeConstant value, - @NotNull final JetType expectedType, + @NotNull ConstantValue value, @NotNull final AnnotationVisitor annotationVisitor ) { AnnotationArgumentVisitor argumentVisitor = new AnnotationArgumentVisitor() { @@ -281,8 +280,8 @@ public abstract class AnnotationCodegen { @Override public Void visitArrayValue(ArrayValue value, Void data) { AnnotationVisitor visitor = annotationVisitor.visitArray(name); - for (CompileTimeConstant argument : value.getValue()) { - genCompileTimeValue(null, argument, value.getType(), visitor); + for (ConstantValue argument : value.getValue()) { + genCompileTimeValue(null, argument, visitor); } visitor.visitEnd(); return null; @@ -303,14 +302,7 @@ public abstract class AnnotationCodegen { return null; } - @Override - public Void visitNumberTypeValue(IntegerValueTypeConstant value, Void data) { - Object numberType = value.getValue(expectedType); - annotationVisitor.visit(name, numberType); - return null; - } - - private Void visitSimpleValue(CompileTimeConstant value) { + private Void visitSimpleValue(ConstantValue value) { annotationVisitor.visit(name, value.getValue()); return null; } @@ -325,7 +317,7 @@ public abstract class AnnotationCodegen { return visitUnsupportedValue(value); } - private Void visitUnsupportedValue(CompileTimeConstant value) { + private Void visitUnsupportedValue(ConstantValue value) { throw new IllegalStateException("Don't know how to compile annotation value " + value); } }; @@ -349,7 +341,7 @@ public abstract class AnnotationCodegen { private RetentionPolicy getRetentionPolicy(@NotNull Annotated descriptor) { AnnotationDescriptor kotlinAnnotation = descriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.annotation); if (kotlinAnnotation != null) { - for (Map.Entry> argument: kotlinAnnotation.getAllValueArguments().entrySet()) { + for (Map.Entry> argument: kotlinAnnotation.getAllValueArguments().entrySet()) { if ("retention".equals(argument.getKey().getName().asString()) && argument.getValue() instanceof EnumValue) { ClassDescriptor enumEntry = ((EnumValue) argument.getValue()).getValue(); JetType classObjectType = getClassObjectType(enumEntry); @@ -366,9 +358,9 @@ public abstract class AnnotationCodegen { } AnnotationDescriptor retentionAnnotation = descriptor.getAnnotations().findAnnotation(new FqName(Retention.class.getName())); if (retentionAnnotation != null) { - Collection> valueArguments = retentionAnnotation.getAllValueArguments().values(); + Collection> valueArguments = retentionAnnotation.getAllValueArguments().values(); if (!valueArguments.isEmpty()) { - CompileTimeConstant compileTimeConstant = valueArguments.iterator().next(); + ConstantValue compileTimeConstant = valueArguments.iterator().next(); if (compileTimeConstant instanceof EnumValue) { ClassDescriptor enumEntry = ((EnumValue) compileTimeConstant).getValue(); JetType classObjectType = getClassObjectType(enumEntry); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 462dfbf20d7..0596f41ee19 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -49,12 +49,12 @@ import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.impl.ScriptCodeDescriptor; import org.jetbrains.kotlin.diagnostics.DiagnosticUtils; import org.jetbrains.kotlin.diagnostics.Errors; +import org.jetbrains.kotlin.jvm.RuntimeAssertionInfo; import org.jetbrains.kotlin.jvm.bindingContextSlices.BindingContextSlicesPackage; import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.load.java.JvmAbi; import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor; import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor; -import org.jetbrains.kotlin.jvm.RuntimeAssertionInfo; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.renderer.DescriptorRenderer; @@ -68,9 +68,8 @@ import org.jetbrains.kotlin.resolve.calls.model.*; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; -import org.jetbrains.kotlin.resolve.constants.evaluate.EvaluatePackage; import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage; import org.jetbrains.kotlin.resolve.inline.InlineUtil; import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind; @@ -1292,20 +1291,19 @@ public class ExpressionCodegen extends JetVisitor implem @Override public StackValue visitConstantExpression(@NotNull JetConstantExpression expression, StackValue receiver) { - CompileTimeConstant compileTimeValue = getCompileTimeConstant(expression, bindingContext); + ConstantValue compileTimeValue = getCompileTimeConstant(expression, bindingContext); assert compileTimeValue != null; return StackValue.constant(compileTimeValue.getValue(), expressionType(expression)); } @Nullable - public static CompileTimeConstant getCompileTimeConstant(@NotNull JetExpression expression, @NotNull BindingContext bindingContext) { + public static ConstantValue getCompileTimeConstant(@NotNull JetExpression expression, @NotNull BindingContext bindingContext) { CompileTimeConstant compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, bindingContext); - if (compileTimeValue instanceof IntegerValueTypeConstant) { - JetType expectedType = bindingContext.getType(expression); - assert expectedType != null : "Expression is not type checked: " + expression.getText(); - return EvaluatePackage.createCompileTimeConstantWithType((IntegerValueTypeConstant) compileTimeValue, expectedType); + if (compileTimeValue == null) { + return null; } - return compileTimeValue; + JetType expectedType = bindingContext.getType(expression); + return compileTimeValue.toConstantValue(expectedType); } @Override @@ -3012,7 +3010,7 @@ public class ExpressionCodegen extends JetVisitor implem } private boolean isIntZero(JetExpression expr, Type exprType) { - CompileTimeConstant exprValue = getCompileTimeConstant(expr, bindingContext); + ConstantValue exprValue = getCompileTimeConstant(expr, bindingContext); return isIntPrimitive(exprType) && exprValue != null && Integer.valueOf(0).equals(exprValue.getValue()); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java index 64bfd7e0ba8..8c673bf33e4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java @@ -45,7 +45,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage; import org.jetbrains.kotlin.resolve.calls.callResolverUtil.CallResolverUtilPackage; import org.jetbrains.kotlin.resolve.constants.ArrayValue; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.KClassValue; import org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage; import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin; @@ -538,7 +538,7 @@ public class FunctionCodegen { AnnotationDescriptor annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.throws")); if (annotation == null) return ArrayUtil.EMPTY_STRING_ARRAY; - Collection> values = annotation.getAllValueArguments().values(); + Collection> values = annotation.getAllValueArguments().values(); if (values.isEmpty()) return ArrayUtil.EMPTY_STRING_ARRAY; Object value = values.iterator().next(); @@ -547,9 +547,9 @@ public class FunctionCodegen { List strings = ContainerUtil.mapNotNull( arrayValue.getValue(), - new Function, String>() { + new Function, String>() { @Override - public String fun(CompileTimeConstant constant) { + public String fun(ConstantValue constant) { if (constant instanceof KClassValue) { KClassValue classValue = (KClassValue) constant; ClassDescriptor classDescriptor = DescriptorUtils.getClassDescriptorForType(classValue.getValue()); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java index c4a779ffd2d..a8c687c69b1 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java @@ -39,8 +39,7 @@ import org.jetbrains.kotlin.resolve.BindingContextUtils; import org.jetbrains.kotlin.resolve.BindingTrace; import org.jetbrains.kotlin.resolve.TemporaryBindingTrace; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage; import org.jetbrains.kotlin.storage.LockBasedStorageManager; @@ -379,26 +378,28 @@ public abstract class MemberCodegen initializerValue; - if (property.isVar() && initializer != null) { - BindingTrace tempTrace = TemporaryBindingTrace.create(state.getBindingTrace(), "property initializer"); - initializerValue = ConstantExpressionEvaluator.evaluate(initializer, tempTrace, propertyDescriptor.getType()); - } - else { - initializerValue = propertyDescriptor.getCompileTimeInitializer(); - } + ConstantValue initializerValue = computeInitializerValue(property, propertyDescriptor, initializer); // we must write constant values for fields in light classes, // because Java's completion for annotation arguments uses this information if (initializerValue == null) return state.getClassBuilderMode() != ClassBuilderMode.LIGHT_CLASSES; //TODO: OPTIMIZATION: don't initialize static final fields - - Object value = initializerValue instanceof IntegerValueTypeConstant - ? ((IntegerValueTypeConstant) initializerValue).getValue(propertyDescriptor.getType()) - : initializerValue.getValue(); JetType jetType = getPropertyOrDelegateType(property, propertyDescriptor); Type type = typeMapper.mapType(jetType); - return !skipDefaultValue(propertyDescriptor, value, type); + return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type); + } + + @Nullable + private ConstantValue computeInitializerValue( + @NotNull JetProperty property, + @NotNull PropertyDescriptor propertyDescriptor, + @Nullable JetExpression initializer + ) { + if (property.isVar() && initializer != null) { + BindingTrace tempTrace = TemporaryBindingTrace.create(state.getBindingTrace(), "property initializer"); + return ConstantExpressionEvaluator.evaluateToConstantValue(initializer, tempTrace, propertyDescriptor.getType()); + } + return propertyDescriptor.getCompileTimeInitializer(); } @NotNull diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java index 5454cff725c..828ce88f00b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java @@ -32,7 +32,7 @@ import org.jetbrains.kotlin.resolve.DescriptorFactory; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature; import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor; import org.jetbrains.kotlin.types.ErrorUtils; @@ -180,7 +180,7 @@ public class PropertyCodegen { if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { JetExpression defaultValue = p.getDefaultValue(); if (defaultValue != null) { - CompileTimeConstant constant = ExpressionCodegen.getCompileTimeConstant(defaultValue, bindingContext); + ConstantValue constant = ExpressionCodegen.getCompileTimeConstant(defaultValue, bindingContext); assert constant != null : "Default value for annotation parameter should be compile time value: " + defaultValue.getText(); AnnotationCodegen annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(mv, typeMapper); annotationCodegen.generateAnnotationDefaultValue(constant, descriptor.getType()); @@ -311,7 +311,7 @@ public class PropertyCodegen { Object value = null; if (shouldWriteFieldInitializer(propertyDescriptor)) { - CompileTimeConstant initializer = propertyDescriptor.getCompileTimeInitializer(); + ConstantValue initializer = propertyDescriptor.getCompileTimeInitializer(); if (initializer != null) { value = initializer.getValue(); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java index 69855dca290..1e8413c8131 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java @@ -45,7 +45,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage; import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.EnumValue; import org.jetbrains.kotlin.resolve.constants.NullValue; import org.jetbrains.kotlin.resolve.scopes.JetScope; @@ -537,7 +537,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid { WhenByEnumsMapping mapping = new WhenByEnumsMapping(classDescriptor, currentClassName, fieldNumber); - for (CompileTimeConstant constant : SwitchCodegenUtil.getAllConstants(expression, bindingContext)) { + for (ConstantValue constant : SwitchCodegenUtil.getAllConstants(expression, bindingContext)) { if (constant instanceof NullValue) continue; assert constant instanceof EnumValue : "expression in when should be EnumValue"; @@ -554,9 +554,9 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid { SwitchCodegenUtil.checkAllItemsAreConstantsSatisfying( expression, bindingContext, - new Function1() { + new Function1, Boolean>() { @Override - public Boolean invoke(@NotNull CompileTimeConstant constant) { + public Boolean invoke(@NotNull ConstantValue constant) { return constant instanceof EnumValue || constant instanceof NullValue; } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/JetTypeMapper.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/JetTypeMapper.java index db4519777cb..9c947619152 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/JetTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/JetTypeMapper.java @@ -54,7 +54,7 @@ import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage; import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.StringValue; import org.jetbrains.kotlin.resolve.jvm.AsmTypes; import org.jetbrains.kotlin.resolve.jvm.JvmClassName; @@ -721,10 +721,10 @@ public class JetTypeMapper { AnnotationDescriptor platformNameAnnotation = descriptor.getAnnotations().findAnnotation(new FqName("kotlin.platform.platformName")); if (platformNameAnnotation == null) return null; - Map> arguments = platformNameAnnotation.getAllValueArguments(); + Map> arguments = platformNameAnnotation.getAllValueArguments(); if (arguments.isEmpty()) return null; - CompileTimeConstant name = arguments.values().iterator().next(); + ConstantValue name = arguments.values().iterator().next(); if (!(name instanceof StringValue)) return null; return ((StringValue) name).getValue(); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/EnumSwitchCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/EnumSwitchCodegen.java index 3b092f7e141..7eab364b1f8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/EnumSwitchCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/EnumSwitchCodegen.java @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.codegen.when; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.codegen.ExpressionCodegen; import org.jetbrains.kotlin.psi.JetWhenExpression; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.EnumValue; import org.jetbrains.org.objectweb.asm.Label; import org.jetbrains.org.objectweb.asm.Type; @@ -58,7 +58,7 @@ public class EnumSwitchCodegen extends SwitchCodegen { } @Override - protected void processConstant(@NotNull CompileTimeConstant constant, @NotNull Label entryLabel) { + protected void processConstant(@NotNull ConstantValue constant, @NotNull Label entryLabel) { assert constant instanceof EnumValue : "guaranteed by usage contract"; putTransitionOnce(mapping.getIndexByEntry((EnumValue) constant), entryLabel); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/IntegralConstantsSwitchCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/IntegralConstantsSwitchCodegen.java index 3463d689343..8915ac68485 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/IntegralConstantsSwitchCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/IntegralConstantsSwitchCodegen.java @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.codegen.when; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.codegen.ExpressionCodegen; import org.jetbrains.kotlin.psi.JetWhenExpression; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.org.objectweb.asm.Label; public class IntegralConstantsSwitchCodegen extends SwitchCodegen { @@ -32,7 +32,7 @@ public class IntegralConstantsSwitchCodegen extends SwitchCodegen { } @Override - protected void processConstant(@NotNull CompileTimeConstant constant, @NotNull Label entryLabel) { + protected void processConstant(@NotNull ConstantValue constant, @NotNull Label entryLabel) { assert constant.getValue() != null : "constant value should not be null"; int value = (constant.getValue() instanceof Number) ? ((Number) constant.getValue()).intValue() diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/StringSwitchCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/StringSwitchCodegen.java index f3a1058bab3..e689eefdaa7 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/StringSwitchCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/StringSwitchCodegen.java @@ -21,7 +21,7 @@ import com.intellij.openapi.util.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.codegen.ExpressionCodegen; import org.jetbrains.kotlin.psi.JetWhenExpression; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.StringValue; import org.jetbrains.org.objectweb.asm.Label; import org.jetbrains.org.objectweb.asm.Type; @@ -47,7 +47,7 @@ public class StringSwitchCodegen extends SwitchCodegen { @Override protected void processConstant( - @NotNull CompileTimeConstant constant, @NotNull Label entryLabel + @NotNull ConstantValue constant, @NotNull Label entryLabel ) { assert constant instanceof StringValue : "guaranteed by usage contract"; int hashCode = constant.hashCode(); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegen.java index c65505ce41e..026107f6297 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegen.java @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.codegen.FrameMap; import org.jetbrains.kotlin.psi.JetWhenEntry; import org.jetbrains.kotlin.psi.JetWhenExpression; import org.jetbrains.kotlin.resolve.BindingContext; -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.JetType; import org.jetbrains.kotlin.types.TypeUtils; @@ -96,7 +96,7 @@ abstract public class SwitchCodegen { for (JetWhenEntry entry : expression.getEntries()) { Label entryLabel = new Label(); - for (CompileTimeConstant constant : SwitchCodegenUtil.getConstantsFromEntry(entry, bindingContext)) { + for (ConstantValue constant : SwitchCodegenUtil.getConstantsFromEntry(entry, bindingContext)) { if (constant instanceof NullValue) continue; processConstant(constant, entryLabel); } @@ -110,7 +110,7 @@ abstract public class SwitchCodegen { } abstract protected void processConstant( - @NotNull CompileTimeConstant constant, + @NotNull ConstantValue constant, @NotNull Label entryLabel ); @@ -154,7 +154,7 @@ abstract public class SwitchCodegen { private int findNullEntryIndex(@NotNull JetWhenExpression expression) { int entryIndex = 0; for (JetWhenEntry entry : expression.getEntries()) { - for (CompileTimeConstant constant : SwitchCodegenUtil.getConstantsFromEntry(entry, bindingContext)) { + for (ConstantValue constant : SwitchCodegenUtil.getConstantsFromEntry(entry, bindingContext)) { if (constant instanceof NullValue) { return entryIndex; } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegenUtil.java index de79f42bd81..9e2a7db30d8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/when/SwitchCodegenUtil.java @@ -23,7 +23,7 @@ import org.jetbrains.kotlin.codegen.ExpressionCodegen; import org.jetbrains.kotlin.codegen.binding.CodegenBinding; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.IntegerValueConstant; import org.jetbrains.kotlin.resolve.constants.NullValue; import org.jetbrains.kotlin.resolve.constants.StringValue; @@ -36,7 +36,7 @@ public class SwitchCodegenUtil { public static boolean checkAllItemsAreConstantsSatisfying( @NotNull JetWhenExpression expression, @NotNull BindingContext bindingContext, - Function1 predicate + Function1, Boolean> predicate ) { for (JetWhenEntry entry : expression.getEntries()) { for (JetWhenCondition condition : entry.getConditions()) { @@ -49,7 +49,7 @@ public class SwitchCodegenUtil { if (patternExpression == null) return false; - CompileTimeConstant constant = ExpressionCodegen.getCompileTimeConstant(patternExpression, bindingContext); + ConstantValue constant = ExpressionCodegen.getCompileTimeConstant(patternExpression, bindingContext); if (constant == null || !predicate.invoke(constant)) { return false; } @@ -60,11 +60,11 @@ public class SwitchCodegenUtil { } @NotNull - public static Iterable getAllConstants( + public static Iterable> getAllConstants( @NotNull JetWhenExpression expression, @NotNull BindingContext bindingContext ) { - List result = new ArrayList(); + List> result = new ArrayList>(); for (JetWhenEntry entry : expression.getEntries()) { addConstantsFromEntry(result, entry, bindingContext); @@ -74,7 +74,7 @@ public class SwitchCodegenUtil { } private static void addConstantsFromEntry( - @NotNull List result, + @NotNull List> result, @NotNull JetWhenEntry entry, @NotNull BindingContext bindingContext ) { @@ -89,11 +89,11 @@ public class SwitchCodegenUtil { } @NotNull - public static Iterable getConstantsFromEntry( + public static Iterable> getConstantsFromEntry( @NotNull JetWhenEntry entry, @NotNull BindingContext bindingContext ) { - List result = new ArrayList(); + List> result = new ArrayList>(); addConstantsFromEntry(result, entry, bindingContext); return result; } @@ -132,7 +132,7 @@ public class SwitchCodegenUtil { @NotNull JetWhenExpression expression, @NotNull BindingContext bindingContext ) { - for (CompileTimeConstant constant : getAllConstants(expression, bindingContext)) { + for (ConstantValue constant : getAllConstants(expression, bindingContext)) { if (constant != null && !(constant instanceof NullValue)) return true; } @@ -150,10 +150,10 @@ public class SwitchCodegenUtil { return false; } - return checkAllItemsAreConstantsSatisfying(expression, bindingContext, new Function1() { + return checkAllItemsAreConstantsSatisfying(expression, bindingContext, new Function1, Boolean>() { @Override public Boolean invoke( - @NotNull CompileTimeConstant constant + @NotNull ConstantValue constant ) { return constant instanceof IntegerValueConstant; } @@ -170,10 +170,10 @@ public class SwitchCodegenUtil { return false; } - return checkAllItemsAreConstantsSatisfying(expression, bindingContext, new Function1() { + return checkAllItemsAreConstantsSatisfying(expression, bindingContext, new Function1, Boolean>() { @Override public Boolean invoke( - @NotNull CompileTimeConstant constant + @NotNull ConstantValue constant ) { return constant instanceof StringValue || constant instanceof NullValue; } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.java deleted file mode 100644 index 22870fd170e..00000000000 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.load.java.structure.impl; - -import com.intellij.psi.PsiExpression; -import com.intellij.psi.impl.JavaConstantExpressionEvaluator; -import com.intellij.psi.util.PsiUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.PropertyDescriptor; -import org.jetbrains.kotlin.load.java.structure.JavaField; -import org.jetbrains.kotlin.load.java.structure.JavaPropertyInitializerEvaluator; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstantFactory; -import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; -import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage; - -public class JavaPropertyInitializerEvaluatorImpl implements JavaPropertyInitializerEvaluator { - @Nullable - @Override - public CompileTimeConstant getInitializerConstant(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor) { - PsiExpression initializer = ((JavaFieldImpl) field).getInitializer(); - Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false); - if (evaluatedExpression != null) { - CompileTimeConstantFactory factory = new CompileTimeConstantFactory( - new CompileTimeConstant.Parameters.Impl( - ConstantExpressionEvaluator.isPropertyCompileTimeConstant(descriptor), - false, - true - ), DescriptorUtilPackage.getBuiltIns(descriptor)); - return factory.createCompileTimeConstant(evaluatedExpression, descriptor.getType()); - } - return null; - } - - @Override - public boolean isNotNullCompileTimeConstant(@NotNull JavaField field) { - // PsiUtil.isCompileTimeConstant returns false for null-initialized fields, - // see com.intellij.psi.util.IsConstantExpressionVisitor.visitLiteralExpression() - return PsiUtil.isCompileTimeConstant(((JavaFieldImpl) field).getPsi()); - } -} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.kt new file mode 100644 index 00000000000..50229ad1676 --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaPropertyInitializerEvaluatorImpl.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.load.java.structure.impl + +import com.intellij.psi.impl.JavaConstantExpressionEvaluator +import com.intellij.psi.util.PsiUtil +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.load.java.structure.JavaField +import org.jetbrains.kotlin.load.java.structure.JavaPropertyInitializerEvaluator +import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.ConstantValueFactory +import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns + +public class JavaPropertyInitializerEvaluatorImpl : JavaPropertyInitializerEvaluator { + override fun getInitializerConstant(field: JavaField, descriptor: PropertyDescriptor): ConstantValue<*>? { + val initializer = (field as JavaFieldImpl).getInitializer() + val evaluated = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false) ?: return null + val factory = ConstantValueFactory(descriptor.builtIns) + when (evaluated) { + //Note: evaluated expression may be of class that does not match field type in some cases + // tested for Int, left other checks just in case + is Byte, is Short, is Int, is Long -> { + return factory.createIntegerConstantValue((evaluated as Number).toLong(), descriptor.getType()) + } + else -> { + return factory.createConstantValue(evaluated) + } + } + } + + override fun isNotNullCompileTimeConstant(field: JavaField): Boolean { + // PsiUtil.isCompileTimeConstant returns false for null-initialized fields, + // see com.intellij.psi.util.IsConstantExpressionVisitor.visitLiteralExpression() + return PsiUtil.isCompileTimeConstant((field as JavaFieldImpl).getPsi()) + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java index bae071d01f3..0ad61eb1e58 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java @@ -44,6 +44,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; import org.jetbrains.kotlin.resolve.constants.ArrayValue; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil; @@ -210,16 +211,16 @@ public class AnnotationResolver { } @NotNull - public static Map> resolveAnnotationArguments( + public static Map> resolveAnnotationArguments( @NotNull ResolvedCall resolvedCall, @NotNull BindingTrace trace ) { - Map> arguments = new HashMap>(); + Map> arguments = new HashMap>(); for (Map.Entry descriptorToArgument : resolvedCall.getValueArguments().entrySet()) { ValueParameterDescriptor parameterDescriptor = descriptorToArgument.getKey(); ResolvedValueArgument resolvedArgument = descriptorToArgument.getValue(); - CompileTimeConstant value = getAnnotationArgumentValue(trace, parameterDescriptor, resolvedArgument); + ConstantValue value = getAnnotationArgumentValue(trace, parameterDescriptor, resolvedArgument); if (value != null) { arguments.put(parameterDescriptor, value); } @@ -228,32 +229,30 @@ public class AnnotationResolver { } @Nullable - public static CompileTimeConstant getAnnotationArgumentValue( + public static ConstantValue getAnnotationArgumentValue( BindingTrace trace, ValueParameterDescriptor parameterDescriptor, ResolvedValueArgument resolvedArgument ) { JetType varargElementType = parameterDescriptor.getVarargElementType(); boolean argumentsAsVararg = varargElementType != null && !hasSpread(resolvedArgument); - List> constants = resolveValueArguments( - resolvedArgument, argumentsAsVararg ? varargElementType : parameterDescriptor.getType(), trace); + final JetType constantType = argumentsAsVararg ? varargElementType : parameterDescriptor.getType(); + List> compileTimeConstants = resolveValueArguments(resolvedArgument, constantType, trace); + List> constants = KotlinPackage.map(compileTimeConstants, new Function1, ConstantValue>() { + @Override + public ConstantValue invoke(CompileTimeConstant constant) { + return constant.toConstantValue(constantType); + } + }); if (argumentsAsVararg) { + if (parameterDescriptor.declaresDefaultValue() && compileTimeConstants.isEmpty()) return null; - boolean usesVariableAsConstant = KotlinPackage.any(constants, new Function1, Boolean>() { - @Override - public Boolean invoke(CompileTimeConstant constant) { - return constant.usesVariableAsConstant(); - } - }); - - if (parameterDescriptor.declaresDefaultValue() && constants.isEmpty()) return null; - - return new ArrayValue(constants, parameterDescriptor.getType(), usesVariableAsConstant); + return new ArrayValue(constants, parameterDescriptor.getType()); } else { // we should actually get only one element, but just in case of getting many, we take the last one - return !constants.isEmpty() ? KotlinPackage.last(constants) : null; + return KotlinPackage.lastOrNull(constants); } } @@ -280,7 +279,7 @@ public class AnnotationResolver { } CompileTimeConstant constant = ConstantExpressionEvaluator.getConstant(argumentExpression, trace.getBindingContext()); - if (constant != null && constant.canBeUsedInAnnotations()) { + if (constant != null && constant.getCanBeUsedInAnnotations()) { return; } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java index f08e868dff1..475dde783db 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/BodyResolver.java @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil; import org.jetbrains.kotlin.resolve.scopes.*; import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue; @@ -735,8 +734,7 @@ public class BodyResolver { JetScope propertyDeclarationInnerScope = JetScopeUtils.getPropertyDeclarationInnerScopeForInitializer( propertyDescriptor, scope, propertyDescriptor.getTypeParameters(), NO_RECEIVER_PARAMETER, trace); JetType expectedTypeForInitializer = property.getTypeReference() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE; - CompileTimeConstant compileTimeInitializer = propertyDescriptor.getCompileTimeInitializer(); - if (compileTimeInitializer == null) { + if (propertyDescriptor.getCompileTimeInitializer() == null) { expressionTypingServices.getType(propertyDeclarationInnerScope, initializer, expectedTypeForInitializer, outerDataFlowInfo, trace); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java index b660ef8ffbb..bd8c2912b9f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java @@ -31,6 +31,8 @@ import org.jetbrains.kotlin.psi.JetTypeReference; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.constants.BooleanValue; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; +import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.TypeProjection; @@ -113,7 +115,7 @@ public class CompileTimeConstantUtils { annotatedDescriptor.getAnnotations().findAnnotation(new FqName("kotlin.jvm.internal.Intrinsic")); if (intrinsicAnnotation == null) return null; - Collection> values = intrinsicAnnotation.getAllValueArguments().values(); + Collection> values = intrinsicAnnotation.getAllValueArguments().values(); if (values.isEmpty()) return null; Object value = values.iterator().next().getValue(); @@ -132,9 +134,13 @@ public class CompileTimeConstantUtils { if (expression == null) return false; CompileTimeConstant compileTimeConstant = ConstantExpressionEvaluator.evaluate(expression, trace, KotlinBuiltIns.getInstance().getBooleanType()); - if (!(compileTimeConstant instanceof BooleanValue) || compileTimeConstant.usesVariableAsConstant()) return false; + if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant()) return false; - Boolean value = ((BooleanValue) compileTimeConstant).getValue(); + ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue(); + + if (!(constantValue instanceof BooleanValue)) return false; + + Boolean value = ((BooleanValue) constantValue).getValue(); return expectedValue == null || expectedValue.equals(value); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 6dbac5fab2d..ca3dc003652 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -37,10 +37,8 @@ import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; -import org.jetbrains.kotlin.resolve.constants.evaluate.EvaluatePackage; import org.jetbrains.kotlin.resolve.dataClassUtils.DataClassUtilsPackage; import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil; import org.jetbrains.kotlin.resolve.scopes.JetScope; @@ -868,17 +866,13 @@ public class DescriptorResolver { if (!variable.hasInitializer()) return; variableDescriptor.setCompileTimeInitializer( - storageManager.createRecursionTolerantNullableLazyValue(new Function0>() { + storageManager.createRecursionTolerantNullableLazyValue(new Function0>() { @Nullable @Override - public CompileTimeConstant invoke() { + public ConstantValue invoke() { JetExpression initializer = variable.getInitializer(); JetType initializerType = expressionTypingServices.safeGetType(scope, initializer, variableType, dataFlowInfo, trace); - CompileTimeConstant constant = ConstantExpressionEvaluator.evaluate(initializer, trace, initializerType); - if (constant instanceof IntegerValueTypeConstant) { - return EvaluatePackage.createCompileTimeConstantWithType((IntegerValueTypeConstant) constant, initializerType); - } - return constant; + return ConstantExpressionEvaluator.evaluateToConstantValue(initializer, trace, initializerType); } }, null) ); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ModifiersChecker.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ModifiersChecker.java index 1a4f6c9e987..1fe0859d64e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ModifiersChecker.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ModifiersChecker.java @@ -26,13 +26,13 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.kotlin.diagnostics.*; +import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.lexer.JetModifierKeywordToken; import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.StringValue; import java.util.*; @@ -302,9 +302,9 @@ public class ModifiersChecker { } String value = null; - Collection> values = annotation.getAllValueArguments().values(); + Collection> values = annotation.getAllValueArguments().values(); if (!values.isEmpty()) { - CompileTimeConstant name = values.iterator().next(); + ConstantValue name = values.iterator().next(); if (name instanceof StringValue) { value = ((StringValue) name).getValue(); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java index a598cb915d3..a2fc4c7b3c5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java @@ -39,7 +39,6 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.IntegerValueConstant; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.scopes.receivers.*; import org.jetbrains.kotlin.types.ErrorUtils; @@ -369,7 +368,7 @@ public class CallExpressionResolver { } CompileTimeConstant value = ConstantExpressionEvaluator.evaluate(expression, context.trace, context.expectedType); - if (value instanceof IntegerValueConstant && ((IntegerValueConstant) value).isPure()) { + if (value != null && value.getIsPure()) { return ExpressionTypingUtils.createCompileTimeConstantTypeInfo(value, expression, context); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt index ac78cd3b963..8ba9e0bc3a7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.resolve.calls.util import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant import org.jetbrains.kotlin.resolve.descriptorUtil.classObjectType import org.jetbrains.kotlin.resolve.descriptorUtil.getClassObjectReferenceTarget import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassObjectType @@ -59,7 +58,7 @@ public class FakeCallableDescriptorForObject( override fun getOriginal(): CallableDescriptor = this - override fun getCompileTimeInitializer(): CompileTimeConstant? = null + override fun getCompileTimeInitializer() = null override fun getSource(): SourceElement = classDescriptor.getSource() } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantChecker.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantChecker.java index 907beccac42..5699547eae1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantChecker.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantChecker.java @@ -51,7 +51,7 @@ public class CompileTimeConstantChecker { // return true if there is an error public boolean checkConstantExpressionType( - @Nullable CompileTimeConstant compileTimeConstant, + @Nullable ConstantValue compileTimeConstant, @NotNull JetConstantExpression expression, @NotNull JetType expectedType ) { @@ -76,7 +76,7 @@ public class CompileTimeConstantChecker { } private boolean checkIntegerValue( - @Nullable CompileTimeConstant value, + @Nullable ConstantValue value, @NotNull JetType expectedType, @NotNull JetConstantExpression expression ) { @@ -98,7 +98,7 @@ public class CompileTimeConstantChecker { } private boolean checkFloatValue( - @Nullable CompileTimeConstant value, + @Nullable ConstantValue value, @NotNull JetType expectedType, @NotNull JetConstantExpression expression ) { @@ -125,7 +125,7 @@ public class CompileTimeConstantChecker { return false; } - private boolean checkCharValue(CompileTimeConstant constant, JetType expectedType, JetConstantExpression expression) { + private boolean checkCharValue(ConstantValue constant, JetType expectedType, JetConstantExpression expression) { if (!noExpectedTypeOrError(expectedType) && !JetTypeChecker.DEFAULT.isSubtypeOf(builtIns.getCharType(), expectedType)) { return reportError(CONSTANT_EXPECTED_TYPE_MISMATCH.on(expression, "character", expectedType)); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index 05f234d0810..0c51f3846b5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -41,31 +41,26 @@ import java.math.BigInteger import kotlin.platform.platformStatic public class ConstantExpressionEvaluator private constructor(val trace: BindingTrace) : JetVisitor, JetType>() { - - private val builtIns = KotlinBuiltIns.getInstance() + private val factory = ConstantValueFactory(KotlinBuiltIns.getInstance()) companion object { platformStatic public fun evaluate(expression: JetExpression, trace: BindingTrace, expectedType: JetType? = TypeUtils.NO_EXPECTED_TYPE): CompileTimeConstant<*>? { val evaluator = ConstantExpressionEvaluator(trace) - val constant = evaluator.evaluate(expression, expectedType) - return if (constant !is ErrorValue) constant else null + val constant = evaluator.evaluate(expression, expectedType) ?: return null + return if (!constant.isError) constant else null } - platformStatic public fun isPropertyCompileTimeConstant(descriptor: VariableDescriptor): Boolean { - if (descriptor.isVar()) { - return false - } - if (DescriptorUtils.isObject(descriptor.getContainingDeclaration()) || - DescriptorUtils.isStaticDeclaration(descriptor)) { - val returnType = descriptor.getType() - return KotlinBuiltIns.isPrimitiveType(returnType) || KotlinBuiltIns.isString(returnType) - } - return false + platformStatic public fun evaluateToConstantValue( + expression: JetExpression, + trace: BindingTrace, + expectedType: JetType + ): ConstantValue<*>? { + return evaluate(expression, trace, expectedType)?.toConstantValue(expectedType) } platformStatic public fun getConstant(expression: JetExpression, bindingContext: BindingContext): CompileTimeConstant<*>? { - val constant = getPossiblyErrorConstant(expression, bindingContext) - return if (constant !is ErrorValue) constant else null + val constant = getPossiblyErrorConstant(expression, bindingContext) ?: return null + return if (!constant.isError) constant else null } platformStatic private fun getPossiblyErrorConstant(expression: JetExpression, bindingContext: BindingContext): CompileTimeConstant<*>? { @@ -87,30 +82,38 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT return null } - private val stringExpressionEvaluator = object : JetVisitor() { - private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false), builtIns) + private val stringExpressionEvaluator = object : JetVisitor, Nothing>() { + private fun createStringConstant(compileTimeConstant: CompileTimeConstant<*>): TypedCompileTimeConstant? { + val constantValue = compileTimeConstant.toConstantValue(TypeUtils.NO_EXPECTED_TYPE) + return when (constantValue) { + is ErrorValue, is EnumValue -> return null + is NullValue -> factory.createStringValue("null") + else -> factory.createStringValue(constantValue.value.toString()) + }.wrap(compileTimeConstant.parameters) + } - fun evaluate(entry: JetStringTemplateEntry): StringValue? { + fun evaluate(entry: JetStringTemplateEntry): TypedCompileTimeConstant? { return entry.accept(this, null) } - override fun visitStringTemplateEntryWithExpression(entry: JetStringTemplateEntryWithExpression, data: Nothing?): StringValue? { - val expression = entry.getExpression() - if (expression == null) return null + override fun visitStringTemplateEntryWithExpression(entry: JetStringTemplateEntryWithExpression, data: Nothing?): TypedCompileTimeConstant? { + val expression = entry.getExpression() ?: return null - return createStringConstant(this@ConstantExpressionEvaluator.evaluate(expression, KotlinBuiltIns.getInstance().getStringType())) + return this@ConstantExpressionEvaluator.evaluate(expression, KotlinBuiltIns.getInstance().getStringType())?.let { + createStringConstant(it) + } } - override fun visitLiteralStringTemplateEntry(entry: JetLiteralStringTemplateEntry, data: Nothing?) = factory.createStringValue(entry.getText()) + override fun visitLiteralStringTemplateEntry(entry: JetLiteralStringTemplateEntry, data: Nothing?) = factory.createStringValue(entry.getText()).wrap() - override fun visitEscapeStringTemplateEntry(entry: JetEscapeStringTemplateEntry, data: Nothing?) = factory.createStringValue(entry.getUnescapedValue()) + override fun visitEscapeStringTemplateEntry(entry: JetEscapeStringTemplateEntry, data: Nothing?) = factory.createStringValue(entry.getUnescapedValue()).wrap() } override fun visitConstantExpression(expression: JetConstantExpression, expectedType: JetType?): CompileTimeConstant<*>? { val text = expression.getText() ?: return null val nodeElementType = expression.getNode().getElementType() - if (nodeElementType == JetNodeTypes.NULL) return NullValue(builtIns) + if (nodeElementType == JetNodeTypes.NULL) return factory.createNullValue().wrap() val result: Any? = when (nodeElementType) { JetNodeTypes.INTEGER_CONSTANT -> parseLong(text) @@ -121,7 +124,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT } ?: return null fun isLongWithSuffix() = nodeElementType == JetNodeTypes.INTEGER_CONSTANT && hasLongSuffix(text) - return createConstant(result, expectedType, CompileTimeConstant.Parameters.Impl(true, !isLongWithSuffix(), false)) + return createConstant(result, expectedType, CompileTimeConstant.Parameters(true, !isLongWithSuffix(), false)) } override fun visitParenthesizedExpression(expression: JetParenthesizedExpression, expectedType: JetType?): CompileTimeConstant<*>? { @@ -152,17 +155,17 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT break } else { - if (!constant.canBeUsedInAnnotations()) canBeUsedInAnnotation = false - if (constant.usesVariableAsConstant()) usesVariableAsConstant = true - sb.append(constant.value) + if (!constant.canBeUsedInAnnotations) canBeUsedInAnnotation = false + if (constant.usesVariableAsConstant) usesVariableAsConstant = true + sb.append(constant.constantValue.value) } } return if (!interupted) createConstant( sb.toString(), expectedType, - CompileTimeConstant.Parameters.Impl( - isPure = true, + CompileTimeConstant.Parameters( + isPure = false, canBeUsedInAnnotation = canBeUsedInAnnotation, usesVariableAsConstant = usesVariableAsConstant ) @@ -174,8 +177,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT evaluate(expression.getLeft(), expectedType) override fun visitBinaryExpression(expression: JetBinaryExpression, expectedType: JetType?): CompileTimeConstant<*>? { - val leftExpression = expression.getLeft() - if (leftExpression == null) return null + val leftExpression = expression.getLeft() ?: return null val operationToken = expression.getOperationToken() if (OperatorConventions.BOOLEAN_OPERATIONS.containsKey(operationToken)) { @@ -183,14 +185,12 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT val leftConstant = evaluate(leftExpression, booleanType) if (leftConstant == null) return null - val rightExpression = expression.getRight() - if (rightExpression == null) return null + val rightExpression = expression.getRight() ?: return null - val rightConstant = evaluate(rightExpression, booleanType) - if (rightConstant == null) return null + val rightConstant = evaluate(rightExpression, booleanType) ?: return null - val leftValue = leftConstant.value - val rightValue = rightConstant.value + val leftValue = leftConstant.getValue(booleanType) + val rightValue = rightConstant.getValue(booleanType) if (leftValue !is Boolean || rightValue !is Boolean) return null val result = when (operationToken) { @@ -198,8 +198,14 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT JetTokens.OROR -> leftValue || rightValue else -> throw IllegalArgumentException("Unknown boolean operation token ${operationToken}") } - val usesVariableAsConstant = leftConstant.usesVariableAsConstant() || rightConstant.usesVariableAsConstant() - return createConstant(result, expectedType, CompileTimeConstant.Parameters.Impl(true, true, usesVariableAsConstant)) + return createConstant( + result, expectedType, + CompileTimeConstant.Parameters( + canBeUsedInAnnotation = true, + isPure = false, + usesVariableAsConstant = leftConstant.usesVariableAsConstant || rightConstant.usesVariableAsConstant + ) + ) } else { return evaluateCall(expression.getOperationReference(), leftExpression, expectedType) @@ -226,7 +232,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT return createConstant( result, expectedType, - CompileTimeConstant.Parameters.Impl( + CompileTimeConstant.Parameters( canBeUsedInAnnotation, !isNumberConversionMethod && isArgumentPure, usesVariableAsConstant) @@ -240,8 +246,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT if (isDivisionByZero(resultingDescriptorName.asString(), argumentForParameter.value)) { val parentExpression: JetExpression = PsiTreeUtil.getParentOfType(receiverExpression, javaClass())!! trace.report(Errors.DIVISION_BY_ZERO.on(parentExpression)) - //TODO_R: - return ErrorValue.create("Division by zero") + return factory.createErrorValue("Division by zero").wrap() } val result = evaluateBinaryAndCheck(argumentForReceiver, argumentForParameter, resultingDescriptorName.asString(), callExpression) @@ -250,11 +255,10 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT val areArgumentsPure = isPureConstant(argumentForReceiver.expression) && isPureConstant(argumentForParameter.expression) val canBeUsedInAnnotation = canBeUsedInAnnotation(argumentForReceiver.expression) && canBeUsedInAnnotation(argumentForParameter.expression) val usesVariableAsConstant = usesVariableAsConstant(argumentForReceiver.expression) || usesVariableAsConstant(argumentForParameter.expression) - val parameters = CompileTimeConstant.Parameters.Impl(canBeUsedInAnnotation, areArgumentsPure, usesVariableAsConstant) - val factory = CompileTimeConstantFactory(parameters, builtIns) + val parameters = CompileTimeConstant.Parameters(canBeUsedInAnnotation, areArgumentsPure, usesVariableAsConstant) return when (resultingDescriptorName) { - OperatorConventions.COMPARE_TO -> createCompileTimeConstantForCompareTo(result, callExpression, factory) - OperatorConventions.EQUALS -> createCompileTimeConstantForEquals(result, callExpression, factory) + OperatorConventions.COMPARE_TO -> createCompileTimeConstantForCompareTo(result, callExpression, factory)?.wrap(parameters) + OperatorConventions.EQUALS -> createCompileTimeConstantForEquals(result, callExpression, factory)?.wrap(parameters) else -> { createConstant(result, expectedType, parameters) } @@ -264,17 +268,11 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT return null } - private fun usesVariableAsConstant(expression: JetExpression) = getConstant(expression, trace.getBindingContext())?.usesVariableAsConstant() ?: false + private fun usesVariableAsConstant(expression: JetExpression) = getConstant(expression, trace.getBindingContext())?.usesVariableAsConstant ?: false - private fun canBeUsedInAnnotation(expression: JetExpression) = getConstant(expression, trace.getBindingContext())?.canBeUsedInAnnotations() ?: false + private fun canBeUsedInAnnotation(expression: JetExpression) = getConstant(expression, trace.getBindingContext())?.canBeUsedInAnnotations ?: false - private fun isPureConstant(expression: JetExpression): Boolean { - val compileTimeConstant = getConstant(expression, trace.getBindingContext()) - if (compileTimeConstant is IntegerValueConstant) { - return compileTimeConstant.isPure() - } - return false - } + private fun isPureConstant(expression: JetExpression) = getConstant(expression, trace.getBindingContext())?.isPure ?: false private fun evaluateUnaryAndCheck(receiver: OperationArgument, name: String, callExpression: JetExpression): Any? { val functions = unaryOperations[UnaryOperationKey(receiver.ctcType, name)] @@ -342,25 +340,19 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT override fun visitSimpleNameExpression(expression: JetSimpleNameExpression, expectedType: JetType?): CompileTimeConstant<*>? { val enumDescriptor = trace.getBindingContext().get(BindingContext.REFERENCE_TARGET, expression); if (enumDescriptor != null && DescriptorUtils.isEnumEntry(enumDescriptor)) { - return EnumValue(enumDescriptor as ClassDescriptor) + return factory.createEnumValue(enumDescriptor as ClassDescriptor).wrap() } val resolvedCall = expression.getResolvedCall(trace.getBindingContext()) if (resolvedCall != null) { val callableDescriptor = resolvedCall.getResultingDescriptor() if (callableDescriptor is VariableDescriptor) { - val compileTimeConstant = callableDescriptor.getCompileTimeInitializer() - if (compileTimeConstant == null) return null + val variableInitializer = callableDescriptor.getCompileTimeInitializer() ?: return null - val value: Any? = - if (compileTimeConstant is IntegerValueTypeConstant) - compileTimeConstant.getValue(expectedType ?: TypeUtils.NO_EXPECTED_TYPE) - else - compileTimeConstant.value return createConstant( - value, + variableInitializer.value, expectedType, - CompileTimeConstant.Parameters.Impl( + CompileTimeConstant.Parameters( canBeUsedInAnnotation = isPropertyCompileTimeConstant(callableDescriptor), isPure = false, usesVariableAsConstant = true @@ -371,6 +363,18 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT return null } + private fun isPropertyCompileTimeConstant(descriptor: VariableDescriptor): Boolean { + if (descriptor.isVar()) { + return false + } + if (DescriptorUtils.isObject(descriptor.getContainingDeclaration()) || + DescriptorUtils.isStaticDeclaration(descriptor)) { + val returnType = descriptor.getType() + return KotlinBuiltIns.isPrimitiveType(returnType) || KotlinBuiltIns.isString(returnType) + } + return false + } + override fun visitQualifiedExpression(expression: JetQualifiedExpression, expectedType: JetType?): CompileTimeConstant<*>? { val selectorExpression = expression.getSelectorExpression() // 1.toInt(); 1.plus(1); @@ -409,7 +413,10 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT val arguments = call.getValueArguments().values().flatMap { resolveArguments(it.getArguments(), varargType) } - return ArrayValue(arguments, resultingDescriptor.getReturnType()!!, arguments.any() { it.usesVariableAsConstant() }) + return ArrayValue(arguments.map { it.toConstantValue(varargType) }, resultingDescriptor.getReturnType()!!). + wrap( + usesVariableAsConstant = arguments.any { it.usesVariableAsConstant } + ) } // Ann() @@ -420,7 +427,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT classDescriptor.getDefaultType(), AnnotationResolver.resolveAnnotationArguments(call, trace) ) - return AnnotationValue(descriptor) + return AnnotationValue(descriptor).wrap() } } @@ -430,7 +437,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT override fun visitClassLiteralExpression(expression: JetClassLiteralExpression, expectedType: JetType?): CompileTimeConstant<*>? { val jetType = trace.getType(expression)!! if (jetType.isError()) return null - return KClassValue(jetType) + return KClassValue(jetType).wrap() } private fun resolveArguments(valueArguments: List, expectedType: JetType): List> { @@ -476,32 +483,54 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT } private fun createOperationArgument(expression: JetExpression, expressionType: JetType, compileTimeType: CompileTimeType<*>): OperationArgument? { - val evaluatedConstant = evaluate(expression, trace, expressionType) - if (evaluatedConstant == null) return null - - if (evaluatedConstant is IntegerValueTypeConstant) { - val evaluationResultWithNewType = evaluatedConstant.getValue(expressionType) - return OperationArgument(evaluationResultWithNewType, compileTimeType, expression) - } - - val evaluationResult = evaluatedConstant.value - if (evaluationResult == null) return null - + val compileTimeConstant = evaluate(expression, trace, expressionType) ?: return null + val evaluationResult = compileTimeConstant.getValue(expressionType) ?: return null return OperationArgument(evaluationResult, compileTimeType, expression) } - fun createConstant( + private fun createConstant( value: Any?, expectedType: JetType?, parameters: CompileTimeConstant.Parameters ): CompileTimeConstant<*>? { - return CompileTimeConstantFactory(parameters, builtIns).createCompileTimeConstant(value, if (parameters.isPure) expectedType ?: TypeUtils.NO_EXPECTED_TYPE else null) + return if (parameters.isPure) { + return createCompileTimeConstant(value, parameters, expectedType ?: TypeUtils.NO_EXPECTED_TYPE) + } + else { + factory.createConstantValue(value)?.wrap(parameters) + } + } + + private fun createCompileTimeConstant( + value: Any?, + parameters: CompileTimeConstant.Parameters, + expectedType: JetType + ): CompileTimeConstant<*>? { + return when (value) { + is Byte, is Short, is Int, is Long -> createIntegerCompileTimeConstant((value as Number).toLong(), parameters, expectedType) + else -> factory.createConstantValue(value)?.wrap(parameters) + } + } + + private fun createIntegerCompileTimeConstant( + value: Long, + parameters: CompileTimeConstant.Parameters, + expectedType: JetType + ): CompileTimeConstant<*>? { + if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) { + return IntegerValueTypeConstant(value, parameters) + } + val integerValue = factory.createIntegerConstantValue(value, expectedType) + if (integerValue != null) { + return integerValue.wrap(parameters) + } + return when (value) { + value.toInt().toLong() -> factory.createIntValue(value.toInt()) + else -> factory.createLongValue(value) + }.wrap(parameters) } } -public fun IntegerValueTypeConstant.createCompileTimeConstantWithType(expectedType: JetType): CompileTimeConstant<*>? - = 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') public fun parseLong(text: String): Long? { @@ -557,7 +586,7 @@ private fun parseBoolean(text: String): Boolean { } -private fun createCompileTimeConstantForEquals(result: Any?, operationReference: JetExpression, factory: CompileTimeConstantFactory): CompileTimeConstant<*>? { +private fun createCompileTimeConstantForEquals(result: Any?, operationReference: JetExpression, factory: ConstantValueFactory): ConstantValue<*>? { if (result is Boolean) { assert(operationReference is JetSimpleNameExpression, "This method should be called only for equals operations") val operationToken = (operationReference as JetSimpleNameExpression).getReferencedNameElementType() @@ -575,7 +604,7 @@ private fun createCompileTimeConstantForEquals(result: Any?, operationReference: return null } -private fun createCompileTimeConstantForCompareTo(result: Any?, operationReference: JetExpression, factory: CompileTimeConstantFactory): CompileTimeConstant<*>? { +private fun createCompileTimeConstantForCompareTo(result: Any?, operationReference: JetExpression, factory: ConstantValueFactory): ConstantValue<*>? { if (result is Int) { assert(operationReference is JetSimpleNameExpression, "This method should be called only for compareTo operations") val operationToken = (operationReference as JetSimpleNameExpression).getReferencedNameElementType() @@ -594,19 +623,6 @@ private fun createCompileTimeConstantForCompareTo(result: Any?, operationReferen return null } -private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? { - return when (value) { - is IntegerValueTypeConstant -> CompileTimeConstantFactory(value.parameters, KotlinBuiltIns.getInstance()).createStringValue(value.getValue(TypeUtils.NO_EXPECTED_TYPE).toString()) - is StringValue -> value - is IntValue, is ByteValue, is ShortValue, is LongValue, - is CharValue, - is DoubleValue, is FloatValue, - is BooleanValue, - is NullValue -> CompileTimeConstantFactory(value.parameters, KotlinBuiltIns.getInstance()).createStringValue("${value.value}") - else -> null - } -} - fun isIntegerType(value: Any?) = value is Byte || value is Short || value is Int || value is Long private fun getReceiverExpressionType(resolvedCall: ResolvedCall<*>): JetType? { @@ -668,4 +684,3 @@ private fun unaryOperation( private data class BinaryOperationKey(val f: CompileTimeType, val s: CompileTimeType, val functionName: String) private data class UnaryOperationKey(val f: CompileTimeType, val functionName: String) - diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/diagnostics/DiagnosticsWithSuppression.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/diagnostics/DiagnosticsWithSuppression.java index 85a01b8899d..7482867dd13 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/diagnostics/DiagnosticsWithSuppression.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/diagnostics/DiagnosticsWithSuppression.java @@ -35,7 +35,7 @@ import org.jetbrains.kotlin.diagnostics.Severity; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.constants.ArrayValue; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.StringValue; import org.jetbrains.kotlin.util.ExtensionProvider; @@ -214,9 +214,9 @@ public class DiagnosticsWithSuppression implements Diagnostics { if (!KotlinBuiltIns.isSuppressAnnotation(annotationDescriptor)) continue; // We only add strings and skip other values to facilitate recovery in presence of erroneous code - for (CompileTimeConstant arrayValue : annotationDescriptor.getAllValueArguments().values()) { + for (ConstantValue arrayValue : annotationDescriptor.getAllValueArguments().values()) { if ((arrayValue instanceof ArrayValue)) { - for (CompileTimeConstant value : ((ArrayValue) arrayValue).getValue()) { + for (ConstantValue value : ((ArrayValue) arrayValue).getValue()) { if (value instanceof StringValue) { builder.add(String.valueOf(((StringValue) value).getValue()).toLowerCase()); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/inline/InlineUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/inline/InlineUtil.java index 983deaf5047..afcd4037800 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/inline/InlineUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/inline/InlineUtil.java @@ -32,7 +32,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ArgumentMapping; import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.constants.ArrayValue; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.EnumValue; import static kotlin.KotlinPackage.firstOrNull; @@ -53,7 +53,7 @@ public class InlineUtil { if (annotation == null) { return InlineStrategy.NOT_INLINE; } - CompileTimeConstant argument = firstOrNull(annotation.getAllValueArguments().values()); + ConstantValue argument = firstOrNull(annotation.getAllValueArguments().values()); if (argument == null) { return InlineStrategy.AS_FUNCTION; } @@ -72,9 +72,9 @@ public class InlineUtil { private static boolean hasInlineOption(@NotNull ValueParameterDescriptor descriptor, @NotNull InlineOption option) { AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.inlineOptions); if (annotation != null) { - CompileTimeConstant argument = firstOrNull(annotation.getAllValueArguments().values()); + ConstantValue argument = firstOrNull(annotation.getAllValueArguments().values()); if (argument instanceof ArrayValue) { - for (CompileTimeConstant value : ((ArrayValue) argument).getValue()) { + for (ConstantValue value : ((ArrayValue) argument).getValue()) { if (value instanceof EnumValue && ((EnumValue) value).getValue().getName().asString().equals(option.name())) { return true; } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt index 3cb342e5418..48868f41499 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.resolve.AnnotationResolver import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant +import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil import org.jetbrains.kotlin.resolve.lazy.LazyEntity import org.jetbrains.kotlin.resolve.scopes.JetScope @@ -109,7 +109,7 @@ public class LazyAnnotationDescriptor( override fun getAllValueArguments() = valueArguments() - private fun computeValueArguments(): Map> { + private fun computeValueArguments(): Map> { val resolutionResults = c.annotationResolver.resolveAnnotationCall(annotationEntry, c.scope, c.trace) AnnotationResolver.checkAnnotationType(annotationEntry, c.trace, resolutionResults) @@ -121,7 +121,7 @@ public class LazyAnnotationDescriptor( if (resolvedArgument == null) null else AnnotationResolver.getAnnotationArgumentValue(c.trace, valueParameter, resolvedArgument) } - .filterValues { it != null } as Map> + .filterValues { it != null } as Map> } override fun forceResolveAllContents() { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index 4be38adea30..06383a6cec7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -55,9 +55,7 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind; import org.jetbrains.kotlin.resolve.calls.tasks.ResolutionCandidate; import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstantChecker; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.*; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.scopes.JetScope; import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl; @@ -121,19 +119,20 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { @Override public JetTypeInfo visitConstantExpression(@NotNull JetConstantExpression expression, ExpressionTypingContext context) { - CompileTimeConstant value = ConstantExpressionEvaluator.evaluate(expression, context.trace, context.expectedType); + CompileTimeConstant compileTimeConstant = ConstantExpressionEvaluator.evaluate(expression, context.trace, context.expectedType); - if (!(value instanceof IntegerValueTypeConstant)) { + if (!(compileTimeConstant instanceof IntegerValueTypeConstant)) { CompileTimeConstantChecker compileTimeConstantChecker = context.getCompileTimeConstantChecker(); - boolean hasError = compileTimeConstantChecker.checkConstantExpressionType(value, expression, context.expectedType); + ConstantValue constantValue = compileTimeConstant != null ? ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue() : null; + boolean hasError = compileTimeConstantChecker.checkConstantExpressionType(constantValue, expression, context.expectedType); if (hasError) { IElementType elementType = expression.getNode().getElementType(); return TypeInfoFactoryPackage.createTypeInfo(getDefaultType(elementType), context); } } - assert value != null : "CompileTimeConstant should be evaluated for constant expression or an error should be recorded " + expression.getText(); - return createCompileTimeConstantTypeInfo(value, expression, context); + assert compileTimeConstant != null : "CompileTimeConstant should be evaluated for constant expression or an error should be recorded " + expression.getText(); + return createCompileTimeConstantTypeInfo(compileTimeConstant, expression, context); } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowUtils.java index f297b7e2d6d..c189ca513b3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowUtils.java @@ -31,11 +31,9 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory; import org.jetbrains.kotlin.resolve.calls.smartcasts.SmartCastUtils; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstantChecker; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; -import org.jetbrains.kotlin.resolve.constants.evaluate.EvaluatePackage; import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.TypeUtils; import org.jetbrains.kotlin.types.TypesPackage; @@ -176,12 +174,9 @@ public class DataFlowUtils { } if (expression instanceof JetConstantExpression) { - CompileTimeConstant value = ConstantExpressionEvaluator.evaluate(expression, c.trace, c.expectedType); - if (value instanceof IntegerValueTypeConstant) { - value = EvaluatePackage.createCompileTimeConstantWithType((IntegerValueTypeConstant) value, c.expectedType); - } + ConstantValue constantValue = ConstantExpressionEvaluator.evaluateToConstantValue(expression, c.trace, c.expectedType); boolean error = new CompileTimeConstantChecker(c.trace, true) - .checkConstantExpressionType(value, (JetConstantExpression) expression, c.expectedType); + .checkConstantExpressionType(constantValue, (JetConstantExpression) expression, c.expectedType); if (hasError != null) hasError.set(error); return expressionType; } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingUtils.java index 0e38b188d83..58e9ba815af 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingUtils.java @@ -22,7 +22,6 @@ import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.diagnostics.Diagnostic; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory; @@ -33,6 +32,7 @@ import org.jetbrains.kotlin.resolve.*; import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; +import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant; import org.jetbrains.kotlin.resolve.scopes.WritableScope; import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl; import org.jetbrains.kotlin.resolve.scopes.receivers.ClassReceiver; @@ -248,10 +248,19 @@ public class ExpressionTypingUtils { @NotNull JetExpression expression, @NotNull ExpressionTypingContext context ) { - JetType expressionType = value.getType(); - if (value instanceof IntegerValueTypeConstant && context.contextDependency == INDEPENDENT) { - expressionType = ((IntegerValueTypeConstant) value).getType(context.expectedType); - ArgumentTypeResolver.updateNumberType(expressionType, expression, context); + JetType expressionType; + if (value instanceof IntegerValueTypeConstant) { + IntegerValueTypeConstant integerValueTypeConstant = (IntegerValueTypeConstant) value; + if (context.contextDependency == INDEPENDENT) { + expressionType = integerValueTypeConstant.getType(context.expectedType); + ArgumentTypeResolver.updateNumberType(expressionType, expression, context); + } + else { + expressionType = integerValueTypeConstant.getUnknownIntegerType(); + } + } + else { + expressionType = ((TypedCompileTimeConstant) value).getType(); } return TypeInfoFactoryPackage.createCheckedTypeInfo(expressionType, context, expression); diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt index 1091c6b491c..e47482a3466 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt @@ -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 { 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()) diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java index c48b5449257..d1d0cdede90 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java @@ -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); } diff --git a/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.java b/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.java index 9e0b81576c2..2c5840ad27f 100644 --- a/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.java +++ b/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.java @@ -8,4 +8,6 @@ class Foo { public static final boolean bool = true; public static final char c = 'c'; public static final String str = "str"; + public static final int charAsInt = '3'; + public static final char intAsChar = 3; } \ No newline at end of file diff --git a/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.kt b/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.kt index aa6d75c3acb..cb542bcd213 100644 --- a/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.kt +++ b/compiler/testData/codegen/boxAgainstJava/annotations/javaPropertyAsAnnotationParameter.kt @@ -1,4 +1,4 @@ -Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.bool, Foo.c, Foo.str) class MyClass +Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b, Foo.bool, Foo.c, Foo.str, Foo.charAsInt, Foo.intAsChar) class MyClass fun box(): String { val ann = javaClass().getAnnotation(javaClass()) @@ -12,6 +12,8 @@ fun box(): String { if (!ann.bool) return "fail: annotation parameter i should be true, but was ${ann.i}" if (ann.c != 'c') return "fail: annotation parameter i should be c, but was ${ann.i}" if (ann.str != "str") return "fail: annotation parameter i should be str, but was ${ann.i}" + if (ann.i2 != '3'.toInt()) return "fail: annotation parameter i2 should be ${'3'.toInt()}, but was ${ann.i}" + if (ann.c2 != 3.toChar()) return "fail: annotation parameter c2 should be 3, but was ${ann.i}" return "OK" } @@ -24,5 +26,7 @@ annotation(retention = AnnotationRetention.RUNTIME) class Ann( val b: Byte, val bool: Boolean, val c: Char, - val str: String + val str: String, + val i2: Int, + val c2: Char ) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotatedResultType.txt b/compiler/testData/diagnostics/tests/annotations/AnnotatedResultType.txt index aa0615c7e0b..76c1bb536a0 100644 --- a/compiler/testData/diagnostics/tests/annotations/AnnotatedResultType.txt +++ b/compiler/testData/diagnostics/tests/annotations/AnnotatedResultType.txt @@ -1,6 +1,6 @@ package -internal fun foo(): @[My(x = IntegerValueType(42))] kotlin.Int +internal fun foo(): @[My(x = 42)] kotlin.Int kotlin.annotation.target(allowedTargets = {AnnotationTarget.TYPE}) kotlin.annotation.annotation() internal final class My : kotlin.Annotation { public constructor My(/*0*/ x: kotlin.Int) diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotationOnObject.txt b/compiler/testData/diagnostics/tests/annotations/AnnotationOnObject.txt index 3ffe8307356..58c743b8ff7 100644 --- a/compiler/testData/diagnostics/tests/annotations/AnnotationOnObject.txt +++ b/compiler/testData/diagnostics/tests/annotations/AnnotationOnObject.txt @@ -12,7 +12,7 @@ package test { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } - test.A(a = IntegerValueType(12), c = "Hello") internal object SomeObject { + test.A(a = 12, c = "Hello") internal object SomeObject { private constructor SomeObject() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/tests/annotations/BasicAnnotations.txt b/compiler/testData/diagnostics/tests/annotations/BasicAnnotations.txt index a1080dfccfa..d22174c1d06 100644 --- a/compiler/testData/diagnostics/tests/annotations/BasicAnnotations.txt +++ b/compiler/testData/diagnostics/tests/annotations/BasicAnnotations.txt @@ -2,10 +2,10 @@ package my() internal fun foo(): kotlin.Unit my1() internal fun foo2(): kotlin.Unit -my1(i = IntegerValueType(2)) internal fun foo3(): kotlin.Unit +my1(i = 2) internal fun foo3(): kotlin.Unit my2() internal fun foo4(): kotlin.Unit my2() internal fun foo41(): kotlin.Unit -my2(i = IntegerValueType(2)) internal fun foo42(): kotlin.Unit +my2(i = 2) internal fun foo42(): kotlin.Unit kotlin.annotation.annotation() internal final class my : kotlin.Annotation { public constructor my() diff --git a/compiler/testData/diagnostics/tests/annotations/ConstructorCall.txt b/compiler/testData/diagnostics/tests/annotations/ConstructorCall.txt index 6762022a638..282baf803a7 100644 --- a/compiler/testData/diagnostics/tests/annotations/ConstructorCall.txt +++ b/compiler/testData/diagnostics/tests/annotations/ConstructorCall.txt @@ -1,7 +1,7 @@ package -Ann2(a = Ann1(a = IntegerValueType(1))) internal val a: kotlin.Int = 1 -Ann2(a = Ann1(a = IntegerValueType(1))) internal val c: kotlin.Int = 2 +Ann2(a = Ann1(a = 1)) internal val a: kotlin.Int = 1 +Ann2(a = Ann1(a = 1)) internal val c: kotlin.Int = 2 internal fun bar(/*0*/ a: Ann = ...): kotlin.Unit internal fun foo(): kotlin.Unit internal fun javaClass(): java.lang.Class diff --git a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotated.txt b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotated.txt index 3ce20f4bfb6..4fbbf722b26 100644 --- a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotated.txt +++ b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotated.txt @@ -1,6 +1,6 @@ package -RecursivelyAnnotated(x = IntegerValueType(1)) kotlin.annotation.annotation() internal final class RecursivelyAnnotated : kotlin.Annotation { +RecursivelyAnnotated(x = 1) kotlin.annotation.annotation() internal final class RecursivelyAnnotated : kotlin.Annotation { public constructor RecursivelyAnnotated(/*0*/ x: kotlin.Int) internal final val x: kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameter.txt b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameter.txt index aa410c44299..39cd83556cd 100644 --- a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameter.txt +++ b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameter.txt @@ -1,8 +1,8 @@ package kotlin.annotation.annotation() internal final class RecursivelyAnnotated : kotlin.Annotation { - public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = IntegerValueType(1)) x: kotlin.Int) - RecursivelyAnnotated(x = IntegerValueType(1)) internal final val x: kotlin.Int + public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = 1) x: kotlin.Int) + RecursivelyAnnotated(x = 1) internal final val x: kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String diff --git a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterType.txt b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterType.txt index 111ffbf2d28..a5994d556c1 100644 --- a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterType.txt +++ b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterType.txt @@ -1,8 +1,8 @@ package kotlin.annotation.target(allowedTargets = {AnnotationTarget.TYPE}) kotlin.annotation.annotation() internal final class RecursivelyAnnotated : kotlin.Annotation { - public constructor RecursivelyAnnotated(/*0*/ x: @[RecursivelyAnnotated(x = IntegerValueType(1))] kotlin.Int) - internal final val x: @[RecursivelyAnnotated(x = IntegerValueType(1))] kotlin.Int + public constructor RecursivelyAnnotated(/*0*/ x: @[RecursivelyAnnotated(x = 1)] kotlin.Int) + internal final val x: @[RecursivelyAnnotated(x = 1)] kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String diff --git a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterWithAt.txt b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterWithAt.txt index aa410c44299..39cd83556cd 100644 --- a/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterWithAt.txt +++ b/compiler/testData/diagnostics/tests/annotations/RecursivelyAnnotatedParameterWithAt.txt @@ -1,8 +1,8 @@ package kotlin.annotation.annotation() internal final class RecursivelyAnnotated : kotlin.Annotation { - public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = IntegerValueType(1)) x: kotlin.Int) - RecursivelyAnnotated(x = IntegerValueType(1)) internal final val x: kotlin.Int + public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = 1) x: kotlin.Int) + RecursivelyAnnotated(x = 1) internal final val x: kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String diff --git a/compiler/testData/diagnostics/tests/annotations/RecursivelyIncorrectlyAnnotatedParameter.txt b/compiler/testData/diagnostics/tests/annotations/RecursivelyIncorrectlyAnnotatedParameter.txt index ad23ca551a4..2bc4d2a30c0 100644 --- a/compiler/testData/diagnostics/tests/annotations/RecursivelyIncorrectlyAnnotatedParameter.txt +++ b/compiler/testData/diagnostics/tests/annotations/RecursivelyIncorrectlyAnnotatedParameter.txt @@ -1,8 +1,8 @@ package internal final class RecursivelyAnnotated { - public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = IntegerValueType(1)) x: kotlin.Int) - RecursivelyAnnotated(x = IntegerValueType(1)) internal final val x: kotlin.Int + public constructor RecursivelyAnnotated(/*0*/ RecursivelyAnnotated(x = 1) x: kotlin.Int) + RecursivelyAnnotated(x = 1) internal final val x: kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String diff --git a/compiler/testData/diagnostics/tests/annotations/WrongAnnotationArgsOnObject.txt b/compiler/testData/diagnostics/tests/annotations/WrongAnnotationArgsOnObject.txt index 4593d4f7324..5c3b870ce33 100644 --- a/compiler/testData/diagnostics/tests/annotations/WrongAnnotationArgsOnObject.txt +++ b/compiler/testData/diagnostics/tests/annotations/WrongAnnotationArgsOnObject.txt @@ -11,7 +11,7 @@ package test { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } - test.BadAnnotation(s = IntegerValueType(1)) internal object SomeObject { + test.BadAnnotation(s = 1) internal object SomeObject { private constructor SomeObject() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/tests/annotations/atAnnotationResolve.txt b/compiler/testData/diagnostics/tests/annotations/atAnnotationResolve.txt index 195d72eac5c..8304ffac487 100644 --- a/compiler/testData/diagnostics/tests/annotations/atAnnotationResolve.txt +++ b/compiler/testData/diagnostics/tests/annotations/atAnnotationResolve.txt @@ -1,11 +1,11 @@ package -Ann(x = IntegerValueType(1)) Ann(x = IntegerValueType(2)) Ann(x = IntegerValueType(3)) private final class A { +Ann(x = 1) Ann(x = 2) Ann(x = 3) private final class A { Ann() public constructor A() Ann() internal final val x: kotlin.Int = 1 - internal final fun bar(/*0*/ x: @[Ann(x = IntegerValueType(1)) Ann(x = IntegerValueType(2)) Ann(x = IntegerValueType(3))] kotlin.Int): kotlin.Unit + internal final fun bar(/*0*/ x: @[Ann(x = 1) Ann(x = 2) Ann(x = 3)] kotlin.Int): kotlin.Unit public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - Ann(x = IntegerValueType(5)) internal final fun foo(): kotlin.Unit + Ann(x = 5) internal final fun foo(): kotlin.Unit public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.txt b/compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.txt index b10e1ac4536..08b73867132 100644 --- a/compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.txt +++ b/compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.txt @@ -9,7 +9,7 @@ internal final class A { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String internal final inner class B { - Ann(x = IntegerValueType(2)) public constructor B(/*0*/ y: kotlin.Int) + Ann(x = 2) public constructor B(/*0*/ y: kotlin.Int) internal final val y: kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/tests/namedArguments/allowForJavaAnnotation.txt b/compiler/testData/diagnostics/tests/namedArguments/allowForJavaAnnotation.txt index 72c58383a23..ef1414b9a56 100644 --- a/compiler/testData/diagnostics/tests/namedArguments/allowForJavaAnnotation.txt +++ b/compiler/testData/diagnostics/tests/namedArguments/allowForJavaAnnotation.txt @@ -1,6 +1,6 @@ package -A(x = IntegerValueType(1), y = "2") internal fun test(): kotlin.Unit +A(x = 1, y = "2") internal fun test(): kotlin.Unit public final class A : kotlin.Annotation { public constructor A(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.String) diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/ctrsAnnotationResolve.txt b/compiler/testData/diagnostics/tests/secondaryConstructors/ctrsAnnotationResolve.txt index 1a3899fe003..d2331f93dbd 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/ctrsAnnotationResolve.txt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/ctrsAnnotationResolve.txt @@ -3,7 +3,7 @@ package internal final class A { Ann1() public constructor A() Ann2() public constructor A(/*0*/ x1: kotlin.Int) - Ann2(x = IntegerValueType(2)) public constructor A(/*0*/ x1: kotlin.Int, /*1*/ x2: kotlin.Int) + Ann2(x = 2) public constructor A(/*0*/ x1: kotlin.Int, /*1*/ x2: kotlin.Int) public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationClassMethodCall.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationClassMethodCall.txt index 5d763d51ae8..abd04a4380c 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationClassMethodCall.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationClassMethodCall.txt @@ -14,7 +14,7 @@ public final class A : kotlin.Annotation { public abstract fun value(): kotlin.String } -A(arg = IntegerValueType(1), value = "a") internal final class MyClass { +A(arg = 1, value = "a") internal final class MyClass { public constructor MyClass() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.txt index bc95994fb3f..559b318378d 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.txt @@ -29,7 +29,7 @@ Ann(i = {}) Ann(i = {1}) Ann(i = {}) Ann(i = {1}) Ann(i = {{1}}) internal final public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -AnnAnn(i = {Ann(i = {IntegerValueType(1)})}) AnnAnn(i = {}) internal final class TestAnn { +AnnAnn(i = {Ann(i = {1})}) AnnAnn(i = {}) internal final class TestAnn { public constructor TestAnn() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.txt index f1a137aefb6..6621e28bf33 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.txt @@ -29,7 +29,7 @@ Ann(i = {}) Ann(i = {1}) Ann(i = {}) Ann(i = {1}) Ann(i = {}) Ann(i = {1}) Ann(i public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -AnnAnn(i = {Ann(i = {IntegerValueType(1)})}) AnnAnn(i = {}) internal final class TestAnn { +AnnAnn(i = {Ann(i = {1})}) AnnAnn(i = {}) internal final class TestAnn { public constructor TestAnn() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArray.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArray.txt index 032f49996fc..a69cf471c73 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArray.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArray.txt @@ -1,12 +1,12 @@ package -A(value = {"1", "2", "3"}, y = IntegerValueType(1)) internal fun test1(): kotlin.Unit -A(value = {"4"}, y = IntegerValueType(2)) internal fun test2(): kotlin.Unit -A(value = {{"5", "6"}, "7"}, y = IntegerValueType(3)) internal fun test3(): kotlin.Unit -A(value = {"1", "2", "3"}, x = kotlin.String::class, y = IntegerValueType(4)) internal fun test4(): kotlin.Unit -A(value = {"4"}, y = IntegerValueType(5)) internal fun test5(): kotlin.Unit -A(value = {{"5", "6"}, "7"}, x = kotlin.Any::class, y = IntegerValueType(6)) internal fun test6(): kotlin.Unit -A(value = {}, y = IntegerValueType(7)) internal fun test7(): kotlin.Unit +A(value = {"1", "2", "3"}, y = 1) internal fun test1(): kotlin.Unit +A(value = {"4"}, y = 2) internal fun test2(): kotlin.Unit +A(value = {{"5", "6"}, "7"}, y = 3) internal fun test3(): kotlin.Unit +A(value = {"1", "2", "3"}, x = kotlin.String::class, y = 4) internal fun test4(): kotlin.Unit +A(value = {"4"}, y = 5) internal fun test5(): kotlin.Unit +A(value = {{"5", "6"}, "7"}, x = kotlin.Any::class, y = 6) internal fun test6(): kotlin.Unit +A(value = {}, y = 7) internal fun test7(): kotlin.Unit A(value = {"8", "9", "10"}) internal fun test8(): kotlin.Unit public final class A : kotlin.Annotation { diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayAndOtherDefault.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayAndOtherDefault.txt index 56f9dfb46d6..607954a26f4 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayAndOtherDefault.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayAndOtherDefault.txt @@ -1,16 +1,16 @@ package A(value = {"1", "2", "3"}) internal fun test1(): kotlin.Unit -A(value = {"5", "6"}, x = kotlin.Any::class, y = IntegerValueType(3)) internal fun test10(): kotlin.Unit -A(value = {"5", "6", "7"}, x = kotlin.Any::class, y = IntegerValueType(3)) internal fun test11(): kotlin.Unit +A(value = {"5", "6"}, x = kotlin.Any::class, y = 3) internal fun test10(): kotlin.Unit +A(value = {"5", "6", "7"}, x = kotlin.Any::class, y = 3) internal fun test11(): kotlin.Unit A(value = {"4"}) internal fun test2(): kotlin.Unit A(value = {{"5", "6"}, "7"}) internal fun test3(): kotlin.Unit A(value = {"1", "2", "3"}, x = kotlin.String::class) internal fun test4(): kotlin.Unit -A(value = {"4"}, y = IntegerValueType(2)) internal fun test5(): kotlin.Unit -A(value = {{"5", "6"}, "7"}, x = kotlin.Any::class, y = IntegerValueType(3)) internal fun test6(): kotlin.Unit +A(value = {"4"}, y = 2) internal fun test5(): kotlin.Unit +A(value = {{"5", "6"}, "7"}, x = kotlin.Any::class, y = 3) internal fun test6(): kotlin.Unit A(value = {}) internal fun test7(): kotlin.Unit A(value = {}) internal fun test8(): kotlin.Unit -A(value = {}, x = kotlin.Any::class, y = IntegerValueType(3)) internal fun test9(): kotlin.Unit +A(value = {}, x = kotlin.Any::class, y = 3) internal fun test9(): kotlin.Unit public final class A : kotlin.Annotation { public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array*/, /*1*/ x: kotlin.reflect.KClass<*> = ..., /*2*/ y: kotlin.Int = ...) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/javaAnnotationWithVarargArgument.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/javaAnnotationWithVarargArgument.txt index fd257b84b8c..dac73b98869 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/javaAnnotationWithVarargArgument.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/javaAnnotationWithVarargArgument.txt @@ -1,6 +1,6 @@ package -A(value = {IntegerValueType(1), "b"}) internal fun test(): kotlin.Unit +A(value = {1, "b"}) internal fun test(): kotlin.Unit public final class A : kotlin.Annotation { public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array*/) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/kotlinAnnotationWithVarargArgument.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/kotlinAnnotationWithVarargArgument.txt index ac5ed31f939..5974a3c30b3 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/kotlinAnnotationWithVarargArgument.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter/kotlinAnnotationWithVarargArgument.txt @@ -1,6 +1,6 @@ package -B(args = {IntegerValueType(1), "b"}) internal fun test(): kotlin.Unit +B(args = {1, "b"}) internal fun test(): kotlin.Unit kotlin.annotation.annotation() internal final class B : kotlin.Annotation { public constructor B(/*0*/ vararg args: kotlin.String /*kotlin.Array*/) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/annotationAsArgument.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/annotationAsArgument.txt index a6254ef6253..2c4ea8df39b 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/annotationAsArgument.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/annotationAsArgument.txt @@ -24,14 +24,14 @@ public final class B : kotlin.Annotation { public abstract fun y(): kotlin.Int } -A(arg = kotlin.String::class, b = B(y = IntegerValueType(1))) internal final class MyClass1 { +A(arg = kotlin.String::class, b = B(y = 1)) internal final class MyClass1 { public constructor MyClass1() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(b = B(y = IntegerValueType(3))) internal final class MyClass2 { +A(b = B(y = 3)) internal final class MyClass2 { public constructor MyClass2() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argAndOtherDefault.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argAndOtherDefault.txt index c2c0e772a6d..3395985b2b7 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argAndOtherDefault.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argAndOtherDefault.txt @@ -18,7 +18,7 @@ A(arg = kotlin.String::class) internal final class MyClass1 { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(arg = kotlin.String::class, x = IntegerValueType(2)) internal final class MyClass2 { +A(arg = kotlin.String::class, x = 2) internal final class MyClass2 { public constructor MyClass2() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argWithDefaultAndOther.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argWithDefaultAndOther.txt index 0ad8f243afb..117f5e6ccb0 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argWithDefaultAndOther.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/argWithDefaultAndOther.txt @@ -11,14 +11,14 @@ public final class A : kotlin.Annotation { public abstract fun x(): kotlin.Int } -A(arg = kotlin.String::class, x = IntegerValueType(4)) internal final class MyClass2 { +A(arg = kotlin.String::class, x = 4) internal final class MyClass2 { public constructor MyClass2() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(x = IntegerValueType(5)) internal final class MyClass3 { +A(x = 5) internal final class MyClass3 { public constructor MyClass3() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueAndOtherDefault.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueAndOtherDefault.txt index 783990fc9f0..bb937f10a14 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueAndOtherDefault.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueAndOtherDefault.txt @@ -25,14 +25,14 @@ A(value = kotlin.String::class) internal final class MyClass2 { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(value = kotlin.String::class, x = IntegerValueType(2)) internal final class MyClass3 { +A(value = kotlin.String::class, x = 2) internal final class MyClass3 { public constructor MyClass3() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(value = kotlin.String::class, x = IntegerValueType(4)) internal final class MyClass4 { +A(value = kotlin.String::class, x = 4) internal final class MyClass4 { public constructor MyClass4() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueWithDefaultAndOther.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueWithDefaultAndOther.txt index 15eb1a6a038..d46c0049dc5 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueWithDefaultAndOther.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/javaAnnotationsWithKClassParameter/valueWithDefaultAndOther.txt @@ -11,21 +11,21 @@ public final class A : kotlin.Annotation { public abstract fun x(): kotlin.Int } -A(value = kotlin.String::class, x = IntegerValueType(2)) internal final class MyClass1 { +A(value = kotlin.String::class, x = 2) internal final class MyClass1 { public constructor MyClass1() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(value = kotlin.String::class, x = IntegerValueType(4)) internal final class MyClass2 { +A(value = kotlin.String::class, x = 4) internal final class MyClass2 { public constructor MyClass2() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -A(x = IntegerValueType(5)) internal final class MyClass3 { +A(x = 5) internal final class MyClass3 { public constructor MyClass3() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/kotlinAnnotation.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/kotlinAnnotation.txt index 2ad42368826..f5aeeece4c0 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/kotlinAnnotation.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/kotlinAnnotation.txt @@ -1,8 +1,8 @@ package -Ann(value = "a", x = IntegerValueType(1), y = 1.0.toDouble()) internal fun foo1(): kotlin.Unit -Ann(value = "b", x = IntegerValueType(2), y = 2.0.toDouble()) internal fun foo2(): kotlin.Unit -Ann(value = "c", x = IntegerValueType(3), y = 2.0.toDouble()) internal fun foo3(): kotlin.Unit +Ann(value = "a", x = 1, y = 1.0.toDouble()) internal fun foo1(): kotlin.Unit +Ann(value = "b", x = 2, y = 2.0.toDouble()) internal fun foo2(): kotlin.Unit +Ann(value = "c", x = 3, y = 2.0.toDouble()) internal fun foo3(): kotlin.Unit kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation { public constructor Ann(/*0*/ x: kotlin.Int, /*1*/ value: kotlin.String, /*2*/ y: kotlin.Double) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withValue.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withValue.txt index 964ccf60e5e..74b23d2864c 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withValue.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withValue.txt @@ -1,9 +1,9 @@ package -A(a = IntegerValueType(1), b = 1.0.toDouble(), value = "v1", x = false) internal fun foo1(): kotlin.Unit -A(a = IntegerValueType(2), b = 2.0.toDouble(), value = "v2", x = true) internal fun foo2(): kotlin.Unit -A(a = IntegerValueType(4), b = 3.0.toDouble(), value = "v2", x = true) internal fun foo3(): kotlin.Unit -A(a = IntegerValueType(4), b = 3.0.toDouble(), value = "v2", x = true) internal fun foo4(): kotlin.Unit +A(a = 1, b = 1.0.toDouble(), value = "v1", x = false) internal fun foo1(): kotlin.Unit +A(a = 2, b = 2.0.toDouble(), value = "v2", x = true) internal fun foo2(): kotlin.Unit +A(a = 4, b = 3.0.toDouble(), value = "v2", x = true) internal fun foo3(): kotlin.Unit +A(a = 4, b = 3.0.toDouble(), value = "v2", x = true) internal fun foo4(): kotlin.Unit public final class A : kotlin.Annotation { public constructor A(/*0*/ value: kotlin.String, /*1*/ a: kotlin.Int, /*2*/ b: kotlin.Double, /*3*/ x: kotlin.Boolean) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withoutValue.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withoutValue.txt index af89ce48848..1eaa96222ef 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withoutValue.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/prohibitPositionedArgument/withoutValue.txt @@ -1,8 +1,8 @@ package -A(a = IntegerValueType(1), b = 1.0.toDouble(), x = false) internal fun foo1(): kotlin.Unit -A(a = IntegerValueType(2), b = 2.0.toDouble(), x = true) internal fun foo2(): kotlin.Unit -A(a = IntegerValueType(4), b = 3.0.toDouble(), x = true) internal fun foo3(): kotlin.Unit +A(a = 1, b = 1.0.toDouble(), x = false) internal fun foo1(): kotlin.Unit +A(a = 2, b = 2.0.toDouble(), x = true) internal fun foo2(): kotlin.Unit +A(a = 4, b = 3.0.toDouble(), x = true) internal fun foo3(): kotlin.Unit public final class A : kotlin.Annotation { public constructor A(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Double, /*2*/ x: kotlin.Boolean) diff --git a/compiler/testData/evaluate/usesVariableAsConstant/OtherTypes.kt b/compiler/testData/evaluate/usesVariableAsConstant/OtherTypes.kt index eed8758f371..a4e735bf812 100644 --- a/compiler/testData/evaluate/usesVariableAsConstant/OtherTypes.kt +++ b/compiler/testData/evaluate/usesVariableAsConstant/OtherTypes.kt @@ -6,11 +6,17 @@ fun foo(): Boolean = true val x = 1 -// val prop1: null +// val prop1: false val prop1 = MyEnum.A // val prop2: null val prop2 = foo() // val prop3: true -val prop3 = "$x" \ No newline at end of file +val prop3 = "$x" + +// val prop4: false +val prop4 = intArrayOf(1, 2, 3) + +// val prop5: true +val prop5 = intArrayOf(1, 2, x, x) \ No newline at end of file diff --git a/compiler/testData/resolveAnnotations/parameters/byte.kt b/compiler/testData/resolveAnnotations/parameters/byte.kt index 975bd272817..b953532f881 100644 --- a/compiler/testData/resolveAnnotations/parameters/byte.kt +++ b/compiler/testData/resolveAnnotations/parameters/byte.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1, 1.toByte(), 128.toByte(), 128) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(1), b2 = 1.toByte(), b3 = -128.toByte(), b4 = IntegerValueType(128)) \ No newline at end of file +// EXPECTED: Ann(b1 = 1.toByte(), b2 = 1.toByte(), b3 = -128.toByte(), b4 = 128) \ No newline at end of file diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/char.kt b/compiler/testData/resolveAnnotations/parameters/expressions/char.kt index 2be63e20012..004b0288ea9 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/char.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/char.kt @@ -4,4 +4,4 @@ annotation class Ann(val c1: Char) Ann('a' - 'a') class MyClass -// EXPECTED: Ann(c1 = IntegerValueType(0)) +// EXPECTED: Ann(c1 = 0) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/divide.kt b/compiler/testData/resolveAnnotations/parameters/expressions/divide.kt index 5a146c2e77f..f5e60dd97e1 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/divide.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/divide.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1 / 1, 1 / 1, 1 / 1, 1 / 1) class MyClass -// EXPECTED: Ann(b = IntegerValueType(1), i = IntegerValueType(1), l = IntegerValueType(1), s = IntegerValueType(1)) +// EXPECTED: Ann(b = 1.toByte(), i = 1, l = 1.toLong(), s = 1.toShort()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/infixCallBinary.kt b/compiler/testData/resolveAnnotations/parameters/expressions/infixCallBinary.kt index 6d36415f438..0d67ae790da 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/infixCallBinary.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/infixCallBinary.kt @@ -10,4 +10,4 @@ annotation class Ann( Ann(1 plus 1, 1 minus 1, 1 times 1, 1 div 1, 1 mod 1) class MyClass -// EXPECTED: Ann(p1 = IntegerValueType(2), p2 = IntegerValueType(0), p3 = IntegerValueType(1), p4 = IntegerValueType(1), p5 = IntegerValueType(0)) +// EXPECTED: Ann(p1 = 2, p2 = 0, p3 = 1, p4 = 1, p5 = 0) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/intrincics.kt b/compiler/testData/resolveAnnotations/parameters/expressions/intrincics.kt index 57df6738438..eac70c9d80f 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/intrincics.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/intrincics.kt @@ -10,4 +10,4 @@ annotation class Ann(p1: Int, Ann(1 or 1, 1 and 1, 1 xor 1, 1 shl 1, 1 shr 1, 1 ushr 1) class MyClass -// EXPECTED: Ann(p1 = IntegerValueType(1), p2 = IntegerValueType(1), p3 = IntegerValueType(0), p4 = IntegerValueType(2), p5 = IntegerValueType(0), p6 = IntegerValueType(0)) +// EXPECTED: Ann(p1 = 1, p2 = 1.toShort(), p3 = 0.toByte(), p4 = 2, p5 = 0, p6 = 0) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/long.kt b/compiler/testData/resolveAnnotations/parameters/expressions/long.kt index fd46cfc7617..1363d922c3a 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/long.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/long.kt @@ -8,4 +8,4 @@ annotation class Ann( Ann(1 + 1, java.lang.Long.MAX_VALUE + 1 - 1, java.lang.Long.MAX_VALUE - 1) class MyClass -// EXPECTED: Ann(l1 = IntegerValueType(2), l2 = 9223372036854775807.toLong(), l3 = 9223372036854775806.toLong()) +// EXPECTED: Ann(l1 = 2.toLong(), l2 = 9223372036854775807.toLong(), l3 = 9223372036854775806.toLong()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/maxValueByte.kt b/compiler/testData/resolveAnnotations/parameters/expressions/maxValueByte.kt index 699ed948087..9127af4c873 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/maxValueByte.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/maxValueByte.kt @@ -16,4 +16,4 @@ Ann( p5 = 1.toByte() + 1.toByte() ) class MyClass -// EXPECTED: Ann(p1 = 128, p2 = IntegerValueType(2), p3 = 128, p4 = 2, p5 = 2) +// EXPECTED: Ann(p1 = 128, p2 = 2.toByte(), p3 = 128, p4 = 2, p5 = 2) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/maxValueInt.kt b/compiler/testData/resolveAnnotations/parameters/expressions/maxValueInt.kt index 9d558c9aef3..ad28cc95479 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/maxValueInt.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/maxValueInt.kt @@ -16,4 +16,4 @@ Ann( p5 = 1.toInt() + 1.toInt() ) class MyClass -// EXPECTED: Ann(p1 = -2147483648, p2 = IntegerValueType(2), p3 = -2147483648, p4 = 2, p5 = 2) +// EXPECTED: Ann(p1 = -2147483648, p2 = 2, p3 = -2147483648, p4 = 2, p5 = 2) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/miltiply.kt b/compiler/testData/resolveAnnotations/parameters/expressions/miltiply.kt index 18e45eb61ff..6974a5e5986 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/miltiply.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/miltiply.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1 * 1, 1 * 1, 1 * 1, 1 * 1) class MyClass -// EXPECTED: Ann(b = IntegerValueType(1), i = IntegerValueType(1), l = IntegerValueType(1), s = IntegerValueType(1)) +// EXPECTED: Ann(b = 1.toByte(), i = 1, l = 1.toLong(), s = 1.toShort()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/minus.kt b/compiler/testData/resolveAnnotations/parameters/expressions/minus.kt index 71fd2ae8bc3..f3e7cb53f57 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/minus.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/minus.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1 - 1, 1 - 1, 1 - 1, 1 - 1) class MyClass -// EXPECTED: Ann(b = IntegerValueType(0), i = IntegerValueType(0), l = IntegerValueType(0), s = IntegerValueType(0)) +// EXPECTED: Ann(b = 0.toByte(), i = 0, l = 0.toLong(), s = 0.toShort()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/mod.kt b/compiler/testData/resolveAnnotations/parameters/expressions/mod.kt index 1df17b2e9f1..0754ffe3dc1 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/mod.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/mod.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1 % 1, 1 % 1, 1 % 1, 1 % 1) class MyClass -// EXPECTED: Ann(b = IntegerValueType(0), i = IntegerValueType(0), l = IntegerValueType(0), s = IntegerValueType(0)) +// EXPECTED: Ann(b = 0.toByte(), i = 0, l = 0.toLong(), s = 0.toShort()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/paranthesized.kt b/compiler/testData/resolveAnnotations/parameters/expressions/paranthesized.kt index bc997c607cb..42bd629f2db 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/paranthesized.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/paranthesized.kt @@ -4,4 +4,4 @@ annotation class Ann(i: Int) Ann((1 + 2) * 2) class MyClass -// EXPECTED: Ann(i = IntegerValueType(6)) +// EXPECTED: Ann(i = 6) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/plus.kt b/compiler/testData/resolveAnnotations/parameters/expressions/plus.kt index 9c8042b543a..36d84f9b39d 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/plus.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/plus.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1 + 1, 1 + 1, 1 + 1, 1 + 1) class MyClass -// EXPECTED: Ann(b = IntegerValueType(2), i = IntegerValueType(2), l = IntegerValueType(2), s = IntegerValueType(2)) +// EXPECTED: Ann(b = 2.toByte(), i = 2, l = 2.toLong(), s = 2.toShort()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/simpleCallBinary.kt b/compiler/testData/resolveAnnotations/parameters/expressions/simpleCallBinary.kt index ccd45c47ed1..5e474748142 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/simpleCallBinary.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/simpleCallBinary.kt @@ -10,4 +10,4 @@ annotation class Ann( Ann(1.plus(1), 1.minus(1), 1.times(1), 1.div(1), 1.mod(1)) class MyClass -// EXPECTED: Ann(p1 = IntegerValueType(2), p2 = IntegerValueType(0), p3 = IntegerValueType(1), p4 = IntegerValueType(1), p5 = IntegerValueType(0)) +// EXPECTED: Ann(p1 = 2, p2 = 0, p3 = 1, p4 = 1, p5 = 0) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/unaryMinus.kt b/compiler/testData/resolveAnnotations/parameters/expressions/unaryMinus.kt index d2203adca7b..f7f7c911aac 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/unaryMinus.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/unaryMinus.kt @@ -11,4 +11,4 @@ annotation class Ann( Ann(-1, -1, -1, -1, -1.0, -1.0.toFloat()) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(-1), b2 = IntegerValueType(-1), b3 = IntegerValueType(-1), b4 = IntegerValueType(-1), b5 = -1.0.toDouble(), b6 = -1.0.toFloat()) +// EXPECTED: Ann(b1 = -1.toByte(), b2 = -1.toShort(), b3 = -1, b4 = -1.toLong(), b5 = -1.0.toDouble(), b6 = -1.0.toFloat()) diff --git a/compiler/testData/resolveAnnotations/parameters/expressions/unaryPlus.kt b/compiler/testData/resolveAnnotations/parameters/expressions/unaryPlus.kt index 5d519646817..8d9a91b50af 100644 --- a/compiler/testData/resolveAnnotations/parameters/expressions/unaryPlus.kt +++ b/compiler/testData/resolveAnnotations/parameters/expressions/unaryPlus.kt @@ -11,4 +11,4 @@ annotation class Ann( Ann(+1, +1, +1, +1, +1.0, +1.0.toFloat()) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(1), b2 = IntegerValueType(1), b3 = IntegerValueType(1), b4 = IntegerValueType(1), b5 = 1.0.toDouble(), b6 = 1.0.toFloat()) +// EXPECTED: Ann(b1 = 1.toByte(), b2 = 1.toShort(), b3 = 1, b4 = 1.toLong(), b5 = 1.0.toDouble(), b6 = 1.0.toFloat()) diff --git a/compiler/testData/resolveAnnotations/parameters/int.kt b/compiler/testData/resolveAnnotations/parameters/int.kt index f66cf48ab35..f02f77b3c02 100644 --- a/compiler/testData/resolveAnnotations/parameters/int.kt +++ b/compiler/testData/resolveAnnotations/parameters/int.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1, 1.toInt(), 2147483648.toInt(), 2147483648) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(1), b2 = 1, b3 = -2147483648, b4 = IntegerValueType(2147483648)) \ No newline at end of file +// EXPECTED: Ann(b1 = 1, b2 = 1, b3 = -2147483648, b4 = 2147483648.toLong()) \ No newline at end of file diff --git a/compiler/testData/resolveAnnotations/parameters/long.kt b/compiler/testData/resolveAnnotations/parameters/long.kt index a5e737289e6..06c689d50ef 100644 --- a/compiler/testData/resolveAnnotations/parameters/long.kt +++ b/compiler/testData/resolveAnnotations/parameters/long.kt @@ -7,4 +7,4 @@ annotation class Ann( Ann(1, 1.toLong()) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(1), b2 = 1.toLong()) \ No newline at end of file +// EXPECTED: Ann(b1 = 1.toLong(), b2 = 1.toLong()) \ No newline at end of file diff --git a/compiler/testData/resolveAnnotations/parameters/short.kt b/compiler/testData/resolveAnnotations/parameters/short.kt index bdd892a457a..9192a5bbcb2 100644 --- a/compiler/testData/resolveAnnotations/parameters/short.kt +++ b/compiler/testData/resolveAnnotations/parameters/short.kt @@ -9,4 +9,4 @@ annotation class Ann( Ann(1, 1.toShort(), 32768.toShort(), 32768) class MyClass -// EXPECTED: Ann(b1 = IntegerValueType(1), b2 = 1.toShort(), b3 = -32768.toShort(), b4 = IntegerValueType(32768)) \ No newline at end of file +// EXPECTED: Ann(b1 = 1.toShort(), b2 = 1.toShort(), b3 = -32768.toShort(), b4 = 32768) \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ExpectedLoadErrorsUtil.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ExpectedLoadErrorsUtil.java index b982e1a93e2..77b4dc3a3f9 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ExpectedLoadErrorsUtil.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ExpectedLoadErrorsUtil.java @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.load.java.JavaBindingContext; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.renderer.DescriptorRenderer; import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.scopes.JetScope; import java.util.*; @@ -84,7 +84,7 @@ public class ExpectedLoadErrorsUtil { if (annotation == null) return null; // we expect exactly one annotation argument - CompileTimeConstant argument = annotation.getAllValueArguments().values().iterator().next(); + ConstantValue argument = annotation.getAllValueArguments().values().iterator().next(); String error = (String) argument.getValue(); //noinspection ConstantConditions diff --git a/compiler/tests/org/jetbrains/kotlin/resolve/annotation/AnnotationDescriptorResolveTest.java b/compiler/tests/org/jetbrains/kotlin/resolve/annotation/AnnotationDescriptorResolveTest.java index 5cef40c425d..58202a730fe 100644 --- a/compiler/tests/org/jetbrains/kotlin/resolve/annotation/AnnotationDescriptorResolveTest.java +++ b/compiler/tests/org/jetbrains/kotlin/resolve/annotation/AnnotationDescriptorResolveTest.java @@ -21,7 +21,7 @@ import java.io.IOException; public class AnnotationDescriptorResolveTest extends AbstractAnnotationDescriptorResolveTest { public void testIntAnnotation() throws IOException { String content = getContent("AnnInt(1)"); - String expectedAnnotation = "AnnInt(a = IntegerValueType(1))"; + String expectedAnnotation = "AnnInt(a = 1)"; doTest(content, expectedAnnotation); } @@ -51,13 +51,13 @@ public class AnnotationDescriptorResolveTest extends AbstractAnnotationDescripto public void testIntArrayAnnotation() throws IOException { String content = getContent("AnnIntArray(intArray(1, 2))"); - String expectedAnnotation = "AnnIntArray(a = {IntegerValueType(1), IntegerValueType(2)})"; + String expectedAnnotation = "AnnIntArray(a = {1, 2})"; doTest(content, expectedAnnotation); } public void testIntArrayVarargAnnotation() throws IOException { String content = getContent("AnnIntVararg(1, 2)"); - String expectedAnnotation = "AnnIntVararg(a = {IntegerValueType(1), IntegerValueType(2)})"; + String expectedAnnotation = "AnnIntVararg(a = {1, 2})"; doTest(content, expectedAnnotation); } @@ -81,7 +81,7 @@ public class AnnotationDescriptorResolveTest extends AbstractAnnotationDescripto public void testAnnotationAnnotation() throws Exception { String content = getContent("AnnAnn(AnnInt(1))"); - String expectedAnnotation = "AnnAnn(a = AnnInt(a = IntegerValueType(1)))"; + String expectedAnnotation = "AnnAnn(a = AnnInt(a = 1))"; doTest(content, expectedAnnotation); } diff --git a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt index 21b3da3518c..7f53fbfad86 100644 --- a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt @@ -18,9 +18,12 @@ package org.jetbrains.kotlin.resolve.constants.evaluate import com.intellij.openapi.util.io.FileUtil import org.jetbrains.kotlin.descriptors.VariableDescriptor +import org.jetbrains.kotlin.psi.JetProperty import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DelegatingBindingTrace +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.annotation.AbstractAnnotationDescriptorResolveTest -import org.jetbrains.kotlin.resolve.constants.IntegerValueConstant +import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant import org.jetbrains.kotlin.resolve.constants.StringValue import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.JetTestUtils @@ -47,12 +50,7 @@ public abstract class AbstractEvaluateExpressionTest : AbstractAnnotationDescrip fun doIsPureTest(path: String) { doTest(path) { property, context -> - val compileTimeConstant = property.getCompileTimeInitializer() - if (compileTimeConstant is IntegerValueConstant) { - compileTimeConstant.isPure().toString() - } else { - "null" - } + evaluateInitializer(context, property)?.isPure.toString() } } @@ -60,15 +58,20 @@ public abstract class AbstractEvaluateExpressionTest : AbstractAnnotationDescrip fun doUsesVariableAsConstantTest(path: String) { doTest(path) { property, context -> - val compileTimeConstant = property.getCompileTimeInitializer() - if (compileTimeConstant == null) { - "null" - } else { - compileTimeConstant.usesVariableAsConstant().toString() - } + evaluateInitializer(context, property)?.usesVariableAsConstant.toString() } } + private fun evaluateInitializer(context: BindingContext, property: VariableDescriptor): CompileTimeConstant<*>? { + val propertyDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(property) as JetProperty + val compileTimeConstant = ConstantExpressionEvaluator.evaluate( + propertyDeclaration.getInitializer()!!, + DelegatingBindingTrace(context, "trace for evaluating compile time constant"), + property.getType() + ) + return compileTimeConstant + } + private fun doTest(path: String, getValueToTest: (VariableDescriptor, BindingContext) -> String) { val myFile = File(path) val fileText = FileUtil.loadFile(myFile, true) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt index e1f54ca3f05..7be9187d8cb 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaAnnotationDescriptor.kt @@ -30,7 +30,8 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.renderer.DescriptorRenderer -import org.jetbrains.kotlin.resolve.constants.* +import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.ConstantValueFactory import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass import org.jetbrains.kotlin.resolve.jvm.PLATFORM_TYPES import org.jetbrains.kotlin.types.* @@ -64,7 +65,7 @@ class LazyJavaAnnotationDescriptor( annotationClass?.getDefaultType() ?: ErrorUtils.createErrorType(fqName.asString()) } - private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.Impl(true, false, false), c.module.builtIns) + private val factory = ConstantValueFactory(c.module.builtIns) override fun getType(): JetType = type() @@ -74,7 +75,7 @@ class LazyJavaAnnotationDescriptor( override fun getAllValueArguments() = allValueArguments() - private fun computeValueArguments(): Map> { + private fun computeValueArguments(): Map> { val constructors = getAnnotationClass().getConstructors() if (constructors.isEmpty()) return mapOf() @@ -100,9 +101,9 @@ class LazyJavaAnnotationDescriptor( private fun getAnnotationClass() = getType().getConstructor().getDeclarationDescriptor() as ClassDescriptor - private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? { + private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): ConstantValue<*>? { return when (argument) { - is JavaLiteralAnnotationArgument -> factory.createCompileTimeConstant(argument.value) + is JavaLiteralAnnotationArgument -> factory.createConstantValue(argument.value) is JavaEnumValueAnnotationArgument -> resolveFromEnumValue(argument.resolve()) is JavaArrayAnnotationArgument -> resolveFromArray(argument.name ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements()) is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation()) @@ -111,13 +112,13 @@ class LazyJavaAnnotationDescriptor( } } - private fun resolveFromAnnotation(javaAnnotation: JavaAnnotation): CompileTimeConstant<*>? { + private fun resolveFromAnnotation(javaAnnotation: JavaAnnotation): ConstantValue<*>? { val descriptor = c.resolveAnnotation(javaAnnotation) ?: return null return factory.createAnnotationValue(descriptor) } - private fun resolveFromArray(argumentName: Name, elements: List): CompileTimeConstant<*>? { + private fun resolveFromArray(argumentName: Name, elements: List): ConstantValue<*>? { if (getType().isError()) return null val valueParameter = DescriptorResolverUtils.getAnnotationParameterByName(argumentName, getAnnotationClass()) ?: return null @@ -128,7 +129,7 @@ class LazyJavaAnnotationDescriptor( return factory.createArrayValue(values, valueParameter.getType()) } - private fun resolveFromEnumValue(element: JavaField?): CompileTimeConstant<*>? { + private fun resolveFromEnumValue(element: JavaField?): ConstantValue<*>? { if (element == null || !element.isEnumEntry()) return null val containingJavaClass = element.getContainingClass() @@ -142,7 +143,7 @@ class LazyJavaAnnotationDescriptor( return factory.createEnumValue(classifier) } - private fun resolveFromJavaClassObjectType(javaType: JavaType): CompileTimeConstant<*>? { + private fun resolveFromJavaClassObjectType(javaType: JavaType): ConstantValue<*>? { // Class type is never nullable in 'Foo.class' in Java val type = TypeUtils.makeNotNullable(c.typeResolver.transformJavaType( javaType, diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt index 8dab34fbd9c..a14dcc2e89c 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt @@ -181,7 +181,7 @@ class LazyJavaClassDescriptor( getAnnotations(). findAnnotation(JvmAnnotationNames.PURELY_IMPLEMENTS_ANNOTATION) ?: return null - val fqNameString = (annotation.getAllValueArguments().values().singleOrNull() as? StringValue)?.getValue() ?: return null + val fqNameString = (annotation.getAllValueArguments().values().singleOrNull() as? StringValue)?.value ?: return null if (!isValidJavaFqName(fqNameString)) return null return FqName(fqNameString) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.java deleted file mode 100644 index 712bcb5c2a4..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.load.java.structure; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.PropertyDescriptor; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; - -public interface JavaPropertyInitializerEvaluator { - JavaPropertyInitializerEvaluator DO_NOTHING = new JavaPropertyInitializerEvaluator() { - @Nullable - @Override - public CompileTimeConstant getInitializerConstant(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor) { - return null; - } - - @Override - public boolean isNotNullCompileTimeConstant(@NotNull JavaField field) { - return false; - } - }; - - @Nullable - CompileTimeConstant getInitializerConstant(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor); - - boolean isNotNullCompileTimeConstant(@NotNull JavaField field); -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.kt new file mode 100644 index 00000000000..44f2be2af9d --- /dev/null +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaPropertyInitializerEvaluator.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.load.java.structure + +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.resolve.constants.ConstantValue + +public interface JavaPropertyInitializerEvaluator { + public fun getInitializerConstant(field: JavaField, descriptor: PropertyDescriptor): ConstantValue<*>? + + public fun isNotNullCompileTimeConstant(field: JavaField): Boolean + + public object DoNothing : JavaPropertyInitializerEvaluator { + override fun getInitializerConstant(field: JavaField, descriptor: PropertyDescriptor) = null + + override fun isNotNullCompileTimeConstant(field: JavaField) = false + } +} diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/BinaryClassAnnotationAndConstantLoaderImpl.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/BinaryClassAnnotationAndConstantLoaderImpl.kt index e856d4291d0..a867de852c4 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/BinaryClassAnnotationAndConstantLoaderImpl.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/BinaryClassAnnotationAndConstantLoaderImpl.kt @@ -26,7 +26,9 @@ import org.jetbrains.kotlin.load.java.components.DescriptorResolverUtils import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.* +import org.jetbrains.kotlin.resolve.constants.AnnotationValue +import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.ConstantValueFactory import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.AnnotationDeserializer import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter @@ -42,16 +44,16 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( storageManager: StorageManager, kotlinClassFinder: KotlinClassFinder, errorReporter: ErrorReporter -) : AbstractBinaryClassAnnotationAndConstantLoader>( +) : AbstractBinaryClassAnnotationAndConstantLoader>( storageManager, kotlinClassFinder, errorReporter ) { private val annotationDeserializer = AnnotationDeserializer(module) - private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, module.builtIns) + private val factory = ConstantValueFactory(module.builtIns) override fun loadTypeAnnotation(proto: ProtoBuf.Annotation, nameResolver: NameResolver): AnnotationDescriptor = annotationDeserializer.deserializeAnnotation(proto, nameResolver) - override fun loadConstant(desc: String, initializer: Any): CompileTimeConstant<*>? { + override fun loadConstant(desc: String, initializer: Any): ConstantValue<*>? { val normalizedValue: Any = if (desc in "ZBCS") { val intValue = initializer as Int when (desc) { @@ -66,7 +68,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( initializer } - return factory.createCompileTimeConstant(normalizedValue) + return factory.createConstantValue(normalizedValue) } override fun loadAnnotation( @@ -76,7 +78,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( val annotationClass = resolveClass(annotationClassId) return object : KotlinJvmBinaryClass.AnnotationArgumentVisitor { - private val arguments = HashMap>() + private val arguments = HashMap>() override fun visit(name: Name?, value: Any?) { if (name != null) { @@ -90,7 +92,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( override fun visitArray(name: Name): AnnotationArrayArgumentVisitor? { return object : KotlinJvmBinaryClass.AnnotationArrayArgumentVisitor { - private val elements = ArrayList>() + private val elements = ArrayList>() override fun visit(value: Any?) { elements.add(createConstant(name, value)) @@ -122,7 +124,7 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( } // NOTE: see analogous code in AnnotationDeserializer - private fun enumEntryValue(enumClassId: ClassId, name: Name): CompileTimeConstant<*> { + private fun enumEntryValue(enumClassId: ClassId, name: Name): ConstantValue<*> { val enumClass = resolveClass(enumClassId) if (enumClass.getKind() == ClassKind.ENUM_CLASS) { val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(name) @@ -137,12 +139,12 @@ public class BinaryClassAnnotationAndConstantLoaderImpl( result.add(AnnotationDescriptorImpl(annotationClass.getDefaultType(), arguments)) } - private fun createConstant(name: Name?, value: Any?): CompileTimeConstant<*> { - return factory.createCompileTimeConstant(value) ?: + private fun createConstant(name: Name?, value: Any?): ConstantValue<*> { + return factory.createConstantValue(value) ?: factory.createErrorValue("Unsupported annotation argument: $name") } - private fun setArgumentValueByName(name: Name, argumentValue: CompileTimeConstant<*>) { + private fun setArgumentValueByName(name: Name, argumentValue: ConstantValue<*>) { val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) if (parameter != null) { arguments[parameter] = argumentValue diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/RuntimeModuleData.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/RuntimeModuleData.kt index b581f3b1c5c..49bbc961aaf 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/RuntimeModuleData.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/RuntimeModuleData.kt @@ -50,7 +50,7 @@ public class RuntimeModuleData private constructor(public val module: ModuleDesc val globalJavaResolverContext = GlobalJavaResolverContext( storageManager, ReflectJavaClassFinder(classLoader), reflectKotlinClassFinder, deserializedDescriptorResolver, ExternalAnnotationResolver.EMPTY, ExternalSignatureResolver.DO_NOTHING, RuntimeErrorReporter, JavaResolverCache.EMPTY, - JavaPropertyInitializerEvaluator.DO_NOTHING, SamConversionResolver, RuntimeSourceElementFactory, singleModuleClassResolver + JavaPropertyInitializerEvaluator.DoNothing, SamConversionResolver, RuntimeSourceElementFactory, singleModuleClassResolver ) val lazyJavaPackageFragmentProvider = LazyJavaPackageFragmentProvider(globalJavaResolverContext, module, ReflectionTypes(module)) val javaDescriptorResolver = JavaDescriptorResolver(lazyJavaPackageFragmentProvider, module) diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/BuiltInsAnnotationAndConstantLoader.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/BuiltInsAnnotationAndConstantLoader.kt index 25a8f685cd1..dbf23ac2dc0 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/BuiltInsAnnotationAndConstantLoader.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/BuiltInsAnnotationAndConstantLoader.kt @@ -18,7 +18,7 @@ package org.jetbrains.kotlin.builtins import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant +import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.builtins.BuiltInsProtoBuf import org.jetbrains.kotlin.serialization.deserialization.* @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.types.JetType class BuiltInsAnnotationAndConstantLoader( module: ModuleDescriptor -) : AnnotationAndConstantLoader> { +) : AnnotationAndConstantLoader> { private val deserializer = AnnotationDeserializer(module) override fun loadClassAnnotations( @@ -68,7 +68,7 @@ class BuiltInsAnnotationAndConstantLoader( proto: ProtoBuf.Callable, nameResolver: NameResolver, expectedType: JetType - ): CompileTimeConstant<*>? { + ): ConstantValue<*>? { val value = proto.getExtension(BuiltInsProtoBuf.compileTimeValue) return deserializer.resolveValue(expectedType, value, nameResolver) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java index 42e178b124f..5b519332cdd 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java @@ -32,7 +32,7 @@ import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqNameUnsafe; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.resolve.DescriptorUtils; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.resolve.scopes.JetScope; import org.jetbrains.kotlin.storage.LockBasedStorageManager; import org.jetbrains.kotlin.types.*; @@ -659,7 +659,7 @@ public class KotlinBuiltIns { @NotNull public AnnotationDescriptor createExtensionAnnotation() { return new AnnotationDescriptorImpl(getBuiltInClassByName("extension").getDefaultType(), - Collections.>emptyMap()); + Collections.>emptyMap()); } private static boolean isTypeAnnotatedWithExtension(@NotNull JetType type) { diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/VariableDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/VariableDescriptor.java index cfe8ebb6056..0cc1b7c6080 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/VariableDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/VariableDescriptor.java @@ -18,7 +18,7 @@ package org.jetbrains.kotlin.descriptors; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.TypeSubstitutor; @@ -36,5 +36,5 @@ public interface VariableDescriptor extends CallableDescriptor { boolean isVar(); @Nullable - CompileTimeConstant getCompileTimeInitializer(); + ConstantValue getCompileTimeInitializer(); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationArgumentVisitor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationArgumentVisitor.java index 80e4162a899..b8038c25956 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationArgumentVisitor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationArgumentVisitor.java @@ -50,6 +50,4 @@ public interface AnnotationArgumentVisitor { R visitAnnotationValue(AnnotationValue value, D data); R visitKClassValue(KClassValue value, D data); - - R visitNumberTypeValue(IntegerValueTypeConstant value, D data); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptor.java index 90332148026..f0a8e72179d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptor.java @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.descriptors.annotations; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.ReadOnly; import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.types.JetType; import java.util.Map; @@ -30,5 +30,5 @@ public interface AnnotationDescriptor { @NotNull @ReadOnly - Map> getAllValueArguments(); + Map> getAllValueArguments(); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptorImpl.java index 44479d70dcd..aca2732e1d6 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/AnnotationDescriptorImpl.java @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.descriptors.annotations; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; import org.jetbrains.kotlin.renderer.DescriptorRenderer; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.types.JetType; import java.util.Collections; @@ -27,11 +27,11 @@ import java.util.Map; public class AnnotationDescriptorImpl implements AnnotationDescriptor { private final JetType annotationType; - private final Map> valueArguments; + private final Map> valueArguments; public AnnotationDescriptorImpl( @NotNull JetType annotationType, - @NotNull Map> valueArguments + @NotNull Map> valueArguments ) { this.annotationType = annotationType; this.valueArguments = Collections.unmodifiableMap(valueArguments); @@ -45,7 +45,7 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor { @Override @NotNull - public Map> getAllValueArguments() { + public Map> getAllValueArguments() { return valueArguments; } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/DefaultAnnotationArgumentVisitor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/DefaultAnnotationArgumentVisitor.java index 96cbcb6da63..b9322932867 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/DefaultAnnotationArgumentVisitor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/annotations/DefaultAnnotationArgumentVisitor.java @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.resolve.constants.*; import org.jetbrains.kotlin.resolve.constants.StringValue; public abstract class DefaultAnnotationArgumentVisitor implements AnnotationArgumentVisitor { - public abstract R visitValue(@NotNull CompileTimeConstant value, D data); + public abstract R visitValue(@NotNull ConstantValue value, D data); @Override public R visitLongValue(@NotNull LongValue value, D data) { @@ -92,9 +92,4 @@ public abstract class DefaultAnnotationArgumentVisitor implements Annotati public R visitAnnotationValue(AnnotationValue value, D data) { return visitValue(value, data); } - - @Override - public R visitNumberTypeValue(IntegerValueTypeConstant value, D data) { - return visitValue(value, data); - } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/VariableDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/VariableDescriptorImpl.java index b5ff6c55c58..45fc6153c27 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/VariableDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/VariableDescriptorImpl.java @@ -21,7 +21,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import org.jetbrains.kotlin.storage.NullableLazyValue; import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.LazyType; @@ -32,7 +32,7 @@ import java.util.Set; public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRootImpl implements VariableDescriptor { private JetType outType; - protected NullableLazyValue> compileTimeInitializer; + protected NullableLazyValue> compileTimeInitializer; public VariableDescriptorImpl( @NotNull DeclarationDescriptor containingDeclaration, @@ -59,7 +59,7 @@ public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRoo @Nullable @Override - public CompileTimeConstant getCompileTimeInitializer() { + public ConstantValue getCompileTimeInitializer() { // Force computation and setting of compileTimeInitializer, if needed if (compileTimeInitializer == null && outType instanceof LazyType) { outType.getConstructor(); @@ -71,7 +71,7 @@ public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRoo return null; } - public void setCompileTimeInitializer(@NotNull NullableLazyValue> compileTimeInitializer) { + public void setCompileTimeInitializer(@NotNull NullableLazyValue> compileTimeInitializer) { assert !isVar() : "Compile-time value for property initializer should be recorded only for final variables " + getName(); this.compileTimeInitializer = compileTimeInitializer; } diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index 24970e499a8..9179d91944b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -20,23 +20,20 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.descriptors.annotations.DefaultAnnotationArgumentVisitor -import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqNameBase -import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.resolve.constants.* +import org.jetbrains.kotlin.resolve.DescriptorUtils.isCompanionObject +import org.jetbrains.kotlin.resolve.constants.AnnotationValue +import org.jetbrains.kotlin.resolve.constants.ArrayValue +import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.KClassValue import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.ErrorUtils.UninferredParameterTypeConstructor -import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass -import org.jetbrains.kotlin.utils.* - -import java.util.* - -import org.jetbrains.kotlin.resolve.DescriptorUtils.isCompanionObject import org.jetbrains.kotlin.types.TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE +import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass +import java.util.ArrayList internal class DescriptorRendererImpl( val options: DescriptorRendererOptionsImpl @@ -384,7 +381,7 @@ internal class DescriptorRendererImpl( return (defaultList + argumentList).sort() } - private fun renderConstant(value: CompileTimeConstant<*>): String { + private fun renderConstant(value: ConstantValue<*>): String { return when (value) { is ArrayValue -> value.value.map { renderConstant(it) }.joinToString(", ", "{", "}") is AnnotationValue -> renderAnnotation(value.value) @@ -709,9 +706,8 @@ internal class DescriptorRendererImpl( private fun renderInitializer(variable: VariableDescriptor, builder: StringBuilder) { if (includePropertyConstant) { - val initializer = variable.getCompileTimeInitializer() - if (initializer != null) { - builder.append(" = ").append(escape(renderConstant(initializer))) + variable.getCompileTimeInitializer()?.let { constant -> + builder.append(" = ").append(escape(renderConstant(constant))) } } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt index 8ea1dc4dc19..793ae285248 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt @@ -16,13 +16,11 @@ package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.kotlin.builtins.KotlinBuiltIns 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(value, CompileTimeConstant.Parameters.Impl(true, false, false)) { +public class AnnotationValue(value: AnnotationDescriptor) : ConstantValue(value) { override val type: JetType get() = value.getType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt index 8c95657718b..6a69ae3e742 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt @@ -19,23 +19,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.types.JetType -import java.util.* public class ArrayValue( - value: List>, - override val type: JetType, - parameters: CompileTimeConstant.Parameters -) : CompileTimeConstant>>(value, parameters) { - - public constructor( - value: List>, - type: JetType, - usesVariableAsConstant: Boolean - ) : this(value, type, CompileTimeConstant.Parameters.Impl(true, false, usesVariableAsConstant)) - - override fun canBeUsedInAnnotations() = true - override fun isPure() = false - + value: List>, + override val type: JetType +) : ConstantValue>>(value) { init { assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt index 44dd0386c75..1b97566bb30 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt @@ -18,15 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class BooleanValue( value: Boolean, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : CompileTimeConstant(value, parameters) { - override fun isPure(): Boolean = false - +) : ConstantValue(value) { override val type = builtIns.getBooleanType() override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitBooleanValue(this, data) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt index 2b230fbc5fa..45017963172 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt @@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class ByteValue( value: Byte, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : IntegerValueConstant(value, parameters) { +) : IntegerValueConstant(value) { override val type = builtIns.getByteType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt index a14dd4ac4ab..58f52a4ebe8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt @@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class CharValue( value: Char, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : IntegerValueConstant(value, parameters) { +) : IntegerValueConstant(value) { override val type = builtIns.getCharType() @@ -37,7 +35,7 @@ public class CharValue( '\b' -> return "\\b" '\t' -> return "\\t" '\n' -> return "\\n" - //TODO_R: can't escape form feed in Kotlin + //TODO: KT-8507 12.toChar() -> return "\\f" '\r' -> return "\\r" else -> return if (isPrintableUnicode(c)) Character.toString(c) else "?" diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt index cb76122cdfd..7769ae1ab53 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt @@ -17,43 +17,84 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.types.* -public abstract class CompileTimeConstant protected constructor( - public open val value: T, - public val parameters: CompileTimeConstant.Parameters -) { - public open fun canBeUsedInAnnotations(): Boolean = parameters.canBeUsedInAnnotation +public interface CompileTimeConstant { + public val isError: Boolean + get() = false - public open fun isPure(): Boolean = parameters.isPure + public val parameters: CompileTimeConstant.Parameters - public open fun usesVariableAsConstant(): Boolean = parameters.usesVariableAsConstant + public fun toConstantValue(expectedType: JetType): ConstantValue - public abstract val type: JetType + public fun getValue(expectedType: JetType): T = toConstantValue(expectedType).value - public abstract fun accept(visitor: AnnotationArgumentVisitor, data: D): R + public val canBeUsedInAnnotations: Boolean get() = parameters.canBeUsedInAnnotation - override fun toString() = value.toString() + public val usesVariableAsConstant: Boolean get() = parameters.usesVariableAsConstant - public interface Parameters { - public open val canBeUsedInAnnotation: Boolean - public open val isPure: Boolean - public open val usesVariableAsConstant: Boolean + public val isPure: Boolean get() = parameters.isPure - public class Impl( - override val canBeUsedInAnnotation: Boolean, - override val isPure: Boolean, - override val usesVariableAsConstant: Boolean - ) : Parameters + public class Parameters( + public val canBeUsedInAnnotation: Boolean, + public val isPure: Boolean, + public val usesVariableAsConstant: Boolean + ) +} - 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") +public class TypedCompileTimeConstant( + public val constantValue: ConstantValue, + override val parameters: CompileTimeConstant.Parameters +) : CompileTimeConstant { + override val isError: Boolean + get() = constantValue is ErrorValue + + public val type: JetType = constantValue.type + + override fun toConstantValue(expectedType: JetType): ConstantValue = constantValue +} + +public fun ConstantValue.wrap(parameters: CompileTimeConstant.Parameters): TypedCompileTimeConstant + = TypedCompileTimeConstant(this, parameters) + +public fun ConstantValue.wrap( + canBeUsedInAnnotation: Boolean = this !is NullValue, + isPure: Boolean = false, + usesVariableAsConstant: Boolean = false +): TypedCompileTimeConstant + = wrap(CompileTimeConstant.Parameters(canBeUsedInAnnotation, isPure, usesVariableAsConstant)) + +public class IntegerValueTypeConstant( + private val value: Number, + override val parameters: CompileTimeConstant.Parameters +) : CompileTimeConstant { + private val typeConstructor = IntegerValueTypeConstructor(value.toLong()) + + override fun toConstantValue(expectedType: JetType): ConstantValue { + val factory = ConstantValueFactory(KotlinBuiltIns.getInstance()) + return when (getType(expectedType)) { + KotlinBuiltIns.getInstance().getIntType() -> { + factory.createIntValue(value.toInt()) + } + KotlinBuiltIns.getInstance().getByteType() -> { + factory.createByteValue(value.toByte()) + } + KotlinBuiltIns.getInstance().getShortType() -> { + factory.createShortValue(value.toShort()) + } + else -> { + factory.createLongValue(value.toLong()) + } } } -} \ No newline at end of file + + val unknownIntegerType = JetTypeImpl( + Annotations.EMPTY, typeConstructor, false, emptyList(), + ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true) + ) + + public fun getType(expectedType: JetType): JetType = TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType) + + override fun toString() = typeConstructor.toString() +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantFactory.kt deleted file mode 100644 index 2283ad75233..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstantFactory.kt +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.resolve.constants - -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.types.JetType -import org.jetbrains.kotlin.types.TypeUtils - -public class CompileTimeConstantFactory( - private val parameters: CompileTimeConstant.Parameters, - private val builtins: KotlinBuiltIns -) { - fun createLongValue(value: Long) = LongValue(value, parameters, builtins) - - fun createIntValue(value: Int) = IntValue(value, parameters, builtins) - - fun createErrorValue(message: String) = ErrorValue.create(message) - - fun createShortValue(value: Short) = ShortValue(value, parameters, builtins) - - fun createByteValue(value: Byte) = ByteValue(value, parameters, builtins) - - fun createDoubleValue(value: Double) = DoubleValue(value, parameters, builtins) - - fun createFloatValue(value: Float) = FloatValue(value, parameters, builtins) - - fun createBooleanValue(value: Boolean) = BooleanValue(value, parameters, builtins) - - fun createCharValue(value: Char) = CharValue(value, parameters, builtins) - - fun createStringValue(value: String) = StringValue(value, parameters, builtins) - - fun createNullValue() = NullValue(builtins) - - fun createEnumValue(enumEntryClass: ClassDescriptor): EnumValue = EnumValue(enumEntryClass) - - fun createArrayValue( - value: List>, - type: JetType - ) = ArrayValue(value, type, parameters) - - fun createAnnotationValue(value: AnnotationDescriptor) = AnnotationValue(value) - - fun createKClassValue(type: JetType) = KClassValue(type) - - fun createNumberTypeValue(value: Number) = IntegerValueTypeConstant(value, parameters) - - - fun createCompileTimeConstant( - value: Any?, - expectedType: JetType? = null - ): CompileTimeConstant<*>? { - // TODO: primitive arrays - if (expectedType == null) { - when (value) { - is Byte -> return createByteValue(value) - is Short -> return createShortValue(value) - is Int -> return createIntValue(value) - is Long -> return createLongValue(value) - } - } - return when (value) { - is Byte, is Short, is Int, is Long -> getIntegerValue((value as Number).toLong(), expectedType) - is Char -> createCharValue(value) - is Float -> createFloatValue(value) - is Double -> createDoubleValue(value) - is Boolean -> createBooleanValue(value) - is String -> createStringValue(value) - null -> createNullValue() - else -> null - } - } - - private fun getIntegerValue( - value: Long, - expectedType: JetType - ): CompileTimeConstant<*>? { - fun defaultIntegerValue(value: Long) = when (value) { - value.toInt().toLong() -> createIntValue(value.toInt()) - else -> createLongValue(value) - } - - if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) { - return createNumberTypeValue(value) - } - - val notNullExpected = TypeUtils.makeNotNullable(expectedType) - return when { - KotlinBuiltIns.isLong(notNullExpected) -> createLongValue(value) - - KotlinBuiltIns.isShort(notNullExpected) -> - if (value == value.toShort().toLong()) - createShortValue(value.toShort()) - else - defaultIntegerValue(value) - - KotlinBuiltIns.isByte(notNullExpected) -> - if (value == value.toByte().toLong()) - createByteValue(value.toByte()) - else - defaultIntegerValue(value) - - KotlinBuiltIns.isChar(notNullExpected) -> - createIntValue(value.toInt()) - - else -> defaultIntegerValue(value) - } - } -} \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValue.kt new file mode 100644 index 00000000000..fe9a61cfc6b --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValue.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.resolve.constants + +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType + +public abstract class ConstantValue(public open val value: T) { + public abstract val type: JetType + + public abstract fun accept(visitor: AnnotationArgumentVisitor, data: D): R + + override fun toString() = value.toString() + +} \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValueFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValueFactory.kt new file mode 100644 index 00000000000..fddef729080 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ConstantValueFactory.kt @@ -0,0 +1,95 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.resolve.constants + +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.types.JetType +import org.jetbrains.kotlin.types.TypeUtils + +public class ConstantValueFactory( + private val builtins: KotlinBuiltIns +) { + fun createLongValue(value: Long) = LongValue(value, builtins) + + fun createIntValue(value: Int) = IntValue(value, builtins) + + fun createErrorValue(message: String) = ErrorValue.create(message) + + fun createShortValue(value: Short) = ShortValue(value, builtins) + + fun createByteValue(value: Byte) = ByteValue(value, builtins) + + fun createDoubleValue(value: Double) = DoubleValue(value, builtins) + + fun createFloatValue(value: Float) = FloatValue(value, builtins) + + fun createBooleanValue(value: Boolean) = BooleanValue(value, builtins) + + fun createCharValue(value: Char) = CharValue(value, builtins) + + fun createStringValue(value: String) = StringValue(value, builtins) + + fun createNullValue() = NullValue(builtins) + + fun createEnumValue(enumEntryClass: ClassDescriptor): EnumValue = EnumValue(enumEntryClass) + + fun createArrayValue( + value: List>, + type: JetType + ) = ArrayValue(value, type) + + fun createAnnotationValue(value: AnnotationDescriptor) = AnnotationValue(value) + + fun createKClassValue(type: JetType) = KClassValue(type) + + fun createConstantValue( + value: Any? + ): ConstantValue<*>? { + // TODO: primitive arrays + return when (value) { + is Byte -> createByteValue(value) + is Short -> createShortValue(value) + is Int -> createIntValue(value) + is Long -> createLongValue(value) + is Char -> createCharValue(value) + is Float -> createFloatValue(value) + is Double -> createDoubleValue(value) + is Boolean -> createBooleanValue(value) + is String -> createStringValue(value) + null -> createNullValue() + else -> null + } + } + + public fun createIntegerConstantValue( + value: Long, + expectedType: JetType + ): ConstantValue<*>? { + val notNullExpected = TypeUtils.makeNotNullable(expectedType) + return when { + KotlinBuiltIns.isLong(notNullExpected) -> createLongValue(value) + KotlinBuiltIns.isInt(notNullExpected) && value == value.toInt().toLong() -> createIntValue(value.toInt()) + KotlinBuiltIns.isShort(notNullExpected) && value == value.toShort().toLong() -> createShortValue(value.toShort()) + KotlinBuiltIns.isByte(notNullExpected) && value == value.toByte().toLong() -> createByteValue(value.toByte()) + KotlinBuiltIns.isChar(notNullExpected) -> createIntValue(value.toInt()) + else -> null + } + } +} + diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt index 1dcb99816bc..60483c806e6 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt @@ -18,16 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class DoubleValue( value: Double, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : CompileTimeConstant(value, parameters) { - - override fun isPure() = false - +) : ConstantValue(value) { override val type = builtIns.getDoubleType() override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitDoubleValue(this, data) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt index 7c7b6197178..c943df89c3e 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.resolve.descriptorUtil.classObjectType @@ -25,7 +24,7 @@ import org.jetbrains.kotlin.utils.sure public class EnumValue( value: ClassDescriptor -) : CompileTimeConstant(value, CompileTimeConstant.Parameters.Impl(true, false, false)) { +) : ConstantValue(value) { override val type: JetType get() = value.classObjectType.sure { "Enum entry must have a class object type: " + value } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt index 4a0564c6a42..22ec21ffdc5 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt @@ -16,12 +16,10 @@ package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.kotlin.builtins.KotlinBuiltIns 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, CompileTimeConstant.Parameters.Impl(true, false, false)) { +public abstract class ErrorValue : ConstantValue(Unit) { deprecated("Should not be called, for this is not a real value, but a indication of an error") override val value: Unit diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt index bb1e069e44c..a8b5e702e73 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt @@ -18,15 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class FloatValue( value: Float, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : CompileTimeConstant(value, parameters) { - override fun isPure() = false - +) : ConstantValue(value) { override val type = builtIns.getFloatType() override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitFloatValue(this, data) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt index 0649089b15d..aea685df995 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt @@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class IntValue( value: Int, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : IntegerValueConstant(value, parameters) { +) : IntegerValueConstant(value) { override val type = builtIns.getIntType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt index e6501aee9e2..b23941722db 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt @@ -16,7 +16,4 @@ package org.jetbrains.kotlin.resolve.constants -public abstract class IntegerValueConstant protected constructor( - value: T, - parameters: CompileTimeConstant.Parameters -) : CompileTimeConstant(value, parameters) +public abstract class IntegerValueConstant protected constructor(value: T) : ConstantValue(value) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt deleted file mode 100644 index 054b47dc49e..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.resolve.constants - -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.types.* - -import java.util.Collections - -public class IntegerValueTypeConstant( - value: Number, - parameters: CompileTimeConstant.Parameters -) : IntegerValueConstant(value, parameters) { - - override fun isPure() = true - - private val typeConstructor = IntegerValueTypeConstructor(value.toLong()) - - override val type = JetTypeImpl( - Annotations.EMPTY, typeConstructor, false, emptyList() - , ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true) - ) - - deprecated("") - override val value: Number - get() = throw UnsupportedOperationException("Use IntegerValueTypeConstant.getValue(expectedType) instead") - - public fun getType(expectedType: JetType): JetType = TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType) - - public fun getValue(expectedType: JetType): Number { - val numberValue = typeConstructor.getValue() - val builtIns = KotlinBuiltIns.getInstance() - - val valueType = getType(expectedType) - if (valueType == builtIns.getIntType()) { - return numberValue.toInt() - } - else if (valueType == builtIns.getByteType()) { - return numberValue.toByte() - } - else if (valueType == builtIns.getShortType()) { - return numberValue.toShort() - } - else { - return numberValue.toLong() - } - } - - override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitNumberTypeValue(this, data) - - override fun toString() = typeConstructor.toString() -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt index a3eedd4e0b8..b31f7491743 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt @@ -16,12 +16,11 @@ package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.types.JetType public class KClassValue(override val type: JetType) : - CompileTimeConstant(type, CompileTimeConstant.Parameters.Impl(true, false, false)) { + ConstantValue(type) { override val value: JetType get() = type.getArguments().single().getType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt index 24811e33a6e..74eb173a3ce 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt @@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class LongValue( value: Long, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : IntegerValueConstant(value, parameters) { +) : IntegerValueConstant(value) { override val type = builtIns.getLongType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt index 7676cdcdca7..b8f25749dc5 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt @@ -18,11 +18,10 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class NullValue( builtIns: KotlinBuiltIns -) : CompileTimeConstant(null, CompileTimeConstant.Parameters.Impl(false, false, false)) { +) : ConstantValue(null) { override val type = builtIns.getNullableNothingType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt index 45b6c88a745..04f0236d4c3 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt @@ -18,13 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class ShortValue( value: Short, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : IntegerValueConstant(value, parameters) { +) : IntegerValueConstant(value) { override val type = builtIns.getShortType() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt index 530e310f9d1..ccda72185ce 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt @@ -18,15 +18,11 @@ package org.jetbrains.kotlin.resolve.constants import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor -import org.jetbrains.kotlin.types.JetType public class StringValue( value: String, - parameters: CompileTimeConstant.Parameters, builtIns: KotlinBuiltIns -) : CompileTimeConstant(value, parameters) { - override fun isPure() = false - +) : ConstantValue(value) { override val type = builtIns.getStringType() override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitStringValue(this, data) diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/AnnotationDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/AnnotationDeserializer.kt index 3773495ecc1..475a9e34f8b 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/AnnotationDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/AnnotationDeserializer.kt @@ -16,22 +16,24 @@ package org.jetbrains.kotlin.serialization.deserialization -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.constants.AnnotationValue +import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.ConstantValueFactory import org.jetbrains.kotlin.serialization.ProtoBuf.Annotation import org.jetbrains.kotlin.serialization.ProtoBuf.Annotation.Argument import org.jetbrains.kotlin.serialization.ProtoBuf.Annotation.Argument.Value import org.jetbrains.kotlin.serialization.ProtoBuf.Annotation.Argument.Value.Type -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.* -import org.jetbrains.kotlin.types.JetType -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.types.ErrorUtils -import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf @@ -39,7 +41,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) { private val builtIns: KotlinBuiltIns get() = module.builtIns - private val factory = CompileTimeConstantFactory(CompileTimeConstant.Parameters.ThrowException, builtIns) + private val factory = ConstantValueFactory(builtIns) public fun deserializeAnnotation(proto: Annotation, nameResolver: NameResolver): AnnotationDescriptor { val annotationClass = resolveClass(nameResolver.getClassId(proto.getId())) @@ -60,7 +62,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) { proto: Argument, parameterByName: Map, nameResolver: NameResolver - ): Pair>? { + ): Pair>? { val parameter = parameterByName[nameResolver.getName(proto.getNameId())] ?: return null return Pair(parameter, resolveValue(parameter.getType(), proto.getValue(), nameResolver)) } @@ -69,8 +71,8 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) { expectedType: JetType, value: Value, nameResolver: NameResolver - ): CompileTimeConstant<*> { - val result = when (value.getType()) { + ): ConstantValue<*> { + val result: ConstantValue<*> = when (value.getType()) { Type.BYTE -> factory.createByteValue(value.getIntValue().toByte()) Type.CHAR -> factory.createCharValue(value.getIntValue().toChar()) Type.SHORT -> factory.createShortValue(value.getIntValue().toShort()) @@ -131,7 +133,7 @@ public class AnnotationDeserializer(private val module: ModuleDescriptor) { } // NOTE: see analogous code in BinaryClassAnnotationAndConstantLoaderImpl - private fun resolveEnumValue(enumClassId: ClassId, enumEntryName: Name): CompileTimeConstant<*> { + private fun resolveEnumValue(enumClassId: ClassId, enumEntryName: Name): ConstantValue<*> { val enumClass = resolveClass(enumClassId) if (enumClass.getKind() == ClassKind.ENUM_CLASS) { val enumEntry = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(enumEntryName) diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MemberDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MemberDeserializer.kt index 532075e94b4..694f079c0b2 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MemberDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MemberDeserializer.kt @@ -16,14 +16,22 @@ package org.jetbrains.kotlin.serialization.deserialization -import org.jetbrains.kotlin.serialization.* -import org.jetbrains.kotlin.serialization.deserialization.descriptors.* import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.descriptors.impl.* +import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl +import org.jetbrains.kotlin.descriptors.impl.PropertyGetterDescriptorImpl +import org.jetbrains.kotlin.descriptors.impl.PropertySetterDescriptorImpl +import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.resolve.DescriptorFactory +import org.jetbrains.kotlin.serialization.Flags import org.jetbrains.kotlin.serialization.ProtoBuf.Callable -import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.* +import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.FUN +import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAL +import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAR +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotations +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedSimpleFunctionDescriptor public class MemberDeserializer(private val c: DeserializationContext) { public fun loadCallable(proto: Callable): CallableMemberDescriptor { diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/context.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/context.kt index 002cf06197e..bdd5fdf394d 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/context.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/context.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.serialization.deserialization import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant +import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.storage.StorageManager @@ -27,7 +27,7 @@ public class DeserializationComponents( public val storageManager: StorageManager, public val moduleDescriptor: ModuleDescriptor, public val classDataFinder: ClassDataFinder, - public val annotationAndConstantLoader: AnnotationAndConstantLoader>, + public val annotationAndConstantLoader: AnnotationAndConstantLoader>, public val packageFragmentProvider: PackageFragmentProvider, public val localClassResolver: LocalClassResolver, public val flexibleTypeCapabilitiesDeserializer: FlexibleTypeCapabilitiesDeserializer, diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/DeserializerForDecompilerBase.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/DeserializerForDecompilerBase.kt index e5165daf03b..badad22cf71 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/DeserializerForDecompilerBase.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/textBuilder/DeserializerForDecompilerBase.kt @@ -27,7 +27,7 @@ import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant +import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.serialization.deserialization.AnnotationAndConstantLoader import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents @@ -43,7 +43,7 @@ public abstract class DeserializerForDecompilerBase( protected abstract val classDataFinder: ClassDataFinder - protected abstract val annotationAndConstantLoader: AnnotationAndConstantLoader> + protected abstract val annotationAndConstantLoader: AnnotationAndConstantLoader> protected val storageManager: StorageManager = LockBasedStorageManager.NO_LOCKS diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt index 01afc5eb16e..ca5a53a417a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator public class ConvertToStringTemplateInspection : IntentionBasedInspection( @@ -90,13 +89,7 @@ public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependen is JetConstantExpression -> { val bindingContext = expression.analyze() val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext) - if (constant is IntegerValueTypeConstant) { - val type = bindingContext.getType(expression)!! - constant.getValue(type).toString() - } - else { - constant?.value.toString() - } + constant?.getValue(bindingContext.getType(expression)!!).toString() } is JetStringTemplateExpression -> { diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt index de5fabeb1e3..9cb24cfde40 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt @@ -20,37 +20,28 @@ import com.google.dart.compiler.backend.js.ast.JsFunctionScope import com.google.dart.compiler.backend.js.ast.JsProgram import com.google.dart.compiler.backend.js.ast.JsRootScope import com.google.gwt.dev.js.parserExceptions.AbortParsingException -import com.google.gwt.dev.js.rhino.* -import com.google.gwt.dev.js.rhino.Utils.* -import org.jetbrains.annotations.TestOnly +import com.google.gwt.dev.js.rhino.CodePosition +import com.google.gwt.dev.js.rhino.ErrorReporter +import com.google.gwt.dev.js.rhino.Utils.isEndOfLine +import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1 -import org.jetbrains.kotlin.diagnostics.DiagnosticSink -import org.jetbrains.kotlin.diagnostics.ParametrizedDiagnostic +import org.jetbrains.kotlin.js.parser.parse import org.jetbrains.kotlin.js.patterns.DescriptorPredicate import org.jetbrains.kotlin.js.patterns.PatternBuilder -import org.jetbrains.kotlin.js.resolve.diagnostics.ErrorsJs import org.jetbrains.kotlin.psi.JetCallExpression import org.jetbrains.kotlin.psi.JetExpression import org.jetbrains.kotlin.psi.JetLiteralStringTemplateEntry import org.jetbrains.kotlin.psi.JetStringTemplateExpression -import org.jetbrains.kotlin.renderer.Renderer -import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingTrace +import org.jetbrains.kotlin.resolve.TemporaryBindingTrace import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator -import org.jetbrains.kotlin.types.JetType - -import com.intellij.openapi.util.TextRange -import org.jetbrains.kotlin.js.parser.parse -import java.io.StringReader - import kotlin.platform.platformStatic -import org.jetbrains.kotlin.resolve.TemporaryBindingTrace public class JsCallChecker : CallChecker { @@ -84,7 +75,7 @@ public class JsCallChecker : CallChecker { return } - val code = evaluationResult.value as String + val code = evaluationResult.getValue(stringType) as String val errorReporter = JsCodeErrorReporter(argument, code, context.trace) try { diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java b/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java index ac87a14892d..88217baddd6 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java @@ -27,7 +27,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.kotlin.js.PredefinedAnnotation; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.resolve.DescriptorUtils; -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; +import org.jetbrains.kotlin.resolve.constants.ConstantValue; import java.util.List; import java.util.Set; @@ -53,7 +53,7 @@ public final class AnnotationsUtils { if (annotationDescriptor.getAllValueArguments().isEmpty()) { return null; } - CompileTimeConstant constant = annotationDescriptor.getAllValueArguments().values().iterator().next(); + ConstantValue constant = annotationDescriptor.getAllValueArguments().values().iterator().next(); //TODO: this is a quick fix for unsupported default args problem if (constant == null) { return null; diff --git a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptAnnotationAndConstantLoader.kt b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptAnnotationAndConstantLoader.kt index 33e9db38c2f..880df62257e 100644 --- a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptAnnotationAndConstantLoader.kt +++ b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptAnnotationAndConstantLoader.kt @@ -18,14 +18,14 @@ package org.jetbrains.kotlin.serialization.js import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant +import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.* import org.jetbrains.kotlin.types.JetType class KotlinJavascriptAnnotationAndConstantLoader( module: ModuleDescriptor -) : AnnotationAndConstantLoader> { +) : AnnotationAndConstantLoader> { private val deserializer = AnnotationDeserializer(module) override fun loadClassAnnotations( @@ -67,7 +67,7 @@ class KotlinJavascriptAnnotationAndConstantLoader( proto: ProtoBuf.Callable, nameResolver: NameResolver, expectedType: JetType - ): CompileTimeConstant<*>? { + ): ConstantValue<*>? { val value = proto.getExtension(JsProtoBuf.compileTimeValue) return deserializer.resolveValue(expectedType, value, nameResolver) } diff --git a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt index e680b560895..2f7271c7281 100644 --- a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt +++ b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.resolve.constants.NullValue -import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.serialization.AnnotationSerializer import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.SerializerExtension @@ -48,10 +47,10 @@ public object KotlinJavascriptSerializerExtension : SerializerExtension() { proto.addExtension(JsProtoBuf.callableAnnotation, annotationSerializer.serializeAnnotation(annotation, stringTable)) } val propertyDescriptor = callable as? PropertyDescriptor ?: return - val compileTimeConstant = propertyDescriptor.getCompileTimeInitializer() - if (compileTimeConstant != null && compileTimeConstant !is NullValue) { - val type = compileTimeConstant.type - proto.setExtension(JsProtoBuf.compileTimeValue, annotationSerializer.valueProto(compileTimeConstant, type, stringTable).build()) + val constantInitializer = propertyDescriptor.getCompileTimeInitializer() + if (constantInitializer != null && constantInitializer !is NullValue) { + val type = constantInitializer.type + proto.setExtension(JsProtoBuf.compileTimeValue, annotationSerializer.valueProto(constantInitializer, type, stringTable).build()) } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java index 951c2deb0d0..e1aae4567b7 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContextUtils; import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilPackage; 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.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.inline.InlineUtil; @@ -75,14 +76,13 @@ public final class ExpressionVisitor extends TranslatorVisitor { @NotNull private static JsNode translateConstantExpression(@NotNull JetConstantExpression expression, @NotNull TranslationContext context) { CompileTimeConstant compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext()); - assert compileTimeValue != null : message(expression, "Expression is not compile time value: " + expression.getText() + " "); - - if (compileTimeValue instanceof NullValue) { + JetType expectedType = context.bindingContext().getType(expression); + ConstantValue constant = compileTimeValue.toConstantValue(expectedType != null ? expectedType : TypeUtils.NO_EXPECTED_TYPE); + if (constant instanceof NullValue) { return JsLiteral.NULL; } - - Object value = getCompileTimeValue(context.bindingContext(), expression, compileTimeValue); + Object value = constant.getValue(); if (value instanceof Integer || value instanceof Short || value instanceof Byte) { return context.program().getNumberLiteral(((Number) value).intValue()); } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/UnaryOperationTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/UnaryOperationTranslator.java index 951a1344628..ac8f2397703 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/UnaryOperationTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/UnaryOperationTranslator.java @@ -29,7 +29,6 @@ import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.psi.JetConstantExpression; import org.jetbrains.kotlin.psi.JetExpression; import org.jetbrains.kotlin.psi.JetUnaryExpression; -import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContextUtils; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallExpressionTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallExpressionTranslator.java index 5d6fc2fa0ef..950d877fed8 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallExpressionTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallExpressionTranslator.java @@ -130,7 +130,7 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl CompileTimeConstant constant = ConstantExpressionEvaluator.evaluate(jsCodeExpression, bindingTrace, stringType); assert constant != null: "jsCode must be compile time string " + jsCodeExpression; - String jsCode = (String) constant.getValue(); + String jsCode = (String) constant.getValue(stringType); assert jsCode != null: jsCodeExpression.toString(); // Parser can change local or global scope. diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/BindingUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/BindingUtils.java index e54e943e65a..68bf898c4f2 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/BindingUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/BindingUtils.java @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; -import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.TypeUtils; @@ -159,14 +158,8 @@ public final class BindingUtils { @Nullable public static Object getCompileTimeValue(@NotNull BindingContext context, @NotNull JetExpression expression, @NotNull CompileTimeConstant constant) { - if (constant != null) { - if (constant instanceof IntegerValueTypeConstant) { - JetType expectedType = context.getType(expression); - return ((IntegerValueTypeConstant) constant).getValue(expectedType == null ? TypeUtils.NO_EXPECTED_TYPE : expectedType); - } - return constant.getValue(); - } - return null; + JetType expectedType = context.getType(expression); + return constant.getValue(expectedType == null ? TypeUtils.NO_EXPECTED_TYPE : expectedType); } @NotNull