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 9529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9540 /** | 9540 /** |
9541 * Return `true` if the given element has an override annotation associated wi
th it. | 9541 * Return `true` if the given element has an override annotation associated wi
th it. |
9542 * | 9542 * |
9543 * @param element the element being tested | 9543 * @param element the element being tested |
9544 * @return `true` if the element has an override annotation associated with it | 9544 * @return `true` if the element has an override annotation associated with it |
9545 */ | 9545 */ |
9546 bool _isOverride(Element element) => element != null && element.isOverride; | 9546 bool _isOverride(Element element) => element != null && element.isOverride; |
9547 } | 9547 } |
9548 | 9548 |
9549 /** | 9549 /** |
| 9550 * An AST visitor that is used to resolve the some of the nodes within a single |
| 9551 * compilation unit. The nodes that are skipped are those that are within |
| 9552 * function bodies. |
| 9553 */ |
| 9554 class PartialResolverVisitor extends ResolverVisitor { |
| 9555 /** |
| 9556 * A flag indicating whether the resolver is being run in strong mode. |
| 9557 */ |
| 9558 final bool strongMode; |
| 9559 |
| 9560 /** |
| 9561 * The static variables that have an initializer. These are the variables that |
| 9562 * need to be re-resolved after static variables have their types inferred. A |
| 9563 * subset of these variables are those whose types should be inferred. The |
| 9564 * list will be empty unless the resolver is being run in strong mode. |
| 9565 */ |
| 9566 final List<VariableElement> staticVariables = <VariableElement>[]; |
| 9567 |
| 9568 /** |
| 9569 * A flag indicating whether we should discard errors while resolving the |
| 9570 * initializer for variable declarations. We do this for top-level variables |
| 9571 * and fields because their initializer will be re-resolved at a later time. |
| 9572 */ |
| 9573 bool discardErrorsInInitializer = false; |
| 9574 |
| 9575 /** |
| 9576 * Initialize a newly created visitor to resolve the nodes in an AST node. |
| 9577 * |
| 9578 * The [definingLibrary] is the element for the library containing the node |
| 9579 * being visited. The [source] is the source representing the compilation unit |
| 9580 * containing the node being visited. The [typeProvider] is the object used to |
| 9581 * access the types from the core library. The [errorListener] is the error |
| 9582 * listener that will be informed of any errors that are found during |
| 9583 * resolution. The [nameScope] is the scope used to resolve identifiers in the |
| 9584 * node that will first be visited. If `null` or unspecified, a new |
| 9585 * [LibraryScope] will be created based on [definingLibrary] and |
| 9586 * [typeProvider]. The [inheritanceManager] is used to perform inheritance |
| 9587 * lookups. If `null` or unspecified, a new [InheritanceManager] will be |
| 9588 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to |
| 9589 * create the type analyzer. If `null` or unspecified, a type analyzer of |
| 9590 * type [StaticTypeAnalyzer] will be created. |
| 9591 */ |
| 9592 PartialResolverVisitor(LibraryElement definingLibrary, Source source, |
| 9593 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
| 9594 {Scope nameScope, |
| 9595 InheritanceManager inheritanceManager, |
| 9596 StaticTypeAnalyzerFactory typeAnalyzerFactory}) |
| 9597 : strongMode = definingLibrary.context.analysisOptions.strongMode, |
| 9598 super(definingLibrary, source, typeProvider, |
| 9599 new DisablableErrorListener(errorListener)); |
| 9600 |
| 9601 @override |
| 9602 Object visitBlockFunctionBody(BlockFunctionBody node) { |
| 9603 return null; |
| 9604 } |
| 9605 |
| 9606 @override |
| 9607 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 9608 return null; |
| 9609 } |
| 9610 |
| 9611 @override |
| 9612 Object visitFieldDeclaration(FieldDeclaration node) { |
| 9613 if (strongMode && node.isStatic) { |
| 9614 _addStaticVariables(node.fields.variables); |
| 9615 bool wasDiscarding = discardErrorsInInitializer; |
| 9616 discardErrorsInInitializer = true; |
| 9617 try { |
| 9618 return super.visitFieldDeclaration(node); |
| 9619 } finally { |
| 9620 discardErrorsInInitializer = wasDiscarding; |
| 9621 } |
| 9622 } |
| 9623 return super.visitFieldDeclaration(node); |
| 9624 } |
| 9625 |
| 9626 @override |
| 9627 Object visitNode(AstNode node) { |
| 9628 if (discardErrorsInInitializer) { |
| 9629 AstNode parent = node.parent; |
| 9630 if (parent is VariableDeclaration && parent.initializer == node) { |
| 9631 DisablableErrorListener listener = errorListener; |
| 9632 return listener.disableWhile(() => super.visitNode(node)); |
| 9633 } |
| 9634 } |
| 9635 return super.visitNode(node); |
| 9636 } |
| 9637 |
| 9638 @override |
| 9639 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| 9640 if (strongMode) { |
| 9641 _addStaticVariables(node.variables.variables); |
| 9642 bool wasDiscarding = discardErrorsInInitializer; |
| 9643 discardErrorsInInitializer = true; |
| 9644 try { |
| 9645 return super.visitTopLevelVariableDeclaration(node); |
| 9646 } finally { |
| 9647 discardErrorsInInitializer = wasDiscarding; |
| 9648 } |
| 9649 } |
| 9650 return super.visitTopLevelVariableDeclaration(node); |
| 9651 } |
| 9652 |
| 9653 /** |
| 9654 * Add all of the [variables] with initializers to the list of variables whose |
| 9655 * type can be inferred. Technically, we only infer the types of variables |
| 9656 * that do not have a static type, but all variables with initializers |
| 9657 * potentially need to be re-resolved after inference because they might |
| 9658 * refer to a field whose type was inferred. |
| 9659 */ |
| 9660 void _addStaticVariables(NodeList<VariableDeclaration> variables) { |
| 9661 for (VariableDeclaration variable in variables) { |
| 9662 if (variable.initializer != null) { |
| 9663 staticVariables.add(variable.element); |
| 9664 } |
| 9665 } |
| 9666 } |
| 9667 } |
| 9668 |
| 9669 /** |
9550 * Instances of the class `PubVerifier` traverse an AST structure looking for de
viations from | 9670 * Instances of the class `PubVerifier` traverse an AST structure looking for de
viations from |
9551 * pub best practices. | 9671 * pub best practices. |
9552 */ | 9672 */ |
9553 class PubVerifier extends RecursiveAstVisitor<Object> { | 9673 class PubVerifier extends RecursiveAstVisitor<Object> { |
9554 // static String _PUBSPEC_YAML = "pubspec.yaml"; | 9674 // static String _PUBSPEC_YAML = "pubspec.yaml"; |
9555 | 9675 |
9556 /** | 9676 /** |
9557 * The analysis context containing the sources to be analyzed | 9677 * The analysis context containing the sources to be analyzed |
9558 */ | 9678 */ |
9559 final AnalysisContext _context; | 9679 final AnalysisContext _context; |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10125 * resolve the whole function. | 10245 * resolve the whole function. |
10126 * | 10246 * |
10127 * So, this flag is set to `true`, when just context of the function should | 10247 * So, this flag is set to `true`, when just context of the function should |
10128 * be built and the comment resolved. | 10248 * be built and the comment resolved. |
10129 */ | 10249 */ |
10130 bool resolveOnlyCommentInFunctionBody = false; | 10250 bool resolveOnlyCommentInFunctionBody = false; |
10131 | 10251 |
10132 /** | 10252 /** |
10133 * Initialize a newly created visitor to resolve the nodes in an AST node. | 10253 * Initialize a newly created visitor to resolve the nodes in an AST node. |
10134 * | 10254 * |
10135 * [definingLibrary] is the element for the library containing the node being | 10255 * The [definingLibrary] is the element for the library containing the node |
10136 * visited. | 10256 * being visited. The [source] is the source representing the compilation unit |
10137 * [source] is the source representing the compilation unit containing the | 10257 * containing the node being visited. The [typeProvider] is the object used to |
10138 * node being visited. | 10258 * access the types from the core library. The [errorListener] is the error |
10139 * [typeProvider] the object used to access the types from the core library. | 10259 * listener that will be informed of any errors that are found during |
10140 * [errorListener] the error listener that will be informed of any errors | 10260 * resolution. The [nameScope] is the scope used to resolve identifiers in the |
10141 * that are found during resolution. | 10261 * node that will first be visited. If `null` or unspecified, a new |
10142 * [nameScope] is the scope used to resolve identifiers in the node that will | 10262 * [LibraryScope] will be created based on [definingLibrary] and |
10143 * first be visited. If `null` or unspecified, a new [LibraryScope] will be | 10263 * [typeProvider]. The [inheritanceManager] is used to perform inheritance |
10144 * created based on [definingLibrary] and [typeProvider]. | 10264 * lookups. If `null` or unspecified, a new [InheritanceManager] will be |
10145 * [inheritanceManager] is used to perform inheritance lookups. If `null` or | 10265 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to |
10146 * unspecified, a new [InheritanceManager] will be created based on | 10266 * create the type analyzer. If `null` or unspecified, a type analyzer of |
10147 * [definingLibrary]. | 10267 * type [StaticTypeAnalyzer] will be created. |
10148 * [typeAnalyzerFactory] is used to create the type analyzer. If `null` or | |
10149 * unspecified, a type analyzer of type [StaticTypeAnalyzer] will be created. | |
10150 */ | 10268 */ |
10151 ResolverVisitor(LibraryElement definingLibrary, Source source, | 10269 ResolverVisitor(LibraryElement definingLibrary, Source source, |
10152 TypeProvider typeProvider, AnalysisErrorListener errorListener, | 10270 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
10153 {Scope nameScope, | 10271 {Scope nameScope, |
10154 InheritanceManager inheritanceManager, | 10272 InheritanceManager inheritanceManager, |
10155 StaticTypeAnalyzerFactory typeAnalyzerFactory}) | 10273 StaticTypeAnalyzerFactory typeAnalyzerFactory}) |
10156 : super(definingLibrary, source, typeProvider, errorListener, | 10274 : super(definingLibrary, source, typeProvider, errorListener, |
10157 nameScope: nameScope) { | 10275 nameScope: nameScope) { |
10158 if (inheritanceManager == null) { | 10276 if (inheritanceManager == null) { |
10159 this._inheritanceManager = new InheritanceManager(definingLibrary); | 10277 this._inheritanceManager = new InheritanceManager(definingLibrary); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10278 } | 10396 } |
10279 if (kind == ElementKind.PARAMETER) { | 10397 if (kind == ElementKind.PARAMETER) { |
10280 return element as VariableElement; | 10398 return element as VariableElement; |
10281 } | 10399 } |
10282 return null; | 10400 return null; |
10283 } | 10401 } |
10284 | 10402 |
10285 /** | 10403 /** |
10286 * Prepares this [ResolverVisitor] to using it for incremental resolution. | 10404 * Prepares this [ResolverVisitor] to using it for incremental resolution. |
10287 */ | 10405 */ |
10288 void initForIncrementalResolution() { | 10406 void initForIncrementalResolution([Declaration declaration = null]) { |
| 10407 if (declaration != null) { |
| 10408 Element element = declaration.element; |
| 10409 if (element is ExecutableElement) { |
| 10410 _enclosingFunction = element; |
| 10411 } |
| 10412 _commentBeforeFunction = declaration.documentationComment; |
| 10413 } |
10289 _overrideManager.enterScope(); | 10414 _overrideManager.enterScope(); |
10290 } | 10415 } |
10291 | 10416 |
10292 /** | 10417 /** |
10293 * If it is appropriate to do so, override the current type of the static and
propagated elements | 10418 * If it is appropriate to do so, override the current type of the static and
propagated elements |
10294 * associated with the given expression with the given type. Generally speakin
g, it is appropriate | 10419 * associated with the given expression with the given type. Generally speakin
g, it is appropriate |
10295 * if the given type is more specific than the current type. | 10420 * if the given type is more specific than the current type. |
10296 * | 10421 * |
10297 * @param expression the expression used to access the static and propagated e
lements whose types | 10422 * @param expression the expression used to access the static and propagated e
lements whose types |
10298 * might be overridden | 10423 * might be overridden |
(...skipping 5114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15413 nonFields.add(node); | 15538 nonFields.add(node); |
15414 return null; | 15539 return null; |
15415 } | 15540 } |
15416 | 15541 |
15417 @override | 15542 @override |
15418 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 15543 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
15419 | 15544 |
15420 @override | 15545 @override |
15421 Object visitWithClause(WithClause node) => null; | 15546 Object visitWithClause(WithClause node) => null; |
15422 } | 15547 } |
OLD | NEW |