| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // Test of Compiler.forgetElement. | 5 // Test of Compiler.forgetElement. |
| 6 library trydart.forget_element_test; | 6 library trydart.forget_element_test; |
| 7 | 7 |
| 8 import 'package:compiler/implementation/elements/elements.dart' show | 8 import 'package:compiler/implementation/elements/elements.dart' show |
| 9 AstElement, | 9 AstElement, |
| 10 Element, | 10 Element, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 final int expectedClosureCount; | 37 final int expectedClosureCount; |
| 38 | 38 |
| 39 final int expectedMetadataCount; | 39 final int expectedMetadataCount; |
| 40 | 40 |
| 41 final int expectedConstantCount; | 41 final int expectedConstantCount; |
| 42 | 42 |
| 43 final int expectedInitialValueCount; | 43 final int expectedInitialValueCount; |
| 44 | 44 |
| 45 final int expectedInitialDartValueCount; | 45 final int expectedInitialDartValueCount; |
| 46 | 46 |
| 47 final int additionalClosureClassMaps; |
| 48 |
| 47 ForgetElementTestCase( | 49 ForgetElementTestCase( |
| 48 String source, | 50 String source, |
| 49 {int closureCount: 0, | 51 {int closureCount: 0, |
| 50 int metadataCount: 0, | 52 int metadataCount: 0, |
| 51 int constantCount: 0, | 53 int constantCount: 0, |
| 52 int initialValueCount: 0, | 54 int initialValueCount: 0, |
| 53 int initialDartValueCount: null}) | 55 int initialDartValueCount: null, |
| 56 this.additionalClosureClassMaps: 0}) |
| 54 : this.expectedClosureCount = closureCount, | 57 : this.expectedClosureCount = closureCount, |
| 55 this.expectedMetadataCount = metadataCount, | 58 this.expectedMetadataCount = metadataCount, |
| 56 this.expectedConstantCount = constantCount, | 59 this.expectedConstantCount = constantCount, |
| 57 this.expectedInitialValueCount = initialValueCount, | 60 this.expectedInitialValueCount = initialValueCount, |
| 58 // Sometimes these numbers aren't the same. Appears to happen with | 61 // Sometimes these numbers aren't the same. Appears to happen with |
| 59 // non-const fields, because those aren't compile-time constants in the | 62 // non-const fields, because those aren't compile-time constants in the |
| 60 // strict language specification sense. | 63 // strict language specification sense. |
| 61 this.expectedInitialDartValueCount = (initialDartValueCount == null) | 64 this.expectedInitialDartValueCount = (initialDartValueCount == null) |
| 62 ? initialValueCount : initialDartValueCount, | 65 ? initialValueCount : initialDartValueCount, |
| 63 super(source); | 66 super(source); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 86 // values. | 89 // values. |
| 87 Expect.equals( | 90 Expect.equals( |
| 88 expectedInitialValueCount, | 91 expectedInitialValueCount, |
| 89 elementsWithJsInitialValuesIn(library).length, | 92 elementsWithJsInitialValuesIn(library).length, |
| 90 'number of fields with initial values (JS)'); | 93 'number of fields with initial values (JS)'); |
| 91 Expect.equals( | 94 Expect.equals( |
| 92 expectedInitialDartValueCount, | 95 expectedInitialDartValueCount, |
| 93 elementsWithDartInitialValuesIn(library).length, | 96 elementsWithDartInitialValuesIn(library).length, |
| 94 'number of fields with initial values (Dart)'); | 97 'number of fields with initial values (Dart)'); |
| 95 | 98 |
| 99 // Check that the compiler has recorded the expected number of closure |
| 100 // class maps. There's always at least one, from main. Each top-level |
| 101 // element also seems to induce one. |
| 102 Expect.equals( |
| 103 expectedClosureCount + additionalClosureClassMaps, |
| 104 closureClassMapsIn(library).length - 1, |
| 105 'closure class map count ${closureClassMapsIn(library)}'); |
| 106 |
| 107 |
| 96 // Forget about all elements. | 108 // Forget about all elements. |
| 97 library.forEachLocalMember(compiler.forgetElement); | 109 library.forEachLocalMember(compiler.forgetElement); |
| 98 | 110 |
| 99 // Check that all the closures were forgotten. | 111 // Check that all the closures were forgotten. |
| 100 Expect.isTrue(closuresInLibrary(library).isEmpty, 'closures'); | 112 Expect.isTrue(closuresInLibrary(library).isEmpty, 'closures'); |
| 101 | 113 |
| 102 // Check that the metadata annotations were forgotten. | 114 // Check that the metadata annotations were forgotten. |
| 103 Expect.isTrue(metadataInLibrary(library).isEmpty, 'metadata'); | 115 Expect.isTrue(metadataInLibrary(library).isEmpty, 'metadata'); |
| 104 | 116 |
| 105 // Check that the constants were forgotten. | 117 // Check that the constants were forgotten. |
| 106 Expect.isTrue(constantsIn(library).isEmpty, 'constants'); | 118 Expect.isTrue(constantsIn(library).isEmpty, 'constants'); |
| 107 | 119 |
| 108 // Check that initial values were forgotten. | 120 // Check that initial values were forgotten. |
| 109 Expect.isTrue( | 121 Expect.isTrue( |
| 110 elementsWithJsInitialValuesIn(library).isEmpty, | 122 elementsWithJsInitialValuesIn(library).isEmpty, |
| 111 'fields with initial values (JS)'); | 123 'fields with initial values (JS)'); |
| 112 Expect.isTrue( | 124 Expect.isTrue( |
| 113 elementsWithDartInitialValuesIn(library).isEmpty, | 125 elementsWithDartInitialValuesIn(library).isEmpty, |
| 114 'fields with initial values (Dart)'); | 126 'fields with initial values (Dart)'); |
| 127 |
| 128 // Check that closure class maps were forgotten. |
| 129 Expect.isTrue(closureClassMapsIn(library).isEmpty, 'closure class maps'); |
| 115 }); | 130 }); |
| 116 | 131 |
| 117 Iterable closuresInLibrary(LibraryElement library) { | 132 Iterable closuresInLibrary(LibraryElement library) { |
| 118 return compiler.enqueuer.resolution.universe.allClosures.where( | 133 return compiler.enqueuer.resolution.universe.allClosures.where( |
| 119 (LocalFunctionElement closure) => closure.library == library); | 134 (LocalFunctionElement closure) => closure.library == library); |
| 120 } | 135 } |
| 121 | 136 |
| 122 Iterable metadataInLibrary(LibraryElement library) { | 137 Iterable metadataInLibrary(LibraryElement library) { |
| 123 JavaScriptBackend backend = compiler.backend; | 138 JavaScriptBackend backend = compiler.backend; |
| 124 return backend.constants.metadataConstantMap.keys.where( | 139 return backend.constants.metadataConstantMap.keys.where( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 179 } |
| 165 | 180 |
| 166 Iterable elementsWithDartInitialValuesIn(LibraryElement library) { | 181 Iterable elementsWithDartInitialValuesIn(LibraryElement library) { |
| 167 JavaScriptBackend backend = compiler.backend; | 182 JavaScriptBackend backend = compiler.backend; |
| 168 DartConstantCompiler dartConstants = | 183 DartConstantCompiler dartConstants = |
| 169 backend.constantCompilerTask.dartConstantCompiler; | 184 backend.constantCompilerTask.dartConstantCompiler; |
| 170 return dartConstants.initialVariableValues.keys.where( | 185 return dartConstants.initialVariableValues.keys.where( |
| 171 (VariableElement element) => element.library == library); | 186 (VariableElement element) => element.library == library); |
| 172 } | 187 } |
| 173 | 188 |
| 189 Iterable closureClassMapsIn(LibraryElement library) { |
| 190 Map cache = compiler.closureToClassMapper.closureMappingCache; |
| 191 return nodesIn(library).where((node) => cache[node] != null); |
| 192 } |
| 174 } | 193 } |
| 175 | 194 |
| 176 class NodeCollector extends tree.Visitor { | 195 class NodeCollector extends tree.Visitor { |
| 177 final List<tree.Node> nodes = <tree.Node>[]; | 196 final List<tree.Node> nodes = <tree.Node>[]; |
| 178 | 197 |
| 179 void visitNode(tree.Node node) { | 198 void visitNode(tree.Node node) { |
| 180 nodes.add(node); | 199 nodes.add(node); |
| 181 node.visitChildren(this); | 200 node.visitChildren(this); |
| 182 } | 201 } |
| 183 } | 202 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 'main() => (() => (() => const Constant())())(); $CONSTANT_CLASS', | 288 'main() => (() => (() => const Constant())())(); $CONSTANT_CLASS', |
| 270 constantCount: 1, | 289 constantCount: 1, |
| 271 closureCount: 2), | 290 closureCount: 2), |
| 272 | 291 |
| 273 // Test that a constant in a top-level variable initializer is | 292 // Test that a constant in a top-level variable initializer is |
| 274 // discarded correctly. | 293 // discarded correctly. |
| 275 new ForgetElementTestCase( | 294 new ForgetElementTestCase( |
| 276 'main() => x; var x = const Constant(); $CONSTANT_CLASS', | 295 'main() => x; var x = const Constant(); $CONSTANT_CLASS', |
| 277 constantCount: 1, | 296 constantCount: 1, |
| 278 initialValueCount: 1, | 297 initialValueCount: 1, |
| 279 initialDartValueCount: 0), | 298 initialDartValueCount: 0, |
| 299 additionalClosureClassMaps: 1), |
| 280 | 300 |
| 281 // Test that a constant in a parameter initializer is discarded | 301 // Test that a constant in a parameter initializer is discarded |
| 282 // correctly. | 302 // correctly. |
| 283 new ForgetElementTestCase( | 303 new ForgetElementTestCase( |
| 284 'main([x = const Constant()]) => x; $CONSTANT_CLASS', | 304 'main([x = const Constant()]) => x; $CONSTANT_CLASS', |
| 285 constantCount: 1, | 305 constantCount: 1, |
| 286 initialValueCount: 1), | 306 initialValueCount: 1), |
| 287 | 307 |
| 288 // Test that a constant in a parameter initializer is discarded | 308 // Test that a constant in a parameter initializer is discarded |
| 289 // correctly (nested function). | 309 // correctly (nested function). |
| 290 new ForgetElementTestCase( | 310 new ForgetElementTestCase( |
| 291 'main() => (([x = const Constant()]) => x)(); $CONSTANT_CLASS', | 311 'main() => (([x = const Constant()]) => x)(); $CONSTANT_CLASS', |
| 292 closureCount: 1, | 312 closureCount: 1, |
| 293 constantCount: 1, | 313 constantCount: 1, |
| 294 initialValueCount: 1), | 314 initialValueCount: 1), |
| 295 | 315 |
| 296 // Test that a constant in a parameter initializer is discarded | 316 // Test that a constant in a parameter initializer is discarded |
| 297 // correctly (deeply nested function). | 317 // correctly (deeply nested function). |
| 298 new ForgetElementTestCase( | 318 new ForgetElementTestCase( |
| 299 'main() => (() => (([x = const Constant()]) => x)())();' | 319 'main() => (() => (([x = const Constant()]) => x)())();' |
| 300 ' $CONSTANT_CLASS', | 320 ' $CONSTANT_CLASS', |
| 301 closureCount: 2, | 321 closureCount: 2, |
| 302 constantCount: 1, | 322 constantCount: 1, |
| 303 initialValueCount: 1), | 323 initialValueCount: 1), |
| 304 | 324 |
| 305 ]..addAll(assertUnimplementedLocalMetadata()); | 325 ]..addAll(assertUnimplementedLocalMetadata()); |
| OLD | NEW |