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