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 |
8 Identifiers, | 8 Identifiers, |
9 Selectors; | 9 Selectors; |
10 import '../compiler.dart' show | 10 import '../compiler.dart' show |
11 Compiler; | 11 Compiler; |
12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
13 import '../types/types.dart'; | 13 import '../types/types.dart'; |
14 import '../util/util.dart' show | 14 import '../util/util.dart' show |
15 Hashing, | 15 Hashing, |
16 Setlet; | 16 Setlet; |
17 import '../world.dart' show | 17 import '../world.dart' show |
18 ClassWorld; | 18 ClassWorld; |
19 | 19 |
20 import 'selector.dart' show | 20 import 'selector.dart' show |
21 Selector; | 21 Selector; |
22 import 'universe.dart' show | |
23 ReceiverConstraint; | |
22 | 24 |
23 // TODO(kasperl): This actually holds getters and setters just fine | 25 // TODO(kasperl): This actually holds getters and setters just fine |
24 // too and stricly they aren't functions. Maybe this needs a better | 26 // too and stricly they aren't functions. Maybe this needs a better |
25 // name -- something like ElementSet seems a bit too generic. | 27 // name -- something like ElementSet seems a bit too generic. |
26 class FunctionSet { | 28 class FunctionSet { |
27 final Compiler compiler; | 29 final Compiler compiler; |
28 final Map<String, FunctionSetNode> nodes = | 30 final Map<String, FunctionSetNode> nodes = |
29 new Map<String, FunctionSetNode>(); | 31 new Map<String, FunctionSetNode>(); |
30 FunctionSet(this.compiler); | 32 FunctionSet(this.compiler); |
31 | 33 |
(...skipping 25 matching lines...) Expand all Loading... | |
57 assert(!element.isAbstract); | 59 assert(!element.isAbstract); |
58 String name = element.name; | 60 String name = element.name; |
59 FunctionSetNode node = nodes[name]; | 61 FunctionSetNode node = nodes[name]; |
60 return (node != null) | 62 return (node != null) |
61 ? node.contains(element) | 63 ? node.contains(element) |
62 : false; | 64 : false; |
63 } | 65 } |
64 | 66 |
65 /// Returns an object that allows iterating over all the functions | 67 /// Returns an object that allows iterating over all the functions |
66 /// that may be invoked with the given [selector]. | 68 /// that may be invoked with the given [selector]. |
67 Iterable<Element> filter(Selector selector, TypeMask mask) { | 69 Iterable<Element> filter(Selector selector, ReceiverConstraint mask) { |
Siggi Cherem (dart-lang)
2015/11/12 01:10:59
rename all [mask] to [constraint]? or add TODO?
Harry Terkelsen
2015/11/12 01:25:40
Done.
| |
68 return query(selector, mask).functions; | 70 return query(selector, mask).functions; |
69 } | 71 } |
70 | 72 |
71 /// Returns the mask for the potential receivers of a dynamic call to | 73 /// Returns the mask for the potential receivers of a dynamic call to |
72 /// [selector] on [mask]. | 74 /// [selector] on [mask]. |
73 /// | 75 /// |
74 /// This will reduce the set of classes in [mask] to a [TypeMask] of the set | 76 /// This will narrow the constraints of [mask] to a [TypeMask] of the set |
75 /// of classes that actually implement the selected member or implement the | 77 /// of classes that actually implement the selected member or implement the |
76 /// handling 'noSuchMethod' where the selected member is unimplemented. | 78 /// handling 'noSuchMethod' where the selected member is unimplemented. |
77 TypeMask receiverType(Selector selector, TypeMask mask) { | 79 TypeMask receiverType(Selector selector, ReceiverConstraint mask) { |
78 return query(selector, mask).computeMask(classWorld); | 80 return query(selector, mask).computeMask(classWorld); |
79 } | 81 } |
80 | 82 |
81 SelectorMask _createSelectorMask( | 83 SelectorMask _createSelectorMask( |
82 Selector selector, TypeMask mask, ClassWorld classWorld) { | 84 Selector selector, ReceiverConstraint mask, ClassWorld classWorld) { |
83 return mask != null | 85 return mask != null |
84 ? new SelectorMask(selector, mask) | 86 ? new SelectorMask(selector, mask) |
85 : new SelectorMask(selector, | 87 : new SelectorMask(selector, |
86 new TypeMask.subclass(classWorld.objectClass, classWorld)); | 88 new TypeMask.subclass(classWorld.objectClass, classWorld)); |
87 } | 89 } |
88 | 90 |
89 /// Returns the set of functions that can be the target of a call to | 91 /// Returns the set of functions that can be the target of a call to |
90 /// [selector] on a receiver of type [mask] including 'noSuchMethod' methods | 92 /// [selector] on a receiver constrained by [mask] including 'noSuchMethod' |
91 /// where applicable. | 93 /// methods where applicable. |
92 FunctionSetQuery query(Selector selector, TypeMask mask) { | 94 FunctionSetQuery query(Selector selector, ReceiverConstraint mask) { |
93 String name = selector.name; | 95 String name = selector.name; |
94 SelectorMask selectorMask = _createSelectorMask(selector, mask, classWorld); | 96 SelectorMask selectorMask = _createSelectorMask(selector, mask, classWorld); |
95 SelectorMask noSuchMethodMask = | 97 SelectorMask noSuchMethodMask = |
96 new SelectorMask(Selectors.noSuchMethod_, selectorMask.mask); | 98 new SelectorMask(Selectors.noSuchMethod_, selectorMask.mask); |
97 FunctionSetNode node = nodes[name]; | 99 FunctionSetNode node = nodes[name]; |
98 FunctionSetNode noSuchMethods = nodes[Identifiers.noSuchMethod_]; | 100 FunctionSetNode noSuchMethods = nodes[Identifiers.noSuchMethod_]; |
99 if (node != null) { | 101 if (node != null) { |
100 return node.query( | 102 return node.query( |
101 selectorMask, classWorld, noSuchMethods, noSuchMethodMask); | 103 selectorMask, classWorld, noSuchMethods, noSuchMethodMask); |
102 } | 104 } |
103 // If there is no method that matches [selector] we know we can | 105 // If there is no method that matches [selector] we know we can |
104 // only hit [:noSuchMethod:]. | 106 // only hit [:noSuchMethod:]. |
105 if (noSuchMethods == null) { | 107 if (noSuchMethods == null) { |
106 return const EmptyFunctionSetQuery(); | 108 return const EmptyFunctionSetQuery(); |
107 } | 109 } |
108 return noSuchMethods.query(noSuchMethodMask, classWorld); | 110 return noSuchMethods.query(noSuchMethodMask, classWorld); |
109 } | 111 } |
110 | 112 |
111 void forEach(Function action) { | 113 void forEach(Function action) { |
112 nodes.forEach((String name, FunctionSetNode node) { | 114 nodes.forEach((String name, FunctionSetNode node) { |
113 node.forEach(action); | 115 node.forEach(action); |
114 }); | 116 }); |
115 } | 117 } |
116 } | 118 } |
117 | 119 |
118 /// A selector/mask pair representing the dynamic invocation of [selector] on | 120 /// A selector/mask pair representing the dynamic invocation of [selector] on |
119 /// a receiver of type [mask]. | 121 /// a receiver of type [mask]. |
120 class SelectorMask { | 122 class SelectorMask { |
121 final Selector selector; | 123 final Selector selector; |
122 final TypeMask mask; | 124 final ReceiverConstraint mask; |
123 final int hashCode; | 125 final int hashCode; |
124 | 126 |
125 SelectorMask(Selector selector, TypeMask mask) | 127 SelectorMask(Selector selector, ReceiverConstraint mask) |
126 : this.selector = selector, | 128 : this.selector = selector, |
127 this.mask = mask, | 129 this.mask = mask, |
128 this.hashCode = | 130 this.hashCode = |
129 Hashing.mixHashCodeBits(selector.hashCode, mask.hashCode) { | 131 Hashing.mixHashCodeBits(selector.hashCode, mask.hashCode) { |
130 assert(mask != null); | 132 assert(mask != null); |
131 } | 133 } |
132 | 134 |
133 String get name => selector.name; | 135 String get name => selector.name; |
134 | 136 |
135 bool applies(Element element, ClassWorld classWorld) { | 137 bool applies(Element element, ClassWorld classWorld) { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 } else if (classWorld.isInstantiated(cls.declaration)) { | 303 } else if (classWorld.isInstantiated(cls.declaration)) { |
302 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); | 304 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); |
303 } else { | 305 } else { |
304 // TODO(johnniwinther): Avoid the need for this case. | 306 // TODO(johnniwinther): Avoid the need for this case. |
305 return const TypeMask.empty(); | 307 return const TypeMask.empty(); |
306 } | 308 } |
307 }), | 309 }), |
308 classWorld); | 310 classWorld); |
309 } | 311 } |
310 } | 312 } |
OLD | NEW |