| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 kernel.tree_shaker; | 5 library kernel.tree_shaker; |
| 6 | 6 |
| 7 import '../ast.dart'; | 7 import '../ast.dart'; |
| 8 import '../class_hierarchy.dart'; | 8 import '../class_hierarchy.dart'; |
| 9 import '../core_types.dart'; | 9 import '../core_types.dart'; |
| 10 import '../type_environment.dart'; | 10 import '../type_environment.dart'; |
| (...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 class _TreeShakingTransformer extends Transformer { | 800 class _TreeShakingTransformer extends Transformer { |
| 801 final TreeShaker shaker; | 801 final TreeShaker shaker; |
| 802 | 802 |
| 803 _TreeShakingTransformer(this.shaker); | 803 _TreeShakingTransformer(this.shaker); |
| 804 | 804 |
| 805 Member _translateInterfaceTarget(Member target) { | 805 Member _translateInterfaceTarget(Member target) { |
| 806 return target != null && shaker.isMemberUsed(target) ? target : null; | 806 return target != null && shaker.isMemberUsed(target) ? target : null; |
| 807 } | 807 } |
| 808 | 808 |
| 809 void transform(Program program) { | 809 void transform(Program program) { |
| 810 for (var library in program.libraries) { | |
| 811 if (library.importUri.scheme == 'dart') { | |
| 812 // The backend expects certain things to be present in the core | |
| 813 // libraries, so we currently don't shake off anything there. | |
| 814 continue; | |
| 815 } | |
| 816 library.transformChildren(this); | |
| 817 // Note: we can't shake off empty libraries yet since we don't check if | |
| 818 // there are private names that use the library. | |
| 819 } | |
| 820 for (Expression node in shaker._typedCalls) { | 810 for (Expression node in shaker._typedCalls) { |
| 821 // We should not leave dangling references, so if the target of a typed | 811 // We should not leave dangling references, so if the target of a typed |
| 822 // call has been removed, we must remove the reference. The receiver of | 812 // call has been removed, we must remove the reference. The receiver of |
| 823 // such a call can only be null. | 813 // such a call can only be null. |
| 824 // TODO(asgerf): Rewrite to a NSM call instead of adding dynamic calls. | 814 // TODO(asgerf): Rewrite to a NSM call instead of adding dynamic calls. |
| 825 if (node is MethodInvocation) { | 815 if (node is MethodInvocation) { |
| 826 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); | 816 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); |
| 827 } else if (node is PropertyGet) { | 817 } else if (node is PropertyGet) { |
| 828 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); | 818 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); |
| 829 } else if (node is PropertySet) { | 819 } else if (node is PropertySet) { |
| 830 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); | 820 node.interfaceTarget = _translateInterfaceTarget(node.interfaceTarget); |
| 831 } | 821 } |
| 832 } | 822 } |
| 823 for (var library in program.libraries) { |
| 824 if (library.importUri.scheme == 'dart') { |
| 825 // The backend expects certain things to be present in the core |
| 826 // libraries, so we currently don't shake off anything there. |
| 827 continue; |
| 828 } |
| 829 library.transformChildren(this); |
| 830 // Note: we can't shake off empty libraries yet since we don't check if |
| 831 // there are private names that use the library. |
| 832 } |
| 833 } | 833 } |
| 834 | 834 |
| 835 Class visitClass(Class node) { | 835 Class visitClass(Class node) { |
| 836 switch (shaker.getClassRetention(node)) { | 836 switch (shaker.getClassRetention(node)) { |
| 837 case ClassRetention.None: | 837 case ClassRetention.None: |
| 838 node.canonicalName.unlink(); |
| 838 return null; // Remove the class. | 839 return null; // Remove the class. |
| 839 | 840 |
| 840 case ClassRetention.Namespace: | 841 case ClassRetention.Namespace: |
| 841 // The class is only a namespace for static members. Remove its | 842 // The class is only a namespace for static members. Remove its |
| 842 // hierarchy information. This is mandatory, since these references | 843 // hierarchy information. This is mandatory, since these references |
| 843 // might otherwise become dangling. | 844 // might otherwise become dangling. |
| 844 node.supertype = shaker.coreTypes.objectClass.asRawSupertype; | 845 node.supertype = shaker.coreTypes.objectClass.asRawSupertype; |
| 845 node.implementedTypes.clear(); | 846 node.implementedTypes.clear(); |
| 846 node.typeParameters.clear(); | 847 node.typeParameters.clear(); |
| 847 node.isAbstract = true; | 848 node.isAbstract = true; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 858 case ClassRetention.ExternalInstance: | 859 case ClassRetention.ExternalInstance: |
| 859 break; | 860 break; |
| 860 } | 861 } |
| 861 node.transformChildren(this); | 862 node.transformChildren(this); |
| 862 return node; | 863 return node; |
| 863 } | 864 } |
| 864 | 865 |
| 865 Member defaultMember(Member node) { | 866 Member defaultMember(Member node) { |
| 866 if (!shaker.isMemberBodyUsed(node)) { | 867 if (!shaker.isMemberBodyUsed(node)) { |
| 867 if (!shaker.isMemberOverridden(node)) { | 868 if (!shaker.isMemberOverridden(node)) { |
| 869 node.canonicalName.unlink(); |
| 868 return null; | 870 return null; |
| 869 } | 871 } |
| 870 if (node is Procedure) { | 872 if (node is Procedure) { |
| 871 // Remove body of unused member. | 873 // Remove body of unused member. |
| 872 if (node.enclosingClass.isAbstract) { | 874 if (node.enclosingClass.isAbstract) { |
| 873 node.isAbstract = true; | 875 node.isAbstract = true; |
| 874 node.function.body = null; | 876 node.function.body = null; |
| 875 } else { | 877 } else { |
| 876 // If the enclosing class is not abstract, the method should still | 878 // If the enclosing class is not abstract, the method should still |
| 877 // have a body even if it can never be called. | 879 // have a body even if it can never be called. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 972 classNode == coreTypes.futureClass || | 974 classNode == coreTypes.futureClass || |
| 973 classNode == coreTypes.streamClass || | 975 classNode == coreTypes.streamClass || |
| 974 classNode == coreTypes.listClass || | 976 classNode == coreTypes.listClass || |
| 975 classNode == coreTypes.mapClass; | 977 classNode == coreTypes.mapClass; |
| 976 } | 978 } |
| 977 } | 979 } |
| 978 | 980 |
| 979 /// Exception that is thrown to stop the tree shaking analysis when a use | 981 /// Exception that is thrown to stop the tree shaking analysis when a use |
| 980 /// of `dart:mirrors` is found. | 982 /// of `dart:mirrors` is found. |
| 981 class _UsingMirrorsException {} | 983 class _UsingMirrorsException {} |
| OLD | NEW |