Use smart casts in hasSideEffects()

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent 8f0dfd2725
commit bde818230b
  1. 29
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt

@ -1,10 +1,15 @@
package dev.openrs2.deob.ast.util package dev.openrs2.deob.ast.util
import com.github.javaparser.ast.expr.ArrayAccessExpr
import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.BooleanLiteralExpr import com.github.javaparser.ast.expr.BooleanLiteralExpr
import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.FieldAccessExpr
import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.IntegerLiteralExpr
import com.github.javaparser.ast.expr.LiteralExpr
import com.github.javaparser.ast.expr.LongLiteralExpr import com.github.javaparser.ast.expr.LongLiteralExpr
import com.github.javaparser.ast.expr.NameExpr
import com.github.javaparser.ast.expr.ThisExpr
import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.expr.UnaryExpr
fun IntegerLiteralExpr.checkedAsInt(): Int { fun IntegerLiteralExpr.checkedAsInt(): Int {
@ -116,23 +121,15 @@ fun Expression.countNots(): Int {
} }
fun Expression.hasSideEffects(): Boolean { fun Expression.hasSideEffects(): Boolean {
if (isLiteralExpr || isNameExpr || isThisExpr) { return when (this) {
return false is LiteralExpr, is NameExpr, is ThisExpr -> false
} else if (isUnaryExpr) { is UnaryExpr -> expression.hasSideEffects()
return asUnaryExpr().expression.hasSideEffects() is BinaryExpr -> left.hasSideEffects() || right.hasSideEffects()
} else if (isBinaryExpr) { is ArrayAccessExpr -> name.hasSideEffects() || index.hasSideEffects()
val binary = asBinaryExpr() is FieldAccessExpr -> scope.hasSideEffects()
return binary.left.hasSideEffects() || binary.right.hasSideEffects() // TODO(gpe): more cases
} else if (isArrayAccessExpr) { else -> true
val access = asArrayAccessExpr()
return access.name.hasSideEffects() || access.index.hasSideEffects()
} else if (isFieldAccessExpr) {
val access = asFieldAccessExpr()
return access.scope.hasSideEffects()
} }
// TODO(gpe): more cases
return true
} }
fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? { fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {

Loading…
Cancel
Save