Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: pkg/compiler/lib/src/universe/function_set.dart

Issue 1182913003: Split TypedSelector into Selector and TypeMask. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 30 matching lines...) Expand all
41 FunctionSetNode node = nodes[name]; 41 FunctionSetNode node = nodes[name];
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, TypeMask mask) {
52 return query(selector).functions; 52 return query(selector, mask).functions;
53 } 53 }
54 54
55 TypeMask receiverType(Selector selector) { 55 TypeMask receiverType(Selector selector, TypeMask mask) {
56 return query(selector).computeMask(compiler.world); 56 return query(selector, mask).computeMask(compiler.world);
57 } 57 }
58 58
59 FunctionSetQuery query(Selector selector) { 59 FunctionSetQuery query(Selector selector, TypeMask mask) {
60 String name = selector.name; 60 String name = selector.name;
61 FunctionSetNode node = nodes[name]; 61 FunctionSetNode node = nodes[name];
62 FunctionSetNode noSuchMethods = nodes[Compiler.NO_SUCH_METHOD]; 62 FunctionSetNode noSuchMethods = nodes[Compiler.NO_SUCH_METHOD];
63 if (node != null) { 63 if (node != null) {
64 return node.query(selector, compiler, noSuchMethods); 64 return node.query(selector, mask, compiler, noSuchMethods);
65 } 65 }
66 // 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
67 // only hit [:noSuchMethod:]. 67 // only hit [:noSuchMethod:].
68 if (noSuchMethods == null) return const FunctionSetQuery(const <Element>[]); 68 if (noSuchMethods == null) return const FunctionSetQuery(const <Element>[]);
69 selector = (selector.mask == null) 69 return noSuchMethods.query(
70 ? compiler.noSuchMethodSelector 70 compiler.noSuchMethodSelector, mask, compiler, null);
71 : new TypedSelector(selector.mask, compiler.noSuchMethodSelector,
72 compiler.world);
73
74 return noSuchMethods.query(selector, compiler, null);
75 } 71 }
76 72
77 void forEach(Function action) { 73 void forEach(Function action) {
78 nodes.forEach((String name, FunctionSetNode node) { 74 nodes.forEach((String name, FunctionSetNode node) {
79 node.forEach(action); 75 node.forEach(action);
80 }); 76 });
81 } 77 }
82 } 78 }
83 79
80 class SelectorMask {
81 final Selector selector;
82 final TypeMask mask;
83 final int hashCode;
84
85 SelectorMask(Selector selector, TypeMask mask)
86 : this.selector = selector,
87 this.mask = mask,
88 this.hashCode =
89 Hashing.mixHashCodeBits(selector.hashCode, mask.hashCode);
90
91 String get name => selector.name;
92
93 bool applies(Element element, ClassWorld classWorld) {
94 if (!selector.appliesUnnamed(element, classWorld)) return false;
95 if (mask == null) return true;
96 return mask.canHit(element, selector, classWorld);
97 }
98
99 bool operator ==(other) {
100 if (identical(this, other)) return true;
101 return selector == other.selector && mask == other.mask;
102 }
103
104 String toString() => '($selector,$mask)';
105 }
84 106
85 class FunctionSetNode { 107 class FunctionSetNode {
86 final String name; 108 final String name;
87 final Map<Selector, FunctionSetQuery> cache = 109 final Map<SelectorMask, FunctionSetQuery> cache =
88 new Map<Selector, FunctionSetQuery>(); 110 <SelectorMask, FunctionSetQuery>{};
89 111
90 // Initially, we keep the elements in a list because it is more 112 // Initially, we keep the elements in a list because it is more
91 // compact than a hash set. Once we get enough elements, we change 113 // compact than a hash set. Once we get enough elements, we change
92 // the representation to be a set to get faster contains checks. 114 // the representation to be a set to get faster contains checks.
93 static const int MAX_ELEMENTS_IN_LIST = 8; 115 static const int MAX_ELEMENTS_IN_LIST = 8;
94 var elements = <Element>[]; 116 var elements = <Element>[];
95 bool isList = true; 117 bool isList = true;
96 118
97 FunctionSetNode(this.name); 119 FunctionSetNode(this.name);
98 120
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 157
136 bool contains(Element element) { 158 bool contains(Element element) {
137 assert(element.name == name); 159 assert(element.name == name);
138 return elements.contains(element); 160 return elements.contains(element);
139 } 161 }
140 162
141 void forEach(Function action) { 163 void forEach(Function action) {
142 elements.forEach(action); 164 elements.forEach(action);
143 } 165 }
144 166
145 TypeMask getNonNullTypeMaskOfSelector(Selector selector, Compiler compiler) { 167 TypeMask getNonNullTypeMaskOfSelector(TypeMask mask, ClassWorld classWorld) {
146 // TODO(ngeoffray): We should probably change untyped selector 168 // TODO(ngeoffray): We should probably change untyped selector
147 // to always be a subclass of Object. 169 // to always be a subclass of Object.
148 return selector.mask != null 170 return mask != null
149 ? selector.mask 171 ? mask
150 : new TypeMask.subclass(compiler.objectClass, compiler.world); 172 : new TypeMask.subclass(classWorld.objectClass, classWorld);
151 } 173 }
152 174
175 // TODO(johnniwinther): Use [SelectorMask] instead of [Selector] and
176 // [TypeMask].
153 FunctionSetQuery query(Selector selector, 177 FunctionSetQuery query(Selector selector,
178 TypeMask mask,
154 Compiler compiler, 179 Compiler compiler,
155 FunctionSetNode noSuchMethods) { 180 FunctionSetNode noSuchMethods) {
181 mask = getNonNullTypeMaskOfSelector(mask, compiler.world);
182 SelectorMask selectorMask = new SelectorMask(selector, mask);
156 ClassWorld classWorld = compiler.world; 183 ClassWorld classWorld = compiler.world;
157 assert(selector.name == name); 184 assert(selector.name == name);
158 FunctionSetQuery result = cache[selector]; 185 FunctionSetQuery result = cache[selectorMask];
159 if (result != null) return result; 186 if (result != null) return result;
187
160 Setlet<Element> functions; 188 Setlet<Element> functions;
161 for (Element element in elements) { 189 for (Element element in elements) {
162 if (selector.appliesUnnamed(element, classWorld)) { 190 if (selectorMask.applies(element, classWorld)) {
163 if (functions == null) { 191 if (functions == null) {
164 // Defer the allocation of the functions set until we are 192 // Defer the allocation of the functions set until we are
165 // sure we need it. This allows us to return immutable empty 193 // sure we need it. This allows us to return immutable empty
166 // lists when the filtering produced no results. 194 // lists when the filtering produced no results.
167 functions = new Setlet<Element>(); 195 functions = new Setlet<Element>();
168 } 196 }
169 functions.add(element); 197 functions.add(element);
170 } 198 }
171 } 199 }
172 200
173 TypeMask mask = getNonNullTypeMaskOfSelector(selector, compiler);
174 // If we cannot ensure a method will be found at runtime, we also 201 // If we cannot ensure a method will be found at runtime, we also
175 // add [noSuchMethod] implementations that apply to [mask] as 202 // add [noSuchMethod] implementations that apply to [mask] as
176 // potential targets. 203 // potential targets.
177 if (noSuchMethods != null 204 if (noSuchMethods != null
178 && mask.needsNoSuchMethodHandling(selector, classWorld)) { 205 && mask.needsNoSuchMethodHandling(selector, classWorld)) {
179 FunctionSetQuery noSuchMethodQuery = noSuchMethods.query( 206 FunctionSetQuery noSuchMethodQuery = noSuchMethods.query(
180 new TypedSelector( 207 compiler.noSuchMethodSelector,
181 mask, compiler.noSuchMethodSelector, classWorld), 208 mask,
182 compiler, 209 compiler,
183 null); 210 null);
184 if (!noSuchMethodQuery.functions.isEmpty) { 211 if (!noSuchMethodQuery.functions.isEmpty) {
185 if (functions == null) { 212 if (functions == null) {
186 functions = new Setlet<Element>.from(noSuchMethodQuery.functions); 213 functions = new Setlet<Element>.from(noSuchMethodQuery.functions);
187 } else { 214 } else {
188 functions.addAll(noSuchMethodQuery.functions); 215 functions.addAll(noSuchMethodQuery.functions);
189 } 216 }
190 } 217 }
191 } 218 }
192 cache[selector] = result = (functions != null) 219 cache[selectorMask] = result = (functions != null)
193 ? newQuery(functions, selector, compiler) 220 ? newQuery(functions, selector, mask, compiler)
194 : const FunctionSetQuery(const <Element>[]); 221 : const FunctionSetQuery(const <Element>[]);
195 return result; 222 return result;
196 } 223 }
197 224
198 FunctionSetQuery newQuery(Iterable<Element> functions, 225 FunctionSetQuery newQuery(Iterable<Element> functions,
199 Selector selector, 226 Selector selector,
227 TypeMask mask,
200 Compiler compiler) { 228 Compiler compiler) {
201 return new FullFunctionSetQuery(functions); 229 return new FullFunctionSetQuery(functions);
202 } 230 }
203 } 231 }
204 232
205 class FunctionSetQuery { 233 class FunctionSetQuery {
206 final Iterable<Element> functions; 234 final Iterable<Element> functions;
207 TypeMask computeMask(ClassWorld classWorld) => const TypeMask.nonNullEmpty(); 235 TypeMask computeMask(ClassWorld classWorld) => const TypeMask.nonNullEmpty();
208 const FunctionSetQuery(this.functions); 236 const FunctionSetQuery(this.functions);
209 } 237 }
(...skipping 17 matching lines...) Expand all
227 return const TypeMask.empty(); 255 return const TypeMask.empty();
228 } else { 256 } else {
229 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); 257 return new TypeMask.nonNullSubclass(cls.declaration, classWorld);
230 } 258 }
231 }), 259 }),
232 classWorld); 260 classWorld);
233 } 261 }
234 262
235 FullFunctionSetQuery(functions) : super(functions); 263 FullFunctionSetQuery(functions) : super(functions);
236 } 264 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/types/union_type_mask.dart ('k') | pkg/compiler/lib/src/universe/universe.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698