Debian Codegraph @

src/com/google/javascript/jscomp/AliasExternals.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
17 package com.google.javascript.jscomp;
18
19 import javax.annotation.Nullable;
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Strings;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Sets;
25 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
26 import com.google.javascript.rhino.IR;
27 import com.google.javascript.rhino.JSDocInfo;
28 import com.google.javascript.rhino.Node;
29 import com.google.javascript.rhino.Token;
30
31 import java.util.Arrays;
32 import java.util.IdentityHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37
38 /**
39 * <p>AliasExternals provides wrappers and aliases for external globals and
40 * properties to that they can be referenced by their full name only once
41 * instead of in all use sites.</p>
42 *
43 * <p>The property alias pass creates function wrappers for properties that need
44 * to be accessed externally. These function wrappers are then used by all
45 * internal calls to the property, and the names will be compressed during the
46 * RenamePrototypes step.</p>
47 *
48 * <p>Properties that are accessed externally are either system functions
49 * (i.e. window.document), or used by JavaScript embedded on a page.</p>
50 *
51 * <p>Properties that are r-values are changed to use array notation with
52 * a string that has been defined separately and can be compressed
53 * i.e. document.window -> document[PROP_window].</p>
54 *
55 * <p>Properties that are l-values and can be renamed are renamed to
56 * SETPROP_prop. I.e. node.innerHTML = '&lt;div&gt;Hello&lt;/div&gt;' ->
57 * SETPROP_innerHTML(node, '&lt;div&gt;hello&lt;/div&gt;').</p>
58 *
59 * <p>Properties will only be renamed if they are used more than requiredUsage_
60 * times, as there is overhead for adding the accessor and mutator functions.
61 * This is initialized to DEFAULT_REQUIRED_USAGE (=4), but can be
62 * overridden.</p>
63 *
64 * <p>Certain usages (increment, decrement) won't be addressed, as they would
65 * require a getprop, setprop, and custom logic, and aren't worth
66 * optimizing.</p>
67 *
68 * <p>The global alias pass creates aliases for global variables and functions
69 * that are declared or need to be used externally. These aliases are then used
70 * throughout the code, and will be compressed during the RenameVars step.</p>
71 *
72 * <p>Globals are aliased by inserting code like "var GLOBAL_window = window;"
73 * and then replacing all other uses of "window" with "GLOBAL_window."</p>
74 *
75 * <p>Globals that are l-values are not aliased.</p>
76 *
77 */
78 class AliasExternals implements CompilerPass {
79
80 /** Number of times a property needs to be accessed in order to alias */
81 private static final int DEFAULT_REQUIRED_USAGE = 4;
82
83 /** Number of times a property must be referenced in order to be aliased */
84 private int requiredUsage = DEFAULT_REQUIRED_USAGE;
85
86 /** Minimum property size to be worth renaming */
87 private static final int MIN_PROP_SIZE = 4;
88
89 /**
90 * The name of the variable used for the "prototype" string value. This is
91 * special-cased to make deobfuscated stack traces shorter and more readable
92 * ("$MyClass$$P$$method$" rather than "$MyClass$$$PROP_prototype$method$").
93 * @see NameAnonymousFunctions
94 */
95 static final String PROTOTYPE_PROPERTY_NAME =
96 getArrayNotationNameFor("prototype");
97
98 /** Map of all properties that we may be renaming */
99 private final Map<String, Symbol> props = Maps.newHashMap();
100
101 /** Holds the properties that can be renamed to GETPROP_ */
102 private final List<Node> accessors = Lists.newArrayList();
103
104 /** Holds the properties that can be renamed to SETPROP_ */
105 private final List<Node> mutators = Lists.newArrayList();
106
107 /**
108 * Map of node replacements -
109 * Identity map because Node implements equals() but not hashCode()
110 */
111 private final Map<Node, Node> replacementMap =
112 new IdentityHashMap<Node, Node>();
113
114 /** Map of all globals that we may alias */
115 private final Map<String, Symbol> globals = Maps.newHashMap();
116
117 /** Reference to JS Compiler */
118 private final AbstractCompiler compiler;
119
120 /** Reference to module inputs */
121 private final JSModuleGraph moduleGraph;
122
123 /** Root in parse tree for adding generated nodes */
124 private Node defaultRoot;
125
126 /** Root in each module for adding generated nodes, if using modules */
127 private Map<JSModule, Node> moduleRoots;
128
129 /**
130 * A set of globals that can not be aliased since they may be undefined or
131 * can cause errors
132 */
133 private final Set<String> unaliasableGlobals = Sets.newHashSet(
134 // While "arguments" is declared as a global extern, it really only has
135 // meaning inside function bodies and should not be aliased at a global
136 // level.
137 "arguments",
138 // Eval should not be aliased, per the ECMA-262 spec section 15.1.2.1
139 "eval",
140 // "NodeFilter" is not defined in IE and throws an error if you try to
141 // do var foo = NodeFilter.
142 "NodeFilter",
143 // Calls to this special function are eliminated by the RenameProperties
144 // compiler pass.
145 "JSCompiler_renameProperty");
146
147 /** Whitelist of aliasable externs. */
148 private final Set<String> aliasableGlobals = Sets.newHashSet();
149
150 /**
151 * Creates an instance.
152 *
153 * @param compiler The Compiler
154 * @param moduleGraph The graph of input modules. May be null. If given, we'll
155 * try to push aliased externs into the deepest possible module.
156 */
157 AliasExternals(AbstractCompiler compiler, JSModuleGraph moduleGraph) {
158 this(compiler, moduleGraph, null, null);
159 }
160
161 /**
162 * Creates an instance.
163 *
164 * @param compiler The Compiler
165 * @param moduleGraph The graph of input modules. May be null. If given, we'll
166 * try to push aliased externs into the deepest possible module.
167 * @param unaliasableGlobals Comma-separated list of additional globals that
168 * cannot be aliased since they may be undefined or can cause errors
169 * (e.g. "foo,bar"). May be null or the empty string.
170 * @param aliasableGlobals Comma-separated list of globals that
171 * can be aliased. If provided, only this list of globals can be aliased.
172 */
173 AliasExternals(AbstractCompiler compiler, JSModuleGraph moduleGraph,
174 @Nullable String unaliasableGlobals,
175 @Nullable String aliasableGlobals) {
176 this.compiler = compiler;
177 this.moduleGraph = moduleGraph;
178
179 if (!Strings.isNullOrEmpty(unaliasableGlobals) &&
180 !Strings.isNullOrEmpty(aliasableGlobals)) {
181 throw new IllegalArgumentException(
182 "Cannot pass in both unaliasable and aliasable globals; you must " +
183 "choose one or the other.");
184 }
185
186 if (!Strings.isNullOrEmpty(unaliasableGlobals)) {
187 this.unaliasableGlobals.addAll(
188 Arrays.asList(unaliasableGlobals.split(",")));
189 }
190
191 if (!Strings.isNullOrEmpty(aliasableGlobals)) {
192 this.aliasableGlobals.addAll(Arrays.asList(aliasableGlobals.split(",")));
193 }
194
195 if (moduleGraph != null) {
196 moduleRoots = Maps.newHashMap();
197 }
198 }
199
200 /**
201 * Sets the number of times a property needs to be referenced in order to
202 * create an alias for it.
203 * @param usage Number of times
204 */
205 public void setRequiredUsage(int usage) {
206 this.requiredUsage = usage;
207 }
208
209 /**
210 * Do all processing on the root node.
211 */
212 @Override
213 public void process(Node externs, Node root) {
214 defaultRoot = root.getFirstChild();
215 Preconditions.checkState(defaultRoot.isScript());
216
217 aliasProperties(externs, root);
218 aliasGlobals(externs, root);
219 }
220
221 private void aliasProperties(Node externs, Node root) {
222 // Get the reserved names, filtered by the whitelist.
223 NodeTraversal.traverse(compiler, externs,
224 new GetAliasableNames(aliasableGlobals));
225 props.put("prototype", newSymbolForProperty("prototype"));
226
227 // Find the props that can be changed
228 NodeTraversal.traverse(compiler, root, new PropertyGatherer());
229
230 // Iterate through the reserved names, decide what to change
231 // This could have been done during property traversal, but
232 // This gives opportunity for review & modification if needed
233 for (Symbol prop : props.values()) {
234 if (prop.name.length() >= MIN_PROP_SIZE) {
235 if (prop.accessorCount >= requiredUsage) {
236 prop.aliasAccessor = true;
237 }
238 if (prop.mutatorCount >= requiredUsage) {
239 prop.aliasMutator = true;
240 }
241 }
242 }
243 // Change the references to the property gets
244 for (Node propInfo : accessors) {
245 replaceAccessor(propInfo);
246 }
247
248 // Change the references to the property sets
249 for (Node propInfo : mutators) {
250 replaceMutator(propInfo);
251 }
252
253 // And add the accessor and mutator functions, if needed. Property names are
254 // grouped together so that the CollapseVariableDeclarations pass can put
255 // them in a single variable declaration statement.
256 for (Symbol prop : props.values()) {
257 if (prop.aliasAccessor) {
258 addAccessorPropName(prop.name, getAddingRoot(prop.deepestModuleAccess));
259 }
260 }
261
262 for (Symbol prop : props.values()) {
263 if (prop.aliasMutator) {
264 addMutatorFunction(prop.name, getAddingRoot(prop.deepestModuleMutate));
265 }
266 }
267 }
268
269 /*
270 * Replaces a GETPROP with array notation, so that
271 * it can be optimized.
272 * I.e. prop.length -> prop[PROP_length] -> prop[a];
273 */
274 private void replaceAccessor(Node getPropNode) {
275 /*
276 * BEFORE
277 getprop
278 NODE...
279 string length
280 AFTER
281 getelem
282 NODE...
283 name PROP_length
284 */
285 Node propNameNode = getPropNode.getLastChild();
286 String propName = propNameNode.getString();
287 if (props.get(propName).aliasAccessor) {
288 Node propSrc = getPropNode.getFirstChild();
289 getPropNode.removeChild(propSrc);
290
291 Node newNameNode =
292 IR.name(getArrayNotationNameFor(propName));
293
294 Node elemNode = IR.getelem(propSrc, newNameNode);
295 replaceNode(getPropNode.getParent(), getPropNode, elemNode);
296
297 compiler.reportCodeChange();
298 }
299 }
300
301 /**
302 * Changes a.prop = b to SETPROP_prop(a, b);
303 */
304 private void replaceMutator(Node getPropNode) {
305 /*
306 BEFORE
307 exprstmt 1
308 assign 128
309 getprop
310 NodeTree A
311 string prop
312 NODE TREE B
313
314 AFTER
315 exprstmt 1
316 call
317 name SETPROP_prop
318 NodeTree A
319 NODE TREE B
320 */
321 Node propNameNode = getPropNode.getLastChild();
322 Node parentNode = getPropNode.getParent();
323
324 Symbol prop = props.get(propNameNode.getString());
325 if (prop.aliasMutator) {
326 Node propSrc = getPropNode.getFirstChild();
327 Node propDest = parentNode.getLastChild();
328
329 // Remove the orphaned children
330 getPropNode.removeChild(propSrc);
331 getPropNode.removeChild(propNameNode);
332 parentNode.removeChild(propDest);
333
334 // Create the call GETPROP_prop() node, using the old propSrc as the
335 // one parameter to GETPROP_prop() call.
336 Node callName = IR.name(
337 getMutatorFor(propNameNode.getString()));
338 Node call = IR.call( callName, propSrc, propDest);
339 call.putBooleanProp(Node.FREE_CALL, true);
340
341 // And replace the assign statement with the new call
342 replaceNode(parentNode.getParent(), parentNode, call);
343
344 compiler.reportCodeChange();
345 }
346 }
347
348 /**
349 * Utility function to replace a Node with another node.
350 * Keeps track of previous replacements so that if you try to replace
351 * a child of a parent that has changed, it replaces on the new parent
352 * @param parent Parent of node to be replaced
353 * @param before Node to be replaced
354 * @param after Replacement node
355 */
356 private void replaceNode(Node parent, Node before, Node after) {
357 if (replacementMap.containsKey(parent)) {
358 parent = replacementMap.get(parent);
359 }
360 parent.replaceChild(before, after);
361 replacementMap.put(before, after);
362 }
363
364 /**
365 * Adds a string that can be used to reference properties by array []
366 * notation.
367 *
368 * PROP_prototype = 'prototype';
369 *
370 * @param propName Name of property
371 * @param root Root of output tree that function can be added to
372 */
373 private void addAccessorPropName(String propName, Node root) {
374 /*
375 * Target:
376
377 var 1
378 name PROP_length
379 string length
380 */
381 Node propValue = IR.string(propName);
382 Node propNameNode =
383 IR.name(getArrayNotationNameFor(propName));
384 propNameNode.addChildToFront(propValue);
385 Node var = IR.var(propNameNode);
386 root.addChildToFront(var);
387
388 compiler.reportCodeChange();
389 }
390
391 /**
392 * Create set property function in JS. Output will be:
393 * SETPROP_prop(a, b) {a.prop = b;}
394 *
395 * @param propName Name of property
396 * @param root Root of output tree that function can be added to
397 */
398 private void addMutatorFunction(String propName, Node root) {
399 /*
400 function SETPROP_prop
401 name SETPROP_prop
402 lp
403 name a
404 name b
405 block 1
406 return 1
407 assign
408 getprop
409 name a
410 string prop
411 name b
412 */
413
414 // Function name node
415 String functionName = getMutatorFor(propName);
416
417 // Function arguments
418 String localPropName = getMutatorFor(propName) + "$a";
419 String localValueName = getMutatorFor(propName) + "$b";
420 // Create the function and append to front of output tree
421 Node fnNode = IR.function(
422 IR.name(functionName),
423 IR.paramList(IR.name(localPropName), IR.name(localValueName)),
424 IR.block(
425 IR.returnNode(
426 IR.assign(
427 IR.getprop(IR.name(localPropName), IR.string(propName)),
428 IR.name(localValueName)))));
429 root.addChildToFront(fnNode);
430
431 compiler.reportCodeChange();
432 }
433
434 /**
435 * Gets a SCRIPT node for code insertion in {@code m} or, if {@code m} is
436 * empty, in as deep an ancestor module of {@code m} as possible. Returns
437 * {@code this.defaultRoot} if {@code m} is null.
438 *
439 * @param m The module to find a root in (may be null)
440 * @return A root node
441 */
442 private Node getAddingRoot(JSModule m) {
443 if (m != null) {
444 Node root = moduleRoots.get(m);
445 if (root != null) {
446 return root;
447 }
448
449 root = compiler.getNodeForCodeInsertion(m);
450 if (root != null) {
451 moduleRoots.put(m, root);
452 return root;
453 }
454 }
455
456 return defaultRoot;
457 }
458
459 /**
460 * Gets the aliasable names from externs.js
461 */
462 private class GetAliasableNames extends AbstractPostOrderCallback {
463 private final Set<String> whitelist;
464
465 public GetAliasableNames(final Set<String> whitelist) {
466 this.whitelist = whitelist;
467 }
468
469 @Override
470 public void visit(NodeTraversal t, Node n, Node parent) {
471 switch (n.getType()) {
472 case Token.GETPROP:
473 case Token.GETELEM:
474 Node dest = n.getFirstChild().getNext();
475 if (dest.isString() &&
476 (whitelist.isEmpty() || whitelist.contains(dest.getString()))) {
477 props.put(dest.getString(), newSymbolForProperty(dest.getString()));
478 }
479 }
480 }
481 }
482
483 /**
484 * Gets references to all of the replaceable nodes, as well
485 * as counting the usage for each property name.
486 */
487 private final class PropertyGatherer extends AbstractPostOrderCallback {
488
489 @Override
490 public void visit(NodeTraversal t, Node n, Node parent) {
491 if (n.isGetProp()) {
492 Node propNameNode = n.getLastChild();
493
494 if (canReplaceWithGetProp(propNameNode, n, parent)) {
495 String name = propNameNode.getString();
496 props.get(name).recordAccessor(t);
497 accessors.add(n);
498 }
499 if (canReplaceWithSetProp(propNameNode, n, parent)) {
500 String name = propNameNode.getString();
501
502 props.get(name).recordMutator(t);
503 mutators.add(n);
504 }
505 }
506 }
507
508 /**
509 * Logic for when a getprop can be replaced.
510 * Can't alias a call to eval per ECMA-262 spec section 15.1.2.1
511 * Can't be an assign -> no a.b = c;
512 * Can't be inc or dec -> no a.b++; or a.b--;
513 * Must be a GETPROP (NODE, A) where A is a reserved name
514 * @param propNameNode Property name node
515 * @param getPropNode GETPROP node
516 * @param parent parent node
517 * @return True if can be replaced
518 */
519 private boolean canReplaceWithGetProp(Node propNameNode, Node getPropNode,
520 Node parent) {
521 boolean isCallTarget = (parent.isCall())
522 && (parent.getFirstChild() == getPropNode);
523 boolean isAssignTarget = NodeUtil.isAssignmentOp(parent)
524 && (parent.getFirstChild() == getPropNode);
525 boolean isIncOrDec = (parent.isInc()) ||
526 (parent.isDec());
527 return (propNameNode.isString()) && !isAssignTarget
528 && (!isCallTarget || !"eval".equals(propNameNode.getString()))
529 && !isIncOrDec
530 && props.containsKey(propNameNode.getString());
531 }
532
533 /**
534 * Logic for whether a setprop can be replaced.
535 *
536 * True if it is target of assign (i.e. foo = A.B), and B is a reserved name
537 * @param propNameNode Property name node
538 * @param getPropNode GETPROP node
539 * @param parent parent node
540 * @return True if can be replaced
541 */
542 private boolean canReplaceWithSetProp(Node propNameNode, Node getPropNode,
543 Node parent) {
544 boolean isAssignTarget = (parent.isAssign())
545 && (parent.getFirstChild() == getPropNode);
546 return (propNameNode.isString()) && isAssignTarget
547 && props.containsKey(propNameNode.getString());
548 }
549 }
550
551 /**
552 * Gets the mutator name for a property.
553 */
554 private static String getMutatorFor(String prop) {
555 return "SETPROP_" + prop;
556 }
557
558 /**
559 * Gets the array notation name for a property.
560 */
561 private static String getArrayNotationNameFor(String prop) {
562 return "$$PROP_" + prop;
563 }
564
565 private void aliasGlobals(Node externs, Node root) {
566 // Find all the extern globals that we should alias
567 NodeTraversal.traverse(compiler, externs, new GetGlobals());
568
569 // Find all the globals that can be changed
570 NodeTraversal.traverse(compiler, root, new GlobalGatherer());
571
572 // Iterate through the used globals, decide what to change.
573 for (Symbol global : globals.values()) {
574 if (global.mutatorCount > 0) {
575 continue;
576 }
577
578 // We assume that each alias variable will end up compressed to two letter
579 // names. There is also the overhead of "var xx=<global>;"
580 int currentBytes = global.name.length() * global.accessorCount;
581 int aliasedBytes = 8 + global.name.length() + 2 * global.accessorCount;
582
583 if (aliasedBytes < currentBytes) {
584 global.aliasAccessor = true;
585 }
586 }
587
588 // Change the references to the globals
589 for (Symbol global : globals.values()) {
590 for (Node globalUse : global.uses) {
591 replaceGlobalUse(globalUse);
592 }
593 if (global.aliasAccessor) {
594 addGlobalAliasNode(global,
595 getAddingRoot(global.deepestModuleAccess));
596 }
597 }
598 }
599
600 /**
601 * Gets the aliasable names from externs.js
602 */
603 private class GetGlobals extends NodeTraversal.AbstractShallowCallback {
604 private void getGlobalName(NodeTraversal t, Node dest, Node parent) {
605 if (dest.isName()) {
606
607 JSDocInfo docInfo = dest.getJSDocInfo() == null ?
608 parent.getJSDocInfo() : dest.getJSDocInfo();
609 boolean aliasable = !unaliasableGlobals.contains(dest.getString()) &&
610 (docInfo == null || !docInfo.isNoAlias());
611
612 if (aliasable) {
613 String name = dest.getString();
614 Scope.Var var = t.getScope().getVar(name);
615
616 if (var != null && !var.isLocal()) {
617 globals.put(name, newSymbolForGlobalVar(dest));
618 }
619 }
620 }
621 }
622
623 @Override
624 public void visit(NodeTraversal t, Node n, Node parent) {
625 switch (n.getType()) {
626 case Token.FUNCTION:
627 getGlobalName(t, n.getFirstChild(), n);
628 break;
629 case Token.VAR:
630 for (Node varChild = n.getFirstChild();
631 varChild != null;
632 varChild = varChild.getNext()) {
633 getGlobalName(t, varChild, n);
634 }
635 break;
636 }
637 }
638 }
639
640 /**
641 * Gets references to all of the replaceable nodes, as well as counting the
642 * usage for each global.
643 */
644 private final class GlobalGatherer extends AbstractPostOrderCallback {
645 @Override
646 public void visit(NodeTraversal t, Node n, Node parent) {
647 if (n.isName()) {
648 String name = n.getString();
649 Scope.Var var = t.getScope().getVar(name);
650
651 // It's ok for var to be null since it won't be in any scope if it's
652 // an extern
653 if (var != null && var.isLocal()) {
654 return;
655 }
656
657 Symbol global = globals.get(name);
658 if (global != null) {
659 // If a variable is declared in both externs and normal source,
660 // don't alias it.
661 if (n.getParent().isVar() ||
662 n.getParent().isFunction()) {
663 globals.remove(name);
664 }
665
666 boolean isFirst = parent.getFirstChild() == n;
667 // If a global is being assigned to or otherwise modified, then we
668 // don't want to alias it.
669 // Using "new" with this global is not a mutator, but it's also
670 // something that we want to avoid when aliasing, since we may be
671 // dealing with external objects (e.g. ActiveXObject in MSIE)
672 if ((NodeUtil.isAssignmentOp(parent) && isFirst) ||
673 (parent.isNew() && isFirst) ||
674 parent.isInc() ||
675 parent.isDec()) {
676 global.recordMutator(t);
677 } else {
678 global.recordAccessor(t);
679 }
680
681 global.uses.add(n);
682 }
683 }
684 }
685 }
686
687 /**
688 * Replace uses of a global with its aliased name.
689 */
690 private void replaceGlobalUse(Node globalUse) {
691 String globalName = globalUse.getString();
692 if (globals.get(globalName).aliasAccessor) {
693 globalUse.setString("GLOBAL_" + globalName);
694
695 // None of the aliases are marked as @const.
696 // Because we're reusing the original ref node,
697 // we need to update it to reflect this.
698 globalUse.putBooleanProp(Node.IS_CONSTANT_NAME, false);
699
700 compiler.reportCodeChange();
701 }
702 }
703
704 /**
705 * Adds an alias variable for the global:
706 *
707 * var GLOBAL_window = window;
708 *
709 * @param global Name of global
710 * @param root Root of output tree that function can be added to
711 */
712 private void addGlobalAliasNode(Symbol global, Node root) {
713 /*
714 * Target:
715
716 var 1
717 name GLOBAL_window
718 name window
719 */
720
721 String globalName = global.name;
722 Node globalValue = IR.name(global.name);
723 globalValue.putBooleanProp(Node.IS_CONSTANT_NAME, global.isConstant);
724
725 Node globalNameNode = IR.name("GLOBAL_" + globalName);
726 globalNameNode.addChildToFront(globalValue);
727 Node var = IR.var(globalNameNode);
728 root.addChildToFront(var);
729
730 compiler.reportCodeChange();
731 }
732
733 private Symbol newSymbolForGlobalVar(Node name) {
734 return new Symbol(
735 name.getString(), name.getBooleanProp(Node.IS_CONSTANT_NAME));
736 }
737
738 private Symbol newSymbolForProperty(String name) {
739 return new Symbol(name, false);
740 }
741
742 /** Struct to hold information about properties & usage */
743 private class Symbol {
744 public final String name;
745 public int accessorCount = 0;
746 public int mutatorCount = 0;
747 public boolean aliasMutator = false;
748 public boolean aliasAccessor = false;
749 public final boolean isConstant;
750
751 JSModule deepestModuleAccess = null;
752 JSModule deepestModuleMutate = null;
753
754 List<Node> uses = Lists.newArrayList();
755
756 private Symbol(String name, boolean isConstant) {
757 this.name = name;
758 this.isConstant = isConstant;
759 }
760
761 void recordAccessor(NodeTraversal t) {
762 accessorCount++;
763 if (moduleGraph != null) {
764 deepestModuleAccess = (deepestModuleAccess == null) ?
765 t.getModule() :
766 moduleGraph.getDeepestCommonDependencyInclusive(
767 t.getModule(), deepestModuleAccess);
768 }
769 }
770
771 void recordMutator(NodeTraversal t) {
772 mutatorCount++;
773 if (moduleGraph != null) {
774 deepestModuleMutate = (deepestModuleMutate == null) ?
775 t.getModule() :
776 moduleGraph.getDeepestCommonDependencyInclusive(
777 t.getModule(), deepestModuleMutate);
778 }
779 }
780 }
781 }