Skip to content

Commit a2d0b38

Browse files
committed
Kotlin: extract all binary numeric operators
1 parent 343e45e commit a2d0b38

File tree

5 files changed

+126
-90
lines changed

5 files changed

+126
-90
lines changed

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

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,8 @@ open class KotlinFileExtractor(
17651765
isFunction(target, "kotlin", "Double", fName)
17661766
}
17671767

1768+
private fun isNumericFunction(target: IrFunction, fNames: List<String>) = fNames.any { isNumericFunction(target, it) }
1769+
17681770
private fun isArrayType(typeName: String) =
17691771
when(typeName) {
17701772
"Array" -> true
@@ -1901,47 +1903,81 @@ open class KotlinFileExtractor(
19011903

19021904
val dr = c.dispatchReceiver
19031905
when {
1904-
isNumericFunction(target, "plus")
1905-
|| isFunction(target, "kotlin", "String", "plus", false) -> {
1906+
isFunction(target, "kotlin", "String", "plus", false) -> {
19061907
val id = tw.getFreshIdLabel<DbAddexpr>()
19071908
val type = useType(c.type)
19081909
tw.writeExprs_addexpr(id, type.javaResult.id, parent, idx)
19091910
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1910-
if (c.extensionReceiver != null)
1911-
binopExtensionMethod(id)
1912-
else
1913-
binopDisp(id)
1911+
binopDisp(id)
19141912
}
19151913
isFunction(target, "kotlin", "String", "plus", true) -> {
19161914
findJdkIntrinsicOrWarn("stringPlus", c)?.let { stringPlusFn ->
19171915
extractRawMethodAccess(stringPlusFn, c, callable, parent, idx, enclosingStmt, listOf(c.extensionReceiver, c.getValueArgument(0)), null, null)
19181916
}
19191917
}
1920-
isNumericFunction(target, "minus") -> {
1921-
val id = tw.getFreshIdLabel<DbSubexpr>()
1922-
val type = useType(c.type)
1923-
tw.writeExprs_subexpr(id, type.javaResult.id, parent, idx)
1924-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1925-
binopDisp(id)
1926-
}
1927-
isNumericFunction(target, "times") -> {
1928-
val id = tw.getFreshIdLabel<DbMulexpr>()
1929-
val type = useType(c.type)
1930-
tw.writeExprs_mulexpr(id, type.javaResult.id, parent, idx)
1931-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1932-
binopDisp(id)
1933-
}
1934-
isNumericFunction(target, "div") -> {
1935-
val id = tw.getFreshIdLabel<DbDivexpr>()
1918+
isNumericFunction(target, listOf("plus", "minus", "times", "div", "rem", "and", "or", "xor", "shl", "shr", "ushr")) -> {
19361919
val type = useType(c.type)
1937-
tw.writeExprs_divexpr(id, type.javaResult.id, parent, idx)
1938-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1939-
binopDisp(id)
1940-
}
1941-
isNumericFunction(target, "rem") -> {
1942-
val id = tw.getFreshIdLabel<DbRemexpr>()
1943-
val type = useType(c.type)
1944-
tw.writeExprs_remexpr(id, type.javaResult.id, parent, idx)
1920+
val id: Label<out DbExpr> = when (val targetName = target.name.asString()) {
1921+
"plus" -> {
1922+
val id = tw.getFreshIdLabel<DbAddexpr>()
1923+
tw.writeExprs_addexpr(id, type.javaResult.id, parent, idx)
1924+
id
1925+
}
1926+
"minus" -> {
1927+
val id = tw.getFreshIdLabel<DbSubexpr>()
1928+
tw.writeExprs_subexpr(id, type.javaResult.id, parent, idx)
1929+
id
1930+
}
1931+
"times" -> {
1932+
val id = tw.getFreshIdLabel<DbMulexpr>()
1933+
tw.writeExprs_mulexpr(id, type.javaResult.id, parent, idx)
1934+
id
1935+
}
1936+
"div" -> {
1937+
val id = tw.getFreshIdLabel<DbDivexpr>()
1938+
tw.writeExprs_divexpr(id, type.javaResult.id, parent, idx)
1939+
id
1940+
}
1941+
"rem" -> {
1942+
val id = tw.getFreshIdLabel<DbRemexpr>()
1943+
tw.writeExprs_remexpr(id, type.javaResult.id, parent, idx)
1944+
id
1945+
}
1946+
"and" -> {
1947+
val id = tw.getFreshIdLabel<DbAndbitexpr>()
1948+
tw.writeExprs_andbitexpr(id, type.javaResult.id, parent, idx)
1949+
id
1950+
}
1951+
"or" -> {
1952+
val id = tw.getFreshIdLabel<DbOrbitexpr>()
1953+
tw.writeExprs_orbitexpr(id, type.javaResult.id, parent, idx)
1954+
id
1955+
}
1956+
"xor" -> {
1957+
val id = tw.getFreshIdLabel<DbXorbitexpr>()
1958+
tw.writeExprs_xorbitexpr(id, type.javaResult.id, parent, idx)
1959+
id
1960+
}
1961+
"shl" -> {
1962+
val id = tw.getFreshIdLabel<DbLshiftexpr>()
1963+
tw.writeExprs_lshiftexpr(id, type.javaResult.id, parent, idx)
1964+
id
1965+
}
1966+
"shr" -> {
1967+
val id = tw.getFreshIdLabel<DbRshiftexpr>()
1968+
tw.writeExprs_rshiftexpr(id, type.javaResult.id, parent, idx)
1969+
id
1970+
}
1971+
"ushr" -> {
1972+
val id = tw.getFreshIdLabel<DbUrshiftexpr>()
1973+
tw.writeExprs_urshiftexpr(id, type.javaResult.id, parent, idx)
1974+
id
1975+
}
1976+
else -> {
1977+
logger.errorElement("Unhandled target name: $targetName", c)
1978+
return
1979+
}
1980+
}
19451981
tw.writeExprsKotlinType(id, type.kotlinResult.id)
19461982
binopDisp(id)
19471983
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,11 @@
1212
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
1313
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
1414
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
15-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.and in java.lang.Integer |
1615
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.dec in java.lang.Integer |
1716
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.inv in java.lang.Integer |
18-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.or in java.lang.Integer |
1917
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.rangeTo in java.lang.Integer |
2018
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.rangeTo in java.lang.Integer |
21-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.shl in java.lang.Integer |
22-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.shr in java.lang.Integer |
23-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.ushr in java.lang.Integer |
24-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.xor in java.lang.Integer |
25-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.and in java.lang.Long |
2619
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.inv in java.lang.Long |
27-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.or in java.lang.Long |
28-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.shl in java.lang.Long |
29-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.shr in java.lang.Long |
30-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.ushr in java.lang.Long |
31-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.xor in java.lang.Long |
3220
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
3321
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
3422
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,34 +1944,34 @@ exprs.kt:
19441944
# 15| 1: [VarAccess] y
19451945
# 16| 5: [LocalVariableDeclStmt] var ...;
19461946
# 16| 1: [LocalVariableDeclExpr] i6
1947-
# 16| 0: [MethodAccess] shl(...)
1948-
# 16| -1: [VarAccess] x
1949-
# 16| 0: [VarAccess] y
1947+
# 16| 0: [LShiftExpr] ... << ...
1948+
# 16| 0: [VarAccess] x
1949+
# 16| 1: [VarAccess] y
19501950
# 17| 6: [LocalVariableDeclStmt] var ...;
19511951
# 17| 1: [LocalVariableDeclExpr] i7
1952-
# 17| 0: [MethodAccess] shr(...)
1953-
# 17| -1: [VarAccess] x
1954-
# 17| 0: [VarAccess] y
1952+
# 17| 0: [RShiftExpr] ... >> ...
1953+
# 17| 0: [VarAccess] x
1954+
# 17| 1: [VarAccess] y
19551955
# 18| 7: [LocalVariableDeclStmt] var ...;
19561956
# 18| 1: [LocalVariableDeclExpr] i8
1957-
# 18| 0: [MethodAccess] ushr(...)
1958-
# 18| -1: [VarAccess] x
1959-
# 18| 0: [VarAccess] y
1957+
# 18| 0: [URShiftExpr] ... >>> ...
1958+
# 18| 0: [VarAccess] x
1959+
# 18| 1: [VarAccess] y
19601960
# 19| 8: [LocalVariableDeclStmt] var ...;
19611961
# 19| 1: [LocalVariableDeclExpr] i9
1962-
# 19| 0: [MethodAccess] and(...)
1963-
# 19| -1: [VarAccess] x
1964-
# 19| 0: [VarAccess] y
1962+
# 19| 0: [AndBitwiseExpr] ... & ...
1963+
# 19| 0: [VarAccess] x
1964+
# 19| 1: [VarAccess] y
19651965
# 20| 9: [LocalVariableDeclStmt] var ...;
19661966
# 20| 1: [LocalVariableDeclExpr] i10
1967-
# 20| 0: [MethodAccess] or(...)
1968-
# 20| -1: [VarAccess] x
1969-
# 20| 0: [VarAccess] y
1967+
# 20| 0: [OrBitwiseExpr] ... | ...
1968+
# 20| 0: [VarAccess] x
1969+
# 20| 1: [VarAccess] y
19701970
# 21| 10: [LocalVariableDeclStmt] var ...;
19711971
# 21| 1: [LocalVariableDeclExpr] i11
1972-
# 21| 0: [MethodAccess] xor(...)
1973-
# 21| -1: [VarAccess] x
1974-
# 21| 0: [VarAccess] y
1972+
# 21| 0: [XorBitwiseExpr] ... ^ ...
1973+
# 21| 0: [VarAccess] x
1974+
# 21| 1: [VarAccess] y
19751975
# 22| 11: [LocalVariableDeclStmt] var ...;
19761976
# 22| 1: [LocalVariableDeclExpr] i12
19771977
# 22| 0: [MethodAccess] inv(...)
@@ -2206,34 +2206,34 @@ exprs.kt:
22062206
# 66| 1: [VarAccess] ly
22072207
# 67| 53: [LocalVariableDeclStmt] var ...;
22082208
# 67| 1: [LocalVariableDeclExpr] l6
2209-
# 67| 0: [MethodAccess] shl(...)
2210-
# 67| -1: [VarAccess] lx
2211-
# 67| 0: [VarAccess] y
2209+
# 67| 0: [LShiftExpr] ... << ...
2210+
# 67| 0: [VarAccess] lx
2211+
# 67| 1: [VarAccess] y
22122212
# 68| 54: [LocalVariableDeclStmt] var ...;
22132213
# 68| 1: [LocalVariableDeclExpr] l7
2214-
# 68| 0: [MethodAccess] shr(...)
2215-
# 68| -1: [VarAccess] lx
2216-
# 68| 0: [VarAccess] y
2214+
# 68| 0: [RShiftExpr] ... >> ...
2215+
# 68| 0: [VarAccess] lx
2216+
# 68| 1: [VarAccess] y
22172217
# 69| 55: [LocalVariableDeclStmt] var ...;
22182218
# 69| 1: [LocalVariableDeclExpr] l8
2219-
# 69| 0: [MethodAccess] ushr(...)
2220-
# 69| -1: [VarAccess] lx
2221-
# 69| 0: [VarAccess] y
2219+
# 69| 0: [URShiftExpr] ... >>> ...
2220+
# 69| 0: [VarAccess] lx
2221+
# 69| 1: [VarAccess] y
22222222
# 70| 56: [LocalVariableDeclStmt] var ...;
22232223
# 70| 1: [LocalVariableDeclExpr] l9
2224-
# 70| 0: [MethodAccess] and(...)
2225-
# 70| -1: [VarAccess] lx
2226-
# 70| 0: [VarAccess] ly
2224+
# 70| 0: [AndBitwiseExpr] ... & ...
2225+
# 70| 0: [VarAccess] lx
2226+
# 70| 1: [VarAccess] ly
22272227
# 71| 57: [LocalVariableDeclStmt] var ...;
22282228
# 71| 1: [LocalVariableDeclExpr] l10
2229-
# 71| 0: [MethodAccess] or(...)
2230-
# 71| -1: [VarAccess] lx
2231-
# 71| 0: [VarAccess] ly
2229+
# 71| 0: [OrBitwiseExpr] ... | ...
2230+
# 71| 0: [VarAccess] lx
2231+
# 71| 1: [VarAccess] ly
22322232
# 72| 58: [LocalVariableDeclStmt] var ...;
22332233
# 72| 1: [LocalVariableDeclExpr] l11
2234-
# 72| 0: [MethodAccess] xor(...)
2235-
# 72| -1: [VarAccess] lx
2236-
# 72| 0: [VarAccess] ly
2234+
# 72| 0: [XorBitwiseExpr] ... ^ ...
2235+
# 72| 0: [VarAccess] lx
2236+
# 72| 1: [VarAccess] ly
22372237
# 73| 59: [LocalVariableDeclStmt] var ...;
22382238
# 73| 1: [LocalVariableDeclExpr] l12
22392239
# 73| 0: [MethodAccess] inv(...)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
| exprs.kt:13:14:13:18 | ... - ... | exprs.kt:13:14:13:14 | x | exprs.kt:13:18:13:18 | y |
33
| exprs.kt:14:14:14:18 | ... / ... | exprs.kt:14:14:14:14 | x | exprs.kt:14:18:14:18 | y |
44
| exprs.kt:15:14:15:18 | ... % ... | exprs.kt:15:14:15:14 | x | exprs.kt:15:18:15:18 | y |
5+
| exprs.kt:16:14:16:20 | ... << ... | exprs.kt:16:14:16:14 | x | exprs.kt:16:20:16:20 | y |
6+
| exprs.kt:17:14:17:20 | ... >> ... | exprs.kt:17:14:17:14 | x | exprs.kt:17:20:17:20 | y |
7+
| exprs.kt:18:14:18:21 | ... >>> ... | exprs.kt:18:14:18:14 | x | exprs.kt:18:21:18:21 | y |
8+
| exprs.kt:19:14:19:20 | ... & ... | exprs.kt:19:14:19:14 | x | exprs.kt:19:20:19:20 | y |
9+
| exprs.kt:20:15:20:20 | ... \| ... | exprs.kt:20:15:20:15 | x | exprs.kt:20:20:20:20 | y |
10+
| exprs.kt:21:15:21:21 | ... ^ ... | exprs.kt:21:15:21:15 | x | exprs.kt:21:21:21:21 | y |
511
| exprs.kt:23:15:23:20 | ... (value equals) ... | exprs.kt:23:15:23:15 | x | exprs.kt:23:20:23:20 | y |
612
| exprs.kt:24:15:24:20 | ... (value not-equals) ... | exprs.kt:24:15:24:15 | x | exprs.kt:24:20:24:20 | y |
713
| exprs.kt:25:15:25:19 | ... < ... | exprs.kt:25:15:25:15 | x | exprs.kt:25:19:25:19 | y |
@@ -38,6 +44,12 @@
3844
| exprs.kt:64:14:64:20 | ... - ... | exprs.kt:64:14:64:15 | lx | exprs.kt:64:19:64:20 | ly |
3945
| exprs.kt:65:14:65:20 | ... / ... | exprs.kt:65:14:65:15 | lx | exprs.kt:65:19:65:20 | ly |
4046
| exprs.kt:66:14:66:20 | ... % ... | exprs.kt:66:14:66:15 | lx | exprs.kt:66:19:66:20 | ly |
47+
| exprs.kt:67:14:67:21 | ... << ... | exprs.kt:67:14:67:15 | lx | exprs.kt:67:21:67:21 | y |
48+
| exprs.kt:68:14:68:21 | ... >> ... | exprs.kt:68:14:68:15 | lx | exprs.kt:68:21:68:21 | y |
49+
| exprs.kt:69:14:69:22 | ... >>> ... | exprs.kt:69:14:69:15 | lx | exprs.kt:69:22:69:22 | y |
50+
| exprs.kt:70:14:70:22 | ... & ... | exprs.kt:70:14:70:15 | lx | exprs.kt:70:21:70:22 | ly |
51+
| exprs.kt:71:15:71:22 | ... \| ... | exprs.kt:71:15:71:16 | lx | exprs.kt:71:21:71:22 | ly |
52+
| exprs.kt:72:15:72:23 | ... ^ ... | exprs.kt:72:15:72:16 | lx | exprs.kt:72:22:72:23 | ly |
4153
| exprs.kt:74:15:74:22 | ... (value equals) ... | exprs.kt:74:15:74:16 | lx | exprs.kt:74:21:74:22 | ly |
4254
| exprs.kt:75:15:75:22 | ... (value not-equals) ... | exprs.kt:75:15:75:16 | lx | exprs.kt:75:21:75:22 | ly |
4355
| exprs.kt:76:15:76:21 | ... < ... | exprs.kt:76:15:76:16 | lx | exprs.kt:76:20:76:21 | ly |

0 commit comments

Comments
 (0)