From b3407de1e66c860143edcdb984fca8f343af3edf Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 25 Aug 2016 09:44:16 +0300 Subject: [PATCH] Array access expression. --- .../psi2ir/generators/StatementGenerator.kt | 13 ++++++---- .../kotlin/ir/expressions/IrOperator.kt | 1 + compiler/testData/ir/irText/arrayAccess.kt | 5 ++++ compiler/testData/ir/irText/arrayAccess.txt | 24 +++++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 6 +++++ 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/ir/irText/arrayAccess.kt create mode 100644 compiler/testData/ir/irText/arrayAccess.txt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt index bda95711f52..479f8d0f47a 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt @@ -207,7 +207,6 @@ class StatementGenerator( } } is PropertyDescriptor -> { - // TODO safe calls CallGenerator(this).generateCall(expression.startOffset, expression.endOffset, pregenerateCall(resolvedCall)) } is VariableDescriptor -> @@ -226,11 +225,16 @@ class StatementGenerator( TODO("VariableAsFunctionResolvedCall = variable call + invoke call") } - // TODO safe calls - return CallGenerator(this).generateCall(expression.startOffset, expression.endOffset, pregenerateCall(resolvedCall)) } + override fun visitArrayAccessExpression(expression: KtArrayAccessExpression, data: Nothing?): IrStatement { + val indexedGetCall = getOrFail(BindingContext.INDEXED_LVALUE_GET, expression) + + return CallGenerator(this).generateCall(expression.startOffset, expression.endOffset, + pregenerateCall(indexedGetCall), IrOperator.GET_ARRAY_ELEMENT) + } + override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression, data: Nothing?): IrStatement = expression.selectorExpression!!.accept(this, data) @@ -297,6 +301,5 @@ class StatementGenerator( override fun visitTryExpression(expression: KtTryExpression, data: Nothing?): IrStatement = TryCatchExpressionGenerator(this).generateTryCatch(expression) -} - +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrOperator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrOperator.kt index dcb66dd3cc2..60d35eeea21 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrOperator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrOperator.kt @@ -53,6 +53,7 @@ interface IrOperator { object RANGE : IrOperatorImpl("RANGE") object INVOKE : IrOperatorImpl("INVOKE") + object GET_ARRAY_ELEMENT : IrOperatorImpl("GET_ARRAY_ELEMENT") object PREFIX_INCR : IrOperatorImpl("PREFIX_INCR") object PREFIX_DECR : IrOperatorImpl("PREFIX_DECR") diff --git a/compiler/testData/ir/irText/arrayAccess.kt b/compiler/testData/ir/irText/arrayAccess.kt new file mode 100644 index 00000000000..aea4a7cc602 --- /dev/null +++ b/compiler/testData/ir/irText/arrayAccess.kt @@ -0,0 +1,5 @@ +val p = 0 +fun foo() = 1 + +fun test(a: IntArray) = + a[0] + a[p] + a[foo()] \ No newline at end of file diff --git a/compiler/testData/ir/irText/arrayAccess.txt b/compiler/testData/ir/irText/arrayAccess.txt new file mode 100644 index 00000000000..53d51cbc75e --- /dev/null +++ b/compiler/testData/ir/irText/arrayAccess.txt @@ -0,0 +1,24 @@ +IrFile /arrayAccess.kt + IrProperty public val p: kotlin.Int = 0 getter=null setter=null + IrExpressionBody + CONST Int type=kotlin.Int value='0' + IrFunction public fun foo(): kotlin.Int + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + CONST Int type=kotlin.Int value='1' + IrFunction public fun test(/*0*/ a: kotlin.IntArray): kotlin.Int + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + CALL .plus type=kotlin.Int operator=PLUS + $this: CALL .plus type=kotlin.Int operator=PLUS + $this: CALL .get type=kotlin.Int operator=GET_ARRAY_ELEMENT + $this: GET_VAR a type=kotlin.IntArray operator=null + index: CONST Int type=kotlin.Int value='0' + other: CALL .get type=kotlin.Int operator=GET_ARRAY_ELEMENT + $this: GET_VAR a type=kotlin.IntArray operator=null + index: CALL . type=kotlin.Int operator=GET_PROPERTY + other: CALL .get type=kotlin.Int operator=GET_ARRAY_ELEMENT + $this: GET_VAR a type=kotlin.IntArray operator=null + index: CALL .foo type=kotlin.Int operator=null diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index ba835ae9315..1d5e0842dfa 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -35,6 +35,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("arrayAccess.kt") + public void testArrayAccess() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/arrayAccess.kt"); + doTest(fileName); + } + @TestMetadata("arrayAssignment.kt") public void testArrayAssignment() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/arrayAssignment.kt");