Warning for integer overflow
This commit is contained in:
@@ -40,11 +40,14 @@ fun generate(): String {
|
||||
p.println(FileUtil.loadFile(File("injector-generator/copyright.txt")))
|
||||
p.println("package org.jetbrains.jet.lang.evaluate")
|
||||
p.println()
|
||||
p.println("import java.math.BigInteger")
|
||||
p.println("import java.util.HashMap")
|
||||
p.println()
|
||||
p.println("/** This file is generated by org.jetbrains.jet.generators.evaluate:generate(). DO NOT MODIFY MANUALLY */")
|
||||
p.println()
|
||||
|
||||
val unaryOperationsMap = arrayListOf<Pair<String, List<String>>>()
|
||||
val binaryOperationsMap = arrayListOf<Pair<String, List<String>>>()
|
||||
val unaryOperationsMap = arrayListOf<Pair<String, List<JetType>>>()
|
||||
val binaryOperationsMap = arrayListOf<Pair<String, List<JetType>>>()
|
||||
|
||||
val builtIns = KotlinBuiltIns.getInstance()
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
@@ -67,18 +70,24 @@ fun generate(): String {
|
||||
}
|
||||
}
|
||||
|
||||
p.println("private val unaryOperations = hashMapOf<UnaryOperationKey<*>, (Any?) -> Any>(")
|
||||
p.println("private val emptyBinaryFun: Function2<BigInteger, BigInteger, BigInteger> = { a, b -> BigInteger(\"0\") }")
|
||||
p.println("private val emptyUnaryFun: Function1<Long, Long> = { a -> 1.toLong() }")
|
||||
p.println()
|
||||
p.println("private val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?, Any>, Function1<Long, Long>>>")
|
||||
p.println(" = hashMapOf<UnaryOperationKey<*>, Pair<Function1<Any?, Any>, Function1<Long, Long>>>(")
|
||||
p.pushIndent()
|
||||
|
||||
val unaryOperationsMapIterator = unaryOperationsMap.iterator()
|
||||
while (unaryOperationsMapIterator.hasNext()) {
|
||||
val (funcName, parameters) = unaryOperationsMapIterator.next()
|
||||
p.print(
|
||||
"unaryOperationKey(",
|
||||
parameters.makeString(", "),
|
||||
"unaryOperation(",
|
||||
parameters.map { it.asSrting() }.makeString(", "),
|
||||
", ",
|
||||
"\"$funcName\"",
|
||||
", { a -> a.${funcName}() })"
|
||||
", { a -> a.${funcName}() }, ",
|
||||
renderCheckUnaryOperation(funcName, parameters),
|
||||
")"
|
||||
)
|
||||
if (unaryOperationsMapIterator.hasNext()) {
|
||||
p.printWithNoIndent(", ")
|
||||
@@ -90,19 +99,21 @@ fun generate(): String {
|
||||
|
||||
p.println()
|
||||
|
||||
|
||||
p.println("private val binaryOperations = hashMapOf<BinaryOperationKey<*, *>, (Any?, Any?) -> Any>(")
|
||||
p.println("private val binaryOperations: HashMap<BinaryOperationKey<*, *>, Pair<Function2<Any?, Any?, Any>, Function2<BigInteger, BigInteger, BigInteger>>>")
|
||||
p.println(" = hashMapOf<BinaryOperationKey<*, *>, Pair<Function2<Any?, Any?, Any>, Function2<BigInteger, BigInteger, BigInteger>>>(")
|
||||
p.pushIndent()
|
||||
|
||||
val binaryOperationsMapIterator = binaryOperationsMap.iterator()
|
||||
while (binaryOperationsMapIterator.hasNext()) {
|
||||
val (funcName, parameters) = binaryOperationsMapIterator.next()
|
||||
p.print(
|
||||
"binaryOperationKey(",
|
||||
parameters.makeString(", "),
|
||||
"binaryOperation(",
|
||||
parameters.map { it.asSrting() }.makeString(", "),
|
||||
", ",
|
||||
"\"$funcName\"",
|
||||
", { a, b -> a.${funcName}(b) })"
|
||||
", { a, b -> a.${funcName}(b) }, ",
|
||||
renderCheckBinaryOperation(funcName, parameters),
|
||||
")"
|
||||
)
|
||||
if (binaryOperationsMapIterator.hasNext()) {
|
||||
p.printWithNoIndent(", ")
|
||||
@@ -115,12 +126,52 @@ fun generate(): String {
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun FunctionDescriptor.getParametersTypes(): List<String> {
|
||||
val list = arrayListOf(getExpectedThisObject()!!.getType().asSrting().toUpperCase())
|
||||
fun renderCheckUnaryOperation(name: String, params: List<JetType>): String {
|
||||
val isAllParamsIntegers = params.fold(true) { a, b -> a && b.isIntegerType() }
|
||||
if (!isAllParamsIntegers) {
|
||||
return "emptyUnaryFun"
|
||||
}
|
||||
|
||||
return when(name) {
|
||||
"minus" -> "{ a -> a.$name() }"
|
||||
else -> "emptyUnaryFun"
|
||||
}
|
||||
}
|
||||
|
||||
fun renderCheckBinaryOperation(name: String, params: List<JetType>): String {
|
||||
val isAllParamsIntegers = params.fold(true) { a, b -> a && b.isIntegerType() }
|
||||
if (!isAllParamsIntegers) {
|
||||
return "emptyBinaryFun"
|
||||
}
|
||||
|
||||
return when(name) {
|
||||
"plus" -> "{ a, b -> a.add(b) }"
|
||||
"minus" -> "{ a, b -> a.subtract(b) }"
|
||||
"div" -> "{ a, b -> a.divide(b) }"
|
||||
"times" -> "{ a, b -> a.multiply(b) }"
|
||||
"mod",
|
||||
"xor",
|
||||
"or",
|
||||
"and" -> "{ a, b -> a.$name(b) }"
|
||||
else -> "emptyBinaryFun"
|
||||
}
|
||||
}
|
||||
|
||||
private fun JetType.isIntegerType(): Boolean {
|
||||
val builtIns = KotlinBuiltIns.getInstance()
|
||||
return this == builtIns.getIntType() ||
|
||||
this == builtIns.getShortType() ||
|
||||
this == builtIns.getByteType() ||
|
||||
this == builtIns.getLongType()
|
||||
}
|
||||
|
||||
|
||||
private fun FunctionDescriptor.getParametersTypes(): List<JetType> {
|
||||
val list = arrayListOf(getExpectedThisObject()!!.getType())
|
||||
getValueParameters().map { it.getType() }.forEach {
|
||||
list.add(TypeUtils.makeNotNullable(it).asSrting().toUpperCase())
|
||||
list.add(TypeUtils.makeNotNullable(it))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
private fun JetType.asSrting(): String = getConstructor().getDeclarationDescriptor()!!.getName().asString()
|
||||
private fun JetType.asSrting(): String = getConstructor().getDeclarationDescriptor()!!.getName().asString().toUpperCase()
|
||||
|
||||
Reference in New Issue
Block a user