| 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 library universe.function_set; | 5 library universe.function_set; |
| 6 | 6 |
| 7 import '../common/names.dart' show | 7 import '../common/names.dart' show Identifiers, Selectors; |
| 8 Identifiers, | 8 import '../compiler.dart' show Compiler; |
| 9 Selectors; | |
| 10 import '../compiler.dart' show | |
| 11 Compiler; | |
| 12 import '../elements/elements.dart'; | 9 import '../elements/elements.dart'; |
| 13 import '../types/types.dart'; | 10 import '../types/types.dart'; |
| 14 import '../util/util.dart' show | 11 import '../util/util.dart' show Hashing, Setlet; |
| 15 Hashing, | 12 import '../world.dart' show ClassWorld; |
| 16 Setlet; | |
| 17 import '../world.dart' show | |
| 18 ClassWorld; | |
| 19 | 13 |
| 20 import 'selector.dart' show | 14 import 'selector.dart' show Selector; |
| 21 Selector; | 15 import 'universe.dart' show ReceiverConstraint; |
| 22 import 'universe.dart' show | |
| 23 ReceiverConstraint; | |
| 24 | 16 |
| 25 // TODO(kasperl): This actually holds getters and setters just fine | 17 // TODO(kasperl): This actually holds getters and setters just fine |
| 26 // too and stricly they aren't functions. Maybe this needs a better | 18 // too and stricly they aren't functions. Maybe this needs a better |
| 27 // name -- something like ElementSet seems a bit too generic. | 19 // name -- something like ElementSet seems a bit too generic. |
| 28 class FunctionSet { | 20 class FunctionSet { |
| 29 final Compiler compiler; | 21 final Compiler compiler; |
| 30 final Map<String, FunctionSetNode> nodes = | 22 final Map<String, FunctionSetNode> nodes = new Map<String, FunctionSetNode>(); |
| 31 new Map<String, FunctionSetNode>(); | |
| 32 FunctionSet(this.compiler); | 23 FunctionSet(this.compiler); |
| 33 | 24 |
| 34 ClassWorld get classWorld => compiler.world; | 25 ClassWorld get classWorld => compiler.world; |
| 35 | 26 |
| 36 FunctionSetNode newNode(String name) | 27 FunctionSetNode newNode(String name) => new FunctionSetNode(name); |
| 37 => new FunctionSetNode(name); | |
| 38 | 28 |
| 39 void add(Element element) { | 29 void add(Element element) { |
| 40 assert(element.isInstanceMember); | 30 assert(element.isInstanceMember); |
| 41 assert(!element.isAbstract); | 31 assert(!element.isAbstract); |
| 42 String name = element.name; | 32 String name = element.name; |
| 43 FunctionSetNode node = nodes.putIfAbsent(name, () => newNode(name)); | 33 FunctionSetNode node = nodes.putIfAbsent(name, () => newNode(name)); |
| 44 node.add(element); | 34 node.add(element); |
| 45 } | 35 } |
| 46 | 36 |
| 47 void remove(Element element) { | 37 void remove(Element element) { |
| 48 assert(element.isInstanceMember); | 38 assert(element.isInstanceMember); |
| 49 assert(!element.isAbstract); | 39 assert(!element.isAbstract); |
| 50 String name = element.name; | 40 String name = element.name; |
| 51 FunctionSetNode node = nodes[name]; | 41 FunctionSetNode node = nodes[name]; |
| 52 if (node != null) { | 42 if (node != null) { |
| 53 node.remove(element); | 43 node.remove(element); |
| 54 } | 44 } |
| 55 } | 45 } |
| 56 | 46 |
| 57 bool contains(Element element) { | 47 bool contains(Element element) { |
| 58 assert(element.isInstanceMember); | 48 assert(element.isInstanceMember); |
| 59 assert(!element.isAbstract); | 49 assert(!element.isAbstract); |
| 60 String name = element.name; | 50 String name = element.name; |
| 61 FunctionSetNode node = nodes[name]; | 51 FunctionSetNode node = nodes[name]; |
| 62 return (node != null) | 52 return (node != null) ? node.contains(element) : false; |
| 63 ? node.contains(element) | |
| 64 : false; | |
| 65 } | 53 } |
| 66 | 54 |
| 67 /// Returns an object that allows iterating over all the functions | 55 /// Returns an object that allows iterating over all the functions |
| 68 /// that may be invoked with the given [selector]. | 56 /// that may be invoked with the given [selector]. |
| 69 Iterable<Element> filter(Selector selector, ReceiverConstraint constraint) { | 57 Iterable<Element> filter(Selector selector, ReceiverConstraint constraint) { |
| 70 return query(selector, constraint).functions; | 58 return query(selector, constraint).functions; |
| 71 } | 59 } |
| 72 | 60 |
| 73 /// Returns the mask for the potential receivers of a dynamic call to | 61 /// Returns the mask for the potential receivers of a dynamic call to |
| 74 /// [selector] on [constraint]. | 62 /// [selector] on [constraint]. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 assert(element.name == name); | 197 assert(element.name == name); |
| 210 return elements.contains(element); | 198 return elements.contains(element); |
| 211 } | 199 } |
| 212 | 200 |
| 213 void forEach(Function action) { | 201 void forEach(Function action) { |
| 214 elements.forEach(action); | 202 elements.forEach(action); |
| 215 } | 203 } |
| 216 | 204 |
| 217 /// Returns the set of functions that can be the target of [selectorMask] | 205 /// Returns the set of functions that can be the target of [selectorMask] |
| 218 /// including no such method handling where applicable. | 206 /// including no such method handling where applicable. |
| 219 FunctionSetQuery query(SelectorMask selectorMask, | 207 FunctionSetQuery query(SelectorMask selectorMask, ClassWorld classWorld, |
| 220 ClassWorld classWorld, | 208 [FunctionSetNode noSuchMethods, SelectorMask noSuchMethodMask]) { |
| 221 [FunctionSetNode noSuchMethods, | |
| 222 SelectorMask noSuchMethodMask]) { | |
| 223 assert(selectorMask.name == name); | 209 assert(selectorMask.name == name); |
| 224 FunctionSetQuery result = cache[selectorMask]; | 210 FunctionSetQuery result = cache[selectorMask]; |
| 225 if (result != null) return result; | 211 if (result != null) return result; |
| 226 | 212 |
| 227 Setlet<Element> functions; | 213 Setlet<Element> functions; |
| 228 for (Element element in elements) { | 214 for (Element element in elements) { |
| 229 if (selectorMask.applies(element, classWorld)) { | 215 if (selectorMask.applies(element, classWorld)) { |
| 230 if (functions == null) { | 216 if (functions == null) { |
| 231 // Defer the allocation of the functions set until we are | 217 // Defer the allocation of the functions set until we are |
| 232 // sure we need it. This allows us to return immutable empty | 218 // sure we need it. This allows us to return immutable empty |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 final Iterable<Element> functions; | 272 final Iterable<Element> functions; |
| 287 | 273 |
| 288 TypeMask _mask; | 274 TypeMask _mask; |
| 289 | 275 |
| 290 FullFunctionSetQuery(this.functions); | 276 FullFunctionSetQuery(this.functions); |
| 291 | 277 |
| 292 @override | 278 @override |
| 293 TypeMask computeMask(ClassWorld classWorld) { | 279 TypeMask computeMask(ClassWorld classWorld) { |
| 294 assert(classWorld.hasAnyStrictSubclass(classWorld.objectClass)); | 280 assert(classWorld.hasAnyStrictSubclass(classWorld.objectClass)); |
| 295 if (_mask != null) return _mask; | 281 if (_mask != null) return _mask; |
| 296 return _mask = new TypeMask.unionOf(functions | 282 return _mask = new TypeMask.unionOf( |
| 297 .expand((element) { | 283 functions.expand((element) { |
| 298 ClassElement cls = element.enclosingClass; | 284 ClassElement cls = element.enclosingClass; |
| 299 return [cls]..addAll(classWorld.mixinUsesOf(cls)); | 285 return [cls]..addAll(classWorld.mixinUsesOf(cls)); |
| 300 }) | 286 }).map((cls) { |
| 301 .map((cls) { | |
| 302 if (classWorld.backend.isNullImplementation(cls)) { | 287 if (classWorld.backend.isNullImplementation(cls)) { |
| 303 return const TypeMask.empty(); | 288 return const TypeMask.empty(); |
| 304 } else if (classWorld.isInstantiated(cls.declaration)) { | 289 } else if (classWorld.isInstantiated(cls.declaration)) { |
| 305 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); | 290 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); |
| 306 } else { | 291 } else { |
| 307 // TODO(johnniwinther): Avoid the need for this case. | 292 // TODO(johnniwinther): Avoid the need for this case. |
| 308 return const TypeMask.empty(); | 293 return const TypeMask.empty(); |
| 309 } | 294 } |
| 310 }), | 295 }), |
| 311 classWorld); | 296 classWorld); |
| 312 } | 297 } |
| 313 } | 298 } |
| OLD | NEW |