Debian Codegraph @

src/com/google/javascript/jscomp/MemoizedScopeCreator.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.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Maps;
25 import com.google.javascript.jscomp.Scope.Var;
26 import com.google.javascript.rhino.Node;
27 import com.google.javascript.rhino.jstype.StaticSymbolTable;
28
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33
34 /**
35 * Memoize a scope creator.
36 *
37 * This allows you to make multiple passes, without worrying about
38 * the expense of generating Scope objects over and over again.
39 *
40 * On the other hand, you also have to be more aware of what your passes
41 * are doing. Scopes are memoized stupidly, so if the underlying tree
42 * changes, the scope may be out of sync.
43 *
44 * @author [email protected] (Nick Santos)
45 */
46 class MemoizedScopeCreator
47 implements ScopeCreator, StaticSymbolTable<Var, Var> {
48
49 private final Map<Node, Scope> scopes = Maps.newHashMap();
50 private final ScopeCreator delegate;
51
52 /**
53 * @param delegate The real source of Scope objects.
54 */
55 MemoizedScopeCreator(ScopeCreator delegate) {
56 this.delegate = delegate;
57 }
58
59 @Override
60 public Iterable<Var> getReferences(Var var) {
61 return ImmutableList.of(var);
62 }
63
64 @Override
65 public Scope getScope(Var var) {
66 return var.scope;
67 }
68
69 @Override
70 public Iterable<Var> getAllSymbols() {
71 List<Var> vars = Lists.newArrayList();
72 for (Scope s : scopes.values()) {
73 Iterables.addAll(vars, s.getAllSymbols());
74 }
75 return vars;
76 }
77
78 @Override
79 public Scope createScope(Node n, Scope parent) {
80 Scope scope = scopes.get(n);
81 if (scope == null) {
82 scope = delegate.createScope(n, parent);
83 scopes.put(n, scope);
84 } else {
85 Preconditions.checkState(parent == scope.getParent());
86 }
87 return scope;
88 }
89
90 Collection<Scope> getAllMemoizedScopes() {
91 return Collections.unmodifiableCollection(scopes.values());
92 }
93
94 Scope getScopeIfMemoized(Node n) {
95 return scopes.get(n);
96 }
97
98 /**
99 * Removes all scopes with root nodes from a given script file.
100 *
101 * @param scriptName the name of the script file to remove nodes for.
102 */
103 void removeScopesForScript(String scriptName) {
104 for (Node scopeRoot : ImmutableSet.copyOf(scopes.keySet())) {
105 if (scriptName.equals(scopeRoot.getSourceFileName())) {
106 scopes.remove(scopeRoot);
107 }
108 }
109 }
110 }