| 1 | /* |
| 2 | * Copyright 2010 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.annotations.VisibleForTesting; |
| 20 | import com.google.common.base.Preconditions; |
| 21 | import com.google.javascript.rhino.Node; |
| 22 | |
| 23 | /** |
| 24 | * An abstract class whose implementations run peephole optimizations: |
| 25 | * optimizations that look at a small section of code and either remove |
| 26 | * that code (if it is not needed) or replaces it with smaller code. |
| 27 | * |
| 28 | */ |
| 29 | abstract class AbstractPeepholeOptimization { |
| 30 | |
| 31 | private AbstractCompiler compiler; |
| 32 | |
| 33 | /** |
| 34 | * Given a node to optimize and a traversal, optimize the node. Subclasses |
| 35 | * should override to provide their own peephole optimization. |
| 36 | * |
| 37 | * @param subtree The subtree that will be optimized. |
| 38 | * @return The new version of the subtree (or null if the subtree or one of |
| 39 | * its parents was removed from the AST). If the subtree has not changed, |
| 40 | * this method must return {@code subtree}. |
| 41 | */ |
| 42 | abstract Node optimizeSubtree(Node subtree); |
| 43 | |
| 44 | /** |
| 45 | * Helper method for reporting an error to the compiler when applying a |
| 46 | * peephole optimization. |
| 47 | * |
| 48 | * @param diagnostic The error type |
| 49 | * @param n The node for which the error should be reported |
| 50 | */ |
| 51 | protected void report(DiagnosticType diagnostic, Node n) { |
| 52 | JSError error = |
| 53 | JSError.make(NodeUtil.getSourceName(n), n, diagnostic, n.toString()); |
| 54 | compiler.report(error); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Helper method for telling the compiler that something has changed. |
| 59 | * Subclasses must call these if they have changed the AST. |
| 60 | */ |
| 61 | protected void reportCodeChange() { |
| 62 | Preconditions.checkNotNull(compiler); |
| 63 | compiler.reportCodeChange(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Are the nodes equal for the purpose of inlining? |
| 68 | * If type aware optimizations are on, type equality is checked. |
| 69 | */ |
| 70 | protected boolean areNodesEqualForInlining(Node n1, Node n2) { |
| 71 | /* Our implementation delegates to the compiler. We provide this |
| 72 | * method because we don't want to expose Compiler to PeepholeOptimizations. |
| 73 | */ |
| 74 | Preconditions.checkNotNull(compiler); |
| 75 | return compiler.areNodesEqualForInlining(n1, n2); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Is the current AST normalized? (e.g. has the Normalize pass been run |
| 80 | * and has the Denormalize pass not yet been run?) |
| 81 | */ |
| 82 | protected boolean isASTNormalized() { |
| 83 | Preconditions.checkNotNull(compiler); |
| 84 | |
| 85 | return compiler.getLifeCycleStage().isNormalized(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Informs the optimization that a traversal will begin. |
| 90 | */ |
| 91 | void beginTraversal(AbstractCompiler compiler) { |
| 92 | this.compiler = compiler; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Informs the optimization that a traversal has completed. |
| 97 | * @param compiler The current compiler. |
| 98 | */ |
| 99 | void endTraversal(AbstractCompiler compiler) { |
| 100 | this.compiler = null; |
| 101 | } |
| 102 | |
| 103 | // NodeUtil's mayEffectMutableState and mayHaveSideEffects need access to the |
| 104 | // compiler object, route them through here to give them access. |
| 105 | |
| 106 | /** |
| 107 | * @return Whether the node may create new mutable state, or change existing |
| 108 | * state. |
| 109 | */ |
| 110 | boolean mayEffectMutableState(Node n) { |
| 111 | return NodeUtil.mayEffectMutableState(n, compiler); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return Whether the node may have side effects when executed. |
| 116 | */ |
| 117 | boolean mayHaveSideEffects(Node n) { |
| 118 | return NodeUtil.mayHaveSideEffects(n, compiler); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns true if the current node's type implies side effects. |
| 123 | * |
| 124 | * This is a non-recursive version of the may have side effects |
| 125 | * check; used to check wherever the current node's type is one of |
| 126 | * the reason's why a subtree has side effects. |
| 127 | */ |
| 128 | boolean nodeTypeMayHaveSideEffects(Node n) { |
| 129 | return NodeUtil.nodeTypeMayHaveSideEffects(n, compiler); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return Whether the source code version is ECMAScript 5 or later. |
| 134 | * Workarounds for quirks in browsers that do not support ES5 can be |
| 135 | * ignored when this is true. |
| 136 | */ |
| 137 | boolean isEcmaScript5OrGreater() { |
| 138 | return compiler != null |
| 139 | && compiler.acceptEcmaScript5(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return the current coding convention. |
| 144 | */ |
| 145 | CodingConvention getCodingConvention() { |
| 146 | // Note: this assumes a thread safe coding convention object. |
| 147 | return compiler.getCodingConvention(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Check if the specified node is null or is still in the AST. |
| 152 | */ |
| 153 | @VisibleForTesting |
| 154 | static Node validateResult(Node n) { |
| 155 | done: { |
| 156 | if (n != null && !n.isScript() |
| 157 | && (!n.isBlock() || !n.isSyntheticBlock())) { |
| 158 | for (Node parent : n.getAncestors()) { |
| 159 | if (parent.isScript()) { |
| 160 | break done; |
| 161 | } |
| 162 | } |
| 163 | Preconditions.checkState(false); |
| 164 | } |
| 165 | } |
| 166 | return n; |
| 167 | } |
| 168 | } |