From d280a41062158982e8678cdcc65b6e490ab13618 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Fri, 22 Nov 2024 13:39:55 +0000 Subject: [PATCH] KE2: Small refactoring Avoids shadowing `trapWriterWriteExpr`, and removes the need to check for an impossible case. --- .../src/main/kotlin/entities/Expression.kt | 113 +++++++++--------- 1 file changed, 54 insertions(+), 59 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index 72723446fdce..f078ae3890e9 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -385,69 +385,64 @@ private fun KotlinFileExtractor.extractBinaryExpression( return extractBinaryExpression(expression, callable, parent, trapWriterWriteExpr) } - when { - op in listOf(KtTokens.LT, KtTokens.GT, KtTokens.LTEQ, KtTokens.GTEQ) -> { - // Extract lowered equivalent call, such as `a.compareTo(b) < 0` instead of `a < b` in the below: - // ``` - // fun test(a: Data, b: Data) { - // a < b - // } - // - // class Data(val v: Int) : Comparable { - // override fun compareTo(other: Data) = v.compareTo(other.v) - // } - // ``` - - val exprParent = parent.expr(expression, callable) - - val trapWriterWriteExpr = when (op) { - KtTokens.LT -> tw::writeExprs_ltexpr - KtTokens.GT -> tw::writeExprs_gtexpr - KtTokens.LTEQ -> tw::writeExprs_leexpr - KtTokens.GTEQ -> tw::writeExprs_geexpr - else -> TODO("error") - } - - val id = extractRawBinaryExpression(builtinTypes.boolean, exprParent, trapWriterWriteExpr) - extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt) - - extractRawMethodAccess( - target, - tw.getLocation(expression), - target.returnType, - callable, - id, - 0, - exprParent.enclosingStmt, - if (target.isExtension) null else expression.left!!, - if (target.isExtension) expression.left!! else null, - listOf(expression.right!!) - ) - - extractConstantInteger( - "0", - builtinTypes.int, - 0, - tw.getLocation(expression), - id, - 1, - callable, - exprParent.enclosingStmt, - /* - OLD: KE1 - overrideId = overrideId - */ - ) + val trapWriterWriteExprComparison = when (op) { + KtTokens.LT -> tw::writeExprs_ltexpr + KtTokens.GT -> tw::writeExprs_gtexpr + KtTokens.LTEQ -> tw::writeExprs_leexpr + KtTokens.GTEQ -> tw::writeExprs_geexpr + else -> null + } - return id - } + if (trapWriterWriteExprComparison != null) { + // Extract lowered equivalent call, such as `a.compareTo(b) < 0` instead of `a < b` in the below: + // ``` + // fun test(a: Data, b: Data) { + // a < b + // } + // + // class Data(val v: Int) : Comparable { + // override fun compareTo(other: Data) = v.compareTo(other.v) + // } + // ``` + + val exprParent = parent.expr(expression, callable) + + val id = extractRawBinaryExpression(builtinTypes.boolean, exprParent, trapWriterWriteExprComparison) + extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt) + + extractRawMethodAccess( + target, + tw.getLocation(expression), + target.returnType, + callable, + id, + 0, + exprParent.enclosingStmt, + if (target.isExtension) null else expression.left!!, + if (target.isExtension) expression.left!! else null, + listOf(expression.right!!) + ) - else -> { - // todo: other operators, such as .., ..<, in, !in, =, +=, -=, *=, /=, %=, ==, !=, - TODO("Extract as method call") - } + extractConstantInteger( + "0", + builtinTypes.int, + 0, + tw.getLocation(expression), + id, + 1, + callable, + exprParent.enclosingStmt, + /* + OLD: KE1 + overrideId = overrideId + */ + ) + return id } + + // todo: other operators, such as .., ..<, in, !in, =, +=, -=, *=, /=, %=, ==, !=, + TODO("Extract as method call") }