Debian Codegraph @

src/com/google/javascript/jscomp/AnalyzePrototypeProperties.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 com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Maps;
23 import com.google.common.collect.Sets;
24 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
25 import com.google.javascript.jscomp.Scope.Var;
26 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal;
27 import com.google.javascript.jscomp.graph.LinkedDirectedGraph;
28 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal.EdgeCallback;
29 import com.google.javascript.rhino.Node;
30 import com.google.javascript.rhino.Token;
31
32 import java.util.ArrayDeque;
33 import java.util.Collection;
34 import java.util.Deque;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.Stack;
39
40 /**
41 * Analyzes properties on prototypes.
42 *
43 * Uses a reference graph to analyze prototype properties. Each unique property
44 * name is represented by a node in this graph. An edge from property A to
45 * property B means that there's a GETPROP access of a property B on some
46 * object inside of a method named A.
47 *
48 * Global functions are also represented by nodes in this graph, with
49 * similar semantics.
50 *
51 */
52 class AnalyzePrototypeProperties implements CompilerPass {
53
54 // Constants for symbol types, for easier readability.
55 private final SymbolType PROPERTY = SymbolType.PROPERTY;
56 private final SymbolType VAR = SymbolType.VAR;
57
58 private final AbstractCompiler compiler;
59 private final boolean canModifyExterns;
60 private final boolean anchorUnusedVars;
61 private final JSModuleGraph moduleGraph;
62 private final JSModule firstModule;
63
64 // Properties that are implicitly used as part of the JS language.
65 private static final Set<String> IMPLICITLY_USED_PROPERTIES =
66 ImmutableSet.of("length", "toString", "valueOf");
67
68 // A graph where the nodes are property names or variable names,
69 // and the edges signify the modules where the property is referenced.
70 // For example, if we had the code:
71 //
72 // Foo.prototype.bar = function(x) { x.baz(); }; // in module 2.;
73 //
74 // then this would be represented in the graph by a node representing
75 // "bar", a node representing "baz", and an edge between them representing
76 // module #2.
77 //
78 // Similarly, if we had:
79 //
80 // var scotch = function(f) { return f.age(); };
81 //
82 // then there would be a node for "scotch", a node for "age", and an edge
83 // from scotch to age.
84 private final LinkedDirectedGraph<NameInfo, JSModule> symbolGraph =
85 LinkedDirectedGraph.createWithoutAnnotations();
86
87 // A dummy node for representing global references.
88 private final NameInfo globalNode = new NameInfo("[global]");
89
90 // A dummy node for representing extern references.
91 private final NameInfo externNode = new NameInfo("[extern]");
92
93 // A dummy node for representing all anonymous functions with no names.
94 private final NameInfo anonymousNode = new NameInfo("[anonymous]");
95
96 // All the real NameInfo for prototype properties, hashed by the name
97 // of the property that they represent.
98 private final Map<String, NameInfo> propertyNameInfo = Maps.newHashMap();
99
100 // All the NameInfo for global functions, hashed by the name of the
101 // global variable that it's assigned to.
102 private final Map<String, NameInfo> varNameInfo = Maps.newHashMap();
103
104 /**
105 * Creates a new pass for analyzing prototype properties.
106 * @param compiler The compiler.
107 * @param moduleGraph The graph for resolving module dependencies. May be
108 * null if we don't care about module dependencies.
109 * @param canModifyExterns If true, then we can move prototype
110 * properties that are declared in the externs file.
111 * @param anchorUnusedVars If true, then we must mark all vars as referenced,
112 * even if they are never used.
113 */
114 AnalyzePrototypeProperties(AbstractCompiler compiler,
115 JSModuleGraph moduleGraph, boolean canModifyExterns,
116 boolean anchorUnusedVars) {
117 this.compiler = compiler;
118 this.moduleGraph = moduleGraph;
119 this.canModifyExterns = canModifyExterns;
120 this.anchorUnusedVars = anchorUnusedVars;
121
122 if (moduleGraph != null) {
123 firstModule = moduleGraph.getRootModule();
124 } else {
125 firstModule = null;
126 }
127
128 globalNode.markReference(null);
129 externNode.markReference(null);
130 symbolGraph.createNode(globalNode);
131 symbolGraph.createNode(externNode);
132
133 for (String property : IMPLICITLY_USED_PROPERTIES) {
134 NameInfo nameInfo = getNameInfoForName(property, PROPERTY);
135 if (moduleGraph == null) {
136 symbolGraph.connect(externNode, null, nameInfo);
137 } else {
138 for (JSModule module : moduleGraph.getAllModules()) {
139 symbolGraph.connect(externNode, module, nameInfo);
140 }
141 }
142 }
143 }
144
145 @Override
146 public void process(Node externRoot, Node root) {
147 if (!canModifyExterns) {
148 NodeTraversal.traverse(compiler, externRoot,
149 new ProcessExternProperties());
150 }
151
152 NodeTraversal.traverse(compiler, root, new ProcessProperties());
153
154 FixedPointGraphTraversal<NameInfo, JSModule> t =
155 FixedPointGraphTraversal.newTraversal(new PropagateReferences());
156 t.computeFixedPoint(symbolGraph,
157 Sets.newHashSet(externNode, globalNode));
158 }
159
160 /**
161 * Returns information on all prototype properties.
162 */
163 public Collection<NameInfo> getAllNameInfo() {
164 List<NameInfo> result = Lists.newArrayList(propertyNameInfo.values());
165 result.addAll(varNameInfo.values());
166 return result;
167 }
168
169 /**
170 * Gets the name info for the property or variable of a given name,
171 * and creates a new one if necessary.
172 *
173 * @param name The name of the symbol.
174 * @param type The type of symbol.
175 */
176 private NameInfo getNameInfoForName(String name, SymbolType type) {
177 Map<String, NameInfo> map = type == PROPERTY ?
178 propertyNameInfo : varNameInfo;
179 if (map.containsKey(name)) {
180 return map.get(name);
181 } else {
182 NameInfo nameInfo = new NameInfo(name);
183 map.put(name, nameInfo);
184 symbolGraph.createNode(nameInfo);
185 return nameInfo;
186 }
187 }
188
189 private class ProcessProperties implements NodeTraversal.ScopedCallback {
190 // There are two types of context information on this stack:
191 // 1) Every scope has a NameContext corresponding to its scope.
192 // Variables are given VAR contexts.
193 // Prototype properties are given PROPERTY contexts.
194 // The global scope is given the special [global] context.
195 // And function expressions that we aren't able to give a reasonable
196 // name are given a special [anonymous] context.
197 // 2) Every assignment of a prototype property of a non-function is
198 // given a name context. These contexts do not have scopes.
199 private Stack<NameContext> symbolStack = new Stack<NameContext>();
200
201 @Override
202 public void enterScope(NodeTraversal t) {
203 Node n = t.getCurrentNode();
204 if (n.isFunction()) {
205 String propName = getPrototypePropertyNameFromRValue(n);
206 if (propName != null) {
207 symbolStack.push(
208 new NameContext(
209 getNameInfoForName(propName, PROPERTY),
210 t.getScope()));
211 } else if (isGlobalFunctionDeclaration(t, n)) {
212 Node parent = n.getParent();
213 String name = parent.isName() ?
214 parent.getString() /* VAR */ :
215 n.getFirstChild().getString() /* named function */;
216 symbolStack.push(
217 new NameContext(getNameInfoForName(name, VAR), t.getScope()));
218 } else {
219 // NOTE(nicksantos): We use the same anonymous node for all
220 // functions that do not have reasonable names. I can't remember
221 // at the moment why we do this. I think it's because anonymous
222 // nodes can never have in-edges. They're just there as a placeholder
223 // for scope information, and do not matter in the edge propagation.
224 symbolStack.push(new NameContext(anonymousNode, t.getScope()));
225 }
226 } else {
227 Preconditions.checkState(t.inGlobalScope());
228 symbolStack.push(new NameContext(globalNode, t.getScope()));
229 }
230 }
231
232 @Override
233 public void exitScope(NodeTraversal t) {
234 symbolStack.pop();
235 }
236
237 @Override
238 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
239 // Process prototype assignments to non-functions.
240 String propName = processNonFunctionPrototypeAssign(n, parent);
241 if (propName != null) {
242 symbolStack.push(
243 new NameContext(
244 getNameInfoForName(propName, PROPERTY), null));
245 }
246 return true;
247 }
248
249 @Override
250 public void visit(NodeTraversal t, Node n, Node parent) {
251 if (n.isGetProp()) {
252 String propName = n.getFirstChild().getNext().getString();
253
254 if (n.isQualifiedName()) {
255 if (propName.equals("prototype")) {
256 if (processPrototypeRef(t, n)) {
257 return;
258 }
259 } else if (compiler.getCodingConvention().isExported(propName)) {
260 addGlobalUseOfSymbol(propName, t.getModule(), PROPERTY);
261 return;
262 } else {
263 // Do not mark prototype prop assigns as a 'use' in the global scope.
264 if (n.getParent().isAssign() && n.getNext() != null) {
265 String rValueName = getPrototypePropertyNameFromRValue(n);
266 if (rValueName != null) {
267 return;
268 }
269 }
270 }
271 }
272
273 addSymbolUse(propName, t.getModule(), PROPERTY);
274 } else if (n.isObjectLit()) {
275 // Make sure that we're not handling object literals being
276 // assigned to a prototype, as in:
277 // Foo.prototype = {bar: 3, baz: 5};
278 String lValueName = NodeUtil.getBestLValueName(
279 NodeUtil.getBestLValue(n));
280 if (lValueName != null && lValueName.endsWith(".prototype")) {
281 return;
282 }
283
284 // var x = {a: 1, b: 2}
285 // should count as a use of property a and b.
286 for (Node propNameNode = n.getFirstChild(); propNameNode != null;
287 propNameNode = propNameNode.getNext()) {
288 // May be STRING, GET, or SET, but NUMBER isn't interesting.
289 if (!propNameNode.isQuotedString()) {
290 addSymbolUse(propNameNode.getString(), t.getModule(), PROPERTY);
291 }
292 }
293 } else if (n.isName()) {
294 String name = n.getString();
295
296 Var var = t.getScope().getVar(name);
297 if (var != null) {
298 // Only process global functions.
299 if (var.isGlobal()) {
300 if (var.getInitialValue() != null &&
301 var.getInitialValue().isFunction()) {
302 if (t.inGlobalScope()) {
303 if (!processGlobalFunctionDeclaration(t, n, var)) {
304 addGlobalUseOfSymbol(name, t.getModule(), VAR);
305 }
306 } else {
307 addSymbolUse(name, t.getModule(), VAR);
308 }
309 }
310
311 // If it is not a global, it might be accessing a local of the outer
312 // scope. If that's the case the functions between the variable's
313 // declaring scope and the variable reference scope cannot be moved.
314 } else if (var.getScope() != t.getScope()){
315 for (int i = symbolStack.size() - 1; i >= 0; i--) {
316 NameContext context = symbolStack.get(i);
317 if (context.scope == var.getScope()) {
318 break;
319 }
320
321 context.name.readClosureVariables = true;
322 }
323 }
324 }
325 }
326
327 // Process prototype assignments to non-functions.
328 if (processNonFunctionPrototypeAssign(n, parent) != null) {
329 symbolStack.pop();
330 }
331 }
332
333 private void addSymbolUse(String name, JSModule module, SymbolType type) {
334 NameInfo info = getNameInfoForName(name, type);
335 NameInfo def = null;
336 // Skip all anonymous nodes. We care only about symbols with names.
337 for (int i = symbolStack.size() - 1; i >= 0; i--) {
338 def = symbolStack.get(i).name;
339 if (def != anonymousNode) {
340 break;
341 }
342 }
343 if (!def.equals(info)) {
344 symbolGraph.connect(def, module, info);
345 }
346 }
347
348 /**
349 * If this is a non-function prototype assign, return the prop name.
350 * Otherwise, return null.
351 */
352 private String processNonFunctionPrototypeAssign(Node n, Node parent) {
353 if (isAssignRValue(n, parent) && !n.isFunction()) {
354 return getPrototypePropertyNameFromRValue(n);
355 }
356 return null;
357 }
358
359 /**
360 * Determines whether {@code n} is the FUNCTION node in a global function
361 * declaration.
362 */
363 private boolean isGlobalFunctionDeclaration(NodeTraversal t, Node n) {
364 // Make sure we're either in the global scope, or the function
365 // we're looking at is the root of the current local scope.
366 Scope s = t.getScope();
367 if (!(s.isGlobal() ||
368 s.getDepth() == 1 && s.getRootNode() == n)) {
369 return false;
370 }
371
372 return NodeUtil.isFunctionDeclaration(n) ||
373 n.isFunction() && n.getParent().isName();
374 }
375
376 /**
377 * Returns true if this is the r-value of an assignment.
378 */
379 private boolean isAssignRValue(Node n, Node parent) {
380 return parent != null && parent.isAssign() && parent.getFirstChild() != n;
381 }
382
383 /**
384 * Returns the name of a prototype property being assigned to this r-value.
385 *
386 * Returns null if this is not the R-value of a prototype property, or if
387 * the R-value is used in multiple expressions (i.e., if there's
388 * a prototype property assignment in a more complex expression).
389 */
390 private String getPrototypePropertyNameFromRValue(Node rValue) {
391 Node lValue = NodeUtil.getBestLValue(rValue);
392 if (lValue == null ||
393 lValue.getParent() == null ||
394 lValue.getParent().getParent() == null ||
395 !(NodeUtil.isObjectLitKey(lValue) ||
396 NodeUtil.isExprAssign(lValue.getParent().getParent()))) {
397 return null;
398 }
399
400 String lValueName =
401 NodeUtil.getBestLValueName(NodeUtil.getBestLValue(rValue));
402 if (lValueName == null) {
403 return null;
404 }
405 int lastDot = lValueName.lastIndexOf('.');
406 if (lastDot == -1) {
407 return null;
408 }
409
410 String firstPart = lValueName.substring(0, lastDot);
411 if (!firstPart.endsWith(".prototype")) {
412 return null;
413 }
414
415 return lValueName.substring(lastDot + 1);
416 }
417
418 /**
419 * Processes a NAME node to see if it's a global function declaration.
420 * If it is, record it and return true. Otherwise, return false.
421 */
422 private boolean processGlobalFunctionDeclaration(NodeTraversal t,
423 Node nameNode, Var v) {
424 Node firstChild = nameNode.getFirstChild();
425 Node parent = nameNode.getParent();
426
427 if (// Check for a named FUNCTION.
428 isGlobalFunctionDeclaration(t, parent) ||
429 // Check for a VAR declaration.
430 firstChild != null &&
431 isGlobalFunctionDeclaration(t, firstChild)) {
432 String name = nameNode.getString();
433 getNameInfoForName(name, VAR).getDeclarations().add(
434 new GlobalFunction(nameNode, v, t.getModule()));
435
436 // If the function name is exported, we should create an edge here
437 // so that it's never removed.
438 if (compiler.getCodingConvention().isExported(name) ||
439 anchorUnusedVars) {
440 addGlobalUseOfSymbol(name, t.getModule(), VAR);
441 }
442
443 return true;
444 }
445 return false;
446 }
447
448 /**
449 * Processes the GETPROP of prototype, which can either be under
450 * another GETPROP (in the case of Foo.prototype.bar), or can be
451 * under an assignment (in the case of Foo.prototype = ...).
452 * @return True if a declaration was added.
453 */
454 private boolean processPrototypeRef(NodeTraversal t, Node ref) {
455 Node root = NodeUtil.getRootOfQualifiedName(ref);
456
457 Node n = ref.getParent();
458 switch (n.getType()) {
459 // Foo.prototype.getBar = function() { ... }
460 case Token.GETPROP:
461 Node dest = n.getFirstChild().getNext();
462 Node parent = n.getParent();
463 Node grandParent = parent.getParent();
464
465 if (dest.isString() &&
466 NodeUtil.isExprAssign(grandParent) &&
467 NodeUtil.isVarOrSimpleAssignLhs(n, parent)) {
468 String name = dest.getString();
469 Property prop = new AssignmentProperty(
470 grandParent,
471 maybeGetVar(t, root),
472 t.getModule());
473 getNameInfoForName(name, PROPERTY).getDeclarations().add(prop);
474 return true;
475 }
476 break;
477
478 // Foo.prototype = { "getBar" : function() { ... } }
479 case Token.ASSIGN:
480 Node map = n.getFirstChild().getNext();
481 if (map.isObjectLit()) {
482 for (Node key = map.getFirstChild();
483 key != null; key = key.getNext()) {
484 // May be STRING, GETTER_DEF, or SETTER_DEF,
485 String name = key.getString();
486 Property prop = new LiteralProperty(
487 key, key.getFirstChild(), map, n,
488 maybeGetVar(t, root),
489 t.getModule());
490 getNameInfoForName(name, PROPERTY).getDeclarations().add(prop);
491 }
492 return true;
493 }
494 break;
495 }
496 return false;
497 }
498
499 private Var maybeGetVar(NodeTraversal t, Node maybeName) {
500 return maybeName.isName()
501 ? t.getScope().getVar(maybeName.getString()) : null;
502 }
503
504 private void addGlobalUseOfSymbol(String name, JSModule module,
505 SymbolType type) {
506 symbolGraph.connect(globalNode, module, getNameInfoForName(name, type));
507 }
508 }
509
510 private class ProcessExternProperties extends AbstractPostOrderCallback {
511 @Override
512 public void visit(NodeTraversal t, Node n, Node parent) {
513 if (n.isGetProp()) {
514 symbolGraph.connect(externNode, firstModule,
515 getNameInfoForName(n.getLastChild().getString(), PROPERTY));
516 }
517 }
518 }
519
520 private class PropagateReferences
521 implements EdgeCallback<NameInfo, JSModule> {
522 @Override
523 public boolean traverseEdge(NameInfo start, JSModule edge, NameInfo dest) {
524 if (start.isReferenced()) {
525 JSModule startModule = start.getDeepestCommonModuleRef();
526 if (startModule != null &&
527 moduleGraph.dependsOn(startModule, edge)) {
528 return dest.markReference(startModule);
529 } else {
530 return dest.markReference(edge);
531 }
532 }
533 return false;
534 }
535 }
536
537 // TODO(user): We can use DefinitionsRemover and UseSite here. Then all
538 // we need to do is call getDefinition() and we'll magically know everything
539 // about the definition.
540
541 /**
542 * The declaration of an abstract symbol.
543 */
544 interface Symbol {
545 /**
546 * Remove the declaration from the AST.
547 */
548 void remove();
549
550 /**
551 * The variable for the root of this symbol.
552 */
553 Var getRootVar();
554
555 /**
556 * Returns the module where this appears.
557 */
558 JSModule getModule();
559 }
560
561 private enum SymbolType {
562 PROPERTY,
563 VAR;
564 }
565
566 /**
567 * A function initialized as a VAR statement or a function declaration.
568 */
569 class GlobalFunction implements Symbol {
570 private final Node nameNode;
571 private final Var var;
572 private final JSModule module;
573
574 GlobalFunction(Node nameNode, Var var, JSModule module) {
575 Node parent = nameNode.getParent();
576 Preconditions.checkState(
577 parent.isVar() ||
578 NodeUtil.isFunctionDeclaration(parent));
579 this.nameNode = nameNode;
580 this.var = var;
581 this.module = module;
582 }
583
584 @Override
585 public Var getRootVar() {
586 return var;
587 }
588
589 @Override
590 public void remove() {
591 Node parent = nameNode.getParent();
592 if (parent.isFunction() || parent.hasOneChild()) {
593 NodeUtil.removeChild(parent.getParent(), parent);
594 } else {
595 Preconditions.checkState(parent.isVar());
596 parent.removeChild(nameNode);
597 }
598 }
599
600 @Override
601 public JSModule getModule() {
602 return module;
603 }
604
605 public Node getFunctionNode() {
606 Node parent = nameNode.getParent();
607
608 if (parent.isFunction()) {
609 return parent;
610 } else {
611 // we are the name of a var node, so the function is name's second child
612 return nameNode.getChildAtIndex(1);
613 }
614 }
615 }
616
617 /**
618 * Since there are two ways of assigning properties to prototypes, we hide
619 * then behind this interface so they can both be removed regardless of type.
620 */
621 interface Property extends Symbol {
622
623 /** Returns the GETPROP node that refers to the prototype. */
624 Node getPrototype();
625
626 /** Returns the value of this property. */
627 Node getValue();
628 }
629
630 /**
631 * Properties created via EXPR assignment:
632 *
633 * <pre>function Foo() { ... };
634 * Foo.prototype.bar = function() { ... };</pre>
635 */
636 static class AssignmentProperty implements Property {
637 private final Node exprNode;
638 private final Var rootVar;
639 private final JSModule module;
640
641 /**
642 * @param node An EXPR node.
643 */
644 AssignmentProperty(Node node, Var rootVar, JSModule module) {
645 this.exprNode = node;
646 this.rootVar = rootVar;
647 this.module = module;
648 }
649
650 @Override
651 public Var getRootVar() {
652 return rootVar;
653 }
654
655 @Override
656 public void remove() {
657 NodeUtil.removeChild(exprNode.getParent(), exprNode);
658 }
659
660 @Override
661 public Node getPrototype() {
662 return getAssignNode().getFirstChild().getFirstChild();
663 }
664
665 @Override
666 public Node getValue() {
667 return getAssignNode().getLastChild();
668 }
669
670 private Node getAssignNode() {
671 return exprNode.getFirstChild();
672 }
673
674 @Override
675 public JSModule getModule() {
676 return module;
677 }
678 }
679
680 /**
681 * Properties created via object literals:
682 *
683 * <pre>function Foo() { ... };
684 * Foo.prototype = {bar: function() { ... };</pre>
685 */
686 static class LiteralProperty implements Property {
687 private final Node key;
688 private final Node value;
689 private final Node map;
690 private final Node assign;
691 private final Var rootVar;
692 private final JSModule module;
693
694 LiteralProperty(Node key, Node value, Node map, Node assign,
695 Var rootVar, JSModule module) {
696 this.key = key;
697 this.value = value;
698 this.map = map;
699 this.assign = assign;
700 this.rootVar = rootVar;
701 this.module = module;
702 }
703
704 @Override
705 public Var getRootVar() {
706 return rootVar;
707 }
708
709 @Override
710 public void remove() {
711 map.removeChild(key);
712 }
713
714 @Override
715 public Node getPrototype() {
716 return assign.getFirstChild();
717 }
718
719 @Override
720 public Node getValue() {
721 return value;
722 }
723
724 @Override
725 public JSModule getModule() {
726 return module;
727 }
728 }
729
730 /**
731 * The context of the current name. This includes the NameInfo and the scope
732 * if it is a scope defining name (function).
733 */
734 private class NameContext {
735 final NameInfo name;
736
737 // If this is a function context, then scope will be the scope of the
738 // corresponding function. Otherwise, it will be null.
739 final Scope scope;
740
741 NameContext(NameInfo name, Scope scope) {
742 this.name = name;
743 this.scope = scope;
744 }
745 }
746
747 /**
748 * Information on all properties or global variables of a given name.
749 */
750 class NameInfo {
751
752 final String name;
753
754 private boolean referenced = false;
755 private final Deque<Symbol> declarations = new ArrayDeque<Symbol>();
756 private JSModule deepestCommonModuleRef = null;
757
758 // True if this property is a function that reads a variable from an
759 // outer scope which isn't the global scope.
760 private boolean readClosureVariables = false;
761
762 /**
763 * Constructs a new NameInfo.
764 * @param name The name of the property that this represents. May be null
765 * to signify dummy nodes in the property graph.
766 */
767 NameInfo(String name) {
768 this.name = name;
769 }
770
771 @Override public String toString() { return name; }
772
773 /** Determines whether we've marked a reference to this property name. */
774 boolean isReferenced() {
775 return referenced;
776 }
777
778 /** Determines whether it reads a closure variable. */
779 boolean readsClosureVariables() {
780 return readClosureVariables;
781 }
782
783 /**
784 * Mark a reference in a given module to this property name, and record
785 * the deepest common module reference.
786 * @param module The module where it was referenced.
787 * @return Whether the name info has changed.
788 */
789 boolean markReference(JSModule module) {
790 boolean hasChanged = false;
791 if (!referenced) {
792 referenced = true;
793 hasChanged = true;
794 }
795
796 if (moduleGraph != null) {
797 JSModule originalDeepestCommon = deepestCommonModuleRef;
798
799 if (deepestCommonModuleRef == null) {
800 deepestCommonModuleRef = module;
801 } else {
802 deepestCommonModuleRef =
803 moduleGraph.getDeepestCommonDependencyInclusive(
804 deepestCommonModuleRef, module);
805 }
806
807 if (originalDeepestCommon != deepestCommonModuleRef) {
808 hasChanged = true;
809 }
810 }
811
812 return hasChanged;
813 }
814
815 /**
816 * Returns the deepest common module of all the references to this
817 * property.
818 */
819 JSModule getDeepestCommonModuleRef() {
820 return deepestCommonModuleRef;
821 }
822
823 /**
824 * Returns a mutable collection of all the prototype property declarations
825 * of this property name.
826 */
827 Deque<Symbol> getDeclarations() {
828 return declarations;
829 }
830 }
831 }