Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.LinkedHashMultimap;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Multimap;
22 import com.google.common.collect.Sets;
23 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
24 import com.google.javascript.jscomp.NodeTraversal.Callback;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.Token;
27
28 import java.util.List;
29 import java.util.Set;
30
31 /**
32 * Finds all method declarations and pulls them into data structures
33 * for use during cleanups such as arity checks or inlining.
34 *
35 */
36 abstract class MethodCompilerPass implements CompilerPass {
37 /** List of methods defined in externs */
38 final Set<String> externMethods = Sets.newHashSet();
39
40 /** List of extern methods without signatures that we can't warn about */
41 final Set<String> externMethodsWithoutSignatures = Sets.newHashSet();
42
43 /** List of property names that may not be methods */
44 final Set<String> nonMethodProperties = Sets.newHashSet();
45
46 // Use a linked map here to keep the output deterministic. Otherwise,
47 // the choice of method bodies is random when multiple identical definitions
48 // are found which causes problems in the source maps.
49 final Multimap<String, Node> methodDefinitions =
50 LinkedHashMultimap.create();
51
52 final AbstractCompiler compiler;
53
54 /**
55 * The signature storage is provided by the implementing class.
56 */
57 interface SignatureStore {
58 public void reset();
59 public void addSignature(
60 String functionName, Node functionNode, String sourceFile);
61 public void removeSignature(String functionName);
62 }
63
64 MethodCompilerPass(AbstractCompiler compiler) {
65 this.compiler = compiler;
66 }
67
68 @Override
69 public void process(Node externs, Node root) {
70 externMethods.clear();
71 externMethodsWithoutSignatures.clear();
72 getSignatureStore().reset();
73 methodDefinitions.clear();
74
75 if (externs != null) {
76 NodeTraversal.traverse(compiler, externs, new GetExternMethods());
77 }
78
79
80 List<Node> externsAndJs = Lists.newArrayList(externs, root);
81 NodeTraversal.traverseRoots(
82 compiler, Lists.newArrayList(externs, root), new GatherSignatures());
83 NodeTraversal.traverseRoots(
84 compiler, externsAndJs, getActingCallback());
85 }
86
87 /**
88 * Subclasses should return a callback that does the actual work they
89 * want to perform given the computed list of method signatures
90 */
91 abstract Callback getActingCallback();
92
93 /**
94 * Subclasses should return a SignatureStore for storing discovered
95 * signatures.
96 */
97 abstract SignatureStore getSignatureStore();
98
99 /**
100 * Adds a node that may represent a function signature (if it's a function
101 * itself or the name of a function).
102 */
103 private void addPossibleSignature(String name, Node node, NodeTraversal t) {
104 if (node.isFunction()) {
105 // The node we're looking at is a function, so we can add it directly
106 addSignature(name, node, t.getSourceName());
107 } else {
108 nonMethodProperties.add(name);
109 }
110 }
111
112 private void addSignature(String name, Node function, String fnSourceName) {
113 if (externMethodsWithoutSignatures.contains(name)) {
114 return;
115 }
116
117 getSignatureStore().addSignature(name, function, fnSourceName);
118 methodDefinitions.put(name, function);
119 }
120
121 /**
122 * Gathers methods from the externs file. Methods that are listed there but
123 * do not have a signature are flagged to be ignored when doing arity checks.
124 * Methods that do include signatures will be checked.
125 */
126 private class GetExternMethods extends AbstractPostOrderCallback {
127
128 @Override
129 public void visit(NodeTraversal t, Node n, Node parent) {
130 switch (n.getType()) {
131 case Token.GETPROP:
132 case Token.GETELEM: {
133 Node dest = n.getFirstChild().getNext();
134
135 if (!dest.isString()) {
136 return;
137 }
138
139 String name = dest.getString();
140
141 // We have a signature. Parse tree of the form:
142 // assign <- parent
143 // getprop <- n
144 // name methods
145 // string setTimeout
146 // function
147 if (parent.isAssign() &&
148 parent.getFirstChild() == n &&
149 n.getNext().isFunction()) {
150 addSignature(name, n.getNext(), t.getSourceName());
151 } else {
152 getSignatureStore().removeSignature(name);
153 externMethodsWithoutSignatures.add(name);
154 }
155
156 externMethods.add(name);
157 } break;
158
159 case Token.OBJECTLIT: {
160 for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
161 Node value = key.getFirstChild();
162 String name = key.getString();
163 if (key.isStringKey()
164 && value.isFunction()) {
165 addSignature(name, value, t.getSourceName());
166 } else {
167 getSignatureStore().removeSignature(name);
168 externMethodsWithoutSignatures.add(name);
169 }
170 externMethods.add(name);
171 }
172 } break;
173 }
174 }
175 }
176
177 /**
178 * Gather signatures from the source to be compiled.
179 */
180 private class GatherSignatures extends AbstractPostOrderCallback {
181
182 @Override
183 public void visit(NodeTraversal t, Node n, Node parent) {
184 switch (n.getType()) {
185 case Token.GETPROP:
186 case Token.GETELEM:
187 Node dest = n.getFirstChild().getNext();
188
189 if (dest.isString()) {
190 if (dest.getString().equals("prototype")) {
191 processPrototypeParent(t, parent);
192 } else {
193 // Static methods of the form Foo.bar = function() {} or
194 // Static methods of the form Foo.bar = baz (where baz is a
195 // function name). Parse tree looks like:
196 // assign <- parent
197 // getprop <- n
198 // name Foo
199 // string bar
200 // function or name <- n.getNext()
201 if (parent.isAssign() &&
202 parent.getFirstChild() == n) {
203 addPossibleSignature(dest.getString(), n.getNext(), t);
204 }
205 }
206 }
207 break;
208
209 case Token.OBJECTLIT:
210 for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
211 switch(key.getType()) {
212 case Token.STRING_KEY:
213 addPossibleSignature(key.getString(), key.getFirstChild(), t);
214 break;
215 case Token.SETTER_DEF:
216 case Token.GETTER_DEF:
217 nonMethodProperties.add(key.getString());
218 break;
219 default:
220 throw new IllegalStateException(
221 "unexpect OBJECTLIT key: " + key);
222 }
223 }
224 break;
225 }
226 }
227
228 /**
229 * Processes the parent of a GETPROP prototype, which can either be
230 * another GETPROP (in the case of Foo.prototype.bar), or can be
231 * an assignment (in the case of Foo.prototype = ...).
232 */
233 private void processPrototypeParent(NodeTraversal t, Node n) {
234 switch (n.getType()) {
235 // Foo.prototype.getBar = function() { ... } or
236 // Foo.prototype.getBar = getBaz (where getBaz is a function)
237 // parse tree looks like:
238 // assign <- parent
239 // getprop <- n
240 // getprop
241 // name Foo
242 // string prototype
243 // string getBar
244 // function or name <- assignee
245 case Token.GETPROP:
246 case Token.GETELEM:
247 Node dest = n.getFirstChild().getNext();
248 Node parent = n.getParent().getParent();
249
250 if (dest.isString() &&
251 parent.isAssign()) {
252 Node assignee = parent.getFirstChild().getNext();
253
254 addPossibleSignature(dest.getString(), assignee, t);
255 }
256 break;
257 }
258 }
259 }
260 }