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