Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.ImmutableMap;
21 import com.google.common.collect.Maps;
22 import com.google.common.collect.Sets;
23 import com.google.javascript.jscomp.Scope.Var;
24 import com.google.javascript.jscomp.type.FlowScope;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.jstype.JSType;
27 import com.google.javascript.rhino.jstype.SimpleSlot;
28 import com.google.javascript.rhino.jstype.StaticScope;
29 import com.google.javascript.rhino.jstype.StaticSlot;
30
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36 * A flow scope that tries to store as little symbol information as possible,
37 * instead delegating to its parents. Optimized for low memory use.
38 *
39 * @author [email protected] (Nick Santos)
40 */
41 class LinkedFlowScope implements FlowScope {
42 // The closest flow scope cache.
43 private final FlatFlowScopeCache cache;
44
45 // The parent flow scope.
46 private final LinkedFlowScope parent;
47
48 // The distance between this flow scope and the closest flat flow scope.
49 private int depth;
50
51 static final int MAX_DEPTH = 250;
52
53 // A FlatFlowScopeCache equivalent to this scope.
54 private FlatFlowScopeCache flattened;
55
56 // Flow scopes assume that all their ancestors are immutable.
57 // So once a child scope is created, this flow scope may not be modified.
58 private boolean frozen = false;
59
60 // The last slot defined in this flow instruction, and the head of the
61 // linked list of slots.
62 private LinkedFlowSlot lastSlot;
63
64 private LinkedFlowScope(FlatFlowScopeCache cache,
65 LinkedFlowScope directParent) {
66 this.cache = cache;
67 if (directParent == null) {
68 this.lastSlot = null;
69 this.depth = 0;
70 this.parent = cache.linkedEquivalent;
71 } else {
72 this.lastSlot = directParent.lastSlot;
73 this.depth = directParent.depth + 1;
74 this.parent = directParent;
75 }
76 }
77
78 LinkedFlowScope(FlatFlowScopeCache cache) {
79 this(cache, null);
80 }
81
82 LinkedFlowScope(LinkedFlowScope directParent) {
83 this(directParent.cache, directParent);
84 }
85
86 /** Gets the function scope for this flow scope. */
87 private Scope getFunctionScope() {
88 return cache.functionScope;
89 }
90
91 /** Whether this flows from a bottom scope. */
92 private boolean flowsFromBottom() {
93 return getFunctionScope().isBottom();
94 }
95
96 /**
97 * Creates an entry lattice for the flow.
98 */
99 public static LinkedFlowScope createEntryLattice(Scope scope) {
100 return new LinkedFlowScope(new FlatFlowScopeCache(scope));
101 }
102
103 @Override
104 public void inferSlotType(String symbol, JSType type) {
105 Preconditions.checkState(!frozen);
106 lastSlot = new LinkedFlowSlot(symbol, type, lastSlot);
107 depth++;
108 cache.dirtySymbols.add(symbol);
109 }
110
111 @Override
112 public void inferQualifiedSlot(Node node, String symbol, JSType bottomType,
113 JSType inferredType) {
114 Scope functionScope = getFunctionScope();
115 if (functionScope.isLocal()) {
116 if (functionScope.getVar(symbol) == null && !functionScope.isBottom()) {
117 functionScope.declare(symbol, node, bottomType, null);
118 }
119
120 inferSlotType(symbol, inferredType);
121 }
122 }
123
124 @Override
125 public JSType getTypeOfThis() {
126 return cache.functionScope.getTypeOfThis();
127 }
128
129 @Override
130 public Node getRootNode() {
131 return getFunctionScope().getRootNode();
132 }
133
134 @Override
135 public StaticScope<JSType> getParentScope() {
136 return getFunctionScope().getParentScope();
137 }
138
139 /**
140 * Get the slot for the given symbol.
141 */
142 @Override
143 public StaticSlot<JSType> getSlot(String name) {
144 if (cache.dirtySymbols.contains(name)) {
145 for (LinkedFlowSlot slot = lastSlot;
146 slot != null; slot = slot.parent) {
147 if (slot.getName().equals(name)) {
148 return slot;
149 }
150 }
151 }
152 return cache.getSlot(name);
153 }
154
155 @Override
156 public StaticSlot<JSType> getOwnSlot(String name) {
157 throw new UnsupportedOperationException();
158 }
159
160 @Override
161 public FlowScope createChildFlowScope() {
162 frozen = true;
163
164 if (depth > MAX_DEPTH) {
165 if (flattened == null) {
166 flattened = new FlatFlowScopeCache(this);
167 }
168 return new LinkedFlowScope(flattened);
169 }
170
171 return new LinkedFlowScope(this);
172 }
173
174 /**
175 * Iterate through all the linked flow scopes before this one.
176 * If there's one and only one slot defined between this scope
177 * and the blind scope, return it.
178 */
179 @Override
180 public StaticSlot<JSType> findUniqueRefinedSlot(FlowScope blindScope) {
181 StaticSlot<JSType> result = null;
182
183 for (LinkedFlowScope currentScope = this;
184 currentScope != blindScope;
185 currentScope = currentScope.parent) {
186 for (LinkedFlowSlot currentSlot = currentScope.lastSlot;
187 currentSlot != null &&
188 (currentScope.parent == null ||
189 currentScope.parent.lastSlot != currentSlot);
190 currentSlot = currentSlot.parent) {
191 if (result == null) {
192 result = currentSlot;
193 } else if (!currentSlot.getName().equals(result.getName())) {
194 return null;
195 }
196 }
197 }
198
199 return result;
200 }
201
202 /**
203 * Look through the given scope, and try to find slots where it doesn't
204 * have enough type information. Then fill in that type information
205 * with stuff that we've inferred in the local flow.
206 */
207 @Override
208 public void completeScope(StaticScope<JSType> staticScope) {
209 Scope scope = (Scope) staticScope;
210 for (Iterator<Var> it = scope.getVars(); it.hasNext();) {
211 Var var = it.next();
212 if (var.isTypeInferred()) {
213 JSType type = var.getType();
214 if (type == null || type.isUnknownType()) {
215 JSType flowType = getSlot(var.getName()).getType();
216 var.setType(flowType);
217 }
218 }
219 }
220 }
221
222 /**
223 * Remove flow scopes that add nothing to the flow.
224 */
225 // NOTE(nicksantos): This function breaks findUniqueRefinedSlot, because
226 // findUniqueRefinedSlot assumes that this scope is a direct descendant
227 // of blindScope. This is not necessarily true if this scope has been
228 // optimize()d and blindScope has not. This should be fixed. For now,
229 // we only use optimize() where we know that we won't have to do
230 // a findUniqueRefinedSlot on it.
231 @Override
232 public LinkedFlowScope optimize() {
233 LinkedFlowScope current;
234 for (current = this;
235 current.parent != null &&
236 current.lastSlot == current.parent.lastSlot;
237 current = current.parent) {}
238 return current;
239 }
240
241 /** Join the two FlowScopes. */
242 static class FlowScopeJoinOp extends JoinOp.BinaryJoinOp<FlowScope> {
243 @SuppressWarnings("unchecked")
244 @Override
245 public FlowScope apply(FlowScope a, FlowScope b) {
246 // To join the two scopes, we have to
247 LinkedFlowScope linkedA = (LinkedFlowScope) a;
248 LinkedFlowScope linkedB = (LinkedFlowScope) b;
249 linkedA.frozen = true;
250 linkedB.frozen = true;
251 if (linkedA.optimize() == linkedB.optimize()) {
252 return linkedA.createChildFlowScope();
253 }
254 return new LinkedFlowScope(new FlatFlowScopeCache(linkedA, linkedB));
255 }
256 }
257
258 @Override
259 public boolean equals(Object other) {
260 if (other instanceof LinkedFlowScope) {
261 LinkedFlowScope that = (LinkedFlowScope) other;
262 if (this.optimize() == that.optimize()) {
263 return true;
264 }
265
266 // If two flow scopes are in the same function, then they could have
267 // two possible function scopes: the real one and the BOTTOM scope.
268 // If they have different function scopes, we *should* iterate through all
269 // the variables in each scope and compare. However, 99.9% of the time,
270 // they're not equal. And the other .1% of the time, we can pretend
271 // they're equal--this just means that data flow analysis will have
272 // to propagate the entry lattice a little bit further than it
273 // really needs to. Everything will still come out ok.
274 if (this.getFunctionScope() != that.getFunctionScope()) {
275 return false;
276 }
277
278 if (cache == that.cache) {
279 // If the two flow scopes have the same cache, then we can check
280 // equality a lot faster: by just looking at the "dirty" elements
281 // in the cache, and comparing them in both scopes.
282 for (String name : cache.dirtySymbols) {
283 if (diffSlots(getSlot(name), that.getSlot(name))) {
284 return false;
285 }
286 }
287
288 return true;
289 }
290
291 Map<String, StaticSlot<JSType>> myFlowSlots = allFlowSlots();
292 Map<String, StaticSlot<JSType>> otherFlowSlots = that.allFlowSlots();
293
294 for (StaticSlot<JSType> slot : myFlowSlots.values()) {
295 if (diffSlots(slot, otherFlowSlots.get(slot.getName()))) {
296 return false;
297 }
298 otherFlowSlots.remove(slot.getName());
299 }
300 for (StaticSlot<JSType> slot : otherFlowSlots.values()) {
301 if (diffSlots(slot, myFlowSlots.get(slot.getName()))) {
302 return false;
303 }
304 }
305 return true;
306 }
307 return false;
308 }
309
310 /**
311 * Determines whether two slots are meaningfully different for the
312 * purposes of data flow analysis.
313 */
314 private boolean diffSlots(StaticSlot<JSType> slotA,
315 StaticSlot<JSType> slotB) {
316 boolean aIsNull = slotA == null || slotA.getType() == null;
317 boolean bIsNull = slotB == null || slotB.getType() == null;
318 if (aIsNull && bIsNull) {
319 return false;
320 } else if (aIsNull ^ bIsNull) {
321 return true;
322 }
323
324 // Both slots and types must be non-null.
325 return slotA.getType().differsFrom(slotB.getType());
326 }
327
328 /**
329 * Gets all the symbols that have been defined before this point
330 * in the current flow. Does not return slots that have not changed during
331 * the flow.
332 *
333 * For example, consider the code:
334 * <code>
335 * var x = 3;
336 * function f() {
337 * var y = 5;
338 * y = 6; // FLOW POINT
339 * var z = y;
340 * return z;
341 * }
342 * </code>
343 * A FlowScope at FLOW POINT will return a slot for y, but not
344 * a slot for x or z.
345 */
346 private Map<String, StaticSlot<JSType>> allFlowSlots() {
347 Map<String, StaticSlot<JSType>> slots = Maps.newHashMap();
348 for (LinkedFlowSlot slot = lastSlot;
349 slot != null; slot = slot.parent) {
350 if (!slots.containsKey(slot.getName())) {
351 slots.put(slot.getName(), slot);
352 }
353 }
354
355 for (Map.Entry<String, StaticSlot<JSType>> symbolEntry : cache.symbols.entrySet()) {
356 if (!slots.containsKey(symbolEntry.getKey())) {
357 slots.put(symbolEntry.getKey(), symbolEntry.getValue());
358 }
359 }
360
361 return slots;
362 }
363
364 /**
365 * A static slot that can be used in a linked list.
366 */
367 private static class LinkedFlowSlot extends SimpleSlot {
368 final LinkedFlowSlot parent;
369
370 LinkedFlowSlot(String name, JSType type, LinkedFlowSlot parent) {
371 super(name, type, true);
372 this.parent = parent;
373 }
374 }
375
376 /**
377 * A map that tries to cache as much symbol table information
378 * as possible in a map. Optimized for fast lookup.
379 */
380 private static class FlatFlowScopeCache {
381 // The Scope for the entire function or for the global scope.
382 private final Scope functionScope;
383
384 // The linked flow scope that this cache represents.
385 private final LinkedFlowScope linkedEquivalent;
386
387 // All the symbols defined before this point in the local flow.
388 // May not include lazily declared qualified names.
389 private Map<String, StaticSlot<JSType>> symbols = Maps.newHashMap();
390
391 // Used to help make lookup faster for LinkedFlowScopes by recording
392 // symbols that may be redefined "soon", for an arbitrary definition
393 // of "soon". ;)
394 //
395 // More rigorously, if a symbol is redefined in a LinkedFlowScope,
396 // and this is the closest FlatFlowScopeCache, then that symbol is marked
397 // "dirty". In this way, we don't waste time looking in the LinkedFlowScope
398 // list for symbols that aren't defined anywhere nearby.
399 final Set<String> dirtySymbols = Sets.newHashSet();
400
401 // The cache at the bottom of the lattice.
402 FlatFlowScopeCache(Scope functionScope) {
403 this.functionScope = functionScope;
404 symbols = ImmutableMap.of();
405 linkedEquivalent = null;
406 }
407
408 // A cache in the middle of a long scope chain.
409 FlatFlowScopeCache(LinkedFlowScope directParent) {
410 FlatFlowScopeCache cache = directParent.cache;
411
412 functionScope = cache.functionScope;
413 symbols = directParent.allFlowSlots();
414 linkedEquivalent = directParent;
415 }
416
417 // A cache at the join of two scope chains.
418 FlatFlowScopeCache(LinkedFlowScope joinedScopeA,
419 LinkedFlowScope joinedScopeB) {
420 linkedEquivalent = null;
421
422 // Always prefer the "real" function scope to the faked-out
423 // bottom scope.
424 functionScope = joinedScopeA.flowsFromBottom() ?
425 joinedScopeB.getFunctionScope() : joinedScopeA.getFunctionScope();
426
427 Map<String, StaticSlot<JSType>> slotsA = joinedScopeA.allFlowSlots();
428 Map<String, StaticSlot<JSType>> slotsB = joinedScopeB.allFlowSlots();
429
430 symbols = slotsA;
431
432 // There are 5 different join cases:
433 // 1) The type is declared in joinedScopeA, not in joinedScopeB,
434 // and not in functionScope. Just use the one in A.
435 // 2) The type is declared in joinedScopeB, not in joinedScopeA,
436 // and not in functionScope. Just use the one in B.
437 // 3) The type is declared in functionScope and joinedScopeA, but
438 // not in joinedScopeB. Join the two types.
439 // 4) The type is declared in functionScope and joinedScopeB, but
440 // not in joinedScopeA. Join the two types.
441 // 5) The type is declared in joinedScopeA and joinedScopeB. Join
442 // the two types.
443 Set<String> symbolNames = Sets.newHashSet(symbols.keySet());
444 symbolNames.addAll(slotsB.keySet());
445
446 for (String name : symbolNames) {
447 StaticSlot<JSType> slotA = slotsA.get(name);
448 StaticSlot<JSType> slotB = slotsB.get(name);
449
450 JSType joinedType = null;
451 if (slotB == null || slotB.getType() == null) {
452 StaticSlot<JSType> fnSlot
453 = joinedScopeB.getFunctionScope().getSlot(name);
454 JSType fnSlotType = fnSlot == null ? null : fnSlot.getType();
455 if (fnSlotType == null) {
456 // Case #1 -- already inserted.
457 } else {
458 // Case #3
459 joinedType = slotA.getType().getLeastSupertype(fnSlotType);
460 }
461 } else if (slotA == null || slotA.getType() == null) {
462 StaticSlot<JSType> fnSlot
463 = joinedScopeA.getFunctionScope().getSlot(name);
464 JSType fnSlotType = fnSlot == null ? null : fnSlot.getType();
465 if (fnSlotType == null) {
466 // Case #2
467 symbols.put(name, slotB);
468 } else {
469 // Case #4
470 joinedType = slotB.getType().getLeastSupertype(fnSlotType);
471 }
472 } else {
473 // Case #5
474 joinedType =
475 slotA.getType().getLeastSupertype(slotB.getType());
476 }
477
478 if (joinedType != null) {
479 symbols.put(name, new SimpleSlot(name, joinedType, true));
480 }
481 }
482 }
483
484 /**
485 * Get the slot for the given symbol.
486 */
487 public StaticSlot<JSType> getSlot(String name) {
488 if (symbols.containsKey(name)) {
489 return symbols.get(name);
490 } else {
491 return functionScope.getSlot(name);
492 }
493 }
494 }
495 }