| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 class RelatedTypesChecker extends TraversalVisitor<DartType, dynamic> { | 72 class RelatedTypesChecker extends TraversalVisitor<DartType, dynamic> { |
| 73 final Compiler compiler; | 73 final Compiler compiler; |
| 74 final ResolvedAst resolvedAst; | 74 final ResolvedAst resolvedAst; |
| 75 | 75 |
| 76 RelatedTypesChecker(this.compiler, ResolvedAst resolvedAst) | 76 RelatedTypesChecker(this.compiler, ResolvedAst resolvedAst) |
| 77 : this.resolvedAst = resolvedAst, | 77 : this.resolvedAst = resolvedAst, |
| 78 super(resolvedAst.elements); | 78 super(resolvedAst.elements); |
| 79 | 79 |
| 80 ClosedWorld get world => compiler.resolverWorld.closedWorldForTesting; | 80 ClosedWorld get world => compiler.resolverWorld.closedWorldForTesting; |
| 81 | 81 |
| 82 CoreClasses get coreClasses => compiler.coreClasses; | 82 CommonElements get commonElements => compiler.commonElements; |
| 83 | |
| 84 CoreTypes get coreTypes => compiler.coreTypes; | |
| 85 | 83 |
| 86 DiagnosticReporter get reporter => compiler.reporter; | 84 DiagnosticReporter get reporter => compiler.reporter; |
| 87 | 85 |
| 88 InterfaceType get thisType => resolvedAst.element.enclosingClass.thisType; | 86 InterfaceType get thisType => resolvedAst.element.enclosingClass.thisType; |
| 89 | 87 |
| 90 /// Returns `true` if there exists no common subtype of [left] and [right]. | 88 /// Returns `true` if there exists no common subtype of [left] and [right]. |
| 91 bool hasEmptyIntersection(DartType left, DartType right) { | 89 bool hasEmptyIntersection(DartType left, DartType right) { |
| 92 if (left == right) return false; | 90 if (left == right) return false; |
| 93 if (left == null || right == null) return false; | 91 if (left == null || right == null) return false; |
| 94 ClassElement leftClass = const ClassFinder().findClass(left); | 92 ClassElement leftClass = const ClassFinder().findClass(left); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 InterfaceType findClassType(DartType receiver, ClassElement cls) { | 156 InterfaceType findClassType(DartType receiver, ClassElement cls) { |
| 159 InterfaceType interfaceType = findInterfaceType(receiver); | 157 InterfaceType interfaceType = findInterfaceType(receiver); |
| 160 if (interfaceType == null) return null; | 158 if (interfaceType == null) return null; |
| 161 InterfaceType mapType = interfaceType.asInstanceOf(cls); | 159 InterfaceType mapType = interfaceType.asInstanceOf(cls); |
| 162 if (mapType == null) return null; | 160 if (mapType == null) return null; |
| 163 return mapType; | 161 return mapType; |
| 164 } | 162 } |
| 165 | 163 |
| 166 /// Returns the supertype of [receiver] that implements `Iterable`, if any. | 164 /// Returns the supertype of [receiver] that implements `Iterable`, if any. |
| 167 InterfaceType findIterableType(DartType receiver) { | 165 InterfaceType findIterableType(DartType receiver) { |
| 168 return findClassType(receiver, coreClasses.iterableClass); | 166 return findClassType(receiver, commonElements.iterableClass); |
| 169 } | 167 } |
| 170 | 168 |
| 171 /// Returns the element type of the supertype of [receiver] that implements | 169 /// Returns the element type of the supertype of [receiver] that implements |
| 172 /// `Iterable`, if any. | 170 /// `Iterable`, if any. |
| 173 DartType findIterableElementType(InterfaceType iterableType) { | 171 DartType findIterableElementType(InterfaceType iterableType) { |
| 174 if (iterableType == null) return null; | 172 if (iterableType == null) return null; |
| 175 return iterableType.typeArguments[0]; | 173 return iterableType.typeArguments[0]; |
| 176 } | 174 } |
| 177 | 175 |
| 178 /// Returns the supertype of [receiver] that implements `Map`, if any. | 176 /// Returns the supertype of [receiver] that implements `Map`, if any. |
| 179 InterfaceType findMapType(DartType receiver) { | 177 InterfaceType findMapType(DartType receiver) { |
| 180 return findClassType(receiver, coreClasses.mapClass); | 178 return findClassType(receiver, commonElements.mapClass); |
| 181 } | 179 } |
| 182 | 180 |
| 183 /// Returns the key type of the supertype of [receiver] that implements | 181 /// Returns the key type of the supertype of [receiver] that implements |
| 184 /// `Map`, if any. | 182 /// `Map`, if any. |
| 185 DartType findMapKeyType(InterfaceType mapType) { | 183 DartType findMapKeyType(InterfaceType mapType) { |
| 186 if (mapType == null) return null; | 184 if (mapType == null) return null; |
| 187 return mapType.typeArguments[0]; | 185 return mapType.typeArguments[0]; |
| 188 } | 186 } |
| 189 | 187 |
| 190 /// Returns the value type of the supertype of [receiver] that implements | 188 /// Returns the value type of the supertype of [receiver] that implements |
| 191 /// `Map`, if any. | 189 /// `Map`, if any. |
| 192 DartType findMapValueType(InterfaceType mapType) { | 190 DartType findMapValueType(InterfaceType mapType) { |
| 193 if (mapType == null) return null; | 191 if (mapType == null) return null; |
| 194 return mapType.typeArguments[1]; | 192 return mapType.typeArguments[1]; |
| 195 } | 193 } |
| 196 | 194 |
| 197 /// Returns the supertype of [receiver] that implements `List`, if any. | 195 /// Returns the supertype of [receiver] that implements `List`, if any. |
| 198 InterfaceType findListType(DartType receiver) { | 196 InterfaceType findListType(DartType receiver) { |
| 199 return findClassType(receiver, coreClasses.listClass); | 197 return findClassType(receiver, commonElements.listClass); |
| 200 } | 198 } |
| 201 | 199 |
| 202 /// Returns the element type of the supertype of [receiver] that implements | 200 /// Returns the element type of the supertype of [receiver] that implements |
| 203 /// `List`, if any. | 201 /// `List`, if any. |
| 204 DartType findListElementType(InterfaceType listType) { | 202 DartType findListElementType(InterfaceType listType) { |
| 205 if (listType == null) return null; | 203 if (listType == null) return null; |
| 206 return listType.typeArguments[0]; | 204 return listType.typeArguments[0]; |
| 207 } | 205 } |
| 208 | 206 |
| 209 /// Returns the implied return type of [type] or `dynamic` if no return type | 207 /// Returns the implied return type of [type] or `dynamic` if no return type |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 type = const DynamicType(); | 251 type = const DynamicType(); |
| 254 } | 252 } |
| 255 return type; | 253 return type; |
| 256 } | 254 } |
| 257 | 255 |
| 258 @override | 256 @override |
| 259 DartType visitEquals(Send node, Node left, Node right, _) { | 257 DartType visitEquals(Send node, Node left, Node right, _) { |
| 260 DartType leftType = apply(left); | 258 DartType leftType = apply(left); |
| 261 DartType rightType = apply(right); | 259 DartType rightType = apply(right); |
| 262 checkRelated(node, leftType, rightType); | 260 checkRelated(node, leftType, rightType); |
| 263 return coreTypes.boolType; | 261 return commonElements.boolType; |
| 264 } | 262 } |
| 265 | 263 |
| 266 @override | 264 @override |
| 267 DartType visitNotEquals(Send node, Node left, Node right, _) { | 265 DartType visitNotEquals(Send node, Node left, Node right, _) { |
| 268 DartType leftType = apply(left); | 266 DartType leftType = apply(left); |
| 269 DartType rightType = apply(right); | 267 DartType rightType = apply(right); |
| 270 checkRelated(node, leftType, rightType); | 268 checkRelated(node, leftType, rightType); |
| 271 return coreTypes.boolType; | 269 return commonElements.boolType; |
| 272 } | 270 } |
| 273 | 271 |
| 274 @override | 272 @override |
| 275 DartType visitIndex(Send node, Node receiver, Node index, _) { | 273 DartType visitIndex(Send node, Node receiver, Node index, _) { |
| 276 DartType receiverType = apply(receiver); | 274 DartType receiverType = apply(receiver); |
| 277 DartType indexType = apply(index); | 275 DartType indexType = apply(index); |
| 278 InterfaceType mapType = findMapType(receiverType); | 276 InterfaceType mapType = findMapType(receiverType); |
| 279 DartType keyType = findMapKeyType(mapType); | 277 DartType keyType = findMapKeyType(mapType); |
| 280 DartType valueType = findMapValueType(mapType); | 278 DartType valueType = findMapValueType(mapType); |
| 281 checkRelated(index, keyType, indexType); | 279 checkRelated(index, keyType, indexType); |
| 282 return valueType; | 280 return valueType; |
| 283 } | 281 } |
| 284 | 282 |
| 285 @override | 283 @override |
| 286 DartType visitLiteralInt(LiteralInt node) { | 284 DartType visitLiteralInt(LiteralInt node) { |
| 287 return coreTypes.intType; | 285 return commonElements.intType; |
| 288 } | 286 } |
| 289 | 287 |
| 290 @override | 288 @override |
| 291 DartType visitLiteralString(LiteralString node) { | 289 DartType visitLiteralString(LiteralString node) { |
| 292 return coreTypes.stringType; | 290 return commonElements.stringType; |
| 293 } | 291 } |
| 294 | 292 |
| 295 @override | 293 @override |
| 296 DartType visitLiteralBool(LiteralBool node) { | 294 DartType visitLiteralBool(LiteralBool node) { |
| 297 return coreTypes.boolType; | 295 return commonElements.boolType; |
| 298 } | 296 } |
| 299 | 297 |
| 300 @override | 298 @override |
| 301 DartType visitLiteralMap(LiteralMap node) { | 299 DartType visitLiteralMap(LiteralMap node) { |
| 302 return elements.getType(node); | 300 return elements.getType(node); |
| 303 } | 301 } |
| 304 | 302 |
| 305 @override | 303 @override |
| 306 DartType visitLiteralList(LiteralList node) { | 304 DartType visitLiteralList(LiteralList node) { |
| 307 return elements.getType(node); | 305 return elements.getType(node); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 ClassElement findClass(DartType type) => type.accept(this, null); | 409 ClassElement findClass(DartType type) => type.accept(this, null); |
| 412 | 410 |
| 413 @override | 411 @override |
| 414 ClassElement visitType(DartType type, _) => null; | 412 ClassElement visitType(DartType type, _) => null; |
| 415 | 413 |
| 416 @override | 414 @override |
| 417 ClassElement visitInterfaceType(InterfaceType type, _) { | 415 ClassElement visitInterfaceType(InterfaceType type, _) { |
| 418 return type.element; | 416 return type.element; |
| 419 } | 417 } |
| 420 } | 418 } |
| OLD | NEW |