From ace98b91cc73f2d90a1734b7063b70396d862b87 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 18 Aug 2016 19:23:17 +0300 Subject: [PATCH] Handle infix function calls in binary expressions. --- .../IrOperatorExpressionGenerator.kt | 9 ++++-- .../testData/ir/irText/booleanOperators.kt | 5 ++++ .../testData/ir/irText/booleanOperators.txt | 29 +++++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 6 ++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/ir/irText/booleanOperators.kt create mode 100644 compiler/testData/ir/irText/booleanOperators.txt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/IrOperatorExpressionGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/IrOperatorExpressionGenerator.kt index a31e0f955dc..2e1516ddad8 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/IrOperatorExpressionGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/IrOperatorExpressionGenerator.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi2ir.generators import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtArrayAccessExpression import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtExpression @@ -49,6 +50,10 @@ class IrOperatorExpressionGenerator(val irStatementGenerator: IrStatementGenerat fun generateBinaryExpression(expression: KtBinaryExpression): IrExpression { val ktOperator = expression.operationReference.getReferencedNameElementType() + if (ktOperator == KtTokens.IDENTIFIER) { + return generateBinaryOperatorWithConventionalCall(expression, null) + } + val irOperator = getIrBinaryOperator(ktOperator) return when (irOperator) { @@ -83,7 +88,7 @@ class IrOperatorExpressionGenerator(val irStatementGenerator: IrStatementGenerat val irArgument1 = irStatementGenerator.generateExpression(expression.right!!).toExpectedType(context.builtIns.booleanType) return IrBinaryOperatorExpressionImpl( expression.startOffset, expression.endOffset, context.builtIns.booleanType, - irOperator, null, irArgument0, irArgument1 + irOperator, getResolvedCall(expression)?.resultingDescriptor, irArgument0, irArgument1 ) } @@ -147,7 +152,7 @@ class IrOperatorExpressionGenerator(val irStatementGenerator: IrStatementGenerat ) } - private fun generateBinaryOperatorWithConventionalCall(expression: KtBinaryExpression, irOperator: IrOperator): IrExpression { + private fun generateBinaryOperatorWithConventionalCall(expression: KtBinaryExpression, irOperator: IrOperator?): IrExpression { val operatorCall = getResolvedCall(expression)!! return IrCallGenerator(irStatementGenerator).generateCall(expression, operatorCall, irOperator) } diff --git a/compiler/testData/ir/irText/booleanOperators.kt b/compiler/testData/ir/irText/booleanOperators.kt new file mode 100644 index 00000000000..b9053f71d01 --- /dev/null +++ b/compiler/testData/ir/irText/booleanOperators.kt @@ -0,0 +1,5 @@ +fun test1(a: Boolean, b: Boolean) = a && b +fun test2(a: Boolean, b: Boolean) = a || b + +fun test1x(a: Boolean, b: Boolean) = a and b +fun test2x(a: Boolean, b: Boolean) = a or b diff --git a/compiler/testData/ir/irText/booleanOperators.txt b/compiler/testData/ir/irText/booleanOperators.txt new file mode 100644 index 00000000000..d0eff01dfad --- /dev/null +++ b/compiler/testData/ir/irText/booleanOperators.txt @@ -0,0 +1,29 @@ +IrFile /booleanOperators.kt + IrFunction public fun test1(/*0*/ a: kotlin.Boolean, /*1*/ b: kotlin.Boolean): kotlin.Boolean + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + BINARY_OP operator=ANDAND type=kotlin.Boolean related=null + GET_VAR a type=kotlin.Boolean operator=null + GET_VAR b type=kotlin.Boolean operator=null + IrFunction public fun test2(/*0*/ a: kotlin.Boolean, /*1*/ b: kotlin.Boolean): kotlin.Boolean + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + BINARY_OP operator=OROR type=kotlin.Boolean related=null + GET_VAR a type=kotlin.Boolean operator=null + GET_VAR b type=kotlin.Boolean operator=null + IrFunction public fun test1x(/*0*/ a: kotlin.Boolean, /*1*/ b: kotlin.Boolean): kotlin.Boolean + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + CALL .and type=kotlin.Boolean operator=null + $this: GET_VAR a type=kotlin.Boolean operator=null + other: GET_VAR b type=kotlin.Boolean operator=null + IrFunction public fun test2x(/*0*/ a: kotlin.Boolean, /*1*/ b: kotlin.Boolean): kotlin.Boolean + IrExpressionBody + BLOCK type= hasResult=false operator=null + RETURN type= + CALL .or type=kotlin.Boolean operator=null + $this: GET_VAR a type=kotlin.Boolean operator=null + other: GET_VAR b type=kotlin.Boolean operator=null diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index ee59950f158..2b36e661de4 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -71,6 +71,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { doTest(fileName); } + @TestMetadata("booleanOperators.kt") + public void testBooleanOperators() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/booleanOperators.kt"); + doTest(fileName); + } + @TestMetadata("boxOk.kt") public void testBoxOk() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/boxOk.kt");