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 // | |
| 180 // Return the type of the corresponding parameter in the method with the | |
| 181 // given [methodIndex]. | |
| 182 // | |
| 183 DartType getOverriddenType(int methodIndex) { | |
| 184 // | |
| 185 // Find the corresponding parameter. | |
| 186 // | |
| 187 List<ParameterElement> overriddenParameters = | |
| 188 overriddenMethods[methodIndex].parameters; | |
| 189 ParameterElement overriddenParameter = null; | |
| 190 if (parameter.parameterKind == ParameterKind.NAMED) { | |
| 191 // | |
| 192 // If we're looking for a named parameter, only a named parameter with | |
| 193 // the same name will be matched. | |
| 194 // | |
| 195 for (int i = overriddenParameters.length - 1; i >= 0; i--) { | |
|
Leaf
2015/08/25 23:06:37
I think using
overridenParameter = overridenPar
Brian Wilkerson
2015/08/26 15:25:37
Done
| |
| 196 overriddenParameter = overriddenParameters[index]; | |
|
Leaf
2015/08/25 23:06:37
I think this should be i instead of index?
Brian Wilkerson
2015/08/26 15:25:37
Subsumed by previous change.
| |
| 197 if (overriddenParameter.parameterKind == ParameterKind.NAMED && | |
| 198 overriddenParameter.name == parameter.name) { | |
| 199 break; | |
| 200 } | |
| 201 overriddenParameter = null; | |
| 202 } | |
| 203 } else { | |
| 204 // | |
| 205 // If we're looking for a positional parameter we ignore the difference | |
| 206 // between required and optional parameters. | |
| 207 // | |
| 208 if (index < overriddenParameters.length) { | |
| 209 overriddenParameter = overriddenParameters[index]; | |
| 210 if (overriddenParameter.parameterKind == ParameterKind.NAMED) { | |
| 211 overriddenParameter = null; | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 // | |
| 216 // Then get the type of the parameter. | |
| 217 // | |
| 218 if (overriddenParameter == null) { | |
| 219 return typeProvider.dynamicType; | |
| 220 } | |
| 221 DartType type = overriddenParameter.type; | |
| 222 if (type is TypeParameterType) { | |
|
Leaf
2015/08/25 23:06:37
Hmm, is this to address the possibility that the o
Brian Wilkerson
2015/08/26 15:25:37
Yes, but we do have the instantiated type, so this
| |
| 223 return typeProvider.dynamicType; | |
| 224 } | |
| 225 return type; | |
| 226 } | |
| 227 DartType parameterType = null; | |
| 228 int length = overriddenMethods.length; | |
| 229 for (int i = 0; i < length; i++) { | |
| 230 DartType type = getOverriddenType(i); | |
| 231 if (parameterType == null) { | |
| 232 parameterType = type; | |
| 233 } else if (parameterType != type) { | |
| 234 return typeProvider.dynamicType; | |
| 235 } | |
| 236 } | |
| 237 return parameterType == null ? typeProvider.dynamicType : parameterType; | |
| 238 } | |
| 239 | |
| 240 /** | |
| 167 * Compute the best return type for a method that must be compatible with the | 241 * Compute the best return type for a method that must be compatible with the |
| 168 * return types of each of the given [overriddenMethods]. | 242 * return types of each of the given [overriddenMethods]. |
| 169 * | 243 * |
| 170 * At the moment, this method will only return a type other than 'dynamic' if | 244 * 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 | 245 * the return types of all of the methods are the same. In the future we might |
| 172 * want to be smarter about it. | 246 * want to be smarter about it. |
| 173 */ | 247 */ |
| 174 DartType _computeReturnType(List<ExecutableElement> overriddenMethods) { | 248 DartType _computeReturnType(List<ExecutableElement> overriddenMethods) { |
| 175 DartType returnType = null; | 249 DartType returnType = null; |
| 176 int length = overriddenMethods.length; | 250 int length = overriddenMethods.length; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 _setParameterType(fieldElement.setter, newType); | 391 _setParameterType(fieldElement.setter, newType); |
| 318 } | 392 } |
| 319 } | 393 } |
| 320 } | 394 } |
| 321 | 395 |
| 322 /** | 396 /** |
| 323 * If the given [methodElement] represents a non-synthetic instance method | 397 * 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. | 398 * for which no return type was provided, infer the return type of the method. |
| 325 */ | 399 */ |
| 326 void _inferMethod(MethodElement methodElement) { | 400 void _inferMethod(MethodElement methodElement) { |
| 327 if (!methodElement.isSynthetic && | 401 if (methodElement.isSynthetic || methodElement.isStatic) { |
| 328 !methodElement.isStatic && | 402 return; |
| 329 methodElement.hasImplicitReturnType && | 403 } |
| 404 List<ExecutableElement> overriddenMethods = null; | |
| 405 // | |
| 406 // Infer the return type. | |
| 407 // | |
| 408 if (methodElement.hasImplicitReturnType && | |
| 330 _getReturnType(methodElement).isDynamic) { | 409 _getReturnType(methodElement).isDynamic) { |
| 331 List<ExecutableElement> overriddenMethods = inheritanceManager | 410 overriddenMethods = inheritanceManager.lookupOverrides( |
| 332 .lookupOverrides(methodElement.enclosingElement, methodElement.name); | 411 methodElement.enclosingElement, methodElement.name); |
| 333 if (overriddenMethods.isNotEmpty && _onlyMethods(overriddenMethods)) { | 412 if (overriddenMethods.isEmpty || !_onlyMethods(overriddenMethods)) { |
| 334 MethodElementImpl element = methodElement as MethodElementImpl; | 413 return; |
| 335 _setReturnType(element, _computeReturnType(overriddenMethods)); | 414 } |
| 415 MethodElementImpl element = methodElement as MethodElementImpl; | |
| 416 _setReturnType(element, _computeReturnType(overriddenMethods)); | |
| 417 } | |
| 418 // | |
| 419 // Infer the parameter types. | |
| 420 // | |
| 421 List<ParameterElement> parameters = methodElement.parameters; | |
| 422 var length = parameters.length; | |
| 423 for (int i = 0; i < length; ++i) { | |
| 424 ParameterElement parameter = parameters[i]; | |
| 425 if (parameter.hasImplicitType && parameter.type.isDynamic) { | |
| 426 overriddenMethods = overriddenMethods ?? | |
| 427 inheritanceManager.lookupOverrides( | |
| 428 methodElement.enclosingElement, methodElement.name); | |
| 429 if (overriddenMethods.isEmpty || !_onlyMethods(overriddenMethods)) { | |
| 430 return; | |
| 431 } | |
| 432 DartType type = _computeParameterType(parameter, i, overriddenMethods); | |
| 433 if (!type.isDynamic) { | |
| 434 if (parameter is ParameterElementImpl) { | |
| 435 parameter.type = type; | |
| 436 } | |
| 437 } | |
| 336 } | 438 } |
| 337 } | 439 } |
| 338 } | 440 } |
| 339 | 441 |
| 340 /** | 442 /** |
| 341 * Infer type information for all of the instance members in the given | 443 * Infer type information for all of the instance members in the given |
| 342 * interface [type]. | 444 * interface [type]. |
| 343 */ | 445 */ |
| 344 void _inferType(InterfaceType type) { | 446 void _inferType(InterfaceType type) { |
| 345 if (type != null) { | 447 if (type != null) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 new FunctionTypeImpl(element, functionType.prunedTypedefs); | 529 new FunctionTypeImpl(element, functionType.prunedTypedefs); |
| 428 } | 530 } |
| 429 } | 531 } |
| 430 } | 532 } |
| 431 } | 533 } |
| 432 | 534 |
| 433 /** | 535 /** |
| 434 * A class of exception that is not used anywhere else. | 536 * A class of exception that is not used anywhere else. |
| 435 */ | 537 */ |
| 436 class _CycleException implements Exception {} | 538 class _CycleException implements Exception {} |
| OLD | NEW |