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 dart_backend; | 5 part of dart_backend; |
6 | 6 |
7 // TODO(ahe): This class is simply wrong. This backend should use | 7 // TODO(ahe): This class is simply wrong. This backend should use |
8 // elements when it can, not AST nodes. Perhaps a [Map<Element, | 8 // elements when it can, not AST nodes. Perhaps a [Map<Element, |
9 // TreeElements>] is what is needed. | 9 // TreeElements>] is what is needed. |
10 class ElementAst { | 10 class ElementAst { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 final bool stripAsserts; | 122 final bool stripAsserts; |
123 // TODO(antonm): make available from command-line options. | 123 // TODO(antonm): make available from command-line options. |
124 final bool outputAst = false; | 124 final bool outputAst = false; |
125 final Map<Node, String> renames; | 125 final Map<Node, String> renames; |
126 final Map<LibraryElement, String> imports; | 126 final Map<LibraryElement, String> imports; |
127 final Map<ClassNode, List<Node>> memberNodes; | 127 final Map<ClassNode, List<Node>> memberNodes; |
128 // TODO(zarah) Maybe change this to a command-line option. | 128 // TODO(zarah) Maybe change this to a command-line option. |
129 // Right now, it is set by the tests. | 129 // Right now, it is set by the tests. |
130 bool useMirrorHelperLibrary = false; | 130 bool useMirrorHelperLibrary = false; |
131 | 131 |
| 132 /// Initialized if the useMirrorHelperLibrary field is set. |
| 133 MirrorRenamer mirrorRenamer; |
| 134 |
| 135 /// Initialized when dart:mirrors is loaded if the useMirrorHelperLibrary |
| 136 /// field is set. |
| 137 LibraryElement mirrorHelperLibrary; |
| 138 /// Initialized when dart:mirrors is loaded if the useMirrorHelperLibrary |
| 139 /// field is set. |
| 140 FunctionElement mirrorHelperGetNameFunction; |
| 141 /// Initialized when dart:mirrors is loaded if the useMirrorHelperLibrary |
| 142 /// field is set. |
| 143 Element mirrorHelperSymbolsMap; |
| 144 |
132 Map<Element, TreeElements> get resolvedElements => | 145 Map<Element, TreeElements> get resolvedElements => |
133 compiler.enqueuer.resolution.resolvedElements; | 146 compiler.enqueuer.resolution.resolvedElements; |
134 | 147 |
135 /** | 148 /** |
136 * Tells whether it is safe to remove type declarations from variables, | 149 * Tells whether it is safe to remove type declarations from variables, |
137 * functions parameters. It becomes not safe if: | 150 * functions parameters. It becomes not safe if: |
138 * 1) TypeError is used somewhere in the code, | 151 * 1) TypeError is used somewhere in the code, |
139 * 2) The code has typedefs in right hand side of IS checks, | 152 * 2) The code has typedefs in right hand side of IS checks, |
140 * 3) The code has classes which extend typedefs, have type arguments typedefs | 153 * 3) The code has classes which extend typedefs, have type arguments typedefs |
141 * or type variable bounds typedefs. | 154 * or type variable bounds typedefs. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 270 } |
258 // The VM will automatically invoke the call method of objects | 271 // The VM will automatically invoke the call method of objects |
259 // that are invoked as functions. Make sure to not rename that. | 272 // that are invoked as functions. Make sure to not rename that. |
260 fixedMemberNames.add('call'); | 273 fixedMemberNames.add('call'); |
261 // TODO(antonm): TypeError.srcType and TypeError.dstType are defined in | 274 // TODO(antonm): TypeError.srcType and TypeError.dstType are defined in |
262 // runtime/lib/error.dart. Overall, all DartVM specific libs should be | 275 // runtime/lib/error.dart. Overall, all DartVM specific libs should be |
263 // accounted for. | 276 // accounted for. |
264 fixedMemberNames.add('srcType'); | 277 fixedMemberNames.add('srcType'); |
265 fixedMemberNames.add('dstType'); | 278 fixedMemberNames.add('dstType'); |
266 | 279 |
| 280 if (useMirrorHelperLibrary && compiler.mirrorsLibrary != null) { |
| 281 mirrorRenamer = new MirrorRenamer(compiler, this); |
| 282 } else { |
| 283 useMirrorHelperLibrary = false; |
| 284 } |
| 285 |
267 /** | 286 /** |
268 * Tells whether we should output given element. Corelib classes like | 287 * Tells whether we should output given element. Corelib classes like |
269 * Object should not be in the resulting code. | 288 * Object should not be in the resulting code. |
270 */ | 289 */ |
271 bool shouldOutput(Element element) { | 290 bool shouldOutput(Element element) { |
272 return !identical(element.kind, ElementKind.VOID) | 291 return (element.kind != ElementKind.VOID |
273 && isUserLibrary(element.getLibrary()) | 292 && isUserLibrary(element.getLibrary()) |
274 && !element.isSynthesized | 293 && !element.isSynthesized |
275 && element is !AbstractFieldElement; | 294 && element is !AbstractFieldElement) |
| 295 || element.getLibrary() == mirrorHelperLibrary; |
276 } | 296 } |
277 | 297 |
278 final elementAsts = new Map<Element, ElementAst>(); | 298 final elementAsts = new Map<Element, ElementAst>(); |
279 | 299 |
280 parse(element) => element.parseNode(compiler); | 300 parse(element) => element.parseNode(compiler); |
281 | 301 |
282 Set<Element> topLevelElements = new Set<Element>(); | 302 Set<Element> topLevelElements = new Set<Element>(); |
283 Map<ClassElement, Set<Element>> classMembers = | 303 Map<ClassElement, Set<Element>> classMembers = |
284 new Map<ClassElement, Set<Element>>(); | 304 new Map<ClassElement, Set<Element>>(); |
285 | 305 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 new ElementAst(constructor.cachedNode, new TreeElementMapping(null)); | 410 new ElementAst(constructor.cachedNode, new TreeElementMapping(null)); |
391 } | 411 } |
392 | 412 |
393 // Create all necessary placeholders. | 413 // Create all necessary placeholders. |
394 PlaceholderCollector collector = | 414 PlaceholderCollector collector = |
395 new PlaceholderCollector(compiler, fixedMemberNames, elementAsts); | 415 new PlaceholderCollector(compiler, fixedMemberNames, elementAsts); |
396 // Add synthesizedIdentifier to set of unresolved names to rename it to | 416 // Add synthesizedIdentifier to set of unresolved names to rename it to |
397 // some unused identifier. | 417 // some unused identifier. |
398 collector.unresolvedNodes.add(synthesizedIdentifier); | 418 collector.unresolvedNodes.add(synthesizedIdentifier); |
399 makePlaceholders(element) { | 419 makePlaceholders(element) { |
| 420 bool oldUseHelper = useMirrorHelperLibrary; |
| 421 useMirrorHelperLibrary = (useMirrorHelperLibrary |
| 422 && element.getLibrary() != mirrorHelperLibrary); |
400 collector.collect(element); | 423 collector.collect(element); |
| 424 useMirrorHelperLibrary = oldUseHelper; |
| 425 |
401 if (element.isClass()) { | 426 if (element.isClass()) { |
402 classMembers[element].forEach(makePlaceholders); | 427 classMembers[element].forEach(makePlaceholders); |
403 } | 428 } |
404 } | 429 } |
405 topLevelElements.forEach(makePlaceholders); | 430 topLevelElements.forEach(makePlaceholders); |
406 // Create renames. | 431 // Create renames. |
407 bool shouldCutDeclarationTypes = forceStripTypes | 432 bool shouldCutDeclarationTypes = forceStripTypes |
408 || (compiler.enableMinification | 433 || (compiler.enableMinification |
409 && isSafeToRemoveTypeDeclarations(classMembers)); | 434 && isSafeToRemoveTypeDeclarations(classMembers)); |
410 renamePlaceholders( | 435 renamePlaceholders( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 topLevelNodes.add(elementAsts[element].ast); | 468 topLevelNodes.add(elementAsts[element].ast); |
444 if (element.isClass() && !element.isMixinApplication) { | 469 if (element.isClass() && !element.isMixinApplication) { |
445 final members = <Node>[]; | 470 final members = <Node>[]; |
446 for (final member in sortedClassMembers[element]) { | 471 for (final member in sortedClassMembers[element]) { |
447 members.add(elementAsts[member].ast); | 472 members.add(elementAsts[member].ast); |
448 } | 473 } |
449 memberNodes[elementAsts[element].ast] = members; | 474 memberNodes[elementAsts[element].ast] = members; |
450 } | 475 } |
451 } | 476 } |
452 | 477 |
453 if (useMirrorHelperLibrary && compiler.mirrorsLibrary != null) { | 478 if (useMirrorHelperLibrary) { |
454 MirrorRenamer.addMirrorHelperImport(imports); | 479 mirrorRenamer.addRenames(renames, topLevelNodes); |
455 } | 480 } |
456 | 481 |
457 final unparser = new EmitterUnparser(renames); | 482 final unparser = new EmitterUnparser(renames); |
458 emitCode(unparser, imports, topLevelNodes, memberNodes); | 483 emitCode(unparser, imports, topLevelNodes, memberNodes); |
459 compiler.assembledCode = unparser.result; | 484 compiler.assembledCode = unparser.result; |
460 | 485 |
461 // Output verbose info about size ratio of resulting bundle to all | 486 // Output verbose info about size ratio of resulting bundle to all |
462 // referenced non-platform sources. | 487 // referenced non-platform sources. |
463 logResultBundleSizeInfo(topLevelElements); | 488 logResultBundleSizeInfo(topLevelElements); |
464 } | 489 } |
465 | 490 |
466 void logResultBundleSizeInfo(Set<Element> topLevelElements) { | 491 void logResultBundleSizeInfo(Set<Element> topLevelElements) { |
467 Iterable<LibraryElement> referencedLibraries = | 492 Iterable<LibraryElement> referencedLibraries = |
468 compiler.libraries.values.where(isUserLibrary); | 493 compiler.libraries.values.where(isUserLibrary); |
469 // Sum total size of scripts in each referenced library. | 494 // Sum total size of scripts in each referenced library. |
470 int nonPlatformSize = 0; | 495 int nonPlatformSize = 0; |
471 for (LibraryElement lib in referencedLibraries) { | 496 for (LibraryElement lib in referencedLibraries) { |
472 for (CompilationUnitElement compilationUnit in lib.compilationUnits) { | 497 for (CompilationUnitElement compilationUnit in lib.compilationUnits) { |
473 nonPlatformSize += compilationUnit.script.text.length; | 498 nonPlatformSize += compilationUnit.script.text.length; |
474 } | 499 } |
475 } | 500 } |
476 int percentage = compiler.assembledCode.length * 100 ~/ nonPlatformSize; | 501 int percentage = compiler.assembledCode.length * 100 ~/ nonPlatformSize; |
477 log('Total used non-platform files size: ${nonPlatformSize} bytes, ' | 502 log('Total used non-platform files size: ${nonPlatformSize} bytes, ' |
478 'bundle size: ${compiler.assembledCode.length} bytes (${percentage}%)'); | 503 'bundle size: ${compiler.assembledCode.length} bytes (${percentage}%)'); |
479 } | 504 } |
480 | 505 |
481 log(String message) => compiler.log('[DartBackend] $message'); | 506 log(String message) => compiler.log('[DartBackend] $message'); |
482 | 507 |
| 508 void onLibraryLoaded(LibraryElement library, Uri uri) { |
| 509 if (useMirrorHelperLibrary && library == compiler.mirrorsLibrary) { |
| 510 mirrorHelperLibrary = compiler.scanBuiltinLibrary( |
| 511 MirrorRenamer.MIRROR_HELPER_LIBRARY_NAME); |
| 512 mirrorHelperGetNameFunction = mirrorHelperLibrary.find( |
| 513 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION)); |
| 514 mirrorHelperSymbolsMap = mirrorHelperLibrary.find( |
| 515 const SourceString(MirrorRenamer.MIRROR_HELPER_SYMBOLS_MAP_NAME)); |
| 516 } |
| 517 } |
| 518 |
483 void registerStaticSend(Element element, Node node) { | 519 void registerStaticSend(Element element, Node node) { |
484 if (useMirrorHelperLibrary && compiler.mirrorsLibrary != null) { | 520 if (useMirrorHelperLibrary) { |
485 MirrorRenamer.handleStaticSend(renames, element, node, compiler); | 521 mirrorRenamer.registerStaticSend(element, node); |
| 522 } |
| 523 } |
| 524 |
| 525 void registerMirrorHelperElement(Element element, Node node) { |
| 526 if (element.getLibrary() == mirrorHelperLibrary) { |
| 527 mirrorRenamer.registerHelperElement(element, node); |
| 528 } |
| 529 } |
| 530 |
| 531 void registerStaticUse(Element element, Enqueuer enqueuer) { |
| 532 if (useMirrorHelperLibrary && |
| 533 element == compiler.mirrorSystemGetNameFunction) { |
| 534 enqueuer.addToWorkList(mirrorHelperGetNameFunction); |
486 } | 535 } |
487 } | 536 } |
488 } | 537 } |
489 | 538 |
490 class EmitterUnparser extends Unparser { | 539 class EmitterUnparser extends Unparser { |
491 final Map<Node, String> renames; | 540 final Map<Node, String> renames; |
492 | 541 |
493 EmitterUnparser(this.renames); | 542 EmitterUnparser(this.renames); |
494 | 543 |
495 visit(Node node) { | 544 visit(Node node) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 } | 612 } |
564 | 613 |
565 compareElements(e0, e1) { | 614 compareElements(e0, e1) { |
566 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1); | 615 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1); |
567 if (result != 0) return result; | 616 if (result != 0) return result; |
568 return compareBy((e) => e.position().charOffset)(e0, e1); | 617 return compareBy((e) => e.position().charOffset)(e0, e1); |
569 } | 618 } |
570 | 619 |
571 List<Element> sortElements(Iterable<Element> elements) => | 620 List<Element> sortElements(Iterable<Element> elements) => |
572 sorted(elements, compareElements); | 621 sorted(elements, compareElements); |
OLD | NEW |