Minor refactoring of JS AST nodes representing boolean literals

This commit is contained in:
Alexey Andreev
2017-05-17 12:21:47 +03:00
parent 5715803b96
commit 4346fb5c23
4 changed files with 15 additions and 16 deletions
@@ -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;
@@ -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();
}
}
@@ -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 }
}
@@ -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