Skip to content

Commit

Permalink
More precise types in arithmetic evaluation
Browse files Browse the repository at this point in the history
Previously, we just casted most of our numeric types to `Long` when doing arithmetic operations in the `ValueEvalutor`. This PR tries to optimize that, to at least keep the type if both are the same. It is still somewhat inprecise when two different types of different bitwidth are involved. In these cases we mostly just still cast to `Long`.
  • Loading branch information
oxisto committed Aug 25, 2023
1 parent 4c54110 commit d0c4ea0
Show file tree
Hide file tree
Showing 6 changed files with 654 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,17 @@ class MultiValueEvaluator : ValueEvaluator() {
return when (expr.operatorCode) {
"-" -> {
when (val input = evaluateInternal(expr.input, depth + 1)) {
is Collection<*> -> input.map { n -> (n as? Number)?.negate() }
is Number -> input.negate()
else -> cannotEvaluate(expr, this)
is Collection<*> -> input.map { n -> (n as? Number)?.unaryMinus() }
else -> super.handleUnaryOp(expr, depth)
}
}
"--" -> {
if (expr.astParent is ForStatement) {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
is Number -> input.decrement()
is Collection<*> -> input.map { n -> (n as? Number)?.decrement() }
else -> cannotEvaluate(expr, this)
is Collection<*> -> input.map { n -> (n as? Number)?.dec() }
else -> super.handleUnaryOp(expr, depth)
}
}
}
Expand All @@ -202,15 +200,14 @@ class MultiValueEvaluator : ValueEvaluator() {
evaluateInternal(expr.input, depth + 1)
} else {
when (val input = evaluateInternal(expr.input, depth + 1)) {
is Number -> input.increment()
is Collection<*> -> input.map { n -> (n as? Number)?.increment() }
else -> cannotEvaluate(expr, this)
is Collection<*> -> input.map { n -> (n as? Number)?.inc() }
else -> super.handleUnaryOp(expr, depth)
}
}
}
"*" -> evaluateInternal(expr.input, depth + 1)
"&" -> evaluateInternal(expr.input, depth + 1)
else -> cannotEvaluate(expr, this)
else -> super.handleUnaryOp(expr, depth)
}
}

Expand Down Expand Up @@ -418,22 +415,22 @@ class MultiValueEvaluator : ValueEvaluator() {
return when (expr.operatorCode) {
"-" -> {
when (input) {
is Collection<*> -> input.map { n -> (n as? Number)?.negate() }
is Number -> input.negate()
is Collection<*> -> input.map { n -> (n as? Number)?.unaryMinus() }
is Number -> -input
else -> cannotEvaluate(expr, this)
}
}
"--" -> {
when (input) {
is Number -> input.decrement()
is Collection<*> -> input.map { n -> (n as? Number)?.decrement() }
is Number -> input.dec()
is Collection<*> -> input.map { n -> (n as? Number)?.dec() }
else -> cannotEvaluate(expr, this)
}
}
"++" -> {
when (input) {
is Number -> input.increment()
is Collection<*> -> input.map { n -> (n as? Number)?.increment() }
is Number -> input.inc()
is Collection<*> -> input.map { n -> (n as? Number)?.inc() }
else -> cannotEvaluate(expr, this)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
/*
* Copyright (c) 2023, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.analysis

internal operator fun Number.plus(other: Number): Number =
when (this) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Int.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Byte.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Long.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Short.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Float.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Double.plus(other: Number): Number =
when (other) {
is Int -> this + other
is Byte -> this + other
is Long -> this + other
is Short -> this + other
is Float -> this + other
is Double -> this + other
else -> throw UnsupportedOperationException()
}

internal operator fun Number.minus(other: Number): Number =
when (this) {
is Int -> this + -other
is Byte -> this + -other
is Long -> this + -other
is Short -> this + -other
is Float -> this + -other
is Double -> this + -other
else -> throw UnsupportedOperationException()
}

internal operator fun Number.times(other: Number): Number =
when (this) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Int.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Byte.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Long.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Short.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Float.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Double.times(other: Number): Number =
when (other) {
is Int -> this * other
is Byte -> this * other
is Long -> this * other
is Short -> this * other
is Float -> this * other
is Double -> this * other
else -> throw UnsupportedOperationException()
}

internal operator fun Number.div(other: Number): Number =
when (this) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Int.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Byte.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Long.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Short.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Float.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Double.div(other: Number): Number =
when (other) {
is Int -> this / other
is Byte -> this / other
is Long -> this / other
is Short -> this / other
is Float -> this / other
is Double -> this / other
else -> throw UnsupportedOperationException()
}

internal operator fun Number.unaryMinus(): Number =
when (this) {
is Int -> -this
is Byte -> -this
is Long -> -this
is Short -> -this
is Float -> -this
is Double -> -this
else -> throw UnsupportedOperationException()
}

internal operator fun Number.inc(): Number =
when (this) {
is Int -> this + 1
is Byte -> this + 1
is Long -> this + 1
is Short -> this + 1
is Float -> this + 1
is Double -> this + 1
else -> throw UnsupportedOperationException()
}

internal operator fun Number.dec(): Number =
when (this) {
is Int -> this - 1
is Byte -> this - 1
is Long -> this - 1
is Short -> this - 1
is Float -> this - 1
is Double -> this - 1
else -> throw UnsupportedOperationException()
}
Loading

0 comments on commit d0c4ea0

Please sign in to comment.