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

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

Issue 15959017: Fix issue with dart2dart translation of parameters (collision risk). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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 part of dart_backend; 5 part of dart_backend;
6 6
7 Function get _compareNodes => 7 Function get _compareNodes =>
8 compareBy((n) => n.getBeginToken().charOffset); 8 compareBy((n) => n.getBeginToken().charOffset);
9 9
10 typedef String _Renamer(Renamable renamable); 10 typedef String _Renamer(Renamable renamable);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 .putIfAbsent(originalName, 160 .putIfAbsent(originalName,
161 () => generateUniqueName(originalName)); 161 () => generateUniqueName(originalName));
162 162
163 // Renamer function that takes library and original name and returns a new 163 // Renamer function that takes library and original name and returns a new
164 // name for given identifier. 164 // name for given identifier.
165 Function rename; 165 Function rename;
166 Function renameElement; 166 Function renameElement;
167 // A function that takes original identifier name and generates a new unique 167 // A function that takes original identifier name and generates a new unique
168 // identifier. 168 // identifier.
169 Function generateUniqueName; 169 Function generateUniqueName;
170
171 Set<String> allNamedParameterIdentifiers = new Set<String>();
172 for (var functionScope in placeholderCollector.functionScopes.values) {
173 allNamedParameterIdentifiers.addAll(functionScope.parameterIdentifiers);
174 }
175
170 if (compiler.enableMinification) { 176 if (compiler.enableMinification) {
171 MinifyingGenerator generator = new MinifyingGenerator(); 177 MinifyingGenerator generator = new MinifyingGenerator();
172 Set<String> forbiddenIdentifiers = new Set<String>.from(['main']); 178 Set<String> forbiddenIdentifiers = new Set<String>.from(['main']);
173 forbiddenIdentifiers.addAll(Keyword.keywords.keys); 179 forbiddenIdentifiers.addAll(Keyword.keywords.keys);
174 forbiddenIdentifiers.addAll(fixedMemberNames); 180 forbiddenIdentifiers.addAll(fixedMemberNames);
175 generateUniqueName = (_) => 181 generateUniqueName = (_) =>
176 generator.generate(forbiddenIdentifiers.contains); 182 generator.generate((name) =>
183 forbiddenIdentifiers.contains(name)
184 || allNamedParameterIdentifiers.contains(name));
177 rename = makeRenamer(generateUniqueName); 185 rename = makeRenamer(generateUniqueName);
178 renameElement = makeElementRenamer(rename, generateUniqueName); 186 renameElement = makeElementRenamer(rename, generateUniqueName);
179 187
180 Set<String> allParameterIdentifiers = new Set<String>();
181 for (var functionScope in placeholderCollector.functionScopes.values) {
182 allParameterIdentifiers.addAll(functionScope.parameterIdentifiers);
183 }
184 // Build a sorted (by usage) list of local nodes that will be renamed to 188 // Build a sorted (by usage) list of local nodes that will be renamed to
185 // the same identifier. So the top-used local variables in all functions 189 // the same identifier. So the top-used local variables in all functions
186 // will be renamed first and will all share the same new identifier. 190 // will be renamed first and will all share the same new identifier.
187 List<Set<Node>> allSortedLocals = new List<Set<Node>>(); 191 List<Set<Node>> allSortedLocals = new List<Set<Node>>();
188 for (var functionScope in placeholderCollector.functionScopes.values) { 192 for (var functionScope in placeholderCollector.functionScopes.values) {
189 // Add current sorted local identifiers to the whole sorted list 193 // Add current sorted local identifiers to the whole sorted list
190 // of all local identifiers for all functions. 194 // of all local identifiers for all functions.
191 List<LocalPlaceholder> currentSortedPlaceholders = 195 List<LocalPlaceholder> currentSortedPlaceholders =
192 sorted(functionScope.localPlaceholders, 196 sorted(functionScope.localPlaceholders,
193 compareBy((LocalPlaceholder ph) => -ph.nodes.length)); 197 compareBy((LocalPlaceholder ph) => -ph.nodes.length));
194 List<Set<Node>> currentSortedNodes = 198 List<Set<Node>> currentSortedNodes =
195 currentSortedPlaceholders.map((ph) => ph.nodes).toList(); 199 currentSortedPlaceholders.map((ph) => ph.nodes).toList();
196 // Make room in all sorted locals list for new stuff. 200 // Make room in all sorted locals list for new stuff.
197 while (currentSortedNodes.length > allSortedLocals.length) { 201 while (currentSortedNodes.length > allSortedLocals.length) {
198 allSortedLocals.add(new Set<Node>()); 202 allSortedLocals.add(new Set<Node>());
199 } 203 }
200 for (int i = 0; i < currentSortedNodes.length; i++) { 204 for (int i = 0; i < currentSortedNodes.length; i++) {
201 allSortedLocals[i].addAll(currentSortedNodes[i]); 205 allSortedLocals[i].addAll(currentSortedNodes[i]);
202 } 206 }
203 } 207 }
204 208
205 // Rename elements, members and locals together based on their usage count, 209 // Rename elements, members and locals together based on their usage count,
206 // otherwise when we rename elements first there will be no good identifiers 210 // otherwise when we rename elements first there will be no good identifiers
207 // left for members even if they are used often. 211 // left for members even if they are used often.
208 String elementRenamer(ElementRenamable elementRenamable) => 212 String elementRenamer(ElementRenamable elementRenamable) =>
209 renameElement(elementRenamable.element); 213 renameElement(elementRenamable.element);
210 String memberRenamer(MemberRenamable memberRenamable) => 214 String memberRenamer(MemberRenamable memberRenamable) =>
211 generator.generate(forbiddenIdentifiers.contains); 215 generator.generate(forbiddenIdentifiers.contains);
212 String localRenamer(LocalRenamable localRenamable) => 216 Function localRenamer = generateUniqueName;
Anton Muhin 2013/05/31 08:50:23 maybe drop this local altogether?
kasperl 2013/05/31 08:51:57 I think I'll keep it for consistency with the othe
213 generator.generate((name) =>
214 allParameterIdentifiers.contains(name)
215 || forbiddenIdentifiers.contains(name));
216 List<Renamable> renamables = []; 217 List<Renamable> renamables = [];
217 placeholderCollector.elementNodes.forEach( 218 placeholderCollector.elementNodes.forEach(
218 (Element element, Set<Node> nodes) { 219 (Element element, Set<Node> nodes) {
219 renamables.add(new ElementRenamable(element, nodes, elementRenamer)); 220 renamables.add(new ElementRenamable(element, nodes, elementRenamer));
220 }); 221 });
221 placeholderCollector.memberPlaceholders.forEach( 222 placeholderCollector.memberPlaceholders.forEach(
222 (String memberName, Set<Identifier> identifiers) { 223 (String memberName, Set<Identifier> identifiers) {
223 renamables.add( 224 renamables.add(
224 new MemberRenamable(memberName, identifiers, memberRenamer)); 225 new MemberRenamable(memberName, identifiers, memberRenamer));
225 }); 226 });
226 for (Set<Node> localIdentifiers in allSortedLocals) { 227 for (Set<Node> localIdentifiers in allSortedLocals) {
227 renamables.add(new LocalRenamable(localIdentifiers, localRenamer)); 228 renamables.add(new LocalRenamable(localIdentifiers, localRenamer));
228 } 229 }
229 renamables.sort((Renamable renamable1, Renamable renamable2) => 230 renamables.sort((Renamable renamable1, Renamable renamable2) =>
230 renamable1.compareTo(renamable2)); 231 renamable1.compareTo(renamable2));
231 for (Renamable renamable in renamables) { 232 for (Renamable renamable in renamables) {
232 String newName = renamable.rename(); 233 String newName = renamable.rename();
233 renameNodes(renamable.nodes, (_) => newName); 234 renameNodes(renamable.nodes, (_) => newName);
234 } 235 }
235 } else { 236 } else {
236 // Never rename anything to 'main'. 237 // Never rename anything to 'main'.
237 final usedTopLevelOrMemberIdentifiers = new Set<String>(); 238 final usedTopLevelOrMemberIdentifiers = new Set<String>();
238 usedTopLevelOrMemberIdentifiers.add('main'); 239 usedTopLevelOrMemberIdentifiers.add('main');
239 usedTopLevelOrMemberIdentifiers.addAll(fixedMemberNames); 240 usedTopLevelOrMemberIdentifiers.addAll(fixedMemberNames);
240 generateUniqueName = (originalName) { 241 generateUniqueName = (originalName) {
241 String newName = conservativeGenerator( 242 String newName = conservativeGenerator(
242 originalName, usedTopLevelOrMemberIdentifiers.contains); 243 originalName, (name) =>
244 usedTopLevelOrMemberIdentifiers.contains(name)
Anton Muhin 2013/05/31 08:50:23 why this change, only stylistic or am i missing so
kasperl 2013/05/31 08:51:57 I'm adding the allNamedParameterIdentifiers check
245 || allNamedParameterIdentifiers.contains(name));
243 usedTopLevelOrMemberIdentifiers.add(newName); 246 usedTopLevelOrMemberIdentifiers.add(newName);
244 return newName; 247 return newName;
245 }; 248 };
246 rename = makeRenamer(generateUniqueName); 249 rename = makeRenamer(generateUniqueName);
247 renameElement = makeElementRenamer(rename, generateUniqueName); 250 renameElement = makeElementRenamer(rename, generateUniqueName);
248 // Rename elements. 251 // Rename elements.
249 sortedForEach(placeholderCollector.elementNodes, 252 sortedForEach(placeholderCollector.elementNodes,
250 (Element element, Set<Node> nodes) { 253 (Element element, Set<Node> nodes) {
251 renameNodes(nodes, (_) => renameElement(element)); 254 renameNodes(nodes, (_) => renameElement(element));
252 }); 255 });
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 index ~/= firstCharAlphabet.length; 354 index ~/= firstCharAlphabet.length;
352 int length = otherCharsAlphabet.length; 355 int length = otherCharsAlphabet.length;
353 while (index >= length) { 356 while (index >= length) {
354 resultBuilder.write(otherCharsAlphabet[index % length]); 357 resultBuilder.write(otherCharsAlphabet[index % length]);
355 index ~/= length; 358 index ~/= length;
356 } 359 }
357 resultBuilder.write(otherCharsAlphabet[index]); 360 resultBuilder.write(otherCharsAlphabet[index]);
358 return resultBuilder.toString(); 361 return resultBuilder.toString();
359 } 362 }
360 } 363 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/dart_backend/placeholder_collector.dart ('k') | tests/co19/co19-dart2dart.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698