From 774e5f05358250bf11b13112e16c97b1c4571f1d Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 7 Feb 2014 15:49:28 +0400 Subject: [PATCH] Sort out JVM intrinsics for equals, hashCode, toString --- .../codegen/intrinsics/IntrinsicMethods.java | 18 +++-- .../primitiveTypes/equalsHashCodeToString.kt | 68 +++++++++++++++++++ .../BlackBoxCodegenTestGenerated.java | 5 ++ libraries/stdlib/src/kotlin/JLangJVM.kt | 55 --------------- 4 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 compiler/testData/codegen/box/primitiveTypes/equalsHashCodeToString.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java index f639ff1700f..8f6dd31c5d2 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java @@ -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 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()); diff --git a/compiler/testData/codegen/box/primitiveTypes/equalsHashCodeToString.kt b/compiler/testData/codegen/box/primitiveTypes/equalsHashCodeToString.kt new file mode 100644 index 00000000000..1acdac8c820 --- /dev/null +++ b/compiler/testData/codegen/box/primitiveTypes/equalsHashCodeToString.kt @@ -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" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index 6c067bfb5e0..736668bc11d 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -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"); diff --git a/libraries/stdlib/src/kotlin/JLangJVM.kt b/libraries/stdlib/src/kotlin/JLangJVM.kt index e045ab4955f..a8d3a6a6bee 100644 --- a/libraries/stdlib/src/kotlin/JLangJVM.kt +++ b/libraries/stdlib/src/kotlin/JLangJVM.kt @@ -25,58 +25,3 @@ public annotation class throws(vararg val exceptionClasses: Class get() = (this as java.lang.Object).getClass() as Class [Intrinsic("kotlin.javaClass.function")] fun javaClass() : Class = null as Class - - -/** - * 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)