| 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 9420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 CompilationUnit) { |
| 9818 node.declarations.forEach(visitNode); |
| 9819 } else if (node is ClassDeclaration) { |
| 9820 node.members.forEach(visitNode); |
| 9821 } else if (node is DefaultFormalParameter) { |
| 9822 visitAllNodes(node.defaultValue); |
| 9823 } else if (node is FieldDeclaration) { |
| 9824 visitNode(node.fields); |
| 9825 } else if (node is FunctionBody) { |
| 9826 visitAllNodes(node); |
| 9827 } else if (node is FunctionDeclaration) { |
| 9828 visitNode(node.functionExpression.parameters); |
| 9829 visitAllNodes(node.functionExpression.body); |
| 9830 } else if (node is FormalParameterList) { |
| 9831 node.parameters.accept(this); |
| 9832 } else if (node is MethodDeclaration) { |
| 9833 visitNode(node.parameters); |
| 9834 visitAllNodes(node.body); |
| 9835 } else if (node is TopLevelVariableDeclaration) { |
| 9836 visitNode(node.variables); |
| 9837 } else if (node is VariableDeclaration) { |
| 9838 visitAllNodes(node.initializer); |
| 9839 } else if (node is VariableDeclarationList) { |
| 9840 node.variables.forEach(visitNode); |
| 9841 } |
| 9842 return null; |
| 9843 } |
| 9844 |
| 9845 // The mode in which we visit all nodes. |
| 9797 return super.visitNode(node); | 9846 return super.visitNode(node); |
| 9798 } | 9847 } |
| 9799 | 9848 |
| 9800 @override | 9849 @override |
| 9801 Object visitSimpleFormalParameter(SimpleFormalParameter node) { | 9850 Object visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 9802 super.visitSimpleFormalParameter(node); | 9851 super.visitSimpleFormalParameter(node); |
| 9803 DartType declaredType; | 9852 DartType declaredType; |
| 9804 TypeName typeName = node.type; | 9853 TypeName typeName = node.type; |
| 9805 if (typeName == null) { | 9854 if (typeName == null) { |
| 9806 declaredType = _dynamicType; | 9855 declaredType = _dynamicType; |
| (...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10632 return null; | 10681 return null; |
| 10633 } | 10682 } |
| 10634 if (identical(node.staticElement, variable)) { | 10683 if (identical(node.staticElement, variable)) { |
| 10635 if (node.inSetterContext()) { | 10684 if (node.inSetterContext()) { |
| 10636 result = true; | 10685 result = true; |
| 10637 } | 10686 } |
| 10638 } | 10687 } |
| 10639 return null; | 10688 return null; |
| 10640 } | 10689 } |
| 10641 } | 10690 } |
| OLD | NEW |