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 #library('universe'); | 5 #library('universe'); |
6 | 6 |
7 #import('../closure.dart'); | 7 #import('../closure.dart'); |
8 #import('../elements/elements.dart'); | 8 #import('../elements/elements.dart'); |
9 #import('../leg.dart'); | 9 #import('../leg.dart'); |
10 #import('../scanner/scannerlib.dart'); | 10 #import('../scanner/scannerlib.dart'); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 if (element.isField()) return isGetter() || isSetter() || isCall(); | 208 if (element.isField()) return isGetter() || isSetter() || isCall(); |
209 if (isGetter()) return true; | 209 if (isGetter()) return true; |
210 | 210 |
211 FunctionElement function = element; | 211 FunctionElement function = element; |
212 FunctionSignature parameters = function.computeSignature(compiler); | 212 FunctionSignature parameters = function.computeSignature(compiler); |
213 if (argumentCount > parameters.parameterCount) return false; | 213 if (argumentCount > parameters.parameterCount) return false; |
214 int requiredParameterCount = parameters.requiredParameterCount; | 214 int requiredParameterCount = parameters.requiredParameterCount; |
215 int optionalParameterCount = parameters.optionalParameterCount; | 215 int optionalParameterCount = parameters.optionalParameterCount; |
216 if (positionalArgumentCount < requiredParameterCount) return false; | 216 if (positionalArgumentCount < requiredParameterCount) return false; |
217 | 217 |
| 218 if (!parameters.optionalParametersAreNamed) { |
| 219 if (!Compiler.REJECT_NAMED_ARGUMENT_AS_POSITIONAL) { |
| 220 return optionalParametersAppliesDEPRECATED(element, compiler); |
| 221 } else { |
| 222 return namedArguments.isEmpty(); |
| 223 } |
| 224 } else { |
| 225 if (positionalArgumentCount > requiredParameterCount) return false; |
| 226 if (namedArgumentCount > optionalParameterCount) return false; |
| 227 Set<SourceString> nameSet = new Set<SourceString>(); |
| 228 parameters.optionalParameters.forEach((Element element) { |
| 229 nameSet.add(element.name); |
| 230 }); |
| 231 for (SourceString name in namedArguments) { |
| 232 if (!nameSet.contains(name)) return false; |
| 233 nameSet.remove(name); |
| 234 } |
| 235 return true; |
| 236 } |
| 237 } |
| 238 |
| 239 // TODO(5074): Remove this method once we don't accept the |
| 240 // deprecated parameter specification. |
| 241 bool optionalParametersAppliesDEPRECATED(FunctionElement function, |
| 242 Compiler compiler) { |
| 243 FunctionSignature parameters = function.computeSignature(compiler); |
| 244 int requiredParameterCount = parameters.requiredParameterCount; |
| 245 int optionalParameterCount = parameters.optionalParameterCount; |
| 246 |
218 bool hasOptionalParameters = !parameters.optionalParameters.isEmpty(); | 247 bool hasOptionalParameters = !parameters.optionalParameters.isEmpty(); |
219 if (namedArguments.isEmpty()) { | 248 if (namedArguments.isEmpty()) { |
220 if (!hasOptionalParameters) { | 249 if (!hasOptionalParameters) { |
221 return requiredParameterCount == argumentCount; | 250 return requiredParameterCount == argumentCount; |
222 } else { | 251 } else { |
223 return argumentCount >= requiredParameterCount && | 252 return argumentCount >= requiredParameterCount && |
224 argumentCount <= requiredParameterCount + optionalParameterCount; | 253 argumentCount <= requiredParameterCount + optionalParameterCount; |
225 } | 254 } |
226 } else { | 255 } else { |
227 if (!hasOptionalParameters) return false; | 256 if (!hasOptionalParameters) return false; |
(...skipping 11 matching lines...) Expand all Loading... |
239 for (SourceString name in namedArguments) { | 268 for (SourceString name in namedArguments) { |
240 if (!nameSet.contains(name)) { | 269 if (!nameSet.contains(name)) { |
241 return false; | 270 return false; |
242 } | 271 } |
243 nameSet.remove(name); | 272 nameSet.remove(name); |
244 } | 273 } |
245 return true; | 274 return true; |
246 } | 275 } |
247 } | 276 } |
248 | 277 |
249 /** | 278 // TODO(5074): Remove this method once we don't accept the |
250 * Returns [:true:] if the selector and the [element] match; [:false:] | 279 // deprecated parameter specification. |
251 * otherwise. | 280 bool addOptionalArgumentsToListDEPRECATED(Link<Node> arguments, |
252 */ | 281 List list, |
253 bool addArgumentsToList(Link<Node> arguments, | 282 FunctionElement element, |
254 List list, | 283 compileArgument(Node argument), |
255 FunctionElement element, | 284 compileConstant(Element element), |
256 compileArgument(Node argument), | 285 Compiler compiler) { |
257 compileConstant(Element element), | |
258 Compiler compiler) { | |
259 if (!this.applies(element, compiler)) return false; | |
260 | |
261 void addMatchingArgumentsToList(Link<Node> link) {} | |
262 | |
263 FunctionSignature parameters = element.computeSignature(compiler); | |
264 if (this.positionalArgumentCount == parameters.parameterCount) { | |
265 for (Link<Node> link = arguments; !link.isEmpty(); link = link.tail) { | |
266 list.add(compileArgument(link.head)); | |
267 } | |
268 return true; | |
269 } | |
270 | |
271 // If there are named arguments, provide them in the order | 286 // If there are named arguments, provide them in the order |
272 // expected by the called function, which is the source order. | 287 // expected by the called function, which is the source order. |
| 288 FunctionSignature parameters = element.computeSignature(compiler); |
273 | 289 |
274 // Visit positional arguments and add them to the list. | 290 // Visit positional arguments and add them to the list. |
275 int positionalArgumentCount = this.positionalArgumentCount; | 291 for (int i = parameters.requiredParameterCount; |
276 for (int i = 0; | |
277 i < positionalArgumentCount; | 292 i < positionalArgumentCount; |
278 arguments = arguments.tail, i++) { | 293 arguments = arguments.tail, i++) { |
279 list.add(compileArgument(arguments.head)); | 294 list.add(compileArgument(arguments.head)); |
280 } | 295 } |
281 | 296 |
282 // Visit named arguments and add them into a temporary list. | 297 // Visit named arguments and add them into a temporary list. |
283 List compiledNamedArguments = []; | 298 List compiledNamedArguments = []; |
284 for (; !arguments.isEmpty(); arguments = arguments.tail) { | 299 for (; !arguments.isEmpty(); arguments = arguments.tail) { |
285 NamedArgument namedArgument = arguments.head; | 300 NamedArgument namedArgument = arguments.head; |
286 compiledNamedArguments.add(compileArgument(namedArgument.expression)); | 301 compiledNamedArguments.add(compileArgument(namedArgument.expression)); |
(...skipping 22 matching lines...) Expand all Loading... |
309 foundIndex = i; | 324 foundIndex = i; |
310 break; | 325 break; |
311 } | 326 } |
312 } | 327 } |
313 if (foundIndex != -1) { | 328 if (foundIndex != -1) { |
314 list.add(compiledNamedArguments[foundIndex]); | 329 list.add(compiledNamedArguments[foundIndex]); |
315 } else { | 330 } else { |
316 list.add(compileConstant(parameter)); | 331 list.add(compileConstant(parameter)); |
317 } | 332 } |
318 } | 333 } |
| 334 } |
| 335 |
| 336 |
| 337 /** |
| 338 * Returns [:true:] if the selector and the [element] match; [:false:] |
| 339 * otherwise. |
| 340 */ |
| 341 bool addArgumentsToList(Link<Node> arguments, |
| 342 List list, |
| 343 FunctionElement element, |
| 344 compileArgument(Node argument), |
| 345 compileConstant(Element element), |
| 346 Compiler compiler) { |
| 347 if (!this.applies(element, compiler)) return false; |
| 348 |
| 349 FunctionSignature parameters = element.computeSignature(compiler); |
| 350 parameters.forEachRequiredParameter((element) { |
| 351 list.add(compileArgument(arguments.head)); |
| 352 arguments = arguments.tail; |
| 353 }); |
| 354 |
| 355 if (!parameters.optionalParametersAreNamed) { |
| 356 if (!Compiler.REJECT_NAMED_ARGUMENT_AS_POSITIONAL) { |
| 357 addOptionalArgumentsToListDEPRECATED( |
| 358 arguments, list, element, compileArgument, compileConstant, |
| 359 compiler); |
| 360 } else { |
| 361 parameters.forEachOptionalParameter((element) { |
| 362 if (!arguments.isEmpty()) { |
| 363 list.add(compileArgument(arguments.head)); |
| 364 arguments = arguments.tail; |
| 365 } else { |
| 366 list.add(compileConstant(element)); |
| 367 } |
| 368 }); |
| 369 } |
| 370 } else { |
| 371 // Visit named arguments and add them into a temporary list. |
| 372 List compiledNamedArguments = []; |
| 373 for (; !arguments.isEmpty(); arguments = arguments.tail) { |
| 374 NamedArgument namedArgument = arguments.head; |
| 375 compiledNamedArguments.add(compileArgument(namedArgument.expression)); |
| 376 } |
| 377 // Iterate over the optional parameters of the signature, and try to |
| 378 // find them in [compiledNamedArguments]. If found, we use the |
| 379 // value in the temporary list, otherwise the default value. |
| 380 parameters.forEachOptionalParameter((element) { |
| 381 int foundIndex = -1; |
| 382 for (int i = 0; i < namedArguments.length; i++) { |
| 383 SourceString name = namedArguments[i]; |
| 384 if (name == element.name) { |
| 385 foundIndex = i; |
| 386 break; |
| 387 } |
| 388 } |
| 389 if (foundIndex != -1) { |
| 390 list.add(compiledNamedArguments[foundIndex]); |
| 391 } else { |
| 392 list.add(compileConstant(element)); |
| 393 } |
| 394 }); |
| 395 } |
319 return true; | 396 return true; |
320 } | 397 } |
321 | 398 |
322 static bool sameNames(List<SourceString> first, List<SourceString> second) { | 399 static bool sameNames(List<SourceString> first, List<SourceString> second) { |
323 for (int i = 0; i < first.length; i++) { | 400 for (int i = 0; i < first.length; i++) { |
324 if (first[i] != second[i]) return false; | 401 if (first[i] != second[i]) return false; |
325 } | 402 } |
326 return true; | 403 return true; |
327 } | 404 } |
328 | 405 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 | 506 |
430 if (!self.isInterface() && self.isSubclassOf(other)) { | 507 if (!self.isInterface() && self.isSubclassOf(other)) { |
431 // Resolve an invocation of [element.name] on [self]. If it | 508 // Resolve an invocation of [element.name] on [self]. If it |
432 // is found, this selector is a candidate. | 509 // is found, this selector is a candidate. |
433 return hasElementIn(self, element) && appliesUntyped(element, compiler); | 510 return hasElementIn(self, element) && appliesUntyped(element, compiler); |
434 } | 511 } |
435 | 512 |
436 return false; | 513 return false; |
437 } | 514 } |
438 } | 515 } |
OLD | NEW |