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

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

Issue 10837343: Unify private names treatment. (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 void renamePlaceholders( 9 void renamePlaceholders(
10 Compiler compiler, 10 Compiler compiler,
11 PlaceholderCollector placeholderCollector, 11 PlaceholderCollector placeholderCollector,
12 Map<Node, String> renames, 12 Map<Node, String> renames,
13 Map<LibraryElement, String> imports) { 13 Map<LibraryElement, String> imports) {
14 final Map<LibraryElement, Map<String, String>> renamed 14 final Map<LibraryElement, Map<String, String>> renamed
15 = new Map<LibraryElement, Map<String, String>>(); 15 = new Map<LibraryElement, Map<String, String>>();
16 final Set<String> usedTopLevelIdentifiers = new Set<String>(); 16 final Set<String> usedTopLevelIdentifiers = new Set<String>();
17 int privateNameCounter = 0;
18
19 String getName(LibraryElement library, String originalName, renamer) =>
20 renamed.putIfAbsent(library, () => <String>{})
21 .putIfAbsent(originalName, renamer);
22
23 String renamePrivateIdentifier(LibraryElement library, String id) =>
24 getName(library, id, () => '_${privateNameCounter++}${id}');
25 17
26 Generator topLevelGenerator = 18 Generator topLevelGenerator =
27 true ? conservativeGenerator : new MinifyingGenerator('ABCD').generate; 19 true ? conservativeGenerator : new MinifyingGenerator('ABCD').generate;
28 String generateUniqueName(name) { 20 String generateUniqueName(name) {
29 String newName = topLevelGenerator( 21 String newName = topLevelGenerator(
30 name, usedTopLevelIdentifiers.contains); 22 name, usedTopLevelIdentifiers.contains);
31 usedTopLevelIdentifiers.add(newName); 23 usedTopLevelIdentifiers.add(newName);
32 return newName; 24 return newName;
33 } 25 }
34 26
27 rename(library, originalName) =>
28 renamed.putIfAbsent(library, () => <String>{})
29 .putIfAbsent(originalName, () => generateUniqueName(originalName));
30
35 String renameElement(Element element) { 31 String renameElement(Element element) {
36 assert(element.isTopLevel()); 32 assert(element.isTopLevel());
37 // TODO(smok): Make sure that the new name does not conflict with existing 33 // TODO(smok): Make sure that the new name does not conflict with existing
38 // local identifiers. 34 // local identifiers.
39 String originalName = element.name.slowToString(); 35 String originalName = element.name.slowToString();
40 LibraryElement library = element.getLibrary(); 36 LibraryElement library = element.getLibrary();
41 if (isDartCoreLib(compiler, library)) { 37 if (isDartCoreLib(compiler, library)) {
42 final prefix = 38 final prefix =
43 imports.putIfAbsent(library, () => generateUniqueName('p')); 39 imports.putIfAbsent(library, () => generateUniqueName('p'));
44 return '$prefix.$originalName'; 40 return '$prefix.$originalName';
45 } 41 }
46 42
47 return getName(library, originalName, 43 return rename(library, originalName);
48 () => generateUniqueName(originalName));
49 } 44 }
50 45
51 renameNodes(Collection<Node> nodes, renamer) { 46 renameNodes(Collection<Node> nodes, renamer) {
52 for (Node node in nodes) { 47 for (Node node in nodes) {
53 renames[node] = renamer(node); 48 renames[node] = renamer(node);
54 } 49 }
55 } 50 }
56 51
57 renameNodes(placeholderCollector.nullNodes, (_) => ''); 52 renameNodes(placeholderCollector.nullNodes, (_) => '');
58 renameNodes(placeholderCollector.unresolvedNodes, 53 renameNodes(placeholderCollector.unresolvedNodes,
59 (_) => generateUniqueName('Unresolved')); 54 (_) => generateUniqueName('Unresolved'));
60 placeholderCollector.elementNodes.forEach( 55 placeholderCollector.elementNodes.forEach(
61 (Element element, Set<Node> nodes) { 56 (Element element, Set<Node> nodes) {
62 String renamedElement = renameElement(element); 57 String renamedElement = renameElement(element);
63 renameNodes(nodes, (_) => renamedElement); 58 renameNodes(nodes, (_) => renamedElement);
64 }); 59 });
65 placeholderCollector.localPlaceholders.forEach( 60 placeholderCollector.localPlaceholders.forEach(
66 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) { 61 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) {
67 // TODO(smok): Check for conflicts with class fields and take usages 62 // TODO(smok): Check for conflicts with class fields and take usages
68 // into account. 63 // into account.
69 localPlaceholders.forEach((LocalPlaceholder placeholder) { 64 localPlaceholders.forEach((LocalPlaceholder placeholder) {
70 renameNodes(placeholder.nodes, (_) => placeholder.identifier); 65 renameNodes(placeholder.nodes, (_) => placeholder.identifier);
71 }); 66 });
72 }); 67 });
73 placeholderCollector.privateNodes.forEach( 68 placeholderCollector.privateNodes.forEach(
74 (LibraryElement library, Set<Identifier> nodes) { 69 (LibraryElement library, Set<Identifier> nodes) {
75 renameNodes(nodes, (node) => 70 renameNodes(nodes, (node) =>
76 renamePrivateIdentifier(library, node.source.slowToString())); 71 rename(library, node.source.slowToString()));
77 }); 72 });
78 } 73 }
79 74
80 typedef String Generator(String originalName, bool isForbidden(String name)); 75 typedef String Generator(String originalName, bool isForbidden(String name));
81 76
82 /** Always tries to return original identifier name unless it is forbidden. */ 77 /** Always tries to return original identifier name unless it is forbidden. */
83 String conservativeGenerator( 78 String conservativeGenerator(
84 String originalName, bool isForbidden(String name)) { 79 String originalName, bool isForbidden(String name)) {
85 String newName = originalName; 80 String newName = originalName;
86 while (isForbidden(newName)) { 81 while (isForbidden(newName)) {
(...skipping 29 matching lines...) Expand all
116 int length = alphabet.length; 111 int length = alphabet.length;
117 StringBuffer resultBuilder = new StringBuffer(); 112 StringBuffer resultBuilder = new StringBuffer();
118 while (index >= length) { 113 while (index >= length) {
119 resultBuilder.add(alphabet[index % length]); 114 resultBuilder.add(alphabet[index % length]);
120 index ~/= length; 115 index ~/= length;
121 } 116 }
122 resultBuilder.add(alphabet[index]); 117 resultBuilder.add(alphabet[index]);
123 return resultBuilder.toString(); 118 return resultBuilder.toString();
124 } 119 }
125 } 120 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/dart_backend/placeholder_collector.dart ('k') | tests/language/language.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698