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 '../compiler.dart' show Compiler; | 8 import '../compiler.dart' show Compiler; |
9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
11 import '../elements/modelx.dart' | 11 import '../elements/modelx.dart' |
12 show | 12 show |
13 ErroneousFieldElementX, | 13 ErroneousFieldElementX, |
14 ErroneousInitializingFormalElementX, | 14 ErroneousInitializingFormalElementX, |
15 FormalElementX, | 15 FormalElementX, |
16 FunctionSignatureX, | 16 FunctionSignatureX, |
17 InitializingFormalElementX, | 17 InitializingFormalElementX, |
18 LocalParameterElementX; | 18 LocalParameterElementX, |
| 19 TypeVariableElementX; |
19 import '../tree/tree.dart'; | 20 import '../tree/tree.dart'; |
20 import '../universe/use.dart' show TypeUse; | 21 import '../universe/use.dart' show TypeUse; |
21 import '../util/util.dart' show Link, LinkBuilder; | 22 import '../util/util.dart' show Link, LinkBuilder; |
22 import 'members.dart' show ResolverVisitor; | 23 import 'members.dart' show ResolverVisitor; |
23 import 'registry.dart' show ResolutionRegistry; | 24 import 'registry.dart' show ResolutionRegistry; |
24 import 'resolution_common.dart' show MappingVisitor; | 25 import 'resolution_common.dart' show MappingVisitor; |
25 import 'scope.dart' show Scope; | 26 import 'scope.dart' show Scope, TypeVariablesScope; |
26 | 27 |
27 /** | 28 /** |
28 * [SignatureResolver] resolves function signatures. | 29 * [SignatureResolver] resolves function signatures. |
29 */ | 30 */ |
30 class SignatureResolver extends MappingVisitor<FormalElementX> { | 31 class SignatureResolver extends MappingVisitor<FormalElementX> { |
31 final ResolverVisitor resolver; | 32 final ResolverVisitor resolver; |
32 final FunctionTypedElement enclosingElement; | 33 final FunctionTypedElement enclosingElement; |
33 final Scope scope; | 34 final Scope scope; |
34 final MessageKind defaultValuesError; | 35 final MessageKind defaultValuesError; |
35 final bool createRealParameters; | 36 final bool createRealParameters; |
36 List<Element> optionalParameters = const <Element>[]; | 37 List<Element> optionalParameters = const <Element>[]; |
37 int optionalParameterCount = 0; | 38 int optionalParameterCount = 0; |
38 bool isOptionalParameter = false; | 39 bool isOptionalParameter = false; |
39 bool optionalParametersAreNamed = false; | 40 bool optionalParametersAreNamed = false; |
40 VariableDefinitions currentDefinitions; | 41 VariableDefinitions currentDefinitions; |
41 | 42 |
42 SignatureResolver(Compiler compiler, FunctionTypedElement enclosingElement, | 43 SignatureResolver(Compiler compiler, FunctionTypedElement enclosingElement, |
| 44 Scope scope, |
43 ResolutionRegistry registry, | 45 ResolutionRegistry registry, |
44 {this.defaultValuesError, this.createRealParameters}) | 46 {this.defaultValuesError, this.createRealParameters}) |
45 : this.enclosingElement = enclosingElement, | 47 : this.scope = scope, |
46 this.scope = enclosingElement.buildScope(), | 48 this.enclosingElement = enclosingElement, |
47 this.resolver = | 49 this.resolver = new ResolverVisitor( |
48 new ResolverVisitor(compiler, enclosingElement, registry), | 50 compiler, enclosingElement, registry, scope: scope), |
49 super(compiler, registry); | 51 super(compiler, registry); |
50 | 52 |
51 bool get defaultValuesAllowed => defaultValuesError == null; | 53 bool get defaultValuesAllowed => defaultValuesError == null; |
52 | 54 |
53 visitNodeList(NodeList node) { | 55 visitNodeList(NodeList node) { |
54 // This must be a list of optional arguments. | 56 // This must be a list of optional arguments. |
55 String value = node.beginToken.stringValue; | 57 String value = node.beginToken.stringValue; |
56 if ((!identical(value, '[')) && (!identical(value, '{'))) { | 58 if ((!identical(value, '[')) && (!identical(value, '{'))) { |
57 reporter.internalError(node, "expected optional parameters"); | 59 reporter.internalError(node, "expected optional parameters"); |
58 } | 60 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 Name.isPrivateName(node.source)) { | 105 Name.isPrivateName(node.source)) { |
104 reporter.reportErrorMessage(node, MessageKind.PRIVATE_NAMED_PARAMETER); | 106 reporter.reportErrorMessage(node, MessageKind.PRIVATE_NAMED_PARAMETER); |
105 } | 107 } |
106 } | 108 } |
107 | 109 |
108 void computeParameterType(FormalElementX element, | 110 void computeParameterType(FormalElementX element, |
109 [VariableElement fieldElement]) { | 111 [VariableElement fieldElement]) { |
110 void computeFunctionType(FunctionExpression functionExpression) { | 112 void computeFunctionType(FunctionExpression functionExpression) { |
111 FunctionSignature functionSignature = SignatureResolver.analyze( | 113 FunctionSignature functionSignature = SignatureResolver.analyze( |
112 compiler, | 114 compiler, |
| 115 scope, |
| 116 functionExpression.typeVariables, |
113 functionExpression.parameters, | 117 functionExpression.parameters, |
114 functionExpression.returnType, | 118 functionExpression.returnType, |
115 element, | 119 element, |
116 registry, | 120 registry, |
117 defaultValuesError: MessageKind.FUNCTION_TYPE_FORMAL_WITH_DEFAULT); | 121 defaultValuesError: MessageKind.FUNCTION_TYPE_FORMAL_WITH_DEFAULT); |
118 element.functionSignature = functionSignature; | 122 element.functionSignature = functionSignature; |
119 } | 123 } |
120 | 124 |
121 if (currentDefinitions.type != null) { | 125 if (currentDefinitions.type != null) { |
122 element.typeCache = resolveTypeAnnotation(currentDefinitions.type); | 126 element.typeCache = resolveTypeAnnotation(currentDefinitions.type); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 /** | 284 /** |
281 * Resolves formal parameters and return type of a [FunctionExpression] | 285 * Resolves formal parameters and return type of a [FunctionExpression] |
282 * to a [FunctionSignature]. | 286 * to a [FunctionSignature]. |
283 * | 287 * |
284 * If [createRealParameters] is `true`, the parameters will be | 288 * If [createRealParameters] is `true`, the parameters will be |
285 * real parameters implementing the [ParameterElement] interface. Otherwise, | 289 * real parameters implementing the [ParameterElement] interface. Otherwise, |
286 * the parameters will only implement [FormalElement]. | 290 * the parameters will only implement [FormalElement]. |
287 */ | 291 */ |
288 static FunctionSignature analyze( | 292 static FunctionSignature analyze( |
289 Compiler compiler, | 293 Compiler compiler, |
| 294 Scope scope, |
| 295 NodeList typeVariables, |
290 NodeList formalParameters, | 296 NodeList formalParameters, |
291 Node returnNode, | 297 Node returnNode, |
292 FunctionTypedElement element, | 298 FunctionTypedElement element, |
293 ResolutionRegistry registry, | 299 ResolutionRegistry registry, |
294 {MessageKind defaultValuesError, | 300 {MessageKind defaultValuesError, |
295 bool createRealParameters: false, | 301 bool createRealParameters: false, |
296 bool isFunctionExpression: false}) { | 302 bool isFunctionExpression: false}) { |
297 DiagnosticReporter reporter = compiler.reporter; | 303 DiagnosticReporter reporter = compiler.reporter; |
298 | 304 |
| 305 List<DartType> createTypeVariables(NodeList typeVariableNodes) { |
| 306 if (typeVariableNodes == null) return const <DartType>[]; |
| 307 |
| 308 // Create the types and elements corresponding to [typeVariableNodes]. |
| 309 Link<Node> nodes = typeVariableNodes.nodes; |
| 310 List<DartType> arguments = |
| 311 new List.generate(nodes.slowLength(), (int index) { |
| 312 TypeVariable node = nodes.head; |
| 313 String variableName = node.name.source; |
| 314 nodes = nodes.tail; |
| 315 TypeVariableElementX variableElement = |
| 316 new TypeVariableElementX(variableName, element, index, node); |
| 317 // TODO(eernst): When type variables are implemented fully we will need |
| 318 // to resolve the actual bounds; currently we just claim [dynamic]. |
| 319 variableElement.boundCache = const DynamicType(); |
| 320 TypeVariableType variableType = new TypeVariableType(variableElement); |
| 321 variableElement.typeCache = variableType; |
| 322 return variableType; |
| 323 }, growable: false); |
| 324 return arguments; |
| 325 } |
| 326 |
| 327 List<DartType> typeVariableTypes = createTypeVariables(typeVariables); |
| 328 scope = new FunctionSignatureBuildingScope(scope, typeVariableTypes); |
299 SignatureResolver visitor = new SignatureResolver( | 329 SignatureResolver visitor = new SignatureResolver( |
300 compiler, element, registry, | 330 compiler, element, scope, registry, |
301 defaultValuesError: defaultValuesError, | 331 defaultValuesError: defaultValuesError, |
302 createRealParameters: createRealParameters); | 332 createRealParameters: createRealParameters); |
303 List<Element> parameters = const <Element>[]; | 333 List<Element> parameters = const <Element>[]; |
304 int requiredParameterCount = 0; | 334 int requiredParameterCount = 0; |
305 if (formalParameters == null) { | 335 if (formalParameters == null) { |
306 if (!element.isGetter) { | 336 if (!element.isGetter) { |
307 if (element.isMalformed) { | 337 if (element.isMalformed) { |
308 // If the element is erroneous, an error should already have been | 338 // If the element is erroneous, an error should already have been |
309 // reported. In the case of parse errors, it is possible that there | 339 // reported. In the case of parse errors, it is possible that there |
310 // are formal parameters, but something else in the method failed to | 340 // are formal parameters, but something else in the method failed to |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 optionalParameterTypesBuilder.toLink().toList(growable: false); | 434 optionalParameterTypesBuilder.toLink().toList(growable: false); |
405 } | 435 } |
406 FunctionType type = new FunctionType( | 436 FunctionType type = new FunctionType( |
407 element.declaration, | 437 element.declaration, |
408 returnType, | 438 returnType, |
409 parameterTypes.toLink().toList(growable: false), | 439 parameterTypes.toLink().toList(growable: false), |
410 optionalParameterTypes, | 440 optionalParameterTypes, |
411 namedParameters, | 441 namedParameters, |
412 namedParameterTypes); | 442 namedParameterTypes); |
413 return new FunctionSignatureX( | 443 return new FunctionSignatureX( |
| 444 typeVariables: typeVariableTypes, |
414 requiredParameters: parameters, | 445 requiredParameters: parameters, |
415 optionalParameters: visitor.optionalParameters, | 446 optionalParameters: visitor.optionalParameters, |
416 requiredParameterCount: requiredParameterCount, | 447 requiredParameterCount: requiredParameterCount, |
417 optionalParameterCount: visitor.optionalParameterCount, | 448 optionalParameterCount: visitor.optionalParameterCount, |
418 optionalParametersAreNamed: visitor.optionalParametersAreNamed, | 449 optionalParametersAreNamed: visitor.optionalParametersAreNamed, |
419 orderedOptionalParameters: orderedOptionalParameters, | 450 orderedOptionalParameters: orderedOptionalParameters, |
420 type: type); | 451 type: type); |
421 } | 452 } |
422 | 453 |
423 DartType resolveTypeAnnotation(TypeAnnotation annotation) { | 454 DartType resolveTypeAnnotation(TypeAnnotation annotation) { |
424 DartType type = resolveReturnType(annotation); | 455 DartType type = resolveReturnType(annotation); |
425 if (type.isVoid) { | 456 if (type.isVoid) { |
426 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); | 457 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); |
427 } | 458 } |
428 return type; | 459 return type; |
429 } | 460 } |
430 | 461 |
431 DartType resolveReturnType(TypeAnnotation annotation) { | 462 DartType resolveReturnType(TypeAnnotation annotation) { |
432 if (annotation == null) return const DynamicType(); | 463 if (annotation == null) return const DynamicType(); |
433 DartType result = resolver.resolveTypeAnnotation(annotation); | 464 DartType result = resolver.resolveTypeAnnotation(annotation); |
434 if (result == null) { | 465 if (result == null) { |
435 return const DynamicType(); | 466 return const DynamicType(); |
436 } | 467 } |
437 return result; | 468 return result; |
438 } | 469 } |
439 } | 470 } |
| 471 |
| 472 /// Used during `SignatureResolver.analyze` to provide access to the type |
| 473 /// variables of the function signature itself when its signature is analyzed. |
| 474 class FunctionSignatureBuildingScope extends TypeVariablesScope { |
| 475 @override |
| 476 final List<DartType> typeVariables; |
| 477 |
| 478 FunctionSignatureBuildingScope(Scope parent, this.typeVariables) |
| 479 : super(parent); |
| 480 |
| 481 String toString() => 'FunctionSignatureBuildingScope($typeVariables)'; |
| 482 } |
OLD | NEW |