Introduce new Common state that represent common object
This is a replacement for Complex, that are now an abstract class.
This commit is contained in:
+6
-6
@@ -53,8 +53,8 @@ class IrInterpreter(irModule: IrModuleFragment) {
|
||||
Code.NEXT -> data.popReturnValue().toIrExpression(expression)
|
||||
Code.EXCEPTION -> {
|
||||
val message = when (val exception = data.popReturnValue()) {
|
||||
is Common -> (exception.fields.first { it.descriptor.name.asString() == "message" }.state as Primitive<*>).value.toString()
|
||||
is Wrapper -> (exception.value as Throwable).message.toString()
|
||||
is Complex -> (exception.fields.first { it.descriptor.name.asString() == "message" }.state as Primitive<*>).value.toString()
|
||||
else -> TODO("not supported")
|
||||
}
|
||||
IrErrorExpressionImpl(expression.startOffset, expression.endOffset, expression.type, message)
|
||||
@@ -295,7 +295,7 @@ class IrInterpreter(irModule: IrModuleFragment) {
|
||||
return Wrapper.getConstructorMethod(owner).invokeMethod(owner, newFrame).apply { data.pushReturnValue(newFrame) }
|
||||
}
|
||||
|
||||
val state = Complex(parent, mutableListOf())
|
||||
val state = Common(parent, mutableListOf())
|
||||
newFrame.addVar(Variable(constructorCall.getThisAsReceiver(), state)) //used to set up fields in body
|
||||
val code = constructorCall.getBody()?.interpret(newFrame) ?: Code.NEXT
|
||||
state.superType = newFrame.popReturnValue() as Complex
|
||||
@@ -312,8 +312,8 @@ class IrInterpreter(irModule: IrModuleFragment) {
|
||||
|
||||
private fun interpretDelegatedConstructorCall(delegatingConstructorCall: IrDelegatingConstructorCall, data: Frame): Code {
|
||||
if (delegatingConstructorCall.symbol.descriptor.containingDeclaration.defaultType == DefaultBuiltIns.Instance.anyType) {
|
||||
val anyComplex = Complex(irBuiltIns.anyClass.owner, mutableListOf())
|
||||
data.pushReturnValue(anyComplex)
|
||||
val anyAsStateObject = Common(irBuiltIns.anyClass.owner, mutableListOf())
|
||||
data.pushReturnValue(anyAsStateObject)
|
||||
return Code.NEXT
|
||||
}
|
||||
|
||||
@@ -453,7 +453,7 @@ class IrInterpreter(irModule: IrModuleFragment) {
|
||||
}
|
||||
|
||||
private fun interpretGetObjectValue(expression: IrGetObjectValue, data: Frame): Code {
|
||||
val objectState = Complex(expression.symbol.owner, mutableListOf())
|
||||
val objectState = Common(expression.symbol.owner, mutableListOf())
|
||||
val newFrame = data.copy().apply { addVar(Variable(expression.symbol.descriptor.thisAsReceiverParameter, objectState)) }
|
||||
|
||||
val constructor = expression.symbol.owner.declarations.single { it is IrConstructor } as IrConstructor
|
||||
@@ -552,7 +552,7 @@ class IrInterpreter(irModule: IrModuleFragment) {
|
||||
when (val returnValue = data.popReturnValue()) {
|
||||
is Primitive<*> -> returnValue.value.toString()
|
||||
is Wrapper -> returnValue.value.toString()
|
||||
is Complex -> {
|
||||
is Common -> {
|
||||
val toStringFun = returnValue.getToStringFunction()
|
||||
val newFrame = InterpreterFrame(mutableListOf(Variable(toStringFun.symbol.getReceiver()!!, returnValue)))
|
||||
val code = toStringFun.body?.let { toStringFun.interpret(newFrame) } ?: calculateOverridden(toStringFun, newFrame)
|
||||
|
||||
+19
-13
@@ -104,7 +104,7 @@ class Primitive<T>(var value: T, val type: IrType) : State {
|
||||
}
|
||||
}
|
||||
|
||||
open class Complex(override var irClass: IrClass, override val fields: MutableList<Variable>) : State {
|
||||
abstract class Complex(override var irClass: IrClass, override val fields: MutableList<Variable>) : State {
|
||||
var superType: Complex? = null
|
||||
var instance: Complex? = null
|
||||
|
||||
@@ -117,6 +117,14 @@ open class Complex(override var irClass: IrClass, override val fields: MutableLi
|
||||
return irClass.thisReceiver!!.descriptor
|
||||
}
|
||||
|
||||
override fun getIrFunction(descriptor: FunctionDescriptor): IrFunction? {
|
||||
return irClass.declarations.filterIsInstance<IrFunction>()
|
||||
.filter { it.descriptor.name == descriptor.name }
|
||||
.firstOrNull { it.descriptor.valueParameters.map { it.type } == descriptor.valueParameters.map { it.type } }
|
||||
}
|
||||
}
|
||||
|
||||
class Common(override var irClass: IrClass, override val fields: MutableList<Variable>) : Complex(irClass, fields) {
|
||||
override fun setState(newVar: Variable) {
|
||||
when (val oldState = fields.firstOrNull { it.descriptor == newVar.descriptor }) {
|
||||
null -> fields.add(newVar) // newVar isn't present in value list
|
||||
@@ -124,12 +132,6 @@ open class Complex(override var irClass: IrClass, override val fields: MutableLi
|
||||
}
|
||||
}
|
||||
|
||||
override fun getIrFunction(descriptor: FunctionDescriptor): IrFunction? {
|
||||
return irClass.declarations.filterIsInstance<IrFunction>()
|
||||
.filter { it.descriptor.name == descriptor.name }
|
||||
.firstOrNull { it.descriptor.valueParameters.map { it.type } == descriptor.valueParameters.map { it.type } }
|
||||
}
|
||||
|
||||
fun getToStringFunction(): IrFunctionImpl {
|
||||
return irClass.declarations.filterIsInstance<IrFunction>()
|
||||
.filter { it.descriptor.name.asString() == "toString" }
|
||||
@@ -137,14 +139,14 @@ open class Complex(override var irClass: IrClass, override val fields: MutableLi
|
||||
}
|
||||
|
||||
override fun copy(): State {
|
||||
return Complex(irClass, fields).apply {
|
||||
this@apply.superType = this@Complex.superType
|
||||
this@apply.instance = this@Complex.instance
|
||||
return Common(irClass, fields).apply {
|
||||
this@apply.superType = this@Common.superType
|
||||
this@apply.instance = this@Common.instance
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "Complex(obj='${irClass.fqNameForIrSerialization}', super=$superType, values=$fields)"
|
||||
return "Common(obj='${irClass.fqNameForIrSerialization}', super=$superType, values=$fields)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,6 +168,10 @@ class Wrapper(val value: Any, override var irClass: IrClass) : Complex(irClass,
|
||||
return MethodHandles.lookup().findVirtual(receiverClass, methodName, methodType)
|
||||
}
|
||||
|
||||
override fun setState(newVar: Variable) {
|
||||
throw UnsupportedOperationException("Method setState is not supported in Wrapper class")
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getConstructorMethod(irConstructor: IrFunction): MethodHandle {
|
||||
val methodType = irConstructor.getMethodType()
|
||||
@@ -250,7 +256,7 @@ class Wrapper(val value: Any, override var irClass: IrClass) : Complex(irClass,
|
||||
}
|
||||
|
||||
override fun copy(): State {
|
||||
return Wrapper(value, irClass)
|
||||
return Wrapper(value, irClass).apply { this@apply.instance = this@Wrapper.instance }
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
@@ -271,7 +277,7 @@ class Lambda(val irFunction: IrFunction, override var irClass: IrClass) : Comple
|
||||
}
|
||||
|
||||
override fun copy(): State {
|
||||
return Lambda(irFunction, irClass)
|
||||
return Lambda(irFunction, irClass).apply { this@apply.instance = this@Lambda.instance }
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
|
||||
Reference in New Issue
Block a user