Chromium Code Reviews| 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 dev_compiler.src.checker.rules; | 5 library dev_compiler.src.checker.rules; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/resolver.dart'; | 9 import 'package:analyzer/src/generated/resolver.dart'; |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 bool isDynamic(DartType t) => true; | 75 bool isDynamic(DartType t) => true; |
| 76 bool isDynamicTarget(Expression expr) => true; | 76 bool isDynamicTarget(Expression expr) => true; |
| 77 bool isDynamicGet(Expression expr) => true; | 77 bool isDynamicGet(Expression expr) => true; |
| 78 bool isDynamicCall(Expression call) => true; | 78 bool isDynamicCall(Expression call) => true; |
| 79 } | 79 } |
| 80 | 80 |
| 81 class RestrictedRules extends TypeRules { | 81 class RestrictedRules extends TypeRules { |
| 82 final CheckerReporter _reporter; | 82 final CheckerReporter _reporter; |
| 83 final RulesOptions options; | 83 final RulesOptions options; |
| 84 final List<DartType> _nonnullableTypes; | 84 final List<DartType> _nonnullableTypes; |
| 85 DownwardsInference inferrer; | |
| 85 | 86 |
| 86 DartType _typeFromName(String name) { | 87 DartType _typeFromName(String name) { |
| 87 switch (name) { | 88 switch (name) { |
| 88 case 'int': | 89 case 'int': |
| 89 return provider.intType; | 90 return provider.intType; |
| 90 case 'double': | 91 case 'double': |
| 91 return provider.doubleType; | 92 return provider.doubleType; |
| 92 case 'num': | 93 case 'num': |
| 93 return provider.numType; | 94 return provider.numType; |
| 94 case 'bool': | 95 case 'bool': |
| 95 return provider.boolType; | 96 return provider.boolType; |
| 96 case 'String': | 97 case 'String': |
| 97 return provider.stringType; | 98 return provider.stringType; |
| 98 default: | 99 default: |
| 99 throw new UnsupportedError('Unsupported non-nullable type $name'); | 100 throw new UnsupportedError('Unsupported non-nullable type $name'); |
| 100 } | 101 } |
| 101 } | 102 } |
| 102 | 103 |
| 103 RestrictedRules(TypeProvider provider, this._reporter, {this.options}) | 104 RestrictedRules(TypeProvider provider, this._reporter, {this.options}) |
| 104 : _nonnullableTypes = <DartType>[], | 105 : _nonnullableTypes = <DartType>[], |
| 105 super(provider) { | 106 super(provider) { |
| 106 var types = options.nonnullableTypes; | 107 var types = options.nonnullableTypes; |
| 107 _nonnullableTypes.addAll(types.map(_typeFromName)); | 108 _nonnullableTypes.addAll(types.map(_typeFromName)); |
| 109 inferrer = new DownwardsInference(this); | |
| 108 } | 110 } |
| 109 | 111 |
| 110 DartType getStaticType(Expression expr) { | 112 DartType getStaticType(Expression expr) { |
| 111 var type = expr.staticType; | 113 var type = expr.staticType; |
| 112 if (type != null) return type; | 114 if (type != null) return type; |
| 113 _reporter.log(new MissingTypeError(expr)); | 115 _reporter.log(new MissingTypeError(expr)); |
| 114 return provider.dynamicType; | 116 return provider.dynamicType; |
| 115 } | 117 } |
| 116 | 118 |
| 117 bool _isBottom(DartType t, {bool dynamicIsBottom: false}) { | 119 bool _isBottom(DartType t, {bool dynamicIsBottom: false}) { |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 return Coercion.cast(fromT, toT); | 486 return Coercion.cast(fromT, toT); |
| 485 } | 487 } |
| 486 return Coercion.error(); | 488 return Coercion.error(); |
| 487 } | 489 } |
| 488 | 490 |
| 489 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { | 491 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { |
| 490 final fromT = getStaticType(expr); | 492 final fromT = getStaticType(expr); |
| 491 final Coercion c = _coerceTo(fromT, toT, options.wrapClosures); | 493 final Coercion c = _coerceTo(fromT, toT, options.wrapClosures); |
| 492 if (c is Identity) return null; | 494 if (c is Identity) return null; |
| 493 if (c is CoercionError) return new StaticTypeError(this, expr, toT); | 495 if (c is CoercionError) return new StaticTypeError(this, expr, toT); |
| 496 if (options.inferDownwards && inferrer.inferExpression(expr, toT)) { | |
| 497 return InferredType.create(this, expr, toT); | |
| 498 } | |
| 494 if (constContext && !options.allowConstCasts) { | 499 if (constContext && !options.allowConstCasts) { |
| 495 return new StaticTypeError(this, expr, toT); | 500 return new StaticTypeError(this, expr, toT); |
| 496 } | 501 } |
| 497 if (c is Cast) return DownCast.create(this, expr, c); | 502 if (c is Cast) return DownCast.create(this, expr, c); |
| 498 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); | 503 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); |
| 499 assert(false); | 504 assert(false); |
| 500 return null; | 505 return null; |
| 501 } | 506 } |
| 502 | 507 |
| 503 DartType elementType(Element e) { | 508 DartType elementType(Element e) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 if (element is FunctionElement || element is MethodElement) { | 543 if (element is FunctionElement || element is MethodElement) { |
| 539 // An original declaration. | 544 // An original declaration. |
| 540 return false; | 545 return false; |
| 541 } | 546 } |
| 542 } | 547 } |
| 543 | 548 |
| 544 var ft = t as FunctionType; | 549 var ft = t as FunctionType; |
| 545 return _anyParameterType(ft, (pt) => pt.isDynamic); | 550 return _anyParameterType(ft, (pt) => pt.isDynamic); |
| 546 } | 551 } |
| 547 } | 552 } |
| 553 | |
| 554 class DownwardsInference { | |
| 555 final TypeRules rules; | |
| 556 | |
| 557 DownwardsInference(this.rules); | |
| 558 | |
| 559 ////////// Called for each list literal which gets inferred ///// | |
|
vsm
2015/03/31 17:11:28
Nit: dart style is '///' as prefix: https://www.da
Leaf
2015/04/03 19:24:51
Done.
| |
| 560 void annotateListLiteral(ListLiteral e, List<DartType> targs) {} | |
| 561 | |
| 562 ////////// Called for each map literal which gets inferred ///// | |
| 563 void annotateMapLiteral(MapLiteral e, List<DartType> targs) {} | |
| 564 | |
| 565 ////////// Called for each new/const which gets inferred ///// | |
| 566 void annotateInstanceCreationExpression( | |
| 567 InstanceCreationExpression e, List<DartType> targs) {} | |
| 568 | |
| 569 ////////// Downward inference //////////////// | |
| 570 bool inferExpression(Expression e, DartType t) { | |
| 571 if (e is Conversion) return inferExpression(e.node, t); | |
| 572 if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true; | |
| 573 if (e is ListLiteral) return _inferListLiteral(e, t); | |
| 574 if (e is MapLiteral) return _inferMapLiteral(e, t); | |
| 575 if (e is NamedExpression) return _inferNamedExpression(e, t); | |
| 576 if (e is InstanceCreationExpression) return _inferInstanceCreationExpression ( | |
| 577 e, t); | |
| 578 return false; | |
| 579 } | |
| 580 | |
| 581 // If t1 = I<dynamic, ..., dynamic>, then look for a supertype | |
| 582 // of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'> | |
| 583 // If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm' | |
| 584 // to derive a concrete instantation for I of the form <T0, ..., Tn>, | |
| 585 // such that I<T0, .., Tn> <: t2 | |
| 586 List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) { | |
| 587 if (t1 == t2) return t2.typeArguments; | |
| 588 var tArgs1 = t1.typeArguments; | |
| 589 var tArgs2 = t2.typeArguments; | |
| 590 // If t1 isn't a raw type, bail out | |
| 591 if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) return null; | |
| 592 | |
| 593 // This is our inferred type argument list. We start at all dynamic, | |
| 594 // and fill in with inferred types when we reach a match. | |
| 595 var actuals = | |
| 596 new List<DartType>.filled(tArgs1.length, rules.provider.dynamicType); | |
| 597 | |
| 598 // When we find the supertype of t1 with the same | |
| 599 // classname as t2 (see below), we have the following: | |
| 600 // If t1 is an instantiation of a class T1<X0, ..., Xn> | |
| 601 // and t2 is an instantiation of a class T2<Y0, ...., Ym> | |
| 602 // of the form t2 = T2<S0, ..., Sm> | |
| 603 // then we want to choose instantiations for the Xi | |
| 604 // T0, ..., Tn such that T1<T0, ..., Tn> <: t2 . | |
| 605 // To find this, we simply instantate T1 with | |
| 606 // X0, ..., Xn, and then find its superclass | |
| 607 // T2<T0', ..., Tn'>. We then solve the constraint | |
| 608 // set T0' <: S0, ..., Tn' <: Sn for the Xi. | |
| 609 // Currently, we only handle constraints where | |
| 610 // the Ti' is one of the Xi'. If there are multiple | |
| 611 // constraints on some Xi, we choose the lower of the | |
| 612 // two (if it exists). | |
| 613 bool permute(List<DartType> permutedArgs) { | |
| 614 if (permutedArgs == null) return false; | |
| 615 var ps = t1.typeParameters; | |
| 616 var ts = ps.map((p) => p.type).toList(); | |
| 617 for (int i = 0; i < permutedArgs.length; i++) { | |
| 618 var tVar = permutedArgs[i]; | |
| 619 var tActual = tArgs2[i]; | |
| 620 var index = ts.indexOf(tVar); | |
| 621 if (index >= 0 && rules.isSubTypeOf(tActual, actuals[index])) { | |
| 622 actuals[index] = tActual; | |
| 623 } | |
| 624 } | |
| 625 return actuals.any((x) => !x.isDynamic); | |
| 626 } | |
| 627 | |
| 628 // Look for the first supertype of t1 with the same class name as t2. | |
| 629 bool match(InterfaceType t1) { | |
| 630 if (t1.element == t2.element) { | |
| 631 return permute(t1.typeArguments); | |
| 632 } | |
| 633 | |
| 634 if (t1 == rules.provider.objectType) return false; | |
| 635 | |
| 636 if (match(t1.superclass)) return true; | |
| 637 | |
| 638 for (final parent in t1.interfaces) { | |
| 639 if (match(parent)) return true; | |
| 640 } | |
| 641 | |
| 642 for (final parent in t1.mixins) { | |
| 643 if (match(parent)) return true; | |
| 644 } | |
| 645 return false; | |
| 646 } | |
| 647 | |
| 648 // We have that t1 = T1<dynamic, ..., dynamic>. | |
| 649 // To match t1 against t2, we use the uninstantiated version | |
| 650 // of t1, essentially treating it as an instantiation with | |
| 651 // fresh variables, and solve for the variables. | |
| 652 // t1.element.type will be of the form T1<X0, ..., Xn> | |
| 653 if (!match(t1.element.type)) return null; | |
| 654 var newT1 = t1.element.type.substitute4(actuals); | |
| 655 // If we found a solution, return it. | |
| 656 if (rules.isSubTypeOf(newT1, t2)) return actuals; | |
| 657 return null; | |
| 658 } | |
| 659 | |
| 660 ///////// These assume that e is not already a subtype of t ////////////////// | |
| 661 | |
| 662 bool _inferInstanceCreationExpression( | |
| 663 InstanceCreationExpression e, DartType t) { | |
| 664 var arguments = e.argumentList.arguments; | |
| 665 var rawType = rules.getStaticType(e); | |
| 666 // rawType is the instantiated type of the instance | |
| 667 if (rawType is! InterfaceType) return false; | |
| 668 var type = (rawType as InterfaceType); | |
| 669 if (type.typeParameters == null || | |
| 670 type.typeParameters.length == 0) return false; | |
| 671 if (e.constructorName.type == null) return false; | |
| 672 // classTypeName is the type name of the class being instantiated | |
| 673 var classTypeName = e.constructorName.type; | |
| 674 // Check that we were not passed any type arguments | |
| 675 if (classTypeName.typeArguments != null) return false; | |
| 676 // Infer type arguments | |
| 677 var targs = _matchTypes(type, t); | |
| 678 if (targs == null) return false; | |
| 679 if (e.staticElement == null) return false; | |
| 680 var constructorElement = e.staticElement; | |
| 681 // From the constructor element get: | |
| 682 // the instantiated type of the constructor, then | |
| 683 // the uninstantiated element for the constructor, then | |
| 684 // the uninstantiated type for the constructor | |
| 685 var baseType = constructorElement.type.element.type; | |
| 686 if (baseType == null) return false; | |
| 687 // From the interface type (instantiated), get: | |
| 688 // the uninstantiated element, then | |
| 689 // the uninstantiated type, then | |
| 690 // the type arguments (aka the type parameters) | |
| 691 var tparams = type.element.type.typeArguments; | |
| 692 // Take the uninstantiated constructor type, and replace the type | |
| 693 // parameters with the inferred arguments. | |
| 694 var fType = baseType.substitute2(targs, tparams); | |
| 695 { | |
| 696 var rTypes = fType.normalParameterTypes; | |
| 697 var oTypes = fType.optionalParameterTypes; | |
| 698 var pTypes = new List.from(rTypes)..addAll(oTypes); | |
| 699 var pArgs = arguments.where((x) => x is! NamedExpression); | |
| 700 var pi = 0; | |
| 701 for (var arg in pArgs) { | |
| 702 if (pi >= pTypes.length) return false; | |
| 703 var argType = pTypes[pi]; | |
| 704 if (!inferExpression(arg, argType)) return false; | |
| 705 pi++; | |
| 706 } | |
| 707 var nTypes = fType.namedParameterTypes; | |
| 708 for (var arg0 in arguments) { | |
| 709 if (arg0 is! NamedExpression) continue; | |
| 710 var arg = arg0 as NamedExpression; | |
| 711 SimpleIdentifier nameNode = arg.name.label; | |
| 712 String name = nameNode.name; | |
| 713 var argType = nTypes[name]; | |
| 714 if (argType == null) return false; | |
| 715 if (!inferExpression(arg, argType)) return false; | |
| 716 } | |
| 717 } | |
| 718 annotateInstanceCreationExpression(e, targs); | |
| 719 return true; | |
| 720 } | |
| 721 | |
| 722 bool _inferNamedExpression(NamedExpression e, DartType t) { | |
| 723 return inferExpression(e.expression, t); | |
| 724 } | |
| 725 | |
| 726 bool _inferListLiteral(ListLiteral e, DartType t) { | |
| 727 var dyn = rules.provider.dynamicType; | |
| 728 var listT = rules.provider.listType.substitute4([dyn]); | |
| 729 // List <: t (using dart rules) must be true | |
| 730 if (!listT.isSubtypeOf(t)) return false; | |
| 731 // The list literal must have no type arguments | |
| 732 if (e.typeArguments != null) return false; | |
| 733 if (t is! InterfaceType) return false; | |
| 734 var targs = _matchTypes(listT, t); | |
| 735 if (targs == null) return false; | |
| 736 assert(targs.length == 1); | |
| 737 var etype = targs[0]; | |
| 738 assert(!etype.isDynamic); | |
| 739 var elements = e.elements; | |
| 740 var b = elements.every((e) => inferExpression(e, etype)); | |
| 741 if (b) annotateListLiteral(e, targs); | |
| 742 return b; | |
| 743 } | |
| 744 | |
| 745 bool _inferMapLiteral(MapLiteral e, DartType t) { | |
| 746 var dyn = rules.provider.dynamicType; | |
| 747 var mapT = rules.provider.mapType.substitute4([dyn, dyn]); | |
| 748 // Map <: t (using dart rules) must be true | |
| 749 if (!mapT.isSubtypeOf(t)) return false; | |
| 750 // The map literal must have no type arguments | |
| 751 if (e.typeArguments != null) return false; | |
| 752 if (t is! InterfaceType) return false; | |
| 753 var targs = _matchTypes(mapT, t); | |
| 754 if (targs == null) return false; | |
| 755 assert(targs.length == 2); | |
| 756 var kType = targs[0]; | |
| 757 var vType = targs[1]; | |
| 758 assert(!(kType.isDynamic && vType.isDynamic)); | |
| 759 var entries = e.entries; | |
| 760 bool inferEntry(MapLiteralEntry entry) { | |
| 761 return inferExpression(entry.key, kType) && | |
| 762 inferExpression(entry.value, vType); | |
| 763 } | |
| 764 var b = entries.every(inferEntry); | |
| 765 if (b) annotateMapLiteral(e, targs); | |
| 766 return b; | |
| 767 } | |
| 768 } | |
| OLD | NEW |