| 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 | |
| 533 /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype | |
| 534 /// 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' | |
| 536 /// to derive a concrete instantation for I of the form <T0, ..., Tn>, | |
| 537 /// such that I<T0, .., Tn> <: t2 | |
| 538 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { | |
| 539 if (t1 == t2) return t2.typeArguments; | |
| 540 var tArgs1 = t1.typeArguments; | |
| 541 var tArgs2 = t2.typeArguments; | |
| 542 // If t1 isn't a raw type, bail out | |
| 543 if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) return null; | |
| 544 | |
| 545 // This is our inferred type argument list. We start at all dynamic, | |
| 546 // and fill in with inferred types when we reach a match. | |
| 547 var actuals = | |
| 548 new List<DartType>.filled(tArgs1.length, rules.provider.dynamicType); | |
| 549 | |
| 550 // When we find the supertype of t1 with the same | |
| 551 // classname as t2 (see below), we have the following: | |
| 552 // If t1 is an instantiation of a class T1<X0, ..., Xn> | |
| 553 // and t2 is an instantiation of a class T2<Y0, ...., Ym> | |
| 554 // of the form t2 = T2<S0, ..., Sm> | |
| 555 // then we want to choose instantiations for the Xi | |
| 556 // T0, ..., Tn such that T1<T0, ..., Tn> <: t2 . | |
| 557 // To find this, we simply instantate T1 with | |
| 558 // X0, ..., Xn, and then find its superclass | |
| 559 // T2<T0', ..., Tn'>. We then solve the constraint | |
| 560 // set T0' <: S0, ..., Tn' <: Sn for the Xi. | |
| 561 // Currently, we only handle constraints where | |
| 562 // the Ti' is one of the Xi'. If there are multiple | |
| 563 // constraints on some Xi, we choose the lower of the | |
| 564 // two (if it exists). | |
| 565 bool permute(List<DartType> permutedArgs) { | |
| 566 if (permutedArgs == null) return false; | |
| 567 var ps = t1.typeParameters; | |
| 568 var ts = ps.map((p) => p.type).toList(); | |
| 569 for (int i = 0; i < permutedArgs.length; i++) { | |
| 570 var tVar = permutedArgs[i]; | |
| 571 var tActual = tArgs2[i]; | |
| 572 var index = ts.indexOf(tVar); | |
| 573 if (index >= 0 && rules.isSubTypeOf(tActual, actuals[index])) { | |
| 574 actuals[index] = tActual; | |
| 575 } | |
| 576 } | |
| 577 return actuals.any((x) => !x.isDynamic); | |
| 578 } | |
| 579 | |
| 580 // Look for the first supertype of t1 with the same class name as t2. | |
| 581 bool match(InterfaceType t1) { | |
| 582 if (t1.element == t2.element) { | |
| 583 return permute(t1.typeArguments); | |
| 584 } | |
| 585 | |
| 586 if (t1 == rules.provider.objectType) return false; | |
| 587 | |
| 588 if (match(t1.superclass)) return true; | |
| 589 | |
| 590 for (final parent in t1.interfaces) { | |
| 591 if (match(parent)) return true; | |
| 592 } | |
| 593 | |
| 594 for (final parent in t1.mixins) { | |
| 595 if (match(parent)) return true; | |
| 596 } | |
| 597 return false; | |
| 598 } | |
| 599 | |
| 600 // We have that t1 = T1<dynamic, ..., dynamic>. | |
| 601 // To match t1 against t2, we use the uninstantiated version | |
| 602 // of t1, essentially treating it as an instantiation with | |
| 603 // fresh variables, and solve for the variables. | |
| 604 // t1.element.type will be of the form T1<X0, ..., Xn> | |
| 605 if (!match(t1.element.type)) return null; | |
| 606 var newT1 = t1.element.type.substitute4(actuals); | |
| 607 // If we found a solution, return it. | |
| 608 if (rules.isSubTypeOf(newT1, t2)) return actuals; | |
| 609 return null; | |
| 610 } | |
| 611 | |
| 612 /// These assume that e is not already a subtype of t | |
| 613 | |
| 614 bool _inferConditionalExpression( | |
| 615 ConditionalExpression e, DartType t, errors) { | |
| 616 return _inferExpression(e.thenExpression, t, errors) && | |
| 617 _inferExpression(e.elseExpression, t, errors); | |
| 618 } | |
| 619 | |
| 620 bool _inferParenthesizedExpression( | |
| 621 ParenthesizedExpression e, DartType t, errors) { | |
| 622 return _inferExpression(e.expression, t, errors); | |
| 623 } | |
| 624 | |
| 625 bool _inferInstanceCreationExpression( | |
| 626 InstanceCreationExpression e, DartType t, errors) { | |
| 627 var arguments = e.argumentList.arguments; | |
| 628 var rawType = rules.getStaticType(e); | |
| 629 // rawType is the instantiated type of the instance | |
| 630 if (rawType is! InterfaceType) return false; | |
| 631 var type = (rawType as InterfaceType); | |
| 632 if (type.typeParameters == null || | |
| 633 type.typeParameters.length == 0) return false; | |
| 634 if (e.constructorName.type == null) return false; | |
| 635 // classTypeName is the type name of the class being instantiated | |
| 636 var classTypeName = e.constructorName.type; | |
| 637 // Check that we were not passed any type arguments | |
| 638 if (classTypeName.typeArguments != null) return false; | |
| 639 // Infer type arguments | |
| 640 if (t is! InterfaceType) return false; | |
| 641 var targs = _matchTypes(type, t); | |
| 642 if (targs == null) return false; | |
| 643 if (e.staticElement == null) return false; | |
| 644 var constructorElement = e.staticElement; | |
| 645 // From the constructor element get: | |
| 646 // the instantiated type of the constructor, then | |
| 647 // the uninstantiated element for the constructor, then | |
| 648 // the uninstantiated type for the constructor | |
| 649 var rawConstructorElement = | |
| 650 constructorElement.type.element as ConstructorElement; | |
| 651 var baseType = rawConstructorElement.type; | |
| 652 if (baseType == null) return false; | |
| 653 // From the interface type (instantiated), get: | |
| 654 // the uninstantiated element, then | |
| 655 // the uninstantiated type, then | |
| 656 // the type arguments (aka the type parameters) | |
| 657 var tparams = type.element.type.typeArguments; | |
| 658 // Take the uninstantiated constructor type, and replace the type | |
| 659 // parameters with the inferred arguments. | |
| 660 var fType = baseType.substitute2(targs, tparams); | |
| 661 { | |
| 662 var rTypes = fType.normalParameterTypes; | |
| 663 var oTypes = fType.optionalParameterTypes; | |
| 664 var pTypes = new List.from(rTypes)..addAll(oTypes); | |
| 665 var pArgs = arguments.where((x) => x is! NamedExpression); | |
| 666 var pi = 0; | |
| 667 for (var arg in pArgs) { | |
| 668 if (pi >= pTypes.length) return false; | |
| 669 var argType = pTypes[pi]; | |
| 670 if (!_inferExpression(arg, argType, errors)) return false; | |
| 671 pi++; | |
| 672 } | |
| 673 var nTypes = fType.namedParameterTypes; | |
| 674 for (var arg0 in arguments) { | |
| 675 if (arg0 is! NamedExpression) continue; | |
| 676 var arg = arg0 as NamedExpression; | |
| 677 SimpleIdentifier nameNode = arg.name.label; | |
| 678 String name = nameNode.name; | |
| 679 var argType = nTypes[name]; | |
| 680 if (argType == null) return false; | |
| 681 if (!_inferExpression(arg, argType, errors)) return false; | |
| 682 } | |
| 683 } | |
| 684 annotateInstanceCreationExpression(e, targs); | |
| 685 return true; | |
| 686 } | |
| 687 | |
| 688 bool _inferNamedExpression(NamedExpression e, DartType t, errors) { | |
| 689 return _inferExpression(e.expression, t, errors); | |
| 690 } | |
| 691 | |
| 692 bool _inferFunctionExpression(FunctionExpression e, DartType t, errors) { | |
| 693 if (t is! FunctionType) return false; | |
| 694 var fType = t as FunctionType; | |
| 695 var eType = e.staticType as FunctionType; | |
| 696 if (eType is! FunctionType) return false; | |
| 697 | |
| 698 // We have a function literal, so we can treat the arrow type | |
| 699 // as non-fuzzy. Since we're not improving on parameter types | |
| 700 // currently, if this check fails then we cannot succeed, so | |
| 701 // bail out. Otherwise, we never need to check the parameter types | |
| 702 // again. | |
| 703 if (!rules.isFunctionSubTypeOf(eType, fType, | |
| 704 fuzzyArrows: false, ignoreReturn: true)) return false; | |
| 705 | |
| 706 // This only entered inference because of fuzzy typing. | |
| 707 // The function type is already specific enough, we can just | |
| 708 // succeed and treat it as a successful inference | |
| 709 if (rules.isSubTypeOf(eType.returnType, fType.returnType)) return true; | |
| 710 | |
| 711 // Fuzzy typing again, handle the void case (not caught by the previous) | |
| 712 if (fType.returnType.isVoid) return true; | |
| 713 | |
| 714 if (e.body is! ExpressionFunctionBody) return false; | |
| 715 var body = (e.body as ExpressionFunctionBody).expression; | |
| 716 if (!_inferExpression(body, fType.returnType, errors)) return false; | |
| 717 | |
| 718 // TODO(leafp): Try narrowing the argument types if possible | |
| 719 // to get better code in the function body. This requires checking | |
| 720 // that the body is well-typed at the more specific type. | |
| 721 | |
| 722 // At this point, we know that the parameter types are in the appropriate su
btype | |
| 723 // relation, and we have checked that we can type the body at the appropriat
e return | |
| 724 // type, so we can are done. | |
| 725 annotateFunctionExpression(e, fType.returnType); | |
| 726 return true; | |
| 727 } | |
| 728 | |
| 729 bool _inferListLiteral(ListLiteral e, DartType t, errors) { | |
| 730 var dyn = rules.provider.dynamicType; | |
| 731 var listT = rules.provider.listType.substitute4([dyn]); | |
| 732 // List <: t (using dart rules) must be true | |
| 733 if (!listT.isSubtypeOf(t)) return false; | |
| 734 // The list literal must have no type arguments | |
| 735 if (e.typeArguments != null) return false; | |
| 736 if (t is! InterfaceType) return false; | |
| 737 var targs = _matchTypes(listT, t); | |
| 738 if (targs == null) return false; | |
| 739 assert(targs.length == 1); | |
| 740 var etype = targs[0]; | |
| 741 assert(!etype.isDynamic); | |
| 742 var elements = e.elements; | |
| 743 var b = elements.every((e) => _inferExpression(e, etype, errors)); | |
| 744 if (b) annotateListLiteral(e, targs); | |
| 745 return b; | |
| 746 } | |
| 747 | |
| 748 bool _inferMapLiteral(MapLiteral e, DartType t, errors) { | |
| 749 var dyn = rules.provider.dynamicType; | |
| 750 var mapT = rules.provider.mapType.substitute4([dyn, dyn]); | |
| 751 // Map <: t (using dart rules) must be true | |
| 752 if (!mapT.isSubtypeOf(t)) return false; | |
| 753 // The map literal must have no type arguments | |
| 754 if (e.typeArguments != null) return false; | |
| 755 if (t is! InterfaceType) return false; | |
| 756 var targs = _matchTypes(mapT, t); | |
| 757 if (targs == null) return false; | |
| 758 assert(targs.length == 2); | |
| 759 var kType = targs[0]; | |
| 760 var vType = targs[1]; | |
| 761 assert(!(kType.isDynamic && vType.isDynamic)); | |
| 762 var entries = e.entries; | |
| 763 bool inferEntry(MapLiteralEntry entry) { | |
| 764 return _inferExpression(entry.key, kType, errors) && | |
| 765 _inferExpression(entry.value, vType, errors); | |
| 766 } | |
| 767 var b = entries.every(inferEntry); | |
| 768 if (b) annotateMapLiteral(e, targs); | |
| 769 return b; | |
| 770 } | |
| 771 } | 520 } |
| OLD | NEW |