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

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

Issue 10834416: Rename elements after sorting. (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,
(...skipping 26 matching lines...) Expand all
37 if (isDartCoreLib(compiler, library)) { 37 if (isDartCoreLib(compiler, library)) {
38 final prefix = 38 final prefix =
39 imports.putIfAbsent(library, () => generateUniqueName('p')); 39 imports.putIfAbsent(library, () => generateUniqueName('p'));
40 return '$prefix.$originalName'; 40 return '$prefix.$originalName';
41 } 41 }
42 42
43 return rename(library, originalName); 43 return rename(library, originalName);
44 } 44 }
45 45
46 renameNodes(Collection<Node> nodes, renamer) { 46 renameNodes(Collection<Node> nodes, renamer) {
47 for (Node node in nodes) { 47 final comparison = compareBy((node) => node.getBeginToken().charOffset);
48 for (Node node in sorted(nodes, comparison)) {
48 renames[node] = renamer(node); 49 renames[node] = renamer(node);
49 } 50 }
50 } 51 }
51 52
53 sortedForEach(Map<Element, Dynamic> map, f) {
54 for (Element element in sortElements(map.getKeys())) {
55 f(element, map[element]);
56 }
57 }
58
52 renameNodes(placeholderCollector.nullNodes, (_) => ''); 59 renameNodes(placeholderCollector.nullNodes, (_) => '');
53 renameNodes(placeholderCollector.unresolvedNodes, 60 renameNodes(placeholderCollector.unresolvedNodes,
54 (_) => generateUniqueName('Unresolved')); 61 (_) => generateUniqueName('Unresolved'));
55 placeholderCollector.elementNodes.forEach( 62 sortedForEach(placeholderCollector.elementNodes, (element, nodes) {
56 (Element element, Set<Node> nodes) { 63 String renamedElement = renameElement(element);
57 String renamedElement = renameElement(element); 64 renameNodes(nodes, (_) => renamedElement);
58 renameNodes(nodes, (_) => renamedElement);
59 }); 65 });
60 placeholderCollector.localPlaceholders.forEach( 66 sortedForEach(placeholderCollector.localPlaceholders,
61 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) { 67 (element, placeholders) {
62 // TODO(smok): Check for conflicts with class fields and take usages 68 // TODO(smok): Check for conflicts with class fields and take usages
63 // into account. 69 // into account.
64 localPlaceholders.forEach((LocalPlaceholder placeholder) { 70 for (LocalPlaceholder placeholder in placeholders) {
65 renameNodes(placeholder.nodes, (_) => placeholder.identifier); 71 renameNodes(placeholder.nodes, (_) => placeholder.identifier);
66 }); 72 }
67 }); 73 });
68 placeholderCollector.privateNodes.forEach( 74 sortedForEach(placeholderCollector.privateNodes, (library, nodes) {
69 (LibraryElement library, Set<Identifier> nodes) { 75 renameNodes(nodes, (node) => rename(library, node.source.slowToString()));
70 renameNodes(nodes, (node) =>
71 rename(library, node.source.slowToString()));
72 }); 76 });
73 } 77 }
74 78
75 typedef String Generator(String originalName, bool isForbidden(String name)); 79 typedef String Generator(String originalName, bool isForbidden(String name));
76 80
77 /** Always tries to return original identifier name unless it is forbidden. */ 81 /** Always tries to return original identifier name unless it is forbidden. */
78 String conservativeGenerator( 82 String conservativeGenerator(
79 String originalName, bool isForbidden(String name)) { 83 String originalName, bool isForbidden(String name)) {
80 String newName = originalName; 84 String newName = originalName;
81 while (isForbidden(newName)) { 85 while (isForbidden(newName)) {
(...skipping 29 matching lines...) Expand all
111 int length = alphabet.length; 115 int length = alphabet.length;
112 StringBuffer resultBuilder = new StringBuffer(); 116 StringBuffer resultBuilder = new StringBuffer();
113 while (index >= length) { 117 while (index >= length) {
114 resultBuilder.add(alphabet[index % length]); 118 resultBuilder.add(alphabet[index % length]);
115 index ~/= length; 119 index ~/= length;
116 } 120 }
117 resultBuilder.add(alphabet[index]); 121 resultBuilder.add(alphabet[index]);
118 return resultBuilder.toString(); 122 return resultBuilder.toString();
119 } 123 }
120 } 124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698