Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.collect.Lists;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.CodingConvention.AssertionFunctionSpec;
22 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
23 import com.google.javascript.jscomp.NodeTraversal.Callback;
24 import com.google.javascript.rhino.Node;
25 import java.util.List;
26 import java.util.Set;
27
28 /**
29 * <p>Compiler pass that removes Closure-specific code patterns.</p>
30 *
31 * <p>Currently does the following:</p>
32 *
33 * <ul>
34 * <li> Instead of setting abstract methods to a function that throws an
35 * informative error, this pass allows some binary size reduction by
36 * removing these methods altogether for production builds.</li>
37 * <li> Remove calls to assertion functions (like goog.asserts.assert).
38 * If the return value of the assertion function is used, then
39 * the first argument (the asserted value) will be directly inlined.
40 * Otherwise, the entire call will be removed. It is well-known that
41 * this is not provably safe, much like the equivalent assert
42 * statement in Java.</li>
43 * </ul>
44 *
45 * @author [email protected] (Robby Walker)
46 */
47 final class ClosureCodeRemoval implements CompilerPass {
48
49 /** Reference to the JS compiler */
50 private final AbstractCompiler compiler;
51
52 /** Name used to denote an abstract function */
53 static final String ABSTRACT_METHOD_NAME = "goog.abstractMethod";
54
55 private final boolean removeAbstractMethods;
56 private final boolean removeAssertionCalls;
57
58 /**
59 * List of names referenced in successive generations of finding referenced
60 * nodes.
61 */
62 private final List<RemovableAssignment> abstractMethodAssignmentNodes =
63 Lists.newArrayList();
64
65 /**
66 * List of assertion functions.
67 */
68 private final List<Node> assertionCalls = Lists.newArrayList();
69
70
71 /**
72 * Utility class to track a node and its parent.
73 */
74 private class RemovableAssignment {
75 /**
76 * The node
77 */
78 final Node node;
79
80 /**
81 * Its parent
82 */
83 final Node parent;
84
85 /**
86 * Full chain of ASSIGN ancestors
87 */
88 final List<Node> assignAncestors = Lists.newArrayList();
89
90 /**
91 * The last ancestor
92 */
93 final Node lastAncestor;
94
95 /**
96 * Data structure for information about a removable assignment.
97 *
98 * @param nameNode The LHS
99 * @param assignNode The parent ASSIGN node
100 * @param traversal Access to further levels, assumed to start at 1
101 */
102 public RemovableAssignment(Node nameNode, Node assignNode,
103 NodeTraversal traversal) {
104 this.node = nameNode;
105 this.parent = assignNode;
106
107 Node ancestor = assignNode;
108 do {
109 ancestor = ancestor.getParent();
110 assignAncestors.add(ancestor);
111 } while (ancestor.isAssign() &&
112 ancestor.getFirstChild().isQualifiedName());
113 lastAncestor = ancestor.getParent();
114 }
115
116 /**
117 * Remove this node.
118 */
119 public void remove() {
120 Node rhs = node.getNext();
121 Node last = parent;
122 for (Node ancestor : assignAncestors) {
123 if (ancestor.isExprResult()) {
124 lastAncestor.removeChild(ancestor);
125 } else {
126 rhs.detachFromParent();
127 ancestor.replaceChild(last, rhs);
128 }
129 last = ancestor;
130 }
131 compiler.reportCodeChange();
132 }
133 }
134
135 /**
136 * Identifies all assignments of the abstract method to a variable.
137 */
138 private class FindAbstractMethods extends AbstractPostOrderCallback {
139 @Override
140 public void visit(NodeTraversal t, Node n, Node parent) {
141 if (n.isAssign()) {
142 Node nameNode = n.getFirstChild();
143 Node valueNode = n.getLastChild();
144
145 if (nameNode.isQualifiedName() &&
146 valueNode.isQualifiedName() &&
147 ABSTRACT_METHOD_NAME.equals(valueNode.getQualifiedName())) {
148 abstractMethodAssignmentNodes.add(new RemovableAssignment(
149 n.getFirstChild(), n, t));
150 }
151 }
152 }
153 }
154
155
156 /**
157 * Identifies all assertion calls.
158 */
159 private class FindAssertionCalls extends AbstractPostOrderCallback {
160 Set<String> assertionNames = Sets.newHashSet();
161
162 FindAssertionCalls() {
163 for (AssertionFunctionSpec spec :
164 compiler.getCodingConvention().getAssertionFunctions()) {
165 assertionNames.add(spec.getFunctionName());
166 }
167 }
168
169
170 @Override
171 public void visit(NodeTraversal t, Node n, Node parent) {
172 if (n.isCall()) {
173 String fnName = n.getFirstChild().getQualifiedName();
174 if (assertionNames.contains(fnName)) {
175 assertionCalls.add(n);
176 }
177 }
178 }
179 }
180
181
182 /**
183 * Creates a Closure code remover.
184 *
185 * @param compiler The AbstractCompiler
186 * @param removeAbstractMethods Remove declarations of abstract methods.
187 * @param removeAssertionCalls Remove calls to goog.assert functions.
188 */
189 ClosureCodeRemoval(AbstractCompiler compiler, boolean removeAbstractMethods,
190 boolean removeAssertionCalls) {
191 this.compiler = compiler;
192 this.removeAbstractMethods = removeAbstractMethods;
193 this.removeAssertionCalls = removeAssertionCalls;
194 }
195
196 @Override
197 public void process(Node externs, Node root) {
198 List<Callback> passes = Lists.newArrayList();
199 if (removeAbstractMethods) {
200 passes.add(new FindAbstractMethods());
201 }
202 if (removeAssertionCalls) {
203 passes.add(new FindAssertionCalls());
204 }
205 CombinedCompilerPass.traverse(compiler, root, passes);
206
207 for (RemovableAssignment assignment : abstractMethodAssignmentNodes) {
208 assignment.remove();
209 }
210
211 for (Node call : assertionCalls) {
212 // If the assertion is an expression, just strip the whole thing.
213 Node parent = call.getParent();
214 if (parent.isExprResult()) {
215 parent.getParent().removeChild(parent);
216 } else {
217 // Otherwise, replace the assertion with its first argument,
218 // which is the return value of the assertion.
219 Node firstArg = call.getFirstChild().getNext();
220 if (firstArg == null) {
221 parent.replaceChild(call, NodeUtil.newUndefinedNode(call));
222 } else {
223 parent.replaceChild(call, firstArg.detachFromParent());
224 }
225 }
226 compiler.reportCodeChange();
227 }
228 }
229 }