| 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.Supplier; |
| 20 | import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; |
| 21 | import com.google.javascript.jscomp.Scope.Var; |
| 22 | import com.google.javascript.jscomp.parsing.Config; |
| 23 | import com.google.javascript.jscomp.type.ReverseAbstractInterpreter; |
| 24 | import com.google.javascript.rhino.InputId; |
| 25 | import com.google.javascript.rhino.Node; |
| 26 | import com.google.javascript.rhino.head.ErrorReporter; |
| 27 | import com.google.javascript.rhino.head.ast.AstRoot; |
| 28 | import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 29 | |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | |
| 33 | import javax.annotation.Nullable; |
| 34 | |
| 35 | /** |
| 36 | * An abstract compiler, to help remove the circular dependency of |
| 37 | * passes on JSCompiler. |
| 38 | * |
| 39 | * This is an abstract class, so that we can make the methods package-private. |
| 40 | * |
| 41 | * @author [email protected] (Nick Santos) |
| 42 | */ |
| 43 | public abstract class AbstractCompiler implements SourceExcerptProvider { |
| 44 | static final DiagnosticType READ_ERROR = DiagnosticType.error( |
| 45 | "JSC_READ_ERROR", "Cannot read: {0}"); |
| 46 | |
| 47 | private LifeCycleStage stage = LifeCycleStage.RAW; |
| 48 | |
| 49 | // TODO(nicksantos): Decide if all of these are really necessary. |
| 50 | // Many of them are just accessors that should be passed to the |
| 51 | // CompilerPass's constructor. |
| 52 | |
| 53 | /** |
| 54 | * Looks up an input (possibly an externs input) by input id. |
| 55 | * May return null. |
| 56 | */ |
| 57 | public abstract CompilerInput getInput(InputId inputId); |
| 58 | |
| 59 | /** |
| 60 | * Looks up a source file by name. May return null. |
| 61 | */ |
| 62 | abstract SourceFile getSourceFileByName(String sourceName); |
| 63 | |
| 64 | /** |
| 65 | * Creates a new externs file. |
| 66 | * @param name A name for the new externs file. |
| 67 | * @throws IllegalArgumentException If the name of the externs file conflicts |
| 68 | * with a pre-existing externs file. |
| 69 | */ |
| 70 | abstract CompilerInput newExternInput(String name); |
| 71 | |
| 72 | /** |
| 73 | * Gets the module graph. May return null if there aren't at least two |
| 74 | * modules. |
| 75 | */ |
| 76 | abstract JSModuleGraph getModuleGraph(); |
| 77 | |
| 78 | /** |
| 79 | * Gets the inputs in the order in which they are being processed. |
| 80 | * Only for use by {@code AbstractCompilerRunner}. |
| 81 | */ |
| 82 | abstract List<CompilerInput> getInputsInOrder(); |
| 83 | |
| 84 | /** |
| 85 | * Gets a central registry of type information from the compiled JS. |
| 86 | */ |
| 87 | public abstract JSTypeRegistry getTypeRegistry(); |
| 88 | |
| 89 | /** |
| 90 | * Gets a memoized scope creator with type information. |
| 91 | */ |
| 92 | abstract ScopeCreator getTypedScopeCreator(); |
| 93 | |
| 94 | /** |
| 95 | * Gets the top scope. |
| 96 | */ |
| 97 | public abstract Scope getTopScope(); |
| 98 | |
| 99 | /** |
| 100 | * Report an error or warning. |
| 101 | */ |
| 102 | public abstract void report(JSError error); |
| 103 | |
| 104 | /** |
| 105 | * Report an internal error. |
| 106 | */ |
| 107 | abstract void throwInternalError(String msg, Exception cause); |
| 108 | |
| 109 | /** |
| 110 | * Gets the current coding convention. |
| 111 | */ |
| 112 | public abstract CodingConvention getCodingConvention(); |
| 113 | |
| 114 | /** |
| 115 | * Report code changes. |
| 116 | */ |
| 117 | public abstract void reportCodeChange(); |
| 118 | |
| 119 | /** |
| 120 | * Logs a message under a central logger. |
| 121 | */ |
| 122 | abstract void addToDebugLog(String message); |
| 123 | |
| 124 | /** |
| 125 | * Sets the CssRenamingMap. |
| 126 | */ |
| 127 | abstract void setCssRenamingMap(CssRenamingMap map); |
| 128 | |
| 129 | /** |
| 130 | * Gets the CssRenamingMap. |
| 131 | */ |
| 132 | abstract CssRenamingMap getCssRenamingMap(); |
| 133 | |
| 134 | /** |
| 135 | * Gets a suitable SCRIPT node to serve as a parent for code insertion. If |
| 136 | * {@code module} contains any inputs, the returned node will be the SCRIPT |
| 137 | * node corresponding to its first input. If {@code module} is empty, on the |
| 138 | * other hand, then the returned node will be the first SCRIPT node in a |
| 139 | * non-empty module that {@code module} depends on (the deepest one possible). |
| 140 | * |
| 141 | * @param module A module. If null, will return the first SCRIPT node of all |
| 142 | * modules. |
| 143 | * @return A SCRIPT node (never null). |
| 144 | */ |
| 145 | abstract Node getNodeForCodeInsertion(JSModule module); |
| 146 | |
| 147 | /** |
| 148 | * Gets the central registry of type violations. |
| 149 | */ |
| 150 | abstract TypeValidator getTypeValidator(); |
| 151 | |
| 152 | /** |
| 153 | * Parses code for injecting. |
| 154 | */ |
| 155 | abstract Node parseSyntheticCode(String code); |
| 156 | |
| 157 | /** |
| 158 | * Parses code for injecting, and associate it with a given source file. |
| 159 | */ |
| 160 | abstract Node parseSyntheticCode(String filename, String code); |
| 161 | |
| 162 | /** |
| 163 | * Parses code for testing. |
| 164 | */ |
| 165 | abstract Node parseTestCode(String code); |
| 166 | |
| 167 | /** |
| 168 | * Prints a node to source code. |
| 169 | */ |
| 170 | abstract String toSource(Node root); |
| 171 | |
| 172 | /** |
| 173 | * Gets a default error reporter for injecting into Rhino. |
| 174 | */ |
| 175 | abstract ErrorReporter getDefaultErrorReporter(); |
| 176 | |
| 177 | /** |
| 178 | * Get an interpreter for type analysis. |
| 179 | */ |
| 180 | public abstract ReverseAbstractInterpreter getReverseAbstractInterpreter(); |
| 181 | |
| 182 | /** |
| 183 | * @return The current life-cycle stage of the AST we're working on. |
| 184 | */ |
| 185 | LifeCycleStage getLifeCycleStage() { |
| 186 | return stage; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Generates unique ids. |
| 191 | */ |
| 192 | abstract Supplier<String> getUniqueNameIdSupplier(); |
| 193 | |
| 194 | /** |
| 195 | * @return Whether any errors have been encountered that |
| 196 | * should stop the compilation process. |
| 197 | */ |
| 198 | abstract boolean hasHaltingErrors(); |
| 199 | |
| 200 | /** |
| 201 | * Register a listener for code change events. |
| 202 | */ |
| 203 | abstract void addChangeHandler(CodeChangeHandler handler); |
| 204 | |
| 205 | /** |
| 206 | * Remove a listener for code change events. |
| 207 | */ |
| 208 | abstract void removeChangeHandler(CodeChangeHandler handler); |
| 209 | |
| 210 | /** |
| 211 | * Returns true if compiling in IDE mode. |
| 212 | */ |
| 213 | abstract boolean isIdeMode(); |
| 214 | |
| 215 | /** |
| 216 | * @return Whether the compiler is in ES5Mode. |
| 217 | */ |
| 218 | abstract boolean acceptEcmaScript5(); |
| 219 | |
| 220 | /** |
| 221 | * @return Whether the compiler accepts `const' keyword. |
| 222 | */ |
| 223 | abstract boolean acceptConstKeyword(); |
| 224 | |
| 225 | /** |
| 226 | * Returns the parser configuration. |
| 227 | */ |
| 228 | abstract Config getParserConfig(); |
| 229 | |
| 230 | /** |
| 231 | * Returns true if type checking is enabled. |
| 232 | */ |
| 233 | abstract boolean isTypeCheckingEnabled(); |
| 234 | |
| 235 | /** |
| 236 | * Normalizes the types of AST nodes in the given tree, and |
| 237 | * annotates any nodes to which the coding convention applies so that passes |
| 238 | * can read the annotations instead of using the coding convention. |
| 239 | */ |
| 240 | abstract void prepareAst(Node root); |
| 241 | |
| 242 | /** |
| 243 | * Gets the error manager. |
| 244 | */ |
| 245 | abstract public ErrorManager getErrorManager(); |
| 246 | |
| 247 | /** |
| 248 | * Set the current life-cycle state. |
| 249 | */ |
| 250 | void setLifeCycleStage(LifeCycleStage stage) { |
| 251 | this.stage = stage; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Are the nodes equal for the purpose of inlining? |
| 256 | * If type aware optimizations are on, type equality is checked. |
| 257 | */ |
| 258 | abstract boolean areNodesEqualForInlining(Node n1, Node n2); |
| 259 | |
| 260 | /** |
| 261 | * Set if RegExp global properties are used. |
| 262 | * @param references Whether there are references to the RegExp global object |
| 263 | * properties. |
| 264 | */ |
| 265 | abstract void setHasRegExpGlobalReferences(boolean references); |
| 266 | |
| 267 | /** |
| 268 | * @return Whether the AST contains references to the RegExp global object |
| 269 | * properties. |
| 270 | */ |
| 271 | abstract boolean hasRegExpGlobalReferences(); |
| 272 | |
| 273 | /** |
| 274 | * @return The error level the given error object will be reported at. |
| 275 | */ |
| 276 | abstract CheckLevel getErrorLevel(JSError error); |
| 277 | |
| 278 | static enum LifeCycleStage { |
| 279 | RAW, |
| 280 | |
| 281 | // See constraints put on the tree by Normalize.java |
| 282 | NORMALIZED, |
| 283 | |
| 284 | // The normalize pass has put constraints on the tree, |
| 285 | // but variables and properties have been renamed so |
| 286 | // coding conventions no longer apply. |
| 287 | NORMALIZED_OBFUSCATED; |
| 288 | |
| 289 | boolean isNormalized() { |
| 290 | return this == NORMALIZED || this == NORMALIZED_OBFUSCATED; |
| 291 | } |
| 292 | |
| 293 | boolean isNormalizedUnobfuscated() { |
| 294 | return this == NORMALIZED; |
| 295 | } |
| 296 | |
| 297 | boolean isNormalizedObfuscated() { |
| 298 | return this == NORMALIZED_OBFUSCATED; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Runs a given compiler-pass by calling its {@code process()} method. |
| 304 | * @param pass The pass to be run. |
| 305 | */ |
| 306 | abstract void process(CompilerPass pass); |
| 307 | |
| 308 | /** |
| 309 | * Returns the root node of the AST, which includes both externs and source. |
| 310 | */ |
| 311 | abstract Node getRoot(); |
| 312 | |
| 313 | // TODO(bashir) It would be good to extract a single dumb data object with |
| 314 | // only getters and setters that keeps all global information we keep for a |
| 315 | // compiler instance. Then move some of the functions of this class there. |
| 316 | |
| 317 | /** |
| 318 | * Updates the list of references for variables in global scope. |
| 319 | * |
| 320 | * @param refMapPatch Maps each variable to all of its references; may contain |
| 321 | * references collected from the whole AST or only a SCRIPT sub-tree. |
| 322 | * @param collectionRoot The root of sub-tree in which reference collection |
| 323 | * has been done. This should either be a SCRIPT node (if collection is |
| 324 | * done on a single file) or it is assumed that collection is on full AST. |
| 325 | */ |
| 326 | abstract void updateGlobalVarReferences(Map<Var, ReferenceCollection> |
| 327 | refMapPatch, Node collectionRoot); |
| 328 | |
| 329 | /** |
| 330 | * This can be used to get the list of all references to all global variables |
| 331 | * based on all previous calls to {@code updateGlobalVarReferences}. |
| 332 | * |
| 333 | * @return The reference collection map associated to global scope variable. |
| 334 | */ |
| 335 | abstract GlobalVarReferenceMap getGlobalVarReferences(); |
| 336 | |
| 337 | /** |
| 338 | * @return a CompilerInput that can be modified to add addition extern |
| 339 | * definitions; |
| 340 | */ |
| 341 | abstract CompilerInput getSynthesizedExternsInput(); |
| 342 | |
| 343 | /** |
| 344 | * @return a number in [0,1] range indicating an approximate progress of the |
| 345 | * last compile. Note this should only be used as a hint and no assumptions |
| 346 | * should be made on accuracy, even a completed compile may choose not to set |
| 347 | * this to 1.0 at the end. |
| 348 | */ |
| 349 | public abstract double getProgress(); |
| 350 | |
| 351 | /** |
| 352 | * Gets the last pass name set by setProgress. |
| 353 | */ |
| 354 | abstract String getLastPassName(); |
| 355 | |
| 356 | /** |
| 357 | * Sets the progress percentage as well as the name of the last pass that |
| 358 | * ran (if available). |
| 359 | * @param progress A precentage expressed as a double in the range [0, 1]. |
| 360 | * Use -1 if you just want to set the last pass name. |
| 361 | */ |
| 362 | abstract void setProgress(double progress, @Nullable String lastPassName); |
| 363 | |
| 364 | /** |
| 365 | * The subdir js/ contains libraries of code that we inject |
| 366 | * at compile-time only if requested by this function. |
| 367 | * |
| 368 | * Notice that these libraries will almost always create global symbols. |
| 369 | * |
| 370 | * @param resourceName The name of the library. For example, if "base" is |
| 371 | * is specified, then we load js/base.js |
| 372 | * @return If new code was injected, returns the last expression node of the |
| 373 | * library. If the caller needs to add additional code, they should add |
| 374 | * it as the next sibling of this node. If new code was not injected, |
| 375 | * returns null. |
| 376 | */ |
| 377 | abstract Node ensureLibraryInjected(String resourceName); |
| 378 | |
| 379 | /** |
| 380 | * Stores the "new" Rhino parse tree for a given source file. |
| 381 | * @param sourceName The source file name. |
| 382 | * @param astRoot The "new" Rhino parse tree. |
| 383 | */ |
| 384 | abstract void setOldParseTree(String sourceName, AstRoot astRoot); |
| 385 | |
| 386 | /** |
| 387 | * Gets an old format parse tree for a given source file. |
| 388 | * @param sourceName The source file name to get the tree for. |
| 389 | * @return The "new" Rhino parse tree for the given source file. |
| 390 | */ |
| 391 | abstract AstRoot getOldParseTreeByName(String sourceName); |
| 392 | } |