| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.body_builder; | 5 library fasta.body_builder; |
| 6 | 6 |
| 7 import '../parser/parser.dart' show FormalParameterType, optional; | 7 import '../parser/parser.dart' show FormalParameterType, optional; |
| 8 | 8 |
| 9 import '../parser/error_kind.dart' show ErrorKind; | 9 import '../parser/error_kind.dart' show ErrorKind; |
| 10 | 10 |
| (...skipping 1604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1615 push(name); | 1615 push(name); |
| 1616 } | 1616 } |
| 1617 | 1617 |
| 1618 @override | 1618 @override |
| 1619 Expression buildStaticInvocation(Member target, Arguments arguments, | 1619 Expression buildStaticInvocation(Member target, Arguments arguments, |
| 1620 {bool isConst: false, int charOffset: -1}) { | 1620 {bool isConst: false, int charOffset: -1}) { |
| 1621 List<TypeParameter> typeParameters = target.function.typeParameters; | 1621 List<TypeParameter> typeParameters = target.function.typeParameters; |
| 1622 if (target is Constructor) { | 1622 if (target is Constructor) { |
| 1623 typeParameters = target.enclosingClass.typeParameters; | 1623 typeParameters = target.enclosingClass.typeParameters; |
| 1624 } | 1624 } |
| 1625 if (!addDefaultArguments(target.function, arguments, typeParameters)) { | 1625 if (!checkArguments(target.function, arguments, typeParameters)) { |
| 1626 return throwNoSuchMethodError(target.name.name, arguments, charOffset); | 1626 return throwNoSuchMethodError(target.name.name, arguments, charOffset); |
| 1627 } | 1627 } |
| 1628 if (target is Constructor) { | 1628 if (target is Constructor) { |
| 1629 return new ConstructorInvocation(target, arguments)..isConst = isConst; | 1629 return new ConstructorInvocation(target, arguments)..isConst = isConst; |
| 1630 } else { | 1630 } else { |
| 1631 return new StaticInvocation(target, arguments)..isConst = isConst; | 1631 return new StaticInvocation(target, arguments)..isConst = isConst; |
| 1632 } | 1632 } |
| 1633 } | 1633 } |
| 1634 | 1634 |
| 1635 bool addDefaultArguments(FunctionNode function, Arguments arguments, | 1635 bool checkArguments(FunctionNode function, Arguments arguments, |
| 1636 List<TypeParameter> typeParameters) { | 1636 List<TypeParameter> typeParameters) { |
| 1637 bool missingInitializers = false; | |
| 1638 | |
| 1639 Expression defaultArgumentFrom(Expression expression) { | |
| 1640 if (expression == null) { | |
| 1641 missingInitializers = true; | |
| 1642 return null; | |
| 1643 } | |
| 1644 cloner ??= new CloneVisitor(); | |
| 1645 return cloner.clone(expression); | |
| 1646 } | |
| 1647 | 1637 |
| 1648 if (arguments.positional.length < function.requiredParameterCount || | 1638 if (arguments.positional.length < function.requiredParameterCount || |
| 1649 arguments.positional.length > function.positionalParameters.length) { | 1639 arguments.positional.length > function.positionalParameters.length) { |
| 1650 return false; | 1640 return false; |
| 1651 } | 1641 } |
| 1652 for (int i = arguments.positional.length; | |
| 1653 i < function.positionalParameters.length; | |
| 1654 i++) { | |
| 1655 var expression = | |
| 1656 defaultArgumentFrom(function.positionalParameters[i].initializer); | |
| 1657 expression?.parent = arguments; | |
| 1658 arguments.positional.add(expression); | |
| 1659 } | |
| 1660 Map<String, VariableDeclaration> names; | 1642 Map<String, VariableDeclaration> names; |
| 1661 if (function.namedParameters.isNotEmpty) { | 1643 if (function.namedParameters.isNotEmpty) { |
| 1662 names = <String, VariableDeclaration>{}; | 1644 names = <String, VariableDeclaration>{}; |
| 1663 for (VariableDeclaration parameter in function.namedParameters) { | 1645 for (VariableDeclaration parameter in function.namedParameters) { |
| 1664 names[parameter.name] = parameter; | 1646 names[parameter.name] = parameter; |
| 1665 } | 1647 } |
| 1666 } | 1648 } |
| 1667 if (arguments.named.isNotEmpty) { | 1649 if (arguments.named.isNotEmpty) { |
| 1668 if (names == null) return false; | 1650 if (names == null) return false; |
| 1669 for (NamedExpression argument in arguments.named) { | 1651 for (NamedExpression argument in arguments.named) { |
| 1670 VariableDeclaration parameter = names.remove(argument.name); | 1652 VariableDeclaration parameter = names.remove(argument.name); |
| 1671 if (parameter == null) { | 1653 if (parameter == null) { |
| 1672 return false; | 1654 return false; |
| 1673 } | 1655 } |
| 1674 } | 1656 } |
| 1675 } | 1657 } |
| 1676 if (names != null) { | |
| 1677 for (String name in names.keys) { | |
| 1678 VariableDeclaration parameter = names[name]; | |
| 1679 arguments.named.add(new NamedExpression( | |
| 1680 name, defaultArgumentFrom(parameter.initializer)) | |
| 1681 ..parent = arguments); | |
| 1682 } | |
| 1683 } | |
| 1684 if (typeParameters.length != arguments.types.length) { | 1658 if (typeParameters.length != arguments.types.length) { |
| 1685 arguments.types.clear(); | 1659 arguments.types.clear(); |
| 1686 for (int i = 0; i < typeParameters.length; i++) { | 1660 for (int i = 0; i < typeParameters.length; i++) { |
| 1687 arguments.types.add(const DynamicType()); | 1661 arguments.types.add(const DynamicType()); |
| 1688 } | 1662 } |
| 1689 } | 1663 } |
| 1690 | 1664 |
| 1691 if (missingInitializers) { | |
| 1692 library.addArgumentsWithMissingDefaultValues(arguments, function); | |
| 1693 } | |
| 1694 return true; | 1665 return true; |
| 1695 } | 1666 } |
| 1696 | 1667 |
| 1697 @override | 1668 @override |
| 1698 void handleNewExpression(Token token) { | 1669 void handleNewExpression(Token token) { |
| 1699 debugEvent("NewExpression"); | 1670 debugEvent("NewExpression"); |
| 1700 Arguments arguments = pop(); | 1671 Arguments arguments = pop(); |
| 1701 String name = pop(); | 1672 String name = pop(); |
| 1702 List<DartType> typeArguments = pop(); | 1673 List<DartType> typeArguments = pop(); |
| 1703 var type = pop(); | 1674 var type = pop(); |
| (...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2778 } else if (node is PrefixBuilder) { | 2749 } else if (node is PrefixBuilder) { |
| 2779 return node.name; | 2750 return node.name; |
| 2780 } else if (node is ThisAccessor) { | 2751 } else if (node is ThisAccessor) { |
| 2781 return node.isSuper ? "super" : "this"; | 2752 return node.isSuper ? "super" : "this"; |
| 2782 } else if (node is BuilderAccessor) { | 2753 } else if (node is BuilderAccessor) { |
| 2783 return node.plainNameForRead; | 2754 return node.plainNameForRead; |
| 2784 } else { | 2755 } else { |
| 2785 return internalError("Unhandled: ${node.runtimeType}"); | 2756 return internalError("Unhandled: ${node.runtimeType}"); |
| 2786 } | 2757 } |
| 2787 } | 2758 } |
| OLD | NEW |