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; | |
20 import '../tree/tree.dart'; | 19 import '../tree/tree.dart'; |
21 import '../universe/use.dart' show TypeUse; | 20 import '../universe/use.dart' show TypeUse; |
22 import '../util/util.dart' show Link, LinkBuilder; | 21 import '../util/util.dart' show Link, LinkBuilder; |
23 import 'members.dart' show ResolverVisitor; | 22 import 'members.dart' show ResolverVisitor; |
24 import 'registry.dart' show ResolutionRegistry; | 23 import 'registry.dart' show ResolutionRegistry; |
25 import 'resolution_common.dart' show MappingVisitor; | 24 import 'resolution_common.dart' show MappingVisitor; |
26 import 'scope.dart' show Scope, TypeVariablesScope; | 25 import 'scope.dart' show Scope; |
27 | 26 |
28 /** | 27 /** |
29 * [SignatureResolver] resolves function signatures. | 28 * [SignatureResolver] resolves function signatures. |
30 */ | 29 */ |
31 class SignatureResolver extends MappingVisitor<FormalElementX> { | 30 class SignatureResolver extends MappingVisitor<FormalElementX> { |
32 final ResolverVisitor resolver; | 31 final ResolverVisitor resolver; |
33 final FunctionTypedElement enclosingElement; | 32 final FunctionTypedElement enclosingElement; |
34 final Scope scope; | 33 final Scope scope; |
35 final MessageKind defaultValuesError; | 34 final MessageKind defaultValuesError; |
36 final bool createRealParameters; | 35 final bool createRealParameters; |
37 List<Element> optionalParameters = const <Element>[]; | 36 List<Element> optionalParameters = const <Element>[]; |
38 int optionalParameterCount = 0; | 37 int optionalParameterCount = 0; |
39 bool isOptionalParameter = false; | 38 bool isOptionalParameter = false; |
40 bool optionalParametersAreNamed = false; | 39 bool optionalParametersAreNamed = false; |
41 VariableDefinitions currentDefinitions; | 40 VariableDefinitions currentDefinitions; |
42 | 41 |
43 SignatureResolver(Compiler compiler, FunctionTypedElement enclosingElement, | 42 SignatureResolver(Compiler compiler, FunctionTypedElement enclosingElement, |
44 Scope scope, | |
45 ResolutionRegistry registry, | 43 ResolutionRegistry registry, |
46 {this.defaultValuesError, this.createRealParameters}) | 44 {this.defaultValuesError, this.createRealParameters}) |
47 : this.scope = scope, | 45 : this.enclosingElement = enclosingElement, |
48 this.enclosingElement = enclosingElement, | 46 this.scope = enclosingElement.buildScope(), |
49 this.resolver = new ResolverVisitor( | 47 this.resolver = |
50 compiler, enclosingElement, registry, scope: scope), | 48 new ResolverVisitor(compiler, enclosingElement, registry), |
51 super(compiler, registry); | 49 super(compiler, registry); |
52 | 50 |
53 bool get defaultValuesAllowed => defaultValuesError == null; | 51 bool get defaultValuesAllowed => defaultValuesError == null; |
54 | 52 |
55 visitNodeList(NodeList node) { | 53 visitNodeList(NodeList node) { |
56 // This must be a list of optional arguments. | 54 // This must be a list of optional arguments. |
57 String value = node.beginToken.stringValue; | 55 String value = node.beginToken.stringValue; |
58 if ((!identical(value, '[')) && (!identical(value, '{'))) { | 56 if ((!identical(value, '[')) && (!identical(value, '{'))) { |
59 reporter.internalError(node, "expected optional parameters"); | 57 reporter.internalError(node, "expected optional parameters"); |
60 } | 58 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 Name.isPrivateName(node.source)) { | 103 Name.isPrivateName(node.source)) { |
106 reporter.reportErrorMessage(node, MessageKind.PRIVATE_NAMED_PARAMETER); | 104 reporter.reportErrorMessage(node, MessageKind.PRIVATE_NAMED_PARAMETER); |
107 } | 105 } |
108 } | 106 } |
109 | 107 |
110 void computeParameterType(FormalElementX element, | 108 void computeParameterType(FormalElementX element, |
111 [VariableElement fieldElement]) { | 109 [VariableElement fieldElement]) { |
112 void computeFunctionType(FunctionExpression functionExpression) { | 110 void computeFunctionType(FunctionExpression functionExpression) { |
113 FunctionSignature functionSignature = SignatureResolver.analyze( | 111 FunctionSignature functionSignature = SignatureResolver.analyze( |
114 compiler, | 112 compiler, |
115 scope, | |
116 functionExpression.typeVariables, | |
117 functionExpression.parameters, | 113 functionExpression.parameters, |
118 functionExpression.returnType, | 114 functionExpression.returnType, |
119 element, | 115 element, |
120 registry, | 116 registry, |
121 defaultValuesError: MessageKind.FUNCTION_TYPE_FORMAL_WITH_DEFAULT); | 117 defaultValuesError: MessageKind.FUNCTION_TYPE_FORMAL_WITH_DEFAULT); |
122 element.functionSignature = functionSignature; | 118 element.functionSignature = functionSignature; |
123 } | 119 } |
124 | 120 |
125 if (currentDefinitions.type != null) { | 121 if (currentDefinitions.type != null) { |
126 element.typeCache = resolveTypeAnnotation(currentDefinitions.type); | 122 element.typeCache = resolveTypeAnnotation(currentDefinitions.type); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 /** | 280 /** |
285 * Resolves formal parameters and return type of a [FunctionExpression] | 281 * Resolves formal parameters and return type of a [FunctionExpression] |
286 * to a [FunctionSignature]. | 282 * to a [FunctionSignature]. |
287 * | 283 * |
288 * If [createRealParameters] is `true`, the parameters will be | 284 * If [createRealParameters] is `true`, the parameters will be |
289 * real parameters implementing the [ParameterElement] interface. Otherwise, | 285 * real parameters implementing the [ParameterElement] interface. Otherwise, |
290 * the parameters will only implement [FormalElement]. | 286 * the parameters will only implement [FormalElement]. |
291 */ | 287 */ |
292 static FunctionSignature analyze( | 288 static FunctionSignature analyze( |
293 Compiler compiler, | 289 Compiler compiler, |
294 Scope scope, | |
295 NodeList typeVariables, | |
296 NodeList formalParameters, | 290 NodeList formalParameters, |
297 Node returnNode, | 291 Node returnNode, |
298 FunctionTypedElement element, | 292 FunctionTypedElement element, |
299 ResolutionRegistry registry, | 293 ResolutionRegistry registry, |
300 {MessageKind defaultValuesError, | 294 {MessageKind defaultValuesError, |
301 bool createRealParameters: false, | 295 bool createRealParameters: false, |
302 bool isFunctionExpression: false}) { | 296 bool isFunctionExpression: false}) { |
303 DiagnosticReporter reporter = compiler.reporter; | 297 DiagnosticReporter reporter = compiler.reporter; |
304 | 298 |
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); | |
329 SignatureResolver visitor = new SignatureResolver( | 299 SignatureResolver visitor = new SignatureResolver( |
330 compiler, element, scope, registry, | 300 compiler, element, registry, |
331 defaultValuesError: defaultValuesError, | 301 defaultValuesError: defaultValuesError, |
332 createRealParameters: createRealParameters); | 302 createRealParameters: createRealParameters); |
333 List<Element> parameters = const <Element>[]; | 303 List<Element> parameters = const <Element>[]; |
334 int requiredParameterCount = 0; | 304 int requiredParameterCount = 0; |
335 if (formalParameters == null) { | 305 if (formalParameters == null) { |
336 if (!element.isGetter) { | 306 if (!element.isGetter) { |
337 if (element.isMalformed) { | 307 if (element.isMalformed) { |
338 // If the element is erroneous, an error should already have been | 308 // If the element is erroneous, an error should already have been |
339 // reported. In the case of parse errors, it is possible that there | 309 // reported. In the case of parse errors, it is possible that there |
340 // are formal parameters, but something else in the method failed to | 310 // are formal parameters, but something else in the method failed to |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 optionalParameterTypesBuilder.toLink().toList(growable: false); | 404 optionalParameterTypesBuilder.toLink().toList(growable: false); |
435 } | 405 } |
436 FunctionType type = new FunctionType( | 406 FunctionType type = new FunctionType( |
437 element.declaration, | 407 element.declaration, |
438 returnType, | 408 returnType, |
439 parameterTypes.toLink().toList(growable: false), | 409 parameterTypes.toLink().toList(growable: false), |
440 optionalParameterTypes, | 410 optionalParameterTypes, |
441 namedParameters, | 411 namedParameters, |
442 namedParameterTypes); | 412 namedParameterTypes); |
443 return new FunctionSignatureX( | 413 return new FunctionSignatureX( |
444 typeVariables: typeVariableTypes, | |
445 requiredParameters: parameters, | 414 requiredParameters: parameters, |
446 optionalParameters: visitor.optionalParameters, | 415 optionalParameters: visitor.optionalParameters, |
447 requiredParameterCount: requiredParameterCount, | 416 requiredParameterCount: requiredParameterCount, |
448 optionalParameterCount: visitor.optionalParameterCount, | 417 optionalParameterCount: visitor.optionalParameterCount, |
449 optionalParametersAreNamed: visitor.optionalParametersAreNamed, | 418 optionalParametersAreNamed: visitor.optionalParametersAreNamed, |
450 orderedOptionalParameters: orderedOptionalParameters, | 419 orderedOptionalParameters: orderedOptionalParameters, |
451 type: type); | 420 type: type); |
452 } | 421 } |
453 | 422 |
454 DartType resolveTypeAnnotation(TypeAnnotation annotation) { | 423 DartType resolveTypeAnnotation(TypeAnnotation annotation) { |
455 DartType type = resolveReturnType(annotation); | 424 DartType type = resolveReturnType(annotation); |
456 if (type.isVoid) { | 425 if (type.isVoid) { |
457 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); | 426 reporter.reportErrorMessage(annotation, MessageKind.VOID_NOT_ALLOWED); |
458 } | 427 } |
459 return type; | 428 return type; |
460 } | 429 } |
461 | 430 |
462 DartType resolveReturnType(TypeAnnotation annotation) { | 431 DartType resolveReturnType(TypeAnnotation annotation) { |
463 if (annotation == null) return const DynamicType(); | 432 if (annotation == null) return const DynamicType(); |
464 DartType result = resolver.resolveTypeAnnotation(annotation); | 433 DartType result = resolver.resolveTypeAnnotation(annotation); |
465 if (result == null) { | 434 if (result == null) { |
466 return const DynamicType(); | 435 return const DynamicType(); |
467 } | 436 } |
468 return result; | 437 return result; |
469 } | 438 } |
470 } | 439 } |
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 |