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 related_types; | 5 library related_types; |
6 | 6 |
7 import 'package:compiler/src/commandline_options.dart'; | 7 import 'package:compiler/src/commandline_options.dart'; |
8 import 'package:compiler/src/compiler.dart'; | 8 import 'package:compiler/src/compiler.dart'; |
9 import 'package:compiler/src/core_types.dart'; | 9 import 'package:compiler/src/core_types.dart'; |
10 import 'package:compiler/src/dart_types.dart'; | 10 import 'package:compiler/src/dart_types.dart'; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 return !world.haveAnyCommonSubtypes(leftClass, rightClass); | 96 return !world.haveAnyCommonSubtypes(leftClass, rightClass); |
97 } | 97 } |
98 return false; | 98 return false; |
99 } | 99 } |
100 | 100 |
101 /// Checks that there exists a common subtype of [left] and [right] or report | 101 /// Checks that there exists a common subtype of [left] and [right] or report |
102 /// a hint otherwise. | 102 /// a hint otherwise. |
103 void checkRelated(Node node, DartType left, DartType right) { | 103 void checkRelated(Node node, DartType left, DartType right) { |
104 if (hasEmptyIntersection(left, right)) { | 104 if (hasEmptyIntersection(left, right)) { |
105 reporter.reportHintMessage( | 105 reporter.reportHintMessage( |
106 node, | 106 node, MessageKind.NO_COMMON_SUBTYPES, {'left': left, 'right': right}); |
107 MessageKind.NO_COMMON_SUBTYPES, | |
108 {'left': left, 'right': right}); | |
109 } | 107 } |
110 } | 108 } |
111 | 109 |
112 /// Check weakly typed collection methods, like `Map.containsKey`, | 110 /// Check weakly typed collection methods, like `Map.containsKey`, |
113 /// `Map.containsValue` and `Iterable.contains`. | 111 /// `Map.containsValue` and `Iterable.contains`. |
114 void checkDynamicInvoke( | 112 void checkDynamicInvoke(Node node, DartType receiverType, |
115 Node node, | 113 List<DartType> argumentTypes, Selector selector) { |
116 DartType receiverType, | |
117 List<DartType> argumentTypes, | |
118 Selector selector) { | |
119 if (selector.name == 'containsKey' && | 114 if (selector.name == 'containsKey' && |
120 selector.callStructure == CallStructure.ONE_ARG) { | 115 selector.callStructure == CallStructure.ONE_ARG) { |
121 InterfaceType mapType = findMapType(receiverType); | 116 InterfaceType mapType = findMapType(receiverType); |
122 if (mapType != null) { | 117 if (mapType != null) { |
123 DartType keyType = findMapKeyType(mapType); | 118 DartType keyType = findMapKeyType(mapType); |
124 checkRelated(node, keyType, argumentTypes.first); | 119 checkRelated(node, keyType, argumentTypes.first); |
125 } | 120 } |
126 } else if (selector.name == 'containsValue' && | 121 } else if (selector.name == 'containsValue' && |
127 selector.callStructure == CallStructure.ONE_ARG) { | 122 selector.callStructure == CallStructure.ONE_ARG) { |
128 InterfaceType mapType = findMapType(receiverType); | 123 InterfaceType mapType = findMapType(receiverType); |
129 if (mapType != null) { | 124 if (mapType != null) { |
130 DartType valueType = findMapValueType(mapType); | 125 DartType valueType = findMapValueType(mapType); |
131 checkRelated(node, valueType, argumentTypes.first); | 126 checkRelated(node, valueType, argumentTypes.first); |
132 } | 127 } |
133 } else if (selector.name == 'contains' && | 128 } else if (selector.name == 'contains' && |
134 selector.callStructure == CallStructure.ONE_ARG) { | 129 selector.callStructure == CallStructure.ONE_ARG) { |
135 InterfaceType iterableType = findIterableType(receiverType); | 130 InterfaceType iterableType = findIterableType(receiverType); |
136 if (iterableType != null) { | 131 if (iterableType != null) { |
137 DartType elementType = findIterableElementType(iterableType); | 132 DartType elementType = findIterableElementType(iterableType); |
138 checkRelated(node, elementType, argumentTypes.first); | 133 checkRelated(node, elementType, argumentTypes.first); |
139 } | 134 } |
140 } else if (selector.name == 'remove' && | 135 } else if (selector.name == 'remove' && |
141 selector.callStructure == CallStructure.ONE_ARG) { | 136 selector.callStructure == CallStructure.ONE_ARG) { |
142 InterfaceType mapType = findMapType(receiverType); | 137 InterfaceType mapType = findMapType(receiverType); |
143 if (mapType != null) { | 138 if (mapType != null) { |
144 DartType keyType = findMapKeyType(mapType); | 139 DartType keyType = findMapKeyType(mapType); |
145 checkRelated(node, keyType, argumentTypes.first); | 140 checkRelated(node, keyType, argumentTypes.first); |
146 } | 141 } |
147 InterfaceType listType = findListType(receiverType); | 142 InterfaceType listType = findListType(receiverType); |
148 if (listType != null) { | 143 if (listType != null) { |
149 DartType valueType = findListElementType(listType); | 144 DartType valueType = findListElementType(listType); |
150 checkRelated(node, valueType, argumentTypes.first); | 145 checkRelated(node, valueType, argumentTypes.first); |
151 } | 146 } |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 return field.type; | 349 return field.type; |
355 } | 350 } |
356 | 351 |
357 @override | 352 @override |
358 DartType visitTopLevelFieldGet(Send node, FieldElement field, _) { | 353 DartType visitTopLevelFieldGet(Send node, FieldElement field, _) { |
359 return field.type; | 354 return field.type; |
360 } | 355 } |
361 | 356 |
362 @override | 357 @override |
363 DartType visitDynamicPropertyInvoke( | 358 DartType visitDynamicPropertyInvoke( |
364 Send node, | 359 Send node, Node receiver, NodeList arguments, Selector selector, _) { |
365 Node receiver, | |
366 NodeList arguments, | |
367 Selector selector, _) { | |
368 DartType receiverType = apply(receiver); | 360 DartType receiverType = apply(receiver); |
369 List<DartType> argumentTypes = findArgumentTypes(arguments); | 361 List<DartType> argumentTypes = findArgumentTypes(arguments); |
370 FunctionType methodType = lookupInterfaceMemberInvocationType( | 362 FunctionType methodType = |
371 receiverType, selector.memberName); | 363 lookupInterfaceMemberInvocationType(receiverType, selector.memberName); |
372 checkDynamicInvoke(node, receiverType, argumentTypes, selector); | 364 checkDynamicInvoke(node, receiverType, argumentTypes, selector); |
373 return findReturnType(methodType); | 365 return findReturnType(methodType); |
374 } | 366 } |
375 | 367 |
376 @override | 368 @override |
377 DartType visitThisPropertyInvoke( | 369 DartType visitThisPropertyInvoke( |
378 Send node, | 370 Send node, NodeList arguments, Selector selector, _) { |
379 NodeList arguments, | |
380 Selector selector, _) { | |
381 DartType receiverType = thisType; | 371 DartType receiverType = thisType; |
382 List<DartType> argumentTypes = findArgumentTypes(arguments); | 372 List<DartType> argumentTypes = findArgumentTypes(arguments); |
383 FunctionType methodType = lookupInterfaceMemberInvocationType( | 373 FunctionType methodType = |
384 receiverType, selector.memberName); | 374 lookupInterfaceMemberInvocationType(receiverType, selector.memberName); |
385 checkDynamicInvoke(node, receiverType, argumentTypes, selector); | 375 checkDynamicInvoke(node, receiverType, argumentTypes, selector); |
386 return findReturnType(methodType); | 376 return findReturnType(methodType); |
387 } | 377 } |
388 | 378 |
389 @override | 379 @override |
390 DartType visitIfNotNullDynamicPropertyInvoke( | 380 DartType visitIfNotNullDynamicPropertyInvoke( |
391 Send node, | 381 Send node, Node receiver, NodeList arguments, Selector selector, _) { |
392 Node receiver, | |
393 NodeList arguments, | |
394 Selector selector, _) { | |
395 DartType receiverType = apply(receiver); | 382 DartType receiverType = apply(receiver); |
396 List<DartType> argumentTypes = findArgumentTypes(arguments); | 383 List<DartType> argumentTypes = findArgumentTypes(arguments); |
397 FunctionType methodType = lookupInterfaceMemberInvocationType( | 384 FunctionType methodType = |
398 receiverType, selector.memberName); | 385 lookupInterfaceMemberInvocationType(receiverType, selector.memberName); |
399 checkDynamicInvoke(node, receiverType, argumentTypes, selector); | 386 checkDynamicInvoke(node, receiverType, argumentTypes, selector); |
400 return findReturnType(methodType); | 387 return findReturnType(methodType); |
401 } | 388 } |
402 | 389 |
403 @override | 390 @override |
404 DartType visitTopLevelFunctionInvoke( | 391 DartType visitTopLevelFunctionInvoke(Send node, MethodElement function, |
405 Send node, | 392 NodeList arguments, CallStructure callStructure, _) { |
406 MethodElement function, | |
407 NodeList arguments, | |
408 CallStructure callStructure, _) { | |
409 apply(arguments); | 393 apply(arguments); |
410 return findReturnType(function.type); | 394 return findReturnType(function.type); |
411 } | 395 } |
412 | 396 |
413 @override | 397 @override |
414 DartType visitStaticFunctionInvoke( | 398 DartType visitStaticFunctionInvoke(Send node, MethodElement function, |
415 Send node, | 399 NodeList arguments, CallStructure callStructure, _) { |
416 MethodElement function, | |
417 NodeList arguments, | |
418 CallStructure callStructure, _) { | |
419 apply(arguments); | 400 apply(arguments); |
420 return findReturnType(function.type); | 401 return findReturnType(function.type); |
421 } | 402 } |
422 } | 403 } |
423 | 404 |
424 /// Computes the [ClassElement] implied by a type. | 405 /// Computes the [ClassElement] implied by a type. |
425 // TODO(johnniwinther): Handle type variables, function types and typedefs. | 406 // TODO(johnniwinther): Handle type variables, function types and typedefs. |
426 class ClassFinder extends BaseDartTypeVisitor<ClassElement, dynamic> { | 407 class ClassFinder extends BaseDartTypeVisitor<ClassElement, dynamic> { |
427 const ClassFinder(); | 408 const ClassFinder(); |
428 | 409 |
429 ClassElement findClass(DartType type) => type.accept(this, null); | 410 ClassElement findClass(DartType type) => type.accept(this, null); |
430 | 411 |
431 @override | 412 @override |
432 ClassElement visitType(DartType type, _) => null; | 413 ClassElement visitType(DartType type, _) => null; |
433 | 414 |
434 @override | 415 @override |
435 ClassElement visitInterfaceType(InterfaceType type, _) { | 416 ClassElement visitInterfaceType(InterfaceType type, _) { |
436 return type.element; | 417 return type.element; |
437 } | 418 } |
438 } | 419 } |
OLD | NEW |