-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Denormalize.java
205 lines (183 loc) · 6.92 KB
/
Denormalize.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 2009 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkState;
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
/**
* The goal with this pass is to reverse the simplifications done in the normalization pass that are
* not handled by other passes (such as CollapseVariableDeclarations) to avoid making the resulting
* code larger.
*
* <p>Currently this pass only does a few things:
*
* <p>1. Push statements into for-loop initializer. This: var a = 0; for(;a<0;a++) {} becomes:
* for(var a = 0;a<0;a++) {}
*
* <p>2. Fold assignments like x = x + 1 into x += 1
*
* <p>3. Inline 'var' keyword. For instance: <code>
* var x;
* if (y) { x = 0; }
* </code> becomes <code>if (y) { var x = 0; }</code>, effectively undoing what {@link
* HoistVarsOutOfBlocks} does.
*
* @author johnlenz@google.com (johnlenz)
*/
class Denormalize implements CompilerPass, Callback, Behavior {
private final AbstractCompiler compiler;
Denormalize(AbstractCompiler compiler) {
this.compiler = compiler;
}
@Override
public void process(Node externs, Node root) {
NodeTraversal.traverse(compiler, root, this);
// Don't inline the VAR declaration if this compilation involves old-style ctemplates.
if (compiler.getOptions().syntheticBlockStartMarker == null) {
(new ReferenceCollectingCallback(compiler, this, new SyntacticScopeCreator(compiler)))
.process(root);
}
}
@Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
return true;
}
/**
* Implements step 3 (inlining the var keyword).
*/
@Override
public void afterExitScope(NodeTraversal t, ReferenceMap referenceMap) {
Node scopeRoot = t.getScopeRoot();
if (scopeRoot.isBlock() && scopeRoot.getParent().isFunction()) {
boolean changed = false;
for (Var v : t.getScope().getVarIterable()) {
ReferenceCollection references = referenceMap.getReferences(v);
Reference declaration = null;
Reference assign = null;
for (Reference r : references) {
if (r.isVarDeclaration()
&& NodeUtil.isStatement(r.getNode().getParent())
&& !r.isInitializingDeclaration()) {
declaration = r;
} else if (assign == null
&& r.isSimpleAssignmentToName()
&& r.getScope().getClosestHoistScope().equals(t.getScope())) {
assign = r;
}
}
if (declaration != null && assign != null) {
Node lhs = assign.getNode();
Node assignNode = lhs.getParent();
if (assignNode.getParent().isExprResult()) {
Node rhs = lhs.getNext();
assignNode
.getGrandparent()
.replaceChild(assignNode.getParent(), IR.var(lhs.detach(), rhs.detach()));
Node var = declaration.getNode().getParent();
checkState(var.isVar(), var);
NodeUtil.removeChild(var, declaration.getNode());
changed = true;
}
}
}
if (changed) {
t.reportCodeChange();
}
}
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
maybeCollapseIntoForStatements(n, parent);
maybeCollapseAssignShorthand(n, parent);
}
/**
* Collapse VARs and EXPR_RESULT node into FOR loop initializers where
* possible.
*/
private void maybeCollapseIntoForStatements(Node n, Node parent) {
// Only SCRIPT, BLOCK, and LABELs can have FORs that can be collapsed into.
// LABELs are not supported here.
if (parent == null || !NodeUtil.isStatementBlock(parent)) {
return;
}
// Is the current node something that can be in a for loop initializer?
if (!n.isExprResult() && !n.isVar()) {
return;
}
// Is the next statement a valid FOR?
Node nextSibling = n.getNext();
if (nextSibling == null) {
return;
} else if (nextSibling.isForIn() || nextSibling.isForOf()) {
Node forNode = nextSibling;
Node forVar = forNode.getFirstChild();
if (forVar.isName()
&& n.isVar() && n.hasOneChild()) {
Node name = n.getFirstChild();
if (!name.hasChildren()
&& forVar.getString().equals(name.getString())) {
// OK, the names match, and the var declaration does not have an
// initializer. Move it into the loop.
parent.removeChild(n);
forNode.replaceChild(forVar, n);
compiler.reportChangeToEnclosingScope(parent);
}
}
} else if (nextSibling.isVanillaFor() && nextSibling.getFirstChild().isEmpty()) {
// Does the current node contain an in operator? If so, embedding
// the expression in a for loop can cause some JavaScript parsers (such
// as the PlayStation 3's browser based on Access's NetFront
// browser) to fail to parse the code.
// See bug 1778863 for details.
if (NodeUtil.containsType(n, Token.IN)) {
return;
}
// Move the current node into the FOR loop initializer.
Node forNode = nextSibling;
Node oldInitializer = forNode.getFirstChild();
parent.removeChild(n);
Node newInitializer;
if (n.isVar()) {
newInitializer = n;
} else {
// Extract the expression from EXPR_RESULT node.
checkState(n.hasOneChild(), n);
newInitializer = n.getFirstChild();
n.removeChild(newInitializer);
}
forNode.replaceChild(oldInitializer, newInitializer);
compiler.reportChangeToEnclosingScope(forNode);
}
}
private void maybeCollapseAssignShorthand(Node n, Node parent) {
if (n.isAssign() && n.getFirstChild().isName()
&& NodeUtil.hasCorrespondingAssignmentOp(n.getLastChild())
&& n.getLastChild().getFirstChild().isName()) {
Node op = n.getLastChild();
Token assignOp = NodeUtil.getAssignOpFromOp(op);
if (n.getFirstChild().getString().equals(op.getFirstChild().getString())) {
op.setToken(assignOp);
Node opDetached = op.detach();
opDetached.setJSDocInfo(n.getJSDocInfo());
parent.replaceChild(n, opDetached);
compiler.reportChangeToEnclosingScope(parent);
}
}
}
}