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

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

Issue 10834343: Fix access to fields that didn't exist in the class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix type annotation. 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
« no previous file with comments | « no previous file | tests/language/cast2_test.dart » ('j') | tests/language/cast2_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 13 matching lines...) Expand all
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] accessible from the given [library] on a receiver 33 * selector named [member] accessible from the given [library] on a receiver
34 * with the given [type]. 34 * with the given [type]. This includes all sub types.
35 * 35 *
36 * The [library] can be `null` if the [member] is not private. 36 * The [library] can be `null` if the [member] is not private.
37 */ 37 */
38 MemberSet _memberSetFor(Type type, 38 MemberSet _memberSetFor(Type type,
39 LibraryElement library, 39 LibraryElement library,
40 SourceString member) { 40 SourceString member) {
41 assert(compiler !== null); 41 assert(compiler !== null);
42 ClassElement cls = type.element; 42 ClassElement cls = type.element;
43 MemberSet result = new MemberSet(member); 43 MemberSet result = new MemberSet(member);
44 Element element = cls.lookupMemberInLibrary(member, library); 44 Element element = cls.lookupMemberInLibrary(member, library);
45 if (element !== null) result.add(element); 45 if (element !== null) result.add(element);
46 46
47 bool isPrivate = member.isPrivate(); 47 bool isPrivate = member.isPrivate();
48 Set<ClassElement> subtypesOfCls = subtypes[cls]; 48 Set<ClassElement> subtypesOfCls = subtypes[cls];
49 if (subtypesOfCls !== null) { 49 if (subtypesOfCls !== null) {
50 for (ClassElement sub in subtypesOfCls) { 50 for (ClassElement sub in subtypesOfCls) {
51 // Private members from a different library are not visible. 51 // Private members from a different library are not visible.
52 if (isPrivate && sub.getLibrary() != library) continue; 52 if (isPrivate && sub.getLibrary() != library) continue;
53 element = sub.lookupLocalMember(member); 53 element = sub.lookupLocalMember(member);
54 if (element !== null) result.add(element); 54 if (element !== null) result.add(element);
55 } 55 }
56 } 56 }
57 return result; 57 return result;
58 } 58 }
59 59
60 /** 60 /**
61 * Returns the single field with the given [name] accessible in the given 61 * Returns the field in [type] with the given [name] accessible in the given
62 * [library]. If there is no such field, or there are multiple possible 62 * [library]. If no such field exists, or a subclass overrides the field
63 * fields returns `null`. 63 * returns `null`.
64 */ 64 */
65 VariableElement locateSingleField(Type type, 65 VariableElement locateSingleField(Type type,
66 LibraryElement library, 66 LibraryElement library,
67 SourceString name) { 67 SourceString name) {
68 ClassElement cls = type.element;
69 Element result = cls.lookupMemberInLibrary(name, library);
70 if (result == null) return null;
71 if (!result.isField()) return null;
72
73 // Verify that no subclass overrides the field.
68 MemberSet memberSet = _memberSetFor(type, library, name); 74 MemberSet memberSet = _memberSetFor(type, library, name);
69 int fieldCount = 0; 75 if (memberSet.elements.length != 1) return null;
70 int nonFieldCount = 0; 76 assert(memberSet.elements.contains(result));
71 VariableElement field; 77 return result;
72 memberSet.elements.forEach((Element element) {
73 if (element.isField()) {
74 field = element;
75 fieldCount++;
76 } else {
77 nonFieldCount++;
78 }
79 });
80 return (fieldCount == 1 && nonFieldCount == 0) ? field : null;
81 } 78 }
82 79
83 Set<ClassElement> findNoSuchMethodHolders(Type type) { 80 Set<ClassElement> findNoSuchMethodHolders(Type type) {
84 Set<ClassElement> result = new Set<ClassElement>(); 81 Set<ClassElement> result = new Set<ClassElement>();
85 MemberSet memberSet = _memberSetFor(type, null, Compiler.NO_SUCH_METHOD); 82 MemberSet memberSet = _memberSetFor(type, null, Compiler.NO_SUCH_METHOD);
86 Selector noSuchMethodSelector = new Selector.noSuchMethod(); 83 Selector noSuchMethodSelector = new Selector.noSuchMethod();
87 for (Element element in memberSet.elements) { 84 for (Element element in memberSet.elements) {
88 ClassElement holder = element.getEnclosingClass(); 85 ClassElement holder = element.getEnclosingClass();
89 if (holder !== compiler.objectClass && 86 if (holder !== compiler.objectClass &&
90 noSuchMethodSelector.applies(element, compiler)) { 87 noSuchMethodSelector.applies(element, compiler)) {
(...skipping 12 matching lines...) Expand all
103 final SourceString name; 100 final SourceString name;
104 101
105 MemberSet(SourceString this.name) : elements = new Set<Element>(); 102 MemberSet(SourceString this.name) : elements = new Set<Element>();
106 103
107 void add(Element element) { 104 void add(Element element) {
108 elements.add(element); 105 elements.add(element);
109 } 106 }
110 107
111 bool isEmpty() => elements.isEmpty(); 108 bool isEmpty() => elements.isEmpty();
112 } 109 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/cast2_test.dart » ('j') | tests/language/cast2_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698