From 0a988584b7324544a6a074baebf03fd05ceb181a Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 15 Feb 2020 21:02:30 +0000 Subject: [PATCH] Convert some deob-ast methods to extension methods --- .../deob/ast/transform/AddSubTransformer.kt | 20 +++-- .../transform/BinaryExprOrderTransformer.kt | 26 +++---- .../ast/transform/ComplementTransformer.kt | 22 +++--- .../deob/ast/transform/IdentityTransformer.kt | 36 +++++---- .../deob/ast/transform/IfElseTransformer.kt | 74 +++++++++---------- .../ast/transform/IncrementTransformer.kt | 14 ++-- 6 files changed, 91 insertions(+), 101 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt index eee041ed..99592dd0 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -21,11 +21,11 @@ class AddSubTransformer : Transformer() { return@walk } - if (op == BinaryExpr.Operator.PLUS && isNegative(right)) { + if (op == BinaryExpr.Operator.PLUS && right.isNegative()) { // x + -y => x - y expr.operator = BinaryExpr.Operator.MINUS expr.right = right.negate() - } else if (op == BinaryExpr.Operator.PLUS && isNegative(left)) { + } else if (op == BinaryExpr.Operator.PLUS && left.isNegative()) { if (expr.hasSideEffects()) { return@walk } @@ -34,7 +34,7 @@ class AddSubTransformer : Transformer() { expr.operator = BinaryExpr.Operator.MINUS expr.left = right.clone() expr.right = left.negate() - } else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { + } else if (op == BinaryExpr.Operator.MINUS && right.isNegative()) { // x - -y => x + y expr.operator = BinaryExpr.Operator.PLUS expr.right = right.negate() @@ -42,14 +42,12 @@ class AddSubTransformer : Transformer() { } } - companion object { - private fun isNegative(expr: Expression): Boolean { - return when { - expr.isUnaryExpr -> expr.asUnaryExpr().operator == UnaryExpr.Operator.MINUS - expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asInt() < 0 - expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asLong() < 0 - else -> false - } + private fun Expression.isNegative(): Boolean { + return when { + isUnaryExpr -> asUnaryExpr().operator == UnaryExpr.Operator.MINUS + isIntegerLiteralExpr -> asIntegerLiteralExpr().asInt() < 0 + isLongLiteralExpr -> asLongLiteralExpr().asLong() < 0 + else -> false } } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt index 64f59a3d..a38d9e98 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt @@ -8,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class BinaryExprOrderTransformer : Transformer() { override fun transform(unit: CompilationUnit) { unit.walk { expr: BinaryExpr -> - val op = flip(expr.operator) ?: return@walk + val op = expr.operator.flip() ?: return@walk val type = expr.calculateResolvedType() if (op == BinaryExpr.Operator.PLUS && type.isString()) { @@ -25,19 +25,17 @@ class BinaryExprOrderTransformer : Transformer() { } } - companion object { - private fun flip(op: BinaryExpr.Operator): BinaryExpr.Operator? { - return when (op) { - BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> op - BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op - BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> op - BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> op - BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS - BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS - BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER - BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS - else -> null - } + private fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? { + return when (this) { + BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> this + BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> this + BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> this + BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> this + BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS + BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS + BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER + BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS + else -> null } } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt index 2ce1f82d..94778153 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -18,21 +18,21 @@ class ComplementTransformer : Transformer() { val right = expr.right val bothLiteral = left.isIntegerOrLongLiteral() && right.isIntegerOrLongLiteral() - if (isComplementOrLiteral(left) && isComplementOrLiteral(right) && !bothLiteral) { + if (left.isComplementOrLiteral() && right.isComplementOrLiteral() && !bothLiteral) { expr.operator = op - expr.left = complement(left) - expr.right = complement(right) + expr.left = left.complement() + expr.right = right.complement() } } } companion object { - private fun isComplement(expr: Expression): Boolean { - return expr.isUnaryExpr && expr.asUnaryExpr().operator == UnaryExpr.Operator.BITWISE_COMPLEMENT + private fun Expression.isComplement(): Boolean { + return isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.BITWISE_COMPLEMENT } - private fun isComplementOrLiteral(expr: Expression): Boolean { - return isComplement(expr) || expr.isIntegerOrLongLiteral() + private fun Expression.isComplementOrLiteral(): Boolean { + return isComplement() || isIntegerOrLongLiteral() } private fun complement(op: BinaryExpr.Operator): BinaryExpr.Operator? { @@ -46,11 +46,11 @@ class ComplementTransformer : Transformer() { } } - private fun complement(expr: Expression): Expression { + private fun Expression.complement(): Expression { return when { - expr.isUnaryExpr -> expr.asUnaryExpr().expression - expr.isIntegerLiteralExpr -> IntegerLiteralExpr(expr.asIntegerLiteralExpr().asInt().inv()) - expr.isLongLiteralExpr -> createLong(expr.asLongLiteralExpr().asLong().inv()) + isUnaryExpr -> asUnaryExpr().expression + isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().asInt().inv()) + isLongLiteralExpr -> createLong(asLongLiteralExpr().asLong().inv()) else -> throw IllegalArgumentException() } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt index c4fdd662..8d8b05ab 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt @@ -12,28 +12,28 @@ class IdentityTransformer : Transformer() { @Suppress("NON_EXHAUSTIVE_WHEN") when (expr.operator) { BinaryExpr.Operator.PLUS -> { - if (isZero(expr.left)) { + if (expr.left.isZero()) { // 0 + x => x expr.replace(expr.right) - } else if (isZero(expr.right)) { + } else if (expr.right.isZero()) { // x + 0 => x expr.replace(expr.left) } } BinaryExpr.Operator.MINUS -> { - if (isZero(expr.left)) { + if (expr.left.isZero()) { // 0 - x => -x expr.replace(UnaryExpr(expr.right, UnaryExpr.Operator.MINUS)) - } else if (isZero(expr.right)) { + } else if (expr.right.isZero()) { // x - 0 => x expr.replace(expr.left) } } BinaryExpr.Operator.MULTIPLY -> { - if (isOne(expr.left)) { + if (expr.left.isOne()) { // 1 * x => x expr.replace(expr.right) - } else if (isOne(expr.right)) { + } else if (expr.right.isOne()) { // x * 1 => x expr.replace(expr.left) } @@ -42,21 +42,19 @@ class IdentityTransformer : Transformer() { } } - companion object { - private fun isZero(expr: Expression): Boolean { - return when { - expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 0 - expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 0L - else -> false - } + private fun Expression.isZero(): Boolean { + return when { + isIntegerLiteralExpr -> asIntegerLiteralExpr().asNumber() == 0 + isLongLiteralExpr -> asLongLiteralExpr().asNumber() == 0L + else -> false } + } - private fun isOne(expr: Expression): Boolean { - return when { - expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 1 - expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 1L - else -> false - } + private fun Expression.isOne(): Boolean { + return when { + isIntegerLiteralExpr -> asIntegerLiteralExpr().asNumber() == 1 + isLongLiteralExpr -> asLongLiteralExpr().asNumber() == 1L + else -> false } } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt index efd005e1..81c5ea63 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt @@ -14,11 +14,11 @@ class IfElseTransformer : Transformer() { stmt.elseStmt.ifPresent { elseStmt: Statement -> val condition = stmt.condition val thenStmt = stmt.thenStmt - if (isIf(thenStmt) && !isIf(elseStmt)) { + if (thenStmt.isIf() && !elseStmt.isIf()) { stmt.condition = condition.not() stmt.thenStmt = elseStmt.clone() stmt.setElseStmt(thenStmt.clone()) - } else if (!isIf(thenStmt) && isIf(elseStmt)) { + } else if (!thenStmt.isIf() && elseStmt.isIf()) { /* * Don't consider any more conditions for swapping the * if/else branches, as it'll introduce another level of @@ -45,8 +45,8 @@ class IfElseTransformer : Transformer() { unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> - if (isIf(elseStmt)) { - stmt.setElseStmt(getIf(elseStmt)) + if (elseStmt.isIf()) { + stmt.setElseStmt(elseStmt.getIf()) } } } @@ -97,7 +97,7 @@ class IfElseTransformer : Transformer() { } val thenStmt = ifStmt.thenStmt - if (!isTailThrowOrReturn(thenStmt)) { + if (!thenStmt.isTailThrowOrReturn()) { return@ifPresent } @@ -112,47 +112,45 @@ class IfElseTransformer : Transformer() { } } - companion object { - private fun isIf(stmt: Statement): Boolean { - return when { - stmt.isIfStmt -> true - stmt.isBlockStmt -> { - val stmts = stmt.asBlockStmt().statements - stmts.size == 1 && stmts[0].isIfStmt - } - else -> false + private fun Statement.isIf(): Boolean { + return when { + isIfStmt -> true + isBlockStmt -> { + val stmts = asBlockStmt().statements + stmts.size == 1 && stmts[0].isIfStmt } + else -> false } + } - private fun getIf(stmt: Statement): Statement { - if (stmt.isIfStmt) { - return stmt.clone() - } else if (stmt.isBlockStmt) { - val stmts = stmt.asBlockStmt().statements - if (stmts.size == 1) { - val head = stmts[0] - if (head.isIfStmt) { - return head.clone() - } + private fun Statement.getIf(): Statement { + if (isIfStmt) { + return clone() + } else if (isBlockStmt) { + val stmts = asBlockStmt().statements + if (stmts.size == 1) { + val head = stmts[0] + if (head.isIfStmt) { + return head.clone() } } - throw IllegalArgumentException() } + throw IllegalArgumentException() + } - private fun isTailThrowOrReturn(stmt: Statement): Boolean { - return if (stmt.isThrowStmt || stmt.isReturnStmt) { - true - } else if (stmt.isBlockStmt) { - val stmts = stmt.asBlockStmt().statements - if (stmts.isEmpty()) { - return false - } - - val tail = stmts[stmts.size - 1] - tail.isThrowStmt || tail.isReturnStmt - } else { - false + private fun Statement.isTailThrowOrReturn(): Boolean { + return if (isThrowStmt || isReturnStmt) { + true + } else if (isBlockStmt) { + val stmts = asBlockStmt().statements + if (stmts.isEmpty()) { + return false } + + val tail = stmts[stmts.size - 1] + tail.isThrowStmt || tail.isReturnStmt + } else { + false } } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt index bd577826..fcec6026 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -14,7 +14,7 @@ class IncrementTransformer : Transformer() { } val unaryExpr = stmt.expression.asUnaryExpr() - unaryExpr.operator = prefixToPostfix(unaryExpr.operator) + unaryExpr.operator = unaryExpr.operator.toPostfix() } unit.walk { stmt: ForStmt -> @@ -24,16 +24,14 @@ class IncrementTransformer : Transformer() { } val unaryExpr = expr.asUnaryExpr() - unaryExpr.operator = prefixToPostfix(unaryExpr.operator) + unaryExpr.operator = unaryExpr.operator.toPostfix() } } } - companion object { - private fun prefixToPostfix(operator: UnaryExpr.Operator) = when (operator) { - UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT - UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT - else -> operator - } + private fun UnaryExpr.Operator.toPostfix() = when (this) { + UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT + UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT + else -> this } }