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

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

Issue 10825337: Add name and library to selectors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make library == null for non-private selectors. 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 Universe { 5 class Universe {
6 Map<Element, CodeBuffer> generatedCode; 6 Map<Element, CodeBuffer> generatedCode;
7 Map<Element, CodeBuffer> generatedBailoutCode; 7 Map<Element, CodeBuffer> generatedBailoutCode;
8 final Set<ClassElement> instantiatedClasses; 8 final Set<ClassElement> instantiatedClasses;
9 final Set<SourceString> instantiatedClassInstanceFields; 9 final Set<SourceString> instantiatedClassInstanceFields;
10 final Set<FunctionElement> staticFunctionsNeedingGetter; 10 final Set<FunctionElement> staticFunctionsNeedingGetter;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 return hasMatchingSelector(fieldSetters[member.name], member, compiler); 68 return hasMatchingSelector(fieldSetters[member.name], member, compiler);
69 } 69 }
70 } 70 }
71 71
72 class SelectorKind { 72 class SelectorKind {
73 final String name; 73 final String name;
74 const SelectorKind(this.name); 74 const SelectorKind(this.name);
75 75
76 static final SelectorKind GETTER = const SelectorKind('getter'); 76 static final SelectorKind GETTER = const SelectorKind('getter');
77 static final SelectorKind SETTER = const SelectorKind('setter'); 77 static final SelectorKind SETTER = const SelectorKind('setter');
78 // TODO(kasperl): Rename INVOCATION to CALL and introduce CALL_ANY.
78 static final SelectorKind INVOCATION = const SelectorKind('invocation'); 79 static final SelectorKind INVOCATION = const SelectorKind('invocation');
79 static final SelectorKind OPERATOR = const SelectorKind('operator'); 80 static final SelectorKind OPERATOR = const SelectorKind('operator');
80 static final SelectorKind INDEX = const SelectorKind('index'); 81 static final SelectorKind INDEX = const SelectorKind('index');
81 82
82 toString() => name; 83 toString() => name;
83 } 84 }
84 85
85 class Selector implements Hashable { 86 class Selector implements Hashable {
87 final SelectorKind kind;
88 final SourceString name; // Name is null for call-any selectors.
89 final LibraryElement library; // Library is null for non-private selectors.
90
86 // The numbers of arguments of the selector. Includes named arguments. 91 // The numbers of arguments of the selector. Includes named arguments.
87 final int argumentCount; 92 final int argumentCount;
88 final SelectorKind kind;
89 final List<SourceString> namedArguments; 93 final List<SourceString> namedArguments;
90 final List<SourceString> orderedNamedArguments; 94 final List<SourceString> orderedNamedArguments;
91 95
92 // The const constructor.
93 const Selector.constant(this.kind, this.argumentCount)
94 : this.namedArguments = const <SourceString>[],
95 this.orderedNamedArguments = const <SourceString>[];
96
97 Selector( 96 Selector(
98 this.kind, 97 this.kind,
98 SourceString name,
99 LibraryElement library,
99 this.argumentCount, 100 this.argumentCount,
100 [List<SourceString> namedArguments = const <SourceString>[]]) 101 [List<SourceString> namedArguments = const <SourceString>[]])
101 : this.namedArguments = namedArguments, 102 : this.name = name,
103 this.library = (name != null && name.isPrivate()) ? library : null,
104 this.namedArguments = namedArguments,
102 this.orderedNamedArguments = namedArguments.isEmpty() 105 this.orderedNamedArguments = namedArguments.isEmpty()
103 ? namedArguments 106 ? namedArguments
104 : <SourceString>[]; 107 : <SourceString>[] {
108 // TODO(kasperl): Only allow null name for call-any selectors.
109 assert(name == null || !name.isPrivate() || library != null);
110 }
105 111
106 Selector.invocation( 112 Selector.getter(SourceString name, LibraryElement library)
107 int argumentCount, 113 : this(SelectorKind.GETTER, name, library, 0);
108 [List<SourceString> namedArguments = const <SourceString>[]]) 114
109 : this(SelectorKind.INVOCATION, argumentCount, namedArguments); 115 Selector.setter(SourceString name, LibraryElement library)
116 : this(SelectorKind.SETTER, name, library, 1);
117
118 Selector.unaryOperator(SourceString name)
119 : this(SelectorKind.OPERATOR, operatorName(name, true), null, 0);
120
121 Selector.binaryOperator(SourceString name)
122 : this(SelectorKind.OPERATOR, operatorName(name, false), null, 1);
123
124 Selector.index()
125 : this(SelectorKind.INDEX, indexName(), null, 1);
126
127 Selector.indexSet()
128 : this(SelectorKind.INDEX, indexSetName(), null, 2);
129
130 Selector.call(SourceString name,
131 LibraryElement library,
132 int arity,
133 [List<SourceString> named = const []])
134 : this(SelectorKind.INVOCATION, name, library, arity, named);
135
136 Selector.callAny(int arity, [List<SourceString> named = const []])
137 : this(SelectorKind.INVOCATION, null, null, arity, named);
138
139 // TODO(kasperl): This belongs somewhere else.
140 Selector.noSuchMethod()
141 : this(SelectorKind.INVOCATION, Compiler.NO_SUCH_METHOD, null, 2);
142
143 bool isGetter() => kind === SelectorKind.GETTER;
144 bool isSetter() => kind === SelectorKind.SETTER;
145 bool isCall() => kind === SelectorKind.INVOCATION;
146
147 bool isIndex() => kind === SelectorKind.INDEX && argumentCount == 1;
148 bool isIndexSet() => kind === SelectorKind.INDEX && argumentCount == 2;
149
150 bool isOperator() => kind === SelectorKind.OPERATOR;
151 bool isUnaryOperator() => isOperator() && argumentCount == 0;
152 bool isBinaryOperator() => isOperator() && argumentCount == 1;
153
154 static SourceString operatorName(SourceString name, bool isUnary)
155 => Elements.constructOperatorName(
156 const SourceString('operator'), name, isUnary);
157
158 static SourceString indexName()
159 => operatorName(const SourceString('[]'), false);
160
161 static SourceString indexSetName()
162 => operatorName(const SourceString('[]='), false);
110 163
111 int hashCode() => argumentCount + 1000 * namedArguments.length; 164 int hashCode() => argumentCount + 1000 * namedArguments.length;
112 int get namedArgumentCount() => namedArguments.length; 165 int get namedArgumentCount() => namedArguments.length;
113 int get positionalArgumentCount() => argumentCount - namedArgumentCount; 166 int get positionalArgumentCount() => argumentCount - namedArgumentCount;
114 Type get receiverType() => null; 167 Type get receiverType() => null;
115 168
116 static final Selector GETTER =
117 const Selector.constant(SelectorKind.GETTER, 0);
118 static final Selector SETTER =
119 const Selector.constant(SelectorKind.SETTER, 1);
120 static final Selector UNARY_OPERATOR =
121 const Selector.constant(SelectorKind.OPERATOR, 0);
122 static final Selector BINARY_OPERATOR =
123 const Selector.constant(SelectorKind.OPERATOR, 1);
124 static final Selector INDEX =
125 const Selector.constant(SelectorKind.INDEX, 1);
126 static final Selector INDEX_SET =
127 const Selector.constant(SelectorKind.INDEX, 2);
128 static final Selector INDEX_AND_INDEX_SET =
129 const Selector.constant(SelectorKind.INDEX, 2);
130 static final Selector GETTER_AND_SETTER =
131 const Selector.constant(SelectorKind.SETTER, 1);
132 static final Selector INVOCATION_0 =
133 const Selector.constant(SelectorKind.INVOCATION, 0);
134 static final Selector INVOCATION_1 =
135 const Selector.constant(SelectorKind.INVOCATION, 1);
136 static final Selector INVOCATION_2 =
137 const Selector.constant(SelectorKind.INVOCATION, 2);
138
139 bool applies(Element element, Compiler compiler) { 169 bool applies(Element element, Compiler compiler) {
140 if (element.isSetter()) return kind === SelectorKind.SETTER; 170 if (element.isSetter()) return kind === SelectorKind.SETTER;
141 if (element.isGetter()) { 171 if (element.isGetter()) {
142 return kind === SelectorKind.GETTER || kind === SelectorKind.INVOCATION; 172 return kind === SelectorKind.GETTER || kind === SelectorKind.INVOCATION;
143 } 173 }
144 if (element.isField()) { 174 if (element.isField()) {
145 return kind === SelectorKind.GETTER 175 return kind === SelectorKind.GETTER
146 || kind === SelectorKind.INVOCATION 176 || kind === SelectorKind.INVOCATION
147 || kind === SelectorKind.SETTER; 177 || kind === SelectorKind.SETTER;
148 } 178 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 291
262 static bool sameNames(List<SourceString> first, List<SourceString> second) { 292 static bool sameNames(List<SourceString> first, List<SourceString> second) {
263 for (int i = 0; i < first.length; i++) { 293 for (int i = 0; i < first.length; i++) {
264 if (first[i] != second[i]) return false; 294 if (first[i] != second[i]) return false;
265 } 295 }
266 return true; 296 return true;
267 } 297 }
268 298
269 bool operator ==(other) { 299 bool operator ==(other) {
270 if (other is !Selector) return false; 300 if (other is !Selector) return false;
271 if (receiverType !== other.receiverType) return false; 301 return name == other.name
272 return argumentCount == other.argumentCount 302 && library === other.library
303 && argumentCount == other.argumentCount
273 && namedArguments.length == other.namedArguments.length 304 && namedArguments.length == other.namedArguments.length
305 && receiverType === other.receiverType
274 && sameNames(namedArguments, other.namedArguments); 306 && sameNames(namedArguments, other.namedArguments);
275 } 307 }
276 308
277 List<SourceString> getOrderedNamedArguments() { 309 List<SourceString> getOrderedNamedArguments() {
278 if (namedArguments.isEmpty()) return namedArguments; 310 if (namedArguments.isEmpty()) return namedArguments;
279 if (!orderedNamedArguments.isEmpty()) return orderedNamedArguments; 311 if (!orderedNamedArguments.isEmpty()) return orderedNamedArguments;
280 312
281 orderedNamedArguments.addAll(namedArguments); 313 orderedNamedArguments.addAll(namedArguments);
282 orderedNamedArguments.sort((SourceString first, SourceString second) { 314 orderedNamedArguments.sort((SourceString first, SourceString second) {
283 return first.slowToString().compareTo(second.slowToString()); 315 return first.slowToString().compareTo(second.slowToString());
284 }); 316 });
285 return orderedNamedArguments; 317 return orderedNamedArguments;
286 } 318 }
287 319
288 toString() => 'Selector($kind, $argumentCount)'; 320 toString() => 'Selector($kind, $name, $argumentCount)';
289 } 321 }
290 322
291 class TypedSelector extends Selector { 323 class TypedSelector extends Selector {
292 /** 324 /**
293 * The type of the receiver. Any subtype of that type can be the 325 * The type of the receiver. Any subtype of that type can be the
294 * target of the invocation. 326 * target of the invocation.
295 */ 327 */
296 final Type receiverType; 328 final Type receiverType;
297 329
298 TypedSelector(this.receiverType, Selector selector) 330 TypedSelector(this.receiverType, Selector selector)
299 : super(selector.kind, 331 : super(selector.kind,
332 selector.name,
333 selector.library,
300 selector.argumentCount, 334 selector.argumentCount,
301 selector.namedArguments); 335 selector.namedArguments);
302 336
303 /** 337 /**
304 * Check if [element] will be the one used at runtime when being 338 * Check if [element] will be the one used at runtime when being
305 * invoked on an instance of [cls]. 339 * invoked on an instance of [cls].
306 */ 340 */
307 bool hasElementIn(ClassElement cls, Element element) { 341 bool hasElementIn(ClassElement cls, Element element) {
308 Element resolved = cls.lookupMember(element.name); 342 Element resolved = cls.lookupMember(element.name);
309 if (resolved === element) return true; 343 if (resolved === element) return true;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 // Resolve an invocation of [element.name] on [self]. If it 379 // Resolve an invocation of [element.name] on [self]. If it
346 // is found, this selector is a candidate. 380 // is found, this selector is a candidate.
347 return hasElementIn(self, element) && super.applies(element, compiler); 381 return hasElementIn(self, element) && super.applies(element, compiler);
348 } 382 }
349 383
350 return false; 384 return false;
351 } 385 }
352 386
353 toString() => 'TypedSelector($kind, $receiverType, $argumentCount)'; 387 toString() => 'TypedSelector($kind, $receiverType, $argumentCount)';
354 } 388 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698