JS inline refactor: moved contexts to 'context' package

This commit is contained in:
Alexey Tsvetkov
2014-10-02 15:26:40 +04:00
committed by Zalim Bashorov
parent cd9349deb7
commit 1ad090e3a7
8 changed files with 121 additions and 125 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.inline;
import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.*;
import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.inline.context.*;
import static org.jetbrains.k2js.inline.util.UtilPackage.collectInstances; import static org.jetbrains.k2js.inline.util.UtilPackage.collectInstances;
import static org.jetbrains.k2js.inline.util.UtilPackage.replaceReturns; import static org.jetbrains.k2js.inline.util.UtilPackage.replaceReturns;
import static org.jetbrains.k2js.inline.util.UtilPackage.replaceThisReference; import static org.jetbrains.k2js.inline.util.UtilPackage.replaceThisReference;
@@ -32,10 +33,10 @@ class FunctionInlineMutator {
private final JsInvocation call; private final JsInvocation call;
private final InliningContext inliningContext; private final InliningContext inliningContext;
private final FunctionContext functionContext;
private final JsFunction invokedFunction; private final JsFunction invokedFunction;
private final boolean isResultNeeded; private final boolean isResultNeeded;
private final RenamingContext<JsBlock> renamingContext; private final NamingContext namingContext;
private final InsertionPoint<JsStatement> insertionPoint;
private JsBlock body; private JsBlock body;
private JsExpression resultExpr = null; private JsExpression resultExpr = null;
private JsLabel breakLabel = null; private JsLabel breakLabel = null;
@@ -65,12 +66,11 @@ class FunctionInlineMutator {
this.inliningContext = inliningContext; this.inliningContext = inliningContext;
this.call = call; this.call = call;
FunctionContext functionContext = inliningContext.getFunctionContext(); functionContext = inliningContext.getFunctionContext();
invokedFunction = functionContext.getFunctionDefinition(call); invokedFunction = functionContext.getFunctionDefinition(call);
body = invokedFunction.getBody().deepCopy(); body = invokedFunction.getBody().deepCopy();
isResultNeeded = inliningContext.isResultNeeded(call); isResultNeeded = isResultNeeded(call);
renamingContext = inliningContext.getRenamingContext(); namingContext = inliningContext.newNamingContext();
insertionPoint = inliningContext.getStatementContext().getInsertionPoint();
} }
private void process() { private void process() {
@@ -79,15 +79,14 @@ class FunctionInlineMutator {
replaceThis(); replaceThis();
removeRedundantDefaultInitializers(arguments, parameters, body); removeRedundantDefaultInitializers(arguments, parameters, body);
aliasArgumentsIfNeeded(renamingContext, arguments, parameters); aliasArgumentsIfNeeded(namingContext, arguments, parameters);
renameLocalNames(renamingContext, invokedFunction); renameLocalNames(namingContext, invokedFunction);
if (canBeExpression(body)) { if (canBeExpression(body)) {
applyRenaming();
doDirectInline(); doDirectInline();
} else { } else {
processReturns(); processReturns();
applyRenaming(); namingContext.applyRenameTo(body);
} }
} }
@@ -98,21 +97,14 @@ class FunctionInlineMutator {
if (thisReplacement == null) return; if (thisReplacement == null) return;
if (needToAlias(thisReplacement)) { if (needToAlias(thisReplacement)) {
JsName thisName = renamingContext.getFreshName(getThisAlias()); JsName thisName = namingContext.getFreshName(getThisAlias());
renamingContext.newVar(thisName, thisReplacement); namingContext.newVar(thisName, thisReplacement);
thisReplacement = thisName.makeRef(); thisReplacement = thisName.makeRef();
} }
replaceThisReference(body, thisReplacement); replaceThisReference(body, thisReplacement);
} }
private void applyRenaming() {
RenamingResult<JsBlock> renamingResult = renamingContext.applyRename(body);
body = renamingResult.getRenamed();
Collection<JsVars> declarations = renamingResult.getDeclarations();
insertionPoint.insertAllBefore(declarations);
}
private void processReturns() { private void processReturns() {
int returnCount = collectInstances(JsReturn.class, body).size(); int returnCount = collectInstances(JsReturn.class, body).size();
if (returnCount == 0) { if (returnCount == 0) {
@@ -133,8 +125,8 @@ class FunctionInlineMutator {
boolean hasReturnOnTopLevel = returnOnTop != null; boolean hasReturnOnTopLevel = returnOnTop != null;
if (isResultNeeded) { if (isResultNeeded) {
JsName resultName = renamingContext.getFreshName(getResultLabel()); JsName resultName = namingContext.getFreshName(getResultLabel());
renamingContext.newVar(resultName, null); namingContext.newVar(resultName, null);
resultExpr = resultName.makeRef(); resultExpr = resultName.makeRef();
} }
@@ -142,7 +134,7 @@ class FunctionInlineMutator {
JsNameRef breakLabelRef = null; JsNameRef breakLabelRef = null;
if (needBreakLabel) { if (needBreakLabel) {
JsName breakName = renamingContext.getFreshName(getBreakLabel()); JsName breakName = namingContext.getFreshName(getBreakLabel());
breakLabelRef = breakName.makeRef(); breakLabelRef = breakName.makeRef();
breakLabel = new JsLabel(breakName); breakLabel = new JsLabel(breakName);
} }
@@ -153,7 +145,19 @@ class FunctionInlineMutator {
@NotNull @NotNull
private List<JsExpression> getArguments() { private List<JsExpression> getArguments() {
return inliningContext.getArguments(call); List<JsExpression> arguments = call.getArguments();
if (isCallInvocation(call)) {
return arguments.subList(1, arguments.size());
}
return arguments;
}
private boolean isResultNeeded(JsInvocation call) {
StatementContext statementContext = inliningContext.getStatementContext();
JsStatement currentStatement = statementContext.getCurrentStatement();
return !(currentStatement instanceof JsExpressionStatement)
|| call != ((JsExpressionStatement) currentStatement).getExpression();
} }
@NotNull @NotNull
@@ -1,53 +0,0 @@
/*
* Copyright 2010-2014 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.k2js.inline;
import com.google.dart.compiler.backend.js.ast.JsContext;
import com.google.dart.compiler.backend.js.ast.JsNode;
import com.intellij.util.containers.ContainerUtil;
import java.util.Collection;
import java.util.List;
class InsertionPoint<T extends JsNode> {
private final JsContext context;
InsertionPoint(JsContext insertionContext) {
context = insertionContext;
}
public void insertBefore(T node) {
context.insertBefore(node);
}
public void insertAfter(T node) {
context.insertAfter(node);
}
public void insertAllBefore(Collection<? extends T> nodes) {
for (T node : nodes) {
insertBefore(node);
}
}
public void insertAllAfter(List<? extends T> nodes) {
for (T node : ContainerUtil.reverse(nodes)) {
insertAfter(node);
}
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.inline;
import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.inline.context.*;
import org.jetbrains.k2js.inline.exception.InlineRecursionException; import org.jetbrains.k2js.inline.exception.InlineRecursionException;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
@@ -14,12 +14,24 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.k2js.inline package org.jetbrains.k2js.inline.context
import com.google.dart.compiler.backend.js.ast.* import com.google.dart.compiler.backend.js.ast.JsEmpty
import com.google.dart.compiler.backend.js.ast.JsExpression
import com.google.dart.compiler.backend.js.ast.JsFunction
import com.google.dart.compiler.backend.js.ast.JsInvocation
import com.google.dart.compiler.backend.js.ast.JsLiteral
import com.google.dart.compiler.backend.js.ast.JsName
import com.google.dart.compiler.backend.js.ast.JsNameRef
import com.google.dart.compiler.backend.js.ast.JsScope
import com.google.dart.compiler.backend.js.ast.JsStatement
import org.jetbrains.k2js.inline.util.aliasArgumentsIfNeeded
import org.jetbrains.k2js.inline.util.getInnerFunction
import org.jetbrains.k2js.inline.util.getSimpleName
import org.jetbrains.k2js.inline.util.isCallInvocation
import org.jetbrains.k2js.inline.util.isFunctionCreator
import com.intellij.util.containers.ContainerUtil import com.intellij.util.containers.ContainerUtil
import java.util.IdentityHashMap import java.util.IdentityHashMap
abstract class FunctionContext( abstract class FunctionContext(
@@ -55,14 +67,14 @@ abstract class FunctionContext(
val calls = ContainerUtil.findAll<JsExpression, JsInvocation>(arguments, javaClass<JsInvocation>()) val calls = ContainerUtil.findAll<JsExpression, JsInvocation>(arguments, javaClass<JsInvocation>())
for (call in calls) { for (call in calls) {
val callName = InvocationUtil.getName(call) val callName = getSimpleName(call)
if (callName == null) continue if (callName == null) continue
val staticRef = callName.getStaticRef() val staticRef = callName.getStaticRef()
if (staticRef !is JsFunction) continue if (staticRef !is JsFunction) continue
val functionCalled = staticRef as JsFunction val functionCalled = staticRef as JsFunction
if (InvocationUtil.getInnerFunction(functionCalled) != null) { if (isFunctionCreator(functionCalled)) {
declareFunctionConstructorCall(call) declareFunctionConstructorCall(call)
} }
} }
@@ -107,7 +119,7 @@ abstract class FunctionContext(
var callQualifier = call.getQualifier() var callQualifier = call.getQualifier()
/** remove ending `.call()` */ /** remove ending `.call()` */
if (InvocationUtil.isCallInvocation(call)) { if (isCallInvocation(call)) {
callQualifier = (callQualifier as JsNameRef).getQualifier() callQualifier = (callQualifier as JsNameRef).getQualifier()
} }
@@ -158,9 +170,9 @@ abstract class FunctionContext(
if (constructed is JsFunction) return constructed if (constructed is JsFunction) return constructed
val name = InvocationUtil.getName(call)!! val name = getSimpleName(call)!!
val closureCreator = lookUpStaticFunction(name)!! val closureCreator = lookUpStaticFunction(name)!!
val innerFunction = InvocationUtil.getInnerFunction(closureCreator)!! val innerFunction = closureCreator.getInnerFunction()!!
val withCapturedArgs = applyCapturedArgs(call, innerFunction, closureCreator) val withCapturedArgs = applyCapturedArgs(call, innerFunction, closureCreator)
functionsWithClosure.put(call, withCapturedArgs) functionsWithClosure.put(call, withCapturedArgs)
@@ -171,15 +183,12 @@ abstract class FunctionContext(
private fun applyCapturedArgs(call: JsInvocation, inner: JsFunction, outer: JsFunction): JsFunction { private fun applyCapturedArgs(call: JsInvocation, inner: JsFunction, outer: JsFunction): JsFunction {
val innerClone = inner.deepCopy() val innerClone = inner.deepCopy()
val renamingContext = inliningContext.getRenamingContext<JsFunction>() val namingContext = inliningContext.newNamingContext()
val arguments = call.getArguments()!! val arguments = call.getArguments()
val parameters = outer.getParameters() val parameters = outer.getParameters()
aliasArgumentsIfNeeded(renamingContext, arguments, parameters) aliasArgumentsIfNeeded(namingContext, arguments, parameters)
val renamingResult = renamingContext.applyRename(innerClone) namingContext.applyRenameTo(innerClone)
val declarations = renamingResult.declarations return innerClone
val insertionPoint = inliningContext.getStatementContext().getInsertionPoint<JsStatement>()
insertionPoint.insertAllBefore(declarations)
return renamingResult.renamed
} }
} }
@@ -14,28 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.k2js.inline; package org.jetbrains.k2js.inline.context
import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.JsNode
import org.jetbrains.annotations.NotNull;
import java.util.List; trait InliningContext {
public val statementContext: StatementContext
public val functionContext: FunctionContext
interface InliningContext { public fun newNamingContext(): NamingContext
@NotNull
<T extends JsNode> RenamingContext<T> getRenamingContext();
@NotNull
StatementContext getStatementContext();
@NotNull
FunctionContext getFunctionContext();
@NotNull
JsExpression getThisReplacement(JsInvocation call);
@NotNull
List<JsExpression> getArguments(JsInvocation call);
boolean isResultNeeded(JsInvocation call);
} }
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2014 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.k2js.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)
}
}
}
@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.k2js.inline package org.jetbrains.k2js.inline.context
import com.google.dart.compiler.backend.js.ast.* import com.google.dart.compiler.backend.js.ast.*
import com.google.dart.compiler.backend.js.ast.JsVars.JsVar import com.google.dart.compiler.backend.js.ast.JsVars.JsVar
@@ -23,18 +23,24 @@ import java.util.ArrayList
import java.util.IdentityHashMap import java.util.IdentityHashMap
import org.jetbrains.k2js.translate.utils.JsAstUtils import org.jetbrains.k2js.translate.utils.JsAstUtils
import org.jetbrains.k2js.inline.util.replaceNames
class NamingContext(
data class RenamingResult<T : JsNode>(val renamed: T, val declarations: Collection<JsVars>) private val scope: JsScope,
private val insertionPoint: InsertionPoint<JsStatement>
class RenamingContext<T : JsNode>(scope: JsScope) { ) {
private val scope = scope
private val renamings = IdentityHashMap<JsName, JsExpression>() private val renamings = IdentityHashMap<JsName, JsExpression>()
private val declarations = ArrayList<JsVars>() private val declarations = ArrayList<JsVars>()
private var renamingApplied = false
public fun applyRename(target : T): RenamingResult<T> { public fun applyRenameTo(target: JsNode): JsNode {
val renamed = RenamingVisitor.rename(target, renamings) if (renamingApplied) throw RuntimeException("RenamingContext has been applied already")
return RenamingResult(renamed, declarations)
val result = replaceNames(target, renamings)
insertionPoint.insertAllBefore(declarations)
renamingApplied = true
return result
} }
public fun replaceName(name: JsName, replacement: JsExpression) { public fun replaceName(name: JsName, replacement: JsExpression) {
@@ -14,11 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.k2js.inline package org.jetbrains.k2js.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.JsStatement import com.google.dart.compiler.backend.js.ast.JsStatement
import com.google.dart.compiler.backend.js.ast.JsNode
abstract class StatementContext { abstract class StatementContext {
public abstract fun getCurrentStatementContext(): JsContext public abstract fun getCurrentStatementContext(): JsContext
@@ -45,4 +44,4 @@ abstract class StatementContext {
} }
protected abstract fun getEmptyStatement(): JsStatement protected abstract fun getEmptyStatement(): JsStatement
} }