Skip to content

Commit ea0a04a

Browse files
committed
Kotlin: extract unary plus and minus operators
1 parent 2e72ec7 commit ea0a04a

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,7 @@ open class KotlinFileExtractor(
20942094
id
20952095
}
20962096
else -> {
2097-
logger.errorElement("Unhandled target name: $targetName", c)
2097+
logger.errorElement("Unhandled binary target name: $targetName", c)
20982098
return
20992099
}
21002100
}
@@ -2130,10 +2130,29 @@ open class KotlinFileExtractor(
21302130
tw.writeExprsKotlinType(id, type.kotlinResult.id)
21312131
unaryopDisp(id)
21322132
}
2133-
isNumericFunction(target, "inv") -> {
2134-
val id = tw.getFreshIdLabel<DbBitnotexpr>()
2133+
isNumericFunction(target, listOf("inv", "unaryMinus", "unaryPlus")) -> {
21352134
val type = useType(c.type)
2136-
tw.writeExprs_bitnotexpr(id, type.javaResult.id, parent, idx)
2135+
val id: Label<out DbExpr> = when (val targetName = target.name.asString()) {
2136+
"inv" -> {
2137+
val id = tw.getFreshIdLabel<DbBitnotexpr>()
2138+
tw.writeExprs_bitnotexpr(id, type.javaResult.id, parent, idx)
2139+
id
2140+
}
2141+
"unaryMinus" -> {
2142+
val id = tw.getFreshIdLabel<DbMinusexpr>()
2143+
tw.writeExprs_minusexpr(id, type.javaResult.id, parent, idx)
2144+
id
2145+
}
2146+
"unaryPlus" -> {
2147+
val id = tw.getFreshIdLabel<DbPlusexpr>()
2148+
tw.writeExprs_plusexpr(id, type.javaResult.id, parent, idx)
2149+
id
2150+
}
2151+
else -> {
2152+
logger.errorElement("Unhandled unary target name: $targetName", c)
2153+
return
2154+
}
2155+
}
21372156
tw.writeExprsKotlinType(id, type.kotlinResult.id)
21382157
unaryopDisp(id)
21392158
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,15 +2866,23 @@ exprs.kt:
28662866
# 280| 0: [ExprStmt] <Expr>;
28672867
# 280| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
28682868
# 280| 0: [TypeAccess] Unit
2869+
# 280| 1: [MinusExpr] -...
2870+
# 280| 0: [VarAccess] i
28692871
# 281| 1: [ExprStmt] <Expr>;
28702872
# 281| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
28712873
# 281| 0: [TypeAccess] Unit
2874+
# 281| 1: [PlusExpr] +...
2875+
# 281| 0: [VarAccess] i
28722876
# 282| 2: [ExprStmt] <Expr>;
28732877
# 282| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
28742878
# 282| 0: [TypeAccess] Unit
2879+
# 282| 1: [MinusExpr] -...
2880+
# 282| 0: [VarAccess] d
28752881
# 283| 3: [ExprStmt] <Expr>;
28762882
# 283| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
28772883
# 283| 0: [TypeAccess] Unit
2884+
# 283| 1: [PlusExpr] +...
2885+
# 283| 0: [VarAccess] d
28782886
# 284| 4: [LocalVariableDeclStmt] var ...;
28792887
# 284| 1: [LocalVariableDeclExpr] i0
28802888
# 284| 0: [IntegerLiteral] 1

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,14 +1741,22 @@
17411741
| exprs.kt:279:1:294:1 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
17421742
| exprs.kt:279:16:279:21 | int | file://:0:0:0:0 | <none> | TypeAccess |
17431743
| exprs.kt:279:24:279:32 | double | file://:0:0:0:0 | <none> | TypeAccess |
1744+
| exprs.kt:280:5:280:6 | -... | exprs.kt:279:1:294:1 | unaryExprs | MinusExpr |
17441745
| exprs.kt:280:5:280:6 | <implicit coercion to unit> | exprs.kt:279:1:294:1 | unaryExprs | ImplicitCoercionToUnitExpr |
17451746
| exprs.kt:280:5:280:6 | Unit | exprs.kt:279:1:294:1 | unaryExprs | TypeAccess |
1747+
| exprs.kt:280:6:280:6 | i | exprs.kt:279:1:294:1 | unaryExprs | VarAccess |
1748+
| exprs.kt:281:5:281:6 | +... | exprs.kt:279:1:294:1 | unaryExprs | PlusExpr |
17461749
| exprs.kt:281:5:281:6 | <implicit coercion to unit> | exprs.kt:279:1:294:1 | unaryExprs | ImplicitCoercionToUnitExpr |
17471750
| exprs.kt:281:5:281:6 | Unit | exprs.kt:279:1:294:1 | unaryExprs | TypeAccess |
1751+
| exprs.kt:281:6:281:6 | i | exprs.kt:279:1:294:1 | unaryExprs | VarAccess |
1752+
| exprs.kt:282:5:282:6 | -... | exprs.kt:279:1:294:1 | unaryExprs | MinusExpr |
17481753
| exprs.kt:282:5:282:6 | <implicit coercion to unit> | exprs.kt:279:1:294:1 | unaryExprs | ImplicitCoercionToUnitExpr |
17491754
| exprs.kt:282:5:282:6 | Unit | exprs.kt:279:1:294:1 | unaryExprs | TypeAccess |
1755+
| exprs.kt:282:6:282:6 | d | exprs.kt:279:1:294:1 | unaryExprs | VarAccess |
1756+
| exprs.kt:283:5:283:6 | +... | exprs.kt:279:1:294:1 | unaryExprs | PlusExpr |
17501757
| exprs.kt:283:5:283:6 | <implicit coercion to unit> | exprs.kt:279:1:294:1 | unaryExprs | ImplicitCoercionToUnitExpr |
17511758
| exprs.kt:283:5:283:6 | Unit | exprs.kt:279:1:294:1 | unaryExprs | TypeAccess |
1759+
| exprs.kt:283:6:283:6 | d | exprs.kt:279:1:294:1 | unaryExprs | VarAccess |
17521760
| exprs.kt:284:5:284:14 | i0 | exprs.kt:279:1:294:1 | unaryExprs | LocalVariableDeclExpr |
17531761
| exprs.kt:284:14:284:14 | 1 | exprs.kt:279:1:294:1 | unaryExprs | IntegerLiteral |
17541762
| exprs.kt:285:5:285:14 | i1 | exprs.kt:279:1:294:1 | unaryExprs | LocalVariableDeclExpr |

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
| exprs.kt:196:19:196:20 | ...!! | exprs.kt:196:18:196:18 | x |
66
| exprs.kt:205:20:205:21 | ...!! | exprs.kt:205:19:205:19 | s |
77
| exprs.kt:206:20:206:21 | ...!! | exprs.kt:206:19:206:19 | s |
8+
| exprs.kt:280:5:280:6 | -... | exprs.kt:280:6:280:6 | i |
9+
| exprs.kt:281:5:281:6 | +... | exprs.kt:281:6:281:6 | i |
10+
| exprs.kt:282:5:282:6 | -... | exprs.kt:282:6:282:6 | d |
11+
| exprs.kt:283:5:283:6 | +... | exprs.kt:283:6:283:6 | d |

0 commit comments

Comments
 (0)