Handle infix function calls in binary expressions.
This commit is contained in:
committed by
Dmitry Petrov
parent
3614b0a06c
commit
ace98b91cc
+7
-2
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,29 @@
|
||||
IrFile /booleanOperators.kt
|
||||
IrFunction public fun test1(/*0*/ a: kotlin.Boolean, /*1*/ b: kotlin.Boolean): kotlin.Boolean
|
||||
IrExpressionBody
|
||||
BLOCK type=<no-type> hasResult=false operator=null
|
||||
RETURN type=<no-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=<no-type> hasResult=false operator=null
|
||||
RETURN type=<no-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=<no-type> hasResult=false operator=null
|
||||
RETURN type=<no-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=<no-type> hasResult=false operator=null
|
||||
RETURN type=<no-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
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user