| 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/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 7142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7153 * A comment before a function should be resolved in the context of the | 7153 * A comment before a function should be resolved in the context of the |
| 7154 * function. But when we incrementally resolve a comment, we don't want to | 7154 * function. But when we incrementally resolve a comment, we don't want to |
| 7155 * resolve the whole function. | 7155 * resolve the whole function. |
| 7156 * | 7156 * |
| 7157 * So, this flag is set to `true`, when just context of the function should | 7157 * So, this flag is set to `true`, when just context of the function should |
| 7158 * be built and the comment resolved. | 7158 * be built and the comment resolved. |
| 7159 */ | 7159 */ |
| 7160 bool resolveOnlyCommentInFunctionBody = false; | 7160 bool resolveOnlyCommentInFunctionBody = false; |
| 7161 | 7161 |
| 7162 /** | 7162 /** |
| 7163 * Body of the function currently being analyzed, if any. |
| 7164 */ |
| 7165 FunctionBody _currentFunctionBody; |
| 7166 |
| 7167 /** |
| 7163 * Initialize a newly created visitor to resolve the nodes in an AST node. | 7168 * Initialize a newly created visitor to resolve the nodes in an AST node. |
| 7164 * | 7169 * |
| 7165 * The [definingLibrary] is the element for the library containing the node | 7170 * The [definingLibrary] is the element for the library containing the node |
| 7166 * being visited. The [source] is the source representing the compilation unit | 7171 * being visited. The [source] is the source representing the compilation unit |
| 7167 * containing the node being visited. The [typeProvider] is the object used to | 7172 * containing the node being visited. The [typeProvider] is the object used to |
| 7168 * access the types from the core library. The [errorListener] is the error | 7173 * access the types from the core library. The [errorListener] is the error |
| 7169 * listener that will be informed of any errors that are found during | 7174 * listener that will be informed of any errors that are found during |
| 7170 * resolution. The [nameScope] is the scope used to resolve identifiers in the | 7175 * resolution. The [nameScope] is the scope used to resolve identifiers in the |
| 7171 * node that will first be visited. If `null` or unspecified, a new | 7176 * node that will first be visited. If `null` or unspecified, a new |
| 7172 * [LibraryScope] will be created based on [definingLibrary] and | 7177 * [LibraryScope] will be created based on [definingLibrary] and |
| (...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7838 } else if (thenIsAbrupt && !elseIsAbrupt) { | 7843 } else if (thenIsAbrupt && !elseIsAbrupt) { |
| 7839 _propagateFalseState(condition); | 7844 _propagateFalseState(condition); |
| 7840 _propagateState(elseExpression); | 7845 _propagateState(elseExpression); |
| 7841 } | 7846 } |
| 7842 return null; | 7847 return null; |
| 7843 } | 7848 } |
| 7844 | 7849 |
| 7845 @override | 7850 @override |
| 7846 Object visitConstructorDeclaration(ConstructorDeclaration node) { | 7851 Object visitConstructorDeclaration(ConstructorDeclaration node) { |
| 7847 ExecutableElement outerFunction = _enclosingFunction; | 7852 ExecutableElement outerFunction = _enclosingFunction; |
| 7853 FunctionBody outerFunctionBody = _currentFunctionBody; |
| 7848 try { | 7854 try { |
| 7855 _currentFunctionBody = node.body; |
| 7849 _enclosingFunction = node.element; | 7856 _enclosingFunction = node.element; |
| 7850 FunctionType type = _enclosingFunction.type; | 7857 FunctionType type = _enclosingFunction.type; |
| 7851 InferenceContext.setType(node.body, type.returnType); | 7858 InferenceContext.setType(node.body, type.returnType); |
| 7852 super.visitConstructorDeclaration(node); | 7859 super.visitConstructorDeclaration(node); |
| 7853 } finally { | 7860 } finally { |
| 7861 _currentFunctionBody = outerFunctionBody; |
| 7854 _enclosingFunction = outerFunction; | 7862 _enclosingFunction = outerFunction; |
| 7855 } | 7863 } |
| 7856 ConstructorElementImpl constructor = node.element; | 7864 ConstructorElementImpl constructor = node.element; |
| 7857 constructor.constantInitializers = | 7865 constructor.constantInitializers = |
| 7858 new ConstantAstCloner().cloneNodeList(node.initializers); | 7866 new ConstantAstCloner().cloneNodeList(node.initializers); |
| 7859 return null; | 7867 return null; |
| 7860 } | 7868 } |
| 7861 | 7869 |
| 7862 @override | 7870 @override |
| 7863 void visitConstructorDeclarationInScope(ConstructorDeclaration node) { | 7871 void visitConstructorDeclarationInScope(ConstructorDeclaration node) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8087 } finally { | 8095 } finally { |
| 8088 _overrideManager.exitScope(); | 8096 _overrideManager.exitScope(); |
| 8089 } | 8097 } |
| 8090 // TODO(brianwilkerson) If the loop can only be exited because the condition | 8098 // TODO(brianwilkerson) If the loop can only be exited because the condition |
| 8091 // is false, then propagateFalseState(condition); | 8099 // is false, then propagateFalseState(condition); |
| 8092 } | 8100 } |
| 8093 | 8101 |
| 8094 @override | 8102 @override |
| 8095 Object visitFunctionDeclaration(FunctionDeclaration node) { | 8103 Object visitFunctionDeclaration(FunctionDeclaration node) { |
| 8096 ExecutableElement outerFunction = _enclosingFunction; | 8104 ExecutableElement outerFunction = _enclosingFunction; |
| 8105 FunctionBody outerFunctionBody = _currentFunctionBody; |
| 8097 try { | 8106 try { |
| 8098 SimpleIdentifier functionName = node.name; | 8107 SimpleIdentifier functionName = node.name; |
| 8108 _currentFunctionBody = node.functionExpression.body; |
| 8099 _enclosingFunction = functionName.staticElement as ExecutableElement; | 8109 _enclosingFunction = functionName.staticElement as ExecutableElement; |
| 8100 InferenceContext.setType( | 8110 InferenceContext.setType( |
| 8101 node.functionExpression, _enclosingFunction.type); | 8111 node.functionExpression, _enclosingFunction.type); |
| 8102 super.visitFunctionDeclaration(node); | 8112 super.visitFunctionDeclaration(node); |
| 8103 } finally { | 8113 } finally { |
| 8114 _currentFunctionBody = outerFunctionBody; |
| 8104 _enclosingFunction = outerFunction; | 8115 _enclosingFunction = outerFunction; |
| 8105 } | 8116 } |
| 8106 return null; | 8117 return null; |
| 8107 } | 8118 } |
| 8108 | 8119 |
| 8109 @override | 8120 @override |
| 8110 void visitFunctionDeclarationInScope(FunctionDeclaration node) { | 8121 void visitFunctionDeclarationInScope(FunctionDeclaration node) { |
| 8111 super.visitFunctionDeclarationInScope(node); | 8122 super.visitFunctionDeclarationInScope(node); |
| 8112 safelyVisitComment(node.documentationComment); | 8123 safelyVisitComment(node.documentationComment); |
| 8113 } | 8124 } |
| 8114 | 8125 |
| 8115 @override | 8126 @override |
| 8116 Object visitFunctionExpression(FunctionExpression node) { | 8127 Object visitFunctionExpression(FunctionExpression node) { |
| 8117 ExecutableElement outerFunction = _enclosingFunction; | 8128 ExecutableElement outerFunction = _enclosingFunction; |
| 8129 FunctionBody outerFunctionBody = _currentFunctionBody; |
| 8118 try { | 8130 try { |
| 8131 _currentFunctionBody = node.body; |
| 8119 _enclosingFunction = node.element; | 8132 _enclosingFunction = node.element; |
| 8120 _overrideManager.enterScope(); | 8133 _overrideManager.enterScope(); |
| 8121 try { | 8134 try { |
| 8122 DartType functionType = InferenceContext.getType(node); | 8135 DartType functionType = InferenceContext.getType(node); |
| 8123 if (functionType is FunctionType) { | 8136 if (functionType is FunctionType) { |
| 8124 functionType = | 8137 functionType = |
| 8125 matchFunctionTypeParameters(node.typeParameters, functionType); | 8138 matchFunctionTypeParameters(node.typeParameters, functionType); |
| 8126 if (functionType is FunctionType) { | 8139 if (functionType is FunctionType) { |
| 8127 _inferFormalParameterList(node.parameters, functionType); | 8140 _inferFormalParameterList(node.parameters, functionType); |
| 8128 DartType returnType = _computeReturnOrYieldType( | 8141 DartType returnType = _computeReturnOrYieldType( |
| 8129 functionType.returnType, | 8142 functionType.returnType, |
| 8130 _enclosingFunction.isGenerator, | 8143 _enclosingFunction.isGenerator, |
| 8131 _enclosingFunction.isAsynchronous); | 8144 _enclosingFunction.isAsynchronous); |
| 8132 InferenceContext.setType(node.body, returnType); | 8145 InferenceContext.setType(node.body, returnType); |
| 8133 } | 8146 } |
| 8134 } | 8147 } |
| 8135 super.visitFunctionExpression(node); | 8148 super.visitFunctionExpression(node); |
| 8136 } finally { | 8149 } finally { |
| 8137 _overrideManager.exitScope(); | 8150 _overrideManager.exitScope(); |
| 8138 } | 8151 } |
| 8139 } finally { | 8152 } finally { |
| 8153 _currentFunctionBody = outerFunctionBody; |
| 8140 _enclosingFunction = outerFunction; | 8154 _enclosingFunction = outerFunction; |
| 8141 } | 8155 } |
| 8142 return null; | 8156 return null; |
| 8143 } | 8157 } |
| 8144 | 8158 |
| 8145 @override | 8159 @override |
| 8146 Object visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { | 8160 Object visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { |
| 8147 safelyVisit(node.function); | 8161 safelyVisit(node.function); |
| 8148 node.accept(elementResolver); | 8162 node.accept(elementResolver); |
| 8149 _inferFunctionExpressionsParametersTypes(node.argumentList); | 8163 _inferFunctionExpressionsParametersTypes(node.argumentList); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8326 } else { | 8340 } else { |
| 8327 InferenceContext.clearType(node); | 8341 InferenceContext.clearType(node); |
| 8328 } | 8342 } |
| 8329 super.visitMapLiteral(node); | 8343 super.visitMapLiteral(node); |
| 8330 return null; | 8344 return null; |
| 8331 } | 8345 } |
| 8332 | 8346 |
| 8333 @override | 8347 @override |
| 8334 Object visitMethodDeclaration(MethodDeclaration node) { | 8348 Object visitMethodDeclaration(MethodDeclaration node) { |
| 8335 ExecutableElement outerFunction = _enclosingFunction; | 8349 ExecutableElement outerFunction = _enclosingFunction; |
| 8350 FunctionBody outerFunctionBody = _currentFunctionBody; |
| 8336 try { | 8351 try { |
| 8352 _currentFunctionBody = node.body; |
| 8337 _enclosingFunction = node.element; | 8353 _enclosingFunction = node.element; |
| 8338 DartType returnType = _computeReturnOrYieldType( | 8354 DartType returnType = _computeReturnOrYieldType( |
| 8339 _enclosingFunction.type?.returnType, | 8355 _enclosingFunction.type?.returnType, |
| 8340 _enclosingFunction.isGenerator, | 8356 _enclosingFunction.isGenerator, |
| 8341 _enclosingFunction.isAsynchronous); | 8357 _enclosingFunction.isAsynchronous); |
| 8342 InferenceContext.setType(node.body, returnType); | 8358 InferenceContext.setType(node.body, returnType); |
| 8343 super.visitMethodDeclaration(node); | 8359 super.visitMethodDeclaration(node); |
| 8344 } finally { | 8360 } finally { |
| 8361 _currentFunctionBody = outerFunctionBody; |
| 8345 _enclosingFunction = outerFunction; | 8362 _enclosingFunction = outerFunction; |
| 8346 } | 8363 } |
| 8347 return null; | 8364 return null; |
| 8348 } | 8365 } |
| 8349 | 8366 |
| 8350 @override | 8367 @override |
| 8351 void visitMethodDeclarationInScope(MethodDeclaration node) { | 8368 void visitMethodDeclarationInScope(MethodDeclaration node) { |
| 8352 super.visitMethodDeclarationInScope(node); | 8369 super.visitMethodDeclarationInScope(node); |
| 8353 safelyVisitComment(node.documentationComment); | 8370 safelyVisitComment(node.documentationComment); |
| 8354 } | 8371 } |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8580 /** | 8597 /** |
| 8581 * Checks each promoted variable in the current scope for compliance with the
following | 8598 * Checks each promoted variable in the current scope for compliance with the
following |
| 8582 * specification statement: | 8599 * specification statement: |
| 8583 * | 8600 * |
| 8584 * If the variable <i>v</i> is accessed by a closure in <i>s<sub>1</sub></i> t
hen the variable | 8601 * If the variable <i>v</i> is accessed by a closure in <i>s<sub>1</sub></i> t
hen the variable |
| 8585 * <i>v</i> is not potentially mutated anywhere in the scope of <i>v</i>. | 8602 * <i>v</i> is not potentially mutated anywhere in the scope of <i>v</i>. |
| 8586 */ | 8603 */ |
| 8587 void _clearTypePromotionsIfAccessedInClosureAndProtentiallyMutated( | 8604 void _clearTypePromotionsIfAccessedInClosureAndProtentiallyMutated( |
| 8588 AstNode target) { | 8605 AstNode target) { |
| 8589 for (Element element in _promoteManager.promotedElements) { | 8606 for (Element element in _promoteManager.promotedElements) { |
| 8590 if ((element as VariableElementImpl).isPotentiallyMutatedInScope) { | 8607 if (_currentFunctionBody.isPotentiallyMutatedInScope(element)) { |
| 8591 if (_isVariableAccessedInClosure(element, target)) { | 8608 if (_isVariableAccessedInClosure(element, target)) { |
| 8592 _promoteManager.setType(element, null); | 8609 _promoteManager.setType(element, null); |
| 8593 } | 8610 } |
| 8594 } | 8611 } |
| 8595 } | 8612 } |
| 8596 } | 8613 } |
| 8597 | 8614 |
| 8598 /** | 8615 /** |
| 8599 * Checks each promoted variable in the current scope for compliance with the
following | 8616 * Checks each promoted variable in the current scope for compliance with the
following |
| 8600 * specification statement: | 8617 * specification statement: |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8873 * type is more specific than the current type. | 8890 * type is more specific than the current type. |
| 8874 * | 8891 * |
| 8875 * @param expression the expression used to access the static element whose ty
pes might be | 8892 * @param expression the expression used to access the static element whose ty
pes might be |
| 8876 * promoted | 8893 * promoted |
| 8877 * @param potentialType the potential type of the elements | 8894 * @param potentialType the potential type of the elements |
| 8878 */ | 8895 */ |
| 8879 void _promote(Expression expression, DartType potentialType) { | 8896 void _promote(Expression expression, DartType potentialType) { |
| 8880 VariableElement element = getPromotionStaticElement(expression); | 8897 VariableElement element = getPromotionStaticElement(expression); |
| 8881 if (element != null) { | 8898 if (element != null) { |
| 8882 // may be mutated somewhere in closure | 8899 // may be mutated somewhere in closure |
| 8883 if (element.isPotentiallyMutatedInClosure) { | 8900 if (_currentFunctionBody.isPotentiallyMutatedInClosure(element)) { |
| 8884 return; | 8901 return; |
| 8885 } | 8902 } |
| 8886 // prepare current variable type | 8903 // prepare current variable type |
| 8887 DartType type = _promoteManager.getType(element) ?? | 8904 DartType type = _promoteManager.getType(element) ?? |
| 8888 expression.staticType ?? | 8905 expression.staticType ?? |
| 8889 DynamicTypeImpl.instance; | 8906 DynamicTypeImpl.instance; |
| 8890 | 8907 |
| 8891 potentialType ??= DynamicTypeImpl.instance; | 8908 potentialType ??= DynamicTypeImpl.instance; |
| 8892 | 8909 |
| 8893 // Check if we can promote to potentialType from type. | 8910 // Check if we can promote to potentialType from type. |
| (...skipping 3891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12785 nonFields.add(node); | 12802 nonFields.add(node); |
| 12786 return null; | 12803 return null; |
| 12787 } | 12804 } |
| 12788 | 12805 |
| 12789 @override | 12806 @override |
| 12790 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 12807 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
| 12791 | 12808 |
| 12792 @override | 12809 @override |
| 12793 Object visitWithClause(WithClause node) => null; | 12810 Object visitWithClause(WithClause node) => null; |
| 12794 } | 12811 } |
| OLD | NEW |