Support equlas in ConstantExpressionEvaluator
This commit is contained in:
+44
-28
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.jet.lang.evaluate;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
@@ -184,13 +183,24 @@ public class ConstantExpressionEvaluator extends JetVisitor<CompileTimeConstant<
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
}
|
||||
else {
|
||||
return getCallConstant(expression.getOperationReference(), leftExpression);
|
||||
Object result = evaluateCall(expression.getOperationReference(), leftExpression);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
if (OperatorConventions.COMPARISON_OPERATIONS.contains(operationToken)) {
|
||||
return createCompileTimeConstantForCompareTo(result, operationToken);
|
||||
}
|
||||
else if (OperatorConventions.EQUALS_OPERATIONS.contains(operationToken)) {
|
||||
return createCompileTimeConstantForEquals(result, operationToken);
|
||||
}
|
||||
else {
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private CompileTimeConstant<?> getCallConstant(@NotNull JetExpression resolvedCallExpression, @NotNull JetExpression receiverExpression) {
|
||||
private Object evaluateCall(@NotNull JetExpression resolvedCallExpression, @NotNull JetExpression receiverExpression) {
|
||||
ResolvedCall<?> resolvedCall = trace.getBindingContext().get(BindingContext.RESOLVED_CALL, resolvedCallExpression);
|
||||
if (resolvedCall != null) {
|
||||
CallableDescriptor resultingDescriptor = resolvedCall.getResultingDescriptor();
|
||||
@@ -209,42 +219,40 @@ public class ConstantExpressionEvaluator extends JetVisitor<CompileTimeConstant<
|
||||
}
|
||||
Name resultingDescriptorName = resultingDescriptor.getName();
|
||||
if (arguments.isEmpty()) {
|
||||
Object result = EvaluatePackage.evaluateUnaryExpression(receiverValue, resultingDescriptorName);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
return EvaluatePackage.evaluateUnaryExpression(receiverValue, resultingDescriptorName);
|
||||
}
|
||||
else if (arguments.size() == 1) {
|
||||
Object result = EvaluatePackage.evaluateBinaryExpression(receiverValue, arguments.iterator().next(), resultingDescriptorName);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
if (OperatorConventions.COMPARE_TO.equals(resultingDescriptorName)) {
|
||||
PsiElement parentExpression = resolvedCallExpression.getParent();
|
||||
if (parentExpression instanceof JetBinaryExpression) {
|
||||
return createCompileTimeConstantForCompareTo(result, (JetBinaryExpression) parentExpression);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
}
|
||||
return EvaluatePackage.evaluateBinaryExpression(receiverValue, arguments.iterator().next(), resultingDescriptorName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static CompileTimeConstant<?> createCompileTimeConstantForEquals(
|
||||
@NotNull Object result,
|
||||
@NotNull IElementType operationToken
|
||||
) {
|
||||
if (result instanceof Boolean) {
|
||||
boolean resultCode = ((Boolean) result).booleanValue();
|
||||
if (operationToken == JetTokens.EQEQ) {
|
||||
return resultCode ? BooleanValue.TRUE : BooleanValue.FALSE;
|
||||
}
|
||||
else if (operationToken == JetTokens.EXCLEQ) {
|
||||
return resultCode ? BooleanValue.FALSE : BooleanValue.TRUE;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static CompileTimeConstant<?> createCompileTimeConstantForCompareTo(
|
||||
@NotNull Object result,
|
||||
@NotNull JetBinaryExpression expression
|
||||
@NotNull IElementType operationToken
|
||||
) {
|
||||
if (result instanceof Integer) {
|
||||
int resultCode = ((Integer) result).intValue();
|
||||
IElementType operationToken = expression.getOperationToken();
|
||||
if (resultCode == 0) {
|
||||
if (operationToken == JetTokens.EQEQ ||
|
||||
operationToken == JetTokens.LTEQ ||
|
||||
@@ -283,7 +291,11 @@ public class ConstantExpressionEvaluator extends JetVisitor<CompileTimeConstant<
|
||||
if (leftExpression == null) {
|
||||
return null;
|
||||
}
|
||||
return getCallConstant(expression.getOperationReference(), leftExpression);
|
||||
Object result = evaluateCall(expression.getOperationReference(), leftExpression);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
}
|
||||
|
||||
public static CompileTimeConstant<?> createCompileTimeConstant(@NotNull Object value, @NotNull JetType expectedType) {
|
||||
@@ -403,7 +415,11 @@ public class ConstantExpressionEvaluator extends JetVisitor<CompileTimeConstant<
|
||||
}
|
||||
|
||||
if (((JetCallExpression) selectorExpression).getValueArguments().size() < 2) {
|
||||
return getCallConstant(calleeExpression, receiverExpression);
|
||||
Object result = evaluateCall(calleeExpression, receiverExpression);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return createCompileTimeConstant(result, expectedType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+52
-1
@@ -154,6 +154,7 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(STRING, FLOAT, "plus", { a, b -> a + b }),
|
||||
bOp(STRING, CHAR, "plus", { a, b -> a + b }),
|
||||
bOp(STRING, STRING, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(STRING, STRING, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Boolean
|
||||
bOp(BOOLEAN, BOOLEAN, "and", { a, b -> a and b }),
|
||||
@@ -161,6 +162,7 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(BOOLEAN, BOOLEAN, "xor", { a, b -> a xor b }),
|
||||
bOp(BOOLEAN, BOOLEAN, "andand", { a, b -> a && b }),
|
||||
bOp(BOOLEAN, BOOLEAN, "oror", { a, b -> a || b }),
|
||||
bOp(BOOLEAN, BOOLEAN, "equals", { a, b -> a == b }),
|
||||
|
||||
// Byte
|
||||
bOp(BYTE, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -205,6 +207,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(BYTE, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(BYTE, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(BYTE, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(BYTE, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(BYTE, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Short
|
||||
bOp(SHORT, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -249,6 +258,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(SHORT, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(SHORT, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(SHORT, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(SHORT, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(SHORT, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Int
|
||||
bOp(INT, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -299,6 +315,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(INT, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(INT, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(INT, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(INT, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(INT, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Long
|
||||
bOp(LONG, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -349,6 +372,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(LONG, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(LONG, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(LONG, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(LONG, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(LONG, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Double
|
||||
bOp(DOUBLE, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -392,6 +422,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(DOUBLE, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(DOUBLE, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(DOUBLE, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(DOUBLE, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(DOUBLE, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Float
|
||||
bOp(FLOAT, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -436,6 +473,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(FLOAT, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(FLOAT, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(FLOAT, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(FLOAT, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(FLOAT, CHAR, "equals", { a, b -> a.equals(b) }),
|
||||
|
||||
// Char
|
||||
bOp(CHAR, DOUBLE, "plus", { a, b -> a + b }),
|
||||
@@ -475,6 +519,13 @@ private val binaryOperations = hashMapOf<BinaryOperation<*, *>, (Any?, Any?) ->
|
||||
bOp(CHAR, INT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(CHAR, SHORT, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(CHAR, BYTE, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(CHAR, CHAR, "compareTo", { a, b -> a.compareTo(b) })
|
||||
bOp(CHAR, CHAR, "compareTo", { a, b -> a.compareTo(b) }),
|
||||
bOp(CHAR, DOUBLE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, FLOAT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, LONG, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, INT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, SHORT, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, BYTE, "equals", { a, b -> a.equals(b) }),
|
||||
bOp(CHAR, CHAR, "equals", { a, b -> a.equals(b) })
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package test
|
||||
|
||||
annotation class Ann(
|
||||
val b1: Boolean,
|
||||
val b2: Boolean,
|
||||
val b3: Boolean,
|
||||
val b4: Boolean,
|
||||
val b5: Boolean
|
||||
)
|
||||
|
||||
Ann(1 == 2, 1.0 == 2.0, 'b' == 'a', "a" == "b", "a" == "a") class MyClass
|
||||
|
||||
// EXPECTED: Ann[b1 = false: jet.Boolean, b2 = false: jet.Boolean, b3 = false: jet.Boolean, b4 = false: jet.Boolean, b5 = true: jet.Boolean]
|
||||
@@ -0,0 +1,13 @@
|
||||
package test
|
||||
|
||||
annotation class Ann(
|
||||
val b1: Boolean,
|
||||
val b2: Boolean,
|
||||
val b3: Boolean,
|
||||
val b4: Boolean,
|
||||
val b5: Boolean
|
||||
)
|
||||
|
||||
Ann(1 != 2, 1.0 != 2.0, 'b' != 'a', "a" != "b", "a" != "a") class MyClass
|
||||
|
||||
// EXPECTED: Ann[b1 = true: jet.Boolean, b2 = true: jet.Boolean, b3 = true: jet.Boolean, b4 = true: jet.Boolean, b5 = false: jet.Boolean]
|
||||
+5
@@ -203,6 +203,11 @@ public class AnnotationParameterTestGenerated extends AbstractAnnotationParamete
|
||||
doTest("compiler/testData/resolveAnnotations/parameters/expressions/not.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noteq.kt")
|
||||
public void testNoteq() throws Exception {
|
||||
doTest("compiler/testData/resolveAnnotations/parameters/expressions/noteq.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("orOr.kt")
|
||||
public void testOrOr() throws Exception {
|
||||
doTest("compiler/testData/resolveAnnotations/parameters/expressions/orOr.kt");
|
||||
|
||||
Reference in New Issue
Block a user