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 /// Encapsulates how to invoke the analyzer resolver and overrides how it | 5 /// Encapsulates how to invoke the analyzer resolver and overrides how it |
6 /// computes types on expressions to use our restricted set of types. | 6 /// computes types on expressions to use our restricted set of types. |
7 library dev_compiler.src.checker.resolver; | 7 library dev_compiler.src.checker.resolver; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 if (type == null || type == _typeProvider.bottomType) return; | 613 if (type == null || type == _typeProvider.bottomType) return; |
614 element.type = type; | 614 element.type = type; |
615 if (element is PropertyInducingElement) { | 615 if (element is PropertyInducingElement) { |
616 element.getter.returnType = type; | 616 element.getter.returnType = type; |
617 if (!element.isFinal && !element.isConst) { | 617 if (!element.isFinal && !element.isConst) { |
618 element.setter.parameters[0].type = type; | 618 element.setter.parameters[0].type = type; |
619 } | 619 } |
620 } | 620 } |
621 } | 621 } |
622 | 622 |
| 623 // TODO(vsm): Use leafp's matchType here? |
| 624 DartType _findIteratedType(InterfaceType type) { |
| 625 if (type.element == _typeProvider.iterableType.element) { |
| 626 var typeArguments = type.typeArguments; |
| 627 assert(typeArguments.length == 1); |
| 628 return typeArguments[0]; |
| 629 } |
| 630 |
| 631 if (type == _typeProvider.objectType) return null; |
| 632 |
| 633 var result = _findIteratedType(type.superclass); |
| 634 if (result != null) return result; |
| 635 |
| 636 for (final parent in type.interfaces) { |
| 637 result = _findIteratedType(parent); |
| 638 if (result != null) return result; |
| 639 } |
| 640 |
| 641 for (final parent in type.mixins) { |
| 642 result = _findIteratedType(parent); |
| 643 if (result != null) return result; |
| 644 } |
| 645 |
| 646 return null; |
| 647 } |
| 648 |
| 649 @override |
| 650 visitDeclaredIdentifier(DeclaredIdentifier node) { |
| 651 super.visitDeclaredIdentifier(node); |
| 652 if (node.type != null) return; |
| 653 |
| 654 var parent = node.parent; |
| 655 assert(parent is ForEachStatement); |
| 656 var expr = parent.iterable; |
| 657 var element = node.element as LocalVariableElementImpl; |
| 658 var exprType = expr.staticType; |
| 659 if (exprType is InterfaceType) { |
| 660 var iteratedType = _findIteratedType(exprType); |
| 661 if (iteratedType != null) { |
| 662 element.type = iteratedType; |
| 663 } |
| 664 } |
| 665 } |
| 666 |
623 @override // to propagate types to identifiers | 667 @override // to propagate types to identifiers |
624 visitMethodInvocation(MethodInvocation node) { | 668 visitMethodInvocation(MethodInvocation node) { |
625 // TODO(sigmund): follow up with analyzer team - why is this needed? | 669 // TODO(sigmund): follow up with analyzer team - why is this needed? |
626 visitSimpleIdentifier(node.methodName); | 670 visitSimpleIdentifier(node.methodName); |
627 super.visitMethodInvocation(node); | 671 super.visitMethodInvocation(node); |
628 | 672 |
629 var e = node.methodName.staticElement; | 673 var e = node.methodName.staticElement; |
630 if (e is FunctionElement && | 674 if (e is FunctionElement && |
631 e.library.name == '_foreign_helper' && | 675 e.library.name == '_foreign_helper' && |
632 e.name == 'JS') { | 676 e.name == 'JS') { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 } | 709 } |
666 } | 710 } |
667 | 711 |
668 // Review note: no longer need to override visitFunctionExpression, this is | 712 // Review note: no longer need to override visitFunctionExpression, this is |
669 // handled by the analyzer internally. | 713 // handled by the analyzer internally. |
670 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? | 714 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? |
671 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression | 715 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression |
672 // type in a (...) => expr or just the written type? | 716 // type in a (...) => expr or just the written type? |
673 | 717 |
674 } | 718 } |
OLD | NEW |