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 class World { | 5 class World { |
6 Compiler compiler; // Set in populate(). | 6 Compiler compiler; // Set in populate(). |
7 final Map<ClassElement, Set<ClassElement>> subtypes; | 7 final Map<ClassElement, Set<ClassElement>> subtypes; |
8 | 8 |
9 World() : subtypes = new Map<ClassElement, Set<ClassElement>>(); | 9 World() : subtypes = new Map<ClassElement, Set<ClassElement>>(); |
10 | 10 |
11 void populate(Compiler compiler) { | 11 void populate(Compiler compiler) { |
12 void addSubtypes(ClassElement cls) { | 12 void addSubtypes(ClassElement cls) { |
13 if (cls.resolutionState != STATE_DONE) { | 13 if (cls.resolutionState != STATE_DONE) { |
14 compiler.internalErrorOnElement( | 14 compiler.internalErrorOnElement( |
15 cls, 'Class "${cls.name.slowToString()}" is not resolved.'); | 15 cls, 'Class "${cls.name.slowToString()}" is not resolved.'); |
16 } | 16 } |
17 for (Type type in cls.allSupertypes) { | 17 for (DartType type in cls.allSupertypes) { |
18 Set<Element> subtypesOfCls = | 18 Set<Element> subtypesOfCls = |
19 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>()); | 19 subtypes.putIfAbsent(type.element, () => new Set<ClassElement>()); |
20 subtypesOfCls.add(cls); | 20 subtypesOfCls.add(cls); |
21 } | 21 } |
22 } | 22 } |
23 | 23 |
24 compiler.resolverWorld.instantiatedClasses.forEach(addSubtypes); | 24 compiler.resolverWorld.instantiatedClasses.forEach(addSubtypes); |
25 | 25 |
26 // Mark the world as populated. | 26 // Mark the world as populated. |
27 assert(compiler !== null); | 27 assert(compiler !== null); |
28 this.compiler = compiler; | 28 this.compiler = compiler; |
29 } | 29 } |
30 | 30 |
31 /** | 31 /** |
32 * Returns a [MemberSet] that contains the possible targets of the given | 32 * Returns a [MemberSet] that contains the possible targets of the given |
33 * [selector] on a receiver with the given [type]. This includes all sub | 33 * [selector] on a receiver with the given [type]. This includes all sub |
34 * types. | 34 * types. |
35 */ | 35 */ |
36 MemberSet _memberSetFor(Type type, Selector selector) { | 36 MemberSet _memberSetFor(DartType type, Selector selector) { |
37 assert(compiler !== null); | 37 assert(compiler !== null); |
38 ClassElement cls = type.element; | 38 ClassElement cls = type.element; |
39 SourceString name = selector.name; | 39 SourceString name = selector.name; |
40 LibraryElement library = selector.library; | 40 LibraryElement library = selector.library; |
41 MemberSet result = new MemberSet(name); | 41 MemberSet result = new MemberSet(name); |
42 Element element = cls.lookupSelector(selector); | 42 Element element = cls.lookupSelector(selector); |
43 if (element !== null) result.add(element); | 43 if (element !== null) result.add(element); |
44 | 44 |
45 bool isPrivate = name.isPrivate(); | 45 bool isPrivate = name.isPrivate(); |
46 Set<ClassElement> subtypesOfCls = subtypes[cls]; | 46 Set<ClassElement> subtypesOfCls = subtypes[cls]; |
47 if (subtypesOfCls !== null) { | 47 if (subtypesOfCls !== null) { |
48 for (ClassElement sub in subtypesOfCls) { | 48 for (ClassElement sub in subtypesOfCls) { |
49 // Private members from a different library are not visible. | 49 // Private members from a different library are not visible. |
50 if (isPrivate && sub.getLibrary() != library) continue; | 50 if (isPrivate && sub.getLibrary() != library) continue; |
51 element = sub.lookupLocalMember(name); | 51 element = sub.lookupLocalMember(name); |
52 if (element !== null) result.add(element); | 52 if (element !== null) result.add(element); |
53 } | 53 } |
54 } | 54 } |
55 return result; | 55 return result; |
56 } | 56 } |
57 | 57 |
58 /** | 58 /** |
59 * Returns the field in [type] described by the given [selector]. | 59 * Returns the field in [type] described by the given [selector]. |
60 * If no such field exists, or a subclass overrides the field | 60 * If no such field exists, or a subclass overrides the field |
61 * returns [:null:]. | 61 * returns [:null:]. |
62 */ | 62 */ |
63 VariableElement locateSingleField(Type type, Selector selector) { | 63 VariableElement locateSingleField(DartType type, Selector selector) { |
64 MemberSet memberSet = _memberSetFor(type, selector); | 64 MemberSet memberSet = _memberSetFor(type, selector); |
65 ClassElement cls = type.element; | 65 ClassElement cls = type.element; |
66 Element result = cls.lookupSelector(selector); | 66 Element result = cls.lookupSelector(selector); |
67 if (result == null) return null; | 67 if (result == null) return null; |
68 if (!result.isField()) return null; | 68 if (!result.isField()) return null; |
69 | 69 |
70 // Verify that no subclass overrides the field. | 70 // Verify that no subclass overrides the field. |
71 if (memberSet.elements.length != 1) return null; | 71 if (memberSet.elements.length != 1) return null; |
72 assert(memberSet.elements.contains(result)); | 72 assert(memberSet.elements.contains(result)); |
73 return result; | 73 return result; |
74 } | 74 } |
75 | 75 |
76 Set<ClassElement> findNoSuchMethodHolders(Type type) { | 76 Set<ClassElement> findNoSuchMethodHolders(DartType type) { |
77 Set<ClassElement> result = new Set<ClassElement>(); | 77 Set<ClassElement> result = new Set<ClassElement>(); |
78 Selector noSuchMethodSelector = new Selector.noSuchMethod(); | 78 Selector noSuchMethodSelector = new Selector.noSuchMethod(); |
79 MemberSet memberSet = _memberSetFor(type, noSuchMethodSelector); | 79 MemberSet memberSet = _memberSetFor(type, noSuchMethodSelector); |
80 for (Element element in memberSet.elements) { | 80 for (Element element in memberSet.elements) { |
81 ClassElement holder = element.getEnclosingClass(); | 81 ClassElement holder = element.getEnclosingClass(); |
82 if (holder !== compiler.objectClass && | 82 if (holder !== compiler.objectClass && |
83 noSuchMethodSelector.applies(element, compiler)) { | 83 noSuchMethodSelector.applies(element, compiler)) { |
84 result.add(holder); | 84 result.add(holder); |
85 } | 85 } |
86 } | 86 } |
87 return result; | 87 return result; |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 /** | 91 /** |
92 * A [MemberSet] contains all the possible targets for a selector. | 92 * A [MemberSet] contains all the possible targets for a selector. |
93 */ | 93 */ |
94 class MemberSet { | 94 class MemberSet { |
95 final Set<Element> elements; | 95 final Set<Element> elements; |
96 final SourceString name; | 96 final SourceString name; |
97 | 97 |
98 MemberSet(SourceString this.name) : elements = new Set<Element>(); | 98 MemberSet(SourceString this.name) : elements = new Set<Element>(); |
99 | 99 |
100 void add(Element element) { | 100 void add(Element element) { |
101 elements.add(element); | 101 elements.add(element); |
102 } | 102 } |
103 | 103 |
104 bool isEmpty() => elements.isEmpty(); | 104 bool isEmpty() => elements.isEmpty(); |
105 } | 105 } |
OLD | NEW |