Support basic interpretation of IrGetClass

This commit is contained in:
Ivan Kylchik
2021-05-29 21:59:14 +03:00
committed by TeamCityServer
parent 6fe8dd1254
commit cf20e64c61
5 changed files with 37 additions and 0 deletions
@@ -694,6 +694,12 @@ public class IrInterpreterAfterFir2IrTestGenerated extends AbstractIrInterpreter
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/interpreter/reference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("getClass.kt")
public void testGetClass() throws Exception {
runTest("compiler/testData/ir/interpreter/reference/getClass.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -694,6 +694,12 @@ public class IrInterpreterAfterPsi2IrTestGenerated extends AbstractIrInterpreter
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/interpreter/reference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("getClass.kt")
public void testGetClass() throws Exception {
runTest("compiler/testData/ir/interpreter/reference/getClass.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -68,6 +68,7 @@ internal fun unfoldInstruction(element: IrElement?, environment: IrInterpreterEn
is IrFunctionReference -> unfoldFunctionReference(element, callStack)
is IrPropertyReference -> unfoldPropertyReference(element, callStack)
is IrClassReference -> unfoldClassReference(element, callStack)
is IrGetClass -> unfoldGetClass(element, callStack)
is IrComposite -> unfoldComposite(element, callStack)
else -> TODO("${element.javaClass} not supported")
@@ -401,4 +402,9 @@ private fun unfoldPropertyReference(propertyReference: IrPropertyReference, call
private fun unfoldClassReference(classReference: IrClassReference, callStack: CallStack) {
callStack.addInstruction(SimpleInstruction(classReference))
}
private fun unfoldGetClass(element: IrGetClass, callStack: CallStack) {
callStack.addInstruction(SimpleInstruction(element))
callStack.addInstruction(CompoundInstruction(element.argument))
}
@@ -132,6 +132,7 @@ class IrInterpreter private constructor(
is IrFunctionReference -> interpretFunctionReference(element)
is IrPropertyReference -> interpretPropertyReference(element)
is IrClassReference -> interpretClassReference(element)
is IrGetClass -> interpretGetClass(element)
else -> TODO("${element.javaClass} not supported for interpretation")
}
}
@@ -616,4 +617,9 @@ class IrInterpreter private constructor(
else -> callStack.pushState(KClassState(classReference))
}
}
private fun interpretGetClass(expression: IrGetClass) {
val irClass = callStack.popState().irClass
callStack.pushState(KClassState(irClass, expression.type.classOrNull!!.owner))
}
}
+13
View File
@@ -0,0 +1,13 @@
import kotlin.*
fun test(a: Any) = when (a::class) {
String::class -> "String"
Int::class -> "Int"
Boolean::class -> "Boolean"
else -> "Else"
}
const val a = <!EVALUATED: `String`!>test("")<!>
const val b = <!EVALUATED: `Int`!>test(1)<!>
const val c = <!EVALUATED: `Boolean`!>test(true)<!>
const val d = <!EVALUATED: `Else`!>test(2.0)<!>