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 constraint) { |
68 return query(selector, mask).functions; | 70 return query(selector, constraint).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 [constraint]. |
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 [constraint] to a [TypeMask] of the |
75 /// of classes that actually implement the selected member or implement the | 77 /// set of classes that actually implement the selected member or implement |
76 /// handling 'noSuchMethod' where the selected member is unimplemented. | 78 /// the handling 'noSuchMethod' where the selected member is unimplemented. |
77 TypeMask receiverType(Selector selector, TypeMask mask) { | 79 TypeMask receiverType(Selector selector, ReceiverConstraint constraint) { |
78 return query(selector, mask).computeMask(classWorld); | 80 return query(selector, constraint).computeMask(classWorld); |
79 } | 81 } |
80 | 82 |
81 SelectorMask _createSelectorMask( | 83 SelectorMask _createSelectorMask( |
82 Selector selector, TypeMask mask, ClassWorld classWorld) { | 84 Selector selector, ReceiverConstraint constraint, ClassWorld classWorld) { |
83 return mask != null | 85 return constraint != null |
84 ? new SelectorMask(selector, mask) | 86 ? new SelectorMask(selector, constraint) |
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 [constraint] including |
91 /// where applicable. | 93 /// 'noSuchMethod' methods where applicable. |
92 FunctionSetQuery query(Selector selector, TypeMask mask) { | 94 FunctionSetQuery query(Selector selector, ReceiverConstraint constraint) { |
93 String name = selector.name; | 95 String name = selector.name; |
94 SelectorMask selectorMask = _createSelectorMask(selector, mask, classWorld); | 96 SelectorMask selectorMask = |
| 97 _createSelectorMask(selector, constraint, classWorld); |
95 SelectorMask noSuchMethodMask = | 98 SelectorMask noSuchMethodMask = |
96 new SelectorMask(Selectors.noSuchMethod_, selectorMask.mask); | 99 new SelectorMask(Selectors.noSuchMethod_, selectorMask.constraint); |
97 FunctionSetNode node = nodes[name]; | 100 FunctionSetNode node = nodes[name]; |
98 FunctionSetNode noSuchMethods = nodes[Identifiers.noSuchMethod_]; | 101 FunctionSetNode noSuchMethods = nodes[Identifiers.noSuchMethod_]; |
99 if (node != null) { | 102 if (node != null) { |
100 return node.query( | 103 return node.query( |
101 selectorMask, classWorld, noSuchMethods, noSuchMethodMask); | 104 selectorMask, classWorld, noSuchMethods, noSuchMethodMask); |
102 } | 105 } |
103 // If there is no method that matches [selector] we know we can | 106 // If there is no method that matches [selector] we know we can |
104 // only hit [:noSuchMethod:]. | 107 // only hit [:noSuchMethod:]. |
105 if (noSuchMethods == null) { | 108 if (noSuchMethods == null) { |
106 return const EmptyFunctionSetQuery(); | 109 return const EmptyFunctionSetQuery(); |
107 } | 110 } |
108 return noSuchMethods.query(noSuchMethodMask, classWorld); | 111 return noSuchMethods.query(noSuchMethodMask, classWorld); |
109 } | 112 } |
110 | 113 |
111 void forEach(Function action) { | 114 void forEach(Function action) { |
112 nodes.forEach((String name, FunctionSetNode node) { | 115 nodes.forEach((String name, FunctionSetNode node) { |
113 node.forEach(action); | 116 node.forEach(action); |
114 }); | 117 }); |
115 } | 118 } |
116 } | 119 } |
117 | 120 |
118 /// A selector/mask pair representing the dynamic invocation of [selector] on | 121 /// A selector/constraint pair representing the dynamic invocation of [selector] |
119 /// a receiver of type [mask]. | 122 /// on a receiver constrained by [constraint]. |
120 class SelectorMask { | 123 class SelectorMask { |
121 final Selector selector; | 124 final Selector selector; |
122 final TypeMask mask; | 125 final ReceiverConstraint constraint; |
123 final int hashCode; | 126 final int hashCode; |
124 | 127 |
125 SelectorMask(Selector selector, TypeMask mask) | 128 SelectorMask(Selector selector, ReceiverConstraint constraint) |
126 : this.selector = selector, | 129 : this.selector = selector, |
127 this.mask = mask, | 130 this.constraint = constraint, |
128 this.hashCode = | 131 this.hashCode = |
129 Hashing.mixHashCodeBits(selector.hashCode, mask.hashCode) { | 132 Hashing.mixHashCodeBits(selector.hashCode, constraint.hashCode) { |
130 assert(mask != null); | 133 assert(constraint != null); |
131 } | 134 } |
132 | 135 |
133 String get name => selector.name; | 136 String get name => selector.name; |
134 | 137 |
135 bool applies(Element element, ClassWorld classWorld) { | 138 bool applies(Element element, ClassWorld classWorld) { |
136 if (!selector.appliesUnnamed(element, classWorld)) return false; | 139 if (!selector.appliesUnnamed(element, classWorld)) return false; |
137 return mask.canHit(element, selector, classWorld); | 140 return constraint.canHit(element, selector, classWorld); |
138 } | 141 } |
139 | 142 |
140 bool needsNoSuchMethodHandling(ClassWorld classWorld) { | 143 bool needsNoSuchMethodHandling(ClassWorld classWorld) { |
141 return mask.needsNoSuchMethodHandling(selector, classWorld); | 144 return constraint.needsNoSuchMethodHandling(selector, classWorld); |
142 } | 145 } |
143 | 146 |
144 bool operator ==(other) { | 147 bool operator ==(other) { |
145 if (identical(this, other)) return true; | 148 if (identical(this, other)) return true; |
146 return selector == other.selector && mask == other.mask; | 149 return selector == other.selector && constraint == other.constraint; |
147 } | 150 } |
148 | 151 |
149 String toString() => '($selector,$mask)'; | 152 String toString() => '($selector,$constraint)'; |
150 } | 153 } |
151 | 154 |
152 /// A node in the [FunctionSet] caching all [FunctionSetQuery] object for | 155 /// A node in the [FunctionSet] caching all [FunctionSetQuery] object for |
153 /// selectors with the same [name]. | 156 /// selectors with the same [name]. |
154 class FunctionSetNode { | 157 class FunctionSetNode { |
155 final String name; | 158 final String name; |
156 final Map<SelectorMask, FunctionSetQuery> cache = | 159 final Map<SelectorMask, FunctionSetQuery> cache = |
157 <SelectorMask, FunctionSetQuery>{}; | 160 <SelectorMask, FunctionSetQuery>{}; |
158 | 161 |
159 // Initially, we keep the elements in a list because it is more | 162 // Initially, we keep the elements in a list because it is more |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 } else if (classWorld.isInstantiated(cls.declaration)) { | 304 } else if (classWorld.isInstantiated(cls.declaration)) { |
302 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); | 305 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); |
303 } else { | 306 } else { |
304 // TODO(johnniwinther): Avoid the need for this case. | 307 // TODO(johnniwinther): Avoid the need for this case. |
305 return const TypeMask.empty(); | 308 return const TypeMask.empty(); |
306 } | 309 } |
307 }), | 310 }), |
308 classWorld); | 311 classWorld); |
309 } | 312 } |
310 } | 313 } |
OLD | NEW |