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

Side by Side Diff: lib/compiler/implementation/world.dart

Issue 10827359: Fix field-accesses for private fields that were "shadowed" by other private fields. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
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 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
(...skipping 12 matching lines...) Expand all
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 a 32 * Returns a [MemberSet] that contains the possible targets of a
33 * selector named [member] on a receiver whose type is [type]. 33 * selector named [member] accessible from the given [library] on a receiver
34 * with the given [type].
35 *
36 * The [library] can be `null` if the [member] is not private.
34 */ 37 */
35 MemberSet _memberSetFor(Type type, SourceString member) { 38 MemberSet _memberSetFor(Type type,
kasperl 2012/08/16 12:47:57 Again it would seem like this could be rewritten w
floitsch 2012/08/16 16:15:55 Done.
39 LibraryElement library,
40 SourceString member) {
36 assert(compiler !== null); 41 assert(compiler !== null);
37 ClassElement cls = type.element; 42 ClassElement cls = type.element;
38 MemberSet result = new MemberSet(member); 43 MemberSet result = new MemberSet(member);
39 Element element = cls.lookupMember(member); 44 Element element = cls.lookupMemberInLibrary(member, library);
40 if (element !== null) result.add(element); 45 if (element !== null) result.add(element);
41 46
47 bool isPrivate = member.isPrivate();
42 Set<ClassElement> subtypesOfCls = subtypes[cls]; 48 Set<ClassElement> subtypesOfCls = subtypes[cls];
43 if (subtypesOfCls !== null) { 49 if (subtypesOfCls !== null) {
44 for (ClassElement sub in subtypesOfCls) { 50 for (ClassElement sub in subtypesOfCls) {
51 // Private members from a different library are not visible.
52 if (isPrivate && sub.getLibrary() != library) continue;
45 element = sub.lookupLocalMember(member); 53 element = sub.lookupLocalMember(member);
46 if (element !== null) result.add(element); 54 if (element !== null) result.add(element);
47 } 55 }
48 } 56 }
49 return result; 57 return result;
50 } 58 }
51 59
52 /** 60 /**
53 * Returns the single field with the given name, if such a field 61 * Returns the single field with the given [name] accessible in the given
54 * exists. If there are multple fields, or none, return null. 62 * [library]. If there is no such field, or there are multiple possible
63 * fields returns `null`.
55 */ 64 */
56 VariableElement locateSingleField(Type type, SourceString member) { 65 VariableElement locateSingleField(Type type,
57 MemberSet memberSet = _memberSetFor(type, member); 66 LibraryElement library,
67 SourceString name) {
68 MemberSet memberSet = _memberSetFor(type, library, name);
58 int fieldCount = 0; 69 int fieldCount = 0;
59 int nonFieldCount = 0; 70 int nonFieldCount = 0;
60 VariableElement field; 71 VariableElement field;
61 memberSet.elements.forEach((Element element) { 72 memberSet.elements.forEach((Element element) {
62 if (element.isField()) { 73 if (element.isField()) {
63 field = element; 74 field = element;
64 fieldCount++; 75 fieldCount++;
65 } else { 76 } else {
66 nonFieldCount++; 77 nonFieldCount++;
67 } 78 }
68 }); 79 });
69 return (fieldCount == 1 && nonFieldCount == 0) ? field : null; 80 return (fieldCount == 1 && nonFieldCount == 0) ? field : null;
70 } 81 }
71 82
72 Set<ClassElement> findNoSuchMethodHolders(Type type) { 83 Set<ClassElement> findNoSuchMethodHolders(Type type) {
73 Set<ClassElement> result = new Set<ClassElement>(); 84 Set<ClassElement> result = new Set<ClassElement>();
74 MemberSet memberSet = _memberSetFor(type, Compiler.NO_SUCH_METHOD); 85 MemberSet memberSet = _memberSetFor(type, null, Compiler.NO_SUCH_METHOD);
75 Selector noSuchMethodSelector = new Selector.noSuchMethod(); 86 Selector noSuchMethodSelector = new Selector.noSuchMethod();
76 for (Element element in memberSet.elements) { 87 for (Element element in memberSet.elements) {
77 ClassElement holder = element.getEnclosingClass(); 88 ClassElement holder = element.getEnclosingClass();
78 if (holder !== compiler.objectClass && 89 if (holder !== compiler.objectClass &&
79 noSuchMethodSelector.applies(element, compiler)) { 90 noSuchMethodSelector.applies(element, compiler)) {
80 result.add(holder); 91 result.add(holder);
81 } 92 }
82 } 93 }
83 return result; 94 return result;
84 } 95 }
85 } 96 }
86 97
87 /** 98 /**
88 * A [MemberSet] contains all the possible targets for a selector. 99 * A [MemberSet] contains all the possible targets for a selector.
89 */ 100 */
90 class MemberSet { 101 class MemberSet {
91 final Set<Element> elements; 102 final Set<Element> elements;
92 final SourceString name; 103 final SourceString name;
93 104
94 MemberSet(SourceString this.name) : elements = new Set<Element>(); 105 MemberSet(SourceString this.name) : elements = new Set<Element>();
95 106
96 void add(Element element) { 107 void add(Element element) {
97 elements.add(element); 108 elements.add(element);
98 } 109 }
99 110
100 bool isEmpty() => elements.isEmpty(); 111 bool isEmpty() => elements.isEmpty();
101 } 112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698