Skip to content

Commit 16cb4c5

Browse files
committed
Kotlin: Extract extension binary operators
1 parent 70561ca commit 16cb4c5

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,9 @@ open class KotlinFileExtractor(
22352235
result
22362236
}
22372237

2238+
private fun isFunction(target: IrFunction, pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, vararg fNames: String, isNullable: Boolean? = false) =
2239+
fNames.any { isFunction(target, pkgName, classNameLogged, classNamePredicate, it, isNullable) }
2240+
22382241
private fun isFunction(target: IrFunction, pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, fName: String, isNullable: Boolean? = false): Boolean {
22392242
val verbose = false
22402243
fun verboseln(s: String) { if(verbose) println(s) }
@@ -2291,7 +2294,7 @@ open class KotlinFileExtractor(
22912294
isFunction(target, "kotlin", "Double", fName)
22922295
}
22932296

2294-
private fun isNumericFunction(target: IrFunction, fNames: List<String>) = fNames.any { isNumericFunction(target, it) }
2297+
private fun isNumericFunction(target: IrFunction, vararg fNames: String) = fNames.any { isNumericFunction(target, it) }
22952298

22962299
private fun isArrayType(typeName: String) =
22972300
when(typeName) {
@@ -2428,10 +2431,18 @@ open class KotlinFileExtractor(
24282431
binopReceiver(id, c.dispatchReceiver, "Dispatch receiver")
24292432
}
24302433

2434+
fun binopExt(id: Label<out DbExpr>) {
2435+
binopReceiver(id, c.extensionReceiver, "Extension receiver")
2436+
}
2437+
24312438
fun unaryopDisp(id: Label<out DbExpr>) {
24322439
unaryopReceiver(id, c.dispatchReceiver, "Dispatch receiver")
24332440
}
24342441

2442+
fun unaryopExt(id: Label<out DbExpr>) {
2443+
unaryopReceiver(id, c.extensionReceiver, "Extension receiver")
2444+
}
2445+
24352446
val dr = c.dispatchReceiver
24362447
when {
24372448
isFunction(target, "kotlin", "String", "plus", false) -> {
@@ -2446,7 +2457,7 @@ open class KotlinFileExtractor(
24462457
extractRawMethodAccess(stringPlusFn, c, c.type, callable, parent, idx, enclosingStmt, listOf(c.extensionReceiver, c.getValueArgument(0)), null, null)
24472458
}
24482459
}
2449-
isNumericFunction(target, listOf("plus", "minus", "times", "div", "rem", "and", "or", "xor", "shl", "shr", "ushr")) -> {
2460+
isNumericFunction(target, "plus", "minus", "times", "div", "rem", "and", "or", "xor", "shl", "shr", "ushr") -> {
24502461
val type = useType(c.type)
24512462
val id: Label<out DbExpr> = when (val targetName = target.name.asString()) {
24522463
"plus" -> {
@@ -2510,7 +2521,10 @@ open class KotlinFileExtractor(
25102521
}
25112522
}
25122523
tw.writeExprsKotlinType(id, type.kotlinResult.id)
2513-
binopDisp(id)
2524+
if (isFunction(target, "kotlin", "Byte or Short", { it == "Byte" || it == "Short" }, "and", "or", "xor"))
2525+
binopExt(id)
2526+
else
2527+
binopDisp(id)
25142528
}
25152529
// != gets desugared into not and ==. Here we resugar it.
25162530
c.origin == IrStatementOrigin.EXCLEQ && isFunction(target, "kotlin", "Boolean", "not") && c.valueArgumentsCount == 0 && dr != null && dr is IrCall && isBuiltinCallInternal(dr, "EQEQ") -> {
@@ -2541,7 +2555,7 @@ open class KotlinFileExtractor(
25412555
tw.writeExprsKotlinType(id, type.kotlinResult.id)
25422556
unaryopDisp(id)
25432557
}
2544-
isNumericFunction(target, listOf("inv", "unaryMinus", "unaryPlus")) -> {
2558+
isNumericFunction(target, "inv", "unaryMinus", "unaryPlus") -> {
25452559
val type = useType(c.type)
25462560
val id: Label<out DbExpr> = when (val targetName = target.name.asString()) {
25472561
"inv" -> {
@@ -2566,7 +2580,7 @@ open class KotlinFileExtractor(
25662580
}
25672581
tw.writeExprsKotlinType(id, type.kotlinResult.id)
25682582
if (isFunction(target, "kotlin", "Byte or Short", { it == "Byte" || it == "Short" }, "inv"))
2569-
unaryopReceiver(id, c.extensionReceiver, "Extension receiver")
2583+
unaryopExt(id)
25702584
else
25712585
unaryopDisp(id)
25722586
}

java/ql/test/kotlin/library-tests/exprs/CONSISTENCY/BinaryExpr.expected

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
| exprs.kt:47:5:47:25 | var ...; | LocalVariableDeclStmt | unexpected dead end |
2-
| exprs.kt:48:5:48:26 | var ...; | LocalVariableDeclStmt | unexpected dead end |
3-
| exprs.kt:49:5:49:26 | var ...; | LocalVariableDeclStmt | unexpected dead end |
4-
| exprs.kt:64:5:64:22 | var ...; | LocalVariableDeclStmt | unexpected dead end |
5-
| exprs.kt:65:5:65:23 | var ...; | LocalVariableDeclStmt | unexpected dead end |
6-
| exprs.kt:66:5:66:23 | var ...; | LocalVariableDeclStmt | unexpected dead end |
71
| exprs.kt:278:52:278:66 | <error expr> | ErrorExpr | unexpected dead end |
82
| exprs.kt:278:52:278:66 | { ... } | BlockStmt | unexpected dead end |

java/ql/test/kotlin/library-tests/exprs/CONSISTENCY/children.expected

Lines changed: 0 additions & 6 deletions
This file was deleted.

java/ql/test/kotlin/library-tests/exprs/CONSISTENCY/diags.expected

Lines changed: 0 additions & 7 deletions
This file was deleted.

java/ql/test/kotlin/library-tests/exprs/PrintAst.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,14 +2109,17 @@ exprs.kt:
21092109
# 47| 35: [LocalVariableDeclStmt] var ...;
21102110
# 47| 1: [LocalVariableDeclExpr] by14
21112111
# 47| 0: [OrBitwiseExpr] ... | ...
2112+
# 47| 0: [VarAccess] byx
21122113
# 47| 1: [VarAccess] byy
21132114
# 48| 36: [LocalVariableDeclStmt] var ...;
21142115
# 48| 1: [LocalVariableDeclExpr] by15
21152116
# 48| 0: [AndBitwiseExpr] ... & ...
2117+
# 48| 0: [VarAccess] byx
21162118
# 48| 1: [VarAccess] byy
21172119
# 49| 37: [LocalVariableDeclStmt] var ...;
21182120
# 49| 1: [LocalVariableDeclExpr] by16
21192121
# 49| 0: [XorBitwiseExpr] ... ^ ...
2122+
# 49| 0: [VarAccess] byx
21202123
# 49| 1: [VarAccess] byy
21212124
# 51| 38: [LocalVariableDeclStmt] var ...;
21222125
# 51| 1: [LocalVariableDeclExpr] s1
@@ -2196,14 +2199,17 @@ exprs.kt:
21962199
# 64| 51: [LocalVariableDeclStmt] var ...;
21972200
# 64| 1: [LocalVariableDeclExpr] s14
21982201
# 64| 0: [OrBitwiseExpr] ... | ...
2202+
# 64| 0: [VarAccess] sx
21992203
# 64| 1: [VarAccess] sy
22002204
# 65| 52: [LocalVariableDeclStmt] var ...;
22012205
# 65| 1: [LocalVariableDeclExpr] s15
22022206
# 65| 0: [AndBitwiseExpr] ... & ...
2207+
# 65| 0: [VarAccess] sx
22032208
# 65| 1: [VarAccess] sy
22042209
# 66| 53: [LocalVariableDeclStmt] var ...;
22052210
# 66| 1: [LocalVariableDeclExpr] s16
22062211
# 66| 0: [XorBitwiseExpr] ... ^ ...
2212+
# 66| 0: [VarAccess] sx
22072213
# 66| 1: [VarAccess] sy
22082214
# 68| 54: [LocalVariableDeclStmt] var ...;
22092215
# 68| 1: [LocalVariableDeclExpr] l1

java/ql/test/kotlin/library-tests/exprs/binop.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
| exprs.kt:44:16:44:25 | ... >= ... | exprs.kt:44:16:44:18 | intValue(...) | exprs.kt:44:23:44:25 | intValue(...) |
2929
| exprs.kt:45:16:45:26 | ... == ... | exprs.kt:45:16:45:18 | byx | exprs.kt:45:24:45:26 | byy |
3030
| exprs.kt:46:16:46:26 | ... != ... | exprs.kt:46:16:46:18 | byx | exprs.kt:46:24:46:26 | byy |
31-
| exprs.kt:47:16:47:25 | ... \| ... | file://:0:0:0:0 | <none> | exprs.kt:47:23:47:25 | byy |
32-
| exprs.kt:48:16:48:26 | ... & ... | file://:0:0:0:0 | <none> | exprs.kt:48:24:48:26 | byy |
33-
| exprs.kt:49:16:49:26 | ... ^ ... | file://:0:0:0:0 | <none> | exprs.kt:49:24:49:26 | byy |
31+
| exprs.kt:47:16:47:25 | ... \| ... | exprs.kt:47:16:47:18 | byx | exprs.kt:47:23:47:25 | byy |
32+
| exprs.kt:48:16:48:26 | ... & ... | exprs.kt:48:16:48:18 | byx | exprs.kt:48:24:48:26 | byy |
33+
| exprs.kt:49:16:49:26 | ... ^ ... | exprs.kt:49:16:49:18 | byx | exprs.kt:49:24:49:26 | byy |
3434
| exprs.kt:52:14:52:20 | ... + ... | exprs.kt:52:14:52:15 | sx | exprs.kt:52:19:52:20 | sy |
3535
| exprs.kt:53:14:53:20 | ... - ... | exprs.kt:53:14:53:15 | sx | exprs.kt:53:19:53:20 | sy |
3636
| exprs.kt:54:14:54:20 | ... / ... | exprs.kt:54:14:54:15 | sx | exprs.kt:54:19:54:20 | sy |
@@ -43,9 +43,9 @@
4343
| exprs.kt:61:15:61:22 | ... >= ... | exprs.kt:61:15:61:16 | intValue(...) | exprs.kt:61:21:61:22 | intValue(...) |
4444
| exprs.kt:62:15:62:23 | ... == ... | exprs.kt:62:15:62:16 | sx | exprs.kt:62:22:62:23 | sy |
4545
| exprs.kt:63:15:63:23 | ... != ... | exprs.kt:63:15:63:16 | sx | exprs.kt:63:22:63:23 | sy |
46-
| exprs.kt:64:15:64:22 | ... \| ... | file://:0:0:0:0 | <none> | exprs.kt:64:21:64:22 | sy |
47-
| exprs.kt:65:15:65:23 | ... & ... | file://:0:0:0:0 | <none> | exprs.kt:65:22:65:23 | sy |
48-
| exprs.kt:66:15:66:23 | ... ^ ... | file://:0:0:0:0 | <none> | exprs.kt:66:22:66:23 | sy |
46+
| exprs.kt:64:15:64:22 | ... \| ... | exprs.kt:64:15:64:16 | sx | exprs.kt:64:21:64:22 | sy |
47+
| exprs.kt:65:15:65:23 | ... & ... | exprs.kt:65:15:65:16 | sx | exprs.kt:65:22:65:23 | sy |
48+
| exprs.kt:66:15:66:23 | ... ^ ... | exprs.kt:66:15:66:16 | sx | exprs.kt:66:22:66:23 | sy |
4949
| exprs.kt:69:14:69:20 | ... + ... | exprs.kt:69:14:69:15 | lx | exprs.kt:69:19:69:20 | ly |
5050
| exprs.kt:70:14:70:20 | ... - ... | exprs.kt:70:14:70:15 | lx | exprs.kt:70:19:70:20 | ly |
5151
| exprs.kt:71:14:71:20 | ... / ... | exprs.kt:71:14:71:15 | lx | exprs.kt:71:19:71:20 | ly |

java/ql/test/kotlin/library-tests/exprs/exprs.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,12 +1057,15 @@
10571057
| exprs.kt:46:16:46:26 | ... != ... | exprs.kt:4:1:142:1 | topLevelMethod | NEExpr |
10581058
| exprs.kt:46:24:46:26 | byy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10591059
| exprs.kt:47:5:47:25 | by14 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1060+
| exprs.kt:47:16:47:18 | byx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10601061
| exprs.kt:47:16:47:25 | ... \| ... | exprs.kt:4:1:142:1 | topLevelMethod | OrBitwiseExpr |
10611062
| exprs.kt:47:23:47:25 | byy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10621063
| exprs.kt:48:5:48:26 | by15 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1064+
| exprs.kt:48:16:48:18 | byx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10631065
| exprs.kt:48:16:48:26 | ... & ... | exprs.kt:4:1:142:1 | topLevelMethod | AndBitwiseExpr |
10641066
| exprs.kt:48:24:48:26 | byy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10651067
| exprs.kt:49:5:49:26 | by16 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1068+
| exprs.kt:49:16:49:18 | byx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10661069
| exprs.kt:49:16:49:26 | ... ^ ... | exprs.kt:4:1:142:1 | topLevelMethod | XorBitwiseExpr |
10671070
| exprs.kt:49:24:49:26 | byy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
10681071
| exprs.kt:51:5:51:16 | s1 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
@@ -1128,12 +1131,15 @@
11281131
| exprs.kt:63:15:63:23 | ... != ... | exprs.kt:4:1:142:1 | topLevelMethod | NEExpr |
11291132
| exprs.kt:63:22:63:23 | sy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11301133
| exprs.kt:64:5:64:22 | s14 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1134+
| exprs.kt:64:15:64:16 | sx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11311135
| exprs.kt:64:15:64:22 | ... \| ... | exprs.kt:4:1:142:1 | topLevelMethod | OrBitwiseExpr |
11321136
| exprs.kt:64:21:64:22 | sy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11331137
| exprs.kt:65:5:65:23 | s15 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1138+
| exprs.kt:65:15:65:16 | sx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11341139
| exprs.kt:65:15:65:23 | ... & ... | exprs.kt:4:1:142:1 | topLevelMethod | AndBitwiseExpr |
11351140
| exprs.kt:65:22:65:23 | sy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11361141
| exprs.kt:66:5:66:23 | s16 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |
1142+
| exprs.kt:66:15:66:16 | sx | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11371143
| exprs.kt:66:15:66:23 | ... ^ ... | exprs.kt:4:1:142:1 | topLevelMethod | XorBitwiseExpr |
11381144
| exprs.kt:66:22:66:23 | sy | exprs.kt:4:1:142:1 | topLevelMethod | VarAccess |
11391145
| exprs.kt:68:5:68:16 | l1 | exprs.kt:4:1:142:1 | topLevelMethod | LocalVariableDeclExpr |

0 commit comments

Comments
 (0)