-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
InlineObjectLiterals.java
468 lines (412 loc) · 16.4 KB
/
InlineObjectLiterals.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright 2011 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.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.javascript.jscomp.ReferenceCollectingCallback.Behavior;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceMap;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Using the infrastructure provided by {@link ReferenceCollectingCallback},
* identify variables that are only ever assigned to object literals
* and that are never used in entirety, and expand the objects into
* individual variables.
*
* Based on the InlineVariables pass
*
*/
class InlineObjectLiterals implements CompilerPass {
public static final String VAR_PREFIX = "JSCompiler_object_inline_";
private final AbstractCompiler compiler;
private final Supplier<String> safeNameIdSupplier;
InlineObjectLiterals(
AbstractCompiler compiler,
Supplier<String> safeNameIdSupplier) {
this.compiler = compiler;
this.safeNameIdSupplier = safeNameIdSupplier;
}
@Override
public void process(Node externs, Node root) {
ReferenceCollectingCallback callback = new ReferenceCollectingCallback(
compiler, new InliningBehavior(), SyntacticScopeCreator.makeUntyped(compiler));
callback.process(externs, root);
}
/**
* Builds up information about nodes in each scope. When exiting the
* scope, inspects all variables in that scope, and inlines any
* that we can.
*/
private class InliningBehavior implements Behavior {
/**
* A list of variables that should not be inlined, because their
* reference information is out of sync with the state of the AST.
*/
private final Set<Var> staleVars = new HashSet<>();
@Override
public void afterExitScope(NodeTraversal t, ReferenceMap referenceMap) {
for (Var v : t.getScope().getVarIterable()) {
if (isVarInlineForbidden(v)) {
continue;
}
ReferenceCollection referenceInfo = referenceMap.getReferences(v);
if (isInlinableObject(referenceInfo.references)) {
// Blacklist the object itself, as well as any other values
// that it refers to, since they will have been moved around.
staleVars.add(v);
Reference init = referenceInfo.getInitializingReference();
// Split up the object into individual variables if the object
// is never referenced directly in full.
splitObject(v, init, referenceInfo);
}
}
}
/**
* If there are any variable references in the given node tree,
* blacklist them to prevent the pass from trying to inline the
* variable. Any code modifications will have potentially made the
* ReferenceCollection invalid.
*/
private void blacklistVarReferencesInTree(Node root, final Scope scope) {
NodeUtil.visitPreOrder(root, new NodeUtil.Visitor() {
@Override
public void visit(Node node) {
if (node.isName()) {
staleVars.add(scope.getVar(node.getString()));
}
}
}, NodeUtil.MATCH_NOT_FUNCTION);
}
/**
* Whether the given variable is forbidden from being inlined.
*/
private boolean isVarInlineForbidden(Var var) {
// A variable may not be inlined if:
// 1) The variable is defined in the externs
// 2) The variable is exported,
// 3) Don't inline the special RENAME_PROPERTY_FUNCTION_NAME
// 4) A reference to the variable has been inlined. We're downstream
// of the mechanism that creates variable references, so we don't
// have a good way to update the reference. Just punt on it.
// Additionally, exclude global variables for now.
return var.isGlobal()
|| var.isExtern()
|| compiler.getCodingConvention().isExported(var.name)
|| compiler
.getCodingConvention()
.isPropertyRenameFunction(var.nameNode.getQualifiedName())
|| staleVars.contains(var);
}
/**
* Counts the number of direct (full) references to an object.
* Specifically, we check for references of the following type:
* <pre>
* x;
* x.fn();
* </pre>
*/
private boolean isInlinableObject(List<Reference> refs) {
boolean ret = false;
Set<String> validProperties = new HashSet<>();
for (Reference ref : refs) {
Node name = ref.getNode();
Node parent = ref.getParent();
Node grandparent = ref.getGrandparent();
// Ignore most indirect references, like x.y (but not x.y(),
// since the function referenced by y might reference 'this').
//
if (parent.isGetProp()) {
checkState(parent.getFirstChild() == name);
// A call target may be using the object as a 'this' value.
if (grandparent.isCall()
&& grandparent.getFirstChild() == parent) {
return false;
}
// Deleting a property has different semantics from deleting
// a variable, so deleted properties should not be inlined.
if (grandparent.isDelProp()) {
return false;
}
// NOTE(nicksantos): This pass's object-splitting algorithm has
// a blind spot. It assumes that if a property isn't defined on an
// object, then the value is undefined. This is not true, because
// Object.prototype can have arbitrary properties on it.
//
// We short-circuit this problem by bailing out if we see a reference
// to a property that isn't defined on the object literal. This
// isn't a perfect algorithm, but it should catch most cases.
String propName = parent.getLastChild().getString();
if (!validProperties.contains(propName)) {
if (NodeUtil.isVarOrSimpleAssignLhs(parent, grandparent)) {
validProperties.add(propName);
} else {
return false;
}
}
continue;
}
// Only rewrite VAR declarations or simple assignment statements
if (!isVarOrAssignExprLhs(name)) {
return false;
}
Node val = ref.getAssignedValue();
if (val == null) {
// A var with no assignment.
continue;
}
// We're looking for object literal assignments only.
if (!val.isObjectLit()) {
return false;
}
// Make sure that the value is not self-referential. IOW,
// disallow things like x = {b: x.a}.
//
// TODO(dimvar): Only exclude unorderable self-referential
// assignments. i.e. x = {a: x.b, b: x.a} is not orderable,
// but x = {a: 1, b: x.a} is.
//
// Also, ES5 getters/setters aren't handled by this pass.
for (Node child = val.getFirstChild(); child != null;
child = child.getNext()) {
if (child.isGetterDef() || child.isSetterDef()) {
// ES5 get/set not supported.
return false;
}
validProperties.add(child.getString());
Node childVal = child.getFirstChild();
// Check if childVal is the parent of any of the passed in
// references, as that is how self-referential assignments
// will happen.
for (Reference t : refs) {
Node refNode = t.getParent();
while (!NodeUtil.isStatementBlock(refNode)) {
if (refNode == childVal) {
// There's a self-referential assignment
return false;
}
refNode = refNode.getParent();
}
}
}
// We have found an acceptable object literal assignment. As
// long as there are no other assignments that mess things up,
// we can inline.
ret = true;
}
return ret;
}
private boolean isVarOrAssignExprLhs(Node n) {
Node parent = n.getParent();
return parent.isVar()
|| (parent.isAssign()
&& parent.getFirstChild() == n
&& parent.getParent().isExprResult());
}
/**
* Computes a list of ever-referenced keys in the object being
* inlined, and returns a mapping of key name -> generated
* variable name.
*/
private Map<String, String> computeVarList(
ReferenceCollection referenceInfo) {
Map<String, String> varmap = new LinkedHashMap<>();
for (Reference ref : referenceInfo.references) {
if (ref.isLvalue() || ref.isInitializingDeclaration()) {
Node val = ref.getAssignedValue();
if (val != null) {
checkState(val.isObjectLit(), val);
for (Node child = val.getFirstChild(); child != null;
child = child.getNext()) {
String varname = child.getString();
if (varmap.containsKey(varname)) {
continue;
}
String var = VAR_PREFIX + varname + "_" + safeNameIdSupplier.get();
varmap.put(varname, var);
}
}
} else if (ref.getParent().isVar()) {
// This is the var. There is no value.
} else {
Node getprop = ref.getParent();
checkState(getprop.isGetProp(), getprop);
// The key being looked up in the original map.
String varname = getprop.getLastChild().getString();
if (varmap.containsKey(varname)) {
continue;
}
String var = VAR_PREFIX + varname + "_" + safeNameIdSupplier.get();
varmap.put(varname, var);
}
}
return varmap;
}
/**
* Populates a map of key names -> initial assigned values. The
* object literal these are being pulled from is invalidated as
* a result.
*/
private void fillInitialValues(Reference init, Map<String, Node> initvals) {
Node object = init.getAssignedValue();
checkState(object.isObjectLit(), object);
for (Node key = object.getFirstChild(); key != null;
key = key.getNext()) {
initvals.put(key.getString(), key.removeFirstChild());
}
}
/**
* Replaces an assignment like x = {...} with t1=a,t2=b,t3=c,true.
* Note that the resulting expression will always evaluate to
* true, as would the x = {...} expression.
*/
private void replaceAssignmentExpression(Var v, Reference ref,
Map<String, String> varmap) {
// Compute all of the assignments necessary
List<Node> nodes = new ArrayList<>();
Node val = ref.getAssignedValue();
blacklistVarReferencesInTree(val, v.scope);
checkState(val.isObjectLit(), val);
Set<String> all = new LinkedHashSet<>(varmap.keySet());
for (Node key = val.getFirstChild(); key != null;
key = key.getNext()) {
String var = key.getString();
Node value = key.removeFirstChild();
// TODO(user): Copy type information.
nodes.add(
IR.assign(
IR.name(varmap.get(var)),
value));
all.remove(var);
}
// TODO(user): Better source information.
for (String var : all) {
nodes.add(
IR.assign(
IR.name(varmap.get(var)),
NodeUtil.newUndefinedNode(null)));
}
Node replacement;
if (nodes.isEmpty()) {
replacement = IR.trueNode();
} else {
// All assignments evaluate to true, so make sure that the
// expr statement evaluates to true in case it matters.
nodes.add(IR.trueNode());
// Join these using COMMA. A COMMA node must have 2 children, so we
// create a tree. In the tree the first child be the COMMA to match
// the parser, otherwise tree equality tests fail.
nodes = Lists.reverse(nodes);
replacement = new Node(Token.COMMA);
Node cur = replacement;
int i;
for (i = 0; i < nodes.size() - 2; i++) {
cur.addChildToFront(nodes.get(i));
Node t = new Node(Token.COMMA);
cur.addChildToFront(t);
cur = t;
}
cur.addChildToFront(nodes.get(i));
cur.addChildToFront(nodes.get(i + 1));
}
Node replace = ref.getParent();
replacement.useSourceInfoIfMissingFromForTree(replace);
if (replace.isVar()) {
replace.replaceWith(NodeUtil.newExpr(replacement));
} else {
replace.replaceWith(replacement);
}
}
/**
* Splits up the object literal into individual variables, and
* updates all uses.
*/
private void splitObject(Var v, Reference init,
ReferenceCollection referenceInfo) {
// First figure out the FULL set of possible keys, so that they
// can all be properly set as necessary.
Map<String, String> varmap = computeVarList(referenceInfo);
Map<String, Node> initvals = new HashMap<>();
// Figure out the top-level of the var assign node. If it's a plain
// ASSIGN, then there's an EXPR_STATEMENT above it, if it's a
// VAR then it should be directly replaced.
Node vnode;
boolean defined = referenceInfo.isWellDefined() && init.getParent().isVar();
if (defined) {
vnode = init.getParent();
fillInitialValues(init, initvals);
} else {
// TODO(user): More test / rewrite this part.
// Find the beginning of the function / script.
vnode = v.getScope().getClosestHoistScope().getRootNode().getLastChild().getFirstChild();
}
for (Map.Entry<String, String> entry : varmap.entrySet()) {
Node val = initvals.get(entry.getKey());
Node newVarNode = NodeUtil.newVarNode(entry.getValue(), val);
if (val == null) {
// is this right?
newVarNode.useSourceInfoIfMissingFromForTree(vnode);
} else {
blacklistVarReferencesInTree(val, v.scope);
}
vnode.getParent().addChildBefore(newVarNode, vnode);
compiler.reportChangeToEnclosingScope(vnode);
}
if (defined) {
compiler.reportChangeToEnclosingScope(vnode.getParent());
vnode.getParent().removeChild(vnode);
}
for (Reference ref : referenceInfo.references) {
// The init/decl have already been converted.
if (defined && ref == init) {
continue;
}
compiler.reportChangeToEnclosingScope(ref.getNode());
if (ref.isLvalue()) {
// Assignments have to be handled specially, since they
// expand out into multiple assignments.
replaceAssignmentExpression(v, ref, varmap);
} else if (ref.getParent().isVar()) {
// The old variable declaration. It didn't have a
// value. Remove it entirely as it should now be unused.
ref.getGrandparent().removeChild(ref.getParent());
} else {
// Make sure that the reference is a GETPROP as we expect it to be.
Node getprop = ref.getParent();
checkState(getprop.isGetProp(), getprop);
// The key being looked up in the original map.
String var = getprop.getSecondChild().getString();
// If the variable hasn't already been declared, add an empty
// declaration near all the other declarations.
checkState(varmap.containsKey(var));
// Replace the GETPROP node with a NAME.
Node replacement = IR.name(varmap.get(var));
replacement.useSourceInfoIfMissingFrom(getprop);
ref.getGrandparent().replaceChild(ref.getParent(), replacement);
}
}
}
}
}