Debian Codegraph @

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

raw symbols SCIP
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.Predicate;
20 import com.google.common.base.Supplier;
21 import com.google.common.collect.Lists;
22 import com.google.javascript.rhino.IR;
23 import com.google.javascript.rhino.Node;
24 import java.util.List;
25
26 /**
27 * Breaks up the AST at different levels for parallel analysis and
28 * optimizations. In all cases, the subtrees are detached from the original
29 * source tree and are replaced by place-holders for the reverse process.
30 * Although this class breaks the AST into independent subtrees and make tree
31 * transformation safe, it is still up to individual passes to preserve proper
32 * semantics when analyzing the subtrees.
33 *
34 */
35 class AstParallelizer {
36
37 public static final String TEMP_NAME = "JSC_TMP_PLACE_HOLDER";
38
39 private final Predicate<Node> shouldSplit;
40
41 private final Supplier<Node> placeHolderProvider;
42
43 private final List<Node> forest;
44
45 private final Node root;
46
47 private final boolean includeRoot;
48
49 // Maps to place holder to the original function.
50 private final List<DetachPoint> detachPointList;
51
52 /**
53 * Constructor.
54 *
55 * @param shouldSplit Specify at which node it should split the subtree.
56 * @param shouldTraverse Specify when to stop looking for subtree to split.
57 * This is <b>very</b> important for performance as we do not want to
58 * traverse too much just looking for subtree.
59 * @param placeHolderProvider Specify what type of node should be place as
60 * a temporary place holder for where the subtree is detached.
61 * @param root The AST itself.
62 * @param includeRoot Should we include the root inside the forest returned
63 * by {{@link #split()}.
64 */
65 public AstParallelizer(
66 Predicate<Node> shouldSplit,
67 Predicate<Node> shouldTraverse,
68 Supplier<Node> placeHolderProvider,
69 Node root,
70 boolean includeRoot) {
71 this.shouldSplit = shouldSplit;
72 this.placeHolderProvider = placeHolderProvider;
73 this.root = root;
74 this.includeRoot = includeRoot;
75 this.forest = Lists.newLinkedList();
76 this.detachPointList = Lists.newLinkedList();
77 }
78
79 public static AstParallelizer createNewFunctionLevelAstParallelizer(
80 Node root, boolean globalPass) {
81
82 // Split at function level.
83 Predicate<Node> shouldSplit = new Predicate<Node>() {
84 @Override
85 public boolean apply(Node input) {
86 return input.isFunction();
87 }
88 };
89
90 // Always traverse until it finds a split point.
91 Predicate<Node> shouldTraverse = new Predicate<Node>() {
92 @Override
93 public boolean apply(Node ignored) {
94 return true;
95 }
96 };
97
98 // Use a function declaration of the same name.
99 Supplier<Node> placeHolders = new Supplier<Node>() {
100 @Override
101 public Node get() {
102 return IR.function(IR.name(TEMP_NAME), IR.paramList(), IR.block());
103 }
104 };
105 return new AstParallelizer(
106 shouldSplit, shouldTraverse, placeHolders, root, globalPass);
107 }
108
109 public static AstParallelizer createNewFileLevelAstParallelizer(Node root) {
110
111 // Split at every node that has a file name prop.
112 Predicate<Node> shouldSplit = new Predicate<Node>() {
113 @Override
114 public boolean apply(Node input) {
115 return input.getSourceFileName() != null;
116 }
117 };
118
119 // Use a string as place holder.
120 Supplier<Node> placeHolders = new Supplier<Node>() {
121 @Override
122 public Node get() {
123 return NodeUtil.newExpr(IR.string(TEMP_NAME));
124 }
125 };
126
127 // Only traverse blocks.
128 Predicate<Node> shouldTraverse = new Predicate<Node>() {
129 @Override
130 public boolean apply(Node n) {
131 return n.isBlock();
132 }
133 };
134 return new AstParallelizer(
135 shouldSplit, shouldTraverse, placeHolders, root, false);
136 }
137
138 /**
139 * Remembers the split point for use in {@link #join()}.
140 */
141 private void recordSplitPoint(Node placeHolder, Node before, Node original) {
142 detachPointList.add(new DetachPoint(placeHolder, before, original));
143 }
144
145 /**
146 * Splits the AST into subtree at different levels. The subtrees itself are
147 * usually not valid JavaScript but they are all subtrees of some valid
148 * JavaScript.
149 */
150 public List<Node> split() {
151 if (includeRoot) {
152 forest.add(root);
153 }
154 split(root);
155 return forest;
156 }
157
158 private void split(Node n) {
159 Node c = n.getFirstChild();
160 Node before = null;
161 while (c != null) {
162 Node next = c.getNext();
163 if (shouldSplit.apply(c)) {
164 Node placeHolder = placeHolderProvider.get();
165 if (before == null) {
166 forest.add(n.removeFirstChild());
167 n.addChildToFront(placeHolder);
168 } else {
169 n.addChildAfter(placeHolder, c);
170 n.removeChildAfter(before);
171 forest.add(c);
172 }
173 recordSplitPoint(placeHolder, before, c);
174 before = placeHolder;
175 } else {
176 split(c);
177 before = c;
178 }
179 c = next;
180 }
181 }
182
183 /**
184 * Reverse the splitting done by {@link #split()}.
185 */
186 public void join() {
187 // Revert in a reverse order to undo the detachment.
188 while (!detachPointList.isEmpty()) {
189 DetachPoint entry = detachPointList.remove(detachPointList.size() - 1);
190 entry.reattach();
191 }
192 }
193
194 /**
195 * A class to map the place holder to the original subtree for re-attachment.
196 * Normally a Map from Node -> Node is sufficient, however, if we also
197 * remember the node before the place holder, we can avoid using
198 * {@link Node#replaceChild(Node, Node)} which requires a linear search of
199 * the before node. Maybe someday we should get a prev pointer for this
200 * purpose.
201 */
202 private static class DetachPoint {
203
204 // The place holder to remember where the original node was.
205 private Node placeHolder;
206
207 // The node before the place holder and the original, null if
208 private Node before;
209
210 // The root of the subtree to be temporary detached.
211 private Node original;
212
213 private DetachPoint(Node placeHolder, Node before, Node original) {
214 this.placeHolder = placeHolder;
215 this.before = before;
216 this.original = original;
217 }
218
219 public void reattach() {
220 // If the place-holder no longer has a parent, this implies the function
221 // has been removed from the AST.
222 if (placeHolder.getParent() != null) {
223 if (before == null) {
224 placeHolder.getParent().addChildrenToFront(original);
225 placeHolder.getParent().removeChildAfter(original);
226 } else {
227 placeHolder.getParent().addChildAfter(original, before);
228 placeHolder.getParent().removeChildAfter(original);
229 }
230 }
231 }
232 }
233 }