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

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

Issue 10911211: Runtime support for the new parameter specification. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 #library('universe'); 5 #library('universe');
6 6
7 #import('../closure.dart'); 7 #import('../closure.dart');
8 #import('../elements/elements.dart'); 8 #import('../elements/elements.dart');
9 #import('../leg.dart'); 9 #import('../leg.dart');
10 #import('../scanner/scannerlib.dart'); 10 #import('../scanner/scannerlib.dart');
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 if (element.isField()) return isGetter() || isSetter() || isCall(); 208 if (element.isField()) return isGetter() || isSetter() || isCall();
209 if (isGetter()) return true; 209 if (isGetter()) return true;
210 210
211 FunctionElement function = element; 211 FunctionElement function = element;
212 FunctionSignature parameters = function.computeSignature(compiler); 212 FunctionSignature parameters = function.computeSignature(compiler);
213 if (argumentCount > parameters.parameterCount) return false; 213 if (argumentCount > parameters.parameterCount) return false;
214 int requiredParameterCount = parameters.requiredParameterCount; 214 int requiredParameterCount = parameters.requiredParameterCount;
215 int optionalParameterCount = parameters.optionalParameterCount; 215 int optionalParameterCount = parameters.optionalParameterCount;
216 if (positionalArgumentCount < requiredParameterCount) return false; 216 if (positionalArgumentCount < requiredParameterCount) return false;
217 217
218 if (!parameters.optionalParametersAreNamed) {
219 if (!Compiler.REJECT_NAMED_ARGUMENT_AS_POSITIONAL) {
220 return optionalParametersAppliesDEPRECATED(element, compiler);
221 } else {
222 // We have already checked that the number of arguments are
223 // not greater than the number of parameters. Therefore the
224 // number of positional arguments are not greater than the
225 // number of parameters.
226 assert(positionalArgumentCount <= parameters.parameterCount);
227 return namedArguments.isEmpty();
228 }
229 } else {
230 if (positionalArgumentCount > requiredParameterCount) return false;
ahe 2012/09/13 14:29:10 assert(positionalArgumentCount == requiredParamete
ngeoffray 2012/09/17 12:21:01 Done.
231 if (namedArgumentCount > optionalParameterCount) return false;
232 Set<SourceString> nameSet = new Set<SourceString>();
233 parameters.optionalParameters.forEach((Element element) {
234 nameSet.add(element.name);
235 });
236 for (SourceString name in namedArguments) {
237 if (!nameSet.contains(name)) return false;
238 // TODO(ngeoffray): By removing from the set we are checking
ahe 2012/09/13 14:29:10 Perhaps Jamie would like to look at that. File a b
ngeoffray 2012/09/17 12:21:01 Done: http://code.google.com/p/dart/issues/detail?
239 // that we are not passing the name twice. We should have this
240 // check in the resolver instead.
241 nameSet.remove(name);
242 }
243 return true;
244 }
245 }
246
247 // TODO(5074): Remove this method once we don't accept the
248 // deprecated parameter specification.
249 bool optionalParametersAppliesDEPRECATED(FunctionElement function,
250 Compiler compiler) {
251 FunctionSignature parameters = function.computeSignature(compiler);
252 int requiredParameterCount = parameters.requiredParameterCount;
253 int optionalParameterCount = parameters.optionalParameterCount;
254
218 bool hasOptionalParameters = !parameters.optionalParameters.isEmpty(); 255 bool hasOptionalParameters = !parameters.optionalParameters.isEmpty();
219 if (namedArguments.isEmpty()) { 256 if (namedArguments.isEmpty()) {
220 if (!hasOptionalParameters) { 257 if (!hasOptionalParameters) {
221 return requiredParameterCount == argumentCount; 258 return requiredParameterCount == argumentCount;
222 } else { 259 } else {
223 return argumentCount >= requiredParameterCount && 260 return argumentCount >= requiredParameterCount &&
224 argumentCount <= requiredParameterCount + optionalParameterCount; 261 argumentCount <= requiredParameterCount + optionalParameterCount;
225 } 262 }
226 } else { 263 } else {
227 if (!hasOptionalParameters) return false; 264 if (!hasOptionalParameters) return false;
(...skipping 11 matching lines...) Expand all
239 for (SourceString name in namedArguments) { 276 for (SourceString name in namedArguments) {
240 if (!nameSet.contains(name)) { 277 if (!nameSet.contains(name)) {
241 return false; 278 return false;
242 } 279 }
243 nameSet.remove(name); 280 nameSet.remove(name);
244 } 281 }
245 return true; 282 return true;
246 } 283 }
247 } 284 }
248 285
249 /** 286 // TODO(5074): Remove this method once we don't accept the
250 * Returns [:true:] if the selector and the [element] match; [:false:] 287 // deprecated parameter specification.
251 * otherwise. 288 bool addOptionalArgumentsToListDEPRECATED(Link<Node> arguments,
252 */ 289 List list,
253 bool addArgumentsToList(Link<Node> arguments, 290 FunctionElement element,
254 List list, 291 compileArgument(Node argument),
255 FunctionElement element, 292 compileConstant(Element element),
256 compileArgument(Node argument), 293 Compiler compiler) {
257 compileConstant(Element element),
258 Compiler compiler) {
259 if (!this.applies(element, compiler)) return false;
260
261 void addMatchingArgumentsToList(Link<Node> link) {}
262
263 FunctionSignature parameters = element.computeSignature(compiler);
264 if (this.positionalArgumentCount == parameters.parameterCount) {
265 for (Link<Node> link = arguments; !link.isEmpty(); link = link.tail) {
266 list.add(compileArgument(link.head));
267 }
268 return true;
269 }
270
271 // If there are named arguments, provide them in the order 294 // If there are named arguments, provide them in the order
272 // expected by the called function, which is the source order. 295 // expected by the called function, which is the source order.
296 FunctionSignature parameters = element.computeSignature(compiler);
273 297
274 // Visit positional arguments and add them to the list. 298 // Visit positional arguments and add them to the list.
275 int positionalArgumentCount = this.positionalArgumentCount; 299 for (int i = parameters.requiredParameterCount;
276 for (int i = 0;
277 i < positionalArgumentCount; 300 i < positionalArgumentCount;
278 arguments = arguments.tail, i++) { 301 arguments = arguments.tail, i++) {
279 list.add(compileArgument(arguments.head)); 302 list.add(compileArgument(arguments.head));
280 } 303 }
281 304
282 // Visit named arguments and add them into a temporary list. 305 // Visit named arguments and add them into a temporary list.
283 List compiledNamedArguments = []; 306 List compiledNamedArguments = [];
284 for (; !arguments.isEmpty(); arguments = arguments.tail) { 307 for (; !arguments.isEmpty(); arguments = arguments.tail) {
285 NamedArgument namedArgument = arguments.head; 308 NamedArgument namedArgument = arguments.head;
286 compiledNamedArguments.add(compileArgument(namedArgument.expression)); 309 compiledNamedArguments.add(compileArgument(namedArgument.expression));
287 } 310 }
288 311
289 Link<Element> remainingNamedParameters = parameters.optionalParameters; 312 Link<Element> remainingNamedParameters = parameters.optionalParameters;
290 // Skip the optional parameters that have been given in the 313 // Skip the optional parameters that have been given in the
291 // positional arguments. 314 // positional arguments.
292 for (int i = parameters.requiredParameterCount; 315 for (int i = parameters.requiredParameterCount;
293 i < positionalArgumentCount; 316 i < positionalArgumentCount;
294 i++) { 317 i++) {
295 remainingNamedParameters = remainingNamedParameters.tail; 318 remainingNamedParameters = remainingNamedParameters.tail;
296 } 319 }
297 320
298 // Loop over the remaining named parameters, and try to find 321 // Loop over the remaining named parameters, and try to find
299 // their values: either in the temporary list or using the 322 // their values: either in the temporary list or using the
300 // default value. 323 // default value.
301 for (; 324 for (;
302 !remainingNamedParameters.isEmpty(); 325 !remainingNamedParameters.isEmpty();
303 remainingNamedParameters = remainingNamedParameters.tail) { 326 remainingNamedParameters = remainingNamedParameters.tail) {
304 Element parameter = remainingNamedParameters.head; 327 Element parameter = remainingNamedParameters.head;
305 int foundIndex = -1; 328 int foundIndex = namedArguments.indexOf(parameter.name);
306 for (int i = 0; i < namedArguments.length; i++) {
307 SourceString name = namedArguments[i];
308 if (name == parameter.name) {
309 foundIndex = i;
310 break;
311 }
312 }
313 if (foundIndex != -1) { 329 if (foundIndex != -1) {
314 list.add(compiledNamedArguments[foundIndex]); 330 list.add(compiledNamedArguments[foundIndex]);
315 } else { 331 } else {
316 list.add(compileConstant(parameter)); 332 list.add(compileConstant(parameter));
317 } 333 }
318 } 334 }
335 }
336
337
338 /**
339 * Returns [:true:] if the selector and the [element] match; [:false:]
340 * otherwise.
341 */
342 bool addArgumentsToList(Link<Node> arguments,
343 List list,
344 FunctionElement element,
345 compileArgument(Node argument),
346 compileConstant(Element element),
347 Compiler compiler) {
348 if (!this.applies(element, compiler)) return false;
349
350 FunctionSignature parameters = element.computeSignature(compiler);
351 parameters.forEachRequiredParameter((element) {
352 list.add(compileArgument(arguments.head));
353 arguments = arguments.tail;
354 });
355
356 if (!parameters.optionalParametersAreNamed) {
357 if (!Compiler.REJECT_NAMED_ARGUMENT_AS_POSITIONAL) {
ahe 2012/09/13 14:29:10 TODO?
ngeoffray 2012/09/17 12:21:01 Done.
358 addOptionalArgumentsToListDEPRECATED(
359 arguments, list, element, compileArgument, compileConstant,
360 compiler);
361 } else {
362 parameters.forEachOptionalParameter((element) {
363 if (!arguments.isEmpty()) {
364 list.add(compileArgument(arguments.head));
365 arguments = arguments.tail;
366 } else {
367 list.add(compileConstant(element));
368 }
369 });
370 }
371 } else {
372 // Visit named arguments and add them into a temporary list.
373 List compiledNamedArguments = [];
374 for (; !arguments.isEmpty(); arguments = arguments.tail) {
375 NamedArgument namedArgument = arguments.head;
376 compiledNamedArguments.add(compileArgument(namedArgument.expression));
377 }
378 // Iterate over the optional parameters of the signature, and try to
379 // find them in [compiledNamedArguments]. If found, we use the
380 // value in the temporary list, otherwise the default value.
381 parameters.forEachOptionalParameter((element) {
382 int foundIndex = namedArguments.indexOf(element.name);
383 if (foundIndex != -1) {
384 list.add(compiledNamedArguments[foundIndex]);
385 } else {
386 list.add(compileConstant(element));
387 }
388 });
389 }
319 return true; 390 return true;
320 } 391 }
321 392
322 static bool sameNames(List<SourceString> first, List<SourceString> second) { 393 static bool sameNames(List<SourceString> first, List<SourceString> second) {
323 for (int i = 0; i < first.length; i++) { 394 for (int i = 0; i < first.length; i++) {
324 if (first[i] != second[i]) return false; 395 if (first[i] != second[i]) return false;
325 } 396 }
326 return true; 397 return true;
327 } 398 }
328 399
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 500
430 if (!self.isInterface() && self.isSubclassOf(other)) { 501 if (!self.isInterface() && self.isSubclassOf(other)) {
431 // Resolve an invocation of [element.name] on [self]. If it 502 // Resolve an invocation of [element.name] on [self]. If it
432 // is found, this selector is a candidate. 503 // is found, this selector is a candidate.
433 return hasElementIn(self, element) && appliesUntyped(element, compiler); 504 return hasElementIn(self, element) && appliesUntyped(element, compiler);
434 } 505 }
435 506
436 return false; 507 return false;
437 } 508 }
438 } 509 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698