OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of universe; | 5 part of universe; |
6 | 6 |
7 // TODO(kasperl): This actually holds getters and setters just fine | 7 // TODO(kasperl): This actually holds getters and setters just fine |
8 // too and stricly they aren't functions. Maybe this needs a better | 8 // too and stricly they aren't functions. Maybe this needs a better |
9 // name -- something like ElementSet seems a bit too generic. | 9 // name -- something like ElementSet seems a bit too generic. |
10 class FunctionSet { | 10 class FunctionSet { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 return (node != null) | 42 return (node != null) |
43 ? node.contains(element) | 43 ? node.contains(element) |
44 : false; | 44 : false; |
45 } | 45 } |
46 | 46 |
47 /** | 47 /** |
48 * Returns an object that allows iterating over all the functions | 48 * Returns an object that allows iterating over all the functions |
49 * that may be invoked with the given [selector]. | 49 * that may be invoked with the given [selector]. |
50 */ | 50 */ |
51 Iterable<Element> filter(Selector selector) { | 51 Iterable<Element> filter(Selector selector) { |
| 52 return query(selector).functions; |
| 53 } |
| 54 |
| 55 TypeMask receiverType(Selector selector) { |
| 56 return query(selector).computeMask(compiler); |
| 57 } |
| 58 |
| 59 FunctionSetQuery query(Selector selector) { |
52 SourceString name = selector.name; | 60 SourceString name = selector.name; |
53 FunctionSetNode node = nodes[name]; | 61 FunctionSetNode node = nodes[name]; |
54 FunctionSetNode noSuchMethods = nodes[Compiler.NO_SUCH_METHOD]; | 62 FunctionSetNode noSuchMethods = nodes[Compiler.NO_SUCH_METHOD]; |
55 if (node != null) { | 63 if (node != null) { |
56 return node.query(selector, compiler, noSuchMethods).functions; | 64 return node.query(selector, compiler, noSuchMethods); |
57 } | 65 } |
58 // If there is no method that matches [selector] we know we can | 66 // If there is no method that matches [selector] we know we can |
59 // only hit [:noSuchMethod:]. | 67 // only hit [:noSuchMethod:]. |
60 if (noSuchMethods == null) return const <Element>[]; | 68 if (noSuchMethods == null) return const FunctionSetQuery(const <Element>[]); |
61 selector = (selector.mask == null) | 69 selector = (selector.mask == null) |
62 ? compiler.noSuchMethodSelector | 70 ? compiler.noSuchMethodSelector |
63 : new TypedSelector(selector.mask, compiler.noSuchMethodSelector); | 71 : new TypedSelector(selector.mask, compiler.noSuchMethodSelector); |
64 | 72 |
65 return noSuchMethods.query(selector, compiler, null).functions; | 73 return noSuchMethods.query(selector, compiler, null); |
66 } | 74 } |
67 | 75 |
68 void forEach(Function action) { | 76 void forEach(Function action) { |
69 nodes.forEach((SourceString name, FunctionSetNode node) { | 77 nodes.forEach((SourceString name, FunctionSetNode node) { |
70 node.forEach(action); | 78 node.forEach(action); |
71 }); | 79 }); |
72 } | 80 } |
73 } | 81 } |
74 | 82 |
75 | 83 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 187 } |
180 cache[selector] = result = (functions != null) | 188 cache[selector] = result = (functions != null) |
181 ? newQuery(functions, selector, compiler) | 189 ? newQuery(functions, selector, compiler) |
182 : const FunctionSetQuery(const <Element>[]); | 190 : const FunctionSetQuery(const <Element>[]); |
183 return result; | 191 return result; |
184 } | 192 } |
185 | 193 |
186 FunctionSetQuery newQuery(Iterable<Element> functions, | 194 FunctionSetQuery newQuery(Iterable<Element> functions, |
187 Selector selector, | 195 Selector selector, |
188 Compiler compiler) { | 196 Compiler compiler) { |
189 return new FunctionSetQuery(functions); | 197 return new FullFunctionSetQuery(functions); |
190 } | 198 } |
191 } | 199 } |
192 | 200 |
193 class FunctionSetQuery { | 201 class FunctionSetQuery { |
194 final Iterable<Element> functions; | 202 final Iterable<Element> functions; |
| 203 TypeMask computeMask(Compiler compiler) => const TypeMask.nonNullEmpty(); |
195 const FunctionSetQuery(this.functions); | 204 const FunctionSetQuery(this.functions); |
196 } | 205 } |
| 206 |
| 207 class FullFunctionSetQuery extends FunctionSetQuery { |
| 208 TypeMask _mask; |
| 209 |
| 210 /** |
| 211 * Compute the type of all potential receivers of this function set. |
| 212 */ |
| 213 TypeMask computeMask(Compiler compiler) { |
| 214 if (_mask != null) return _mask; |
| 215 return _mask = new TypeMask.unionOf(functions |
| 216 .expand((element) { |
| 217 ClassElement cls = element.getEnclosingClass(); |
| 218 return compiler.world.isUsedAsMixin(cls) |
| 219 ? ([cls]..addAll(compiler.world.mixinUses[cls])) |
| 220 : [cls]; |
| 221 }) |
| 222 .map((cls) { |
| 223 return compiler.world.hasSubclasses(cls) |
| 224 ? new TypeMask.nonNullSubclass(cls.rawType) |
| 225 : new TypeMask.nonNullExact(cls.rawType); |
| 226 }), |
| 227 compiler); |
| 228 } |
| 229 |
| 230 FullFunctionSetQuery(functions) : super(functions); |
| 231 } |
OLD | NEW |