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 analyzer.src.generated.resolver; | 5 library analyzer.src.generated.resolver; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/visitor.dart'; | 10 import 'package:analyzer/dart/ast/visitor.dart'; |
(...skipping 7277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7288 if (declaration != null) { | 7288 if (declaration != null) { |
7289 Element element = declaration.element; | 7289 Element element = declaration.element; |
7290 if (element is ExecutableElement) { | 7290 if (element is ExecutableElement) { |
7291 _enclosingFunction = element; | 7291 _enclosingFunction = element; |
7292 } | 7292 } |
7293 } | 7293 } |
7294 _overrideManager.enterScope(); | 7294 _overrideManager.enterScope(); |
7295 } | 7295 } |
7296 | 7296 |
7297 /** | 7297 /** |
| 7298 * Given a downward inference type [fnType], and the declared |
| 7299 * [typeParameterList] for a function expression, determines if we can enable |
| 7300 * downward inference and if so, returns the function type to use for |
| 7301 * inference. |
| 7302 * |
| 7303 * This will return null if inference is not possible. This happens when |
| 7304 * there is no way we can find a subtype of the function type, given the |
| 7305 * provided type parameter list. |
| 7306 */ |
| 7307 FunctionType matchFunctionTypeParameters( |
| 7308 TypeParameterList typeParameterList, FunctionType fnType) { |
| 7309 if (typeParameterList == null) { |
| 7310 if (fnType.typeFormals.isEmpty) { |
| 7311 return fnType; |
| 7312 } |
| 7313 |
| 7314 // A non-generic function cannot be a subtype of a generic one. |
| 7315 return null; |
| 7316 } |
| 7317 |
| 7318 NodeList<TypeParameter> typeParameters = typeParameterList.typeParameters; |
| 7319 if (fnType.typeFormals.isEmpty) { |
| 7320 // TODO(jmesserly): this is a legal subtype. We don't currently infer |
| 7321 // here, but we could. This is similar to |
| 7322 // StrongTypeSystemImpl.inferFunctionTypeInstantiation, but we don't |
| 7323 // have the FunctionType yet for the current node, so it's not quite |
| 7324 // straightforward to apply. |
| 7325 return null; |
| 7326 } |
| 7327 |
| 7328 if (fnType.typeFormals.length != typeParameters.length) { |
| 7329 // A subtype cannot have different number of type formals. |
| 7330 return null; |
| 7331 } |
| 7332 |
| 7333 // Same number of type formals. Instantiate the function type so its |
| 7334 // parameter and return type are in terms of the surrounding context. |
| 7335 return fnType.instantiate( |
| 7336 typeParameters.map((t) => t.name.staticElement.type).toList()); |
| 7337 } |
| 7338 |
| 7339 /** |
7298 * If it is appropriate to do so, override the current type of the static and
propagated elements | 7340 * If it is appropriate to do so, override the current type of the static and
propagated elements |
7299 * associated with the given expression with the given type. Generally speakin
g, it is appropriate | 7341 * associated with the given expression with the given type. Generally speakin
g, it is appropriate |
7300 * if the given type is more specific than the current type. | 7342 * if the given type is more specific than the current type. |
7301 * | 7343 * |
7302 * @param expression the expression used to access the static and propagated e
lements whose types | 7344 * @param expression the expression used to access the static and propagated e
lements whose types |
7303 * might be overridden | 7345 * might be overridden |
7304 * @param potentialType the potential type of the elements | 7346 * @param potentialType the potential type of the elements |
7305 * @param allowPrecisionLoss see @{code overrideVariable} docs | 7347 * @param allowPrecisionLoss see @{code overrideVariable} docs |
7306 */ | 7348 */ |
7307 void overrideExpression(Expression expression, DartType potentialType, | 7349 void overrideExpression(Expression expression, DartType potentialType, |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8071 | 8113 |
8072 @override | 8114 @override |
8073 Object visitFunctionExpression(FunctionExpression node) { | 8115 Object visitFunctionExpression(FunctionExpression node) { |
8074 ExecutableElement outerFunction = _enclosingFunction; | 8116 ExecutableElement outerFunction = _enclosingFunction; |
8075 try { | 8117 try { |
8076 _enclosingFunction = node.element; | 8118 _enclosingFunction = node.element; |
8077 _overrideManager.enterScope(); | 8119 _overrideManager.enterScope(); |
8078 try { | 8120 try { |
8079 DartType functionType = InferenceContext.getType(node); | 8121 DartType functionType = InferenceContext.getType(node); |
8080 if (functionType is FunctionType) { | 8122 if (functionType is FunctionType) { |
8081 _inferFormalParameterList(node.parameters, functionType); | 8123 functionType = |
8082 DartType returnType = _computeReturnOrYieldType( | 8124 matchFunctionTypeParameters(node.typeParameters, functionType); |
8083 functionType.returnType, | 8125 if (functionType is FunctionType) { |
8084 _enclosingFunction.isGenerator, | 8126 _inferFormalParameterList(node.parameters, functionType); |
8085 _enclosingFunction.isAsynchronous); | 8127 DartType returnType = _computeReturnOrYieldType( |
8086 InferenceContext.setType(node.body, returnType); | 8128 functionType.returnType, |
| 8129 _enclosingFunction.isGenerator, |
| 8130 _enclosingFunction.isAsynchronous); |
| 8131 InferenceContext.setType(node.body, returnType); |
| 8132 } |
8087 } | 8133 } |
8088 super.visitFunctionExpression(node); | 8134 super.visitFunctionExpression(node); |
8089 } finally { | 8135 } finally { |
8090 _overrideManager.exitScope(); | 8136 _overrideManager.exitScope(); |
8091 } | 8137 } |
8092 } finally { | 8138 } finally { |
8093 _enclosingFunction = outerFunction; | 8139 _enclosingFunction = outerFunction; |
8094 } | 8140 } |
8095 return null; | 8141 return null; |
8096 } | 8142 } |
(...skipping 4585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12682 nonFields.add(node); | 12728 nonFields.add(node); |
12683 return null; | 12729 return null; |
12684 } | 12730 } |
12685 | 12731 |
12686 @override | 12732 @override |
12687 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 12733 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
12688 | 12734 |
12689 @override | 12735 @override |
12690 Object visitWithClause(WithClause node) => null; | 12736 Object visitWithClause(WithClause node) => null; |
12691 } | 12737 } |
OLD | NEW |