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 12 matching lines...) Expand all Loading... |
23 import 'package:analyzer/src/generated/element_resolver.dart'; | 23 import 'package:analyzer/src/generated/element_resolver.dart'; |
24 import 'package:analyzer/src/generated/engine.dart'; | 24 import 'package:analyzer/src/generated/engine.dart'; |
25 import 'package:analyzer/src/generated/error.dart'; | 25 import 'package:analyzer/src/generated/error.dart'; |
26 import 'package:analyzer/src/generated/error_verifier.dart'; | 26 import 'package:analyzer/src/generated/error_verifier.dart'; |
27 import 'package:analyzer/src/generated/java_core.dart'; | 27 import 'package:analyzer/src/generated/java_core.dart'; |
28 import 'package:analyzer/src/generated/java_engine.dart'; | 28 import 'package:analyzer/src/generated/java_engine.dart'; |
29 import 'package:analyzer/src/generated/source.dart'; | 29 import 'package:analyzer/src/generated/source.dart'; |
30 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 30 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
31 import 'package:analyzer/src/generated/type_system.dart'; | 31 import 'package:analyzer/src/generated/type_system.dart'; |
32 import 'package:analyzer/src/generated/utilities_dart.dart'; | 32 import 'package:analyzer/src/generated/utilities_dart.dart'; |
33 import 'package:analyzer/src/task/strong/info.dart' | |
34 show InferredType, StaticInfo; | |
35 | 33 |
36 export 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; | 34 export 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; |
37 export 'package:analyzer/src/dart/resolver/scope.dart'; | 35 export 'package:analyzer/src/dart/resolver/scope.dart'; |
38 export 'package:analyzer/src/generated/type_system.dart'; | 36 export 'package:analyzer/src/generated/type_system.dart'; |
39 | 37 |
40 /** | 38 /** |
41 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for | 39 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for |
42 * violations of Dart best practices. | 40 * violations of Dart best practices. |
43 */ | 41 */ |
44 class BestPracticesVerifier extends RecursiveAstVisitor<Object> { | 42 class BestPracticesVerifier extends RecursiveAstVisitor<Object> { |
(...skipping 4594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4639 */ | 4637 */ |
4640 class InferenceContext { | 4638 class InferenceContext { |
4641 // TODO(leafp): Consider replacing these node properties with a | 4639 // TODO(leafp): Consider replacing these node properties with a |
4642 // hash table help in an instance of this class. | 4640 // hash table help in an instance of this class. |
4643 static const String _typeProperty = | 4641 static const String _typeProperty = |
4644 'analyzer.src.generated.InferenceContext.contextType'; | 4642 'analyzer.src.generated.InferenceContext.contextType'; |
4645 | 4643 |
4646 /** | 4644 /** |
4647 * The error listener on which to record inference information. | 4645 * The error listener on which to record inference information. |
4648 */ | 4646 */ |
4649 final AnalysisErrorListener _errorListener; | 4647 final ErrorReporter _errorReporter; |
4650 | 4648 |
4651 /** | 4649 /** |
4652 * If true, emit hints when types are inferred | 4650 * If true, emit hints when types are inferred |
4653 */ | 4651 */ |
4654 final bool _inferenceHints; | 4652 final bool _inferenceHints; |
4655 | 4653 |
4656 /** | 4654 /** |
4657 * Type provider, needed for type matching. | 4655 * Type provider, needed for type matching. |
4658 */ | 4656 */ |
4659 final TypeProvider _typeProvider; | 4657 final TypeProvider _typeProvider; |
(...skipping 12 matching lines...) Expand all Loading... |
4672 final List<DartType> _inferredReturn = <DartType>[]; | 4670 final List<DartType> _inferredReturn = <DartType>[]; |
4673 | 4671 |
4674 /** | 4672 /** |
4675 * A stack of return types for all of the enclosing | 4673 * A stack of return types for all of the enclosing |
4676 * functions and methods. | 4674 * functions and methods. |
4677 */ | 4675 */ |
4678 // TODO(leafp) Handle the implicit union type for Futures | 4676 // TODO(leafp) Handle the implicit union type for Futures |
4679 // https://github.com/dart-lang/sdk/issues/25322 | 4677 // https://github.com/dart-lang/sdk/issues/25322 |
4680 final List<DartType> _returnStack = <DartType>[]; | 4678 final List<DartType> _returnStack = <DartType>[]; |
4681 | 4679 |
4682 InferenceContext._(this._errorListener, TypeProvider typeProvider, | 4680 InferenceContext._(this._errorReporter, TypeProvider typeProvider, |
4683 this._typeSystem, this._inferenceHints) | 4681 this._typeSystem, this._inferenceHints) |
4684 : _typeProvider = typeProvider; | 4682 : _typeProvider = typeProvider; |
4685 | 4683 |
4686 /** | 4684 /** |
4687 * Get the return type of the current enclosing function, if any. | 4685 * Get the return type of the current enclosing function, if any. |
4688 * | 4686 * |
4689 * The type returned for a function is the type that is expected | 4687 * The type returned for a function is the type that is expected |
4690 * to be used in a return or yield context. For ordinary functions | 4688 * to be used in a return or yield context. For ordinary functions |
4691 * this is the same as the return type of the function. For async | 4689 * this is the same as the return type of the function. For async |
4692 * functions returning Future<T> and for generator functions | 4690 * functions returning Future<T> and for generator functions |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4751 DartType returnType = getType(node); | 4749 DartType returnType = getType(node); |
4752 _returnStack.add(returnType); | 4750 _returnStack.add(returnType); |
4753 _inferredReturn.add(BottomTypeImpl.instance); | 4751 _inferredReturn.add(BottomTypeImpl.instance); |
4754 } | 4752 } |
4755 | 4753 |
4756 /** | 4754 /** |
4757 * Place an info node into the error stream indicating that a | 4755 * Place an info node into the error stream indicating that a |
4758 * [type] has been inferred as the type of [node]. | 4756 * [type] has been inferred as the type of [node]. |
4759 */ | 4757 */ |
4760 void recordInference(Expression node, DartType type) { | 4758 void recordInference(Expression node, DartType type) { |
4761 StaticInfo info = InferredType.create(_typeSystem, node, type); | 4759 if (!_inferenceHints) { |
4762 if (!_inferenceHints || info == null) { | |
4763 return; | 4760 return; |
4764 } | 4761 } |
4765 AnalysisError error = info.toAnalysisError(); | 4762 |
4766 _errorListener.onError(error); | 4763 ErrorCode error; |
| 4764 if (node is Literal) { |
| 4765 error = StrongModeCode.INFERRED_TYPE_LITERAL; |
| 4766 } else if (node is InstanceCreationExpression) { |
| 4767 error = StrongModeCode.INFERRED_TYPE_ALLOCATION; |
| 4768 } else if (node is FunctionExpression) { |
| 4769 error = StrongModeCode.INFERRED_TYPE_CLOSURE; |
| 4770 } else { |
| 4771 error = StrongModeCode.INFERRED_TYPE; |
| 4772 } |
| 4773 |
| 4774 _errorReporter.reportErrorForNode(error, node, [node, type]); |
4767 } | 4775 } |
4768 | 4776 |
4769 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { | 4777 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { |
4770 if (t1 == t2) { | 4778 if (t1 == t2) { |
4771 return t2.typeArguments; | 4779 return t2.typeArguments; |
4772 } | 4780 } |
4773 List<DartType> tArgs1 = t1.typeArguments; | 4781 List<DartType> tArgs1 = t1.typeArguments; |
4774 List<DartType> tArgs2 = t2.typeArguments; | 4782 List<DartType> tArgs2 = t2.typeArguments; |
4775 // If t1 isn't a raw type, bail out | 4783 // If t1 isn't a raw type, bail out |
4776 if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) { | 4784 if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) { |
(...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5570 : super(definingLibrary, source, typeProvider, errorListener, | 5578 : super(definingLibrary, source, typeProvider, errorListener, |
5571 nameScope: nameScope) { | 5579 nameScope: nameScope) { |
5572 this.elementResolver = new ElementResolver(this); | 5580 this.elementResolver = new ElementResolver(this); |
5573 this.typeSystem = definingLibrary.context.typeSystem; | 5581 this.typeSystem = definingLibrary.context.typeSystem; |
5574 bool strongModeHints = false; | 5582 bool strongModeHints = false; |
5575 AnalysisOptions options = definingLibrary.context.analysisOptions; | 5583 AnalysisOptions options = definingLibrary.context.analysisOptions; |
5576 if (options is AnalysisOptionsImpl) { | 5584 if (options is AnalysisOptionsImpl) { |
5577 strongModeHints = options.strongModeHints; | 5585 strongModeHints = options.strongModeHints; |
5578 } | 5586 } |
5579 this.inferenceContext = new InferenceContext._( | 5587 this.inferenceContext = new InferenceContext._( |
5580 errorListener, typeProvider, typeSystem, strongModeHints); | 5588 errorReporter, typeProvider, typeSystem, strongModeHints); |
5581 this.typeAnalyzer = new StaticTypeAnalyzer(this); | 5589 this.typeAnalyzer = new StaticTypeAnalyzer(this); |
5582 } | 5590 } |
5583 | 5591 |
5584 /** | 5592 /** |
5585 * Return the element representing the function containing the current node, o
r `null` if | 5593 * Return the element representing the function containing the current node, o
r `null` if |
5586 * the current node is not contained in a function. | 5594 * the current node is not contained in a function. |
5587 * | 5595 * |
5588 * @return the element representing the function containing the current node | 5596 * @return the element representing the function containing the current node |
5589 */ | 5597 */ |
5590 ExecutableElement get enclosingFunction => _enclosingFunction; | 5598 ExecutableElement get enclosingFunction => _enclosingFunction; |
(...skipping 5343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10934 return null; | 10942 return null; |
10935 } | 10943 } |
10936 if (identical(node.staticElement, variable)) { | 10944 if (identical(node.staticElement, variable)) { |
10937 if (node.inSetterContext()) { | 10945 if (node.inSetterContext()) { |
10938 result = true; | 10946 result = true; |
10939 } | 10947 } |
10940 } | 10948 } |
10941 return null; | 10949 return null; |
10942 } | 10950 } |
10943 } | 10951 } |
OLD | NEW |