Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise types in arithmetic evaluation #1290

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading