Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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.Lists;
21 import com.google.common.collect.Maps;
22 import com.google.javascript.jscomp.ReferenceCollectingCallback.Reference;
23 import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection;
24 import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceMap;
25 import com.google.javascript.jscomp.Scope.Var;
26 import com.google.javascript.rhino.InputId;
27 import com.google.javascript.rhino.Node;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 /**
33 * An implementation for {@code ReferenceMap} that is specific to global scope
34 * and can be used in different passes. In other words instead of relying on
35 * Var object it relies on the name of the variable. It also supports hot-swap
36 * update of reference map for a specific script.
37 *
38 * @see ReferenceCollectingCallback#exitScope(NodeTraversal)
39 *
40 * @author [email protected] (Bashir Sadjad)
41 */
42 class GlobalVarReferenceMap implements ReferenceMap {
43
44 private Map<String, ReferenceCollection> refMap = null;
45
46 private final Map<InputId, Integer> inputOrder;
47
48 /**
49 * @param inputs The ordered list of all inputs for the compiler.
50 */
51 GlobalVarReferenceMap(List<CompilerInput> inputs, List<CompilerInput> externs) {
52 inputOrder = Maps.newHashMap();
53 int ind = 0;
54 for (CompilerInput extern : externs) {
55 inputOrder.put(extern.getInputId(), ind);
56 ind++;
57 }
58 for (CompilerInput input : inputs) {
59 inputOrder.put(input.getInputId(), ind);
60 ind++;
61 }
62 }
63
64 @Override
65 public ReferenceCollection getReferences(Var var) {
66 if (!var.isGlobal()) {
67 return null;
68 }
69 return refMap.get(var.getName());
70 }
71
72 /**
73 * Resets global var reference map with the new provide map.
74 *
75 * @param globalRefMap The reference map result of a
76 * {@link ReferenceCollectingCallback} pass collected from the whole AST.
77 */
78 private void resetGlobalVarReferences(
79 Map<Var, ReferenceCollection> globalRefMap) {
80 refMap = Maps.newHashMap();
81 for (Entry<Var, ReferenceCollection> entry : globalRefMap.entrySet()) {
82 Var var = entry.getKey();
83 if (var.isGlobal()) {
84 refMap.put(var.getName(), entry.getValue());
85 }
86 }
87 }
88
89 /**
90 * Updates the internal reference map based on the provided parameters. If
91 * {@code scriptRoot} is not SCRIPT, it basically replaces the internal map
92 * with the new one, otherwise it replaces all the information associated to
93 * the given script.
94 *
95 * @param refMapPatch The reference map result of a
96 * {@link ReferenceCollectingCallback} pass which might be collected from
97 * the whole AST or just a sub-tree associated to a SCRIPT node.
98 * @param root AST sub-tree root on which reference collection was done.
99 */
100 void updateGlobalVarReferences(Map<Var, ReferenceCollection>
101 refMapPatch, Node root) {
102 if (refMap == null || !root.isScript()) {
103 resetGlobalVarReferences(refMapPatch);
104 return;
105 }
106
107 InputId inputId = root.getInputId();
108 Preconditions.checkNotNull(inputId);
109 // Note there are two assumptions here (i) the order of compiler inputs
110 // has not changed and (ii) all references are in the order they appear
111 // in AST (this is enforced in ReferenceCollectionCallback).
112 removeScriptReferences(inputId);
113 for (Entry<Var, ReferenceCollection> entry : refMapPatch.entrySet()) {
114 Var var = entry.getKey();
115 if (var.isGlobal()) {
116 replaceReferences(var.getName(), inputId, entry.getValue());
117 }
118 }
119 }
120
121 private void removeScriptReferences(InputId inputId) {
122 Preconditions.checkNotNull(inputId);
123
124 if (!inputOrder.containsKey(inputId)) {
125 return; // Input did not exist when last computed, so skip
126 }
127 // TODO(bashir): If this is too slow it is not too difficult to make it
128 // faster with keeping an index for variables accessed in sourceName.
129 for (ReferenceCollection collection : refMap.values()) {
130 if (collection == null) {
131 continue;
132 }
133 List<Reference> oldRefs = collection.references;
134 SourceRefRange range = findSourceRefRange(oldRefs, inputId);
135 List<Reference> newRefs = Lists.newArrayList(range.refsBefore());
136 newRefs.addAll(range.refsAfter());
137 collection.references = newRefs;
138 }
139 }
140
141 private void replaceReferences(String varName, InputId inputId,
142 ReferenceCollection newSourceCollection) {
143 ReferenceCollection combined = new ReferenceCollection();
144 List<Reference> combinedRefs = combined.references;
145 ReferenceCollection oldCollection = refMap.get(varName);
146 refMap.put(varName, combined);
147 if (oldCollection == null) {
148 combinedRefs.addAll(newSourceCollection.references);
149 return;
150 }
151 // otherwise replace previous references that are from sourceName
152 SourceRefRange range = findSourceRefRange(oldCollection.references,
153 inputId);
154 combinedRefs.addAll(range.refsBefore());
155 combinedRefs.addAll(newSourceCollection.references);
156 combinedRefs.addAll(range.refsAfter());
157 }
158
159 /**
160 * Finds the range of references associated to {@code sourceName}. Note that
161 * even if there is no sourceName references the returned information can be
162 * used to decide where to insert new sourceName refs.
163 */
164 private SourceRefRange findSourceRefRange(List<Reference> refList,
165 InputId inputId) {
166 Preconditions.checkNotNull(inputId);
167
168 // TODO(bashir): We can do binary search here, but since this is fast enough
169 // right now, we just do a linear search for simplicity.
170 int lastBefore = -1;
171 int firstAfter = refList.size();
172 int index = 0;
173
174 Preconditions.checkState(inputOrder.containsKey(inputId), inputId.getIdName());
175 int sourceInputOrder = inputOrder.get(inputId);
176 for (Reference ref : refList) {
177 Preconditions.checkNotNull(ref.getInputId());
178 int order = inputOrder.get(ref.getInputId());
179 if (order < sourceInputOrder) {
180 lastBefore = index;
181 } else if (order > sourceInputOrder) {
182 firstAfter = index;
183 break;
184 }
185 index++;
186 }
187 return new SourceRefRange(refList, lastBefore, firstAfter);
188 }
189
190 private static class SourceRefRange {
191 private final int lastBefore;
192 private final int firstAfter;
193 private final List<Reference> refList;
194
195 SourceRefRange(List<Reference> refList, int lastBefore,
196 int firstAfter) {
197 this.lastBefore = Math.max(lastBefore, -1);
198 this.firstAfter = Math.min(firstAfter, refList.size());
199 this.refList = refList;
200 }
201
202 /** Note that the returned list is backed by {@code refList}! */
203 List<Reference> refsBefore() {
204 return refList.subList(0, lastBefore + 1);
205 }
206
207 /** Note that the returned list is backed by {@code refList}! */
208 List<Reference> refsAfter() {
209 return refList.subList(firstAfter, refList.size());
210 }
211 }
212
213 /**
214 * @param globalScope a new Global Scope to replace the scope of references
215 * with.
216 */
217 public void updateReferencesWithGlobalScope(Scope globalScope) {
218 for (ReferenceCollection collection : refMap.values()) {
219 List<Reference> newRefs =
220 Lists.newArrayListWithCapacity(collection.references.size());
221 for (Reference ref : collection.references) {
222 if (ref.getScope() != globalScope) {
223 newRefs.add(ref.cloneWithNewScope(globalScope));
224 } else {
225 newRefs.add(ref);
226 }
227 }
228 collection.references = newRefs;
229 }
230 }
231
232 /**
233 * A CleanupPass implementation that will replace references to old Syntactic
234 * Global Scopes generated in previous compile runs with references to the
235 * Global Typed Scope.
236 *
237 * @author [email protected] (Tyler Goodwin)
238 */
239 static class GlobalVarRefCleanupPass implements HotSwapCompilerPass {
240
241 private final AbstractCompiler compiler;
242
243 public GlobalVarRefCleanupPass(AbstractCompiler compiler) {
244 this.compiler = compiler;
245 }
246
247 @Override
248 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
249 GlobalVarReferenceMap refMap = compiler.getGlobalVarReferences();
250 if (refMap != null) {
251 refMap.updateReferencesWithGlobalScope(compiler.getTopScope());
252 }
253 }
254
255 @Override
256 public void process(Node externs, Node root) {
257 // GlobalVarRefCleanupPass should not do work during process.
258 }
259 }
260 }