| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.resolution.signatures; | 5 library dart2js.resolution.signatures; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common/resolution.dart'; | 8 import '../common/resolution.dart'; |
| 9 import '../elements/resolution_types.dart'; | 9 import '../elements/resolution_types.dart'; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // Inline function typed initializing formal or | 141 // Inline function typed initializing formal or |
| 142 // parameter with default value, like `C(int this.f(String s))` or | 142 // parameter with default value, like `C(int this.f(String s))` or |
| 143 // `void m([int f(String s) = null])`. | 143 // `void m([int f(String s) = null])`. |
| 144 computeFunctionType(link.head.asSend().selector.asFunctionExpression()); | 144 computeFunctionType(link.head.asSend().selector.asFunctionExpression()); |
| 145 } else { | 145 } else { |
| 146 assert(invariant(currentDefinitions, | 146 assert(invariant(currentDefinitions, |
| 147 link.head.asIdentifier() != null || link.head.asSend() != null)); | 147 link.head.asIdentifier() != null || link.head.asSend() != null)); |
| 148 if (fieldElement != null) { | 148 if (fieldElement != null) { |
| 149 element.typeCache = fieldElement.computeType(resolution); | 149 element.typeCache = fieldElement.computeType(resolution); |
| 150 } else { | 150 } else { |
| 151 element.typeCache = const DynamicType(); | 151 element.typeCache = const ResolutionDynamicType(); |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 FormalElementX visitIdentifier(Identifier node) { | 157 FormalElementX visitIdentifier(Identifier node) { |
| 158 return createParameter(node, null); | 158 return createParameter(node, null); |
| 159 } | 159 } |
| 160 | 160 |
| 161 Identifier getParameterName(Send node) { | 161 Identifier getParameterName(Send node) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 NodeList typeVariables, | 299 NodeList typeVariables, |
| 300 NodeList formalParameters, | 300 NodeList formalParameters, |
| 301 Node returnNode, | 301 Node returnNode, |
| 302 FunctionTypedElement element, | 302 FunctionTypedElement element, |
| 303 ResolutionRegistry registry, | 303 ResolutionRegistry registry, |
| 304 {MessageKind defaultValuesError, | 304 {MessageKind defaultValuesError, |
| 305 bool createRealParameters: false, | 305 bool createRealParameters: false, |
| 306 bool isFunctionExpression: false}) { | 306 bool isFunctionExpression: false}) { |
| 307 DiagnosticReporter reporter = resolution.reporter; | 307 DiagnosticReporter reporter = resolution.reporter; |
| 308 | 308 |
| 309 List<DartType> createTypeVariables(NodeList typeVariableNodes) { | 309 List<ResolutionDartType> createTypeVariables(NodeList typeVariableNodes) { |
| 310 if (element.isPatch) { | 310 if (element.isPatch) { |
| 311 FunctionTypedElement origin = element.origin; | 311 FunctionTypedElement origin = element.origin; |
| 312 origin.computeType(resolution); | 312 origin.computeType(resolution); |
| 313 return origin.typeVariables; | 313 return origin.typeVariables; |
| 314 } | 314 } |
| 315 if (typeVariableNodes == null) return const <DartType>[]; | 315 if (typeVariableNodes == null) return const <ResolutionDartType>[]; |
| 316 | 316 |
| 317 // Create the types and elements corresponding to [typeVariableNodes]. | 317 // Create the types and elements corresponding to [typeVariableNodes]. |
| 318 Link<Node> nodes = typeVariableNodes.nodes; | 318 Link<Node> nodes = typeVariableNodes.nodes; |
| 319 List<DartType> arguments = | 319 List<ResolutionDartType> arguments = |
| 320 new List.generate(nodes.slowLength(), (int index) { | 320 new List.generate(nodes.slowLength(), (int index) { |
| 321 TypeVariable node = nodes.head; | 321 TypeVariable node = nodes.head; |
| 322 String variableName = node.name.source; | 322 String variableName = node.name.source; |
| 323 nodes = nodes.tail; | 323 nodes = nodes.tail; |
| 324 TypeVariableElementX variableElement = | 324 TypeVariableElementX variableElement = |
| 325 new TypeVariableElementX(variableName, element, index, node); | 325 new TypeVariableElementX(variableName, element, index, node); |
| 326 // GENERIC_METHODS: When method type variables are implemented fully we | 326 // GENERIC_METHODS: When method type variables are implemented fully we |
| 327 // must resolve the actual bounds; currently we just claim that | 327 // must resolve the actual bounds; currently we just claim that |
| 328 // every method type variable has upper bound [dynamic]. | 328 // every method type variable has upper bound [dynamic]. |
| 329 variableElement.boundCache = const DynamicType(); | 329 variableElement.boundCache = const ResolutionDynamicType(); |
| 330 TypeVariableType variableType = | 330 ResolutionTypeVariableType variableType = |
| 331 new MethodTypeVariableType(variableElement); | 331 new MethodTypeVariableType(variableElement); |
| 332 variableElement.typeCache = variableType; | 332 variableElement.typeCache = variableType; |
| 333 return variableType; | 333 return variableType; |
| 334 }, growable: false); | 334 }, growable: false); |
| 335 return arguments; | 335 return arguments; |
| 336 } | 336 } |
| 337 | 337 |
| 338 List<DartType> typeVariableTypes = createTypeVariables(typeVariables); | 338 List<ResolutionDartType> typeVariableTypes = |
| 339 createTypeVariables(typeVariables); |
| 339 scope = new FunctionSignatureBuildingScope(scope, typeVariableTypes); | 340 scope = new FunctionSignatureBuildingScope(scope, typeVariableTypes); |
| 340 SignatureResolver visitor = new SignatureResolver( | 341 SignatureResolver visitor = new SignatureResolver( |
| 341 resolution, element, scope, registry, | 342 resolution, element, scope, registry, |
| 342 defaultValuesError: defaultValuesError, | 343 defaultValuesError: defaultValuesError, |
| 343 createRealParameters: createRealParameters); | 344 createRealParameters: createRealParameters); |
| 344 List<Element> parameters = const <Element>[]; | 345 List<Element> parameters = const <Element>[]; |
| 345 int requiredParameterCount = 0; | 346 int requiredParameterCount = 0; |
| 346 if (formalParameters == null) { | 347 if (formalParameters == null) { |
| 347 if (!element.isGetter) { | 348 if (!element.isGetter) { |
| 348 if (element.isMalformed) { | 349 if (element.isMalformed) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 363 'native')) { | 364 'native')) { |
| 364 reporter.reportErrorMessage( | 365 reporter.reportErrorMessage( |
| 365 formalParameters, MessageKind.EXTRA_FORMALS); | 366 formalParameters, MessageKind.EXTRA_FORMALS); |
| 366 } | 367 } |
| 367 } | 368 } |
| 368 LinkBuilder<Element> parametersBuilder = | 369 LinkBuilder<Element> parametersBuilder = |
| 369 visitor.analyzeNodes(formalParameters.nodes); | 370 visitor.analyzeNodes(formalParameters.nodes); |
| 370 requiredParameterCount = parametersBuilder.length; | 371 requiredParameterCount = parametersBuilder.length; |
| 371 parameters = parametersBuilder.toList(); | 372 parameters = parametersBuilder.toList(); |
| 372 } | 373 } |
| 373 DartType returnType; | 374 ResolutionDartType returnType; |
| 374 if (element.isFactoryConstructor) { | 375 if (element.isFactoryConstructor) { |
| 375 returnType = element.enclosingClass.thisType; | 376 returnType = element.enclosingClass.thisType; |
| 376 // Because there is no type annotation for the return type of | 377 // Because there is no type annotation for the return type of |
| 377 // this element, we explicitly add one. | 378 // this element, we explicitly add one. |
| 378 registry.registerCheckedModeCheck(returnType); | 379 registry.registerCheckedModeCheck(returnType); |
| 379 } else { | 380 } else { |
| 380 AsyncMarker asyncMarker = AsyncMarker.SYNC; | 381 AsyncMarker asyncMarker = AsyncMarker.SYNC; |
| 381 if (isFunctionExpression) { | 382 if (isFunctionExpression) { |
| 382 // Use async marker to determine the return type of function | 383 // Use async marker to determine the return type of function |
| 383 // expressions. | 384 // expressions. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 401 } | 402 } |
| 402 | 403 |
| 403 if (element.isSetter && | 404 if (element.isSetter && |
| 404 (requiredParameterCount != 1 || visitor.optionalParameterCount != 0)) { | 405 (requiredParameterCount != 1 || visitor.optionalParameterCount != 0)) { |
| 405 // If there are no formal parameters, we already reported an error above. | 406 // If there are no formal parameters, we already reported an error above. |
| 406 if (formalParameters != null) { | 407 if (formalParameters != null) { |
| 407 reporter.reportErrorMessage( | 408 reporter.reportErrorMessage( |
| 408 formalParameters, MessageKind.ILLEGAL_SETTER_FORMALS); | 409 formalParameters, MessageKind.ILLEGAL_SETTER_FORMALS); |
| 409 } | 410 } |
| 410 } | 411 } |
| 411 LinkBuilder<DartType> parameterTypes = new LinkBuilder<DartType>(); | 412 LinkBuilder<ResolutionDartType> parameterTypes = |
| 413 new LinkBuilder<ResolutionDartType>(); |
| 412 for (FormalElement parameter in parameters) { | 414 for (FormalElement parameter in parameters) { |
| 413 parameterTypes.addLast(parameter.type); | 415 parameterTypes.addLast(parameter.type); |
| 414 } | 416 } |
| 415 List<DartType> optionalParameterTypes = const <DartType>[]; | 417 List<ResolutionDartType> optionalParameterTypes = |
| 418 const <ResolutionDartType>[]; |
| 416 List<String> namedParameters = const <String>[]; | 419 List<String> namedParameters = const <String>[]; |
| 417 List<DartType> namedParameterTypes = const <DartType>[]; | 420 List<ResolutionDartType> namedParameterTypes = const <ResolutionDartType>[]; |
| 418 List<Element> orderedOptionalParameters = | 421 List<Element> orderedOptionalParameters = |
| 419 visitor.optionalParameters.toList(); | 422 visitor.optionalParameters.toList(); |
| 420 if (visitor.optionalParametersAreNamed) { | 423 if (visitor.optionalParametersAreNamed) { |
| 421 // TODO(karlklose); replace when [visitor.optinalParameters] is a [List]. | 424 // TODO(karlklose); replace when [visitor.optinalParameters] is a [List]. |
| 422 orderedOptionalParameters.sort((Element a, Element b) { | 425 orderedOptionalParameters.sort((Element a, Element b) { |
| 423 return a.name.compareTo(b.name); | 426 return a.name.compareTo(b.name); |
| 424 }); | 427 }); |
| 425 LinkBuilder<String> namedParametersBuilder = new LinkBuilder<String>(); | 428 LinkBuilder<String> namedParametersBuilder = new LinkBuilder<String>(); |
| 426 LinkBuilder<DartType> namedParameterTypesBuilder = | 429 LinkBuilder<ResolutionDartType> namedParameterTypesBuilder = |
| 427 new LinkBuilder<DartType>(); | 430 new LinkBuilder<ResolutionDartType>(); |
| 428 for (FormalElement parameter in orderedOptionalParameters) { | 431 for (FormalElement parameter in orderedOptionalParameters) { |
| 429 namedParametersBuilder.addLast(parameter.name); | 432 namedParametersBuilder.addLast(parameter.name); |
| 430 namedParameterTypesBuilder.addLast(parameter.type); | 433 namedParameterTypesBuilder.addLast(parameter.type); |
| 431 } | 434 } |
| 432 namedParameters = namedParametersBuilder.toLink().toList(growable: false); | 435 namedParameters = namedParametersBuilder.toLink().toList(growable: false); |
| 433 namedParameterTypes = | 436 namedParameterTypes = |
| 434 namedParameterTypesBuilder.toLink().toList(growable: false); | 437 namedParameterTypesBuilder.toLink().toList(growable: false); |
| 435 } else { | 438 } else { |
| 436 // TODO(karlklose); replace when [visitor.optinalParameters] is a [List]. | 439 // TODO(karlklose); replace when [visitor.optinalParameters] is a [List]. |
| 437 LinkBuilder<DartType> optionalParameterTypesBuilder = | 440 LinkBuilder<ResolutionDartType> optionalParameterTypesBuilder = |
| 438 new LinkBuilder<DartType>(); | 441 new LinkBuilder<ResolutionDartType>(); |
| 439 for (FormalElement parameter in visitor.optionalParameters) { | 442 for (FormalElement parameter in visitor.optionalParameters) { |
| 440 optionalParameterTypesBuilder.addLast(parameter.type); | 443 optionalParameterTypesBuilder.addLast(parameter.type); |
| 441 } | 444 } |
| 442 optionalParameterTypes = | 445 optionalParameterTypes = |
| 443 optionalParameterTypesBuilder.toLink().toList(growable: false); | 446 optionalParameterTypesBuilder.toLink().toList(growable: false); |
| 444 } | 447 } |
| 445 FunctionType type = new FunctionType( | 448 ResolutionFunctionType type = new ResolutionFunctionType( |
| 446 element.declaration, | 449 element.declaration, |
| 447 returnType, | 450 returnType, |
| 448 parameterTypes.toLink().toList(growable: false), | 451 parameterTypes.toLink().toList(growable: false), |
| 449 optionalParameterTypes, | 452 optionalParameterTypes, |
| 450 namedParameters, | 453 namedParameters, |
| 451 namedParameterTypes); | 454 namedParameterTypes); |
| 452 return new FunctionSignatureX( | 455 return new FunctionSignatureX( |
| 453 typeVariables: typeVariableTypes, | 456 typeVariables: typeVariableTypes, |
| 454 requiredParameters: parameters, | 457 requiredParameters: parameters, |
| 455 optionalParameters: visitor.optionalParameters, | 458 optionalParameters: visitor.optionalParameters, |
| 456 requiredParameterCount: requiredParameterCount, | 459 requiredParameterCount: requiredParameterCount, |
| 457 optionalParameterCount: visitor.optionalParameterCount, | 460 optionalParameterCount: visitor.optionalParameterCount, |
| 458 optionalParametersAreNamed: visitor.optionalParametersAreNamed, | 461 optionalParametersAreNamed: visitor.optionalParametersAreNamed, |
| 459 orderedOptionalParameters: orderedOptionalParameters, | 462 orderedOptionalParameters: orderedOptionalParameters, |
| 460 type: type); | 463 type: type); |
| 461 } | 464 } |
| 462 | 465 |
| 463 DartType resolveTypeAnnotation(TypeAnnotation annotation) { | 466 ResolutionDartType resolveTypeAnnotation(TypeAnnotation annotation) { |
| 464 DartType type = resolveReturnType(annotation); | 467 ResolutionDartType type = resolveReturnType(annotation); |
| 465 if (type.isVoid) { | 468 if (type.isVoid) { |
| 466 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); | 469 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); |
| 467 } | 470 } |
| 468 return type; | 471 return type; |
| 469 } | 472 } |
| 470 | 473 |
| 471 DartType resolveReturnType(TypeAnnotation annotation) { | 474 ResolutionDartType resolveReturnType(TypeAnnotation annotation) { |
| 472 if (annotation == null) return const DynamicType(); | 475 if (annotation == null) return const ResolutionDynamicType(); |
| 473 DartType result = resolver.resolveTypeAnnotation(annotation); | 476 ResolutionDartType result = resolver.resolveTypeAnnotation(annotation); |
| 474 if (result == null) { | 477 if (result == null) { |
| 475 return const DynamicType(); | 478 return const ResolutionDynamicType(); |
| 476 } | 479 } |
| 477 return result; | 480 return result; |
| 478 } | 481 } |
| 479 } | 482 } |
| 480 | 483 |
| 481 /// Used during `SignatureResolver.analyze` to provide access to the type | 484 /// Used during `SignatureResolver.analyze` to provide access to the type |
| 482 /// variables of the function signature itself when its signature is analyzed. | 485 /// variables of the function signature itself when its signature is analyzed. |
| 483 class FunctionSignatureBuildingScope extends TypeVariablesScope { | 486 class FunctionSignatureBuildingScope extends TypeVariablesScope { |
| 484 @override | 487 @override |
| 485 final List<DartType> typeVariables; | 488 final List<ResolutionDartType> typeVariables; |
| 486 | 489 |
| 487 FunctionSignatureBuildingScope(Scope parent, this.typeVariables) | 490 FunctionSignatureBuildingScope(Scope parent, this.typeVariables) |
| 488 : super(parent); | 491 : super(parent); |
| 489 | 492 |
| 490 String toString() => 'FunctionSignatureBuildingScope($typeVariables)'; | 493 String toString() => 'FunctionSignatureBuildingScope($typeVariables)'; |
| 491 } | 494 } |
| OLD | NEW |