Use one method to create compile-time value

This commit is contained in:
Natalia Ukhorskaya
2014-02-19 12:34:46 +04:00
parent 201af666a9
commit a80114d720
8 changed files with 86 additions and 187 deletions
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.constants.ConstantUtils;
import org.jetbrains.jet.lang.resolve.constants.ConstantsPackage;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.java.structure.JavaPropertyInitializerEvaluator;
@@ -34,7 +34,7 @@ public class JavaPropertyInitializerEvaluatorImpl extends JavaPropertyInitialize
PsiExpression initializer = ((JavaFieldImpl) field).getInitializer();
Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false);
if (evaluatedExpression != null) {
return ConstantUtils.createCompileTimeConstant(
return ConstantsPackage.createCompileTimeConstant(
evaluatedExpression,
DescriptorUtils.isPropertyCompileTimeConstant(descriptor),
false,
@@ -540,7 +540,7 @@ private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? {
}
private fun createCompileTimeConstant(value: Any?, c: EvaluatorContext, expectedType: JetType? = null): CompileTimeConstant<*>? {
return ConstantUtils.createCompileTimeConstant(value, c.canBeUsedInAnnotation, c.isPure, expectedType)
return createCompileTimeConstant(value, c.canBeUsedInAnnotation, c.isPure, expectedType)
}
fun isIntegerType(value: Any?) = value is Byte || value is Short || value is Int || value is Long
@@ -48,7 +48,6 @@ import org.jetbrains.jet.lang.resolve.java.lazy.types.toAttributes
import org.jetbrains.jet.renderer.DescriptorRenderer
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap
import org.jetbrains.jet.lang.types.TypeUtils
import org.jetbrains.jet.lang.resolve.java.resolver.resolveCompileTimeConstantValue
private object DEPRECATED_IN_JAVA : JavaLiteralAnnotationArgument {
override fun getName(): Name? = null
@@ -109,7 +108,7 @@ class LazyJavaAnnotationDescriptor(
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? {
return when (argument) {
is JavaLiteralAnnotationArgument -> ConstantUtils.createCompileTimeConstant(argument.getValue(), true, false, null)
is JavaLiteralAnnotationArgument -> createCompileTimeConstant(argument.getValue(), true, false, null)
is JavaReferenceAnnotationArgument -> resolveFromReference(argument.resolve())
is JavaArrayAnnotationArgument -> resolveFromArray(argument.getName() ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
@@ -1,45 +0,0 @@
/*
* Copyright 2010-2014 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.jet.lang.resolve.java.resolver
import org.jetbrains.jet.lang.resolve.constants.*
import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
fun resolveCompileTimeConstantValue(value: Any?, canBeUsedInAnnotations: Boolean, expectedType: JetType?): CompileTimeConstant<*>? {
return when (value) {
is String -> StringValue(value, canBeUsedInAnnotations)
is Byte -> ByteValue(value, canBeUsedInAnnotations, false)
is Short -> ShortValue(value, canBeUsedInAnnotations, false)
is Char -> CharValue(value, canBeUsedInAnnotations, false)
is Int -> {
val builtIns = KotlinBuiltIns.getInstance()
when (expectedType) {
builtIns.getShortType() -> ShortValue(value.toShort(), canBeUsedInAnnotations, false)
builtIns.getByteType() -> ByteValue(value.toByte(), canBeUsedInAnnotations, false)
builtIns.getCharType() -> CharValue(value.toChar(), canBeUsedInAnnotations, false)
else -> IntValue(value, canBeUsedInAnnotations, false)
}
}
is Long -> LongValue(value, canBeUsedInAnnotations, false)
is Float -> FloatValue(value, canBeUsedInAnnotations)
is Double -> DoubleValue(value, canBeUsedInAnnotations)
is Boolean -> BooleanValue(value, canBeUsedInAnnotations)
null -> NullValue.NULL
else -> null
}
}
@@ -27,15 +27,11 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationsImpl;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.constants.ConstantUtils;
import org.jetbrains.jet.lang.resolve.constants.EnumValue;
import org.jetbrains.jet.lang.resolve.constants.ErrorValue;
import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils;
import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
import org.jetbrains.jet.lang.resolve.java.resolver.ResolverPackage;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.DependencyClassByQualifiedNameResolver;
import org.jetbrains.jet.lang.types.ErrorUtils;
@@ -135,7 +131,7 @@ public class AnnotationDescriptorDeserializer extends BaseDescriptorDeserializer
@Override
public void visit(@Nullable Name name, @Nullable Object value) {
if (name != null) {
CompileTimeConstant<?> argument = ConstantUtils.createCompileTimeConstant(value, true, false, null);
CompileTimeConstant<?> argument = ConstantsPackage.createCompileTimeConstant(value, true, false, null);
setArgumentValueByName(name, argument != null ? argument : ErrorValue.create("Unsupported annotation argument: " + name));
}
}
@@ -88,7 +88,7 @@ public class DescriptorDeserializersStorage {
public KotlinJvmBinaryClass.AnnotationVisitor visitField(@NotNull Name name, @NotNull String desc, @Nullable Object initializer) {
MemberSignature signature = MemberSignature.fromFieldNameAndDesc(name, desc);
if (initializer != null) {
propertyConstants.put(signature, ConstantUtils.createCompileTimeConstant(
propertyConstants.put(signature, ConstantsPackage.createCompileTimeConstant(
initializer, /* canBeUsedInAnnotation */ true, /* isPureIntConstant */ true, /* expectedType */ null));
}
return new MemberAnnotationVisitor(signature);
@@ -1,130 +0,0 @@
/*
* Copyright 2010-2014 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.jet.lang.resolve.constants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class ConstantUtils {
@Nullable
private static CompileTimeConstant<?> getIntegerValue(
long value,
boolean canBeUsedInAnnotations,
boolean isPureIntConstant,
@NotNull JetType expectedType
) {
if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) {
return new IntegerValueTypeConstant(value, canBeUsedInAnnotations);
}
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
JetType notNullExpectedType = TypeUtils.makeNotNullable(expectedType);
if (notNullExpectedType.equals(builtIns.getLongType())) {
return new LongValue(value, canBeUsedInAnnotations, isPureIntConstant);
}
else if (notNullExpectedType.equals(builtIns.getShortType())) {
if (value == (long) ((short) value)) {
return new ShortValue((short) value, canBeUsedInAnnotations, isPureIntConstant);
}
return getDefaultIntegerValue(value, canBeUsedInAnnotations, isPureIntConstant);
}
if (notNullExpectedType.equals(builtIns.getByteType())) {
if (value == (long) ((byte) value)) {
return new ByteValue((byte) value, canBeUsedInAnnotations, isPureIntConstant);
}
return getDefaultIntegerValue(value, canBeUsedInAnnotations, isPureIntConstant);
}
else {
return getDefaultIntegerValue(value, canBeUsedInAnnotations, isPureIntConstant);
}
}
@NotNull
private static CompileTimeConstant<?> getDefaultIntegerValue(
long value, boolean canBeUsedInAnnotations,
boolean isPureIntConstant
) {
if (value == (long) ((int) value)) {
return new IntValue((int) value, canBeUsedInAnnotations, isPureIntConstant);
}
else {
return new LongValue(value, canBeUsedInAnnotations, isPureIntConstant);
}
}
@Nullable
public static CompileTimeConstant<?> createCompileTimeConstant(
@Nullable Object value,
boolean canBeUsedInAnnotations,
boolean isPureIntConstant,
@Nullable JetType expectedType
) {
if (expectedType == null) {
if (value instanceof Integer) {
return new IntValue((Integer) value, canBeUsedInAnnotations, isPureIntConstant);
}
else if (value instanceof Long) {
return new LongValue((Long) value, canBeUsedInAnnotations, isPureIntConstant);
}
else if (value instanceof Byte) {
return new ByteValue((Byte) value, canBeUsedInAnnotations, isPureIntConstant);
}
else if (value instanceof Short) {
return new ShortValue((Short) value, canBeUsedInAnnotations, isPureIntConstant);
}
}
if (value instanceof Integer) {
return getIntegerValue((Integer) value, canBeUsedInAnnotations, isPureIntConstant, expectedType);
}
else if (value instanceof Long) {
return getIntegerValue((Long) value, canBeUsedInAnnotations, isPureIntConstant, expectedType);
}
else if (value instanceof Byte) {
return getIntegerValue((Byte) value, canBeUsedInAnnotations, isPureIntConstant, expectedType);
}
else if (value instanceof Short) {
return getIntegerValue((Short) value, canBeUsedInAnnotations, isPureIntConstant, expectedType);
}
else if (value instanceof Character) {
return new CharValue((Character) value, canBeUsedInAnnotations, isPureIntConstant);
}
else if (value instanceof Float) {
return new FloatValue((Float) value, canBeUsedInAnnotations);
}
else if (value instanceof Double) {
return new DoubleValue((Double) value, canBeUsedInAnnotations);
}
else if (value instanceof Boolean) {
return new BooleanValue((Boolean) value, canBeUsedInAnnotations);
}
else if (value instanceof String) {
return new StringValue((String) value, canBeUsedInAnnotations);
}
else if (value == null) {
return NullValue.NULL;
}
return null;
}
private ConstantUtils() {
}
}
@@ -0,0 +1,79 @@
/*
* Copyright 2010-2014 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.jet.lang.resolve.constants
import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
import org.jetbrains.jet.lang.types.TypeUtils
public fun createCompileTimeConstant(
value: Any?,
canBeUsedInAnnotation: Boolean,
isPureIntConstant: Boolean,
expectedType: JetType? = null
): CompileTimeConstant<*>? {
if (expectedType == null) {
when(value) {
is Byte -> return ByteValue(value, canBeUsedInAnnotation, isPureIntConstant)
is Short -> return ShortValue(value, canBeUsedInAnnotation, isPureIntConstant)
is Int -> return IntValue(value, canBeUsedInAnnotation, isPureIntConstant)
is Long -> return LongValue(value, canBeUsedInAnnotation, isPureIntConstant)
}
}
return when(value) {
is Byte, is Short, is Int, is Long -> getIntegerValue((value as Number).toLong(), canBeUsedInAnnotation, isPureIntConstant, expectedType)
is Char -> CharValue(value, canBeUsedInAnnotation, isPureIntConstant)
is Float -> FloatValue(value, canBeUsedInAnnotation)
is Double -> DoubleValue(value, canBeUsedInAnnotation)
is Boolean -> BooleanValue(value, canBeUsedInAnnotation)
is String -> StringValue(value, canBeUsedInAnnotation)
null -> NullValue.NULL
else -> null
}
}
private fun getIntegerValue(
value: Long,
canBeUsedInAnnotation: Boolean,
isPureIntConstant: Boolean,
expectedType: JetType
): CompileTimeConstant<*>? {
fun defaultIntegerValue(value: Long) = when (value) {
value.toInt().toLong() -> IntValue(value.toInt(), canBeUsedInAnnotation, isPureIntConstant)
else -> LongValue(value, canBeUsedInAnnotation, isPureIntConstant)
}
if (TypeUtils.noExpectedType(expectedType) || expectedType.isError()) {
return IntegerValueTypeConstant(value, canBeUsedInAnnotation)
}
val builtIns = KotlinBuiltIns.getInstance()
return when (TypeUtils.makeNotNullable(expectedType)) {
builtIns.getLongType() -> LongValue(value, canBeUsedInAnnotation, isPureIntConstant)
builtIns.getShortType() -> when (value) {
value.toShort().toLong() -> ShortValue(value.toShort(), canBeUsedInAnnotation, isPureIntConstant)
else -> defaultIntegerValue(value)
}
builtIns.getByteType() -> when (value) {
value.toByte().toLong() -> ByteValue(value.toByte(), canBeUsedInAnnotation, isPureIntConstant)
else -> defaultIntegerValue(value)
}
builtIns.getCharType() -> IntValue(value.toInt(), canBeUsedInAnnotation, isPureIntConstant)
else -> defaultIntegerValue(value)
}
}