Add all methods from Any class in ir builtins map

This commit is contained in:
Ivan Kylchik
2020-01-30 17:35:59 +03:00
parent d03937cdb6
commit 4345294ac1
3 changed files with 42 additions and 44 deletions
@@ -165,7 +165,8 @@ class IrInterpreter(irModule: IrModuleFragment) {
private fun calculateOverridden(owner: IrFunctionImpl, data: Frame): Code {
val variableDescriptor = owner.symbol.getReceiver()!!
val superQualifier = (data.getVariableState(variableDescriptor) as Complex).superType!!
val superQualifier = (data.getVariableState(variableDescriptor) as? Complex)?.superType
?: return calculateBuiltIns(owner.overriddenSymbols.first().owner, data)
val overridden = owner.overriddenSymbols.first { it.getReceiver()?.equalTo(superQualifier.getReceiver()) == true }
val newStates = InterpreterFrame(mutableListOf(Variable(overridden.getReceiver()!!, superQualifier)))
@@ -186,46 +187,33 @@ class IrInterpreter(irModule: IrModuleFragment) {
val methodName = descriptor.name.asString()
val args = data.getAll().map { it.state }
val result: Any?
if (irFunction.parent.fqNameForIrSerialization.toString() == "kotlin.Any" || args.any { it !is Primitive<*> }) {
// if ir function is declaration in Any class OR it is ir builtin operator for non primitive types
val receiver = (args[0] as Complex).instance
result = when (methodName) {
"equals", "EQEQ", "EQEQEQ" -> (args[1] as Complex).instance === receiver
"hashCode" -> System.identityHashCode(receiver)
"toString" -> receiver?.irClass?.name?.asString() + "@" + System.identityHashCode(receiver).toString(16).padStart(8)
else -> throw IllegalStateException("Method $methodName can not be interpreted")
}
} else {
val receiverType = descriptor.dispatchReceiverParameter?.type ?: descriptor.extensionReceiverParameter?.type
val argsType = listOfNotNull(receiverType) + descriptor.valueParameters.map { it.original.type }
val argsValues = args
.map { it as? Primitive<*> ?: throw IllegalArgumentException("Builtin functions accept only const args") }
.map { it.value }
val signature = CompileTimeFunction(methodName, argsType.map { it.toString() })
val receiverType = descriptor.dispatchReceiverParameter?.type ?: descriptor.extensionReceiverParameter?.type
val argsType = listOfNotNull(receiverType) + descriptor.valueParameters.map { it.original.type }
val argsValues = args.map { (it as? Complex)?.instance ?: (it as Primitive<*>).value }
val signature = CompileTimeFunction(methodName, argsType.map { it.toString() })
result = when (argsType.size) {
1 -> {
val function = unaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in unary map")
function.invoke(argsValues.first())
}
2 -> {
val function = binaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in binary map")
when (methodName) {
"rangeTo" -> return calculateRangeTo(irFunction.returnType, data)
else -> function.invoke(argsValues[0], argsValues[1])
}
}
3 -> {
val function = ternaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in ternary map")
function.invoke(argsValues[0], argsValues[1], argsValues[2])
}
else -> throw UnsupportedOperationException("Unsupported number of arguments")
val result = when (argsType.size) {
1 -> {
val function = unaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in unary map")
function.invoke(argsValues.first())
}
2 -> {
val function = binaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in binary map")
when (methodName) {
"rangeTo" -> return calculateRangeTo(irFunction.returnType, data)
else -> function.invoke(argsValues[0], argsValues[1])
}
}
3 -> {
val function = ternaryFunctions[signature]
?: throw NoSuchMethodException("For given function $signature there is no entry in ternary map")
function.invoke(argsValues[0], argsValues[1], argsValues[2])
}
else -> throw UnsupportedOperationException("Unsupported number of arguments")
}
data.pushReturnValue(result.toState(result.getType(irFunction.returnType)))
return Code.NEXT
}
@@ -14,10 +14,10 @@
* limitations under the License.
*/
@file:Suppress("DEPRECATION_ERROR")
package org.jetbrains.kotlin.backend.common.interpreter.builtins
import org.jetbrains.kotlin.backend.common.interpreter.stack.State
/** This file is generated by org.jetbrains.kotlin.backend.common.interpreter.builtins.GenerateBuiltInsMap.generateMap(). DO NOT MODIFY MANUALLY */
val unaryFunctions = mapOf<CompileTimeFunction, Function1<Any?, Any?>>(
@@ -113,6 +113,10 @@ val unaryFunctions = mapOf<CompileTimeFunction, Function1<Any?, Any?>>(
unaryOperation<LongArray>("iterator", "LongArray") { a -> a.iterator() },
unaryOperation<DoubleArray>("size", "DoubleArray") { a -> a.size },
unaryOperation<DoubleArray>("iterator", "DoubleArray") { a -> a.iterator() },
unaryOperation<Any>("hashCode", "Any") { a -> a.hashCode() },
unaryOperation<Any>("toString", "Any") { a ->
if (a is State) "${a.irClass.name}@" + System.identityHashCode(a).toString(16).padStart(8) else a.toString()
},
unaryOperation<Any?>("CHECK_NOT_NULL", "T0?") { a -> a!! }
)
@@ -390,6 +394,7 @@ val binaryFunctions = mapOf<CompileTimeFunction, Function2<Any?, Any?, Any?>>(
binaryOperation<FloatArray, Int>("get", "FloatArray", "Int") { a, b -> a.get(b) },
binaryOperation<LongArray, Int>("get", "LongArray", "Int") { a, b -> a.get(b) },
binaryOperation<DoubleArray, Int>("get", "DoubleArray", "Int") { a, b -> a.get(b) },
binaryOperation<Any, Any?>("equals", "Any", "Any?") { a, b -> a.equals(b) },
binaryOperation<Char, Char>("less", "Char", "Char") { a, b -> a < b },
binaryOperation<Byte, Byte>("less", "Byte", "Byte") { a, b -> a < b },
binaryOperation<Short, Short>("less", "Short", "Short") { a, b -> a < b },
+9 -4
View File
@@ -39,10 +39,10 @@ fun generateMap(): String {
val sb = StringBuilder()
val p = Printer(sb)
p.println(File("license/COPYRIGHT.txt").readText())
p.println("@file:Suppress(\"DEPRECATION_ERROR\")")
p.println()
p.println("package org.jetbrains.kotlin.backend.common.interpreter.builtins")
p.println()
p.println("import org.jetbrains.kotlin.backend.common.interpreter.stack.State")
p.println()
p.println("/** This file is generated by org.jetbrains.kotlin.backend.common.interpreter.builtins.GenerateBuiltInsMap.generateMap(). DO NOT MODIFY MANUALLY */")
p.println()
@@ -58,10 +58,11 @@ fun generateMap(): String {
val arrays = listOf(builtIns.array) + PrimitiveType.values().map { builtIns.getPrimitiveArrayClassDescriptor(it) }
for (descriptor in allPrimitiveTypes + builtIns.string + arrays) {
for (descriptor in allPrimitiveTypes + builtIns.string + arrays + builtIns.any) {
val typeParameters = descriptor.typeConstructor.parameters.map { it.name.asString() }
val typeParametersAsAny = if (typeParameters.isNotEmpty()) typeParameters.joinToString(prefix = "<", postfix = ">") { "Any?" } else ""
val descriptorType = descriptor.defaultType
@Suppress("UNCHECKED_CAST")
val functions = descriptor.getMemberScope(listOf()).getContributedDescriptors()
.filter {
@@ -117,7 +118,11 @@ fun generateMap(): String {
val unaryBody = unaryOperationsMap.entries.joinToString(",\n", postfix = ",\n") { (function, parameters) ->
val methodName = "${function.name}"
val parentheses = if (function is FunctionDescriptor) "()" else ""
" unaryOperation${parameters.first}(\"$methodName\", ${parameters.second}) { a -> a.$methodName$parentheses }"
val body = if (methodName == "toString" && parameters.first == "<Any>")
"\n\t\tif (a is State) \"\${a.irClass.name}@\" + System.identityHashCode(a).toString(16).padStart(8) else a.toString()\n\t"
else
"a.$methodName$parentheses "
" unaryOperation${parameters.first}(\"$methodName\", ${parameters.second}) { a -> $body}"
} + " unaryOperation<Any?>(\"${irNullCheck.name}\", \"${irNullCheck.valueParameters.first().type}\") { a -> a!! }"
p.println(unaryBody)
p.println(")")