From add0aca746b5dbefbe7874a6fd26d918737a51de Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 27 Mar 2015 21:10:42 +0300 Subject: [PATCH] JS: removed StatementContext, InsertionPoint --- .../js/inline/FunctionInlineMutator.java | 5 +- .../jetbrains/kotlin/js/inline/JsInliner.java | 23 ++-------- .../removeUnusedLocalFunctionDeclarations.kt | 4 +- .../js/inline/context/InliningContext.kt | 5 +- .../js/inline/context/InsertionPoint.kt | 44 ------------------ .../kotlin/js/inline/context/NamingContext.kt | 4 +- .../js/inline/context/StatementContext.kt | 46 ------------------- 7 files changed, 13 insertions(+), 118 deletions(-) delete mode 100644 js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InsertionPoint.kt delete mode 100644 js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/StatementContext.kt diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionInlineMutator.java b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionInlineMutator.java index 692a9069ef9..842b862488f 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionInlineMutator.java +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionInlineMutator.java @@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.js.inline.context.FunctionContext; import org.jetbrains.kotlin.js.inline.context.InliningContext; import org.jetbrains.kotlin.js.inline.context.NamingContext; -import org.jetbrains.kotlin.js.inline.context.StatementContext; import java.util.List; @@ -168,8 +167,8 @@ class FunctionInlineMutator { } private boolean isResultNeeded(JsInvocation call) { - StatementContext statementContext = inliningContext.getStatementContext(); - JsStatement currentStatement = statementContext.getCurrentStatement(); + JsContext statementContext = inliningContext.getStatementContext(); + JsStatement currentStatement = statementContext.getCurrentNode(); return !(currentStatement instanceof JsExpressionStatement) || call != ((JsExpressionStatement) currentStatement).getExpression(); } diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java index 12c8dcb4e9f..e50667599af 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java @@ -174,7 +174,7 @@ public class JsInliner extends JsVisitorWithContextImpl { JsStatement inlineableBody = inlineableResult.getInlineableBody(); JsExpression resultExpression = inlineableResult.getResultExpression(); - StatementContext statementContext = inliningContext.getStatementContext(); + JsContext statementContext = inliningContext.getStatementContext(); accept(inlineableBody); /** @@ -189,8 +189,6 @@ public class JsInliner extends JsVisitorWithContextImpl { /** @see #lastStatementWasShifted */ statementContext.shiftCurrentStatementForward(); - InsertionPoint insertionPoint = statementContext.getInsertionPoint(); - insertionPoint.insertAllAfter(flattenStatement(inlineableBody)); } /** @@ -269,26 +267,13 @@ public class JsInliner extends JsVisitorWithContextImpl { @Override public NamingContext newNamingContext() { JsScope scope = getFunctionContext().getScope(); - InsertionPoint insertionPoint = getStatementContext().getInsertionPoint(); - return new NamingContext(scope, insertionPoint); + return new NamingContext(scope, getStatementContext()); } @NotNull @Override - public StatementContext getStatementContext() { - return new StatementContext() { - @NotNull - @Override - public JsContext getCurrentStatementContext() { - return getLastStatementLevelContext(); - } - - @Override - public void shiftCurrentStatementForward() { - super.shiftCurrentStatementForward(); - lastStatementWasShifted = true; - } - }; + public JsContext getStatementContext() { + return getLastStatementLevelContext(); } @NotNull diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedLocalFunctionDeclarations.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedLocalFunctionDeclarations.kt index 349d4d7ef19..1569f3c33a4 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedLocalFunctionDeclarations.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedLocalFunctionDeclarations.kt @@ -52,9 +52,7 @@ private class UnusedInstanceCollector : JsVisitorWithContextImpl() { val name = x.getName()!! val statementContext = getLastStatementLevelContext() - val currentNode = statementContext.getCurrentNode() - assert(currentNode is JsStatement) { "expected context containing statement" } - val currentStatement = currentNode as JsStatement + val currentStatement = statementContext.getCurrentNode() tracker.addCandidateForRemoval(name, currentStatement) val references = collectReferencesInside(x) diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InliningContext.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InliningContext.kt index 3e753f8b572..a7eccc37387 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InliningContext.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InliningContext.kt @@ -16,8 +16,11 @@ package org.jetbrains.kotlin.js.inline.context +import com.google.dart.compiler.backend.js.ast.JsContext +import com.google.dart.compiler.backend.js.ast.JsStatement + trait InliningContext { - public val statementContext: StatementContext + public val statementContext: JsContext public val functionContext: FunctionContext public fun newNamingContext(): NamingContext diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InsertionPoint.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InsertionPoint.kt deleted file mode 100644 index 659772658cc..00000000000 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/InsertionPoint.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.js.inline.context - -import com.google.dart.compiler.backend.js.ast.JsContext -import com.google.dart.compiler.backend.js.ast.JsNode -import com.intellij.util.containers.ContainerUtil - -class InsertionPoint(private val context: JsContext) { - - public fun insertBefore(node: T) { - context.insertBefore(node) - } - - public fun insertAfter(node: T) { - context.insertAfter(node) - } - - public fun insertAllBefore(nodes: Collection) { - for (node in nodes) { - insertBefore(node) - } - } - - public fun insertAllAfter(nodes: List) { - for (node in ContainerUtil.reverse(nodes)) { - insertAfter(node) - } - } -} diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/NamingContext.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/NamingContext.kt index 781efd9e17f..4d1a2be88a0 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/NamingContext.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/NamingContext.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.js.inline.util.replaceNames class NamingContext( private val scope: JsScope, - private val insertionPoint: InsertionPoint + private val statementContext: JsContext ) { private val renamings = IdentityHashMap() private val declarations = ArrayList() @@ -36,7 +36,7 @@ class NamingContext( if (renamingApplied) throw RuntimeException("RenamingContext has been applied already") val result = replaceNames(target, renamings) - insertionPoint.insertAllBefore(declarations) + statementContext.insertAllBefore(declarations) renamingApplied = true return result diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/StatementContext.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/StatementContext.kt deleted file mode 100644 index 2a6680c5bc4..00000000000 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/context/StatementContext.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.js.inline.context - -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 - -abstract class StatementContext { - public abstract fun getCurrentStatementContext(): JsContext - - public fun getInsertionPoint(): InsertionPoint { - return InsertionPoint(getCurrentStatementContext()) - } - - public fun removeCurrentStatement() { - val statementContext = getCurrentStatementContext() - statementContext.replaceMe(JsEmpty) - } - - open public fun shiftCurrentStatementForward() { - val statementContext = getCurrentStatementContext() - val currentStatement = getCurrentStatement() - statementContext.insertAfter(currentStatement) - statementContext.replaceMe(JsEmpty) - } - - public fun getCurrentStatement(): JsStatement { - val statementContext = getCurrentStatementContext() - return statementContext.getCurrentNode() as JsStatement - } -}