Skip to content

Commit c8cdf84

Browse files
committed
KE2: Extract binary operators on numeric types
1 parent 9a4cd21 commit c8cdf84

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
@@ -3697,11 +3697,6 @@ OLD: KE1
36973697
when {
36983698
isNumericFunction(
36993699
target,
3700-
"plus",
3701-
"minus",
3702-
"times",
3703-
"div",
3704-
"rem",
37053700
"and",
37063701
"or",
37073702
"xor",
@@ -3712,26 +3707,6 @@ OLD: KE1
37123707
val type = useType(c.type)
37133708
val id: Label<out DbExpr> =
37143709
when (val targetName = target.name.asString()) {
3715-
"minus" -> {
3716-
val id = tw.getFreshIdLabel<DbSubexpr>()
3717-
tw.writeExprs_subexpr(id, type.javaResult.id, parent, idx)
3718-
id
3719-
}
3720-
"times" -> {
3721-
val id = tw.getFreshIdLabel<DbMulexpr>()
3722-
tw.writeExprs_mulexpr(id, type.javaResult.id, parent, idx)
3723-
id
3724-
}
3725-
"div" -> {
3726-
val id = tw.getFreshIdLabel<DbDivexpr>()
3727-
tw.writeExprs_divexpr(id, type.javaResult.id, parent, idx)
3728-
id
3729-
}
3730-
"rem" -> {
3731-
val id = tw.getFreshIdLabel<DbRemexpr>()
3732-
tw.writeExprs_remexpr(id, type.javaResult.id, parent, idx)
3733-
id
3734-
}
37353710
"and" -> {
37363711
val id = tw.getFreshIdLabel<DbAndbitexpr>()
37373712
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
@@ -290,37 +290,60 @@ private fun KotlinFileExtractor.extractBinaryExpression(
290290
val op = expression.operationToken
291291
val target = ((expression.resolveToCall() as? KaSuccessCallInfo)?.call as? KaSimpleFunctionCall)?.symbol
292292

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

313-
extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt)
314-
extractExpressionExpr(expression.left!!, callable, id, 0, exprParent.enclosingStmt)
315-
extractExpressionExpr(expression.right!!, callable, id, 1, exprParent.enclosingStmt)
316-
} else {
317-
TODO("Extract as method call")
318-
}
297+
if (op == KtTokens.PLUS && target.isBinaryPlus()) {
298+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_addexpr)
299+
} else if (op == KtTokens.MINUS && target.isNumericWithName("minus")) {
300+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_subexpr)
301+
} else if (op == KtTokens.MUL && target.isNumericWithName("times")) {
302+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_mulexpr)
303+
} else if (op == KtTokens.DIV && target.isNumericWithName("div")) {
304+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_divexpr)
305+
} else if (op == KtTokens.PERC && target.isNumericWithName("rem")) {
306+
extractBinaryExpression(expression, callable, parent, tw::writeExprs_remexpr)
307+
} else {
308+
if (op !in listOf(KtTokens.PLUS, KtTokens.MINUS, KtTokens.MUL, KtTokens.DIV, KtTokens.PERC)) {
309+
TODO("Unhandled binary op")
319310
}
320311

321-
else -> TODO()
312+
TODO("Extract as method call")
322313
}
314+
}
323315

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

326349
context(KaSession)

0 commit comments

Comments
 (0)