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

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: 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 static final SelectorKind INVOCATION = const SelectorKind('invocation'); 78 static final SelectorKind INVOCATION = const SelectorKind('invocation');
Søren Gjesse 2012/08/14 12:01:38 Rename INVOCATION and 'invocation' to CALL and 'ca
kasperl 2012/08/14 12:15:46 Will do in next CL.
79 static final SelectorKind OPERATOR = const SelectorKind('operator'); 79 static final SelectorKind OPERATOR = const SelectorKind('operator');
80 static final SelectorKind INDEX = const SelectorKind('index'); 80 static final SelectorKind INDEX = const SelectorKind('index');
81 81
82 toString() => name; 82 toString() => name;
83 } 83 }
84 84
85 class Selector implements Hashable { 85 class Selector implements Hashable {
86 final SelectorKind kind;
87 final SourceString name; // May be null for callAny.
88 final LibraryElement library; // XXX: Should be null for non-private names?
kasperl 2012/08/14 08:18:46 I think I need to make sure this is actually null
89
86 // The numbers of arguments of the selector. Includes named arguments. 90 // The numbers of arguments of the selector. Includes named arguments.
87 final int argumentCount; 91 final int argumentCount;
88 final SelectorKind kind;
89 final List<SourceString> namedArguments; 92 final List<SourceString> namedArguments;
90 final List<SourceString> orderedNamedArguments; 93 final List<SourceString> orderedNamedArguments;
91 94
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( 95 Selector(
98 this.kind, 96 this.kind,
97 this.name,
98 this.library,
kasperl 2012/08/14 08:18:46 I'll make the library null for non-private names h
99 this.argumentCount, 99 this.argumentCount,
100 [List<SourceString> namedArguments = const <SourceString>[]]) 100 [List<SourceString> namedArguments = const <SourceString>[]])
101 : this.namedArguments = namedArguments, 101 : this.namedArguments = namedArguments,
102 this.orderedNamedArguments = namedArguments.isEmpty() 102 this.orderedNamedArguments = namedArguments.isEmpty()
103 ? namedArguments 103 ? namedArguments
104 : <SourceString>[]; 104 : <SourceString>[] {
105 assert(name == null || !name.isPrivate() || library != null);
106 }
105 107
106 Selector.invocation( 108 Selector.getter(SourceString name, LibraryElement library)
107 int argumentCount, 109 : this(SelectorKind.GETTER, name, library, 0);
108 [List<SourceString> namedArguments = const <SourceString>[]]) 110
109 : this(SelectorKind.INVOCATION, argumentCount, namedArguments); 111 Selector.setter(SourceString name, LibraryElement library)
112 : this(SelectorKind.SETTER, name, library, 1);
113
114 Selector.unaryOperator(SourceString name)
115 : this(SelectorKind.OPERATOR, operatorName(name, true), null, 0);
116
117 Selector.binaryOperator(SourceString name)
118 : this(SelectorKind.OPERATOR, operatorName(name, false), null, 1);
119
120 Selector.index()
121 : this(SelectorKind.INDEX, indexName(), null, 1);
122
123 Selector.indexSet()
124 : this(SelectorKind.INDEX, indexSetName(), null, 2);
125
126 Selector.call(SourceString name,
127 LibraryElement library,
128 int arity,
129 [List<SourceString> named = const []])
130 : this(SelectorKind.INVOCATION, name, library, arity, named);
131
132 Selector.callAny(int arity, [List<SourceString> named = const []])
133 : this(SelectorKind.INVOCATION, null, null, arity, named);
134
135 // TODO(kasperl): This belongs somewhere else.
136 Selector.noSuchMethod()
137 : this(SelectorKind.INVOCATION, Compiler.NO_SUCH_METHOD, null, 2);
138
139 bool isGetter() => kind === SelectorKind.GETTER;
140 bool isSetter() => kind === SelectorKind.SETTER;
141 bool isCall() => kind === SelectorKind.INVOCATION;
142
143 bool isIndex() => kind === SelectorKind.INDEX && argumentCount == 1;
144 bool isIndexSet() => kind === SelectorKind.INDEX && argumentCount == 2;
145
146 bool isOperator() => kind === SelectorKind.OPERATOR;
147 bool isUnaryOperator() => isOperator() && argumentCount == 0;
148 bool isBinaryOperator() => isOperator() && argumentCount == 1;
149
150 static SourceString operatorName(SourceString name, bool isUnary)
151 => Elements.constructOperatorName(
152 const SourceString('operator'), name, isUnary);
153
154 static SourceString indexName()
155 => operatorName(const SourceString('[]'), false);
156
157 static SourceString indexSetName()
158 => operatorName(const SourceString('[]='), false);
110 159
111 int hashCode() => argumentCount + 1000 * namedArguments.length; 160 int hashCode() => argumentCount + 1000 * namedArguments.length;
112 int get namedArgumentCount() => namedArguments.length; 161 int get namedArgumentCount() => namedArguments.length;
113 int get positionalArgumentCount() => argumentCount - namedArgumentCount; 162 int get positionalArgumentCount() => argumentCount - namedArgumentCount;
114 Type get receiverType() => null; 163 Type get receiverType() => null;
115 164
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) { 165 bool applies(Element element, Compiler compiler) {
140 if (element.isSetter()) return kind === SelectorKind.SETTER; 166 if (element.isSetter()) return kind === SelectorKind.SETTER;
141 if (element.isGetter()) { 167 if (element.isGetter()) {
142 return kind === SelectorKind.GETTER || kind === SelectorKind.INVOCATION; 168 return kind === SelectorKind.GETTER || kind === SelectorKind.INVOCATION;
143 } 169 }
144 if (element.isField()) { 170 if (element.isField()) {
145 return kind === SelectorKind.GETTER 171 return kind === SelectorKind.GETTER
146 || kind === SelectorKind.INVOCATION 172 || kind === SelectorKind.INVOCATION
147 || kind === SelectorKind.SETTER; 173 || kind === SelectorKind.SETTER;
148 } 174 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 287
262 static bool sameNames(List<SourceString> first, List<SourceString> second) { 288 static bool sameNames(List<SourceString> first, List<SourceString> second) {
263 for (int i = 0; i < first.length; i++) { 289 for (int i = 0; i < first.length; i++) {
264 if (first[i] != second[i]) return false; 290 if (first[i] != second[i]) return false;
265 } 291 }
266 return true; 292 return true;
267 } 293 }
268 294
269 bool operator ==(other) { 295 bool operator ==(other) {
270 if (other is !Selector) return false; 296 if (other is !Selector) return false;
271 if (receiverType !== other.receiverType) return false; 297 return name == other.name
272 return argumentCount == other.argumentCount 298 && library === other.library
299 && argumentCount == other.argumentCount
273 && namedArguments.length == other.namedArguments.length 300 && namedArguments.length == other.namedArguments.length
301 && receiverType === other.receiverType
274 && sameNames(namedArguments, other.namedArguments); 302 && sameNames(namedArguments, other.namedArguments);
275 } 303 }
276 304
277 List<SourceString> getOrderedNamedArguments() { 305 List<SourceString> getOrderedNamedArguments() {
278 if (namedArguments.isEmpty()) return namedArguments; 306 if (namedArguments.isEmpty()) return namedArguments;
279 if (!orderedNamedArguments.isEmpty()) return orderedNamedArguments; 307 if (!orderedNamedArguments.isEmpty()) return orderedNamedArguments;
280 308
281 orderedNamedArguments.addAll(namedArguments); 309 orderedNamedArguments.addAll(namedArguments);
282 orderedNamedArguments.sort((SourceString first, SourceString second) { 310 orderedNamedArguments.sort((SourceString first, SourceString second) {
283 return first.slowToString().compareTo(second.slowToString()); 311 return first.slowToString().compareTo(second.slowToString());
284 }); 312 });
285 return orderedNamedArguments; 313 return orderedNamedArguments;
286 } 314 }
287 315
288 toString() => 'Selector($kind, $argumentCount)'; 316 toString() => 'Selector($kind, $name, $argumentCount)';
289 } 317 }
290 318
291 class TypedSelector extends Selector { 319 class TypedSelector extends Selector {
292 /** 320 /**
293 * The type of the receiver. Any subtype of that type can be the 321 * The type of the receiver. Any subtype of that type can be the
294 * target of the invocation. 322 * target of the invocation.
295 */ 323 */
296 final Type receiverType; 324 final Type receiverType;
297 325
298 TypedSelector(this.receiverType, Selector selector) 326 TypedSelector(this.receiverType, Selector selector)
299 : super(selector.kind, 327 : super(selector.kind,
328 selector.name,
329 selector.library,
300 selector.argumentCount, 330 selector.argumentCount,
301 selector.namedArguments); 331 selector.namedArguments);
302 332
303 /** 333 /**
304 * Check if [element] will be the one used at runtime when being 334 * Check if [element] will be the one used at runtime when being
305 * invoked on an instance of [cls]. 335 * invoked on an instance of [cls].
306 */ 336 */
307 bool hasElementIn(ClassElement cls, Element element) { 337 bool hasElementIn(ClassElement cls, Element element) {
308 Element resolved = cls.lookupMember(element.name); 338 Element resolved = cls.lookupMember(element.name);
309 if (resolved === element) return true; 339 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 375 // Resolve an invocation of [element.name] on [self]. If it
346 // is found, this selector is a candidate. 376 // is found, this selector is a candidate.
347 return hasElementIn(self, element) && super.applies(element, compiler); 377 return hasElementIn(self, element) && super.applies(element, compiler);
348 } 378 }
349 379
350 return false; 380 return false;
351 } 381 }
352 382
353 toString() => 'TypedSelector($kind, $receiverType, $argumentCount)'; 383 toString() => 'TypedSelector($kind, $receiverType, $argumentCount)';
354 } 384 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698