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

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: Rebase. 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') | no next file with comments »
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 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 the given 32 * Returns a [MemberSet] that contains the possible targets of the given
33 * [selector] on a receiver with the given [type]. 33 * [selector] on a receiver with the given [type]. This includes all sub
34 * types.
34 */ 35 */
35 MemberSet _memberSetFor(Type type, Selector selector) { 36 MemberSet _memberSetFor(Type type, Selector selector) {
36 assert(compiler !== null); 37 assert(compiler !== null);
37 ClassElement cls = type.element; 38 ClassElement cls = type.element;
38 SourceString name = selector.name; 39 SourceString name = selector.name;
39 LibraryElement library = selector.library; 40 LibraryElement library = selector.library;
40 MemberSet result = new MemberSet(name); 41 MemberSet result = new MemberSet(name);
41 Element element = cls.lookupSelector(selector); 42 Element element = cls.lookupSelector(selector);
42 if (element !== null) result.add(element); 43 if (element !== null) result.add(element);
43 44
44 bool isPrivate = name.isPrivate(); 45 bool isPrivate = name.isPrivate();
45 Set<ClassElement> subtypesOfCls = subtypes[cls]; 46 Set<ClassElement> subtypesOfCls = subtypes[cls];
46 if (subtypesOfCls !== null) { 47 if (subtypesOfCls !== null) {
47 for (ClassElement sub in subtypesOfCls) { 48 for (ClassElement sub in subtypesOfCls) {
48 // Private members from a different library are not visible. 49 // Private members from a different library are not visible.
49 if (isPrivate && sub.getLibrary() != library) continue; 50 if (isPrivate && sub.getLibrary() != library) continue;
50 element = sub.lookupLocalMember(name); 51 element = sub.lookupLocalMember(name);
51 if (element !== null) result.add(element); 52 if (element !== null) result.add(element);
52 } 53 }
53 } 54 }
54 return result; 55 return result;
55 } 56 }
56 57
57 /** 58 /**
58 * Returns the single field with the given [selector]. If there is no such 59 * Returns the field in [type] described by the given [selector].
59 * field, or there are multiple possible fields returns [:null:]. 60 * If no such field exists, or a subclass overrides the field
61 * returns [:null:].
60 */ 62 */
61 VariableElement locateSingleField(Type type, Selector selector) { 63 VariableElement locateSingleField(Type type, Selector selector) {
62 MemberSet memberSet = _memberSetFor(type, selector); 64 MemberSet memberSet = _memberSetFor(type, selector);
63 int fieldCount = 0; 65 ClassElement cls = type.element;
64 int nonFieldCount = 0; 66 Element result = cls.lookupSelector(selector);
65 VariableElement field; 67 if (result == null) return null;
66 memberSet.elements.forEach((Element element) { 68 if (!result.isField()) return null;
67 if (element.isField()) { 69
68 field = element; 70 // Verify that no subclass overrides the field.
69 fieldCount++; 71 if (memberSet.elements.length != 1) return null;
70 } else { 72 assert(memberSet.elements.contains(result));
71 nonFieldCount++; 73 return result;
72 }
73 });
74 return (fieldCount == 1 && nonFieldCount == 0) ? field : null;
75 } 74 }
76 75
77 Set<ClassElement> findNoSuchMethodHolders(Type type) { 76 Set<ClassElement> findNoSuchMethodHolders(Type type) {
78 Set<ClassElement> result = new Set<ClassElement>(); 77 Set<ClassElement> result = new Set<ClassElement>();
79 Selector noSuchMethodSelector = new Selector.noSuchMethod(); 78 Selector noSuchMethodSelector = new Selector.noSuchMethod();
80 MemberSet memberSet = _memberSetFor(type, noSuchMethodSelector); 79 MemberSet memberSet = _memberSetFor(type, noSuchMethodSelector);
81 for (Element element in memberSet.elements) { 80 for (Element element in memberSet.elements) {
82 ClassElement holder = element.getEnclosingClass(); 81 ClassElement holder = element.getEnclosingClass();
83 if (holder !== compiler.objectClass && 82 if (holder !== compiler.objectClass &&
84 noSuchMethodSelector.applies(element, compiler)) { 83 noSuchMethodSelector.applies(element, compiler)) {
(...skipping 12 matching lines...) Expand all
97 final SourceString name; 96 final SourceString name;
98 97
99 MemberSet(SourceString this.name) : elements = new Set<Element>(); 98 MemberSet(SourceString this.name) : elements = new Set<Element>();
100 99
101 void add(Element element) { 100 void add(Element element) {
102 elements.add(element); 101 elements.add(element);
103 } 102 }
104 103
105 bool isEmpty() => elements.isEmpty(); 104 bool isEmpty() => elements.isEmpty();
106 } 105 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/cast2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698