Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 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 package com.google.javascript.jscomp;
17
18 import com.google.common.base.Preconditions;
19 import com.google.javascript.rhino.Node;
20 import com.google.javascript.rhino.Token;
21
22 /**
23 * Tries to chain assignments together.
24 *
25 * @author [email protected] (Nick Santos)
26 * @author [email protected] (Alan Leung)
27 *
28 */
29 class ExploitAssigns extends AbstractPeepholeOptimization {
30
31 @Override
32 Node optimizeSubtree(Node subtree) {
33 for (Node child = subtree.getFirstChild(); child != null;) {
34 Node next = child.getNext();
35 if (NodeUtil.isExprAssign(child)) {
36 collapseAssign(child.getFirstChild(), child, subtree);
37 }
38 child = next;
39 }
40 return subtree;
41 }
42
43 /**
44 * Try to collapse the given assign into subsequent expressions.
45 */
46 private void collapseAssign(Node assign, Node expr,
47 Node exprParent) {
48 Node leftValue = assign.getFirstChild();
49 Node rightValue = leftValue.getNext();
50 if (isCollapsibleValue(leftValue, true) &&
51 collapseAssignEqualTo(expr, exprParent, leftValue)) {
52 reportCodeChange();
53 } else if (isCollapsibleValue(rightValue, false) &&
54 collapseAssignEqualTo(expr, exprParent, rightValue)) {
55 reportCodeChange();
56 } else if (rightValue.isAssign()) {
57 // Recursively deal with nested assigns.
58 collapseAssign(rightValue, expr, exprParent);
59 }
60 }
61
62 /**
63 * Determines whether we know enough about the given value to be able
64 * to collapse it into subsequent expressions.
65 *
66 * For example, we can collapse booleans and variable names:
67 * <code>
68 * x = 3; y = x; // y = x = 3;
69 * a = true; b = true; // b = a = true;
70 * <code>
71 * But we won't try to collapse complex expressions.
72 *
73 * @param value The value node.
74 * @param isLValue Whether it's on the left-hand side of an expr.
75 */
76 private boolean isCollapsibleValue(Node value, boolean isLValue) {
77 switch (value.getType()) {
78 case Token.GETPROP:
79 // Do not collapse GETPROPs on arbitrary objects, because
80 // they may be implemented setter functions, and oftentimes
81 // setter functions fail on native objects. This is OK for "THIS"
82 // objects, because we assume that they are non-native.
83 return !isLValue || value.getFirstChild().isThis();
84
85 case Token.NAME:
86 return true;
87
88 default:
89 return NodeUtil.isImmutableValue(value);
90 }
91 }
92
93 /**
94 * Collapse the given assign expression into the expression directly
95 * following it, if possible.
96 *
97 * @param expr The expression that may be moved.
98 * @param exprParent The parent of {@code expr}.
99 * @param value The value of this expression, expressed as a node. Each
100 * expression may have multiple values, so this function may be called
101 * multiple times for the same expression. For example,
102 * <code>
103 * a = true;
104 * </code>
105 * is equal to the name "a" and the boolean "true".
106 * @return Whether the expression was collapsed successfully.
107 */
108 private boolean collapseAssignEqualTo(Node expr, Node exprParent,
109 Node value) {
110 Node assign = expr.getFirstChild();
111 Node parent = exprParent;
112 Node next = expr.getNext();
113 while (next != null) {
114 switch (next.getType()) {
115 case Token.AND:
116 case Token.OR:
117 case Token.HOOK:
118 case Token.IF:
119 case Token.RETURN:
120 case Token.EXPR_RESULT:
121 // Dive down the left side
122 parent = next;
123 next = next.getFirstChild();
124 break;
125
126 case Token.VAR:
127 if (next.getFirstChild().hasChildren()) {
128 parent = next.getFirstChild();
129 next = parent.getFirstChild();
130 break;
131 }
132 return false;
133
134 case Token.GETPROP:
135 case Token.NAME:
136 if (next.isQualifiedName()) {
137 String nextName = next.getQualifiedName();
138 if (value.isQualifiedName() &&
139 nextName.equals(value.getQualifiedName())) {
140 // If the previous expression evaluates to value of a
141 // qualified name, and that qualified name is used again
142 // shortly, then we can exploit the assign here.
143
144 // Verify the assignment doesn't change its own value.
145 if (!isSafeReplacement(next, assign)) {
146 return false;
147 }
148
149 exprParent.removeChild(expr);
150 expr.removeChild(assign);
151 parent.replaceChild(next, assign);
152 return true;
153 }
154 }
155 return false;
156
157 case Token.ASSIGN:
158 // Assigns are really tricky. In lots of cases, we want to inline
159 // into the right side of the assign. But the left side of the
160 // assign is evaluated first, and it may have convoluted logic:
161 // a = null;
162 // (a = b).c = null;
163 // We don't want to exploit the first assign. Similarly:
164 // a.b = null;
165 // a.b.c = null;
166 // We don't want to exploit the first assign either.
167 //
168 // To protect against this, we simply only inline when the left side
169 // is guaranteed to evaluate to the same L-value no matter what.
170 Node leftSide = next.getFirstChild();
171 if (leftSide.isName() ||
172 leftSide.isGetProp() &&
173 leftSide.getFirstChild().isThis()) {
174 // Dive down the right side of the assign.
175 parent = next;
176 next = leftSide.getNext();
177 break;
178 } else {
179 return false;
180 }
181
182 default:
183 if (NodeUtil.isImmutableValue(next)
184 && next.isEquivalentTo(value)) {
185 // If the r-value of the expr assign is an immutable value,
186 // and the value is used again shortly, then we can exploit
187 // the assign here.
188 exprParent.removeChild(expr);
189 expr.removeChild(assign);
190 parent.replaceChild(next, assign);
191 return true;
192 }
193 // Return without inlining a thing
194 return false;
195 }
196 }
197
198 return false;
199 }
200
201 /**
202 * Checks name referenced in node to determine if it might have
203 * changed.
204 * @return Whether the replacement can be made.
205 */
206 private boolean isSafeReplacement(Node node, Node replacement) {
207 // No checks are needed for simple names.
208 if (node.isName()) {
209 return true;
210 }
211 Preconditions.checkArgument(node.isGetProp());
212
213 Node name = node.getFirstChild();
214 if (name.isName()
215 && isNameAssignedTo(name.getString(), replacement)) {
216 return false;
217 }
218
219 return true;
220 }
221
222 /**
223 * @return Whether name is assigned in the expression rooted at node.
224 */
225
226 private boolean isNameAssignedTo(String name, Node node) {
227 for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
228 if (isNameAssignedTo(name, c)) {
229 return true;
230 }
231 }
232
233 if (node.isName()) {
234 Node parent = node.getParent();
235 if (parent.isAssign() && parent.getFirstChild() == node) {
236 if (name.equals(node.getString())) {
237 return true;
238 }
239 }
240 }
241
242 return false;
243 }
244 }