Debian Codegraph @

src/com/google/javascript/jscomp/CreateSyntheticBlocks.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.collect.Lists;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.IR;
22 import com.google.javascript.rhino.Node;
23 import java.util.ArrayDeque;
24 import java.util.Deque;
25 import java.util.List;
26
27 import javax.annotation.Nullable;
28
29 /**
30 * Creates synthetic blocks to optimizations from moving code
31 * past markers in the source.
32 *
33 */
34 class CreateSyntheticBlocks implements CompilerPass {
35 static final DiagnosticType UNMATCHED_START_MARKER = DiagnosticType.warning(
36 "JSC_UNMATCHED_START_MARKER", "Unmatched {0}");
37
38 static final DiagnosticType UNMATCHED_END_MARKER = DiagnosticType.warning(
39 "JSC_UNMATCHED_END_MARKER", "Unmatched {1} - {0} not in the same block");
40
41 static final DiagnosticType INVALID_MARKER_USAGE = DiagnosticType.warning(
42 "JSC_INVALID_MARKER_USAGE", "Marker {0} can only be used in a simple "
43 + "call expression");
44
45 private final AbstractCompiler compiler;
46
47 /** Name of the start marker. */
48 private final String startMarkerName;
49
50 /** Name of the end marker. */
51 private final String endMarkerName;
52
53 /**
54 * Markers can be nested.
55 */
56 private final Deque<Node> markerStack = new ArrayDeque<Node>();
57
58 private final List<Marker> validMarkers = Lists.newArrayList();
59
60 private class Marker {
61 final Node startMarker;
62 final Node endMarker;
63 public Marker(Node startMarker, Node endMarker) {
64 this.startMarker = startMarker;
65 this.endMarker = endMarker;
66 }
67 }
68
69 public CreateSyntheticBlocks(AbstractCompiler compiler,
70 String startMarkerName, String endMarkerName) {
71 this.compiler = compiler;
72 this.startMarkerName = startMarkerName;
73 this.endMarkerName = endMarkerName;
74 }
75
76 @Override
77 public void process(Node externs, Node root) {
78 // Find and validate the markers.
79 NodeTraversal.traverse(compiler, root, new Callback());
80
81 // Complain about any unmatched markers.
82 for (Node node : markerStack) {
83 compiler.report(
84 JSError.make(NodeUtil.getSourceName(node),
85 node,
86 UNMATCHED_START_MARKER, startMarkerName));
87 }
88
89 // Add the block for the valid marker sets.
90 for (Marker marker : validMarkers) {
91 addBlocks(marker);
92 }
93 }
94
95 /**
96 * @param marker The marker to add synthetic blocks for.
97 */
98 private void addBlocks(Marker marker) {
99 // Add block around the template section so that it looks like this:
100 // BLOCK (synthetic)
101 // START
102 // BLOCK (synthetic)
103 // BODY
104 // END
105 // This prevents the start or end markers from mingling with the code
106 // in the block body.
107
108
109 Node originalParent = marker.endMarker.getParent();
110 Node outerBlock = IR.block();
111 outerBlock.setIsSyntheticBlock(true);
112 originalParent.addChildBefore(outerBlock, marker.startMarker);
113
114 Node innerBlock = IR.block();
115 innerBlock.setIsSyntheticBlock(true);
116 // Move everything after the start Node up to the end Node into the inner
117 // block.
118 moveSiblingExclusive(originalParent, innerBlock,
119 marker.startMarker,
120 marker.endMarker);
121
122 // Add the start node.
123 outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));
124 // Add the inner block
125 outerBlock.addChildToBack(innerBlock);
126 // and finally the end node.
127 outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));
128
129 compiler.reportCodeChange();
130 }
131
132 /**
133 * Move the Nodes between start and end from the source block to the
134 * destination block. If start is null, move the first child of the block.
135 * If end is null, move the last child of the block.
136 */
137 private void moveSiblingExclusive(
138 Node src, Node dest, @Nullable Node start, @Nullable Node end) {
139 while (childAfter(src, start) != end) {
140 Node child = removeChildAfter(src, start);
141 dest.addChildToBack(child);
142 }
143 }
144
145 /**
146 * Like Node.getNext, that null is used to signal the child before the
147 * block.
148 */
149 private Node childAfter(Node parent, @Nullable Node siblingBefore) {
150 if (siblingBefore == null) {
151 return parent.getFirstChild();
152 } else {
153 return siblingBefore.getNext();
154 }
155 }
156
157 /**
158 * Like removeChildAfter, the firstChild is removed
159 */
160 private Node removeChildAfter(Node parent, @Nullable Node siblingBefore) {
161 if (siblingBefore == null) {
162 return parent.removeFirstChild();
163 } else {
164 return parent.removeChildAfter(siblingBefore);
165 }
166 }
167
168 private class Callback extends AbstractPostOrderCallback {
169 @Override
170 public void visit(NodeTraversal t, Node n, Node parent) {
171 if (!n.isCall() || !n.getFirstChild().isName()) {
172 return;
173 }
174
175 Node callTarget = n.getFirstChild();
176 String callName = callTarget.getString();
177
178 if (startMarkerName.equals(callName)) {
179 if (!parent.isExprResult()) {
180 compiler.report(
181 t.makeError(n, INVALID_MARKER_USAGE, startMarkerName));
182 return;
183 }
184 markerStack.push(parent);
185 return;
186 }
187
188 if (!endMarkerName.equals(callName)) {
189 return;
190 }
191
192 Node endMarkerNode = parent;
193 if (!endMarkerNode.isExprResult()) {
194 compiler.report(
195 t.makeError(n, INVALID_MARKER_USAGE, endMarkerName));
196 return;
197 }
198
199 if (markerStack.isEmpty()) {
200 compiler.report(t.makeError(n, UNMATCHED_END_MARKER,
201 startMarkerName, endMarkerName));
202 return;
203 }
204
205 Node startMarkerNode = markerStack.pop();
206 if (endMarkerNode.getParent() != startMarkerNode.getParent()) {
207 // The end marker isn't in the same block as the start marker.
208 compiler.report(t.makeError(n, UNMATCHED_END_MARKER,
209 startMarkerName, endMarkerName));
210 return;
211 }
212
213 // This is a valid marker set add it to the list of markers to process.
214 validMarkers.add(new Marker(startMarkerNode, endMarkerNode));
215 }
216 }
217 }