| 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 engine.resolver; | 5 library engine.resolver; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'ast.dart'; | 9 import 'ast.dart'; |
| 10 import 'constant.dart'; | 10 import 'constant.dart'; |
| (...skipping 9707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9718 bool _isOverride(Element element) => element != null && element.isOverride; | 9718 bool _isOverride(Element element) => element != null && element.isOverride; |
| 9719 } | 9719 } |
| 9720 | 9720 |
| 9721 /** | 9721 /** |
| 9722 * An AST visitor that is used to resolve the some of the nodes within a single | 9722 * An AST visitor that is used to resolve the some of the nodes within a single |
| 9723 * compilation unit. The nodes that are skipped are those that are within | 9723 * compilation unit. The nodes that are skipped are those that are within |
| 9724 * function bodies. | 9724 * function bodies. |
| 9725 */ | 9725 */ |
| 9726 class PartialResolverVisitor extends ResolverVisitor { | 9726 class PartialResolverVisitor extends ResolverVisitor { |
| 9727 /** | 9727 /** |
| 9728 * A flag indicating whether the resolver is being run in strong mode. | |
| 9729 */ | |
| 9730 final bool strongMode; | |
| 9731 | |
| 9732 /** | |
| 9733 * The static variables and fields that have an initializer. These are the | 9728 * The static variables and fields that have an initializer. These are the |
| 9734 * variables that need to be re-resolved after static variables have their | 9729 * variables that need to be re-resolved after static variables have their |
| 9735 * types inferred. A subset of these variables are those whose types should | 9730 * types inferred. A subset of these variables are those whose types should |
| 9736 * be inferred. The list will be empty unless the resolver is being run in | 9731 * be inferred. |
| 9737 * strong mode. | |
| 9738 */ | 9732 */ |
| 9739 final List<VariableElement> variablesAndFields = <VariableElement>[]; | 9733 final List<VariableElement> staticVariables = <VariableElement>[]; |
| 9734 |
| 9735 /** |
| 9736 * The static and instance variables and fields that have an initializer. |
| 9737 * These are the variables whose types might be propagated. |
| 9738 */ |
| 9739 final List<VariableElement> propagableVariables = <VariableElement>[]; |
| 9740 | 9740 |
| 9741 /** | 9741 /** |
| 9742 * Initialize a newly created visitor to resolve the nodes in an AST node. | 9742 * Initialize a newly created visitor to resolve the nodes in an AST node. |
| 9743 * | 9743 * |
| 9744 * The [definingLibrary] is the element for the library containing the node | 9744 * The [definingLibrary] is the element for the library containing the node |
| 9745 * being visited. The [source] is the source representing the compilation unit | 9745 * being visited. The [source] is the source representing the compilation unit |
| 9746 * containing the node being visited. The [typeProvider] is the object used to | 9746 * containing the node being visited. The [typeProvider] is the object used to |
| 9747 * access the types from the core library. The [errorListener] is the error | 9747 * access the types from the core library. The [errorListener] is the error |
| 9748 * listener that will be informed of any errors that are found during | 9748 * listener that will be informed of any errors that are found during |
| 9749 * resolution. The [nameScope] is the scope used to resolve identifiers in the | 9749 * resolution. The [nameScope] is the scope used to resolve identifiers in the |
| 9750 * node that will first be visited. If `null` or unspecified, a new | 9750 * node that will first be visited. If `null` or unspecified, a new |
| 9751 * [LibraryScope] will be created based on [definingLibrary] and | 9751 * [LibraryScope] will be created based on [definingLibrary] and |
| 9752 * [typeProvider]. The [inheritanceManager] is used to perform inheritance | 9752 * [typeProvider]. The [inheritanceManager] is used to perform inheritance |
| 9753 * lookups. If `null` or unspecified, a new [InheritanceManager] will be | 9753 * lookups. If `null` or unspecified, a new [InheritanceManager] will be |
| 9754 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to | 9754 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to |
| 9755 * create the type analyzer. If `null` or unspecified, a type analyzer of | 9755 * create the type analyzer. If `null` or unspecified, a type analyzer of |
| 9756 * type [StaticTypeAnalyzer] will be created. | 9756 * type [StaticTypeAnalyzer] will be created. |
| 9757 */ | 9757 */ |
| 9758 PartialResolverVisitor(LibraryElement definingLibrary, Source source, | 9758 PartialResolverVisitor(LibraryElement definingLibrary, Source source, |
| 9759 TypeProvider typeProvider, AnalysisErrorListener errorListener, | 9759 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
| 9760 {Scope nameScope, | 9760 {Scope nameScope, |
| 9761 InheritanceManager inheritanceManager, | 9761 InheritanceManager inheritanceManager, |
| 9762 StaticTypeAnalyzerFactory typeAnalyzerFactory}) | 9762 StaticTypeAnalyzerFactory typeAnalyzerFactory}) |
| 9763 : strongMode = definingLibrary.context.analysisOptions.strongMode, | 9763 : super(definingLibrary, source, typeProvider, errorListener); |
| 9764 super(definingLibrary, source, typeProvider, errorListener); | |
| 9765 | 9764 |
| 9766 @override | 9765 @override |
| 9767 Object visitBlockFunctionBody(BlockFunctionBody node) { | 9766 Object visitBlockFunctionBody(BlockFunctionBody node) { |
| 9768 if (_shouldBeSkipped(node)) { | 9767 if (_shouldBeSkipped(node)) { |
| 9769 return null; | 9768 return null; |
| 9770 } | 9769 } |
| 9771 return super.visitBlockFunctionBody(node); | 9770 return super.visitBlockFunctionBody(node); |
| 9772 } | 9771 } |
| 9773 | 9772 |
| 9774 @override | 9773 @override |
| 9775 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { | 9774 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 9776 if (_shouldBeSkipped(node)) { | 9775 if (_shouldBeSkipped(node)) { |
| 9777 return null; | 9776 return null; |
| 9778 } | 9777 } |
| 9779 return super.visitExpressionFunctionBody(node); | 9778 return super.visitExpressionFunctionBody(node); |
| 9780 } | 9779 } |
| 9781 | 9780 |
| 9782 @override | 9781 @override |
| 9783 Object visitFieldDeclaration(FieldDeclaration node) { | 9782 Object visitFieldDeclaration(FieldDeclaration node) { |
| 9784 if (strongMode && node.isStatic) { | 9783 _addPropagableVariables(node.fields.variables); |
| 9785 _addVariables(node.fields.variables); | 9784 if (node.isStatic) { |
| 9785 _addStaticVariables(node.fields.variables); |
| 9786 } | 9786 } |
| 9787 return super.visitFieldDeclaration(node); | 9787 return super.visitFieldDeclaration(node); |
| 9788 } | 9788 } |
| 9789 | 9789 |
| 9790 @override | 9790 @override |
| 9791 Object visitNode(AstNode node) { | 9791 Object visitNode(AstNode node) { |
| 9792 return super.visitNode(node); | 9792 return super.visitNode(node); |
| 9793 } | 9793 } |
| 9794 | 9794 |
| 9795 @override | 9795 @override |
| 9796 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { | 9796 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| 9797 if (strongMode) { | 9797 _addPropagableVariables(node.variables.variables); |
| 9798 _addVariables(node.variables.variables); | 9798 _addStaticVariables(node.variables.variables); |
| 9799 return super.visitTopLevelVariableDeclaration(node); |
| 9800 } |
| 9801 |
| 9802 /** |
| 9803 * Add all of the [variables] with initializers to [propagableVariables]. |
| 9804 */ |
| 9805 void _addPropagableVariables(List<VariableDeclaration> variables) { |
| 9806 for (VariableDeclaration variable in variables) { |
| 9807 if (variable.initializer != null) { |
| 9808 VariableElement element = variable.element; |
| 9809 if (element.isConst || element.isFinal) { |
| 9810 propagableVariables.add(element); |
| 9811 } |
| 9812 } |
| 9799 } | 9813 } |
| 9800 return super.visitTopLevelVariableDeclaration(node); | |
| 9801 } | 9814 } |
| 9802 | 9815 |
| 9803 /** | 9816 /** |
| 9804 * Add all of the [variables] with initializers to the list of variables whose | 9817 * Add all of the [variables] with initializers to the list of variables whose |
| 9805 * type can be inferred. Technically, we only infer the types of variables | 9818 * type can be inferred. Technically, we only infer the types of variables |
| 9806 * that do not have a static type, but all variables with initializers | 9819 * that do not have a static type, but all variables with initializers |
| 9807 * potentially need to be re-resolved after inference because they might | 9820 * potentially need to be re-resolved after inference because they might |
| 9808 * refer to a field whose type was inferred. | 9821 * refer to a field whose type was inferred. |
| 9809 */ | 9822 */ |
| 9810 void _addVariables(NodeList<VariableDeclaration> variables) { | 9823 void _addStaticVariables(List<VariableDeclaration> variables) { |
| 9811 for (VariableDeclaration variable in variables) { | 9824 for (VariableDeclaration variable in variables) { |
| 9812 if (variable.initializer != null) { | 9825 if (variable.initializer != null) { |
| 9813 variablesAndFields.add(variable.element); | 9826 staticVariables.add(variable.element); |
| 9814 } | 9827 } |
| 9815 } | 9828 } |
| 9816 } | 9829 } |
| 9817 | 9830 |
| 9818 /** | 9831 /** |
| 9819 * Return `true` if the given function body should be skipped because it is | 9832 * Return `true` if the given function body should be skipped because it is |
| 9820 * the body of a top-level function, method or constructor. | 9833 * the body of a top-level function, method or constructor. |
| 9821 */ | 9834 */ |
| 9822 bool _shouldBeSkipped(FunctionBody body) { | 9835 bool _shouldBeSkipped(FunctionBody body) { |
| 9823 AstNode parent = body.parent; | 9836 AstNode parent = body.parent; |
| (...skipping 5725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15549 nonFields.add(node); | 15562 nonFields.add(node); |
| 15550 return null; | 15563 return null; |
| 15551 } | 15564 } |
| 15552 | 15565 |
| 15553 @override | 15566 @override |
| 15554 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 15567 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
| 15555 | 15568 |
| 15556 @override | 15569 @override |
| 15557 Object visitWithClause(WithClause node) => null; | 15570 Object visitWithClause(WithClause node) => null; |
| 15558 } | 15571 } |
| OLD | NEW |