| 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 11 matching lines...) Expand all Loading... |
| 22 import 'package:analyzer/src/dart/element/type.dart'; | 22 import 'package:analyzer/src/dart/element/type.dart'; |
| 23 import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; | 23 import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; |
| 24 import 'package:analyzer/src/dart/resolver/scope.dart'; | 24 import 'package:analyzer/src/dart/resolver/scope.dart'; |
| 25 import 'package:analyzer/src/error/codes.dart'; | 25 import 'package:analyzer/src/error/codes.dart'; |
| 26 import 'package:analyzer/src/generated/constant.dart'; | 26 import 'package:analyzer/src/generated/constant.dart'; |
| 27 import 'package:analyzer/src/generated/element_resolver.dart'; | 27 import 'package:analyzer/src/generated/element_resolver.dart'; |
| 28 import 'package:analyzer/src/generated/engine.dart'; | 28 import 'package:analyzer/src/generated/engine.dart'; |
| 29 import 'package:analyzer/src/generated/error_verifier.dart'; | 29 import 'package:analyzer/src/generated/error_verifier.dart'; |
| 30 import 'package:analyzer/src/generated/source.dart'; | 30 import 'package:analyzer/src/generated/source.dart'; |
| 31 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 31 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
| 32 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
| 32 import 'package:analyzer/src/generated/type_system.dart'; | 33 import 'package:analyzer/src/generated/type_system.dart'; |
| 33 import 'package:analyzer/src/generated/utilities_dart.dart'; | 34 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 34 | 35 |
| 35 export 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; | 36 export 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; |
| 36 export 'package:analyzer/src/dart/resolver/scope.dart'; | 37 export 'package:analyzer/src/dart/resolver/scope.dart'; |
| 37 export 'package:analyzer/src/generated/type_system.dart'; | 38 export 'package:analyzer/src/generated/type_system.dart'; |
| 38 | 39 |
| 39 /** | 40 /** |
| 40 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for | 41 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for |
| 41 * violations of Dart best practices. | 42 * violations of Dart best practices. |
| (...skipping 4119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4161 if (_returnStack.isEmpty) { | 4162 if (_returnStack.isEmpty) { |
| 4162 return; | 4163 return; |
| 4163 } | 4164 } |
| 4164 | 4165 |
| 4165 DartType inferred = _inferredReturn.last; | 4166 DartType inferred = _inferredReturn.last; |
| 4166 inferred = _typeSystem.getLeastUpperBound(type, inferred); | 4167 inferred = _typeSystem.getLeastUpperBound(type, inferred); |
| 4167 _inferredReturn[_inferredReturn.length - 1] = inferred; | 4168 _inferredReturn[_inferredReturn.length - 1] = inferred; |
| 4168 } | 4169 } |
| 4169 | 4170 |
| 4170 /** | 4171 /** |
| 4172 * Like [getContext] but expands a union type into a list of types. |
| 4173 */ |
| 4174 Iterable<DartType> getTypes(AstNode node) { |
| 4175 DartType t = getContext(node); |
| 4176 if (t == null) { |
| 4177 return DartType.EMPTY_LIST; |
| 4178 } |
| 4179 if (t is InterfaceType && t.isDartAsyncFutureOr) { |
| 4180 var tArg = t.typeArguments[0]; // The T in FutureOr<T> |
| 4181 return [ |
| 4182 _typeProvider.futureType.instantiate([tArg]), |
| 4183 tArg |
| 4184 ]; |
| 4185 } |
| 4186 return [t]; |
| 4187 } |
| 4188 |
| 4189 /** |
| 4171 * Match type [t1] against type [t2] as follows. | 4190 * Match type [t1] against type [t2] as follows. |
| 4172 * If `t1 = I<dynamic, ..., dynamic>`, then look for a supertype | 4191 * If `t1 = I<dynamic, ..., dynamic>`, then look for a supertype |
| 4173 * of t1 of the form `K<S0, ..., Sm>` where `t2 = K<S0', ..., Sm'>` | 4192 * of t1 of the form `K<S0, ..., Sm>` where `t2 = K<S0', ..., Sm'>` |
| 4174 * If the supertype exists, use the constraints `S0 <: S0', ... Sm <: Sm'` | 4193 * If the supertype exists, use the constraints `S0 <: S0', ... Sm <: Sm'` |
| 4175 * to derive a concrete instantation for I of the form `<T0, ..., Tn>`, | 4194 * to derive a concrete instantation for I of the form `<T0, ..., Tn>`, |
| 4176 * such that `I<T0, .., Tn> <: t2` | 4195 * such that `I<T0, .., Tn> <: t2` |
| 4177 */ | 4196 */ |
| 4178 List<DartType> matchTypes(DartType t1, DartType t2) => | 4197 List<DartType> matchTypes(DartType t1, DartType t2) => |
| 4179 (t1 is InterfaceType && t2 is InterfaceType) ? _matchTypes(t1, t2) : null; | 4198 (t1 is InterfaceType && t2 is InterfaceType) ? _matchTypes(t1, t2) : null; |
| 4180 | 4199 |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4365 */ | 4384 */ |
| 4366 static DartType getType(AstNode node) { | 4385 static DartType getType(AstNode node) { |
| 4367 DartType t = getContext(node); | 4386 DartType t = getContext(node); |
| 4368 if (t is InterfaceType && t.isDartAsyncFutureOr) { | 4387 if (t is InterfaceType && t.isDartAsyncFutureOr) { |
| 4369 return t.typeArguments[0]; // The T in FutureOr<T> | 4388 return t.typeArguments[0]; // The T in FutureOr<T> |
| 4370 } | 4389 } |
| 4371 return t; | 4390 return t; |
| 4372 } | 4391 } |
| 4373 | 4392 |
| 4374 /** | 4393 /** |
| 4375 * Like [getContext] but expands a union type into a list of types. | |
| 4376 */ | |
| 4377 Iterable<DartType> getTypes(AstNode node) { | |
| 4378 DartType t = getContext(node); | |
| 4379 if (t == null) { | |
| 4380 return DartType.EMPTY_LIST; | |
| 4381 } | |
| 4382 if (t is InterfaceType && t.isDartAsyncFutureOr) { | |
| 4383 var tArg = t.typeArguments[0]; // The T in FutureOr<T> | |
| 4384 return [ | |
| 4385 _typeProvider.futureType.instantiate([tArg]), | |
| 4386 tArg | |
| 4387 ]; | |
| 4388 } | |
| 4389 return [t]; | |
| 4390 } | |
| 4391 | |
| 4392 /** | |
| 4393 * Attach contextual type information [type] to [node] for use during | 4394 * Attach contextual type information [type] to [node] for use during |
| 4394 * inference. | 4395 * inference. |
| 4395 */ | 4396 */ |
| 4396 static void setType(AstNode node, DartType type) { | 4397 static void setType(AstNode node, DartType type) { |
| 4397 if (type == null || type.isDynamic) { | 4398 if (type == null || type.isDynamic) { |
| 4398 clearType(node); | 4399 clearType(node); |
| 4399 } else { | 4400 } else { |
| 4400 node?.setProperty(_typeProperty, type); | 4401 node?.setProperty(_typeProperty, type); |
| 4401 } | 4402 } |
| 4402 } | 4403 } |
| (...skipping 5058 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9461 _stackTraceType = _getType(coreNamespace, "StackTrace"); | 9462 _stackTraceType = _getType(coreNamespace, "StackTrace"); |
| 9462 _streamType = _getType(asyncNamespace, "Stream"); | 9463 _streamType = _getType(asyncNamespace, "Stream"); |
| 9463 _stringType = _getType(coreNamespace, "String"); | 9464 _stringType = _getType(coreNamespace, "String"); |
| 9464 _symbolType = _getType(coreNamespace, "Symbol"); | 9465 _symbolType = _getType(coreNamespace, "Symbol"); |
| 9465 _typeType = _getType(coreNamespace, "Type"); | 9466 _typeType = _getType(coreNamespace, "Type"); |
| 9466 _undefinedType = UndefinedTypeImpl.instance; | 9467 _undefinedType = UndefinedTypeImpl.instance; |
| 9467 _futureDynamicType = _futureType.instantiate(<DartType>[_dynamicType]); | 9468 _futureDynamicType = _futureType.instantiate(<DartType>[_dynamicType]); |
| 9468 _futureNullType = _futureType.instantiate(<DartType>[_nullType]); | 9469 _futureNullType = _futureType.instantiate(<DartType>[_nullType]); |
| 9469 _iterableDynamicType = _iterableType.instantiate(<DartType>[_dynamicType]); | 9470 _iterableDynamicType = _iterableType.instantiate(<DartType>[_dynamicType]); |
| 9470 _streamDynamicType = _streamType.instantiate(<DartType>[_dynamicType]); | 9471 _streamDynamicType = _streamType.instantiate(<DartType>[_dynamicType]); |
| 9472 // FutureOr<T> is still fairly new, so if we're analyzing an SDK that |
| 9473 // doesn't have it yet, create an element for it. |
| 9474 _futureOrType ??= createPlaceholderFutureOr(_futureType, _objectType); |
| 9475 } |
| 9476 |
| 9477 /** |
| 9478 * Create an [InterfaceType] that can be used for `FutureOr<T>` if the SDK |
| 9479 * being analyzed does not contain its own `FutureOr<T>`. This ensures that |
| 9480 * we can analyze older SDKs. |
| 9481 */ |
| 9482 static InterfaceType createPlaceholderFutureOr( |
| 9483 InterfaceType futureType, InterfaceType objectType) { |
| 9484 var compilationUnit = |
| 9485 futureType.element.getAncestor((e) => e is CompilationUnitElement); |
| 9486 var element = ElementFactory.classElement('FutureOr', objectType, ['T']); |
| 9487 element.enclosingElement = compilationUnit; |
| 9488 return element.type; |
| 9471 } | 9489 } |
| 9472 } | 9490 } |
| 9473 | 9491 |
| 9474 /** | 9492 /** |
| 9475 * Modes in which [TypeResolverVisitor] works. | 9493 * Modes in which [TypeResolverVisitor] works. |
| 9476 */ | 9494 */ |
| 9477 enum TypeResolverMode { | 9495 enum TypeResolverMode { |
| 9478 /** | 9496 /** |
| 9479 * Resolve all names types of all nodes. | 9497 * Resolve all names types of all nodes. |
| 9480 */ | 9498 */ |
| (...skipping 1340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10821 return null; | 10839 return null; |
| 10822 } | 10840 } |
| 10823 if (identical(node.staticElement, variable)) { | 10841 if (identical(node.staticElement, variable)) { |
| 10824 if (node.inSetterContext()) { | 10842 if (node.inSetterContext()) { |
| 10825 result = true; | 10843 result = true; |
| 10826 } | 10844 } |
| 10827 } | 10845 } |
| 10828 return null; | 10846 return null; |
| 10829 } | 10847 } |
| 10830 } | 10848 } |
| OLD | NEW |