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

Side by Side Diff: lib/compiler/implementation/dart_backend/renamer.dart

Issue 10836261: dart2dart Preproces placeholders instead of renaming them lazily, (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 /** 5 /**
6 * Renames only top-level elements that would let to ambiguity if not renamed. 6 * Renames only top-level elements that would let to ambiguity if not renamed.
7 * TODO(smok): Make sure that top-level fields are correctly renamed. 7 * TODO(smok): Make sure that top-level fields are correctly renamed.
8 */ 8 */
9 class ConflictingRenamer { 9 class ConflictingRenamer {
10 final Compiler compiler; 10 final Compiler compiler;
11 final PlaceholderCollector placeholderCollector;
11 final Map<LibraryElement, Map<String, String>> renamed; 12 final Map<LibraryElement, Map<String, String>> renamed;
12 final Set<String> usedTopLevelIdentifiers; 13 final Set<String> usedTopLevelIdentifiers;
13 final Map<LibraryElement, String> imports; 14 final Map<LibraryElement, String> imports;
14 final Map<Node, Placeholder> placeholders; 15 final Map<Node, String> renames;
15 int privateNameCounter = 0; 16 int privateNameCounter = 0;
16 17
17 ConflictingRenamer(this.compiler, this.placeholders) : 18 ConflictingRenamer(this.compiler, this.placeholderCollector) :
18 renamed = new Map<LibraryElement, Map<String, String>>(), 19 renamed = new Map<LibraryElement, Map<String, String>>(),
19 usedTopLevelIdentifiers = new Set<String>(), 20 usedTopLevelIdentifiers = new Set<String>(),
20 imports = new Map<LibraryElement, String>() { 21 imports = new Map<LibraryElement, String>(),
22 renames = new Map<Node, String>() {
21 // Rename main() right now so that nobody takes its place. 23 // Rename main() right now so that nobody takes its place.
22 renameElement(compiler.mainApp.find(Compiler.MAIN)); 24 renameElement(compiler.mainApp.find(Compiler.MAIN));
25
26 placeholderCollector.nullNodes.forEach((Node node) {
27 renames[node] = '';
28 });
29 placeholderCollector.unresolvedNodes.forEach((Node node) {
30 renames[node] = generateUniqueName('Unresolved');
31 });
32 placeholderCollector.elementNodes.forEach(
33 (Element element, Set<Node> nodes) {
34 String renamedElement = renameElement(element);
35 nodes.forEach((Node node) {
36 renames[node] = renamedElement;
37 });
38 });
39 placeholderCollector.localPlaceholders.forEach(
40 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) {
41 // TODO(smok): Check for conflicts with class fields and take usages
42 // into account.
43 localPlaceholders.forEach((LocalPlaceholder placeholder) {
44 placeholder.nodes.forEach((Node node) {
45 renames[node] = placeholder.identifier;
46 });
47 });
48 });
49 placeholderCollector.privateNodes.forEach(
50 (LibraryElement library, Set<Identifier> nodes) {
51 nodes.forEach((Identifier node) {
52 renames[node] =
53 renamePrivateIdentifier(library, node.source.slowToString());
54 });
55 });
56 }
57
58 void renamePlaceholders(Map<Node, Placeholder> placeholders,
59 String rename(Placeholder placeholder)) {
60 placeholders.forEach((Node node, Placeholder placeholder) {
61 renames[node] = rename(placeholder);
62 });
23 } 63 }
24 64
25 // Renamer implementation. 65 // Renamer implementation.
26 String rename(Node node) { 66 String rename(Node node) => renames[node];
27 Placeholder placeholder = placeholders[node];
28 return (placeholder !== null) ? placeholder.rename(this) : null;
29 }
30 67
31 String getName(LibraryElement library, String originalName, renamer) => 68 String getName(LibraryElement library, String originalName, renamer) =>
32 renamed.putIfAbsent(library, () => <String>{}) 69 renamed.putIfAbsent(library, () => <String>{})
33 .putIfAbsent(originalName, renamer); 70 .putIfAbsent(originalName, renamer);
34 71
35 String renamePrivateIdentifier(LibraryElement library, String id) => 72 String renamePrivateIdentifier(LibraryElement library, String id) =>
36 getName(library, id, () => '_${privateNameCounter++}${id}'); 73 getName(library, id, () => '_${privateNameCounter++}${id}');
37 74
38 String generateUniqueName(name) { 75 String generateUniqueName(name) {
39 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name'; 76 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name';
40 usedTopLevelIdentifiers.add(name); 77 usedTopLevelIdentifiers.add(name);
41 return name; 78 return name;
42 } 79 }
43 80
44 // TODO(smok): Check for conflicts with class fields and take usages
45 // into account.
46 String renameLocalIdentifier(FunctionElement scope, String identifier) =>
47 identifier;
48
49 String renameElement(Element element) { 81 String renameElement(Element element) {
50 assert(element.isTopLevel()); 82 assert(element.isTopLevel());
51 // TODO(smok): Make sure that the new name does not conflict with existing 83 // TODO(smok): Make sure that the new name does not conflict with existing
52 // local identifiers. 84 // local identifiers.
53 String originalName = element.name.slowToString(); 85 String originalName = element.name.slowToString();
54 LibraryElement library = element.getLibrary(); 86 LibraryElement library = element.getLibrary();
55 if (library === compiler.coreLibrary) return originalName; 87 if (library === compiler.coreLibrary) return originalName;
56 if (isDartCoreLib(compiler, library)) { 88 if (isDartCoreLib(compiler, library)) {
57 final prefix = 89 final prefix =
58 imports.putIfAbsent(library, () => generateUniqueName('p')); 90 imports.putIfAbsent(library, () => generateUniqueName('p'));
59 return '$prefix.$originalName'; 91 return '$prefix.$originalName';
60 } 92 }
61 93
62 return getName(library, originalName, 94 return getName(library, originalName,
63 () => generateUniqueName(originalName)); 95 () => generateUniqueName(originalName));
64 } 96 }
65 } 97 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/dart_backend/placeholder_collector.dart ('k') | tests/compiler/dart2js/unparser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698