| 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 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be | 5 // TODO(jmesserly): this was ported from package:dev_compiler, and needs to be |
| 6 // refactored to fit into analyzer. | 6 // refactored to fit into analyzer. |
| 7 library analyzer.src.task.strong.rules; | 7 library analyzer.src.task.strong.rules; |
| 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'; |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 } | 414 } |
| 415 | 415 |
| 416 StaticInfo checkAssignment(Expression expr, DartType toT) { | 416 StaticInfo checkAssignment(Expression expr, DartType toT) { |
| 417 final fromT = getStaticType(expr); | 417 final fromT = getStaticType(expr); |
| 418 final Coercion c = _coerceTo(fromT, toT); | 418 final Coercion c = _coerceTo(fromT, toT); |
| 419 if (c is Identity) return null; | 419 if (c is Identity) return null; |
| 420 if (c is CoercionError) return new StaticTypeError(this, expr, toT); | 420 if (c is CoercionError) return new StaticTypeError(this, expr, toT); |
| 421 var reason = null; | 421 var reason = null; |
| 422 | 422 |
| 423 var errors = <String>[]; | 423 var errors = <String>[]; |
| 424 |
| 424 var ok = inferrer.inferExpression(expr, toT, errors); | 425 var ok = inferrer.inferExpression(expr, toT, errors); |
| 425 if (ok) return InferredType.create(this, expr, toT); | 426 if (ok) return InferredType.create(this, expr, toT); |
| 426 reason = (errors.isNotEmpty) ? errors.first : null; | 427 reason = (errors.isNotEmpty) ? errors.first : null; |
| 427 | 428 |
| 428 if (c is Cast) return DownCast.create(this, expr, c, reason: reason); | 429 if (c is Cast) return DownCast.create(this, expr, c, reason: reason); |
| 429 assert(false); | 430 assert(false); |
| 430 return null; | 431 return null; |
| 431 } | 432 } |
| 432 | 433 |
| 433 DartType elementType(Element e) { | 434 DartType elementType(Element e) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 502 |
| 502 /// Downward inference | 503 /// Downward inference |
| 503 bool inferExpression(Expression e, DartType t, List<String> errors) { | 504 bool inferExpression(Expression e, DartType t, List<String> errors) { |
| 504 // Don't cast top level expressions, only sub-expressions | 505 // Don't cast top level expressions, only sub-expressions |
| 505 return _inferExpression(e, t, errors, cast: false); | 506 return _inferExpression(e, t, errors, cast: false); |
| 506 } | 507 } |
| 507 | 508 |
| 508 /// Downward inference | 509 /// Downward inference |
| 509 bool _inferExpression(Expression e, DartType t, List<String> errors, | 510 bool _inferExpression(Expression e, DartType t, List<String> errors, |
| 510 {cast: true}) { | 511 {cast: true}) { |
| 511 if (e is ConditionalExpression) { | |
| 512 return _inferConditionalExpression(e, t, errors); | |
| 513 } | |
| 514 if (e is ParenthesizedExpression) { | |
| 515 return _inferParenthesizedExpression(e, t, errors); | |
| 516 } | |
| 517 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true; | 512 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true; |
| 518 if (cast && rules.getStaticType(e).isDynamic) { | 513 if (cast && rules.getStaticType(e).isDynamic) { |
| 519 annotateCastFromDynamic(e, t); | 514 annotateCastFromDynamic(e, t); |
| 520 return true; | 515 return true; |
| 521 } | 516 } |
| 522 if (e is FunctionExpression) return _inferFunctionExpression(e, t, errors); | |
| 523 if (e is ListLiteral) return _inferListLiteral(e, t, errors); | |
| 524 if (e is MapLiteral) return _inferMapLiteral(e, t, errors); | |
| 525 if (e is NamedExpression) return _inferNamedExpression(e, t, errors); | |
| 526 if (e is InstanceCreationExpression) { | |
| 527 return _inferInstanceCreationExpression(e, t, errors); | |
| 528 } | |
| 529 errors.add("$e cannot be typed as $t"); | 517 errors.add("$e cannot be typed as $t"); |
| 530 return false; | 518 return false; |
| 531 } | 519 } |
| 532 | 520 |
| 533 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype | 521 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype |
| 534 /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'> | 522 /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'> |
| 535 /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm' | 523 /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm' |
| 536 /// to derive a concrete instantation for I of the form <T0, ..., Tn>, | 524 /// to derive a concrete instantation for I of the form <T0, ..., Tn>, |
| 537 /// such that I<T0, .., Tn> <: t2 | 525 /// such that I<T0, .., Tn> <: t2 |
| 538 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { | 526 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 var entries = e.entries; | 750 var entries = e.entries; |
| 763 bool inferEntry(MapLiteralEntry entry) { | 751 bool inferEntry(MapLiteralEntry entry) { |
| 764 return _inferExpression(entry.key, kType, errors) && | 752 return _inferExpression(entry.key, kType, errors) && |
| 765 _inferExpression(entry.value, vType, errors); | 753 _inferExpression(entry.value, vType, errors); |
| 766 } | 754 } |
| 767 var b = entries.every(inferEntry); | 755 var b = entries.every(inferEntry); |
| 768 if (b) annotateMapLiteral(e, targs); | 756 if (b) annotateMapLiteral(e, targs); |
| 769 return b; | 757 return b; |
| 770 } | 758 } |
| 771 } | 759 } |
| OLD | NEW |