[FIR2IR] Recognize postfix inc/dec pattern from block statements

This commit is contained in:
Ivan Kochurkin
2021-10-14 15:00:42 +03:00
committed by TeamCityServer
parent 0d91e16a15
commit 3210a2a950
26 changed files with 19 additions and 59 deletions
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.parentClassOrNull
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -590,28 +591,25 @@ class Fir2IrVisitor(
}
private fun FirBlock.getBlockOrigin(): IrStatementOrigin? {
if (source?.kind !is FirFakeSourceElementKind.DesugaredIncrementOrDecrement) return null
if (statements.size != 3) return null
val isPrefix: Boolean
val incrementStatement = when (statements.size) {
2 -> {
isPrefix = true
statements[0]
}
3 -> {
isPrefix = false
statements[1]
}
else -> return null
}
val statement0 = statements[0]
if (statement0 !is FirProperty || !statement0.isLocal) return null
val unarySymbol = statement0.symbol
if (unarySymbol.callableId.callableName != SpecialNames.UNARY) return null
val statement2 = statements[2]
if (statement2 !is FirPropertyAccessExpression) return null
if (statement2.calleeReference.toResolvedCallableSymbol() != unarySymbol) return null
val incrementStatement = statements[1]
if (incrementStatement !is FirVariableAssignment) return null
val origin = incrementStatement.getIrAssignmentOrigin(isPrefix)
return if (origin == IrStatementOrigin.EQ)
null
else
origin
val variable = statement0.initializer?.toResolvedCallableSymbol() ?: return null
if (incrementStatement.lValue.toResolvedCallableSymbol() != variable) return null
val origin = incrementStatement.getIrAssignmentOrigin()
return if (origin == IrStatementOrigin.EQ) null else origin
}
override fun visitErrorExpression(errorExpression: FirErrorExpression, data: Any?): IrElement {
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,5 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
@@ -32,13 +32,13 @@ FILE fqName:<root> fileName:/incrementDecrement.kt
VAR name:x type:kotlin.Int [var]
CONST Int type=kotlin.Int value=0
VAR name:x1 type:kotlin.Int [val]
BLOCK type=kotlin.Int origin=PREFIX_INCR
BLOCK type=kotlin.Int origin=null
SET_VAR 'var x: kotlin.Int [var] declared in <root>.testVarPrefix' type=kotlin.Unit origin=PREFIX_INCR
CALL 'public final fun inc (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
$this: GET_VAR 'var x: kotlin.Int [var] declared in <root>.testVarPrefix' type=kotlin.Int origin=null
GET_VAR 'var x: kotlin.Int [var] declared in <root>.testVarPrefix' type=kotlin.Int origin=null
VAR name:x2 type:kotlin.Int [val]
BLOCK type=kotlin.Int origin=PREFIX_DECR
BLOCK type=kotlin.Int origin=null
SET_VAR 'var x: kotlin.Int [var] declared in <root>.testVarPrefix' type=kotlin.Unit origin=PREFIX_DECR
CALL 'public final fun dec (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
$this: GET_VAR 'var x: kotlin.Int [var] declared in <root>.testVarPrefix' type=kotlin.Int origin=null
@@ -23,7 +23,7 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
arg0: GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null
arg1: CONST Int type=kotlin.Int value=10
body: BLOCK type=kotlin.Int origin=null
body: BLOCK type=kotlin.Int origin=POSTFIX_INCR
VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Int [val]
GET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Int origin=null
SET_VAR 'var x: kotlin.Int [var] declared in <root>.test' type=kotlin.Unit origin=POSTFIX_INCR