Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 2432273003: Implement 'local' mode in TypeResolverVisitor. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 9420 matching lines...) Expand 10 before | Expand all | Expand 10 after
9431 */ 9431 */
9432 TypeSystem _typeSystem; 9432 TypeSystem _typeSystem;
9433 9433
9434 /** 9434 /**
9435 * The helper to resolve [TypeName]s. 9435 * The helper to resolve [TypeName]s.
9436 */ 9436 */
9437 TypeNameResolver _typeNameResolver; 9437 TypeNameResolver _typeNameResolver;
9438 9438
9439 final TypeResolverMode mode; 9439 final TypeResolverMode mode;
9440 9440
9441 bool _visitAllInLocalMode = false;
9442
9441 /** 9443 /**
9442 * Initialize a newly created visitor to resolve the nodes in an AST node. 9444 * Initialize a newly created visitor to resolve the nodes in an AST node.
9443 * 9445 *
9444 * [definingLibrary] is the element for the library containing the node being 9446 * [definingLibrary] is the element for the library containing the node being
9445 * visited. 9447 * visited.
9446 * [source] is the source representing the compilation unit containing the 9448 * [source] is the source representing the compilation unit containing the
9447 * node being visited. 9449 * node being visited.
9448 * [typeProvider] is the object used to access the types from the core 9450 * [typeProvider] is the object used to access the types from the core
9449 * library. 9451 * library.
9450 * [errorListener] is the error listener that will be informed of any errors 9452 * [errorListener] is the error listener that will be informed of any errors
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
9489 element is PropertyAccessorElement) { 9491 element is PropertyAccessorElement) {
9490 // This is the @proxy from dart.core 9492 // This is the @proxy from dart.core
9491 ElementAnnotationImpl elementAnnotation = node.elementAnnotation; 9493 ElementAnnotationImpl elementAnnotation = node.elementAnnotation;
9492 elementAnnotation.element = element; 9494 elementAnnotation.element = element;
9493 } 9495 }
9494 } 9496 }
9495 return null; 9497 return null;
9496 } 9498 }
9497 9499
9498 @override 9500 @override
9499 Object visitBlockFunctionBody(BlockFunctionBody node) {
9500 if (mode == TypeResolverMode.api) {
9501 return null;
9502 }
9503 return super.visitBlockFunctionBody(node);
9504 }
9505
9506 @override
9507 Object visitCatchClause(CatchClause node) { 9501 Object visitCatchClause(CatchClause node) {
9508 super.visitCatchClause(node); 9502 super.visitCatchClause(node);
9509 SimpleIdentifier exception = node.exceptionParameter; 9503 SimpleIdentifier exception = node.exceptionParameter;
9510 if (exception != null) { 9504 if (exception != null) {
9511 // If an 'on' clause is provided the type of the exception parameter is 9505 // If an 'on' clause is provided the type of the exception parameter is
9512 // the type in the 'on' clause. Otherwise, the type of the exception 9506 // the type in the 'on' clause. Otherwise, the type of the exception
9513 // parameter is 'Object'. 9507 // parameter is 'Object'.
9514 TypeName exceptionTypeName = node.exceptionType; 9508 TypeName exceptionTypeName = node.exceptionType;
9515 DartType exceptionType; 9509 DartType exceptionType;
9516 if (exceptionTypeName == null) { 9510 if (exceptionTypeName == null) {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
9655 declaredType = _dynamicType; 9649 declaredType = _dynamicType;
9656 } else { 9650 } else {
9657 declaredType = _typeNameResolver._getType(typeName); 9651 declaredType = _typeNameResolver._getType(typeName);
9658 } 9652 }
9659 LocalVariableElementImpl element = node.element as LocalVariableElementImpl; 9653 LocalVariableElementImpl element = node.element as LocalVariableElementImpl;
9660 element.type = declaredType; 9654 element.type = declaredType;
9661 return null; 9655 return null;
9662 } 9656 }
9663 9657
9664 @override 9658 @override
9665 Object visitExpressionFunctionBody(ExpressionFunctionBody node) {
9666 if (mode == TypeResolverMode.api) {
9667 return null;
9668 }
9669 return super.visitExpressionFunctionBody(node);
9670 }
9671
9672 @override
9673 Object visitFieldFormalParameter(FieldFormalParameter node) { 9659 Object visitFieldFormalParameter(FieldFormalParameter node) {
9674 super.visitFieldFormalParameter(node); 9660 super.visitFieldFormalParameter(node);
9675 Element element = node.identifier.staticElement; 9661 Element element = node.identifier.staticElement;
9676 if (element is ParameterElementImpl) { 9662 if (element is ParameterElementImpl) {
9677 FormalParameterList parameterList = node.parameters; 9663 FormalParameterList parameterList = node.parameters;
9678 if (parameterList == null) { 9664 if (parameterList == null) {
9679 DartType type; 9665 DartType type;
9680 TypeName typeName = node.type; 9666 TypeName typeName = node.type;
9681 if (typeName == null) { 9667 if (typeName == null) {
9682 element.hasImplicitType = true; 9668 element.hasImplicitType = true;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
9775 if (parameters != null && parameters.length > 0) { 9761 if (parameters != null && parameters.length > 0) {
9776 variable.type = parameters[0].type; 9762 variable.type = parameters[0].type;
9777 } 9763 }
9778 } 9764 }
9779 } 9765 }
9780 return null; 9766 return null;
9781 } 9767 }
9782 9768
9783 @override 9769 @override
9784 Object visitNode(AstNode node) { 9770 Object visitNode(AstNode node) {
9785 // In API mode we need to ignore: 9771 // In API mode we need to skip:
9772 // - function bodies;
9786 // - default values of parameters; 9773 // - default values of parameters;
9787 // - initializers of top-level variables. 9774 // - initializers of top-level variables.
9788 if (mode == TypeResolverMode.api) { 9775 if (mode == TypeResolverMode.api) {
9776 if (node is FunctionBody) {
9777 return null;
9778 }
9789 if (node is DefaultFormalParameter) { 9779 if (node is DefaultFormalParameter) {
9790 node.parameter.accept(this); 9780 node.parameter.accept(this);
9791 return null; 9781 return null;
9792 } 9782 }
9793 if (node is VariableDeclaration) { 9783 if (node is VariableDeclaration) {
9794 return null; 9784 return null;
9795 } 9785 }
9796 } 9786 }
9787
9788 // In local mode we need to resolve only:
9789 // - function bodies;
9790 // - default values of parameters;
9791 // - initializers of top-level variables.
9792 // So, we carefully visit only nodes that are, or contain, these nodes.
9793 // The client may choose to start visiting any node, but we still want to
9794 // resolve only type names that are local.
9795 if (mode == TypeResolverMode.local) {
9796 // We are in the state of visiting all nodes.
9797 if (_visitAllInLocalMode) {
9798 return super.visitNode(node);
9799 }
9800
9801 /**
9802 * Visit the given [node] and all its children.
9803 */
9804 void visitAllNodes(AstNode node) {
9805 if (node != null) {
9806 bool wasVisitAllInLocalMode = _visitAllInLocalMode;
9807 try {
9808 _visitAllInLocalMode = true;
9809 node.accept(this);
9810 } finally {
9811 _visitAllInLocalMode = wasVisitAllInLocalMode;
9812 }
9813 }
9814 }
9815
9816 // Visit only nodes that may contain type names to resolve.
9817 if (node is FunctionBody) {
Brian Wilkerson 2016/10/19 16:30:18 This like of is tests is kind of ugly, but if ther
Paul Berry 2016/10/19 16:37:49 Another possibility: create a little visitor which
scheglov 2016/10/19 16:47:10 Done.
9818 visitAllNodes(node);
9819 return null;
9820 }
9821 if (node is DefaultFormalParameter) {
9822 visitAllNodes(node.defaultValue);
9823 return null;
9824 }
9825 if (node is CompilationUnit) {
9826 node.declarations.forEach(visitNode);
9827 return null;
9828 }
9829 if (node is ClassDeclaration) {
9830 for (ClassMember member in node.members) {
9831 visitNode(member);
9832 }
9833 return null;
9834 }
9835 if (node is FieldDeclaration) {
9836 visitNode(node.fields);
9837 return null;
9838 }
9839 if (node is FunctionDeclaration) {
9840 node.functionExpression.parameters?.accept(this);
9841 visitAllNodes(node.functionExpression.body);
9842 return null;
9843 }
9844 if (node is FormalParameterList) {
9845 node.parameters.accept(this);
9846 return null;
9847 }
9848 if (node is MethodDeclaration) {
9849 visitAllNodes(node.body);
9850 return null;
9851 }
9852 if (node is TopLevelVariableDeclaration) {
9853 visitNode(node.variables);
9854 return null;
9855 }
9856 if (node is VariableDeclaration) {
9857 visitAllNodes(node.initializer);
9858 return null;
9859 }
9860 if (node is VariableDeclarationList) {
9861 node.variables.forEach(visitNode);
9862 return null;
9863 }
9864
9865 // Ignore all other nodes.
9866 return null;
9867 }
9868
9869 // The mode in which we visit all nodes.
9797 return super.visitNode(node); 9870 return super.visitNode(node);
9798 } 9871 }
9799 9872
9800 @override 9873 @override
9801 Object visitSimpleFormalParameter(SimpleFormalParameter node) { 9874 Object visitSimpleFormalParameter(SimpleFormalParameter node) {
9802 super.visitSimpleFormalParameter(node); 9875 super.visitSimpleFormalParameter(node);
9803 DartType declaredType; 9876 DartType declaredType;
9804 TypeName typeName = node.type; 9877 TypeName typeName = node.type;
9805 if (typeName == null) { 9878 if (typeName == null) {
9806 declaredType = _dynamicType; 9879 declaredType = _dynamicType;
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after
10632 return null; 10705 return null;
10633 } 10706 }
10634 if (identical(node.staticElement, variable)) { 10707 if (identical(node.staticElement, variable)) {
10635 if (node.inSetterContext()) { 10708 if (node.inSetterContext()) {
10636 result = true; 10709 result = true;
10637 } 10710 }
10638 } 10711 }
10639 return null; 10712 return null;
10640 } 10713 }
10641 } 10714 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698