Add new intrinsic methods: unaryPlus, unaryMinus

This commit is contained in:
Yan Zhulanow
2015-10-12 20:24:12 +03:00
parent 2fba80168d
commit f7ce0c2d20
5 changed files with 22 additions and 2 deletions
@@ -80,7 +80,9 @@ public class IntrinsicMethods {
for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) {
String typeName = type.getTypeName().asString();
declareIntrinsicFunction(typeName, "plus", 0, UNARY_PLUS);
declareIntrinsicFunction(typeName, "unaryPlus", 0, UNARY_PLUS);
declareIntrinsicFunction(typeName, "minus", 0, UNARY_MINUS);
declareIntrinsicFunction(typeName, "unaryMinus", 0, UNARY_MINUS);
declareIntrinsicFunction(typeName, "inv", 0, INV);
declareIntrinsicFunction(typeName, "rangeTo", 1, RANGE_TO);
declareIntrinsicFunction(typeName, "inc", 0, INC);
@@ -455,7 +455,7 @@ private class ConstantExpressionEvaluatorVisitor(
return result
}
assert(isIntegerType(receiver.value)) { "Only integer constants should be checked for overflow" }
assert(name == "minus") { "Only negation should be checked for overflow" }
assert(name == "minus" || name == "unaryMinus") { "Only negation should be checked for overflow" }
if (receiver.value == result) {
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<JetExpression>() ?: callExpression))
@@ -37,6 +37,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(BYTE, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(BYTE, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(BYTE, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(BYTE, "unaryMinus", { a -> a.minus() }, { a -> a.minus() }),
unaryOperation(BYTE, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(BYTE, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(CHAR, "toByte", { a -> a.toByte() }, emptyUnaryFun),
unaryOperation(CHAR, "toChar", { a -> a.toChar() }, emptyUnaryFun),
@@ -55,6 +57,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(DOUBLE, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(DOUBLE, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(DOUBLE, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(DOUBLE, "unaryMinus", { a -> a.minus() }, emptyUnaryFun),
unaryOperation(DOUBLE, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(DOUBLE, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(FLOAT, "minus", { a -> a.minus() }, emptyUnaryFun),
unaryOperation(FLOAT, "plus", { a -> a.plus() }, emptyUnaryFun),
@@ -65,6 +69,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(FLOAT, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(FLOAT, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(FLOAT, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(FLOAT, "unaryMinus", { a -> a.minus() }, emptyUnaryFun),
unaryOperation(FLOAT, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(FLOAT, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(INT, "inv", { a -> a.inv() }, emptyUnaryFun),
unaryOperation(INT, "minus", { a -> a.minus() }, { a -> a.minus() }),
@@ -76,6 +82,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(INT, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(INT, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(INT, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(INT, "unaryMinus", { a -> a.minus() }, { a -> a.minus() }),
unaryOperation(INT, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(INT, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(LONG, "inv", { a -> a.inv() }, emptyUnaryFun),
unaryOperation(LONG, "minus", { a -> a.minus() }, { a -> a.minus() }),
@@ -87,6 +95,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(LONG, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(LONG, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(LONG, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(LONG, "unaryMinus", { a -> a.minus() }, { a -> a.minus() }),
unaryOperation(LONG, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(LONG, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(SHORT, "minus", { a -> a.minus() }, { a -> a.minus() }),
unaryOperation(SHORT, "plus", { a -> a.plus() }, emptyUnaryFun),
@@ -97,6 +107,8 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
unaryOperation(SHORT, "toInt", { a -> a.toInt() }, emptyUnaryFun),
unaryOperation(SHORT, "toLong", { a -> a.toLong() }, emptyUnaryFun),
unaryOperation(SHORT, "toShort", { a -> a.toShort() }, emptyUnaryFun),
unaryOperation(SHORT, "unaryMinus", { a -> a.minus() }, { a -> a.minus() }),
unaryOperation(SHORT, "unaryPlus", { a -> a.plus() }, emptyUnaryFun),
unaryOperation(SHORT, "toString", { a -> a.toString() }, emptyUnaryFun),
unaryOperation(STRING, "length", { a -> a.length() }, emptyUnaryFun),
unaryOperation(STRING, "toString", { a -> a.toString() }, emptyUnaryFun)
@@ -129,7 +129,7 @@ fun renderCheckUnaryOperation(name: String, params: List<JetType>): String {
}
return when(name) {
"minus" -> "{ a -> a.$name() }"
"unaryMinus", "minus" -> "{ a -> a.$name() }"
else -> "emptyUnaryFun"
}
}
@@ -165,6 +165,12 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
return null;
}
if (pattern("Char.unaryPlus()").apply(descriptor)) {
return CHAR_PLUS;
}
if (pattern("Char.unaryMinus()").apply(descriptor)) {
return CHAR_MINUS;
}
if (pattern("Char.plus()").apply(descriptor)) {
return CHAR_PLUS;
}