Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(971)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/dart_backend/backend.dart

Issue 22791002: Add renames in output when using the mirror helper library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changed solution approach. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 } 257 }
258 // The VM will automatically invoke the call method of objects 258 // The VM will automatically invoke the call method of objects
259 // that are invoked as functions. Make sure to not rename that. 259 // that are invoked as functions. Make sure to not rename that.
260 fixedMemberNames.add('call'); 260 fixedMemberNames.add('call');
261 // TODO(antonm): TypeError.srcType and TypeError.dstType are defined in 261 // TODO(antonm): TypeError.srcType and TypeError.dstType are defined in
262 // runtime/lib/error.dart. Overall, all DartVM specific libs should be 262 // runtime/lib/error.dart. Overall, all DartVM specific libs should be
263 // accounted for. 263 // accounted for.
264 fixedMemberNames.add('srcType'); 264 fixedMemberNames.add('srcType');
265 fixedMemberNames.add('dstType'); 265 fixedMemberNames.add('dstType');
266 266
267 if (useMirrorHelperLibrary) {
268 useMirrorHelperLibrary = compiler.mirrorsLibrary != null;
269 }
270
267 /** 271 /**
268 * Tells whether we should output given element. Corelib classes like 272 * Tells whether we should output given element. Corelib classes like
269 * Object should not be in the resulting code. 273 * Object should not be in the resulting code.
270 */ 274 */
271 bool shouldOutput(Element element) { 275 bool shouldOutput(Element element) {
272 return !identical(element.kind, ElementKind.VOID) 276 return (!identical(element.kind, ElementKind.VOID)
ahe 2013/08/19 15:34:06 Since you're changing this line, would you mind up
zarah 2013/08/20 14:08:17 Done.
273 && isUserLibrary(element.getLibrary()) 277 && isUserLibrary(element.getLibrary())
274 && !element.isSynthesized 278 && !element.isSynthesized
275 && element is !AbstractFieldElement; 279 && element is !AbstractFieldElement)
280 || element == compiler.mirrorHelperFunction;
276 } 281 }
277 282
278 final elementAsts = new Map<Element, ElementAst>(); 283 final elementAsts = new Map<Element, ElementAst>();
279 284
280 parse(element) => element.parseNode(compiler); 285 parse(element) => element.parseNode(compiler);
281 286
282 Set<Element> topLevelElements = new Set<Element>(); 287 Set<Element> topLevelElements = new Set<Element>();
283 Map<ClassElement, Set<Element>> classMembers = 288 Map<ClassElement, Set<Element>> classMembers =
284 new Map<ClassElement, Set<Element>>(); 289 new Map<ClassElement, Set<Element>>();
285 290
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 new ElementAst(constructor.cachedNode, new TreeElementMapping(null)); 395 new ElementAst(constructor.cachedNode, new TreeElementMapping(null));
391 } 396 }
392 397
393 // Create all necessary placeholders. 398 // Create all necessary placeholders.
394 PlaceholderCollector collector = 399 PlaceholderCollector collector =
395 new PlaceholderCollector(compiler, fixedMemberNames, elementAsts); 400 new PlaceholderCollector(compiler, fixedMemberNames, elementAsts);
396 // Add synthesizedIdentifier to set of unresolved names to rename it to 401 // Add synthesizedIdentifier to set of unresolved names to rename it to
397 // some unused identifier. 402 // some unused identifier.
398 collector.unresolvedNodes.add(synthesizedIdentifier); 403 collector.unresolvedNodes.add(synthesizedIdentifier);
399 makePlaceholders(element) { 404 makePlaceholders(element) {
400 collector.collect(element); 405 if (useMirrorHelperLibrary &&
406 element.getLibrary() == compiler.mirrorHelperLibrary) {
407 useMirrorHelperLibrary = false;
408 collector.collect(element);
409 useMirrorHelperLibrary = true;
ahe 2013/08/19 15:34:06 I would store the old value. It is always good whe
zarah 2013/08/20 14:08:17 Done.
410 } else {
411 collector.collect(element);
412 }
401 if (element.isClass()) { 413 if (element.isClass()) {
402 classMembers[element].forEach(makePlaceholders); 414 classMembers[element].forEach(makePlaceholders);
403 } 415 }
404 } 416 }
405 topLevelElements.forEach(makePlaceholders); 417 topLevelElements.forEach(makePlaceholders);
406 // Create renames. 418 // Create renames.
407 bool shouldCutDeclarationTypes = forceStripTypes 419 bool shouldCutDeclarationTypes = forceStripTypes
408 || (compiler.enableMinification 420 || (compiler.enableMinification
409 && isSafeToRemoveTypeDeclarations(classMembers)); 421 && isSafeToRemoveTypeDeclarations(classMembers));
410 renamePlaceholders( 422 renamePlaceholders(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 topLevelNodes.add(elementAsts[element].ast); 455 topLevelNodes.add(elementAsts[element].ast);
444 if (element.isClass() && !element.isMixinApplication) { 456 if (element.isClass() && !element.isMixinApplication) {
445 final members = <Node>[]; 457 final members = <Node>[];
446 for (final member in sortedClassMembers[element]) { 458 for (final member in sortedClassMembers[element]) {
447 members.add(elementAsts[member].ast); 459 members.add(elementAsts[member].ast);
448 } 460 }
449 memberNodes[elementAsts[element].ast] = members; 461 memberNodes[elementAsts[element].ast] = members;
450 } 462 }
451 } 463 }
452 464
453 if (useMirrorHelperLibrary && compiler.mirrorsLibrary != null) { 465 if (useMirrorHelperLibrary) {
454 MirrorRenamer.addMirrorHelperImport(imports); 466 MirrorRenamer.addRenames(renames, topLevelNodes, compiler);
455 } 467 }
456 468
457 final unparser = new EmitterUnparser(renames); 469 final unparser = new EmitterUnparser(renames);
458 emitCode(unparser, imports, topLevelNodes, memberNodes); 470 emitCode(unparser, imports, topLevelNodes, memberNodes);
459 compiler.assembledCode = unparser.result; 471 compiler.assembledCode = unparser.result;
460 472
461 // Output verbose info about size ratio of resulting bundle to all 473 // Output verbose info about size ratio of resulting bundle to all
462 // referenced non-platform sources. 474 // referenced non-platform sources.
463 logResultBundleSizeInfo(topLevelElements); 475 logResultBundleSizeInfo(topLevelElements);
464 } 476 }
465 477
466 void logResultBundleSizeInfo(Set<Element> topLevelElements) { 478 void logResultBundleSizeInfo(Set<Element> topLevelElements) {
467 Iterable<LibraryElement> referencedLibraries = 479 Iterable<LibraryElement> referencedLibraries =
468 compiler.libraries.values.where(isUserLibrary); 480 compiler.libraries.values.where(isUserLibrary);
469 // Sum total size of scripts in each referenced library. 481 // Sum total size of scripts in each referenced library.
470 int nonPlatformSize = 0; 482 int nonPlatformSize = 0;
471 for (LibraryElement lib in referencedLibraries) { 483 for (LibraryElement lib in referencedLibraries) {
472 for (CompilationUnitElement compilationUnit in lib.compilationUnits) { 484 for (CompilationUnitElement compilationUnit in lib.compilationUnits) {
473 nonPlatformSize += compilationUnit.script.text.length; 485 nonPlatformSize += compilationUnit.script.text.length;
474 } 486 }
475 } 487 }
476 int percentage = compiler.assembledCode.length * 100 ~/ nonPlatformSize; 488 int percentage = compiler.assembledCode.length * 100 ~/ nonPlatformSize;
477 log('Total used non-platform files size: ${nonPlatformSize} bytes, ' 489 log('Total used non-platform files size: ${nonPlatformSize} bytes, '
478 'bundle size: ${compiler.assembledCode.length} bytes (${percentage}%)'); 490 'bundle size: ${compiler.assembledCode.length} bytes (${percentage}%)');
479 } 491 }
480 492
481 log(String message) => compiler.log('[DartBackend] $message'); 493 log(String message) => compiler.log('[DartBackend] $message');
482 494
495 void onLibraryLoaded(LibraryElement library, Uri uri) {
496 if (useMirrorHelperLibrary && library == compiler.mirrorsLibrary) {
497 compiler.mirrorHelperLibrary = compiler.scanBuiltinLibrary(
498 MirrorRenamer.MIRROR_HELPER_LIBRARY_NAME);
499 compiler.mirrorHelperFunction = compiler.mirrorHelperLibrary.find(
500 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION));
501 }
502 }
483 void registerStaticSend(Element element, Node node) { 503 void registerStaticSend(Element element, Node node) {
ahe 2013/08/19 15:34:06 Add newline before this method.
zarah 2013/08/20 14:08:17 Done.
484 if (useMirrorHelperLibrary && compiler.mirrorsLibrary != null) { 504 if (useMirrorHelperLibrary) {
485 MirrorRenamer.handleStaticSend(renames, element, node, compiler); 505 MirrorRenamer.registerStaticSend(element, node, compiler);
486 } 506 }
487 } 507 }
508
509 void registerHelperFunction(Element element, Node node) {
510 if (element.getLibrary() == compiler.mirrorHelperLibrary) {
511 MirrorRenamer.registerHelperFunction(element, node, compiler);
512 }
513 }
514
515 void registerStaticUse(Element element, Enqueuer enqueuer) {
516 if (useMirrorHelperLibrary &&
517 element == compiler.mirrorSystemGetNameFunction) {
518 enqueuer.addToWorkList(compiler.mirrorHelperFunction);
519 }
ahe 2013/08/19 15:34:06 Indentation.
zarah 2013/08/20 14:08:17 Done.
520 }
488 } 521 }
489 522
490 class EmitterUnparser extends Unparser { 523 class EmitterUnparser extends Unparser {
491 final Map<Node, String> renames; 524 final Map<Node, String> renames;
492 525
493 EmitterUnparser(this.renames); 526 EmitterUnparser(this.renames);
494 527
495 visit(Node node) { 528 visit(Node node) {
496 if (node != null && renames.containsKey(node)) { 529 if (node != null && renames.containsKey(node)) {
497 sb.write(renames[node]); 530 sb.write(renames[node]);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 } 596 }
564 597
565 compareElements(e0, e1) { 598 compareElements(e0, e1) {
566 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1); 599 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1);
567 if (result != 0) return result; 600 if (result != 0) return result;
568 return compareBy((e) => e.position().charOffset)(e0, e1); 601 return compareBy((e) => e.position().charOffset)(e0, e1);
569 } 602 }
570 603
571 List<Element> sortElements(Iterable<Element> elements) => 604 List<Element> sortElements(Iterable<Element> elements) =>
572 sorted(elements, compareElements); 605 sorted(elements, compareElements);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698