| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.task.strong_mode; | 5 library analyzer.src.task.strong_mode; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/dart/ast/visitor.dart'; |
| 11 import 'package:analyzer/src/generated/resolver.dart'; | 11 import 'package:analyzer/dart/element/element.dart'; |
| 12 import 'package:analyzer/dart/element/type.dart'; |
| 13 import 'package:analyzer/src/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/dart/element/type.dart'; |
| 15 import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; |
| 16 import 'package:analyzer/src/generated/resolver.dart' |
| 17 show TypeProvider, InheritanceManager; |
| 18 import 'package:analyzer/src/generated/type_system.dart'; |
| 12 import 'package:analyzer/src/generated/utilities_dart.dart'; | 19 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 13 | 20 |
| 14 /** | 21 /** |
| 15 * Set the type of the sole parameter of the given [element] to the given [type]
. | 22 * Sets the type of the field. This is stored in the field itself, and the |
| 23 * synthetic getter/setter types. |
| 16 */ | 24 */ |
| 17 void setParameterType(PropertyAccessorElement element, DartType type) { | 25 void setFieldType(VariableElement field, DartType newType) { |
| 18 if (element is PropertyAccessorElementImpl) { | 26 (field as VariableElementImpl).type = newType; |
| 19 ParameterElement parameter = _getParameter(element); | 27 if (field.initializer != null) { |
| 20 if (parameter is ParameterElementImpl) { | 28 (field.initializer as ExecutableElementImpl).returnType = newType; |
| 21 // | |
| 22 // Update the type of the parameter. | |
| 23 // | |
| 24 parameter.type = type; | |
| 25 // | |
| 26 // Update the type of the setter to reflect the new parameter type. | |
| 27 // | |
| 28 FunctionType functionType = element.type; | |
| 29 if (functionType is FunctionTypeImpl) { | |
| 30 element.type = | |
| 31 new FunctionTypeImpl(element, functionType.prunedTypedefs) | |
| 32 ..typeArguments = functionType.typeArguments; | |
| 33 } else { | |
| 34 assert(false); | |
| 35 } | |
| 36 } else { | |
| 37 assert(false); | |
| 38 } | |
| 39 } else { | |
| 40 throw new StateError('element is an instance of ${element.runtimeType}'); | |
| 41 assert(false); | |
| 42 } | 29 } |
| 43 } | 30 } |
| 44 | 31 |
| 45 /** | |
| 46 * Set the return type of the given [element] to the given [type]. | |
| 47 */ | |
| 48 void setReturnType(ExecutableElement element, DartType type) { | |
| 49 if (element is ExecutableElementImpl) { | |
| 50 // | |
| 51 // Update the return type of the element, which is stored in two places: | |
| 52 // directly in the element and indirectly in the type of the element. | |
| 53 // | |
| 54 element.returnType = type; | |
| 55 FunctionType functionType = element.type; | |
| 56 if (functionType is FunctionTypeImpl) { | |
| 57 element.type = new FunctionTypeImpl(element, functionType.prunedTypedefs) | |
| 58 ..typeArguments = functionType.typeArguments; | |
| 59 } else { | |
| 60 assert(false); | |
| 61 } | |
| 62 } else { | |
| 63 assert(false); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 /** | 32 /** |
| 68 * Return the element for the single parameter of the given [setter], or `null` | 33 * Return the element for the single parameter of the given [setter], or `null` |
| 69 * if the executable element is not a setter or does not have a single | 34 * if the executable element is not a setter or does not have a single |
| 70 * parameter. | 35 * parameter. |
| 71 */ | 36 */ |
| 72 ParameterElement _getParameter(ExecutableElement setter) { | 37 ParameterElement _getParameter(ExecutableElement setter) { |
| 73 if (setter is PropertyAccessorElement && setter.isSetter) { | 38 if (setter is PropertyAccessorElement && setter.isSetter) { |
| 74 List<ParameterElement> parameters = setter.parameters; | 39 List<ParameterElement> parameters = setter.parameters; |
| 75 if (parameters.length == 1) { | 40 if (parameters.length == 1) { |
| 76 return parameters[0]; | 41 return parameters[0]; |
| 77 } | 42 } |
| 78 } | 43 } |
| 79 return null; | 44 return null; |
| 80 } | 45 } |
| 81 | 46 |
| 82 /** | 47 /** |
| 83 * A function that returns `true` if the given [variable] passes the filter. | 48 * A function that returns `true` if the given [element] passes the filter. |
| 84 */ | 49 */ |
| 85 typedef bool VariableFilter(VariableElement element); | 50 typedef bool VariableFilter(VariableElement element); |
| 86 | 51 |
| 87 /** | 52 /** |
| 88 * An object used to infer the type of instance fields and the return types of | 53 * An object used to infer the type of instance fields and the return types of |
| 89 * instance methods within a single compilation unit. | 54 * instance methods within a single compilation unit. |
| 90 */ | 55 */ |
| 91 class InstanceMemberInferrer { | 56 class InstanceMemberInferrer { |
| 92 /** | 57 /** |
| 93 * The type provider used to look up types. | 58 * The type provider used to look up types. |
| 94 */ | 59 */ |
| 95 final TypeProvider typeProvider; | 60 final TypeProvider typeProvider; |
| 96 | 61 |
| 97 /** | 62 /** |
| 98 * The type system used to compute the least upper bound of types. | 63 * The type system used to compute the least upper bound of types. |
| 99 */ | 64 */ |
| 100 TypeSystem typeSystem; | 65 TypeSystem typeSystem; |
| 101 | 66 |
| 102 /** | 67 /** |
| 103 * The inheritance manager used to find overridden method. | 68 * The inheritance manager used to find overridden method. |
| 104 */ | 69 */ |
| 105 InheritanceManager inheritanceManager; | 70 final InheritanceManager inheritanceManager; |
| 106 | 71 |
| 107 /** | 72 /** |
| 108 * The classes that have been visited while attempting to infer the types of | 73 * The classes that have been visited while attempting to infer the types of |
| 109 * instance members of some base class. | 74 * instance members of some base class. |
| 110 */ | 75 */ |
| 111 HashSet<ClassElementImpl> elementsBeingInferred = | 76 HashSet<ClassElementImpl> elementsBeingInferred = |
| 112 new HashSet<ClassElementImpl>(); | 77 new HashSet<ClassElementImpl>(); |
| 113 | 78 |
| 114 /** | 79 /** |
| 115 * Initialize a newly create inferrer. | 80 * Initialize a newly create inferrer. |
| 116 */ | 81 */ |
| 117 InstanceMemberInferrer(this.typeProvider, {TypeSystem typeSystem}) | 82 InstanceMemberInferrer(this.typeProvider, this.inheritanceManager, |
| 83 {TypeSystem typeSystem}) |
| 118 : typeSystem = (typeSystem != null) ? typeSystem : new TypeSystemImpl(); | 84 : typeSystem = (typeSystem != null) ? typeSystem : new TypeSystemImpl(); |
| 119 | 85 |
| 120 /** | 86 /** |
| 121 * Infer type information for all of the instance members in the given | 87 * Infer type information for all of the instance members in the given |
| 122 * compilation [unit]. | 88 * compilation [unit]. |
| 123 */ | 89 */ |
| 124 void inferCompilationUnit(CompilationUnitElement unit) { | 90 void inferCompilationUnit(CompilationUnitElement unit) { |
| 125 inheritanceManager = new InheritanceManager(unit.library); | |
| 126 unit.types.forEach((ClassElement classElement) { | 91 unit.types.forEach((ClassElement classElement) { |
| 127 try { | 92 try { |
| 128 _inferClass(classElement); | 93 _inferClass(classElement); |
| 129 } on _CycleException { | 94 } on _CycleException { |
| 130 // This is a short circuit return to prevent types that inherit from | 95 // This is a short circuit return to prevent types that inherit from |
| 131 // types containing a circular reference from being inferred. | 96 // types containing a circular reference from being inferred. |
| 132 } | 97 } |
| 133 }); | 98 }); |
| 134 } | 99 } |
| 135 | 100 |
| 136 /** | 101 /** |
| 102 * Return `true` if the list of [elements] contains only methods. |
| 103 */ |
| 104 bool _allSameElementKind( |
| 105 ExecutableElement element, List<ExecutableElement> elements) { |
| 106 return elements.every((e) => e.kind == element.kind); |
| 107 } |
| 108 |
| 109 /** |
| 137 * Compute the best type for the [parameter] at the given [index] that must be | 110 * Compute the best type for the [parameter] at the given [index] that must be |
| 138 * compatible with the types of the corresponding parameters of the given | 111 * compatible with the types of the corresponding parameters of the given |
| 139 * [overriddenMethods]. | 112 * [overriddenMethods]. |
| 140 * | 113 * |
| 141 * At the moment, this method will only return a type other than 'dynamic' if | 114 * At the moment, this method will only return a type other than 'dynamic' if |
| 142 * the types of all of the parameters are the same. In the future we might | 115 * the types of all of the parameters are the same. In the future we might |
| 143 * want to be smarter about it, such as by returning the least upper bound of | 116 * want to be smarter about it, such as by returning the least upper bound of |
| 144 * the parameter types. | 117 * the parameter types. |
| 145 */ | 118 */ |
| 146 DartType _computeParameterType(ParameterElement parameter, int index, | 119 DartType _computeParameterType(ParameterElement parameter, int index, |
| 147 List<ExecutableElement> overriddenMethods) { | 120 List<FunctionType> overriddenTypes) { |
| 148 DartType parameterType = null; | 121 DartType parameterType = null; |
| 149 int length = overriddenMethods.length; | 122 int length = overriddenTypes.length; |
| 150 for (int i = 0; i < length; i++) { | 123 for (int i = 0; i < length; i++) { |
| 151 DartType type = _getTypeOfCorrespondingParameter( | 124 ParameterElement matchingParam = _getCorrespondingParameter( |
| 152 parameter, index, overriddenMethods[i]); | 125 parameter, index, overriddenTypes[i].parameters); |
| 126 var type = matchingParam?.type ?? typeProvider.dynamicType; |
| 153 if (parameterType == null) { | 127 if (parameterType == null) { |
| 154 parameterType = type; | 128 parameterType = type; |
| 155 } else if (parameterType != type) { | 129 } else if (parameterType != type) { |
| 156 return typeProvider.dynamicType; | 130 return typeProvider.dynamicType; |
| 157 } | 131 } |
| 158 } | 132 } |
| 159 return parameterType == null ? typeProvider.dynamicType : parameterType; | 133 return parameterType ?? typeProvider.dynamicType; |
| 160 } | 134 } |
| 161 | 135 |
| 162 /** | 136 /** |
| 163 * Compute the best return type for a method that must be compatible with the | 137 * Compute the best return type for a method that must be compatible with the |
| 164 * return types of each of the given [overriddenMethods]. | 138 * return types of each of the given [overriddenReturnTypes]. |
| 165 * | 139 * |
| 166 * At the moment, this method will only return a type other than 'dynamic' if | 140 * At the moment, this method will only return a type other than 'dynamic' if |
| 167 * the return types of all of the methods are the same. In the future we might | 141 * the return types of all of the methods are the same. In the future we might |
| 168 * want to be smarter about it. | 142 * want to be smarter about it. |
| 169 */ | 143 */ |
| 170 DartType _computeReturnType(List<ExecutableElement> overriddenMethods) { | 144 DartType _computeReturnType(Iterable<DartType> overriddenReturnTypes) { |
| 171 DartType returnType = null; | 145 DartType returnType = null; |
| 172 int length = overriddenMethods.length; | 146 for (DartType type in overriddenReturnTypes) { |
| 173 for (int i = 0; i < length; i++) { | 147 if (type == null) { |
| 174 DartType type = _getReturnType(overriddenMethods[i]); | 148 type = typeProvider.dynamicType; |
| 149 } |
| 175 if (returnType == null) { | 150 if (returnType == null) { |
| 176 returnType = type; | 151 returnType = type; |
| 177 } else if (returnType != type) { | 152 } else if (returnType != type) { |
| 178 return typeProvider.dynamicType; | 153 return typeProvider.dynamicType; |
| 179 } | 154 } |
| 180 } | 155 } |
| 181 return returnType == null ? typeProvider.dynamicType : returnType; | 156 return returnType ?? typeProvider.dynamicType; |
| 182 } | |
| 183 | |
| 184 DartType _getReturnType(ExecutableElement element) { | |
| 185 DartType returnType = element.returnType; | |
| 186 if (returnType == null) { | |
| 187 return typeProvider.dynamicType; | |
| 188 } | |
| 189 return returnType; | |
| 190 } | 157 } |
| 191 | 158 |
| 192 /** | 159 /** |
| 193 * Given a [method], return the type of the parameter in the method that | 160 * Given a method, return the parameter in the method that corresponds to the |
| 194 * corresponds to the given [parameter]. If the parameter is positional, then | 161 * given [parameter]. If the parameter is positional, then |
| 195 * it appears at the given [index] in its enclosing element's list of | 162 * it appears at the given [index] in its enclosing element's list of |
| 196 * parameters. | 163 * parameters. |
| 197 */ | 164 */ |
| 198 DartType _getTypeOfCorrespondingParameter( | 165 ParameterElement _getCorrespondingParameter(ParameterElement parameter, |
| 199 ParameterElement parameter, int index, ExecutableElement method) { | 166 int index, List<ParameterElement> methodParameters) { |
| 200 // | 167 // |
| 201 // Find the corresponding parameter. | 168 // Find the corresponding parameter. |
| 202 // | 169 // |
| 203 List<ParameterElement> methodParameters = method.parameters; | |
| 204 ParameterElement matchingParameter = null; | |
| 205 if (parameter.parameterKind == ParameterKind.NAMED) { | 170 if (parameter.parameterKind == ParameterKind.NAMED) { |
| 206 // | 171 // |
| 207 // If we're looking for a named parameter, only a named parameter with | 172 // If we're looking for a named parameter, only a named parameter with |
| 208 // the same name will be matched. | 173 // the same name will be matched. |
| 209 // | 174 // |
| 210 matchingParameter = methodParameters.lastWhere( | 175 return methodParameters.lastWhere( |
| 211 (ParameterElement methodParameter) => | 176 (ParameterElement methodParameter) => |
| 212 methodParameter.parameterKind == ParameterKind.NAMED && | 177 methodParameter.parameterKind == ParameterKind.NAMED && |
| 213 methodParameter.name == parameter.name, | 178 methodParameter.name == parameter.name, |
| 214 orElse: () => null); | 179 orElse: () => null); |
| 215 } else { | 180 } |
| 216 // | 181 // |
| 217 // If we're looking for a positional parameter we ignore the difference | 182 // If we're looking for a positional parameter we ignore the difference |
| 218 // between required and optional parameters. | 183 // between required and optional parameters. |
| 219 // | 184 // |
| 220 if (index < methodParameters.length) { | 185 if (index < methodParameters.length) { |
| 221 matchingParameter = methodParameters[index]; | 186 var matchingParameter = methodParameters[index]; |
| 222 if (matchingParameter.parameterKind == ParameterKind.NAMED) { | 187 if (matchingParameter.parameterKind != ParameterKind.NAMED) { |
| 223 matchingParameter = null; | 188 return matchingParameter; |
| 224 } | |
| 225 } | 189 } |
| 226 } | 190 } |
| 227 // | 191 return null; |
| 228 // Then return the type of the parameter. | |
| 229 // | |
| 230 return matchingParameter == null | |
| 231 ? typeProvider.dynamicType | |
| 232 : matchingParameter.type; | |
| 233 } | 192 } |
| 234 | 193 |
| 235 /** | 194 /** |
| 236 * Infer type information for all of the instance members in the given | 195 * Infer type information for all of the instance members in the given |
| 237 * [classElement]. | 196 * [classElement]. |
| 238 */ | 197 */ |
| 239 void _inferClass(ClassElement classElement) { | 198 void _inferClass(ClassElement classElement) { |
| 240 if (classElement is ClassElementImpl) { | 199 if (classElement is ClassElementImpl) { |
| 241 if (classElement.hasBeenInferred) { | 200 if (classElement.hasBeenInferred) { |
| 242 return; | 201 return; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 268 // field types are inferred. | 227 // field types are inferred. |
| 269 // | 228 // |
| 270 classElement.constructors.forEach(_inferConstructorFieldFormals); | 229 classElement.constructors.forEach(_inferConstructorFieldFormals); |
| 271 classElement.hasBeenInferred = true; | 230 classElement.hasBeenInferred = true; |
| 272 } finally { | 231 } finally { |
| 273 elementsBeingInferred.remove(classElement); | 232 elementsBeingInferred.remove(classElement); |
| 274 } | 233 } |
| 275 } | 234 } |
| 276 } | 235 } |
| 277 | 236 |
| 237 void _inferConstructorFieldFormals(ConstructorElement element) { |
| 238 for (ParameterElement p in element.parameters) { |
| 239 if (p is FieldFormalParameterElement) { |
| 240 _inferFieldFormalParameter(p); |
| 241 } |
| 242 } |
| 243 } |
| 244 |
| 245 /** |
| 246 * If the given [element] represents a non-synthetic instance method, |
| 247 * getter or setter, infer the return type and any parameter type(s) where |
| 248 * they were not provided. |
| 249 */ |
| 250 void _inferExecutable(ExecutableElement element) { |
| 251 if (element.isSynthetic || element.isStatic) { |
| 252 return; |
| 253 } |
| 254 List<ExecutableElement> overriddenMethods = inheritanceManager |
| 255 .lookupOverrides(element.enclosingElement, element.name); |
| 256 if (overriddenMethods.isEmpty || |
| 257 !_allSameElementKind(element, overriddenMethods)) { |
| 258 return; |
| 259 } |
| 260 |
| 261 // |
| 262 // Overridden methods must have the same number of generic type parameters |
| 263 // as this method, or none. |
| 264 // |
| 265 // If we do have generic type parameters on the element we're inferring, |
| 266 // we must express its parameter and return types in terms of its own |
| 267 // parameters. For example, given `m<T>(t)` overriding `m<S>(S s)` we |
| 268 // should infer this as `m<T>(T t)`. |
| 269 // |
| 270 List<DartType> typeFormals = |
| 271 TypeParameterTypeImpl.getTypes(element.type.typeFormals); |
| 272 |
| 273 List<FunctionType> overriddenTypes = new List<FunctionType>(); |
| 274 for (ExecutableElement overriddenMethod in overriddenMethods) { |
| 275 FunctionType overriddenType = overriddenMethod.type; |
| 276 if (overriddenType == null) { |
| 277 // TODO(brianwilkerson) I think the overridden method should always have |
| 278 // a type, but there appears to be a bug that causes it to sometimes be |
| 279 // null, we guard against that case by not performing inference. |
| 280 return; |
| 281 } |
| 282 if (overriddenType.typeFormals.isNotEmpty) { |
| 283 if (overriddenType.typeFormals.length != typeFormals.length) { |
| 284 return; |
| 285 } |
| 286 overriddenType = overriddenType.instantiate(typeFormals); |
| 287 } |
| 288 overriddenTypes.add(overriddenType); |
| 289 } |
| 290 |
| 291 // |
| 292 // Infer the return type. |
| 293 // |
| 294 if (element.hasImplicitReturnType) { |
| 295 (element as ExecutableElementImpl).returnType = |
| 296 _computeReturnType(overriddenTypes.map((t) => t.returnType)); |
| 297 if (element is PropertyAccessorElement) { |
| 298 _updateSyntheticVariableType(element); |
| 299 } |
| 300 } |
| 301 // |
| 302 // Infer the parameter types. |
| 303 // |
| 304 List<ParameterElement> parameters = element.parameters; |
| 305 int length = parameters.length; |
| 306 for (int i = 0; i < length; ++i) { |
| 307 ParameterElement parameter = parameters[i]; |
| 308 if (parameter is ParameterElementImpl) { |
| 309 _inferParameterCovariance(parameter, i, overriddenTypes); |
| 310 |
| 311 if (parameter.hasImplicitType) { |
| 312 parameter.type = _computeParameterType(parameter, i, overriddenTypes); |
| 313 if (element is PropertyAccessorElement) { |
| 314 _updateSyntheticVariableType(element); |
| 315 } |
| 316 } |
| 317 } |
| 318 } |
| 319 } |
| 320 |
| 278 /** | 321 /** |
| 279 * If the given [fieldElement] represents a non-synthetic instance field for | 322 * If the given [fieldElement] represents a non-synthetic instance field for |
| 280 * which no type was provided, infer the type of the field. | 323 * which no type was provided, infer the type of the field. |
| 281 */ | 324 */ |
| 282 void _inferField(FieldElement fieldElement) { | 325 void _inferField(FieldElement fieldElement) { |
| 283 if (!fieldElement.isSynthetic && | 326 if (fieldElement.isSynthetic || fieldElement.isStatic) { |
| 284 !fieldElement.isStatic && | 327 return; |
| 285 fieldElement.hasImplicitType) { | 328 } |
| 329 List<ExecutableElement> overriddenSetters = |
| 330 inheritanceManager.lookupOverrides( |
| 331 fieldElement.enclosingElement, fieldElement.name + '='); |
| 332 var setter = fieldElement.setter; |
| 333 if (setter != null && overriddenSetters.isNotEmpty) { |
| 334 _inferParameterCovariance( |
| 335 setter.parameters[0], 0, overriddenSetters.map((s) => s.type)); |
| 336 } |
| 337 |
| 338 if (fieldElement.hasImplicitType) { |
| 286 // | 339 // |
| 287 // First look for overridden getters with the same name as the field. | 340 // First look for overridden getters with the same name as the field. |
| 288 // | 341 // |
| 289 List<ExecutableElement> overriddenGetters = inheritanceManager | 342 List<ExecutableElement> overriddenGetters = inheritanceManager |
| 290 .lookupOverrides(fieldElement.enclosingElement, fieldElement.name); | 343 .lookupOverrides(fieldElement.enclosingElement, fieldElement.name); |
| 291 DartType newType = null; | 344 DartType newType = null; |
| 292 if (overriddenGetters.isNotEmpty && _onlyGetters(overriddenGetters)) { | 345 if (overriddenGetters.isNotEmpty && _onlyGetters(overriddenGetters)) { |
| 293 newType = _computeReturnType(overriddenGetters); | 346 newType = |
| 294 List<ExecutableElement> overriddenSetters = inheritanceManager | 347 _computeReturnType(overriddenGetters.map((e) => e.returnType)); |
| 295 .lookupOverrides( | 348 |
| 296 fieldElement.enclosingElement, fieldElement.name + '='); | |
| 297 if (!_isCompatible(newType, overriddenSetters)) { | 349 if (!_isCompatible(newType, overriddenSetters)) { |
| 298 newType = null; | 350 newType = null; |
| 299 } | 351 } |
| 300 } | 352 } |
| 301 // | 353 // |
| 302 // If there is no overridden getter or if the overridden getter's type is | 354 // If there is no overridden getter or if the overridden getter's type is |
| 303 // dynamic, then we can infer the type from the initialization expression | 355 // dynamic, then we can infer the type from the initialization expression |
| 304 // without breaking subtype rules. We could potentially infer a consistent | 356 // without breaking subtype rules. We could potentially infer a consistent |
| 305 // return type even if the overridden getter's type was not dynamic, but | 357 // return type even if the overridden getter's type was not dynamic, but |
| 306 // choose not to for simplicity. The field is required to be final to | 358 // choose not to for simplicity. The field is required to be final to |
| 307 // prevent choosing a type that is inconsistent with assignments we cannot | 359 // prevent choosing a type that is inconsistent with assignments we cannot |
| 308 // analyze. | 360 // analyze. |
| 309 // | 361 // |
| 310 if (newType == null || newType.isDynamic) { | 362 if (newType == null || newType.isDynamic) { |
| 311 if (fieldElement.initializer != null && | 363 if (fieldElement.initializer != null && |
| 312 (fieldElement.isFinal || overriddenGetters.isEmpty)) { | 364 (fieldElement.isFinal || overriddenGetters.isEmpty)) { |
| 313 newType = fieldElement.initializer.returnType; | 365 newType = fieldElement.initializer.returnType; |
| 314 } | 366 } |
| 315 } | 367 } |
| 316 if (newType == null || newType.isBottom) { | 368 if (newType == null || newType.isBottom) { |
| 317 newType = typeProvider.dynamicType; | 369 newType = typeProvider.dynamicType; |
| 318 } | 370 } |
| 319 (fieldElement as FieldElementImpl).type = newType; | 371 setFieldType(fieldElement, newType); |
| 320 setReturnType(fieldElement.getter, newType); | 372 } |
| 321 if (!fieldElement.isFinal && !fieldElement.isConst) { | 373 } |
| 322 setParameterType(fieldElement.setter, newType); | 374 |
| 323 } | 375 void _inferFieldFormalParameter(FieldFormalParameterElement element) { |
| 376 FieldElement field = element.field; |
| 377 if (field != null && element.hasImplicitType) { |
| 378 (element as FieldFormalParameterElementImpl).type = field.type; |
| 324 } | 379 } |
| 325 } | 380 } |
| 326 | 381 |
| 327 /** | 382 /** |
| 328 * If the given [element] represents a non-synthetic instance method, | 383 * If a parameter is covariant, any parameters that override it are too. |
| 329 * getter or setter, infer the return type and any parameter type(s) where | |
| 330 * they were not provided. | |
| 331 */ | 384 */ |
| 332 void _inferExecutable(ExecutableElement element) { | 385 void _inferParameterCovariance(ParameterElementImpl parameter, int index, |
| 333 if (element.isSynthetic || element.isStatic) { | 386 Iterable<FunctionType> overriddenTypes) { |
| 334 return; | 387 parameter.inheritsCovariant = overriddenTypes.any((f) { |
| 335 } | 388 var param = _getCorrespondingParameter(parameter, index, f.parameters); |
| 336 List<ExecutableElement> overriddenMethods = null; | 389 return param != null && param.isCovariant; |
| 337 // | 390 }); |
| 338 // Infer the return type. | |
| 339 // | |
| 340 if (element.hasImplicitReturnType) { | |
| 341 overriddenMethods = inheritanceManager.lookupOverrides( | |
| 342 element.enclosingElement, element.name); | |
| 343 if (overriddenMethods.isEmpty || | |
| 344 !_allSameElementKind(element, overriddenMethods)) { | |
| 345 return; | |
| 346 } | |
| 347 setReturnType(element, _computeReturnType(overriddenMethods)); | |
| 348 if (element is PropertyAccessorElement) { | |
| 349 _updateSyntheticVariableType(element); | |
| 350 } | |
| 351 } | |
| 352 // | |
| 353 // Infer the parameter types. | |
| 354 // | |
| 355 List<ParameterElement> parameters = element.parameters; | |
| 356 int length = parameters.length; | |
| 357 for (int i = 0; i < length; ++i) { | |
| 358 ParameterElement parameter = parameters[i]; | |
| 359 if (parameter is ParameterElementImpl && parameter.hasImplicitType) { | |
| 360 if (overriddenMethods == null) { | |
| 361 overriddenMethods = inheritanceManager.lookupOverrides( | |
| 362 element.enclosingElement, element.name); | |
| 363 } | |
| 364 if (overriddenMethods.isEmpty || | |
| 365 !_allSameElementKind(element, overriddenMethods)) { | |
| 366 return; | |
| 367 } | |
| 368 parameter.type = _computeParameterType(parameter, i, overriddenMethods); | |
| 369 if (element is PropertyAccessorElement) { | |
| 370 _updateSyntheticVariableType(element); | |
| 371 } | |
| 372 } | |
| 373 } | |
| 374 } | 391 } |
| 375 | 392 |
| 376 /** | 393 /** |
| 377 * If the given [element] is a non-synthetic getter or setter, update its | |
| 378 * synthetic variable's type to match the getter's return type, or if no | |
| 379 * corresponding getter exists, use the setter's parameter type. | |
| 380 * | |
| 381 * In general, the type of the synthetic variable should not be used, because | |
| 382 * getters and setters are independent methods. But this logic matches what | |
| 383 * `TypeResolverVisitor.visitMethodDeclaration` would fill in there. | |
| 384 */ | |
| 385 void _updateSyntheticVariableType(PropertyAccessorElement element) { | |
| 386 assert(!element.isSynthetic); | |
| 387 PropertyAccessorElement getter = element; | |
| 388 if (element.isSetter) { | |
| 389 // See if we can find any getter. | |
| 390 getter = element.correspondingGetter; | |
| 391 } | |
| 392 DartType newType; | |
| 393 if (getter != null) { | |
| 394 newType = getter.returnType; | |
| 395 } else if (element.isSetter && element.parameters.isNotEmpty) { | |
| 396 newType = element.parameters[0].type; | |
| 397 } | |
| 398 if (newType != null) { | |
| 399 (element.variable as VariableElementImpl).type = newType; | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 /** | |
| 404 * Infer type information for all of the instance members in the given | 394 * Infer type information for all of the instance members in the given |
| 405 * interface [type]. | 395 * interface [type]. |
| 406 */ | 396 */ |
| 407 void _inferType(InterfaceType type) { | 397 void _inferType(InterfaceType type) { |
| 408 if (type != null) { | 398 if (type != null) { |
| 409 ClassElement element = type.element; | 399 ClassElement element = type.element; |
| 410 if (element != null) { | 400 if (element != null) { |
| 411 _inferClass(element); | 401 _inferClass(element); |
| 412 } | 402 } |
| 413 } | 403 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 433 bool _onlyGetters(List<ExecutableElement> elements) { | 423 bool _onlyGetters(List<ExecutableElement> elements) { |
| 434 for (ExecutableElement element in elements) { | 424 for (ExecutableElement element in elements) { |
| 435 if (!(element is PropertyAccessorElement && element.isGetter)) { | 425 if (!(element is PropertyAccessorElement && element.isGetter)) { |
| 436 return false; | 426 return false; |
| 437 } | 427 } |
| 438 } | 428 } |
| 439 return true; | 429 return true; |
| 440 } | 430 } |
| 441 | 431 |
| 442 /** | 432 /** |
| 443 * Return `true` if the list of [elements] contains only methods. | 433 * If the given [element] is a non-synthetic getter or setter, update its |
| 434 * synthetic variable's type to match the getter's return type, or if no |
| 435 * corresponding getter exists, use the setter's parameter type. |
| 436 * |
| 437 * In general, the type of the synthetic variable should not be used, because |
| 438 * getters and setters are independent methods. But this logic matches what |
| 439 * `TypeResolverVisitor.visitMethodDeclaration` would fill in there. |
| 444 */ | 440 */ |
| 445 bool _allSameElementKind( | 441 void _updateSyntheticVariableType(PropertyAccessorElement element) { |
| 446 ExecutableElement element, List<ExecutableElement> elements) { | 442 assert(!element.isSynthetic); |
| 447 return elements.every((e) => e.kind == element.kind); | 443 PropertyAccessorElement getter = element; |
| 448 } | 444 if (element.isSetter) { |
| 449 | 445 // See if we can find any getter. |
| 450 void _inferConstructorFieldFormals(ConstructorElement element) { | 446 getter = element.correspondingGetter; |
| 451 for (ParameterElement p in element.parameters) { | |
| 452 if (p is FieldFormalParameterElement) { | |
| 453 _inferFieldFormalParameter(p); | |
| 454 } | |
| 455 } | 447 } |
| 456 } | 448 DartType newType; |
| 457 | 449 if (getter != null) { |
| 458 void _inferFieldFormalParameter(FieldFormalParameterElement element) { | 450 newType = getter.returnType; |
| 459 FieldElement field = element.field; | 451 } else if (element.isSetter && element.parameters.isNotEmpty) { |
| 460 if (field != null && element.hasImplicitType) { | 452 newType = element.parameters[0].type; |
| 461 (element as FieldFormalParameterElementImpl).type = field.type; | 453 } |
| 454 if (newType != null) { |
| 455 (element.variable as VariableElementImpl).type = newType; |
| 462 } | 456 } |
| 463 } | 457 } |
| 464 } | 458 } |
| 465 | 459 |
| 466 /** | 460 /** |
| 467 * A visitor that will gather all of the variables referenced within a given | 461 * A visitor that will gather all of the variables referenced within a given |
| 468 * AST structure. The collection can be restricted to contain only those | 462 * AST structure. The collection can be restricted to contain only those |
| 469 * variables that pass a specified filter. | 463 * variables that pass a specified filter. |
| 470 */ | 464 */ |
| 471 class VariableGatherer extends RecursiveAstVisitor { | 465 class VariableGatherer extends RecursiveAstVisitor { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 482 | 476 |
| 483 /** | 477 /** |
| 484 * Initialize a newly created gatherer to gather all of the variables that | 478 * Initialize a newly created gatherer to gather all of the variables that |
| 485 * pass the given [filter] (or all variables if no filter is provided). | 479 * pass the given [filter] (or all variables if no filter is provided). |
| 486 */ | 480 */ |
| 487 VariableGatherer([this.filter = null]); | 481 VariableGatherer([this.filter = null]); |
| 488 | 482 |
| 489 @override | 483 @override |
| 490 void visitSimpleIdentifier(SimpleIdentifier node) { | 484 void visitSimpleIdentifier(SimpleIdentifier node) { |
| 491 if (!node.inDeclarationContext()) { | 485 if (!node.inDeclarationContext()) { |
| 492 Element element = node.staticElement; | 486 Element nonAccessor(Element element) { |
| 493 if (element is PropertyAccessorElement && element.isSynthetic) { | 487 if (element is PropertyAccessorElement && element.isSynthetic) { |
| 494 element = (element as PropertyAccessorElement).variable; | 488 return element.variable; |
| 489 } |
| 490 return element; |
| 495 } | 491 } |
| 492 |
| 493 Element element = nonAccessor(node.staticElement); |
| 496 if (element is VariableElement && (filter == null || filter(element))) { | 494 if (element is VariableElement && (filter == null || filter(element))) { |
| 497 results.add(element); | 495 results.add(element); |
| 498 } | 496 } |
| 499 } | 497 } |
| 500 } | 498 } |
| 501 } | 499 } |
| 502 | 500 |
| 503 /** | 501 /** |
| 504 * A class of exception that is not used anywhere else. | 502 * A class of exception that is not used anywhere else. |
| 505 */ | 503 */ |
| 506 class _CycleException implements Exception {} | 504 class _CycleException implements Exception {} |
| OLD | NEW |