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 4839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4850 void pushReturnContext(BlockFunctionBody node) { | 4850 void pushReturnContext(BlockFunctionBody node) { |
4851 DartType returnType = getType(node); | 4851 DartType returnType = getType(node); |
4852 _returnStack.add(returnType); | 4852 _returnStack.add(returnType); |
4853 _inferredReturn.add(BottomTypeImpl.instance); | 4853 _inferredReturn.add(BottomTypeImpl.instance); |
4854 } | 4854 } |
4855 | 4855 |
4856 /** | 4856 /** |
4857 * Place an info node into the error stream indicating that a | 4857 * Place an info node into the error stream indicating that a |
4858 * [type] has been inferred as the type of [node]. | 4858 * [type] has been inferred as the type of [node]. |
4859 */ | 4859 */ |
4860 void recordInference(AstNode node, DartType type) { | 4860 void recordInference(Expression node, DartType type) { |
4861 if (!_inferenceHints) { | 4861 if (!_inferenceHints) { |
4862 return; | 4862 return; |
4863 } | 4863 } |
4864 | 4864 |
4865 ErrorCode error; | 4865 ErrorCode error; |
4866 if (node is Literal) { | 4866 if (node is Literal) { |
4867 error = StrongModeCode.INFERRED_TYPE_LITERAL; | 4867 error = StrongModeCode.INFERRED_TYPE_LITERAL; |
4868 } else if (node is InstanceCreationExpression) { | 4868 } else if (node is InstanceCreationExpression) { |
4869 error = StrongModeCode.INFERRED_TYPE_ALLOCATION; | 4869 error = StrongModeCode.INFERRED_TYPE_ALLOCATION; |
4870 } else if (node is FunctionExpression) { | 4870 } else if (node is FunctionExpression) { |
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5646 * A comment before a function should be resolved in the context of the | 5646 * A comment before a function should be resolved in the context of the |
5647 * function. But when we incrementally resolve a comment, we don't want to | 5647 * function. But when we incrementally resolve a comment, we don't want to |
5648 * resolve the whole function. | 5648 * resolve the whole function. |
5649 * | 5649 * |
5650 * So, this flag is set to `true`, when just context of the function should | 5650 * So, this flag is set to `true`, when just context of the function should |
5651 * be built and the comment resolved. | 5651 * be built and the comment resolved. |
5652 */ | 5652 */ |
5653 bool resolveOnlyCommentInFunctionBody = false; | 5653 bool resolveOnlyCommentInFunctionBody = false; |
5654 | 5654 |
5655 /** | 5655 /** |
5656 * True if we're analyzing in strong mode. | |
5657 */ | |
5658 bool _strongMode; | |
5659 | |
5660 /** | |
5661 * Body of the function currently being analyzed, if any. | 5656 * Body of the function currently being analyzed, if any. |
5662 */ | 5657 */ |
5663 FunctionBody _currentFunctionBody; | 5658 FunctionBody _currentFunctionBody; |
5664 | 5659 |
5665 /** | 5660 /** |
5666 * Initialize a newly created visitor to resolve the nodes in an AST node. | 5661 * Initialize a newly created visitor to resolve the nodes in an AST node. |
5667 * | 5662 * |
5668 * The [definingLibrary] is the element for the library containing the node | 5663 * The [definingLibrary] is the element for the library containing the node |
5669 * being visited. The [source] is the source representing the compilation unit | 5664 * being visited. The [source] is the source representing the compilation unit |
5670 * containing the node being visited. The [typeProvider] is the object used to | 5665 * containing the node being visited. The [typeProvider] is the object used to |
(...skipping 10 matching lines...) Expand all Loading... |
5681 */ | 5676 */ |
5682 ResolverVisitor(LibraryElement definingLibrary, Source source, | 5677 ResolverVisitor(LibraryElement definingLibrary, Source source, |
5683 TypeProvider typeProvider, AnalysisErrorListener errorListener, | 5678 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
5684 {Scope nameScope}) | 5679 {Scope nameScope}) |
5685 : super(definingLibrary, source, typeProvider, errorListener, | 5680 : super(definingLibrary, source, typeProvider, errorListener, |
5686 nameScope: nameScope) { | 5681 nameScope: nameScope) { |
5687 this.elementResolver = new ElementResolver(this); | 5682 this.elementResolver = new ElementResolver(this); |
5688 this.typeSystem = definingLibrary.context.typeSystem; | 5683 this.typeSystem = definingLibrary.context.typeSystem; |
5689 bool strongModeHints = false; | 5684 bool strongModeHints = false; |
5690 AnalysisOptions options = definingLibrary.context.analysisOptions; | 5685 AnalysisOptions options = definingLibrary.context.analysisOptions; |
5691 _strongMode = options.strongMode; | |
5692 if (options is AnalysisOptionsImpl) { | 5686 if (options is AnalysisOptionsImpl) { |
5693 strongModeHints = options.strongModeHints; | 5687 strongModeHints = options.strongModeHints; |
5694 } | 5688 } |
5695 this.inferenceContext = new InferenceContext._( | 5689 this.inferenceContext = new InferenceContext._( |
5696 errorReporter, typeProvider, typeSystem, strongModeHints); | 5690 errorReporter, typeProvider, typeSystem, strongModeHints); |
5697 this.typeAnalyzer = new StaticTypeAnalyzer(this); | 5691 this.typeAnalyzer = new StaticTypeAnalyzer(this); |
5698 } | 5692 } |
5699 | 5693 |
5700 /** | 5694 /** |
5701 * Return the element representing the function containing the current node, o
r `null` if | 5695 * Return the element representing the function containing the current node, o
r `null` if |
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6372 // We do not visit the label because it needs to be visited in the context | 6366 // We do not visit the label because it needs to be visited in the context |
6373 // of the statement. | 6367 // of the statement. |
6374 // | 6368 // |
6375 node.accept(elementResolver); | 6369 node.accept(elementResolver); |
6376 node.accept(typeAnalyzer); | 6370 node.accept(typeAnalyzer); |
6377 return null; | 6371 return null; |
6378 } | 6372 } |
6379 | 6373 |
6380 @override | 6374 @override |
6381 Object visitDefaultFormalParameter(DefaultFormalParameter node) { | 6375 Object visitDefaultFormalParameter(DefaultFormalParameter node) { |
| 6376 InferenceContext.setType(node.defaultValue, node.parameter.element?.type); |
| 6377 super.visitDefaultFormalParameter(node); |
6382 ParameterElement element = node.element; | 6378 ParameterElement element = node.element; |
6383 InferenceContext.setType(node.defaultValue, element.type); | |
6384 super.visitDefaultFormalParameter(node); | |
6385 if (element.initializer != null && node.defaultValue != null) { | 6379 if (element.initializer != null && node.defaultValue != null) { |
6386 (element.initializer as FunctionElementImpl).returnType = | 6380 (element.initializer as FunctionElementImpl).returnType = |
6387 node.defaultValue.staticType; | 6381 node.defaultValue.staticType; |
6388 } | 6382 } |
6389 if (_strongMode && | |
6390 node.defaultValue != null && | |
6391 element.hasImplicitType && | |
6392 element is! FieldFormalParameterElement) { | |
6393 | |
6394 DartType type = node.defaultValue.staticType; | |
6395 if (!type.isBottom && !type.isDynamic) { | |
6396 (element as ParameterElementImpl).type = type; | |
6397 inferenceContext.recordInference(node, type); | |
6398 } | |
6399 } | |
6400 // Clone the ASTs for default formal parameters, so that we can use them | 6383 // Clone the ASTs for default formal parameters, so that we can use them |
6401 // during constant evaluation. | 6384 // during constant evaluation. |
6402 if (!LibraryElementImpl.hasResolutionCapability( | 6385 if (!LibraryElementImpl.hasResolutionCapability( |
6403 definingLibrary, LibraryResolutionCapability.constantExpressions)) { | 6386 definingLibrary, LibraryResolutionCapability.constantExpressions)) { |
6404 (element as ConstVariableElement).constantInitializer = | 6387 (element as ConstVariableElement).constantInitializer = |
6405 new ConstantAstCloner().cloneNode(node.defaultValue); | 6388 new ConstantAstCloner().cloneNode(node.defaultValue); |
6406 } | 6389 } |
6407 return null; | 6390 return null; |
6408 } | 6391 } |
6409 | 6392 |
(...skipping 4647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11057 return null; | 11040 return null; |
11058 } | 11041 } |
11059 if (identical(node.staticElement, variable)) { | 11042 if (identical(node.staticElement, variable)) { |
11060 if (node.inSetterContext()) { | 11043 if (node.inSetterContext()) { |
11061 result = true; | 11044 result = true; |
11062 } | 11045 } |
11063 } | 11046 } |
11064 return null; | 11047 return null; |
11065 } | 11048 } |
11066 } | 11049 } |
OLD | NEW |