Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.collect.Sets;
20 import com.google.javascript.jscomp.NameReferenceGraph.Name;
21 import com.google.javascript.jscomp.NameReferenceGraph.Reference;
22 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal;
23 import com.google.javascript.jscomp.graph.Annotation;
24 import com.google.javascript.jscomp.graph.GraphNode;
25 import com.google.javascript.jscomp.graph.FixedPointGraphTraversal.EdgeCallback;
26 import com.google.javascript.rhino.Node;
27
28 /**
29 * Analyzes names and references usage by determining:
30 * <p><ol>
31 * <li>If the name is reachable from the {@link NameReferenceGraph#MAIN}.</li>
32 * <li>as well as the deepest common module that references it.</li>
33 * </ol>
34 *
35 * The two pieces of information will be annotated to {@link NameReferenceGraph}
36 * by {@link NameInfo} objects.
37 *
38 * This is an analysis based on {@link AnalyzeNameReferences} using the more
39 * accurate graph and will soon replace it.
40 *
41 */
42 class AnalyzeNameReferences implements CompilerPass {
43
44 private NameReferenceGraph graph;
45 private final JSModuleGraph moduleGraph;
46 private final AbstractCompiler compiler;
47
48 AnalyzeNameReferences(AbstractCompiler compiler) {
49 this.compiler = compiler;
50 this.moduleGraph = compiler.getModuleGraph();
51 }
52
53 @Override
54 public void process(Node externs, Node root) {
55 NameReferenceGraphConstruction gc =
56 new NameReferenceGraphConstruction(compiler);
57 gc.process(externs, root);
58 graph = gc.getNameReferenceGraph();
59 FixedPointGraphTraversal<Name, Reference> t =
60 FixedPointGraphTraversal.newTraversal(new PropagateReferences());
61 getInfo(graph.MAIN).markReference(null);
62 t.computeFixedPoint(graph, Sets.newHashSet(graph.MAIN));
63 }
64
65 public NameReferenceGraph getGraph() {
66 return graph;
67 }
68
69 private class PropagateReferences implements EdgeCallback<Name, Reference> {
70 @Override
71 public boolean traverseEdge(Name start, Reference edge, Name dest) {
72 NameInfo startInfo = getInfo(start);
73 NameInfo destInfo = getInfo(dest);
74 if (startInfo.isReferenced()) {
75 JSModule startModule = startInfo.getDeepestCommonModuleRef();
76 if (startModule != null &&
77 moduleGraph.dependsOn(startModule, edge.getModule())) {
78 return destInfo.markReference(startModule);
79 } else {
80 return destInfo.markReference(edge.getModule());
81 }
82 }
83 return false;
84 }
85 }
86
87 private NameInfo getInfo(Name symbol) {
88 GraphNode<Name, Reference> name = graph.getNode(symbol);
89 NameInfo info = name.getAnnotation();
90 if (info == null) {
91 info = new NameInfo();
92 name.setAnnotation(info);
93 }
94 return info;
95 }
96
97 final class NameInfo implements Annotation {
98 private boolean referenced = false;
99 private JSModule deepestCommonModuleRef = null;
100
101 /** Determines whether we've marked a reference to this property name. */
102 boolean isReferenced() {
103 return referenced;
104 }
105
106 /**
107 * Returns the deepest common module of all the references to this
108 * property.
109 */
110 JSModule getDeepestCommonModuleRef() {
111 return deepestCommonModuleRef;
112 }
113
114 /**
115 * Mark a reference in a given module to this property name, and record
116 * the deepest common module reference.
117 * @param module The module where it was referenced.
118 * @return Whether the name info has changed.
119 */
120 boolean markReference(JSModule module) {
121 boolean hasChanged = false;
122 if (!referenced) {
123 referenced = true;
124 hasChanged = true;
125 }
126
127 if (moduleGraph != null) {
128 JSModule originalDeepestCommon = deepestCommonModuleRef;
129
130 if (deepestCommonModuleRef == null) {
131 deepestCommonModuleRef = module;
132 } else {
133 deepestCommonModuleRef =
134 moduleGraph.getDeepestCommonDependencyInclusive(
135 deepestCommonModuleRef, module);
136 }
137
138 if (originalDeepestCommon != deepestCommonModuleRef) {
139 hasChanged = true;
140 }
141 }
142 return hasChanged;
143 }
144 }
145 }