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