Skip to content

Commit 1a5bd3c

Browse files
committed
KE2: Extract binary operators on numeric types
1 parent 71931c3 commit 1a5bd3c

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,11 +2981,6 @@ OLD: KE1
29812981
when {
29822982
isNumericFunction(
29832983
target,
2984-
"plus",
2985-
"minus",
2986-
"times",
2987-
"div",
2988-
"rem",
29892984
"and",
29902985
"or",
29912986
"xor",
@@ -2996,26 +2991,6 @@ OLD: KE1
29962991
val type = useType(c.type)
29972992
val id: Label<out DbExpr> =
29982993
when (val targetName = target.name.asString()) {
2999-
"minus" -> {
3000-
val id = tw.getFreshIdLabel<DbSubexpr>()
3001-
tw.writeExprs_subexpr(id, type.javaResult.id, parent, idx)
3002-
id
3003-
}
3004-
"times" -> {
3005-
val id = tw.getFreshIdLabel<DbMulexpr>()
3006-
tw.writeExprs_mulexpr(id, type.javaResult.id, parent, idx)
3007-
id
3008-
}
3009-
"div" -> {
3010-
val id = tw.getFreshIdLabel<DbDivexpr>()
3011-
tw.writeExprs_divexpr(id, type.javaResult.id, parent, idx)
3012-
id
3013-
}
3014-
"rem" -> {
3015-
val id = tw.getFreshIdLabel<DbRemexpr>()
3016-
tw.writeExprs_remexpr(id, type.javaResult.id, parent, idx)
3017-
id
3018-
}
30192994
"and" -> {
30202995
val id = tw.getFreshIdLabel<DbAndbitexpr>()
30212996
tw.writeExprs_andbitexpr(id, type.javaResult.id, parent, idx)

java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -297,37 +297,60 @@ private fun KotlinFileExtractor.extractBinaryExpression(
297297
val op = expression.operationToken
298298
val target = expression.resolveCallTarget()?.symbol
299299

300-
when (op) {
301-
KtTokens.PLUS -> {
302-
if (target == null) {
303-
TODO()
304-
}
305-
306-
if (target.isNumericWithName("plus") ||
307-
target.hasName("kotlin", "String", "plus") ||
308-
target.hasMatchingNames(
309-
CallableId(FqName("kotlin"), null, Name.identifier("plus")),
310-
ClassId(FqName("kotlin"), Name.identifier("String")),
311-
nullability = KaTypeNullability.NULLABLE,
312-
)
313-
) {
314-
val id = tw.getFreshIdLabel<DbAddexpr>()
315-
val type = useType(expression.expressionType)
316-
val exprParent = parent.expr(expression, callable)
317-
tw.writeExprs_addexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx)
318-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
300+
if (target == null) {
301+
TODO()
302+
}
319303

320-
extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt)
321-
extractExpressionExpr(expression.left!!, callable, id, 0, exprParent.enclosingStmt)
322-
extractExpressionExpr(expression.right!!, callable, id, 1, exprParent.enclosingStmt)
323-
} else {
324-
TODO("Extract as method call")
325-
}
304+
if (op == KtTokens.PLUS && target.isBinaryPlus()) {
305+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_addexpr)
306+
} else if (op == KtTokens.MINUS && target.isNumericWithName("minus")) {
307+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_subexpr)
308+
} else if (op == KtTokens.MUL && target.isNumericWithName("times")) {
309+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_mulexpr)
310+
} else if (op == KtTokens.DIV && target.isNumericWithName("div")) {
311+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_divexpr)
312+
} else if (op == KtTokens.PERC && target.isNumericWithName("rem")) {
313+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_remexpr)
314+
} else {
315+
if (op !in listOf(KtTokens.PLUS, KtTokens.MINUS, KtTokens.MUL, KtTokens.DIV, KtTokens.PERC)) {
316+
TODO("Unhandled binary op")
326317
}
327318

328-
else -> TODO()
319+
TODO("Extract as method call")
329320
}
321+
}
330322

323+
private fun KaFunctionSymbol.isBinaryPlus(): Boolean {
324+
return this.isNumericWithName("plus") ||
325+
this.hasName("kotlin", "String", "plus") ||
326+
this.hasMatchingNames(
327+
CallableId(FqName("kotlin"), null, Name.identifier("plus")),
328+
ClassId(FqName("kotlin"), Name.identifier("String")),
329+
nullability = KaTypeNullability.NULLABLE,
330+
)
331+
}
332+
333+
context(KaSession)
334+
private fun <T : DbBinaryexpr> KotlinFileExtractor.extractBinaryExpression(
335+
expression: KtBinaryExpression,
336+
callable: Label<out DbCallable>,
337+
parent: StmtExprParent,
338+
extractExpression: (
339+
id: Label<out T>,
340+
typeid: Label<out DbType>,
341+
parent: Label<out DbExprparent>,
342+
idx: Int
343+
) -> Unit
344+
) {
345+
val id = tw.getFreshIdLabel<T>()
346+
val type = useType(expression.expressionType)
347+
val exprParent = parent.expr(expression, callable)
348+
extractExpression(id, type.javaResult.id, exprParent.parent, exprParent.idx)
349+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
350+
351+
extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt)
352+
extractExpressionExpr(expression.left!!, callable, id, 0, exprParent.enclosingStmt)
353+
extractExpressionExpr(expression.right!!, callable, id, 1, exprParent.enclosingStmt)
331354
}
332355

333356
context(KaSession)

0 commit comments

Comments
 (0)