| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dart2js.call_structure; | 5 library dart2js.call_structure; |
| 6 | 6 |
| 7 import '../common/names.dart' show Names; | 7 import '../common/names.dart' show Names; |
| 8 import '../elements/types.dart' show FunctionType; | 8 import '../elements/entities.dart' show ParameterStructure; |
| 9 import '../util/util.dart'; | 9 import '../util/util.dart'; |
| 10 import 'selector.dart' show Selector; | 10 import 'selector.dart' show Selector; |
| 11 | 11 |
| 12 /// The structure of the arguments at a call-site. | 12 /// The structure of the arguments at a call-site. |
| 13 // TODO(johnniwinther): Should these be cached? | 13 // TODO(johnniwinther): Should these be cached? |
| 14 // TODO(johnniwinther): Should isGetter/isSetter be part of the call structure | 14 // TODO(johnniwinther): Should isGetter/isSetter be part of the call structure |
| 15 // instead of the selector? | 15 // instead of the selector? |
| 16 class CallStructure { | 16 class CallStructure { |
| 17 static const CallStructure NO_ARGS = const CallStructure.unnamed(0); | 17 static const CallStructure NO_ARGS = const CallStructure.unnamed(0); |
| 18 static const CallStructure ONE_ARG = const CallStructure.unnamed(1); | 18 static const CallStructure ONE_ARG = const CallStructure.unnamed(1); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int get hashCode { | 67 int get hashCode { |
| 68 return Hashing.listHash(namedArguments, | 68 return Hashing.listHash(namedArguments, |
| 69 Hashing.objectHash(argumentCount, namedArguments.length)); | 69 Hashing.objectHash(argumentCount, namedArguments.length)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool operator ==(other) { | 72 bool operator ==(other) { |
| 73 if (other is! CallStructure) return false; | 73 if (other is! CallStructure) return false; |
| 74 return match(other); | 74 return match(other); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool signatureApplies(FunctionType type) { | 77 bool signatureApplies(ParameterStructure parameters) { |
| 78 int requiredParameterCount = type.parameterTypes.length; | 78 int requiredParameterCount = parameters.requiredParameters; |
| 79 int optionalParameterCount = | 79 int optionalParameterCount = parameters.optionalParameters; |
| 80 type.optionalParameterTypes.length + type.namedParameters.length; | |
| 81 int parameterCount = requiredParameterCount + optionalParameterCount; | 80 int parameterCount = requiredParameterCount + optionalParameterCount; |
| 82 if (argumentCount > parameterCount) return false; | 81 if (argumentCount > parameterCount) return false; |
| 83 if (positionalArgumentCount < requiredParameterCount) return false; | 82 if (positionalArgumentCount < requiredParameterCount) return false; |
| 84 | 83 |
| 85 if (type.namedParameters.isEmpty) { | 84 if (parameters.namedParameters.isEmpty) { |
| 86 // We have already checked that the number of arguments are | 85 // We have already checked that the number of arguments are |
| 87 // not greater than the number of parameters. Therefore the | 86 // not greater than the number of parameters. Therefore the |
| 88 // number of positional arguments are not greater than the | 87 // number of positional arguments are not greater than the |
| 89 // number of parameters. | 88 // number of parameters. |
| 90 assert(positionalArgumentCount <= parameterCount); | 89 assert(positionalArgumentCount <= parameterCount); |
| 91 return namedArguments.isEmpty; | 90 return namedArguments.isEmpty; |
| 92 } else { | 91 } else { |
| 93 if (positionalArgumentCount > requiredParameterCount) return false; | 92 if (positionalArgumentCount > requiredParameterCount) return false; |
| 94 assert(positionalArgumentCount == requiredParameterCount); | 93 assert(positionalArgumentCount == requiredParameterCount); |
| 95 if (namedArgumentCount > optionalParameterCount) return false; | 94 if (namedArgumentCount > optionalParameterCount) return false; |
| 96 | 95 |
| 97 int nameIndex = 0; | 96 int nameIndex = 0; |
| 98 List<String> namedParameters = type.namedParameters; | 97 List<String> namedParameters = parameters.namedParameters; |
| 99 for (String name in getOrderedNamedArguments()) { | 98 for (String name in getOrderedNamedArguments()) { |
| 100 bool found = false; | 99 bool found = false; |
| 101 // Note: we start at the existing index because arguments are sorted. | 100 // Note: we start at the existing index because arguments are sorted. |
| 102 while (nameIndex < namedParameters.length) { | 101 while (nameIndex < namedParameters.length) { |
| 103 if (name == namedParameters[nameIndex]) { | 102 if (name == namedParameters[nameIndex]) { |
| 104 found = true; | 103 found = true; |
| 105 break; | 104 break; |
| 106 } | 105 } |
| 107 nameIndex++; | 106 nameIndex++; |
| 108 } | 107 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return first.compareTo(second); | 150 return first.compareTo(second); |
| 152 }); | 151 }); |
| 153 return _orderedNamedArguments; | 152 return _orderedNamedArguments; |
| 154 } | 153 } |
| 155 | 154 |
| 156 @override | 155 @override |
| 157 String structureToString() { | 156 String structureToString() { |
| 158 return 'arity=$argumentCount, named=[${namedArguments.join(', ')}]'; | 157 return 'arity=$argumentCount, named=[${namedArguments.join(', ')}]'; |
| 159 } | 158 } |
| 160 } | 159 } |
| OLD | NEW |