From 9a8e0f3fb3a3b5d16876803d0011be1162cd0404 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Mon, 12 Apr 2021 14:32:51 +0300 Subject: [PATCH] Add new builtins methods for some interfaces into IrBuiltInsMapGenerated This change allow us to skip looking for override when evaluate call for some primitive. --- .../ir/interpreter/builtins/IrBuiltInsMapGenerated.kt | 11 +++++++++++ .../kotlin/ir/interpreter/state/Primitive.kt | 8 ++------ generators/interpreter/GenerateInterpreterMap.kt | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/builtins/IrBuiltInsMapGenerated.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/builtins/IrBuiltInsMapGenerated.kt index 79c367818f3..c4c3d8d18df 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/builtins/IrBuiltInsMapGenerated.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/builtins/IrBuiltInsMapGenerated.kt @@ -78,6 +78,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toByte() "Long" -> return (a as Long).toByte() "Double" -> return (a as Double).toByte() + "Number" -> return (a as Number).toByte() } "toChar" -> when (type) { "Char" -> return (a as Char).toChar() @@ -87,6 +88,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toChar() "Long" -> return (a as Long).toChar() "Double" -> return (a as Double).toChar() + "Number" -> return (a as Number).toChar() } "toDouble" -> when (type) { "Char" -> return (a as Char).toDouble() @@ -96,6 +98,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toDouble() "Long" -> return (a as Long).toDouble() "Double" -> return (a as Double).toDouble() + "Number" -> return (a as Number).toDouble() } "toFloat" -> when (type) { "Char" -> return (a as Char).toFloat() @@ -105,6 +108,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toFloat() "Long" -> return (a as Long).toFloat() "Double" -> return (a as Double).toFloat() + "Number" -> return (a as Number).toFloat() } "toInt" -> when (type) { "Char" -> return (a as Char).toInt() @@ -114,6 +118,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toInt() "Long" -> return (a as Long).toInt() "Double" -> return (a as Double).toInt() + "Number" -> return (a as Number).toInt() } "toLong" -> when (type) { "Char" -> return (a as Char).toLong() @@ -123,6 +128,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toLong() "Long" -> return (a as Long).toLong() "Double" -> return (a as Double).toLong() + "Number" -> return (a as Number).toLong() } "toShort" -> when (type) { "Char" -> return (a as Char).toShort() @@ -132,6 +138,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { "Float" -> return (a as Float).toShort() "Long" -> return (a as Long).toShort() "Double" -> return (a as Double).toShort() + "Number" -> return (a as Number).toShort() } "unaryMinus" -> when (type) { "Byte" -> return (a as Byte).unaryMinus() @@ -155,6 +162,7 @@ internal fun interpretUnaryFunction(name: String, type: String, a: Any?): Any? { } "length" -> when (type) { "String" -> return (a as String).length + "CharSequence" -> return (a as CharSequence).length } "size" -> when (type) { "BooleanArray" -> return (a as BooleanArray).size @@ -244,6 +252,7 @@ internal fun interpretBinaryFunction(name: String, typeA: String, typeB: String, "Short" -> return (a as Double).compareTo(b as Short) } "String" -> if (typeB == "String") return (a as String).compareTo(b as String) + "Comparable" -> if (typeB == "T") return (a as Comparable).compareTo(b) } "equals" -> when (typeA) { "Boolean" -> if (typeB == "Any?") return (a as Boolean).equals(b) @@ -564,6 +573,7 @@ internal fun interpretBinaryFunction(name: String, typeA: String, typeB: String, } "get" -> when (typeA) { "String" -> if (typeB == "Int") return (a as String).get(b as Int) + "CharSequence" -> if (typeB == "Int") return (a as CharSequence).get(b as Int) "BooleanArray" -> if (typeB == "Int") return (a as BooleanArray).get(b as Int) "CharArray" -> if (typeB == "Int") return (a as CharArray).get(b as Int) "ByteArray" -> if (typeB == "Int") return (a as ByteArray).get(b as Int) @@ -634,6 +644,7 @@ internal fun interpretTernaryFunction(name: String, typeA: String, typeB: String when (name) { "subSequence" -> when (typeA) { "String" -> if (typeB == "Int" && typeC == "Int") return (a as String).subSequence(b as Int, c as Int) + "CharSequence" -> if (typeB == "Int" && typeC == "Int") return (a as CharSequence).subSequence(b as Int, c as Int) } "set" -> when (typeA) { "BooleanArray" -> if (typeB == "Int" && typeC == "Boolean") return (a as BooleanArray).set(b as Int, c as Boolean) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/Primitive.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/Primitive.kt index 431c0fb54ea..d828927c4e9 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/Primitive.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/state/Primitive.kt @@ -27,13 +27,9 @@ internal class Primitive(val value: T, val type: IrType) : State { return super.getState(symbol) ?: this } - override fun getIrFunctionByIrCall(expression: IrCall): IrFunction? { + override fun getIrFunctionByIrCall(expression: IrCall): IrFunction { val owner = expression.symbol.owner - // must add property's getter to declaration's list because they are not present in ir class for primitives - val declarations = irClass.declarations.map { if (it is IrProperty) it.getter else it } - return declarations.filterIsInstance() - .firstOrNull { it.symbol == owner.symbol || (it is IrSimpleFunction && it.overrides(owner)) } - ?.let { if (it.isFakeOverride) it.getLastOverridden() else it } + return if (owner.isFakeOverride) owner.getLastOverridden() else owner } override fun equals(other: Any?): Boolean { diff --git a/generators/interpreter/GenerateInterpreterMap.kt b/generators/interpreter/GenerateInterpreterMap.kt index a66d0ded3d3..d5e0c2db6cb 100644 --- a/generators/interpreter/GenerateInterpreterMap.kt +++ b/generators/interpreter/GenerateInterpreterMap.kt @@ -142,6 +142,7 @@ private fun generateInterpretTernaryFunction(p: Printer, ternaryOperations: List private fun castValue(name: String, type: String): String = when (type) { "Any?", "T" -> name "Array" -> "$name as Array" + "Comparable" -> "$name as Comparable" else -> "$name as $type" } @@ -184,6 +185,7 @@ private fun getOperationMap(argumentsCount: Int): MutableList { val operationMap = mutableListOf() val allPrimitiveTypes = PrimitiveType.values().map { builtIns.getBuiltInClassByFqName(it.typeFqName) } val arrays = PrimitiveType.values().map { builtIns.getPrimitiveArrayClassDescriptor(it) } + builtIns.array + val additionalBuiltIns = listOf(builtIns.string, builtIns.any, builtIns.charSequence, builtIns.number, builtIns.comparable) fun CallableDescriptor.isFakeOverride(classDescriptor: ClassDescriptor): Boolean { val isPrimitive = KotlinBuiltIns.isPrimitiveClass(classDescriptor) || KotlinBuiltIns.isString(classDescriptor.defaultType) @@ -191,7 +193,7 @@ private fun getOperationMap(argumentsCount: Int): MutableList { return !isPrimitive && isFakeOverridden } - for (classDescriptor in allPrimitiveTypes + builtIns.string + arrays + builtIns.any) { + for (classDescriptor in allPrimitiveTypes + additionalBuiltIns + arrays) { val compileTimeFunctions = classDescriptor.unsubstitutedMemberScope.getContributedDescriptors() .filterIsInstance() .filter { !it.isFakeOverride(classDescriptor) && it.valueParameters.size + 1 == argumentsCount }