| OLD | NEW |
| 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 part of resolution; | 5 part of resolution; |
| 6 | 6 |
| 7 abstract class TreeElements { | 7 abstract class TreeElements { |
| 8 Element operator[](Node node); | 8 Element operator[](Node node); |
| 9 Selector getSelector(Send send); | 9 Selector getSelector(Send send); |
| 10 DartType getType(Node node); | 10 DartType getType(Node node); |
| (...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 } | 684 } |
| 685 } | 685 } |
| 686 | 686 |
| 687 void checkUserDefinableOperator(Element member) { | 687 void checkUserDefinableOperator(Element member) { |
| 688 FunctionElement function = member.asFunctionElement(); | 688 FunctionElement function = member.asFunctionElement(); |
| 689 if (function == null) return; | 689 if (function == null) return; |
| 690 String value = member.name.stringValue; | 690 String value = member.name.stringValue; |
| 691 if (value == null) return; | 691 if (value == null) return; |
| 692 if (!(isUserDefinableOperator(value) || identical(value, 'unary-'))) return; | 692 if (!(isUserDefinableOperator(value) || identical(value, 'unary-'))) return; |
| 693 | 693 |
| 694 bool isMinus = false; |
| 694 int requiredParameterCount; | 695 int requiredParameterCount; |
| 695 MessageKind messageKind; | 696 MessageKind messageKind; |
| 696 FunctionSignature signature = function.computeSignature(compiler); | 697 FunctionSignature signature = function.computeSignature(compiler); |
| 697 if (identical(value, 'unary-')) { | 698 if (identical(value, 'unary-')) { |
| 699 isMinus = true; |
| 698 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY; | 700 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY; |
| 699 requiredParameterCount = 0; | 701 requiredParameterCount = 0; |
| 700 } else if (isMinusOperator(value)) { | 702 } else if (isMinusOperator(value)) { |
| 703 isMinus = true; |
| 701 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY; | 704 messageKind = MessageKind.MINUS_OPERATOR_BAD_ARITY; |
| 702 requiredParameterCount = 1; | 705 requiredParameterCount = 1; |
| 703 } else if (isUnaryOperator(value)) { | 706 } else if (isUnaryOperator(value)) { |
| 704 messageKind = MessageKind.UNARY_OPERATOR_BAD_ARITY; | 707 messageKind = MessageKind.UNARY_OPERATOR_BAD_ARITY; |
| 705 requiredParameterCount = 0; | 708 requiredParameterCount = 0; |
| 706 } else if (isBinaryOperator(value)) { | 709 } else if (isBinaryOperator(value)) { |
| 707 messageKind = MessageKind.BINARY_OPERATOR_BAD_ARITY; | 710 messageKind = MessageKind.BINARY_OPERATOR_BAD_ARITY; |
| 708 requiredParameterCount = 1; | 711 requiredParameterCount = 1; |
| 709 } else if (isTernaryOperator(value)) { | 712 } else if (isTernaryOperator(value)) { |
| 710 messageKind = MessageKind.TERNARY_OPERATOR_BAD_ARITY; | 713 messageKind = MessageKind.TERNARY_OPERATOR_BAD_ARITY; |
| 711 requiredParameterCount = 2; | 714 requiredParameterCount = 2; |
| 712 } else { | 715 } else { |
| 713 compiler.internalErrorOnElement(function, | 716 compiler.internalErrorOnElement(function, |
| 714 'Unexpected user defined operator $value'); | 717 'Unexpected user defined operator $value'); |
| 715 } | 718 } |
| 716 checkArity(function, requiredParameterCount, messageKind); | 719 checkArity(function, requiredParameterCount, messageKind, isMinus); |
| 717 } | 720 } |
| 718 | 721 |
| 719 void checkArity(FunctionElement function, | 722 void checkArity(FunctionElement function, |
| 720 int requiredParameterCount, MessageKind messageKind) { | 723 int requiredParameterCount, MessageKind messageKind, |
| 724 bool isMinus) { |
| 721 FunctionExpression node = function.parseNode(compiler); | 725 FunctionExpression node = function.parseNode(compiler); |
| 722 FunctionSignature signature = function.computeSignature(compiler); | 726 FunctionSignature signature = function.computeSignature(compiler); |
| 723 if (signature.requiredParameterCount != requiredParameterCount) { | 727 if (signature.requiredParameterCount != requiredParameterCount) { |
| 724 Node errorNode = node; | 728 Node errorNode = node; |
| 725 if (node.parameters != null) { | 729 if (node.parameters != null) { |
| 726 if (signature.requiredParameterCount < requiredParameterCount) { | 730 if (isMinus || |
| 731 signature.requiredParameterCount < requiredParameterCount) { |
| 732 // If there are too few parameters, point to the whole parameter list. |
| 733 // For instance |
| 734 // |
| 735 // int operator +() {} |
| 736 // ^^ |
| 737 // |
| 738 // int operator []=(value) {} |
| 739 // ^^^^^^^ |
| 740 // |
| 741 // For operator -, always point the whole parameter list, like |
| 742 // |
| 743 // int operator -(a, b) {} |
| 744 // ^^^^^^ |
| 745 // |
| 746 // instead of |
| 747 // |
| 748 // int operator -(a, b) {} |
| 749 // ^ |
| 750 // |
| 751 // since the correction might not be to remove 'b' but instead to |
| 752 // remove 'a, b'. |
| 727 errorNode = node.parameters; | 753 errorNode = node.parameters; |
| 728 } else { | 754 } else { |
| 729 errorNode = node.parameters.nodes.skip(requiredParameterCount).head; | 755 errorNode = node.parameters.nodes.skip(requiredParameterCount).head; |
| 730 } | 756 } |
| 731 } | 757 } |
| 732 compiler.reportErrorCode( | 758 compiler.reportErrorCode( |
| 733 errorNode, messageKind, {'operatorName': function.name}); | 759 errorNode, messageKind, {'operatorName': function.name}); |
| 734 } | 760 } |
| 735 if (signature.optionalParameterCount != 0) { | 761 if (signature.optionalParameterCount != 0) { |
| 736 Node errorNode = | 762 Node errorNode = |
| (...skipping 2886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3623 return e; | 3649 return e; |
| 3624 } | 3650 } |
| 3625 | 3651 |
| 3626 /// Assumed to be called by [resolveRedirectingFactory]. | 3652 /// Assumed to be called by [resolveRedirectingFactory]. |
| 3627 Element visitReturn(Return node) { | 3653 Element visitReturn(Return node) { |
| 3628 Node expression = node.expression; | 3654 Node expression = node.expression; |
| 3629 return finishConstructorReference(visit(expression), | 3655 return finishConstructorReference(visit(expression), |
| 3630 expression, expression); | 3656 expression, expression); |
| 3631 } | 3657 } |
| 3632 } | 3658 } |
| OLD | NEW |