JS inline refactor: moved JS AST collecting utils to 'collectors' package

This commit is contained in:
Alexey Tsvetkov
2014-10-02 15:10:46 +04:00
committed by Zalim Bashorov
parent 86c253ef6d
commit cd9349deb7
7 changed files with 93 additions and 87 deletions
@@ -1,47 +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.*;
import java.util.IdentityHashMap;
public class FunctionCollector extends RecursiveJsVisitor {
private final IdentityHashMap<JsName, JsFunction> functions = new IdentityHashMap<JsName, JsFunction>();
public static IdentityHashMap<JsName, JsFunction> collectFunctions(JsNode root) {
FunctionCollector collector = new FunctionCollector();
collector.accept(root);
return collector.functions;
}
@Override
public void visitPropertyInitializer(JsPropertyInitializer x) {
super.visitPropertyInitializer(x);
JsExpression label = x.getLabelExpr();
JsExpression value = x.getValueExpr();
if (label instanceof JsNameRef && value instanceof JsFunction) {
JsName name = ((JsNameRef) label).getName();
JsFunction function = (JsFunction) value;
functions.put(name, function);
}
}
}
@@ -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 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;
@@ -112,8 +113,8 @@ class FunctionInlineMutator {
insertionPoint.insertAllBefore(declarations);
}
private void replaceReturns() {
int returnCount = ReturnCounter.countReturns(body);
private void processReturns() {
int returnCount = collectInstances(JsReturn.class, body).size();
if (returnCount == 0) {
// TODO return Unit (KT-5647)
resultExpr = JsLiteral.UNDEFINED;
@@ -30,6 +30,7 @@ import static org.jetbrains.k2js.inline.clean.CleanPackage.removeUnusedLocalFunc
import static org.jetbrains.k2js.inline.clean.CleanPackage.removeUnusedLocalFunctions;
import static org.jetbrains.k2js.inline.FunctionInlineMutator.getInlineableCallReplacement;
import static org.jetbrains.k2js.inline.util.UtilPackage.IdentitySet;
import static org.jetbrains.k2js.inline.util.UtilPackage.collectNamedFunctions;
public class JsInliner extends JsVisitorWithContextImpl {
@@ -69,7 +70,7 @@ public class JsInliner extends JsVisitorWithContextImpl {
private boolean lastStatementWasShifted = false;
public static JsProgram process(JsProgram program) {
IdentityHashMap<JsName, JsFunction> functions = FunctionCollector.collectFunctions(program);
IdentityHashMap<JsName, JsFunction> functions = collectNamedFunctions(program);
JsInliner inliner = new JsInliner(functions);
inliner.accept(program);
removeUnusedLocalFunctions(program, functions);
@@ -17,14 +17,44 @@
package org.jetbrains.k2js.inline.util
import com.google.dart.compiler.backend.js.ast.*
import org.jetbrains.k2js.inline.util.IdentitySet
import java.util.ArrayList
import java.util.IdentityHashMap
import org.jetbrains.k2js.inline.util.collectors.ReferenceNameCollector
import org.jetbrains.k2js.inline.util.collectors.NameCollector
import org.jetbrains.k2js.inline.util.collectors.InstanceCollector
import org.jetbrains.k2js.inline.util.collectors.FunctionCollector
public fun collectFunctionReferencesInside(scope: JsNode): List<JsName> =
collectReferencesInside(scope) filter { it.getStaticRef() is JsFunction }
public fun collectReferencesInside(scope: JsNode): List<JsName> {
return with(ReferenceNameCollector()) {
accept(scope)
references
}
}
public fun collectLocalNames(function: JsFunction): List<JsName> {
val functionScope = function.getScope()
return with(NameCollector(functionScope)) {
accept(function.getBody())
names.values().toList()
}
}
public fun collectNamedFunctions(scope: JsNode): IdentityHashMap<JsName, JsFunction> {
return with(FunctionCollector()) {
accept(scope)
functions
}
}
public fun collectInstances<T : JsNode>(klass: Class<T>, scope: JsNode): List<T> {
return with(InstanceCollector(klass)) {
accept(scope)
collected
}
}
@@ -14,31 +14,20 @@
* limitations under the License.
*/
package org.jetbrains.k2js.inline;
package org.jetbrains.k2js.inline.util.collectors
import com.google.dart.compiler.backend.js.ast.JsNode;
import com.google.dart.compiler.backend.js.ast.JsReturn;
import com.google.dart.compiler.backend.js.ast.RecursiveJsVisitor;
import com.google.dart.compiler.backend.js.ast.JsNode
import com.google.dart.compiler.backend.js.ast.RecursiveJsVisitor
import java.util.ArrayList
public class ReturnCounter extends RecursiveJsVisitor {
class InstanceCollector<T : JsNode>(val klass: Class<T>) : RecursiveJsVisitor() {
public val collected: MutableList<T> = ArrayList()
private int count = 0;
override fun visitElement(node: JsNode?) {
if (klass.isInstance(node)) {
collected.add(klass.cast(node)!!)
}
public static int countReturns(JsNode root) {
ReturnCounter counter = new ReturnCounter();
root.accept(counter);
return counter.getCount();
super.visitElement(node)
}
ReturnCounter() {}
private int getCount() {
return count;
}
@Override
public void visitReturn(JsReturn x) {
count++;
}
}
}
@@ -14,22 +14,18 @@
* limitations under the License.
*/
package org.jetbrains.k2js.inline.util
package org.jetbrains.k2js.inline.util.collectors
import com.google.dart.compiler.backend.js.ast.*
import com.google.dart.compiler.backend.js.ast.JsScope
import com.google.dart.compiler.backend.js.ast.RecursiveJsVisitor
import com.google.dart.compiler.backend.js.ast.JsFunction
import com.google.dart.compiler.backend.js.ast.JsLabel
import com.google.dart.compiler.backend.js.ast.HasName
import com.google.dart.compiler.backend.js.ast.JsName
import com.google.dart.compiler.backend.js.ast.JsVars
import java.util.ArrayList
import java.util.HashMap
public fun collectLocalNames(function: JsFunction): List<JsName> {
val functionScope = function.getScope()
return with (NameCollector(functionScope)) {
accept(function.getBody())
names.values().toList()
}
}
class NameCollector(private val scope: JsScope) : RecursiveJsVisitor() {
public val names: MutableMap<String, JsName> = HashMap()
@@ -57,5 +53,4 @@ class NameCollector(private val scope: JsScope) : RecursiveJsVisitor() {
names.put(ident, name)
}
}
}
@@ -0,0 +1,37 @@
/*
* 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.util.collectors
import com.google.dart.compiler.backend.js.ast.JsVisitorWithContextImpl
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.JsContext
import org.jetbrains.k2js
class ReferenceNameCollector : JsVisitorWithContextImpl() {
private val referenceSet = k2js.inline.util.IdentitySet<JsName>()
public val references: List<JsName>
get() = referenceSet.toList()
override fun endVisit(x: JsNameRef?, ctx: JsContext?) {
val name = x?.getName()
if (name != null) {
referenceSet.add(name)
}
}
}