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

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

Issue 10887012: Rename members and named constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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 Set<String> fixedMemberNames,
Roman 2012/08/29 08:40:09 I think in this context it's more like 'forbiddenM
Anton Muhin 2012/08/29 09:43:46 I've started with forbidden as well, but actually
14 bool minify, 15 bool minify,
15 bool cutDeclarationTypes) { 16 bool cutDeclarationTypes) {
16 final Map<LibraryElement, Map<String, String>> renamed 17 final Map<LibraryElement, Map<String, String>> renamed
17 = new Map<LibraryElement, Map<String, String>>(); 18 = new Map<LibraryElement, Map<String, String>>();
18 final Set<String> usedTopLevelIdentifiers = new Set<String>(); 19 Generator topLevelGenerator =
Roman 2012/08/29 08:40:09 This can be inlined into makeGenerator
Anton Muhin 2012/08/29 09:43:46 But why? I'd rather use the same generator.
20 minify ? new MinifyingGenerator('ABCDEFGHIJKLMNOPQRSTUVWXYZ').generate
21 : conservativeGenerator;
22 makeGenerator(usedIdentifierSet) => (name) {
23 String newName = topLevelGenerator(name, usedIdentifierSet.contains);
24 usedIdentifierSet.add(newName);
25 return newName;
26 };
27
28 final usedTopLevelIdentifiers = new Set<String>();
19 // TODO(antonm): we should also populate this set with top-level 29 // TODO(antonm): we should also populate this set with top-level
20 // names from core library. 30 // names from core library.
21 usedTopLevelIdentifiers.add('main'); // Never rename anything to 'main'. 31 usedTopLevelIdentifiers.add('main'); // Never rename anything to 'main'.
22 32 final generateUniqueName = makeGenerator(usedTopLevelIdentifiers);
23 Generator topLevelGenerator =
24 minify ? new MinifyingGenerator('ABCDEFGHIJKLMNOPQRSTUVWXYZ').generate
25 : conservativeGenerator;
26 String generateUniqueName(name) {
27 String newName = topLevelGenerator(
28 name, usedTopLevelIdentifiers.contains);
29 usedTopLevelIdentifiers.add(newName);
30 return newName;
31 }
32 33
33 rename(library, originalName) => 34 rename(library, originalName) =>
34 renamed.putIfAbsent(library, () => <String>{}) 35 renamed.putIfAbsent(library, () => <String>{})
35 .putIfAbsent(originalName, () => generateUniqueName(originalName)); 36 .putIfAbsent(originalName, () => generateUniqueName(originalName));
36 37
37 String renameElement(Element element) { 38 String renameElement(Element element) {
38 assert(Elements.isStaticOrTopLevel(element) 39 assert(Elements.isStaticOrTopLevel(element)
39 || element is TypeVariableElement); 40 || element is TypeVariableElement);
40 // TODO(smok): Make sure that the new name does not conflict with existing 41 // TODO(smok): Make sure that the new name does not conflict with existing
41 // local identifiers. 42 // local identifiers.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 }); 101 });
101 sortedForEach(placeholderCollector.privateNodes, (library, nodes) { 102 sortedForEach(placeholderCollector.privateNodes, (library, nodes) {
102 renameNodes(nodes, (node) => rename(library, node.source.slowToString())); 103 renameNodes(nodes, (node) => rename(library, node.source.slowToString()));
103 }); 104 });
104 if (cutDeclarationTypes) { 105 if (cutDeclarationTypes) {
105 for (DeclarationTypePlaceholder placeholder in 106 for (DeclarationTypePlaceholder placeholder in
106 placeholderCollector.declarationTypePlaceholders) { 107 placeholderCollector.declarationTypePlaceholders) {
107 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : ''; 108 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : '';
108 } 109 }
109 } 110 }
111
112 final usedMemberIdentifiers = new Set<String>.from(fixedMemberNames);
113 // Do not rename members to top-levels, that allows to avoid renaming
114 // members to constructors.
115 usedMemberIdentifiers.addAll(usedTopLevelIdentifiers);
116 final generateMemberIdentifier = makeGenerator(usedMemberIdentifiers);
117 placeholderCollector.memberPlaceholders.forEach((identifier, nodes) {
118 final newIdentifier = generateMemberIdentifier(identifier);
119 renameNodes(nodes, (_) => newIdentifier);
120 });
110 } 121 }
111 122
112 typedef String Generator(String originalName, bool isForbidden(String name)); 123 typedef String Generator(String originalName, bool isForbidden(String name));
113 124
114 /** Always tries to return original identifier name unless it is forbidden. */ 125 /** Always tries to return original identifier name unless it is forbidden. */
115 String conservativeGenerator( 126 String conservativeGenerator(
116 String originalName, bool isForbidden(String name)) { 127 String originalName, bool isForbidden(String name)) {
117 String newName = originalName; 128 String newName = originalName;
118 while (isForbidden(newName)) { 129 while (isForbidden(newName)) {
119 newName = 'p_$newName'; 130 newName = 'p_$newName';
(...skipping 28 matching lines...) Expand all
148 int length = alphabet.length; 159 int length = alphabet.length;
149 StringBuffer resultBuilder = new StringBuffer(); 160 StringBuffer resultBuilder = new StringBuffer();
150 while (index >= length) { 161 while (index >= length) {
151 resultBuilder.add(alphabet[index % length]); 162 resultBuilder.add(alphabet[index % length]);
152 index ~/= length; 163 index ~/= length;
153 } 164 }
154 resultBuilder.add(alphabet[index]); 165 resultBuilder.add(alphabet[index]);
155 return resultBuilder.toString(); 166 return resultBuilder.toString();
156 } 167 }
157 } 168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698