| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class NativeEmitter { | 7 class NativeEmitter { |
| 8 | 8 |
| 9 final Map<Element, ClassBuilder> cachedBuilders; | 9 final Map<Element, ClassBuilder> cachedBuilders; |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 * leaf class, and all instances of subclasses of `Element` are instances of | 54 * leaf class, and all instances of subclasses of `Element` are instances of |
| 55 * `Element`. | 55 * `Element`. |
| 56 * | 56 * |
| 57 * There is also a performance benefit (in addition to the obvious code size | 57 * There is also a performance benefit (in addition to the obvious code size |
| 58 * benefit), due to how [getNativeInterceptor] works. Finding the interceptor | 58 * benefit), due to how [getNativeInterceptor] works. Finding the interceptor |
| 59 * of a leaf class in the hierarchy is more efficient that a non-leaf, so it | 59 * of a leaf class in the hierarchy is more efficient that a non-leaf, so it |
| 60 * improves performance when more classes can be treated as leaves. | 60 * improves performance when more classes can be treated as leaves. |
| 61 * | 61 * |
| 62 * [classes] contains native classes, mixin applications, and user subclasses | 62 * [classes] contains native classes, mixin applications, and user subclasses |
| 63 * of native classes. | 63 * of native classes. |
| 64 * |
| 65 * [interceptorClassesNeededByConstants] contains the interceptors that are |
| 66 * referenced by constants. |
| 67 * |
| 68 * [classesModifiedByEmitRTISupport] contains the list of classes that must |
| 69 * exist, because runtime-type support adds information to the class. |
| 64 */ | 70 */ |
| 65 Set<Class> prepareNativeClasses(List<Class> classes) { | 71 Set<Class> prepareNativeClasses(List<Class> classes, |
| 72 Set<ClassElement> interceptorClassesNeededByConstants, |
| 73 Set<ClassElement> classesModifiedByEmitRTISupport) { |
| 66 assert(classes.every((Class cls) => cls != null)); | 74 assert(classes.every((Class cls) => cls != null)); |
| 67 | 75 |
| 68 hasNativeClasses = classes.isNotEmpty; | 76 hasNativeClasses = classes.isNotEmpty; |
| 69 | 77 |
| 70 // Compute a pre-order traversal of the subclass forest. We actually want a | 78 // Compute a pre-order traversal of the subclass forest. We actually want a |
| 71 // post-order traversal but it is easier to compute the pre-order and use it | 79 // post-order traversal but it is easier to compute the pre-order and use it |
| 72 // in reverse. | 80 // in reverse. |
| 73 List<Class> preOrder = <Class>[]; | 81 List<Class> preOrder = <Class>[]; |
| 74 Set<Class> seen = new Set<Class>(); | 82 Set<Class> seen = new Set<Class>(); |
| 75 | 83 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 96 // that is not needed can be treated as a leaf class equivalent to some | 104 // that is not needed can be treated as a leaf class equivalent to some |
| 97 // needed class. | 105 // needed class. |
| 98 | 106 |
| 99 Set<Class> neededClasses = new Set<Class>(); | 107 Set<Class> neededClasses = new Set<Class>(); |
| 100 Set<Class> nonLeafClasses = new Set<Class>(); | 108 Set<Class> nonLeafClasses = new Set<Class>(); |
| 101 | 109 |
| 102 Map<Class, List<Class>> extensionPoints = computeExtensionPoints(preOrder); | 110 Map<Class, List<Class>> extensionPoints = computeExtensionPoints(preOrder); |
| 103 | 111 |
| 104 neededClasses.add(objectClass); | 112 neededClasses.add(objectClass); |
| 105 | 113 |
| 106 Set<ClassElement> neededByConstant = emitterTask | |
| 107 .computeInterceptorsReferencedFromConstants(); | |
| 108 Set<ClassElement> modifiedClasses = emitterTask.typeTestRegistry | |
| 109 .computeClassesModifiedByEmitRuntimeTypeSupport(); | |
| 110 | |
| 111 for (Class cls in preOrder.reversed) { | 114 for (Class cls in preOrder.reversed) { |
| 112 ClassElement classElement = cls.element; | 115 ClassElement classElement = cls.element; |
| 113 // Post-order traversal ensures we visit the subclasses before their | 116 // Post-order traversal ensures we visit the subclasses before their |
| 114 // superclass. This makes it easy to tell if a class is needed because a | 117 // superclass. This makes it easy to tell if a class is needed because a |
| 115 // subclass is needed. | 118 // subclass is needed. |
| 116 bool needed = false; | 119 bool needed = false; |
| 117 if (!cls.isNative) { | 120 if (!cls.isNative) { |
| 118 // Mixin applications (native+mixin) are non-native, so [classElement] | 121 // Mixin applications (native+mixin) are non-native, so [classElement] |
| 119 // has already been emitted as a regular class. Mark [classElement] as | 122 // has already been emitted as a regular class. Mark [classElement] as |
| 120 // 'needed' to ensure the native superclass is needed. | 123 // 'needed' to ensure the native superclass is needed. |
| 121 needed = true; | 124 needed = true; |
| 122 } else if (!isTrivialClass(cls)) { | 125 } else if (!isTrivialClass(cls)) { |
| 123 needed = true; | 126 needed = true; |
| 124 } else if (neededByConstant.contains(classElement)) { | 127 } else if (interceptorClassesNeededByConstants.contains(classElement)) { |
| 125 needed = true; | 128 needed = true; |
| 126 } else if (modifiedClasses.contains(classElement)) { | 129 } else if (classesModifiedByEmitRTISupport.contains(classElement)) { |
| 127 // TODO(9556): Remove this test when [emitRuntimeTypeSupport] no longer | 130 // TODO(9556): Remove this test when [emitRuntimeTypeSupport] no longer |
| 128 // adds information to a class prototype or constructor. | 131 // adds information to a class prototype or constructor. |
| 129 needed = true; | 132 needed = true; |
| 130 } else if (extensionPoints.containsKey(cls)) { | 133 } else if (extensionPoints.containsKey(cls)) { |
| 131 needed = true; | 134 needed = true; |
| 132 } | 135 } |
| 133 if (cls.isNative && | 136 if (cls.isNative && |
| 134 native.nativeTagsForcedNonLeaf(classElement)) { | 137 native.nativeTagsForcedNonLeaf(classElement)) { |
| 135 needed = true; | 138 needed = true; |
| 136 nonLeafClasses.add(cls); | 139 nonLeafClasses.add(cls); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 // used. We should also use an interceptor if the check can't be satisfied | 342 // used. We should also use an interceptor if the check can't be satisfied |
| 340 // by a native class in case we get a native instance that tries to spoof | 343 // by a native class in case we get a native instance that tries to spoof |
| 341 // the type info. i.e the criteria for whether or not to use an interceptor | 344 // the type info. i.e the criteria for whether or not to use an interceptor |
| 342 // is whether the receiver can be native, not the type of the test. | 345 // is whether the receiver can be native, not the type of the test. |
| 343 if (element == null || !element.isClass) return false; | 346 if (element == null || !element.isClass) return false; |
| 344 ClassElement cls = element; | 347 ClassElement cls = element; |
| 345 if (Elements.isNativeOrExtendsNative(cls)) return true; | 348 if (Elements.isNativeOrExtendsNative(cls)) return true; |
| 346 return isSupertypeOfNativeClass(element); | 349 return isSupertypeOfNativeClass(element); |
| 347 } | 350 } |
| 348 } | 351 } |
| OLD | NEW |