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

Side by Side Diff: pkg/compiler/lib/src/universe/universe.dart

Issue 742023002: Handle named and optional arguments in cps-ir. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix syntax Created 6 years, 1 month 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 '../elements/elements.dart'; 7 import '../elements/elements.dart';
8 import '../dart2jslib.dart'; 8 import '../dart2jslib.dart';
9 import '../dart_types.dart'; 9 import '../dart_types.dart';
10 import '../types/types.dart'; 10 import '../types/types.dart';
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 name == element.name || 488 name == element.name ||
489 name == 'assert' && world.isAssertMethod(element); 489 name == 'assert' && world.isAssertMethod(element);
490 } 490 }
491 491
492 bool applies(Element element, World world) { 492 bool applies(Element element, World world) {
493 if (!sameNameHack(element, world)) return false; 493 if (!sameNameHack(element, world)) return false;
494 return appliesUnnamed(element, world); 494 return appliesUnnamed(element, world);
495 } 495 }
496 496
497 /** 497 /**
498 * Fills [list] with the arguments in the normalized order. 498 * Returns a `List` with the evaluated arguments in the normalized order.
499 * 499 *
500 * [compileArgument] is a function that returns a compiled version 500 * [compileArgument] is a function that returns a compiled version
501 * of an argument located in [arguments]. 501 * of an argument located in [arguments].
502 * 502 *
503 * [compileDefaultValue] is a function that returns a compiled constant 503 * [compileDefaultValue] is a function that returns a compiled constant
504 * of an optional argument that is not in [arguments]. 504 * of an optional argument that is not in [arguments].
505 * 505 *
506 * Returns [:true:] if the selector and the [element] match; [:false:] 506 * Precondition: `this.applies(element, world)`.
507 * otherwise.
508 * 507 *
509 * Invariant: [element] must be the implementation element. 508 * Invariant: [element] must be the implementation element.
510 */ 509 */
511 /*<T>*/ bool addArgumentsToList( 510 /*<S, T>*/ List/*<T>*/ makeArgumentsList(
511 List/*<S>*/ arguments,
512 FunctionElement element,
513 /*T*/ compileArgument(/*S*/ argument),
514 /*T*/ compileDefaultValue(ParameterElement element)) {
515 assert(invariant(element, element.isImplementation));
516 List/*<T>*/ result = new List();
517 FunctionSignature parameters = element.functionSignature;
518 int i = 0;
519 parameters.forEachRequiredParameter((ParameterElement element) {
520 result.add(compileArgument(arguments[i]));
521 ++i;
522 });
523
524 if (!parameters.optionalParametersAreNamed) {
525 parameters.forEachOptionalParameter((ParameterElement element) {
526 if (i < arguments.length) {
527 result.add(compileArgument(arguments[i]));
528 ++i;
529 } else {
530 result.add(compileDefaultValue(element));
531 }
532 });
533 } else {
534 // Visit named arguments and add them into a temporary list.
535 List compiledNamedArguments = [];
536 for (; i < arguments.length; ++i) {
537 compiledNamedArguments.add(compileArgument(arguments[i]));
538 }
539 // Iterate over the optional parameters of the signature, and try to
540 // find them in [compiledNamedArguments]. If found, we use the
541 // value in the temporary list, otherwise the default value.
542 parameters.orderedOptionalParameters
543 .forEach((ParameterElement element) {
544 int foundIndex = namedArguments.indexOf(element.name);
545 if (foundIndex != -1) {
546 result.add(compiledNamedArguments[foundIndex]);
547 } else {
548 result.add(compileDefaultValue(element));
549 }
550 });
551 }
552 return result;
553 }
554
555 /// This is a version of [makeArgumentsList] that works for a `Link`
556 /// representation of arguments.
557 /*<T>*/ List/*<T>*/ makeArgumentsList2(
512 Link<Node> arguments, 558 Link<Node> arguments,
513 List/*<T>*/ list,
514 FunctionElement element, 559 FunctionElement element,
515 /*T*/ compileArgument(Node argument), 560 /*T*/ compileArgument(Node argument),
516 /*T*/ compileDefaultValue(ParameterElement element), 561 /*T*/ compileDefaultValue(ParameterElement element)) {
517 World world) {
518 assert(invariant(element, element.isImplementation)); 562 assert(invariant(element, element.isImplementation));
519 if (!this.applies(element, world)) return false; 563 List/*<T>*/ result = new List();
520 564
521 FunctionSignature parameters = element.functionSignature; 565 FunctionSignature parameters = element.functionSignature;
522 parameters.forEachRequiredParameter((ParameterElement element) { 566 parameters.forEachRequiredParameter((ParameterElement element) {
523 list.add(compileArgument(arguments.head)); 567 result.add(compileArgument(arguments.head));
524 arguments = arguments.tail; 568 arguments = arguments.tail;
525 }); 569 });
526 570
527 if (!parameters.optionalParametersAreNamed) { 571 if (!parameters.optionalParametersAreNamed) {
528 parameters.forEachOptionalParameter((ParameterElement element) { 572 parameters.forEachOptionalParameter((ParameterElement element) {
529 if (!arguments.isEmpty) { 573 if (!arguments.isEmpty) {
530 list.add(compileArgument(arguments.head)); 574 result.add(compileArgument(arguments.head));
531 arguments = arguments.tail; 575 arguments = arguments.tail;
532 } else { 576 } else {
533 list.add(compileDefaultValue(element)); 577 result.add(compileDefaultValue(element));
534 } 578 }
535 }); 579 });
536 } else { 580 } else {
537 // Visit named arguments and add them into a temporary list. 581 // Visit named arguments and add them into a temporary list.
538 List compiledNamedArguments = []; 582 List compiledNamedArguments = [];
539 for (; !arguments.isEmpty; arguments = arguments.tail) { 583 for (; !arguments.isEmpty; arguments = arguments.tail) {
540 NamedArgument namedArgument = arguments.head; 584 NamedArgument namedArgument = arguments.head;
541 compiledNamedArguments.add(compileArgument(namedArgument.expression)); 585 compiledNamedArguments.add(compileArgument(namedArgument.expression));
542 } 586 }
543 // Iterate over the optional parameters of the signature, and try to 587 // Iterate over the optional parameters of the signature, and try to
544 // find them in [compiledNamedArguments]. If found, we use the 588 // find them in [compiledNamedArguments]. If found, we use the
545 // value in the temporary list, otherwise the default value. 589 // value in the temporary list, otherwise the default value.
546 parameters.orderedOptionalParameters.forEach((ParameterElement element) { 590 parameters.orderedOptionalParameters.forEach((ParameterElement element) {
547 int foundIndex = namedArguments.indexOf(element.name); 591 int foundIndex = namedArguments.indexOf(element.name);
548 if (foundIndex != -1) { 592 if (foundIndex != -1) {
549 list.add(compiledNamedArguments[foundIndex]); 593 result.add(compiledNamedArguments[foundIndex]);
550 } else { 594 } else {
551 list.add(compileDefaultValue(element)); 595 result.add(compileDefaultValue(element));
552 } 596 }
553 }); 597 });
554 } 598 }
555 return true; 599 return result;
556 } 600 }
557 601
602
558 /** 603 /**
559 * Fills [list] with the arguments in the order expected by 604 * Fills [list] with the arguments in the order expected by
560 * [callee], and where [caller] is a synthesized element 605 * [callee], and where [caller] is a synthesized element
561 * 606 *
562 * [compileArgument] is a function that returns a compiled version 607 * [compileArgument] is a function that returns a compiled version
563 * of a parameter of [callee]. 608 * of a parameter of [callee].
564 * 609 *
565 * [compileConstant] is a function that returns a compiled constant 610 * [compileConstant] is a function that returns a compiled constant
566 * of an optional argument that is not in the parameters of [callee]. 611 * of an optional argument that is not in the parameters of [callee].
567 * 612 *
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 List<String> namedParameters; 659 List<String> namedParameters;
615 if (signature.optionalParametersAreNamed) { 660 if (signature.optionalParametersAreNamed) {
616 namedParameters = 661 namedParameters =
617 signature.optionalParameters.mapToList((e) => e.name); 662 signature.optionalParameters.mapToList((e) => e.name);
618 } 663 }
619 Selector selector = new Selector.call(callee.name, 664 Selector selector = new Selector.call(callee.name,
620 caller.library, 665 caller.library,
621 signature.parameterCount, 666 signature.parameterCount,
622 namedParameters); 667 namedParameters);
623 668
624 return selector.addArgumentsToList(nodes, 669 if (!selector.applies(callee, world)) return false;
625 list, 670 list.addAll(selector.makeArgumentsList2(nodes,
626 callee, 671 callee,
627 internalCompileArgument, 672 internalCompileArgument,
628 compileConstant, 673 compileConstant));
629 world); 674
675 return true;
630 } 676 }
631 677
632 static bool sameNames(List<String> first, List<String> second) { 678 static bool sameNames(List<String> first, List<String> second) {
633 for (int i = 0; i < first.length; i++) { 679 for (int i = 0; i < first.length; i++) {
634 if (first[i] != second[i]) return false; 680 if (first[i] != second[i]) return false;
635 } 681 }
636 return true; 682 return true;
637 } 683 }
638 684
639 bool match(SelectorKind kind, 685 bool match(SelectorKind kind,
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 850
805 Selector extendIfReachesAll(Compiler compiler) { 851 Selector extendIfReachesAll(Compiler compiler) {
806 bool canReachAll = compiler.enabledInvokeOn 852 bool canReachAll = compiler.enabledInvokeOn
807 && mask.needsNoSuchMethodHandling(this, compiler.world); 853 && mask.needsNoSuchMethodHandling(this, compiler.world);
808 return canReachAll 854 return canReachAll
809 ? new TypedSelector( 855 ? new TypedSelector(
810 compiler.typesTask.dynamicType, this, compiler.world) 856 compiler.typesTask.dynamicType, this, compiler.world)
811 : this; 857 : this;
812 } 858 }
813 } 859 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698