| 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 dart2js.mirrors_handler; | 5 library dart2js.mirrors_handler; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common/resolution.dart'; | 8 import '../common/resolution.dart'; |
| 9 import '../diagnostics/diagnostic_listener.dart'; | 9 import '../diagnostics/diagnostic_listener.dart'; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 * | 75 * |
| 76 * During resolution, we have to resort to matching elements against the | 76 * During resolution, we have to resort to matching elements against the |
| 77 * [MirrorsUsed] pattern, as we do not have a complete picture of the world, | 77 * [MirrorsUsed] pattern, as we do not have a complete picture of the world, |
| 78 * yet. | 78 * yet. |
| 79 */ | 79 */ |
| 80 bool _shouldIncludeElementDueToMirrors(Element element, | 80 bool _shouldIncludeElementDueToMirrors(Element element, |
| 81 {bool includedEnclosing}) { | 81 {bool includedEnclosing}) { |
| 82 return includedEnclosing || _backend.requiredByMirrorSystem(element); | 82 return includedEnclosing || _backend.requiredByMirrorSystem(element); |
| 83 } | 83 } |
| 84 | 84 |
| 85 /// Enqeue the constructor [ctor] if it is required for reflection. | 85 /// Enqueue the constructor [ctor] if it is required for reflection. |
| 86 /// | 86 /// |
| 87 /// [enclosingWasIncluded] provides a hint whether the enclosing element was | 87 /// [enclosingWasIncluded] provides a hint whether the enclosing element was |
| 88 /// needed for reflection. | 88 /// needed for reflection. |
| 89 void _enqueueReflectiveConstructor(ConstructorElement constructor, | 89 void _enqueueReflectiveConstructor(ConstructorElement constructor, |
| 90 {bool enclosingWasIncluded}) { | 90 {bool enclosingWasIncluded}) { |
| 91 if (_shouldIncludeElementDueToMirrors(constructor, | 91 if (_shouldIncludeElementDueToMirrors(constructor, |
| 92 includedEnclosing: enclosingWasIncluded)) { | 92 includedEnclosing: enclosingWasIncluded)) { |
| 93 _logEnqueueReflectiveAction(constructor); | 93 _logEnqueueReflectiveAction(constructor); |
| 94 ClassElement cls = constructor.declaration.enclosingClass; | 94 ClassElement cls = constructor.declaration.enclosingClass; |
| 95 impactBuilder | 95 impactBuilder |
| 96 .registerTypeUse(new TypeUse.mirrorInstantiation(cls.rawType)); | 96 .registerTypeUse(new TypeUse.mirrorInstantiation(cls.rawType)); |
| 97 impactBuilder | 97 impactBuilder |
| 98 .registerStaticUse(new StaticUse.foreignUse(constructor.declaration)); | 98 .registerStaticUse(new StaticUse.foreignUse(constructor.declaration)); |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 /// Enqeue the member [element] if it is required for reflection. | 102 /// Enqueue the member [element] if it is required for reflection. |
| 103 /// | 103 /// |
| 104 /// [enclosingWasIncluded] provides a hint whether the enclosing element was | 104 /// [enclosingWasIncluded] provides a hint whether the enclosing element was |
| 105 /// needed for reflection. | 105 /// needed for reflection. |
| 106 void _enqueueReflectiveMember(Element element, bool enclosingWasIncluded) { | 106 void _enqueueReflectiveMember(Element element, bool enclosingWasIncluded) { |
| 107 if (_shouldIncludeElementDueToMirrors(element, | 107 if (_shouldIncludeElementDueToMirrors(element, |
| 108 includedEnclosing: enclosingWasIncluded)) { | 108 includedEnclosing: enclosingWasIncluded)) { |
| 109 _logEnqueueReflectiveAction(element); | 109 _logEnqueueReflectiveAction(element); |
| 110 if (element.isTypedef) { | 110 if (element.isTypedef) { |
| 111 TypedefElement typedef = element; | 111 TypedefElement typedef = element; |
| 112 typedef.ensureResolved(_resolution); | 112 typedef.ensureResolved(_resolution); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 124 DynamicUse dynamicUse = new DynamicUse( | 124 DynamicUse dynamicUse = new DynamicUse( |
| 125 new Selector.setter( | 125 new Selector.setter( |
| 126 new Name(element.name, element.library, isSetter: true)), | 126 new Name(element.name, element.library, isSetter: true)), |
| 127 null); | 127 null); |
| 128 impactBuilder.registerDynamicUse(dynamicUse); | 128 impactBuilder.registerDynamicUse(dynamicUse); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 /// Enqeue the member [element] if it is required for reflection. | 134 /// Enqueue the member [element] if it is required for reflection. |
| 135 /// | 135 /// |
| 136 /// [enclosingWasIncluded] provides a hint whether the enclosing element was | 136 /// [enclosingWasIncluded] provides a hint whether the enclosing element was |
| 137 /// needed for reflection. | 137 /// needed for reflection. |
| 138 void _enqueueReflectiveElementsInClass( | 138 void _enqueueReflectiveElementsInClass( |
| 139 ClassElement cls, Iterable<ClassEntity> recents, | 139 ClassElement cls, Iterable<ClassEntity> recents, |
| 140 {bool enclosingWasIncluded}) { | 140 {bool enclosingWasIncluded}) { |
| 141 if (cls.library.isInternalLibrary || cls.isInjected) return; | 141 if (cls.library.isInternalLibrary || cls.isInjected) return; |
| 142 bool includeClass = _shouldIncludeElementDueToMirrors(cls, | 142 bool includeClass = _shouldIncludeElementDueToMirrors(cls, |
| 143 includedEnclosing: enclosingWasIncluded); | 143 includedEnclosing: enclosingWasIncluded); |
| 144 if (includeClass) { | 144 if (includeClass) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 156 cls.constructors.forEach((Element element) { | 156 cls.constructors.forEach((Element element) { |
| 157 _enqueueReflectiveConstructor(element, | 157 _enqueueReflectiveConstructor(element, |
| 158 enclosingWasIncluded: includeClass); | 158 enclosingWasIncluded: includeClass); |
| 159 }); | 159 }); |
| 160 cls.forEachClassMember((Member member) { | 160 cls.forEachClassMember((Member member) { |
| 161 _enqueueReflectiveMember(member.element, includeClass); | 161 _enqueueReflectiveMember(member.element, includeClass); |
| 162 }); | 162 }); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 /// Enqeue special classes that might not be visible by normal means or that | 166 /// Enqueue special classes that might not be visible by normal means or that |
| 167 /// would not normally be enqueued: | 167 /// would not normally be enqueued: |
| 168 /// | 168 /// |
| 169 /// [Closure] is treated specially as it is the superclass of all closures. | 169 /// [Closure] is treated specially as it is the superclass of all closures. |
| 170 /// Although it is in an internal library, we mark it as reflectable. Note | 170 /// Although it is in an internal library, we mark it as reflectable. Note |
| 171 /// that none of its methods are reflectable, unless reflectable by | 171 /// that none of its methods are reflectable, unless reflectable by |
| 172 /// inheritance. | 172 /// inheritance. |
| 173 void _enqueueReflectiveSpecialClasses() { | 173 void _enqueueReflectiveSpecialClasses() { |
| 174 Iterable<ClassElement> classes = _backend.classesRequiredForReflection; | 174 Iterable<ClassElement> classes = _backend.classesRequiredForReflection; |
| 175 for (ClassElement cls in classes) { | 175 for (ClassElement cls in classes) { |
| 176 if (_backend.referencedFromMirrorSystem(cls)) { | 176 if (_backend.referencedFromMirrorSystem(cls)) { |
| 177 _logEnqueueReflectiveAction(cls); | 177 _logEnqueueReflectiveAction(cls); |
| 178 cls.ensureResolved(_resolution); | 178 cls.ensureResolved(_resolution); |
| 179 impactBuilder | 179 impactBuilder |
| 180 .registerTypeUse(new TypeUse.mirrorInstantiation(cls.rawType)); | 180 .registerTypeUse(new TypeUse.mirrorInstantiation(cls.rawType)); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 | 184 |
| 185 /// Enqeue all local members of the library [lib] if they are required for | 185 /// Enqueue all local members of the library [lib] if they are required for |
| 186 /// reflection. | 186 /// reflection. |
| 187 void _enqueueReflectiveElementsInLibrary( | 187 void _enqueueReflectiveElementsInLibrary( |
| 188 LibraryElement lib, Iterable<ClassEntity> recents) { | 188 LibraryElement lib, Iterable<ClassEntity> recents) { |
| 189 bool includeLibrary = | 189 bool includeLibrary = |
| 190 _shouldIncludeElementDueToMirrors(lib, includedEnclosing: false); | 190 _shouldIncludeElementDueToMirrors(lib, includedEnclosing: false); |
| 191 lib.forEachLocalMember((Element member) { | 191 lib.forEachLocalMember((Element member) { |
| 192 if (member.isInjected) return; | 192 if (member.isInjected) return; |
| 193 if (member.isClass) { | 193 if (member.isClass) { |
| 194 ClassElement cls = member; | 194 ClassElement cls = member; |
| 195 cls.ensureResolved(_resolution); | 195 cls.ensureResolved(_resolution); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 /// usage through `MirrorsUsed`. | 244 /// usage through `MirrorsUsed`. |
| 245 // TODO(johnniwinther): Compute [WorldImpact] instead of enqueuing directly. | 245 // TODO(johnniwinther): Compute [WorldImpact] instead of enqueuing directly. |
| 246 void enqueueReflectiveStaticFields(Iterable<Element> elements) { | 246 void enqueueReflectiveStaticFields(Iterable<Element> elements) { |
| 247 if (hasEnqueuedReflectiveStaticFields) return; | 247 if (hasEnqueuedReflectiveStaticFields) return; |
| 248 hasEnqueuedReflectiveStaticFields = true; | 248 hasEnqueuedReflectiveStaticFields = true; |
| 249 for (Element element in elements) { | 249 for (Element element in elements) { |
| 250 _enqueueReflectiveMember(element, true); | 250 _enqueueReflectiveMember(element, true); |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 } | 253 } |
| OLD | NEW |