| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * Represents a meta-value for code generation. | 6 * Represents a meta-value for code generation. |
| 7 */ | 7 */ |
| 8 class Value { | 8 class Value { |
| 9 /** The [Type] of the [Value]. */ | 9 /** The [Type] of the [Value]. */ |
| 10 Type type; | 10 Type type; |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 var callMethod = toType.getCallMethod(); | 203 var callMethod = toType.getCallMethod(); |
| 204 if (callMethod != null) { | 204 if (callMethod != null) { |
| 205 int arity = callMethod.parameters.length; | 205 int arity = callMethod.parameters.length; |
| 206 var myCall = type.getCallMethod(); | 206 var myCall = type.getCallMethod(); |
| 207 if (myCall == null || myCall.parameters.length != arity) { | 207 if (myCall == null || myCall.parameters.length != arity) { |
| 208 return true; | 208 return true; |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 if (options.enableTypeChecks) { | 211 if (options.enableTypeChecks) { |
| 212 Type fromType = type; | 212 Type fromType = type; |
| 213 if (type.isVar && code != 'null') { | 213 if (type.isVar && (code != 'null' || !toType.isNullable)) { |
| 214 fromType = world.objectType; | 214 fromType = world.objectType; |
| 215 } | 215 } |
| 216 bool bothNum = type.isNum && toType.isNum; | 216 bool bothNum = type.isNum && toType.isNum; |
| 217 return fromType.isSubtypeOf(toType) || bothNum; | 217 return fromType.isSubtypeOf(toType) || bothNum; |
| 218 } | 218 } |
| 219 return false; | 219 return false; |
| 220 } | 220 } |
| 221 | 221 |
| 222 /** | 222 /** |
| 223 * Assign or convert this value to another type. | 223 * Assign or convert this value to another type. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 // and then only allowed Dynamic to be bottom for generic type args, I think | 258 // and then only allowed Dynamic to be bottom for generic type args, I think |
| 259 // we'd get the right behavior from isSubtypeOf. | 259 // we'd get the right behavior from isSubtypeOf. |
| 260 Type fromType = type; | 260 Type fromType = type; |
| 261 if (type.isVar && (code != 'null' || !toType.isNullable)) { | 261 if (type.isVar && (code != 'null' || !toType.isNullable)) { |
| 262 fromType = world.objectType; | 262 fromType = world.objectType; |
| 263 } | 263 } |
| 264 | 264 |
| 265 // TODO(jmesserly): remove the special case for "num" when our num handling | 265 // TODO(jmesserly): remove the special case for "num" when our num handling |
| 266 // is better. | 266 // is better. |
| 267 bool bothNum = type.isNum && toType.isNum; | 267 bool bothNum = type.isNum && toType.isNum; |
| 268 if (!checked || fromType.isSubtypeOf(toType) || bothNum) { | 268 if (fromType.isSubtypeOf(toType) || bothNum) { |
| 269 // No checks needed for a widening conversion. | 269 // No checks needed for a widening conversion. |
| 270 return this; | 270 return this; |
| 271 } | 271 } |
| 272 | 272 |
| 273 if (checked && !toType.isSubtypeOf(type)) { | 273 if (checked && !toType.isSubtypeOf(type)) { |
| 274 // According to the static types, this conversion can't work. | 274 // According to the static types, this conversion can't work. |
| 275 convertWarning(toType, node); | 275 convertWarning(toType, node); |
| 276 } | 276 } |
| 277 | 277 |
| 278 // Generate a runtime checks if they're turned on, otherwise skip it. | 278 // Generate a runtime checks if they're turned on, otherwise skip it. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 296 * Generates a run time type assertion for the given value. This works like | 296 * Generates a run time type assertion for the given value. This works like |
| 297 * [instanceOf], but it allows null since Dart types are nullable. | 297 * [instanceOf], but it allows null since Dart types are nullable. |
| 298 * Also it will throw a TypeError if it gets the wrong type. | 298 * Also it will throw a TypeError if it gets the wrong type. |
| 299 */ | 299 */ |
| 300 Value _typeAssert(MethodGenerator context, Type toType, Node node) { | 300 Value _typeAssert(MethodGenerator context, Type toType, Node node) { |
| 301 if (toType is ParameterType) { | 301 if (toType is ParameterType) { |
| 302 ParameterType p = toType; | 302 ParameterType p = toType; |
| 303 toType = p.extendsType; | 303 toType = p.extendsType; |
| 304 } | 304 } |
| 305 | 305 |
| 306 // TODO(jmesserly): fix checking of function types. |
| 307 // For now, don't generate a broken check. |
| 308 if (toType.getCallMethod() != null) { |
| 309 return this; |
| 310 } |
| 311 |
| 306 if (toType.isObject || toType.isVar) { | 312 if (toType.isObject || toType.isVar) { |
| 307 world.internalError('We thought ${type.name} is not a subtype of ${toType.
name}?'); | 313 world.internalError('We thought ${type.name} is not a subtype of ${toType.
name}?'); |
| 308 } | 314 } |
| 309 | 315 |
| 310 // TODO(jmesserly): better assert for integers? | 316 // TODO(jmesserly): better assert for integers? |
| 311 if (toType.isNum) toType = world.numType; | 317 if (toType.isNum) toType = world.numType; |
| 312 | 318 |
| 313 // Generate a check like these: | 319 // Generate a check like these: |
| 314 // obj && obj.is$TypeName() | 320 // obj && obj.is$TypeName() |
| 315 // $assert_int(obj) | 321 // $assert_int(obj) |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 | 674 |
| 669 // Then look for members in my library. | 675 // Then look for members in my library. |
| 670 member = home.library.lookup(name, span); | 676 member = home.library.lookup(name, span); |
| 671 if (member != null) { | 677 if (member != null) { |
| 672 return member; | 678 return member; |
| 673 } | 679 } |
| 674 | 680 |
| 675 return null; | 681 return null; |
| 676 } | 682 } |
| 677 } | 683 } |
| OLD | NEW |