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/standard_resolution_map.dart'; | 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
(...skipping 5004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5015 * A comment before a function should be resolved in the context of the | 5015 * A comment before a function should be resolved in the context of the |
5016 * function. But when we incrementally resolve a comment, we don't want to | 5016 * function. But when we incrementally resolve a comment, we don't want to |
5017 * resolve the whole function. | 5017 * resolve the whole function. |
5018 * | 5018 * |
5019 * So, this flag is set to `true`, when just context of the function should | 5019 * So, this flag is set to `true`, when just context of the function should |
5020 * be built and the comment resolved. | 5020 * be built and the comment resolved. |
5021 */ | 5021 */ |
5022 bool resolveOnlyCommentInFunctionBody = false; | 5022 bool resolveOnlyCommentInFunctionBody = false; |
5023 | 5023 |
5024 /** | 5024 /** |
5025 * This flag is set to `true` while the type of a top-level variable or a | |
5026 * class field is being inferred using its initializer. | |
5027 */ | |
5028 bool isTopLevelInference; | |
5029 | |
5030 /** | |
5031 * Body of the function currently being analyzed, if any. | 5025 * Body of the function currently being analyzed, if any. |
5032 */ | 5026 */ |
5033 FunctionBody _currentFunctionBody; | 5027 FunctionBody _currentFunctionBody; |
5034 | 5028 |
5035 /** | 5029 /** |
5036 * Are we running in strong mode or not. | 5030 * Are we running in strong mode or not. |
5037 */ | 5031 */ |
5038 bool strongMode; | 5032 bool strongMode; |
5039 | 5033 |
5040 /** | 5034 /** |
5041 * Initialize a newly created visitor to resolve the nodes in an AST node. | 5035 * Initialize a newly created visitor to resolve the nodes in an AST node. |
5042 * | 5036 * |
5043 * The [definingLibrary] is the element for the library containing the node | 5037 * The [definingLibrary] is the element for the library containing the node |
5044 * being visited. The [source] is the source representing the compilation unit | 5038 * being visited. The [source] is the source representing the compilation unit |
5045 * containing the node being visited. The [typeProvider] is the object used to | 5039 * containing the node being visited. The [typeProvider] is the object used to |
5046 * access the types from the core library. The [errorListener] is the error | 5040 * access the types from the core library. The [errorListener] is the error |
5047 * listener that will be informed of any errors that are found during | 5041 * listener that will be informed of any errors that are found during |
5048 * resolution. The [nameScope] is the scope used to resolve identifiers in the | 5042 * resolution. The [nameScope] is the scope used to resolve identifiers in the |
5049 * node that will first be visited. If `null` or unspecified, a new | 5043 * node that will first be visited. If `null` or unspecified, a new |
5050 * [LibraryScope] will be created based on [definingLibrary] and | 5044 * [LibraryScope] will be created based on [definingLibrary] and |
5051 * [typeProvider]. The [inheritanceManager] is used to perform inheritance | 5045 * [typeProvider]. The [inheritanceManager] is used to perform inheritance |
5052 * lookups. If `null` or unspecified, a new [InheritanceManager] will be | 5046 * lookups. If `null` or unspecified, a new [InheritanceManager] will be |
5053 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to | 5047 * created based on [definingLibrary]. The [typeAnalyzerFactory] is used to |
5054 * create the type analyzer. If `null` or unspecified, a type analyzer of | 5048 * create the type analyzer. If `null` or unspecified, a type analyzer of |
5055 * type [StaticTypeAnalyzer] will be created. | 5049 * type [StaticTypeAnalyzer] will be created. |
5056 */ | 5050 */ |
5057 ResolverVisitor(LibraryElement definingLibrary, Source source, | 5051 ResolverVisitor(LibraryElement definingLibrary, Source source, |
5058 TypeProvider typeProvider, AnalysisErrorListener errorListener, | 5052 TypeProvider typeProvider, AnalysisErrorListener errorListener, |
5059 {Scope nameScope, this.isTopLevelInference: false}) | 5053 {Scope nameScope}) |
5060 : super(definingLibrary, source, typeProvider, errorListener, | 5054 : super(definingLibrary, source, typeProvider, errorListener, |
5061 nameScope: nameScope) { | 5055 nameScope: nameScope) { |
5062 AnalysisOptions options = definingLibrary.context.analysisOptions; | 5056 AnalysisOptions options = definingLibrary.context.analysisOptions; |
5063 this.strongMode = options.strongMode; | 5057 this.strongMode = options.strongMode; |
5064 this.elementResolver = new ElementResolver(this); | 5058 this.elementResolver = new ElementResolver(this); |
5065 this.typeSystem = definingLibrary.context.typeSystem; | 5059 this.typeSystem = definingLibrary.context.typeSystem; |
5066 bool strongModeHints = false; | 5060 bool strongModeHints = false; |
5067 if (options is AnalysisOptionsImpl) { | 5061 if (options is AnalysisOptionsImpl) { |
5068 strongModeHints = options.strongModeHints; | 5062 strongModeHints = options.strongModeHints; |
5069 } | 5063 } |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5549 } | 5543 } |
5550 rightOperand?.accept(this); | 5544 rightOperand?.accept(this); |
5551 } | 5545 } |
5552 node.accept(elementResolver); | 5546 node.accept(elementResolver); |
5553 node.accept(typeAnalyzer); | 5547 node.accept(typeAnalyzer); |
5554 return null; | 5548 return null; |
5555 } | 5549 } |
5556 | 5550 |
5557 @override | 5551 @override |
5558 Object visitBlockFunctionBody(BlockFunctionBody node) { | 5552 Object visitBlockFunctionBody(BlockFunctionBody node) { |
5559 bool wasTopLevelInference = isTopLevelInference; | |
5560 isTopLevelInference = false; | |
5561 _overrideManager.enterScope(); | 5553 _overrideManager.enterScope(); |
5562 try { | 5554 try { |
5563 inferenceContext.pushReturnContext(node); | 5555 inferenceContext.pushReturnContext(node); |
5564 super.visitBlockFunctionBody(node); | 5556 super.visitBlockFunctionBody(node); |
5565 } finally { | 5557 } finally { |
5566 isTopLevelInference = wasTopLevelInference; | |
5567 _overrideManager.exitScope(); | 5558 _overrideManager.exitScope(); |
5568 inferenceContext.popReturnContext(node); | 5559 inferenceContext.popReturnContext(node); |
5569 } | 5560 } |
5570 return null; | 5561 return null; |
5571 } | 5562 } |
5572 | 5563 |
5573 @override | 5564 @override |
5574 Object visitBreakStatement(BreakStatement node) { | 5565 Object visitBreakStatement(BreakStatement node) { |
5575 // | 5566 // |
5576 // We do not visit the label because it needs to be visited in the context | 5567 // We do not visit the label because it needs to be visited in the context |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5875 InferenceContext.setTypeFromNode(node.expression, node); | 5866 InferenceContext.setTypeFromNode(node.expression, node); |
5876 super.visitExpressionFunctionBody(node); | 5867 super.visitExpressionFunctionBody(node); |
5877 } finally { | 5868 } finally { |
5878 _overrideManager.exitScope(); | 5869 _overrideManager.exitScope(); |
5879 } | 5870 } |
5880 return null; | 5871 return null; |
5881 } | 5872 } |
5882 | 5873 |
5883 @override | 5874 @override |
5884 Object visitFieldDeclaration(FieldDeclaration node) { | 5875 Object visitFieldDeclaration(FieldDeclaration node) { |
5885 bool wasTopLevelInference = isTopLevelInference; | |
5886 isTopLevelInference = node.fields.type == null; | |
5887 _overrideManager.enterScope(); | 5876 _overrideManager.enterScope(); |
5888 try { | 5877 try { |
5889 super.visitFieldDeclaration(node); | 5878 super.visitFieldDeclaration(node); |
5890 } finally { | 5879 } finally { |
5891 isTopLevelInference = wasTopLevelInference; | |
5892 Map<VariableElement, DartType> overrides = | 5880 Map<VariableElement, DartType> overrides = |
5893 _overrideManager.captureOverrides(node.fields); | 5881 _overrideManager.captureOverrides(node.fields); |
5894 _overrideManager.exitScope(); | 5882 _overrideManager.exitScope(); |
5895 _overrideManager.applyOverrides(overrides); | 5883 _overrideManager.applyOverrides(overrides); |
5896 } | 5884 } |
5897 return null; | 5885 return null; |
5898 } | 5886 } |
5899 | 5887 |
5900 @override | 5888 @override |
5901 Object visitForEachStatement(ForEachStatement node) { | 5889 Object visitForEachStatement(ForEachStatement node) { |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6384 try { | 6372 try { |
6385 super.visitSwitchDefault(node); | 6373 super.visitSwitchDefault(node); |
6386 } finally { | 6374 } finally { |
6387 _overrideManager.exitScope(); | 6375 _overrideManager.exitScope(); |
6388 } | 6376 } |
6389 return null; | 6377 return null; |
6390 } | 6378 } |
6391 | 6379 |
6392 @override | 6380 @override |
6393 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { | 6381 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
6394 bool wasTopLevelInference = isTopLevelInference; | |
6395 isTopLevelInference = node.variables.type == null; | |
6396 _overrideManager.enterScope(); | 6382 _overrideManager.enterScope(); |
6397 try { | 6383 try { |
6398 super.visitTopLevelVariableDeclaration(node); | 6384 super.visitTopLevelVariableDeclaration(node); |
6399 } finally { | 6385 } finally { |
6400 isTopLevelInference = wasTopLevelInference; | |
6401 Map<VariableElement, DartType> overrides = | 6386 Map<VariableElement, DartType> overrides = |
6402 _overrideManager.captureOverrides(node.variables); | 6387 _overrideManager.captureOverrides(node.variables); |
6403 _overrideManager.exitScope(); | 6388 _overrideManager.exitScope(); |
6404 _overrideManager.applyOverrides(overrides); | 6389 _overrideManager.applyOverrides(overrides); |
6405 } | 6390 } |
6406 return null; | 6391 return null; |
6407 } | 6392 } |
6408 | 6393 |
6409 @override | 6394 @override |
6410 Object visitTypeName(TypeName node) => null; | 6395 Object visitTypeName(TypeName node) => null; |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6669 | 6654 |
6670 FunctionType _inferArgumentTypesForGeneric(AstNode inferenceNode, | 6655 FunctionType _inferArgumentTypesForGeneric(AstNode inferenceNode, |
6671 DartType uninstantiatedType, TypeArgumentList typeArguments, | 6656 DartType uninstantiatedType, TypeArgumentList typeArguments, |
6672 {AstNode errorNode}) { | 6657 {AstNode errorNode}) { |
6673 errorNode ??= inferenceNode; | 6658 errorNode ??= inferenceNode; |
6674 TypeSystem ts = typeSystem; | 6659 TypeSystem ts = typeSystem; |
6675 if (typeArguments == null && | 6660 if (typeArguments == null && |
6676 uninstantiatedType is FunctionType && | 6661 uninstantiatedType is FunctionType && |
6677 uninstantiatedType.typeFormals.isNotEmpty && | 6662 uninstantiatedType.typeFormals.isNotEmpty && |
6678 ts is StrongTypeSystemImpl) { | 6663 ts is StrongTypeSystemImpl) { |
6679 if (isTopLevelInference) { | |
6680 if (uninstantiatedType.typeFormals.isNotEmpty) { | |
6681 return null; | |
6682 } | |
6683 } | |
6684 return ts.inferGenericFunctionOrType/*<FunctionType>*/( | 6664 return ts.inferGenericFunctionOrType/*<FunctionType>*/( |
6685 uninstantiatedType, | 6665 uninstantiatedType, |
6686 ParameterElement.EMPTY_LIST, | 6666 ParameterElement.EMPTY_LIST, |
6687 DartType.EMPTY_LIST, | 6667 DartType.EMPTY_LIST, |
6688 InferenceContext.getContext(inferenceNode), | 6668 InferenceContext.getContext(inferenceNode), |
6689 downwards: true, | 6669 downwards: true, |
6690 errorReporter: errorReporter, | 6670 errorReporter: errorReporter, |
6691 errorNode: errorNode); | 6671 errorNode: errorNode); |
6692 } | 6672 } |
6693 return null; | 6673 return null; |
(...skipping 4222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10916 return null; | 10896 return null; |
10917 } | 10897 } |
10918 if (identical(node.staticElement, variable)) { | 10898 if (identical(node.staticElement, variable)) { |
10919 if (node.inSetterContext()) { | 10899 if (node.inSetterContext()) { |
10920 result = true; | 10900 result = true; |
10921 } | 10901 } |
10922 } | 10902 } |
10923 return null; | 10903 return null; |
10924 } | 10904 } |
10925 } | 10905 } |
OLD | NEW |