Debian Codegraph @

src/com/google/javascript/jscomp/PassConfig.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.Iterables;
21 import com.google.javascript.jscomp.graph.GraphvizGraph;
22 import com.google.javascript.jscomp.graph.LinkedDirectedGraph;
23 import com.google.javascript.rhino.Node;
24
25 import java.io.Serializable;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 /**
31 * Pass factories and meta-data for native Compiler passes.
32 *
33 * @author [email protected] (Nick Santos)
34 */
35 public abstract class PassConfig {
36
37 // Used by subclasses in this package.
38 final CompilerOptions options;
39
40 /**
41 * A memoized version of scopeCreator. It must be memoized so that
42 * we can make two separate passes over the AST, one for inferring types
43 * and one for checking types.
44 */
45 private MemoizedScopeCreator typedScopeCreator;
46
47 /**
48 * This is the scope creator that {@code TypedScopeCreator} delegates to.
49 */
50 private TypedScopeCreator internalScopeCreator;
51
52 /** The global typed scope. */
53 Scope topScope = null;
54
55 public PassConfig(CompilerOptions options) {
56 this.options = options;
57 }
58
59 /**
60 * Regenerates the top scope from scratch.
61 *
62 * @param compiler The compiler for which the global scope is regenerated.
63 * @param root The root of the AST.
64 */
65 void regenerateGlobalTypedScope(AbstractCompiler compiler, Node root) {
66 internalScopeCreator = new TypedScopeCreator(compiler);
67 typedScopeCreator = new MemoizedScopeCreator(internalScopeCreator);
68 topScope = typedScopeCreator.createScope(root, null);
69 }
70
71 void clearTypedScope() {
72 internalScopeCreator = null;
73 typedScopeCreator = null;
74 topScope = null;
75 }
76
77 /**
78 * Regenerates the top scope potentially only for a sub-tree of AST and then
79 * copies information for the old global scope.
80 *
81 * @param compiler The compiler for which the global scope is generated.
82 * @param scriptRoot The root of the AST used to generate global scope.
83 */
84 void patchGlobalTypedScope(AbstractCompiler compiler, Node scriptRoot) {
85 Preconditions.checkNotNull(internalScopeCreator);
86 internalScopeCreator.patchGlobalScope(topScope, scriptRoot);
87 }
88
89 /**
90 * Gets the scope creator for typed scopes.
91 */
92 MemoizedScopeCreator getTypedScopeCreator() {
93 return typedScopeCreator;
94 }
95
96 /**
97 * Gets the global scope, with type information.
98 */
99 Scope getTopScope() {
100 return topScope;
101 }
102
103 /**
104 * Gets the checking passes to run.
105 *
106 * Checking passes revolve around emitting warnings and errors.
107 * They also may include pre-processor passes needed to do
108 * error analysis more effectively.
109 *
110 * Clients that only want to analyze code (like IDEs) and not emit
111 * code will only run checks and not optimizations.
112 */
113 abstract protected List<PassFactory> getChecks();
114
115 /**
116 * Gets the optimization passes to run.
117 *
118 * Optimization passes revolve around producing smaller and faster code.
119 * They should always run after checking passes.
120 */
121 abstract protected List<PassFactory> getOptimizations();
122
123 /**
124 * Gets a graph of the passes run. For debugging.
125 */
126 GraphvizGraph getPassGraph() {
127 LinkedDirectedGraph<String, String> graph =
128 LinkedDirectedGraph.createWithoutAnnotations();
129 Iterable<PassFactory> allPasses =
130 Iterables.concat(getChecks(), getOptimizations());
131 String lastPass = null;
132 String loopStart = null;
133 for (PassFactory pass : allPasses) {
134 String passName = pass.getName();
135 int i = 1;
136 while (graph.hasNode(passName)) {
137 passName = pass.getName() + (i++);
138 }
139 graph.createNode(passName);
140
141 if (loopStart == null && !pass.isOneTimePass()) {
142 loopStart = passName;
143 } else if (loopStart != null && pass.isOneTimePass()) {
144 graph.connect(lastPass, "loop", loopStart);
145 loopStart = null;
146 }
147
148 if (lastPass != null) {
149 graph.connect(lastPass, "", passName);
150 }
151 lastPass = passName;
152 }
153 return graph;
154 }
155
156 /**
157 * Create a type inference pass.
158 */
159 final TypeInferencePass makeTypeInference(AbstractCompiler compiler) {
160 return new TypeInferencePass(
161 compiler, compiler.getReverseAbstractInterpreter(),
162 topScope, typedScopeCreator);
163 }
164
165 final InferJSDocInfo makeInferJsDocInfo(AbstractCompiler compiler) {
166 return new InferJSDocInfo(compiler);
167 }
168
169 /**
170 * Create a type-checking pass.
171 */
172 final TypeCheck makeTypeCheck(AbstractCompiler compiler) {
173 return new TypeCheck(
174 compiler,
175 compiler.getReverseAbstractInterpreter(),
176 compiler.getTypeRegistry(),
177 topScope,
178 typedScopeCreator,
179 options.reportMissingOverride,
180 options.reportUnknownTypes)
181 .reportMissingProperties(options.enables(
182 DiagnosticGroup.forType(TypeCheck.INEXISTENT_PROPERTY)));
183 }
184
185 /**
186 * Insert the given pass factory before the factory of the given name.
187 */
188 final static void addPassFactoryBefore(
189 List<PassFactory> factoryList, PassFactory factory, String passName) {
190 factoryList.add(
191 findPassIndexByName(factoryList, passName), factory);
192 }
193
194 /**
195 * Find a pass factory with the same name as the given one, and replace it.
196 */
197 final static void replacePassFactory(
198 List<PassFactory> factoryList, PassFactory factory) {
199 factoryList.set(
200 findPassIndexByName(factoryList, factory.getName()), factory);
201 }
202
203 /**
204 * Throws an exception if no pass with the given name exists.
205 */
206 private static int findPassIndexByName(
207 List<PassFactory> factoryList, String name) {
208 for (int i = 0; i < factoryList.size(); i++) {
209 if (factoryList.get(i).getName().equals(name)) {
210 return i;
211 }
212 }
213
214 throw new IllegalArgumentException(
215 "No factory named '" + name + "' in the factory list");
216 }
217
218 /**
219 * Find the first pass provider that does not have a delegate.
220 */
221 final PassConfig getBasePassConfig() {
222 PassConfig current = this;
223 while (current instanceof PassConfigDelegate) {
224 current = ((PassConfigDelegate) current).delegate;
225 }
226 return current;
227 }
228
229 /**
230 * Get intermediate state for a running pass config, so it can
231 * be paused and started again later.
232 */
233 protected abstract State getIntermediateState();
234
235 /**
236 * Set the intermediate state for a pass config, to restart
237 * a compilation process that had been previously paused.
238 */
239 protected abstract void setIntermediateState(State state);
240
241 /**
242 * An implementation of PassConfig that just proxies all its method calls
243 * into an inner class.
244 */
245 static class PassConfigDelegate extends PassConfig {
246
247 private final PassConfig delegate;
248
249 PassConfigDelegate(PassConfig delegate) {
250 super(delegate.options);
251 this.delegate = delegate;
252 }
253
254 @Override protected List<PassFactory> getChecks() {
255 return delegate.getChecks();
256 }
257
258 @Override protected List<PassFactory> getOptimizations() {
259 return delegate.getOptimizations();
260 }
261
262 @Override MemoizedScopeCreator getTypedScopeCreator() {
263 return delegate.getTypedScopeCreator();
264 }
265
266 @Override Scope getTopScope() {
267 return delegate.getTopScope();
268 }
269
270 @Override protected State getIntermediateState() {
271 return delegate.getIntermediateState();
272 }
273
274 @Override protected void setIntermediateState(State state) {
275 delegate.setIntermediateState(state);
276 }
277 }
278
279 /**
280 * Intermediate state for a running pass configuration.
281 */
282 public static class State implements Serializable {
283 private static final long serialVersionUID = 1L;
284
285 final Map<String, Integer> cssNames;
286 final Set<String> exportedNames;
287 final CrossModuleMethodMotion.IdGenerator crossModuleIdGenerator;
288 final VariableMap variableMap;
289 final VariableMap propertyMap;
290 final VariableMap anonymousFunctionNameMap;
291 final VariableMap stringMap;
292 final FunctionNames functionNames;
293 final String idGeneratorMap;
294
295 public State(Map<String, Integer> cssNames, Set<String> exportedNames,
296 CrossModuleMethodMotion.IdGenerator crossModuleIdGenerator,
297 VariableMap variableMap, VariableMap propertyMap,
298 VariableMap anonymousFunctionNameMap,
299 VariableMap stringMap, FunctionNames functionNames,
300 String idGeneratorMap) {
301 this.cssNames = cssNames;
302 this.exportedNames = exportedNames;
303 this.crossModuleIdGenerator = crossModuleIdGenerator;
304 this.variableMap = variableMap;
305 this.propertyMap = propertyMap;
306 this.anonymousFunctionNameMap = anonymousFunctionNameMap;
307 this.stringMap = stringMap;
308 this.idGeneratorMap = idGeneratorMap;
309 this.functionNames = functionNames;
310 }
311 }
312 }