Chromium Code Reviews| 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/src/generated/ast.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:analyzer/src/generated/resolver.dart'; | 11 import 'package:analyzer/src/generated/resolver.dart'; |
| 12 import 'package:analyzer/src/generated/utilities_dart.dart'; | |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * An object used to find static variables whose types should be inferred and | 15 * An object used to find static variables whose types should be inferred and |
| 15 * classes whose members should have types inferred. Clients are expected to | 16 * classes whose members should have types inferred. Clients are expected to |
| 16 * visit a [CompilationUnit]. | 17 * visit a [CompilationUnit]. |
| 17 */ | 18 */ |
| 18 class InferrenceFinder extends SimpleAstVisitor { | 19 class InferrenceFinder extends SimpleAstVisitor { |
| 19 /** | 20 /** |
| 20 * The static variables that should have types inferred for them. | 21 * The static variables that should have types inferred for them. |
| 21 */ | 22 */ |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 try { | 158 try { |
| 158 _inferClass(classElement); | 159 _inferClass(classElement); |
| 159 } on _CycleException { | 160 } on _CycleException { |
| 160 // This is a short circuit return to prevent types that inherit from | 161 // This is a short circuit return to prevent types that inherit from |
| 161 // types containing a circular reference from being inferred. | 162 // types containing a circular reference from being inferred. |
| 162 } | 163 } |
| 163 }); | 164 }); |
| 164 } | 165 } |
| 165 | 166 |
| 166 /** | 167 /** |
| 168 * Compute the best type for the [parameter] at the given [index] that must be | |
| 169 * compatible with the types of the corresponding parameters of the given | |
| 170 * [overriddenMethods]. | |
| 171 * | |
| 172 * At the moment, this method will only return a type other than 'dynamic' if | |
| 173 * the types of all of the parameters are the same. In the future we might | |
| 174 * want to be smarter about it, such as by returning the least upper bound of | |
| 175 * the parameter types. | |
| 176 */ | |
| 177 DartType _computeParameterType(ParameterElement parameter, int index, | |
| 178 List<ExecutableElement> overriddenMethods) { | |
| 179 DartType parameterType = null; | |
| 180 int length = overriddenMethods.length; | |
| 181 for (int i = 0; i < length; i++) { | |
| 182 DartType type = _getTypeOfCorrespondingParameter( | |
| 183 parameter, index, overriddenMethods[i]); | |
| 184 if (parameterType == null) { | |
| 185 parameterType = type; | |
| 186 } else if (parameterType != type) { | |
| 187 return typeProvider.dynamicType; | |
| 188 } | |
| 189 } | |
| 190 return parameterType == null ? typeProvider.dynamicType : parameterType; | |
| 191 } | |
| 192 | |
| 193 /** | |
| 167 * Compute the best return type for a method that must be compatible with the | 194 * Compute the best return type for a method that must be compatible with the |
| 168 * return types of each of the given [overriddenMethods]. | 195 * return types of each of the given [overriddenMethods]. |
| 169 * | 196 * |
| 170 * At the moment, this method will only return a type other than 'dynamic' if | 197 * At the moment, this method will only return a type other than 'dynamic' if |
| 171 * the return types of all of the methods are the same. In the future we might | 198 * the return types of all of the methods are the same. In the future we might |
| 172 * want to be smarter about it. | 199 * want to be smarter about it. |
| 173 */ | 200 */ |
| 174 DartType _computeReturnType(List<ExecutableElement> overriddenMethods) { | 201 DartType _computeReturnType(List<ExecutableElement> overriddenMethods) { |
| 175 DartType returnType = null; | 202 DartType returnType = null; |
| 176 int length = overriddenMethods.length; | 203 int length = overriddenMethods.length; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 202 | 229 |
| 203 DartType _getReturnType(ExecutableElement element) { | 230 DartType _getReturnType(ExecutableElement element) { |
| 204 DartType returnType = element.returnType; | 231 DartType returnType = element.returnType; |
| 205 if (returnType == null) { | 232 if (returnType == null) { |
| 206 return typeProvider.dynamicType; | 233 return typeProvider.dynamicType; |
| 207 } | 234 } |
| 208 return returnType; | 235 return returnType; |
| 209 } | 236 } |
| 210 | 237 |
| 211 /** | 238 /** |
| 239 * Given a [method], return the type of the parameter in the method that | |
| 240 * corresponds to the given [parameter]. If the parameter is positional, then | |
| 241 * it appears at the given [index] in it's enclosing element's list of | |
|
Leaf
2015/08/26 17:11:09
it's -> its.
Brian Wilkerson
2015/08/26 17:36:15
Done
| |
| 242 * parameters. | |
| 243 */ | |
| 244 DartType _getTypeOfCorrespondingParameter( | |
| 245 ParameterElement parameter, int index, ExecutableElement method) { | |
| 246 // | |
| 247 // Find the corresponding parameter. | |
| 248 // | |
| 249 List<ParameterElement> methodParameters = method.parameters; | |
| 250 ParameterElement matchingParameter = null; | |
| 251 if (parameter.parameterKind == ParameterKind.NAMED) { | |
| 252 // | |
| 253 // If we're looking for a named parameter, only a named parameter with | |
| 254 // the same name will be matched. | |
| 255 // | |
| 256 matchingParameter = methodParameters.lastWhere( | |
| 257 (ParameterElement methodParameter) => | |
| 258 methodParameter.parameterKind == ParameterKind.NAMED && | |
| 259 methodParameter.name == parameter.name, | |
| 260 orElse: () => null); | |
| 261 } else { | |
| 262 // | |
| 263 // If we're looking for a positional parameter we ignore the difference | |
| 264 // between required and optional parameters. | |
| 265 // | |
| 266 if (index < methodParameters.length) { | |
| 267 matchingParameter = methodParameters[index]; | |
| 268 if (matchingParameter.parameterKind == ParameterKind.NAMED) { | |
| 269 matchingParameter = null; | |
| 270 } | |
| 271 } | |
| 272 } | |
| 273 // | |
| 274 // Then return the type of the parameter. | |
| 275 // | |
| 276 return matchingParameter == null | |
| 277 ? typeProvider.dynamicType | |
| 278 : matchingParameter.type; | |
| 279 } | |
| 280 | |
| 281 /** | |
| 212 * If the given [accessorElement] represents a non-synthetic instance getter | 282 * If the given [accessorElement] represents a non-synthetic instance getter |
| 213 * for which no return type was provided, infer the return type of the getter. | 283 * for which no return type was provided, infer the return type of the getter. |
| 214 */ | 284 */ |
| 215 void _inferAccessor(PropertyAccessorElement accessorElement) { | 285 void _inferAccessor(PropertyAccessorElement accessorElement) { |
| 216 if (!accessorElement.isSynthetic && | 286 if (!accessorElement.isSynthetic && |
| 217 accessorElement.isGetter && | 287 accessorElement.isGetter && |
| 218 !accessorElement.isStatic && | 288 !accessorElement.isStatic && |
| 219 accessorElement.hasImplicitReturnType && | 289 accessorElement.hasImplicitReturnType && |
| 220 _getReturnType(accessorElement).isDynamic) { | 290 _getReturnType(accessorElement).isDynamic) { |
| 221 List<ExecutableElement> overriddenGetters = inheritanceManager | 291 List<ExecutableElement> overriddenGetters = inheritanceManager |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 _setParameterType(fieldElement.setter, newType); | 387 _setParameterType(fieldElement.setter, newType); |
| 318 } | 388 } |
| 319 } | 389 } |
| 320 } | 390 } |
| 321 | 391 |
| 322 /** | 392 /** |
| 323 * If the given [methodElement] represents a non-synthetic instance method | 393 * If the given [methodElement] represents a non-synthetic instance method |
| 324 * for which no return type was provided, infer the return type of the method. | 394 * for which no return type was provided, infer the return type of the method. |
| 325 */ | 395 */ |
| 326 void _inferMethod(MethodElement methodElement) { | 396 void _inferMethod(MethodElement methodElement) { |
| 327 if (!methodElement.isSynthetic && | 397 if (methodElement.isSynthetic || methodElement.isStatic) { |
| 328 !methodElement.isStatic && | 398 return; |
| 329 methodElement.hasImplicitReturnType && | 399 } |
| 400 List<ExecutableElement> overriddenMethods = null; | |
| 401 // | |
| 402 // Infer the return type. | |
| 403 // | |
| 404 if (methodElement.hasImplicitReturnType && | |
| 330 _getReturnType(methodElement).isDynamic) { | 405 _getReturnType(methodElement).isDynamic) { |
| 331 List<ExecutableElement> overriddenMethods = inheritanceManager | 406 overriddenMethods = inheritanceManager.lookupOverrides( |
| 332 .lookupOverrides(methodElement.enclosingElement, methodElement.name); | 407 methodElement.enclosingElement, methodElement.name); |
| 333 if (overriddenMethods.isNotEmpty && _onlyMethods(overriddenMethods)) { | 408 if (overriddenMethods.isEmpty || !_onlyMethods(overriddenMethods)) { |
| 334 MethodElementImpl element = methodElement as MethodElementImpl; | 409 return; |
| 335 _setReturnType(element, _computeReturnType(overriddenMethods)); | 410 } |
| 411 MethodElementImpl element = methodElement as MethodElementImpl; | |
| 412 _setReturnType(element, _computeReturnType(overriddenMethods)); | |
| 413 } | |
| 414 // | |
| 415 // Infer the parameter types. | |
| 416 // | |
| 417 List<ParameterElement> parameters = methodElement.parameters; | |
| 418 var length = parameters.length; | |
| 419 for (int i = 0; i < length; ++i) { | |
| 420 ParameterElement parameter = parameters[i]; | |
| 421 if (parameter.hasImplicitType && parameter.type.isDynamic) { | |
| 422 overriddenMethods = overriddenMethods ?? | |
|
vicb
2015/08/29 06:10:42
If the analyzer is supposed to work with SDK v1.11
Brian Wilkerson
2015/08/29 16:15:05
If only we had a tool that could tell us when we m
| |
| 423 inheritanceManager.lookupOverrides( | |
| 424 methodElement.enclosingElement, methodElement.name); | |
| 425 if (overriddenMethods.isEmpty || !_onlyMethods(overriddenMethods)) { | |
| 426 return; | |
| 427 } | |
| 428 DartType type = _computeParameterType(parameter, i, overriddenMethods); | |
| 429 if (!type.isDynamic) { | |
| 430 if (parameter is ParameterElementImpl) { | |
| 431 parameter.type = type; | |
| 432 } | |
| 433 } | |
| 336 } | 434 } |
| 337 } | 435 } |
| 338 } | 436 } |
| 339 | 437 |
| 340 /** | 438 /** |
| 341 * Infer type information for all of the instance members in the given | 439 * Infer type information for all of the instance members in the given |
| 342 * interface [type]. | 440 * interface [type]. |
| 343 */ | 441 */ |
| 344 void _inferType(InterfaceType type) { | 442 void _inferType(InterfaceType type) { |
| 345 if (type != null) { | 443 if (type != null) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 new FunctionTypeImpl(element, functionType.prunedTypedefs); | 525 new FunctionTypeImpl(element, functionType.prunedTypedefs); |
| 428 } | 526 } |
| 429 } | 527 } |
| 430 } | 528 } |
| 431 } | 529 } |
| 432 | 530 |
| 433 /** | 531 /** |
| 434 * A class of exception that is not used anywhere else. | 532 * A class of exception that is not used anywhere else. |
| 435 */ | 533 */ |
| 436 class _CycleException implements Exception {} | 534 class _CycleException implements Exception {} |
| OLD | NEW |