| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.collect.Lists; |
| 19 |
import com.google.common.collect.Sets; |
| 20 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 21 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 23 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 24 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
|
| 27 |
import java.util.Collection; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ChainCalls implements CompilerPass { |
| 36 |
private final AbstractCompiler compiler; |
| 37 |
private final Set<Node> badFunctionNodes = Sets.newHashSet(); |
| 38 |
private final Set<Node> goodFunctionNodes = Sets.newHashSet(); |
| 39 |
private final List<CallSite> callSites = Lists.newArrayList(); |
| 40 |
private SimpleDefinitionFinder defFinder; |
| 41 |
private GatherFunctions gatherFunctions = new GatherFunctions(); |
| 42 |
|
| 43 |
ChainCalls(AbstractCompiler compiler) { |
| 44 |
this.compiler = compiler; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void process(Node externs, Node root) { |
| 49 |
defFinder = new SimpleDefinitionFinder(compiler); |
| 50 |
defFinder.process(externs, root); |
| 51 |
|
| 52 |
NodeTraversal.traverse(compiler, root, new GatherCallSites()); |
| 53 |
|
| 54 |
for (CallSite callSite : callSites) { |
| 55 |
callSite.parent.removeChild(callSite.n); |
| 56 |
callSite.n.removeChild(callSite.callNode); |
| 57 |
callSite.nextGetPropNode.replaceChild(callSite.nextGetPropFirstChildNode, |
| 58 |
callSite.callNode); |
| 59 |
compiler.reportCodeChange(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private class GatherFunctions implements ScopedCallback { |
| 67 |
@Override |
| 68 |
public void enterScope(NodeTraversal t) { |
| 69 |
ControlFlowGraph<Node> cfg = t.getControlFlowGraph(); |
| 70 |
|
| 71 |
for (DiGraphEdge<Node, Branch> s : cfg.getImplicitReturn().getInEdges()) { |
| 72 |
Node exitNode = s.getSource().getValue(); |
| 73 |
if (!exitNode.isReturn() || |
| 74 |
exitNode.getFirstChild() == null || |
| 75 |
!exitNode.getFirstChild().isThis()) { |
| 76 |
badFunctionNodes.add(t.getScopeRoot()); |
| 77 |
return; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
goodFunctionNodes.add(t.getScopeRoot()); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void exitScope(NodeTraversal t) { |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, |
| 90 |
Node parent) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
private class GatherCallSites extends AbstractPostOrderCallback { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 106 |
if (!n.isExprResult()) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
Node callNode = n.getFirstChild(); |
| 111 |
if (!callNode.isCall()) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
Node getPropNode = callNode.getFirstChild(); |
| 116 |
if (!getPropNode.isGetProp()) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
Node getPropFirstChildNode = getPropNode.getFirstChild(); |
| 121 |
|
| 122 |
Collection<Definition> definitions = |
| 123 |
defFinder.getDefinitionsReferencedAt(getPropNode); |
| 124 |
if (definitions == null) { |
| 125 |
return; |
| 126 |
} |
| 127 |
for (Definition definition : definitions) { |
| 128 |
Node rValue = definition.getRValue(); |
| 129 |
if (rValue == null) { |
| 130 |
return; |
| 131 |
} |
| 132 |
if (badFunctionNodes.contains(rValue)) { |
| 133 |
return; |
| 134 |
} |
| 135 |
if (!goodFunctionNodes.contains(rValue)) { |
| 136 |
NodeTraversal.traverse(compiler, rValue, gatherFunctions); |
| 137 |
if (badFunctionNodes.contains(rValue)) { |
| 138 |
return; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
Node nextNode = n.getNext(); |
| 144 |
if (nextNode == null || !nextNode.isExprResult()) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
Node nextCallNode = nextNode.getFirstChild(); |
| 149 |
if (!nextCallNode.isCall()) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
Node nextGetPropNode = nextCallNode.getFirstChild(); |
| 154 |
if (!nextGetPropNode.isGetProp()) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
Node nextGetPropFirstChildNode = nextGetPropNode.getFirstChild(); |
| 159 |
if (!compiler.areNodesEqualForInlining( |
| 160 |
nextGetPropFirstChildNode, getPropFirstChildNode)) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if (NodeUtil.mayEffectMutableState(getPropFirstChildNode)) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
callSites.add(new CallSite(parent, n, callNode, nextGetPropNode, |
| 171 |
nextGetPropFirstChildNode)); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
private static class CallSite { |
| 177 |
final Node parent; |
| 178 |
final Node n; |
| 179 |
final Node callNode; |
| 180 |
final Node nextGetPropNode; |
| 181 |
final Node nextGetPropFirstChildNode; |
| 182 |
|
| 183 |
CallSite(Node parent, Node n, Node callNode, Node nextGetPropNode, |
| 184 |
Node nextGetPropFirstChildNode) { |
| 185 |
this.parent = parent; |
| 186 |
this.n = n; |
| 187 |
this.callNode = callNode; |
| 188 |
this.nextGetPropNode = nextGetPropNode; |
| 189 |
this.nextGetPropFirstChildNode = nextGetPropFirstChildNode; |
| 190 |
} |
| 191 |
} |
| 192 |
} |