JS: make JsEmpty singleton

This commit is contained in:
Alexey Tsvetkov
2015-03-26 21:58:34 +03:00
parent dd13e91883
commit efba2a2372
11 changed files with 11 additions and 35 deletions
@@ -4,7 +4,7 @@
package com.google.dart.compiler.backend.js.ast package com.google.dart.compiler.backend.js.ast
public class JsEmpty : SourceInfoAwareJsNode(), JsStatement { public object JsEmpty : SourceInfoAwareJsNode(), JsStatement {
override fun accept(v: JsVisitor) { override fun accept(v: JsVisitor) {
v.visitEmpty(this) v.visitEmpty(this)
@@ -26,7 +26,7 @@ public class JsEmptyExpression extends JsExpressionImpl {
@Override @Override
@NotNull @NotNull
public JsStatement makeStmt() { public JsStatement makeStmt() {
return new JsEmpty(); return JsEmpty.INSTANCE$;
} }
@Override @Override
@@ -18,8 +18,6 @@ import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiter
* A JavaScript program. * A JavaScript program.
*/ */
public final class JsProgram extends SourceInfoAwareJsNode { public final class JsProgram extends SourceInfoAwareJsNode {
@NotNull
private final JsEmpty emptyStatement;
@NotNull final JsExpression emptyExpression; @NotNull final JsExpression emptyExpression;
private JsProgramFragment[] fragments; private JsProgramFragment[] fragments;
@@ -36,15 +34,9 @@ public final class JsProgram extends SourceInfoAwareJsNode {
topScope = new JsObjectScope(rootScope, "Global", unitId); topScope = new JsObjectScope(rootScope, "Global", unitId);
setFragmentCount(1); setFragmentCount(1);
emptyStatement = new JsEmpty();
emptyExpression = new JsEmptyExpression(); emptyExpression = new JsEmptyExpression();
} }
@NotNull
public JsEmpty getEmptyStatement() {
return emptyStatement;
}
@NotNull @NotNull
public JsExpression getEmptyExpression() { public JsExpression getEmptyExpression() {
return emptyExpression; return emptyExpression;
@@ -283,12 +283,6 @@ public class JsInliner extends JsVisitorWithContextImpl {
return getLastStatementLevelContext(); return getLastStatementLevelContext();
} }
@NotNull
@Override
protected JsStatement getEmptyStatement() {
return getFunctionContext().getEmpty();
}
@Override @Override
public void shiftCurrentStatementForward() { public void shiftCurrentStatementForward() {
super.shiftCurrentStatementForward(); super.shiftCurrentStatementForward();
@@ -52,10 +52,6 @@ abstract class FunctionContext(
return getFunctionDefinitionImpl(call) != null return getFunctionDefinitionImpl(call) != null
} }
public fun getEmpty(): JsEmpty {
return getScope().getProgram()?.getEmptyStatement()!!
}
public fun getScope(): JsScope { public fun getScope(): JsScope {
return function.getScope() return function.getScope()
} }
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.js.inline.context package org.jetbrains.kotlin.js.inline.context
import com.google.dart.compiler.backend.js.ast.JsContext import com.google.dart.compiler.backend.js.ast.JsContext
import com.google.dart.compiler.backend.js.ast.JsEmpty
import com.google.dart.compiler.backend.js.ast.JsStatement import com.google.dart.compiler.backend.js.ast.JsStatement
abstract class StatementContext { abstract class StatementContext {
@@ -28,20 +29,18 @@ abstract class StatementContext {
public fun removeCurrentStatement() { public fun removeCurrentStatement() {
val statementContext = getCurrentStatementContext() val statementContext = getCurrentStatementContext()
statementContext.replaceMe(getEmptyStatement()) statementContext.replaceMe(JsEmpty)
} }
open public fun shiftCurrentStatementForward() { open public fun shiftCurrentStatementForward() {
val statementContext = getCurrentStatementContext() val statementContext = getCurrentStatementContext()
val currentStatement = getCurrentStatement() val currentStatement = getCurrentStatement()
statementContext.insertAfter(currentStatement) statementContext.insertAfter(currentStatement)
statementContext.replaceMe(getEmptyStatement()) statementContext.replaceMe(JsEmpty)
} }
public fun getCurrentStatement(): JsStatement { public fun getCurrentStatement(): JsStatement {
val statementContext = getCurrentStatementContext() val statementContext = getCurrentStatementContext()
return statementContext.getCurrentNode() as JsStatement return statementContext.getCurrentNode() as JsStatement
} }
protected abstract fun getEmptyStatement(): JsStatement
} }
@@ -517,7 +517,7 @@ public class JsAstMapper {
toForIn.setBody(bodyStmt); toForIn.setBody(bodyStmt);
} }
else { else {
toForIn.setBody(program.getEmptyStatement()); toForIn.setBody(JsEmpty.INSTANCE$);
} }
return toForIn; return toForIn;
@@ -545,7 +545,7 @@ public class JsAstMapper {
toFor.setBody(bodyStmt); toFor.setBody(bodyStmt);
} }
else { else {
toFor.setBody(program.getEmptyStatement()); toFor.setBody(JsEmpty.INSTANCE$);
} }
return toFor; return toFor;
} }
@@ -892,7 +892,7 @@ public class JsAstMapper {
else { else {
// When map() returns null, we return an empty statement. // When map() returns null, we return an empty statement.
// //
return program.getEmptyStatement(); return JsEmpty.INSTANCE$;
} }
} }
@@ -302,11 +302,6 @@ public class TranslationContext {
return dynamicContext.jsBlock(); return dynamicContext.jsBlock();
} }
@NotNull
public JsEmpty getEmptyStatement() {
return program().getEmptyStatement();
}
@NotNull @NotNull
public JsExpression getEmptyExpression() { public JsExpression getEmptyExpression() {
return program().getEmptyExpression(); return program().getEmptyExpression();
@@ -154,7 +154,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
if (expressionInside != null) { if (expressionInside != null) {
return Translation.translateExpression(expressionInside, context); return Translation.translateExpression(expressionInside, context);
} }
return context.getEmptyStatement(); return JsEmpty.INSTANCE$;
} }
@Override @Override
@@ -45,7 +45,7 @@ public fun createWhile(doWhile: Boolean, expression: JetWhileExpressionBase, con
if (body != null) if (body != null)
Translation.translateAsStatementAndMergeInBlockIfNeeded(body, context) Translation.translateAsStatementAndMergeInBlockIfNeeded(body, context)
else else
context.getEmptyStatement() JsEmpty
if (!conditionBlock.isEmpty()) { if (!conditionBlock.isEmpty()) {
val breakIfConditionIsFalseStatement = JsIf(not(jsCondition), JsBreak()) val breakIfConditionIsFalseStatement = JsIf(not(jsCondition), JsBreak())
@@ -63,7 +63,7 @@ public final class WhenTranslator extends AbstractTranslator {
private JsStatement translate() { private JsStatement translate() {
if (expressionToMatch != null && JsAstUtils.isEmptyExpression(expressionToMatch)) { if (expressionToMatch != null && JsAstUtils.isEmptyExpression(expressionToMatch)) {
return context().getEmptyStatement(); return JsEmpty.INSTANCE$;
} }
JsIf currentIf = null; JsIf currentIf = null;