Chromium Code Reviews| 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 30 matching lines...) Expand all Loading... | |
| 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 Loading... | |
| 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 |
| 153 FunctionSetQuery query(Selector selector, | 175 FunctionSetQuery query(Selector selector, |
|
herhut
2015/06/24 15:19:29
As discussed, it would be nice to move this over t
Johnni Winther
2015/06/25 07:25:28
Added a TODO.
| |
| 176 TypeMask mask, | |
| 154 Compiler compiler, | 177 Compiler compiler, |
| 155 FunctionSetNode noSuchMethods) { | 178 FunctionSetNode noSuchMethods) { |
| 179 mask = getNonNullTypeMaskOfSelector(mask, compiler.world); | |
| 180 SelectorMask selectorMask = new SelectorMask(selector, mask); | |
| 156 ClassWorld classWorld = compiler.world; | 181 ClassWorld classWorld = compiler.world; |
| 157 assert(selector.name == name); | 182 assert(selector.name == name); |
| 158 FunctionSetQuery result = cache[selector]; | 183 FunctionSetQuery result = cache[selectorMask]; |
| 159 if (result != null) return result; | 184 if (result != null) return result; |
| 185 | |
| 160 Setlet<Element> functions; | 186 Setlet<Element> functions; |
| 161 for (Element element in elements) { | 187 for (Element element in elements) { |
| 162 if (selector.appliesUnnamed(element, classWorld)) { | 188 if (selectorMask.applies(element, classWorld)) { |
| 163 if (functions == null) { | 189 if (functions == null) { |
| 164 // Defer the allocation of the functions set until we are | 190 // Defer the allocation of the functions set until we are |
| 165 // sure we need it. This allows us to return immutable empty | 191 // sure we need it. This allows us to return immutable empty |
| 166 // lists when the filtering produced no results. | 192 // lists when the filtering produced no results. |
| 167 functions = new Setlet<Element>(); | 193 functions = new Setlet<Element>(); |
| 168 } | 194 } |
| 169 functions.add(element); | 195 functions.add(element); |
| 170 } | 196 } |
| 171 } | 197 } |
| 172 | 198 |
| 173 TypeMask mask = getNonNullTypeMaskOfSelector(selector, compiler); | |
| 174 // If we cannot ensure a method will be found at runtime, we also | 199 // If we cannot ensure a method will be found at runtime, we also |
| 175 // add [noSuchMethod] implementations that apply to [mask] as | 200 // add [noSuchMethod] implementations that apply to [mask] as |
| 176 // potential targets. | 201 // potential targets. |
| 177 if (noSuchMethods != null | 202 if (noSuchMethods != null |
| 178 && mask.needsNoSuchMethodHandling(selector, classWorld)) { | 203 && mask.needsNoSuchMethodHandling(selector, classWorld)) { |
| 179 FunctionSetQuery noSuchMethodQuery = noSuchMethods.query( | 204 FunctionSetQuery noSuchMethodQuery = noSuchMethods.query( |
| 180 new TypedSelector( | 205 compiler.noSuchMethodSelector, |
| 181 mask, compiler.noSuchMethodSelector, classWorld), | 206 mask, |
| 182 compiler, | 207 compiler, |
| 183 null); | 208 null); |
| 184 if (!noSuchMethodQuery.functions.isEmpty) { | 209 if (!noSuchMethodQuery.functions.isEmpty) { |
| 185 if (functions == null) { | 210 if (functions == null) { |
| 186 functions = new Setlet<Element>.from(noSuchMethodQuery.functions); | 211 functions = new Setlet<Element>.from(noSuchMethodQuery.functions); |
| 187 } else { | 212 } else { |
| 188 functions.addAll(noSuchMethodQuery.functions); | 213 functions.addAll(noSuchMethodQuery.functions); |
| 189 } | 214 } |
| 190 } | 215 } |
| 191 } | 216 } |
| 192 cache[selector] = result = (functions != null) | 217 cache[selectorMask] = result = (functions != null) |
| 193 ? newQuery(functions, selector, compiler) | 218 ? newQuery(functions, selector, mask, compiler) |
| 194 : const FunctionSetQuery(const <Element>[]); | 219 : const FunctionSetQuery(const <Element>[]); |
| 195 return result; | 220 return result; |
| 196 } | 221 } |
| 197 | 222 |
| 198 FunctionSetQuery newQuery(Iterable<Element> functions, | 223 FunctionSetQuery newQuery(Iterable<Element> functions, |
| 199 Selector selector, | 224 Selector selector, |
| 225 TypeMask mask, | |
| 200 Compiler compiler) { | 226 Compiler compiler) { |
| 201 return new FullFunctionSetQuery(functions); | 227 return new FullFunctionSetQuery(functions); |
| 202 } | 228 } |
| 203 } | 229 } |
| 204 | 230 |
| 205 class FunctionSetQuery { | 231 class FunctionSetQuery { |
| 206 final Iterable<Element> functions; | 232 final Iterable<Element> functions; |
| 207 TypeMask computeMask(ClassWorld classWorld) => const TypeMask.nonNullEmpty(); | 233 TypeMask computeMask(ClassWorld classWorld) => const TypeMask.nonNullEmpty(); |
| 208 const FunctionSetQuery(this.functions); | 234 const FunctionSetQuery(this.functions); |
| 209 } | 235 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 227 return const TypeMask.empty(); | 253 return const TypeMask.empty(); |
| 228 } else { | 254 } else { |
| 229 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); | 255 return new TypeMask.nonNullSubclass(cls.declaration, classWorld); |
| 230 } | 256 } |
| 231 }), | 257 }), |
| 232 classWorld); | 258 classWorld); |
| 233 } | 259 } |
| 234 | 260 |
| 235 FullFunctionSetQuery(functions) : super(functions); | 261 FullFunctionSetQuery(functions) : super(functions); |
| 236 } | 262 } |
| OLD | NEW |