Debian Codegraph @

src/com/google/javascript/jscomp/MaybeReachingVariableUse.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.HashMultimap;
21 import com.google.common.collect.Multimap;
22 import com.google.common.collect.Sets;
23 import com.google.javascript.jscomp.ControlFlowGraph.Branch;
24 import com.google.javascript.jscomp.Scope.Var;
25 import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
26 import com.google.javascript.jscomp.graph.GraphNode;
27 import com.google.javascript.jscomp.graph.LatticeElement;
28 import com.google.javascript.rhino.Node;
29 import com.google.javascript.rhino.Token;
30
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Set;
34
35 /**
36 * Computes "may be" reaching use for all definitions of each variables.
37 *
38 * A use of {@code A} in {@code alert(A)} is a "may be" reaching use of
39 * the definition of {@code A} at {@code A = foo()} if at least one path from
40 * the use node reaches that definition and it is the last definition before
41 * the use on that path.
42 *
43 */
44 class MaybeReachingVariableUse extends
45 DataFlowAnalysis<Node, MaybeReachingVariableUse.ReachingUses> {
46
47 // The scope of the function that we are analyzing.
48 private final Scope jsScope;
49 private final Set<Var> escaped;
50
51 MaybeReachingVariableUse(
52 ControlFlowGraph<Node> cfg, Scope jsScope, AbstractCompiler compiler) {
53 super(cfg, new ReachingUsesJoinOp());
54 this.jsScope = jsScope;
55 this.escaped = Sets.newHashSet();
56
57 // TODO(user): Maybe compute it somewhere else and re-use the escape
58 // local set here.
59 computeEscaped(jsScope, escaped, compiler);
60 }
61
62 /**
63 * May use definition lattice representation. It captures a product
64 * lattice for each local (non-escaped) variable. The sub-lattice is
65 * a n + 2 power set element lattice with all the Nodes in the program,
66 * TOP and BOTTOM. This is better explained with an example:
67 *
68 * Consider: A sub-lattice element representing the variable A represented
69 * by { N_4, N_5} where N_x is a Node in the program. This implies at
70 * that particular point in the program the content of A is "upward exposed"
71 * at point N_4 and N_5.
72 *
73 * Example:
74 *
75 * A = 1;
76 * ...
77 * N_3:
78 * N_4: print(A);
79 * N_5: y = A;
80 * N_6: A = 1;
81 * N_7: print(A);
82 *
83 * At N_3, reads of A in {N_4, N_5} are said to be upward exposed.
84 */
85 static final class ReachingUses implements LatticeElement {
86 final Multimap<Var, Node> mayUseMap;
87
88 public ReachingUses() {
89 mayUseMap = HashMultimap.create();
90 }
91
92 /**
93 * Copy constructor.
94 *
95 * @param other The constructed object is a replicated copy of this element.
96 */
97 public ReachingUses(ReachingUses other) {
98 mayUseMap = HashMultimap.create(other.mayUseMap);
99 }
100
101 @Override
102 public boolean equals(Object other) {
103 return (other instanceof ReachingUses) &&
104 ((ReachingUses) other).mayUseMap.equals(this.mayUseMap);
105 }
106
107 @Override
108 public int hashCode() {
109 return mayUseMap.hashCode();
110 }
111 }
112
113 /**
114 * The join is a simple union because of the "may be" nature of the analysis.
115 *
116 * Consider: A = 1; if (x) { A = 2 }; alert(A);
117 *
118 * The read of A "may be" exposed to A = 1 in the beginning.
119 */
120 private static class ReachingUsesJoinOp implements JoinOp<ReachingUses> {
121 @Override
122 public ReachingUses apply(List<ReachingUses> from) {
123 ReachingUses result = new ReachingUses();
124 for (ReachingUses uses : from) {
125 result.mayUseMap.putAll(uses.mayUseMap);
126 }
127 return result;
128 }
129 }
130
131 @Override
132 boolean isForward() {
133 return false;
134 }
135
136 @Override
137 ReachingUses createEntryLattice() {
138 return new ReachingUses();
139 }
140
141 @Override
142 ReachingUses createInitialEstimateLattice() {
143 return new ReachingUses();
144 }
145
146 @Override
147 ReachingUses flowThrough(Node n, ReachingUses input) {
148 ReachingUses output = new ReachingUses(input);
149
150 // If there's an ON_EX edge, this cfgNode may or may not get executed.
151 // We can express this concisely by just pretending this happens in
152 // a conditional.
153 boolean conditional = hasExceptionHandler(n);
154 computeMayUse(n, n, output, conditional);
155
156 return output;
157 }
158
159 private boolean hasExceptionHandler(Node cfgNode) {
160 List<DiGraphEdge<Node, Branch>> branchEdges = getCfg().getOutEdges(cfgNode);
161 for (DiGraphEdge<Node, Branch> edge : branchEdges) {
162 if (edge.getValue() == Branch.ON_EX) {
163 return true;
164 }
165 }
166 return false;
167 }
168
169 private void computeMayUse(
170 Node n, Node cfgNode, ReachingUses output, boolean conditional) {
171 switch (n.getType()) {
172
173 case Token.BLOCK:
174 case Token.FUNCTION:
175 return;
176
177 case Token.NAME:
178 addToUseIfLocal(n.getString(), cfgNode, output);
179 return;
180
181 case Token.WHILE:
182 case Token.DO:
183 case Token.IF:
184 computeMayUse(
185 NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
186 return;
187
188 case Token.FOR:
189 if (!NodeUtil.isForIn(n)) {
190 computeMayUse(
191 NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
192 } else {
193 // for(x in y) {...}
194 Node lhs = n.getFirstChild();
195 Node rhs = lhs.getNext();
196 if (lhs.isVar()) {
197 lhs = lhs.getLastChild(); // for(var x in y) {...}
198 }
199 if (lhs.isName() && !conditional) {
200 removeFromUseIfLocal(lhs.getString(), output);
201 }
202 computeMayUse(rhs, cfgNode, output, conditional);
203 }
204 return;
205
206 case Token.AND:
207 case Token.OR:
208 computeMayUse(n.getLastChild(), cfgNode, output, true);
209 computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
210 return;
211
212 case Token.HOOK:
213 computeMayUse(n.getLastChild(), cfgNode, output, true);
214 computeMayUse(n.getFirstChild().getNext(), cfgNode, output, true);
215 computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
216 return;
217
218 case Token.VAR:
219 Node varName = n.getFirstChild();
220 Preconditions.checkState(n.hasChildren(), "AST should be normalized");
221
222 if (varName.hasChildren()) {
223 computeMayUse(varName.getFirstChild(), cfgNode, output, conditional);
224 if (!conditional) {
225 removeFromUseIfLocal(varName.getString(), output);
226 }
227 }
228 return;
229
230 default:
231 if (NodeUtil.isAssignmentOp(n) && n.getFirstChild().isName()) {
232 Node name = n.getFirstChild();
233 if (!conditional) {
234 removeFromUseIfLocal(name.getString(), output);
235 }
236
237 // In case of a += "Hello". There is a read of a.
238 if (!n.isAssign()) {
239 addToUseIfLocal(name.getString(), cfgNode, output);
240 }
241
242 computeMayUse(name.getNext(), cfgNode, output, conditional);
243 } else {
244 /*
245 * We want to traverse in reverse order because we want the LAST
246 * definition in the sub-tree....
247 * But we have no better way to traverse in reverse other :'(
248 */
249 for (Node c = n.getLastChild(); c != null; c = n.getChildBefore(c)) {
250 computeMayUse(c, cfgNode, output, conditional);
251 }
252 }
253 }
254 }
255
256 /**
257 * Sets the variable for the given name to the node value in the upward
258 * exposed lattice. Do nothing if the variable name is one of the escaped
259 * variable.
260 */
261 private void addToUseIfLocal(String name, Node node, ReachingUses use) {
262 Var var = jsScope.getVar(name);
263 if (var == null || var.scope != jsScope) {
264 return;
265 }
266 if (!escaped.contains(var)) {
267 use.mayUseMap.put(var, node);
268 }
269 }
270
271 /**
272 * Removes the variable for the given name from the node value in the upward
273 * exposed lattice. Do nothing if the variable name is one of the escaped
274 * variable.
275 */
276 private void removeFromUseIfLocal(String name, ReachingUses use) {
277 Var var = jsScope.getVar(name);
278 if (var == null || var.scope != jsScope) {
279 return;
280 }
281 if (!escaped.contains(var)) {
282 use.mayUseMap.removeAll(var);
283 }
284 }
285
286 /**
287 * Gets a list of nodes that may be using the value assigned to {@code name}
288 * in {@code defNode}. {@code defNode} must be one of the control flow graph
289 * nodes.
290 *
291 * @param name name of the variable. It can only be names of local variable
292 * that are not function parameters, escaped variables or variables
293 * declared in catch.
294 * @param defNode The list of upward exposed use for the variable.
295 */
296 Collection<Node> getUses(String name, Node defNode) {
297 GraphNode<Node, Branch> n = getCfg().getNode(defNode);
298 Preconditions.checkNotNull(n);
299 FlowState<ReachingUses> state = n.getAnnotation();
300 return state.getOut().mayUseMap.get(jsScope.getVar(name));
301 }
302 }