Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.Maps;
21 import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection;
22 import com.google.javascript.jscomp.Scope.Var;
23 import com.google.javascript.rhino.Node;
24 import java.util.Map;
25
26 /**
27 * An analysis pass that determines the visibility of variables -- that is,
28 * whether a variable is truly local, a local captured by an inner scope, a
29 * parameter, or a global variable.
30 *
31 * SideEffectsAnalysis uses this class to partition a potentially infinite
32 * number of concrete storage locations into a (small) finite number of
33 * abstract storage locations based on a variable's storage visibility.
34 *
35 * @author [email protected] (Devin Coughlin)
36 */
37 class VariableVisibilityAnalysis implements CompilerPass {
38
39 enum VariableVisibility {
40
41 /** Local variable, not captured by closure */
42 LOCAL,
43
44 /** Local variable captured by a closure */
45 CAPTURED_LOCAL,
46
47 /**
48 * Formal parameter declaration variable
49 *
50 * Parameters are different than local variables because they can be
51 * aliased by elements of the arguments object.
52 */
53 PARAMETER,
54
55 /** A global variable */
56 GLOBAL
57 }
58
59 private AbstractCompiler compiler;
60
61 /**
62 * Maps the declaring name node for a variable to that variable's
63 * visibility.
64 */
65 private Map<Node, VariableVisibility> visibilityByDeclaringNameNode;
66
67 public VariableVisibilityAnalysis(AbstractCompiler compiler) {
68 this.compiler = compiler;
69
70 visibilityByDeclaringNameNode = Maps.newHashMap();
71 }
72
73 /**
74 * Returns the visibility of of a variable, given that variable's declaring
75 * name node.
76 *
77 * The name node's parent must be one of:
78 * <pre>
79 * Token.VAR (for a variable declaration)
80 * Token.FUNCTION (for a function declaration)
81 * Token.PARAM_LIST (for a function formal parameter)
82 * </pre>
83 *
84 * The returned visibility will be one of:
85 * <pre>
86 * LOCAL_VARIABLE : the variable is a local variable used only in its
87 * declared scope
88 * CAPTURED_LOCAL_VARIABLE : A local variable that is used in a capturing
89 * closure
90 * PARAMETER_VARIABLE : the variable is a formal parameter
91 * GLOBAL_VARIABLE : the variable is declared in the global scope
92 * </pre>
93 *
94 * @param declaringNameNode The name node for a declaration.
95 */
96 public VariableVisibility getVariableVisibility(Node declaringNameNode) {
97 Node parent = declaringNameNode.getParent();
98
99 Preconditions.checkArgument(parent.isVar()
100 || parent.isFunction()
101 || parent.isParamList());
102
103 return visibilityByDeclaringNameNode.get(declaringNameNode);
104 }
105
106 /**
107 * Determines the visibility class for each variable in root.
108 */
109 @Override
110 public void process(Node externs, Node root) {
111 ReferenceCollectingCallback callback =
112 new ReferenceCollectingCallback(compiler,
113 ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR);
114
115 NodeTraversal.traverse(compiler, root, callback);
116
117 for (Var variable : callback.getAllSymbols()) {
118 ReferenceCollection referenceCollection =
119 callback.getReferences(variable);
120
121 VariableVisibility visibility;
122
123 if (variableIsParameter(variable)) {
124 visibility = VariableVisibility.PARAMETER;
125 } else if (variable.isLocal()) {
126 if (referenceCollection.isEscaped()) {
127 visibility = VariableVisibility.CAPTURED_LOCAL;
128 } else {
129 visibility = VariableVisibility.LOCAL;
130 }
131 } else if (variable.isGlobal()) {
132 visibility = VariableVisibility.GLOBAL;
133 } else {
134 throw new IllegalStateException("Un-handled variable visibility for " +
135 variable);
136 }
137
138 visibilityByDeclaringNameNode.put(variable.getNameNode(), visibility);
139 }
140 }
141
142 /**
143 * Returns true if the variable is a formal parameter.
144 */
145 private static boolean variableIsParameter(Var variable) {
146 Node variableParent = variable.getParentNode();
147
148 return variableParent != null && variableParent.isParamList();
149 }
150 }