Remove unnecessary getAllTypeArguments method from Stack class

This commit is contained in:
Ivan Kylchik
2020-05-29 15:30:19 +03:00
parent 99d823da8a
commit 19495e40f7
3 changed files with 7 additions and 17 deletions
@@ -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<IdSignature, IrBody> = emptyMap()) {
class IrInterpreter(irModule: IrModuleFragment, private val bodyMap: Map<IdSignature, IrBody> = emptyMap()) {
private val irBuiltIns = irModule.irBuiltins
private val irExceptions = irModule.files.flatMap { it.declarations }.filterIsInstance<IrClass>()
.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
@@ -17,7 +17,6 @@ interface Frame {
fun getVariableState(variableDescriptor: DeclarationDescriptor): State
fun tryGetVariableState(variableDescriptor: DeclarationDescriptor): State?
fun getAll(): List<Variable>
fun getAllTypeArguments(): List<Variable> // 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<Variable> {
return typeArguments
}
override fun contains(descriptor: DeclarationDescriptor): Boolean {
return (typeArguments + pool).any { it.descriptor == descriptor }
}
@@ -30,7 +30,6 @@ interface Stack {
fun addAll(variables: List<Variable>)
fun getVariableState(variableDescriptor: DeclarationDescriptor): State
fun getAll(): List<Variable>
fun getAllTypeArguments(): List<Variable>
fun contains(descriptor: DeclarationDescriptor): Boolean
fun hasReturnValue(): Boolean
@@ -95,10 +94,6 @@ class StackImpl : Stack {
return getCurrentFrame().getAll()
}
override fun getAllTypeArguments(): List<Variable> {
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<Variable>) = 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