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 4817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4828 void pushReturnContext(BlockFunctionBody node) { | 4828 void pushReturnContext(BlockFunctionBody node) { |
4829 DartType returnType = getType(node); | 4829 DartType returnType = getType(node); |
4830 _returnStack.add(returnType); | 4830 _returnStack.add(returnType); |
4831 _inferredReturn.add(BottomTypeImpl.instance); | 4831 _inferredReturn.add(BottomTypeImpl.instance); |
4832 } | 4832 } |
4833 | 4833 |
4834 /** | 4834 /** |
4835 * Place an info node into the error stream indicating that a | 4835 * Place an info node into the error stream indicating that a |
4836 * [type] has been inferred as the type of [node]. | 4836 * [type] has been inferred as the type of [node]. |
4837 */ | 4837 */ |
4838 void recordInference(Expression node, DartType type) { | 4838 void recordInference(AstNode node, DartType type) { |
4839 if (!_inferenceHints) { | 4839 if (!_inferenceHints) { |
4840 return; | 4840 return; |
4841 } | 4841 } |
4842 | 4842 |
4843 ErrorCode error; | 4843 ErrorCode error; |
4844 if (node is Literal) { | 4844 if (node is Literal) { |
4845 error = StrongModeCode.INFERRED_TYPE_LITERAL; | 4845 error = StrongModeCode.INFERRED_TYPE_LITERAL; |
4846 } else if (node is InstanceCreationExpression) { | 4846 } else if (node is InstanceCreationExpression) { |
4847 error = StrongModeCode.INFERRED_TYPE_ALLOCATION; | 4847 error = StrongModeCode.INFERRED_TYPE_ALLOCATION; |
4848 } else if (node is FunctionExpression) { | 4848 } else if (node is FunctionExpression) { |
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5624 * A comment before a function should be resolved in the context of the | 5624 * A comment before a function should be resolved in the context of the |
5625 * function. But when we incrementally resolve a comment, we don't want to | 5625 * function. But when we incrementally resolve a comment, we don't want to |
5626 * resolve the whole function. | 5626 * resolve the whole function. |
5627 * | 5627 * |
5628 * So, this flag is set to `true`, when just context of the function should | 5628 * So, this flag is set to `true`, when just context of the function should |
5629 * be built and the comment resolved. | 5629 * be built and the comment resolved. |
5630 */ | 5630 */ |
5631 bool resolveOnlyCommentInFunctionBody = false; | 5631 bool resolveOnlyCommentInFunctionBody = false; |
5632 | 5632 |
5633 /** | 5633 /** |
| 5634 * True if we're analyzing in strong mode. |
| 5635 */ |
| 5636 bool _strongMode; |
| 5637 |
| 5638 /** |
5634 * Body of the function currently being analyzed, if any. | 5639 * Body of the function currently being analyzed, if any. |
5635 */ | 5640 */ |
5636 FunctionBody _currentFunctionBody; | 5641 FunctionBody _currentFunctionBody; |
5637 | 5642 |
5638 /** | 5643 /** |
5639 * Initialize a newly created visitor to resolve the nodes in an AST node. | 5644 * Initialize a newly created visitor to resolve the nodes in an AST node. |
5640 * | 5645 * |
5641 * The [definingLibrary] is the element for the library containing the node | 5646 * The [definingLibrary] is the element for the library containing the node |
5642 * being visited. The [source] is the source representing the compilation unit | 5647 * being visited. The [source] is the source representing the compilation unit |
5643 * containing the node being visited. The [typeProvider] is the object used to | 5648 * containing the node being visited. The [typeProvider] is the object used to |
(...skipping 10 matching lines...) Expand all Loading... |
5654 */ | 5659 */ |
5655 ResolverVisitor(LibraryElement definingLibrary, Source source, | 5660 ResolverVisitor(LibraryElement definingLibrary, Source source, |
5656 TypeProvider typeProvider, AnalysisErrorListener errorListener, | 5661 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
5657 {Scope nameScope}) | 5662 {Scope nameScope}) |
5658 : super(definingLibrary, source, typeProvider, errorListener, | 5663 : super(definingLibrary, source, typeProvider, errorListener, |
5659 nameScope: nameScope) { | 5664 nameScope: nameScope) { |
5660 this.elementResolver = new ElementResolver(this); | 5665 this.elementResolver = new ElementResolver(this); |
5661 this.typeSystem = definingLibrary.context.typeSystem; | 5666 this.typeSystem = definingLibrary.context.typeSystem; |
5662 bool strongModeHints = false; | 5667 bool strongModeHints = false; |
5663 AnalysisOptions options = definingLibrary.context.analysisOptions; | 5668 AnalysisOptions options = definingLibrary.context.analysisOptions; |
| 5669 _strongMode = options.strongMode; |
5664 if (options is AnalysisOptionsImpl) { | 5670 if (options is AnalysisOptionsImpl) { |
5665 strongModeHints = options.strongModeHints; | 5671 strongModeHints = options.strongModeHints; |
5666 } | 5672 } |
5667 this.inferenceContext = new InferenceContext._( | 5673 this.inferenceContext = new InferenceContext._( |
5668 errorReporter, typeProvider, typeSystem, strongModeHints); | 5674 errorReporter, typeProvider, typeSystem, strongModeHints); |
5669 this.typeAnalyzer = new StaticTypeAnalyzer(this); | 5675 this.typeAnalyzer = new StaticTypeAnalyzer(this); |
5670 } | 5676 } |
5671 | 5677 |
5672 /** | 5678 /** |
5673 * Return the element representing the function containing the current node, o
r `null` if | 5679 * 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... |
6344 // We do not visit the label because it needs to be visited in the context | 6350 // We do not visit the label because it needs to be visited in the context |
6345 // of the statement. | 6351 // of the statement. |
6346 // | 6352 // |
6347 node.accept(elementResolver); | 6353 node.accept(elementResolver); |
6348 node.accept(typeAnalyzer); | 6354 node.accept(typeAnalyzer); |
6349 return null; | 6355 return null; |
6350 } | 6356 } |
6351 | 6357 |
6352 @override | 6358 @override |
6353 Object visitDefaultFormalParameter(DefaultFormalParameter node) { | 6359 Object visitDefaultFormalParameter(DefaultFormalParameter node) { |
6354 InferenceContext.setType(node.defaultValue, node.parameter.element?.type); | 6360 ParameterElement element = node.element; |
| 6361 InferenceContext.setType(node.defaultValue, element.type); |
6355 super.visitDefaultFormalParameter(node); | 6362 super.visitDefaultFormalParameter(node); |
6356 ParameterElement element = node.element; | |
6357 if (element.initializer != null && node.defaultValue != null) { | 6363 if (element.initializer != null && node.defaultValue != null) { |
6358 (element.initializer as FunctionElementImpl).returnType = | 6364 (element.initializer as FunctionElementImpl).returnType = |
6359 node.defaultValue.staticType; | 6365 node.defaultValue.staticType; |
6360 } | 6366 } |
| 6367 if (_strongMode && |
| 6368 node.defaultValue != null && |
| 6369 element.hasImplicitType && |
| 6370 element is! FieldFormalParameterElement) { |
| 6371 |
| 6372 DartType type = node.defaultValue.staticType; |
| 6373 if (!type.isBottom && !type.isDynamic) { |
| 6374 (element as ParameterElementImpl).type = type; |
| 6375 inferenceContext.recordInference(node, type); |
| 6376 } |
| 6377 } |
6361 // Clone the ASTs for default formal parameters, so that we can use them | 6378 // Clone the ASTs for default formal parameters, so that we can use them |
6362 // during constant evaluation. | 6379 // during constant evaluation. |
6363 if (!LibraryElementImpl.hasResolutionCapability( | 6380 if (!LibraryElementImpl.hasResolutionCapability( |
6364 definingLibrary, LibraryResolutionCapability.constantExpressions)) { | 6381 definingLibrary, LibraryResolutionCapability.constantExpressions)) { |
6365 (element as ConstVariableElement).constantInitializer = | 6382 (element as ConstVariableElement).constantInitializer = |
6366 new ConstantAstCloner().cloneNode(node.defaultValue); | 6383 new ConstantAstCloner().cloneNode(node.defaultValue); |
6367 } | 6384 } |
6368 return null; | 6385 return null; |
6369 } | 6386 } |
6370 | 6387 |
(...skipping 4647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11018 return null; | 11035 return null; |
11019 } | 11036 } |
11020 if (identical(node.staticElement, variable)) { | 11037 if (identical(node.staticElement, variable)) { |
11021 if (node.inSetterContext()) { | 11038 if (node.inSetterContext()) { |
11022 result = true; | 11039 result = true; |
11023 } | 11040 } |
11024 } | 11041 } |
11025 return null; | 11042 return null; |
11026 } | 11043 } |
11027 } | 11044 } |
OLD | NEW |