Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.javascript.jscomp.NodeTraversal.Callback;
21 import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
22 import com.google.javascript.rhino.Node;
23
24 import java.util.List;
25
26 /**
27 * <p>A compiler pass combining multiple {@link Callback}
28 * and {@link ScopedCallback} objects. This pass can be used to separate
29 * logically different verifications without incurring any additional traversal
30 * and CFG generation costs.</p>
31 *
32 * <p>Due to this compiler pass' nature, none of the callbacks may mutate
33 * the parse tree.</p>
34 *
35 * <p>TODO(user):
36 * This combined pass is currently limited in the type of callbacks it can
37 * combine due to the difficulty of handling NodeTraversal's methods that
38 * initiate more recursion (e.g., {@link NodeTraversal#traverse(Node)} and
39 * {@link NodeTraversal#traverseInnerNode(Node, Node, Scope)}). The
40 * {@link NodeTraversal} object passed to the individual callbacks should
41 * be instrumented to emulate the correct behavior. For instance,
42 * one could create a {@link NodeTraversal} whose
43 * {@link NodeTraversal#traverseInnerNode(Node, Node, Scope)} ties
44 * back into this compiler pass to give it context about what combined
45 * passes are doing.</p>
46 *
47 */
48 final class CombinedCompilerPass implements HotSwapCompilerPass,
49 ScopedCallback {
50
51 /** The callbacks that this pass combines. */
52 private final CallbackWrapper[] callbacks;
53 private final AbstractCompiler compiler;
54
55 /**
56 * Creates a combined compiler pass.
57 * @param compiler the compiler
58 */
59 CombinedCompilerPass(
60 AbstractCompiler compiler, Callback... callbacks) {
61 this(compiler, Lists.<Callback>newArrayList(callbacks));
62 }
63
64 CombinedCompilerPass(
65 AbstractCompiler compiler, List<Callback> callbacks) {
66 this.compiler = compiler;
67 this.callbacks = new CallbackWrapper[callbacks.size()];
68 for (int i = 0; i < callbacks.size(); i++) {
69 this.callbacks[i] = new CallbackWrapper(callbacks.get(i));
70 }
71 }
72
73 static void traverse(AbstractCompiler compiler, Node root,
74 List<Callback> callbacks) {
75 if (callbacks.size() == 1) {
76 NodeTraversal.traverse(compiler, root, callbacks.get(0));
77 } else {
78 (new CombinedCompilerPass(compiler, callbacks)).process(null, root);
79 }
80 }
81
82 /**
83 * Maintains information about a callback in order to simulate it being the
84 * exclusive client of the shared {@link NodeTraversal}. In particular, this
85 * class simulates abbreviating the traversal when the wrapped callback
86 * returns false for
87 * {@link Callback#shouldTraverse(NodeTraversal, Node, Node)}.
88 * The callback becomes inactive (i.e., traversal messages are not sent to it)
89 * until the main traversal revisits the node during the post-order visit.
90 *
91 */
92 private static class CallbackWrapper {
93 /** The callback being wrapped. Never null. */
94 private final Callback callback;
95 /**
96 * if (callback instanceof ScopedCallback), then scopedCallback points
97 * to an instance of ScopedCallback, otherwise scopedCallback points to null
98 */
99 private final ScopedCallback scopedCallback;
100
101 /**
102 * The node that {@link Callback#shouldTraverse(NodeTraversal, Node, Node)}
103 * returned false for. The wrapped callback doesn't receive messages until
104 * after this node is revisited in the post-order traversal.
105 */
106 private Node waiting = null;
107
108 private CallbackWrapper(Callback callback) {
109 this.callback = callback;
110 if (callback instanceof ScopedCallback) {
111 scopedCallback = (ScopedCallback) callback;
112 } else {
113 scopedCallback = null;
114 }
115 }
116
117 /**
118 * Visits the node unless the wrapped callback is inactive. Activates the
119 * callback if appropriate.
120 */
121 void visitOrMaybeActivate(NodeTraversal t, Node n, Node parent) {
122 if (isActive()) {
123 callback.visit(t, n, parent);
124 } else if (waiting == n) {
125 waiting = null;
126 }
127 }
128
129 void shouldTraverseIfActive(NodeTraversal t, Node n, Node parent) {
130 if (isActive() && !callback.shouldTraverse(t, n, parent)) {
131 waiting = n;
132 }
133 }
134
135 void enterScopeIfActive(NodeTraversal t) {
136 if (isActive() && scopedCallback != null) {
137 scopedCallback.enterScope(t);
138 }
139 }
140
141 void exitScopeIfActive(NodeTraversal t) {
142 if (isActive() && scopedCallback != null) {
143 scopedCallback.exitScope(t);
144 }
145 }
146
147 boolean isActive() {
148 return waiting == null;
149 }
150 }
151
152 @Override
153 public final void process(Node externs, Node root) {
154 NodeTraversal.traverse(compiler, root, this);
155 }
156
157 @Override
158 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
159 NodeTraversal.traverse(compiler, scriptRoot, this);
160 }
161
162 @Override
163 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
164 for (CallbackWrapper callback : callbacks) {
165 callback.shouldTraverseIfActive(t, n, parent);
166 }
167 // Note that this method could return false if all callbacks are inactive.
168 // This apparent optimization would make this method more expensive
169 // in the typical case where not all nodes are inactive. It is
170 // very unlikely that many all callbacks would be inactive at the same
171 // time (indeed, there are several checking passes that never return false).
172 return true;
173 }
174
175 @Override
176 public void visit(NodeTraversal t, Node n, Node parent) {
177 for (CallbackWrapper callback : callbacks) {
178 callback.visitOrMaybeActivate(t, n, parent);
179 }
180 }
181
182 @Override
183 public void enterScope(NodeTraversal t) {
184 for (CallbackWrapper callback : callbacks) {
185 callback.enterScopeIfActive(t);
186 }
187 }
188
189 @Override
190 public void exitScope(NodeTraversal t) {
191 for (CallbackWrapper callback : callbacks) {
192 callback.exitScopeIfActive(t);
193 }
194 }
195 }