From 4346fb5c23d449862810eb4fb4f7104e8155e63b Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 17 May 2017 12:21:47 +0300 Subject: [PATCH] Minor refactoring of JS AST nodes representing boolean literals --- .../kotlin/js/backend/ast/JsBooleanLiteral.java | 13 ++++++++++--- .../jetbrains/kotlin/js/backend/ast/JsLiteral.java | 8 -------- .../kotlin/js/coroutine/CoroutineBodyTransformer.kt | 6 +++--- .../kotlin/js/inline/clean/WhileConditionFolding.kt | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsBooleanLiteral.java b/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsBooleanLiteral.java index 46ddbc4878f..62d657552b8 100644 --- a/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsBooleanLiteral.java +++ b/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsBooleanLiteral.java @@ -21,10 +21,17 @@ import org.jetbrains.annotations.NotNull; public final class JsBooleanLiteral extends JsLiteral.JsValueLiteral { private final boolean value; - // Should be interned by JsProgram public JsBooleanLiteral(boolean value) { - this.value = value; - } + this.value = value; + } + + public static boolean isTrue(@NotNull JsExpression expression) { + return expression instanceof JsBooleanLiteral && ((JsBooleanLiteral) expression).getValue(); + } + + public static boolean isFalse(@NotNull JsExpression expression) { + return expression instanceof JsBooleanLiteral && !((JsBooleanLiteral) expression).getValue(); + } public boolean getValue() { return value; diff --git a/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsLiteral.java b/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsLiteral.java index b4ad6ee4666..8f181edd8f8 100644 --- a/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsLiteral.java +++ b/js/js.ast/src/org/jetbrains/kotlin/js/backend/ast/JsLiteral.java @@ -25,12 +25,4 @@ public abstract class JsLiteral extends JsExpression { return this; } } - - public static boolean isTrueBoolean(@NotNull JsExpression expression) { - return expression instanceof JsBooleanLiteral && ((JsBooleanLiteral) expression).getValue(); - } - - public static boolean isFalseBoolean(@NotNull JsExpression expression) { - return expression instanceof JsBooleanLiteral && !((JsBooleanLiteral) expression).getValue(); - } } diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt index 0dd99c86435..848fbf1f2eb 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt @@ -121,7 +121,7 @@ class CoroutineBodyTransformer(private val context: CoroutineTransformationConte currentStatements += stateAndJump(bodyEntryBlock, x) currentBlock = bodyEntryBlock - if (!JsLiteral.isTrueBoolean(x.condition)) { + if (!JsBooleanLiteral.isTrue(x.condition)) { currentStatements += JsIf(JsAstUtils.notOptimized(x.condition), JsBlock(stateAndJump(successor, x))).apply { source = x.source } } @@ -143,7 +143,7 @@ class CoroutineBodyTransformer(private val context: CoroutineTransformationConte x.body.accept(this) } - if (!JsLiteral.isTrueBoolean(x.condition)) { + if (!JsBooleanLiteral.isTrue(x.condition)) { val jsIf = JsIf(JsAstUtils.notOptimized(x.condition), JsBlock(stateAndJump(successor, x))).apply { source = x.source } currentStatements.add(jsIf) } @@ -168,7 +168,7 @@ class CoroutineBodyTransformer(private val context: CoroutineTransformationConte currentStatements += stateAndJump(bodyEntryBlock, x) currentBlock = bodyEntryBlock - if (x.condition != null && !JsLiteral.isTrueBoolean(x.condition)) { + if (x.condition != null && !JsBooleanLiteral.isTrue(x.condition)) { currentStatements += JsIf(JsAstUtils.notOptimized(x.condition), JsBlock(stateAndJump(successor, x))).apply { source = x.source } } diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/WhileConditionFolding.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/WhileConditionFolding.kt index f8d0a6d4731..8ef355519e2 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/WhileConditionFolding.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/WhileConditionFolding.kt @@ -67,7 +67,7 @@ class WhileConditionFolding(val body: JsBlock) { statement.body = remove(statement.body) val existingCondition = statement.condition statement.condition = when { - JsLiteral.isTrueBoolean(existingCondition) -> condition + JsBooleanLiteral.isTrue(existingCondition) -> condition else -> combine(existingCondition, condition) } changed = true @@ -142,7 +142,7 @@ class WhileConditionFolding(val body: JsBlock) { // Just a little optimization. When inner statement is a single `break`, `nextCondition` would be false. // However, `A || false` can be rewritten as simply `A` nextCondition == null -> null - JsLiteral.isFalseBoolean(nextCondition) -> JsAstUtils.notOptimized(statement.ifExpression) + JsBooleanLiteral.isFalse(nextCondition) -> JsAstUtils.notOptimized(statement.ifExpression) else -> JsAstUtils.or(JsAstUtils.notOptimized(statement.ifExpression), nextCondition) } result