From cf20e64c619ea9d24a51fb3639352537e6478f0d Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Sat, 29 May 2021 21:59:14 +0300 Subject: [PATCH] Support basic interpretation of IrGetClass --- .../IrInterpreterAfterFir2IrTestGenerated.java | 6 ++++++ .../IrInterpreterAfterPsi2IrTestGenerated.java | 6 ++++++ .../kotlin/ir/interpreter/InstructionsUnfolder.kt | 6 ++++++ .../kotlin/ir/interpreter/IrInterpreter.kt | 6 ++++++ .../testData/ir/interpreter/reference/getClass.kt | 13 +++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 compiler/testData/ir/interpreter/reference/getClass.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterFir2IrTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterFir2IrTestGenerated.java index 4db5ab448d6..554b6659e19 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterFir2IrTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterFir2IrTestGenerated.java @@ -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 { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterPsi2IrTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterPsi2IrTestGenerated.java index a28d36e3298..85b4e0312c8 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterPsi2IrTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/interpreter/IrInterpreterAfterPsi2IrTestGenerated.java @@ -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 { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt index dbd46446ab3..ec6e4351582 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/InstructionsUnfolder.kt @@ -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)) } \ No newline at end of file diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt index d7b9c930bc8..9e26d60c37a 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreter.kt @@ -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)) + } } diff --git a/compiler/testData/ir/interpreter/reference/getClass.kt b/compiler/testData/ir/interpreter/reference/getClass.kt new file mode 100644 index 00000000000..b378e010a58 --- /dev/null +++ b/compiler/testData/ir/interpreter/reference/getClass.kt @@ -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 = test("") +const val b = test(1) +const val c = test(true) +const val d = test(2.0)