From bde818230b6edfa930a53f728c41115626624278 Mon Sep 17 00:00:00 2001 From: Graham Date: Wed, 5 Aug 2020 11:32:59 +0100 Subject: [PATCH] Use smart casts in hasSideEffects() Signed-off-by: Graham --- .../dev/openrs2/deob/ast/util/ExprUtils.kt | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt index fb4202a807..7feef152ba 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt @@ -1,10 +1,15 @@ 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.BooleanLiteralExpr 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.LiteralExpr 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 fun IntegerLiteralExpr.checkedAsInt(): Int { @@ -116,23 +121,15 @@ fun Expression.countNots(): Int { } fun Expression.hasSideEffects(): Boolean { - if (isLiteralExpr || isNameExpr || isThisExpr) { - return false - } else if (isUnaryExpr) { - return asUnaryExpr().expression.hasSideEffects() - } else if (isBinaryExpr) { - val binary = asBinaryExpr() - return binary.left.hasSideEffects() || binary.right.hasSideEffects() - } else if (isArrayAccessExpr) { - val access = asArrayAccessExpr() - return access.name.hasSideEffects() || access.index.hasSideEffects() - } else if (isFieldAccessExpr) { - val access = asFieldAccessExpr() - return access.scope.hasSideEffects() + return when (this) { + is LiteralExpr, is NameExpr, is ThisExpr -> false + is UnaryExpr -> expression.hasSideEffects() + is BinaryExpr -> left.hasSideEffects() || right.hasSideEffects() + is ArrayAccessExpr -> name.hasSideEffects() || index.hasSideEffects() + is FieldAccessExpr -> scope.hasSideEffects() + // TODO(gpe): more cases + else -> true } - - // TODO(gpe): more cases - return true } fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {