From cf63a5f52be25324e50b5617d3354bd6f5a36586 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Mon, 13 Apr 2020 19:43:59 +0300 Subject: [PATCH] Replace some casts to Primitive with corresponding extension calls --- .../common/interpreter/IrInterpreter.kt | 24 +++++++++---------- .../backend/common/interpreter/state/State.kt | 6 +++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt index 1f89145a30a..76370b51306 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/IrInterpreter.kt @@ -178,7 +178,7 @@ class IrInterpreter(irModule: IrModuleFragment) { stack.pushReturnValue(result.toState(result.getType(irFunction.returnType))) } "arrayOfNulls" -> { - val size = (stack.getVariableState(irFunction.valueParameters.first().descriptor) as Primitive<*>).value as Int + val size = stack.getVariableState(irFunction.valueParameters.first().descriptor).asInt() val result = arrayOfNulls(size) stack.pushReturnValue(result.toState(result.getType(irFunction.returnType))) } @@ -196,7 +196,7 @@ class IrInterpreter(irModule: IrModuleFragment) { "valueOf", "enumValueOf" -> { val enumClass = (irFunction.parent as? IrClass) ?: stack.getVariableState(irFunction.typeParameters.first().descriptor).irClass - val enumEntryName = (stack.getVariableState(irFunction.valueParameters.first().descriptor) as Primitive<*>).value.toString() + val enumEntryName = stack.getVariableState(irFunction.valueParameters.first().descriptor).asString() val enumEntry = enumClass.declarations .filterIsInstance() .singleOrNull { it.name.asString() == enumEntryName } @@ -211,13 +211,13 @@ class IrInterpreter(irModule: IrModuleFragment) { "replace" -> { val states = stack.getAll().map { it.state } val regex = states.filterIsInstance().single().value as Regex - val input = states.filterIsInstance>().single().value.toString() + val input = states.filterIsInstance>().single().asString() val transform = states.filterIsInstance().single().irFunction val matchResultParameter = transform.valueParameters.single() val result = regex.replace(input) { val itAsState = Variable(matchResultParameter.descriptor, Wrapper(it, matchResultParameter.type.classOrNull!!.owner)) runBlocking { stack.newFrame(initPool = listOf(itAsState)) { transform.interpret() } }//.check { return it } - (stack.popReturnValue() as Primitive<*>).value.toString() + stack.popReturnValue().asString() //TODO("replace not implemented") } stack.pushReturnValue(result.toState(irBuiltIns.stringType)) @@ -436,13 +436,13 @@ class IrInterpreter(irModule: IrModuleFragment) { if (parent.hasAnnotation(evaluateIntrinsicAnnotation)) { return when (owner.parentAsClass.getEvaluateIntrinsicValue()) { "kotlin.Long" -> { - val low = (valueArguments[0].state as Primitive<*>).value as Int - val high = (valueArguments[1].state as Primitive<*>).value as Int + val low = valueArguments[0].state.asInt() + val high = valueArguments[1].state.asInt() stack.pushReturnValue((high.toLong().shl(32) + low).toState(irBuiltIns.longType)) Next } "kotlin.Char" -> { - val value = (valueArguments[0].state as Primitive<*>).value as Int + val value = valueArguments[0].state.asInt() stack.pushReturnValue(value.toChar().toState(irBuiltIns.charType)) Next } @@ -454,7 +454,7 @@ class IrInterpreter(irModule: IrModuleFragment) { // array constructor doesn't have body so must be treated separately val arrayConstructor = irBuiltIns.primitiveArrays.first().constructors.single { it.owner.valueParameters.size == 2 } val sizeDescriptor = arrayConstructor.owner.valueParameters.single { it.name.asString() == "size" }.descriptor - val size = (valueArguments[0].state as Primitive<*>).value as Int + val size = valueArguments[0].state.asInt() val arrayValue = MutableList(size) { 0 } if (owner.valueParameters.size == 2) { @@ -557,10 +557,10 @@ class IrInterpreter(irModule: IrModuleFragment) { } private suspend fun interpretWhile(expression: IrWhileLoop): ExecutionResult { - var executionResult: ExecutionResult = Next + var executionResult: ExecutionResult while (true) { executionResult = expression.condition.interpret().check { return it } - val condition = (stack.popReturnValue() as? Primitive<*>)?.value as? Boolean + val condition = stack.popReturnValue().asBooleanOrNull() if (condition != true) break else expression.body?.interpret()?.check { return it } ?: return Next } return executionResult @@ -576,7 +576,7 @@ class IrInterpreter(irModule: IrModuleFragment) { private suspend fun interpretBranch(expression: IrBranch): ExecutionResult { val executionResult = expression.condition.interpret().check { return it } - if ((stack.popReturnValue() as? Primitive<*>)?.value as? Boolean == true) { + if (stack.popReturnValue().asBooleanOrNull() == true) { expression.result.interpret().check { return it } return BreakWhen } @@ -787,7 +787,7 @@ class IrInterpreter(irModule: IrModuleFragment) { stack.newFrame(initPool = mutableListOf(Variable(toStringFun.getReceiver()!!, returnValue))) { toStringFun.body?.let { toStringFun.interpret() } ?: calculateOverridden(toStringFun) }.check { executionResult -> return executionResult } - (stack.popReturnValue() as Primitive<*>).value.toString() + stack.popReturnValue().asString() } else -> throw InterpreterException("$returnValue cannot be used in StringConcatenation expression") } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/state/State.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/state/State.kt index 626301fea32..b930f519643 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/state/State.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/state/State.kt @@ -33,3 +33,9 @@ interface State { fun getIrFunction(descriptor: FunctionDescriptor): IrFunction? } + +fun State.asInt() = (this as Primitive<*>).value as Int +fun State.asBoolean() = (this as Primitive<*>).value as Boolean +fun State.asString() = (this as Primitive<*>).value.toString() + +fun State.asBooleanOrNull() = (this as? Primitive<*>)?.value as? Boolean \ No newline at end of file