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 78bf8bf00ee..ff1bacdd31e 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 @@ -32,7 +32,7 @@ import java.lang.invoke.MethodHandle private const val MAX_STACK_SIZE = 10_000 private const val MAX_COMMANDS = 500_000 -class IrInterpreter(irModule: IrModuleFragment, private val declarationsMap: Map = emptyMap()) { +class IrInterpreter(irModule: IrModuleFragment, private val bodyMap: Map = emptyMap()) { private val irBuiltIns = irModule.irBuiltins private val irExceptions = irModule.files.flatMap { it.declarations }.filterIsInstance() .filter { it.isSubclassOf(irBuiltIns.throwableClass.owner) } @@ -301,14 +301,15 @@ class IrInterpreter(irModule: IrModuleFragment, private val declarationsMap: Map dispatchReceiver is Wrapper && !isInlineOnly -> dispatchReceiver.getMethod(irFunction).invokeMethod(irFunction) irFunction.hasAnnotation(evaluateIntrinsicAnnotation) -> Wrapper.getStaticMethod(irFunction).invokeMethod(irFunction) dispatchReceiver is Primitive<*> -> calculateBuiltIns(irFunction) // 'is Primitive' check for js char and js long - irFunction.body == null -> irFunction.substituteFunctionBody() + irFunction.body == null -> irFunction.trySubstituteFunctionBody() ?: calculateBuiltIns(irFunction) else -> irFunction.interpret() } } } - private suspend fun IrFunction.substituteFunctionBody(): ExecutionResult { - val body = declarationsMap[this.symbol.signature] + private suspend fun IrFunction.trySubstituteFunctionBody(): ExecutionResult? { + if (!this.symbol.isPublicApi) return null + val body = bodyMap[this.symbol.signature] return body?.let { try { @@ -317,7 +318,7 @@ class IrInterpreter(irModule: IrModuleFragment, private val declarationsMap: Map } finally { this.body = null } - } ?: calculateBuiltIns(this) + } } private suspend fun interpretInstanceInitializerCall(call: IrInstanceInitializerCall): ExecutionResult { @@ -349,7 +350,7 @@ class IrInterpreter(irModule: IrModuleFragment, private val declarationsMap: Map interpretValueParameters(constructorCall, owner, valueArguments).check { return it } val irClass = owner.parent as IrClass - val typeArguments = getTypeArguments(irClass, constructorCall) { stack.getVariableState(it) } + stack.getAllTypeArguments() + val typeArguments = getTypeArguments(irClass, constructorCall) { stack.getVariableState(it) } if (irClass.hasAnnotation(evaluateIntrinsicAnnotation) || irClass.fqNameWhenAvailable!!.startsWith(Name.identifier("java"))) { return stack.newFrame(initPool = valueArguments) { Wrapper.getConstructorMethod(owner).invokeMethod(owner) } .apply { (stack.peekReturnValue() as? Wrapper)?.typeArguments?.addAll(typeArguments) } // 'as?' because can be exception diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Frame.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Frame.kt index db895ab097c..787d0bce72d 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Frame.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Frame.kt @@ -17,7 +17,6 @@ interface Frame { fun getVariableState(variableDescriptor: DeclarationDescriptor): State fun tryGetVariableState(variableDescriptor: DeclarationDescriptor): State? fun getAll(): List - fun getAllTypeArguments(): List // TODO try to get rid of this method; possibly by finding all type arguments in local class fun contains(descriptor: DeclarationDescriptor): Boolean fun pushReturnValue(state: State) fun pushReturnValue(frame: Frame) // TODO rename to getReturnValueFrom @@ -55,10 +54,6 @@ class InterpreterFrame( return pool } - override fun getAllTypeArguments(): List { - return typeArguments - } - override fun contains(descriptor: DeclarationDescriptor): Boolean { return (typeArguments + pool).any { it.descriptor == descriptor } } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Stack.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Stack.kt index 64de5c215ad..fbd737d629e 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Stack.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/stack/Stack.kt @@ -30,7 +30,6 @@ interface Stack { fun addAll(variables: List) fun getVariableState(variableDescriptor: DeclarationDescriptor): State fun getAll(): List - fun getAllTypeArguments(): List fun contains(descriptor: DeclarationDescriptor): Boolean fun hasReturnValue(): Boolean @@ -95,10 +94,6 @@ class StackImpl : Stack { return getCurrentFrame().getAll() } - override fun getAllTypeArguments(): List { - return getCurrentFrame().getAllTypeArguments() - } - override fun contains(descriptor: DeclarationDescriptor): Boolean { return getCurrentFrame().contains(descriptor) } @@ -137,7 +132,6 @@ private class FrameContainer(current: Frame = InterpreterFrame()) { fun addVar(variable: Variable) = getTopFrame().addVar(variable) fun addAll(variables: List) = getTopFrame().addAll(variables) fun getAll() = innerStack.flatMap { it.getAll() } - fun getAllTypeArguments() = innerStack.flatMap { it.getAllTypeArguments() } fun getVariableState(variableDescriptor: DeclarationDescriptor): State { return innerStack.firstNotNullResult { it.tryGetVariableState(variableDescriptor) } ?: throw InterpreterException("$variableDescriptor not found") // TODO better message