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

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: 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)`.
floitsch 2014/11/20 13:14:43 can you assert this?
sigurdm 2014/11/21 09:52:53 Not without requiring the world as a parameter.
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 /*<T>*/ List/*<T>*/ makeArgumentsList(
512 Link<Node> arguments, 511 Link<Node> arguments,
513 List/*<T>*/ list,
514 FunctionElement element, 512 FunctionElement element,
515 /*T*/ compileArgument(Node argument), 513 /*T*/ compileArgument(Node argument),
516 /*T*/ compileDefaultValue(ParameterElement element), 514 /*T*/ compileDefaultValue(ParameterElement element)) {
517 World world) {
518 assert(invariant(element, element.isImplementation)); 515 assert(invariant(element, element.isImplementation));
519 if (!this.applies(element, world)) return false; 516 List/*<T>*/ result = new List();
520 517
521 FunctionSignature parameters = element.functionSignature; 518 FunctionSignature parameters = element.functionSignature;
522 parameters.forEachRequiredParameter((ParameterElement element) { 519 parameters.forEachRequiredParameter((ParameterElement element) {
523 list.add(compileArgument(arguments.head)); 520 result.add(compileArgument(arguments.head));
524 arguments = arguments.tail; 521 arguments = arguments.tail;
525 }); 522 });
526 523
527 if (!parameters.optionalParametersAreNamed) { 524 if (!parameters.optionalParametersAreNamed) {
528 parameters.forEachOptionalParameter((ParameterElement element) { 525 parameters.forEachOptionalParameter((ParameterElement element) {
529 if (!arguments.isEmpty) { 526 if (!arguments.isEmpty) {
530 list.add(compileArgument(arguments.head)); 527 result.add(compileArgument(arguments.head));
531 arguments = arguments.tail; 528 arguments = arguments.tail;
532 } else { 529 } else {
533 list.add(compileDefaultValue(element)); 530 result.add(compileDefaultValue(element));
534 } 531 }
535 }); 532 });
536 } else { 533 } else {
537 // Visit named arguments and add them into a temporary list. 534 // Visit named arguments and add them into a temporary list.
538 List compiledNamedArguments = []; 535 List compiledNamedArguments = [];
539 for (; !arguments.isEmpty; arguments = arguments.tail) { 536 for (; !arguments.isEmpty; arguments = arguments.tail) {
540 NamedArgument namedArgument = arguments.head; 537 NamedArgument namedArgument = arguments.head;
541 compiledNamedArguments.add(compileArgument(namedArgument.expression)); 538 compiledNamedArguments.add(compileArgument(namedArgument.expression));
542 } 539 }
543 // Iterate over the optional parameters of the signature, and try to 540 // Iterate over the optional parameters of the signature, and try to
544 // find them in [compiledNamedArguments]. If found, we use the 541 // find them in [compiledNamedArguments]. If found, we use the
545 // value in the temporary list, otherwise the default value. 542 // value in the temporary list, otherwise the default value.
546 parameters.orderedOptionalParameters.forEach((ParameterElement element) { 543 parameters.orderedOptionalParameters.forEach((ParameterElement element) {
547 int foundIndex = namedArguments.indexOf(element.name); 544 int foundIndex = namedArguments.indexOf(element.name);
548 if (foundIndex != -1) { 545 if (foundIndex != -1) {
549 list.add(compiledNamedArguments[foundIndex]); 546 result.add(compiledNamedArguments[foundIndex]);
550 } else { 547 } else {
551 list.add(compileDefaultValue(element)); 548 result.add(compileDefaultValue(element));
552 } 549 }
553 }); 550 });
554 } 551 }
555 return true; 552 return result;
553 }
554
555 /// This is a version of [makeArgumentsList] that works for the tree_ir
556 /// representation of arguments.
floitsch 2014/11/20 13:14:43 Reverse the order. The Ssa-one is the one that wil
sigurdm 2014/11/21 09:52:53 Done.
557 /*<S, T>*/ List/*<T>*/ makeArgumentsList2(
558 List/*<S>*/ arguments,
559 FunctionElement element,
560 /*T*/ compileArgument(/*S*/ argument),
561 /*T*/ compileDefaultValue(ParameterElement element)) {
562 assert(invariant(element, element.isImplementation));
563 List/*<T>*/ result = new List();
564 FunctionSignature parameters = element.functionSignature;
565 int i = 0;
566 parameters.forEachRequiredParameter((ParameterElement element) {
567 result.add(compileArgument(arguments[i]));
568 ++i;
569 });
570
571 if (!parameters.optionalParametersAreNamed) {
572 parameters.forEachOptionalParameter((ParameterElement element) {
573 if (i < arguments.length) {
574 result.add(compileArgument(arguments[i]));
575 ++i;
576 } else {
577 result.add(compileDefaultValue(element));
578 }
579 });
580 } else {
581 // Visit named arguments and add them into a temporary list.
582 List compiledNamedArguments = [];
583 for (; i < arguments.length; ++i) {
584 compiledNamedArguments.add(compileArgument(arguments[i]));
585 }
586 // Iterate over the optional parameters of the signature, and try to
587 // find them in [compiledNamedArguments]. If found, we use the
588 // value in the temporary list, otherwise the default value.
589 parameters.orderedOptionalParameters
590 .forEach((ParameterElement element) {
591 int foundIndex = namedArguments.indexOf(element.name);
592 if (foundIndex != -1) {
593 result.add(compiledNamedArguments[foundIndex]);
594 } else {
595 result.add(compileDefaultValue(element));
596 }
597 });
598 }
599 return result;
556 } 600 }
557 601
558 /** 602 /**
559 * Fills [list] with the arguments in the order expected by 603 * Fills [list] with the arguments in the order expected by
560 * [callee], and where [caller] is a synthesized element 604 * [callee], and where [caller] is a synthesized element
561 * 605 *
562 * [compileArgument] is a function that returns a compiled version 606 * [compileArgument] is a function that returns a compiled version
563 * of a parameter of [callee]. 607 * of a parameter of [callee].
564 * 608 *
565 * [compileConstant] is a function that returns a compiled constant 609 * [compileConstant] is a function that returns a compiled constant
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 List<String> namedParameters; 658 List<String> namedParameters;
615 if (signature.optionalParametersAreNamed) { 659 if (signature.optionalParametersAreNamed) {
616 namedParameters = 660 namedParameters =
617 signature.optionalParameters.mapToList((e) => e.name); 661 signature.optionalParameters.mapToList((e) => e.name);
618 } 662 }
619 Selector selector = new Selector.call(callee.name, 663 Selector selector = new Selector.call(callee.name,
620 caller.library, 664 caller.library,
621 signature.parameterCount, 665 signature.parameterCount,
622 namedParameters); 666 namedParameters);
623 667
624 return selector.addArgumentsToList(nodes, 668 if (!selector.applies(callee, world)) return false;
625 list, 669 list.addAll(selector.makeArgumentsList(nodes,
626 callee, 670 callee,
627 internalCompileArgument, 671 internalCompileArgument,
628 compileConstant, 672 compileConstant));
629 world); 673
674 return true;
630 } 675 }
631 676
632 static bool sameNames(List<String> first, List<String> second) { 677 static bool sameNames(List<String> first, List<String> second) {
633 for (int i = 0; i < first.length; i++) { 678 for (int i = 0; i < first.length; i++) {
634 if (first[i] != second[i]) return false; 679 if (first[i] != second[i]) return false;
635 } 680 }
636 return true; 681 return true;
637 } 682 }
638 683
639 bool match(SelectorKind kind, 684 bool match(SelectorKind kind,
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 849
805 Selector extendIfReachesAll(Compiler compiler) { 850 Selector extendIfReachesAll(Compiler compiler) {
806 bool canReachAll = compiler.enabledInvokeOn 851 bool canReachAll = compiler.enabledInvokeOn
807 && mask.needsNoSuchMethodHandling(this, compiler.world); 852 && mask.needsNoSuchMethodHandling(this, compiler.world);
808 return canReachAll 853 return canReachAll
809 ? new TypedSelector( 854 ? new TypedSelector(
810 compiler.typesTask.dynamicType, this, compiler.world) 855 compiler.typesTask.dynamicType, this, compiler.world)
811 : this; 856 : this;
812 } 857 }
813 } 858 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698