| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class World { |
| 6 final Map<ClassElement, Set<ClassElement>> subtypes; |
| 7 |
| 8 World() : subtypes = new Map<ClassElement, Set<ClassElement>>(); |
| 9 |
| 10 void populate(Compiler compiler, Collection<LibraryElement> libraries) { |
| 11 void addSubtypes(ClassElement cls) { |
| 12 for (Type type in cls.allSupertypes) { |
| 13 List<Element> subtypes = subtypes.putIfAbsent( |
| 14 type.element, |
| 15 () => <ClassElement>[]); |
| 16 subtypes.add(cls); |
| 17 } |
| 18 } |
| 19 |
| 20 libraries.forEach((LibraryElement library) { |
| 21 for (Link<Element> link = library.topLevelElements; |
| 22 !link.isEmpty(); |
| 23 link = link.tail) { |
| 24 Element element = link.head; |
| 25 if (!element.isClass()) continue; |
| 26 ClassElement cls = element; |
| 27 compiler.resolveClass(cls); |
| 28 addSubtypes(cls); |
| 29 } |
| 30 }); |
| 31 } |
| 32 |
| 33 /** |
| 34 * Returns a [MemberSet] that contains the possible targets of a |
| 35 * selector named [member] on a receiver whose type is [type]. |
| 36 */ |
| 37 MemberSet _memberSetFor(Type type, SourceString member) { |
| 38 ClassElement cls = type.element; |
| 39 MemberSet result = new MemberSet(member); |
| 40 Element element = cls.lookupMember(member); |
| 41 if (element !== null) result.add(element); |
| 42 |
| 43 Set<ClassElement> subtypes = subtypes[cls]; |
| 44 if (subtypes !== null) { |
| 45 for (ClassElement sub in subtypes) { |
| 46 element = sub.lookupLocalMember(member); |
| 47 if (element !== null) result.add(element); |
| 48 } |
| 49 } |
| 50 return result; |
| 51 } |
| 52 |
| 53 bool isOnlyFields(Type type, SourceString member) { |
| 54 MemberSet memberSet = _memberSetFor(type, member); |
| 55 return !memberSet.isEmpty() && memberSet.hasJustFields(); |
| 56 } |
| 57 } |
| 58 |
| 59 /** |
| 60 * A [MemberSet] contains all the possible targets for a selector. |
| 61 */ |
| 62 class MemberSet { |
| 63 final Set<Element> elements; |
| 64 final SourceString name; |
| 65 |
| 66 MemberSet(SourceString this.name) : elements = new Set<Element>(); |
| 67 |
| 68 void add(Element element) { |
| 69 elements.add(element); |
| 70 } |
| 71 |
| 72 bool isEmpty() => elements.isEmpty(); |
| 73 |
| 74 bool hasJustFields() { |
| 75 return elements.every((Element element) => element.isField()); |
| 76 } |
| 77 } |
| OLD | NEW |