OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 compiler.src.inferrer.type_graph_nodes; | 5 library compiler.src.inferrer.type_graph_nodes; |
6 | 6 |
7 import 'dart:collection' show IterableBase; | 7 import 'dart:collection' show IterableBase; |
8 | 8 |
9 import '../common.dart'; | 9 import '../common.dart'; |
10 import '../common/names.dart' show Identifiers; | 10 import '../common/names.dart' show Identifiers; |
11 import '../compiler.dart' show Compiler; | 11 import '../compiler.dart' show Compiler; |
12 import '../constants/values.dart'; | 12 import '../constants/values.dart'; |
13 import '../dart_types.dart' show DartType, FunctionType, TypeKind; | 13 import '../dart_types.dart' show DartType, FunctionType, TypeKind; |
14 import '../elements/elements.dart'; | 14 import '../elements/elements.dart'; |
| 15 import '../js_backend/backend.dart'; |
15 import '../tree/dartstring.dart' show DartString; | 16 import '../tree/dartstring.dart' show DartString; |
16 import '../tree/tree.dart' as ast show Node, LiteralBool, Send; | 17 import '../tree/tree.dart' as ast show Node, LiteralBool, Send; |
17 import '../types/masks.dart' | 18 import '../types/masks.dart' |
18 show | 19 show |
19 CommonMasks, | 20 CommonMasks, |
20 ContainerTypeMask, | 21 ContainerTypeMask, |
21 DictionaryTypeMask, | 22 DictionaryTypeMask, |
22 MapTypeMask, | 23 MapTypeMask, |
23 TypeMask, | 24 TypeMask, |
24 ValueTypeMask; | 25 ValueTypeMask; |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 Compiler compiler = inferrer.compiler; | 515 Compiler compiler = inferrer.compiler; |
515 if (!compiler.options.trustTypeAnnotations && | 516 if (!compiler.options.trustTypeAnnotations && |
516 !compiler.options.enableTypeAssertions && | 517 !compiler.options.enableTypeAssertions && |
517 !inferrer.annotations.trustTypeAnnotations(element)) { | 518 !inferrer.annotations.trustTypeAnnotations(element)) { |
518 return mask; | 519 return mask; |
519 } | 520 } |
520 if (element.isGenerativeConstructor || element.isSetter) { | 521 if (element.isGenerativeConstructor || element.isSetter) { |
521 return mask; | 522 return mask; |
522 } | 523 } |
523 if (element.isField) { | 524 if (element.isField) { |
524 return _narrowType(compiler, mask, element.type); | 525 return _narrowType(compiler.closedWorld, mask, element.type); |
525 } | 526 } |
526 assert( | 527 assert( |
527 element.isFunction || element.isGetter || element.isFactoryConstructor); | 528 element.isFunction || element.isGetter || element.isFactoryConstructor); |
528 | 529 |
529 FunctionType type = element.type; | 530 FunctionType type = element.type; |
530 return _narrowType(compiler, mask, type.returnType); | 531 return _narrowType(compiler.closedWorld, mask, type.returnType); |
531 } | 532 } |
532 | 533 |
533 TypeMask computeType(TypeGraphInferrerEngine inferrer) { | 534 TypeMask computeType(TypeGraphInferrerEngine inferrer) { |
534 TypeMask special = handleSpecialCases(inferrer); | 535 TypeMask special = handleSpecialCases(inferrer); |
535 if (special != null) return potentiallyNarrowType(special, inferrer); | 536 if (special != null) return potentiallyNarrowType(special, inferrer); |
536 return potentiallyNarrowType( | 537 return potentiallyNarrowType( |
537 inferrer.types.computeTypeMask(assignments), inferrer); | 538 inferrer.types.computeTypeMask(assignments), inferrer); |
538 } | 539 } |
539 | 540 |
540 TypeMask safeType(TypeGraphInferrerEngine inferrer) { | 541 TypeMask safeType(TypeGraphInferrerEngine inferrer) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 TypeMask mask, TypeGraphInferrerEngine inferrer) { | 662 TypeMask mask, TypeGraphInferrerEngine inferrer) { |
662 Compiler compiler = inferrer.compiler; | 663 Compiler compiler = inferrer.compiler; |
663 if (!compiler.options.trustTypeAnnotations && | 664 if (!compiler.options.trustTypeAnnotations && |
664 !inferrer.annotations.trustTypeAnnotations(declaration)) { | 665 !inferrer.annotations.trustTypeAnnotations(declaration)) { |
665 return mask; | 666 return mask; |
666 } | 667 } |
667 // When type assertions are enabled (aka checked mode), we have to always | 668 // When type assertions are enabled (aka checked mode), we have to always |
668 // ignore type annotations to ensure that the checks are actually inserted | 669 // ignore type annotations to ensure that the checks are actually inserted |
669 // into the function body and retained until runtime. | 670 // into the function body and retained until runtime. |
670 assert(!compiler.options.enableTypeAssertions); | 671 assert(!compiler.options.enableTypeAssertions); |
671 return _narrowType(compiler, mask, element.type); | 672 return _narrowType(compiler.closedWorld, mask, element.type); |
672 } | 673 } |
673 | 674 |
674 TypeMask computeType(TypeGraphInferrerEngine inferrer) { | 675 TypeMask computeType(TypeGraphInferrerEngine inferrer) { |
675 TypeMask special = handleSpecialCases(inferrer); | 676 TypeMask special = handleSpecialCases(inferrer); |
676 if (special != null) return special; | 677 if (special != null) return special; |
677 return potentiallyNarrowType( | 678 return potentiallyNarrowType( |
678 inferrer.types.computeTypeMask(assignments), inferrer); | 679 inferrer.types.computeTypeMask(assignments), inferrer); |
679 } | 680 } |
680 | 681 |
681 TypeMask safeType(TypeGraphInferrerEngine inferrer) { | 682 TypeMask safeType(TypeGraphInferrerEngine inferrer) { |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 return null; | 964 return null; |
964 } | 965 } |
965 } | 966 } |
966 | 967 |
967 TypeMask computeType(TypeGraphInferrerEngine inferrer) { | 968 TypeMask computeType(TypeGraphInferrerEngine inferrer) { |
968 Iterable<Element> oldTargets = targets; | 969 Iterable<Element> oldTargets = targets; |
969 TypeMask typeMask = computeTypedSelector(inferrer); | 970 TypeMask typeMask = computeTypedSelector(inferrer); |
970 inferrer.updateSelectorInTree(caller, call, selector, typeMask); | 971 inferrer.updateSelectorInTree(caller, call, selector, typeMask); |
971 | 972 |
972 Compiler compiler = inferrer.compiler; | 973 Compiler compiler = inferrer.compiler; |
| 974 JavaScriptBackend backend = compiler.backend; |
973 TypeMask maskToUse = | 975 TypeMask maskToUse = |
974 compiler.closedWorld.extendMaskIfReachesAll(selector, typeMask); | 976 compiler.closedWorld.extendMaskIfReachesAll(selector, typeMask); |
975 bool canReachAll = compiler.enabledInvokeOn && (maskToUse != typeMask); | 977 bool canReachAll = backend.hasInvokeOnSupport && (maskToUse != typeMask); |
976 | 978 |
977 // If this call could potentially reach all methods that satisfy | 979 // If this call could potentially reach all methods that satisfy |
978 // the untyped selector (through noSuchMethod's `Invocation` | 980 // the untyped selector (through noSuchMethod's `Invocation` |
979 // and a call to `delegate`), we iterate over all these methods to | 981 // and a call to `delegate`), we iterate over all these methods to |
980 // update their parameter types. | 982 // update their parameter types. |
981 targets = compiler.closedWorld.allFunctions.filter(selector, maskToUse); | 983 targets = compiler.closedWorld.allFunctions.filter(selector, maskToUse); |
982 Iterable<Element> typedTargets = canReachAll | 984 Iterable<Element> typedTargets = canReachAll |
983 ? compiler.closedWorld.allFunctions.filter(selector, typeMask) | 985 ? compiler.closedWorld.allFunctions.filter(selector, typeMask) |
984 : targets; | 986 : targets; |
985 | 987 |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1717 T visitBoolLiteralTypeInformation(BoolLiteralTypeInformation info); | 1719 T visitBoolLiteralTypeInformation(BoolLiteralTypeInformation info); |
1718 T visitClosureCallSiteTypeInformation(ClosureCallSiteTypeInformation info); | 1720 T visitClosureCallSiteTypeInformation(ClosureCallSiteTypeInformation info); |
1719 T visitStaticCallSiteTypeInformation(StaticCallSiteTypeInformation info); | 1721 T visitStaticCallSiteTypeInformation(StaticCallSiteTypeInformation info); |
1720 T visitDynamicCallSiteTypeInformation(DynamicCallSiteTypeInformation info); | 1722 T visitDynamicCallSiteTypeInformation(DynamicCallSiteTypeInformation info); |
1721 T visitMemberTypeInformation(MemberTypeInformation info); | 1723 T visitMemberTypeInformation(MemberTypeInformation info); |
1722 T visitParameterTypeInformation(ParameterTypeInformation info); | 1724 T visitParameterTypeInformation(ParameterTypeInformation info); |
1723 T visitClosureTypeInformation(ClosureTypeInformation info); | 1725 T visitClosureTypeInformation(ClosureTypeInformation info); |
1724 T visitAwaitTypeInformation(AwaitTypeInformation info); | 1726 T visitAwaitTypeInformation(AwaitTypeInformation info); |
1725 } | 1727 } |
1726 | 1728 |
1727 TypeMask _narrowType(Compiler compiler, TypeMask type, DartType annotation, | 1729 TypeMask _narrowType( |
| 1730 ClosedWorld closedWorld, TypeMask type, DartType annotation, |
1728 {bool isNullable: true}) { | 1731 {bool isNullable: true}) { |
1729 if (annotation.treatAsDynamic) return type; | 1732 if (annotation.treatAsDynamic) return type; |
1730 if (annotation.isObject) return type; | 1733 if (annotation.isObject) return type; |
1731 TypeMask otherType; | 1734 TypeMask otherType; |
1732 if (annotation.isTypedef || annotation.isFunctionType) { | 1735 if (annotation.isTypedef || annotation.isFunctionType) { |
1733 otherType = compiler.commonMasks.functionType; | 1736 otherType = closedWorld.commonMasks.functionType; |
1734 } else if (annotation.isTypeVariable) { | 1737 } else if (annotation.isTypeVariable) { |
1735 // TODO(ngeoffray): Narrow to bound. | 1738 // TODO(ngeoffray): Narrow to bound. |
1736 return type; | 1739 return type; |
1737 } else if (annotation.isVoid) { | 1740 } else if (annotation.isVoid) { |
1738 otherType = compiler.commonMasks.nullType; | 1741 otherType = closedWorld.commonMasks.nullType; |
1739 } else { | 1742 } else { |
1740 assert(annotation.isInterfaceType); | 1743 assert(annotation.isInterfaceType); |
1741 otherType = | 1744 otherType = new TypeMask.nonNullSubtype(annotation.element, closedWorld); |
1742 new TypeMask.nonNullSubtype(annotation.element, compiler.closedWorld); | |
1743 } | 1745 } |
1744 if (isNullable) otherType = otherType.nullable(); | 1746 if (isNullable) otherType = otherType.nullable(); |
1745 if (type == null) return otherType; | 1747 if (type == null) return otherType; |
1746 return type.intersection(otherType, compiler.closedWorld); | 1748 return type.intersection(otherType, closedWorld); |
1747 } | 1749 } |
OLD | NEW |