JS: removed StatementContext, InsertionPoint

This commit is contained in:
Alexey Tsvetkov
2015-03-27 21:10:42 +03:00
parent e94c79f838
commit add0aca746
7 changed files with 13 additions and 118 deletions
@@ -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<JsStatement> statementContext = inliningContext.getStatementContext();
JsStatement currentStatement = statementContext.getCurrentNode();
return !(currentStatement instanceof JsExpressionStatement)
|| call != ((JsExpressionStatement) currentStatement).getExpression();
}
@@ -174,7 +174,7 @@ public class JsInliner extends JsVisitorWithContextImpl {
JsStatement inlineableBody = inlineableResult.getInlineableBody();
JsExpression resultExpression = inlineableResult.getResultExpression();
StatementContext statementContext = inliningContext.getStatementContext();
JsContext<JsStatement> statementContext = inliningContext.getStatementContext();
accept(inlineableBody);
/**
@@ -189,8 +189,6 @@ public class JsInliner extends JsVisitorWithContextImpl {
/** @see #lastStatementWasShifted */
statementContext.shiftCurrentStatementForward();
InsertionPoint<JsStatement> 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<JsStatement> 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<JsStatement> getStatementContext() {
return getLastStatementLevelContext();
}
@NotNull
@@ -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)
@@ -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<JsStatement>
public val functionContext: FunctionContext
public fun newNamingContext(): NamingContext
@@ -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<T : JsNode>(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<T>) {
for (node in nodes) {
insertBefore(node)
}
}
public fun insertAllAfter(nodes: List<T>) {
for (node in ContainerUtil.reverse(nodes)) {
insertAfter(node)
}
}
}
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.js.inline.util.replaceNames
class NamingContext(
private val scope: JsScope,
private val insertionPoint: InsertionPoint<JsStatement>
private val statementContext: JsContext<JsStatement>
) {
private val renamings = IdentityHashMap<JsName, JsExpression>()
private val declarations = ArrayList<JsVars>()
@@ -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
@@ -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<T : JsStatement>(): InsertionPoint<T> {
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
}
}