Sort out JVM intrinsics for equals, hashCode, toString

This commit is contained in:
Alexander Udalov
2014-02-07 15:49:28 +04:00
parent 3dcd85bdb4
commit 774e5f0535
4 changed files with 81 additions and 65 deletions
@@ -58,12 +58,10 @@ public class IntrinsicMethods {
private static final ArraySet ARRAY_SET = new ArraySet();
private static final ArrayGet ARRAY_GET = new ArrayGet();
private static final StringPlus STRING_PLUS = new StringPlus();
public static final String KOTLIN_JAVA_CLASS_FUNCTION = "kotlin.javaClass.function";
private static final String KOTLIN_JAVA_CLASS_FUNCTION = "kotlin.javaClass.function";
private static final String KOTLIN_JAVA_CLASS_PROPERTY = "kotlin.javaClass.property";
public static final String KOTLIN_ARRAYS_ARRAY = "kotlin.arrays.array";
public static final String KOTLIN_COPY_TO_ARRAY = "kotlin.collections.copyToArray";
private static final String KOTLIN_TO_STRING = "kotlin.toString";
private static final String KOTLIN_HASH_CODE = "kotlin.hashCode";
private static final String KOTLIN_ARRAYS_ARRAY = "kotlin.arrays.array";
private static final String KOTLIN_COPY_TO_ARRAY = "kotlin.collections.copyToArray";
private static final EnumValues ENUM_VALUES = new EnumValues();
private static final EnumValueOf ENUM_VALUE_OF = new EnumValueOf();
private static final ToString TO_STRING = new ToString();
@@ -79,8 +77,6 @@ public class IntrinsicMethods {
namedMethods.put(KOTLIN_JAVA_CLASS_PROPERTY, new JavaClassProperty());
namedMethods.put(KOTLIN_ARRAYS_ARRAY, new JavaClassArray());
namedMethods.put(KOTLIN_COPY_TO_ARRAY, new CopyToArray());
namedMethods.put(KOTLIN_HASH_CODE, HASH_CODE);
namedMethods.put(KOTLIN_TO_STRING, TO_STRING);
ImmutableList<Name> primitiveCastMethods = OperatorConventions.NUMBER_CONVERSIONS.asList();
for (Name method : primitiveCastMethods) {
@@ -98,8 +94,12 @@ public class IntrinsicMethods {
declareIntrinsicFunction(typeName, Name.identifier("rangeTo"), 1, RANGE_TO);
declareIntrinsicFunction(typeName, Name.identifier("inc"), 0, INC);
declareIntrinsicFunction(typeName, Name.identifier("dec"), 0, DEC);
declareIntrinsicFunction(typeName, Name.identifier("hashCode"), 0, HASH_CODE);
}
for (PrimitiveType type : PrimitiveType.values()) {
Name typeName = type.getTypeName();
declareIntrinsicFunction(typeName, Name.identifier("equals"), 1, EQUALS);
declareIntrinsicFunction(typeName, Name.identifier("hashCode"), 0, HASH_CODE);
declareIntrinsicFunction(typeName, Name.identifier("toString"), 0, TO_STRING);
}
@@ -116,8 +116,6 @@ public class IntrinsicMethods {
declareBinaryOp(Name.identifier("xor"), IXOR);
declareIntrinsicFunction(Name.identifier("Boolean"), Name.identifier("not"), 0, new Not());
declareIntrinsicFunction(Name.identifier("Boolean"), Name.identifier("equals"), 1, EQUALS);
declareIntrinsicFunction(Name.identifier("Boolean"), Name.identifier("toString"), 0, TO_STRING);
declareIntrinsicFunction(Name.identifier("String"), Name.identifier("plus"), 1, new Concat());
declareIntrinsicFunction(Name.identifier("CharSequence"), Name.identifier("get"), 1, new StringGetChar());
@@ -0,0 +1,68 @@
fun box(): String {
val b: Byte = 42
val c: Char = 'z'
val s: Short = 239
val i: Int = -1
val j: Long = -42L
val f: Float = 3.14f
val d: Double = -2.72
val z: Boolean = true
b.equals(b)
b equals b
b == b
b.hashCode()
b.toString()
"$b"
c.equals(c)
c equals c
c == c
c.hashCode()
c.toString()
"$c"
s.equals(s)
s equals s
s == s
s.hashCode()
s.toString()
"$s"
i.equals(i)
i equals i
i == i
i.hashCode()
i.toString()
"$i"
j.equals(j)
j equals j
j == j
j.hashCode()
j.toString()
"$j"
f.equals(f)
f equals f
f == f
f.hashCode()
f.toString()
"$f"
d.equals(d)
d equals d
d == d
d.hashCode()
d.toString()
"$d"
z.equals(z)
z equals z
z == z
z.hashCode()
z.toString()
"$z"
return "OK"
}
@@ -4256,6 +4256,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest("compiler/testData/codegen/box/primitiveTypes/ea35963.kt");
}
@TestMetadata("equalsHashCodeToString.kt")
public void testEqualsHashCodeToString() throws Exception {
doTest("compiler/testData/codegen/box/primitiveTypes/equalsHashCodeToString.kt");
}
@TestMetadata("incrementByteCharShort.kt")
public void testIncrementByteCharShort() throws Exception {
doTest("compiler/testData/codegen/box/primitiveTypes/incrementByteCharShort.kt");
-55
View File
@@ -25,58 +25,3 @@ public annotation class throws(vararg val exceptionClasses: Class<out Throwable>
get() = (this as java.lang.Object).getClass() as Class<T>
[Intrinsic("kotlin.javaClass.function")] fun <reified T> javaClass() : Class<T> = null as Class<T>
/**
* A helper method for calling hashCode() on Kotlin objects
* TODO remove when Any supports equals() and hashCode()
*/
[Intrinsic("kotlin.hashCode")] public fun Any.hashCode(): Int = (this as java.lang.Object).hashCode()
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Boolean.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Byte.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Short.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Char.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Int.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Float.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Long.toString(): String = (null as String)
/**
* A helper method for calling toString() on Kotlin primitives
* TODO remove when Any supports toString()
*/
[Intrinsic("kotlin.toString")] public fun Double.toString(): String = (null as String)