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

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 // Process local placeholders.
27 placeholderCollector.localPlaceholders.forEach(
28 (Element element, Map<Node, LocalPlaceholder> localPlaceholders) {
29 localPlaceholders.forEach((Node node, LocalPlaceholder placeholder) {
30 // TODO(smok): Check for conflicts with class fields and take usages
31 // into account.
32 renames[node] = placeholder.identifier;
33 });
34 });
35 // Process element placeholders.
36 renamePlaceholders(placeholderCollector.elementPlaceholders,
37 (ElementPlaceholder placeholder) => renameElement(placeholder.element));
38 // Process null placeholders.
39 renamePlaceholders(placeholderCollector.nullPlaceholders,
40 (NullPlaceholder placeholder) => '');
41 // Process private placeholders.
42 renamePlaceholders(placeholderCollector.privatePlaceholders,
43 (PrivatePlaceholder placeholder) =>
44 renamePrivateIdentifier(placeholder.library,
45 placeholder.node.source.slowToString()));
46 // Process unresolved placeholders.
47 renamePlaceholders(placeholderCollector.unresolvedPlaceholders,
48 (UnresolvedPlaceholder placeholder) =>
49 generateUniqueName('Unresolved'));
50 }
51
52 void renamePlaceholders(Map<Node, Placeholder> placeholders,
53 String rename(Placeholder placeholder)) {
54 placeholders.forEach((Node node, Placeholder placeholder) {
55 renames[node] = rename(placeholder);
56 });
23 } 57 }
24 58
25 // Renamer implementation. 59 // Renamer implementation.
26 String rename(Node node) { 60 String rename(Node node) => renames[node];
27 Placeholder placeholder = placeholders[node];
28 return (placeholder !== null) ? placeholder.rename(this) : null;
29 }
30 61
31 String getName(LibraryElement library, String originalName, renamer) => 62 String getName(LibraryElement library, String originalName, renamer) =>
32 renamed.putIfAbsent(library, () => <String>{}) 63 renamed.putIfAbsent(library, () => <String>{})
33 .putIfAbsent(originalName, renamer); 64 .putIfAbsent(originalName, renamer);
34 65
35 String renamePrivateIdentifier(LibraryElement library, String id) => 66 String renamePrivateIdentifier(LibraryElement library, String id) =>
36 getName(library, id, () => '_${privateNameCounter++}${id}'); 67 getName(library, id, () => '_${privateNameCounter++}${id}');
37 68
38 String generateUniqueName(name) { 69 String generateUniqueName(name) {
39 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name'; 70 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name';
40 usedTopLevelIdentifiers.add(name); 71 usedTopLevelIdentifiers.add(name);
41 return name; 72 return name;
42 } 73 }
43 74
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) { 75 String renameElement(Element element) {
50 assert(element.isTopLevel()); 76 assert(element.isTopLevel());
51 // TODO(smok): Make sure that the new name does not conflict with existing 77 // TODO(smok): Make sure that the new name does not conflict with existing
52 // local identifiers. 78 // local identifiers.
53 String originalName = element.name.slowToString(); 79 String originalName = element.name.slowToString();
54 LibraryElement library = element.getLibrary(); 80 LibraryElement library = element.getLibrary();
55 if (library === compiler.coreLibrary) return originalName; 81 if (library === compiler.coreLibrary) return originalName;
56 if (isDartCoreLib(compiler, library)) { 82 if (isDartCoreLib(compiler, library)) {
57 final prefix = 83 final prefix =
58 imports.putIfAbsent(library, () => generateUniqueName('p')); 84 imports.putIfAbsent(library, () => generateUniqueName('p'));
59 return '$prefix.$originalName'; 85 return '$prefix.$originalName';
60 } 86 }
61 87
62 return getName(library, originalName, 88 return getName(library, originalName,
63 () => generateUniqueName(originalName)); 89 () => generateUniqueName(originalName));
64 } 90 }
65 } 91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698