| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public abstract class PassConfig { |
| 36 |
|
| 37 |
|
| 38 |
final CompilerOptions options; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private MemoizedScopeCreator typedScopeCreator; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private TypedScopeCreator internalScopeCreator; |
| 51 |
|
| 52 |
|
| 53 |
Scope topScope = null; |
| 54 |
|
| 55 |
public PassConfig(CompilerOptions options) { |
| 56 |
this.options = options; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
void patchGlobalTypedScope(AbstractCompiler compiler, Node scriptRoot) { |
| 85 |
Preconditions.checkNotNull(internalScopeCreator); |
| 86 |
internalScopeCreator.patchGlobalScope(topScope, scriptRoot); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
MemoizedScopeCreator getTypedScopeCreator() { |
| 93 |
return typedScopeCreator; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Scope getTopScope() { |
| 100 |
return topScope; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
abstract protected List<PassFactory> getChecks(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
abstract protected List<PassFactory> getOptimizations(); |
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
protected abstract State getIntermediateState(); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
protected abstract void setIntermediateState(State state); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 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 |
|
| 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 |
} |