| OLD | NEW |
| 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 Comparator get _compareNodes => | 7 Comparator 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 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 (_) => generateUniqueName('Unresolved')); | 299 (_) => generateUniqueName('Unresolved')); |
| 300 renameNodes(placeholderCollector.nullNodes, (_) => ''); | 300 renameNodes(placeholderCollector.nullNodes, (_) => ''); |
| 301 if (cutDeclarationTypes) { | 301 if (cutDeclarationTypes) { |
| 302 for (DeclarationTypePlaceholder placeholder in | 302 for (DeclarationTypePlaceholder placeholder in |
| 303 placeholderCollector.declarationTypePlaceholders) { | 303 placeholderCollector.declarationTypePlaceholders) { |
| 304 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : ''; | 304 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : ''; |
| 305 } | 305 } |
| 306 } | 306 } |
| 307 } | 307 } |
| 308 | 308 |
| 309 /** |
| 310 * Generates mini ID based on index. |
| 311 * In other words, it converts index to visual representation |
| 312 * as if digits are given characters. |
| 313 */ |
| 314 String generateMiniId(int index) { |
| 315 const String firstCharAlphabet = |
| 316 r'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
| 317 const String otherCharsAlphabet = |
| 318 r'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$'; |
| 319 // It's like converting index in decimal to [chars] radix. |
| 320 StringBuffer resultBuilder = new StringBuffer(); |
| 321 if (index < firstCharAlphabet.length) return firstCharAlphabet[index]; |
| 322 resultBuilder.write(firstCharAlphabet[index % firstCharAlphabet.length]); |
| 323 index ~/= firstCharAlphabet.length; |
| 324 int length = otherCharsAlphabet.length; |
| 325 while (index >= length) { |
| 326 resultBuilder.write(otherCharsAlphabet[index % length]); |
| 327 index ~/= length; |
| 328 } |
| 329 resultBuilder.write(otherCharsAlphabet[index]); |
| 330 return resultBuilder.toString(); |
| 331 } |
| 332 |
| 333 |
| 309 /** Always tries to return original identifier name unless it is forbidden. */ | 334 /** Always tries to return original identifier name unless it is forbidden. */ |
| 310 String conservativeGenerator( | 335 String conservativeGenerator(String name, bool isForbidden(String name)) { |
| 311 String originalName, bool isForbidden(String name)) { | 336 String result = name; |
| 312 String newName = originalName; | 337 int index = 0; |
| 313 while (isForbidden(newName)) { | 338 while (isForbidden(result)) { |
| 314 newName = 'p_$newName'; | 339 result = '${generateMiniId(index++)}_$name'; |
| 315 } | 340 } |
| 316 return newName; | 341 return result; |
| 317 } | 342 } |
| 318 | 343 |
| 344 |
| 319 /** Always tries to generate the most compact identifier. */ | 345 /** Always tries to generate the most compact identifier. */ |
| 320 class MinifyingGenerator { | 346 class MinifyingGenerator { |
| 321 static const String firstCharAlphabet = | 347 int index = 0; |
| 322 r'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | |
| 323 static const String otherCharsAlphabet = | |
| 324 r'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$'; | |
| 325 int nextIdIndex; | |
| 326 | 348 |
| 327 MinifyingGenerator() : nextIdIndex = 0; | 349 MinifyingGenerator(); |
| 328 | 350 |
| 329 String generate(bool isForbidden(String name)) { | 351 String generate(bool isForbidden(String name)) { |
| 330 String newName; | 352 String result; |
| 331 do { | 353 do { |
| 332 newName = getNextId(); | 354 result = generateMiniId(index++); |
| 333 } while(isForbidden(newName)); | 355 } while(isForbidden(result)); |
| 334 return newName; | 356 return result; |
| 335 } | 357 } |
| 336 | 358 } |
| 337 /** | |
| 338 * Generates next mini ID with current index and alphabet. | |
| 339 * Advances current index. | |
| 340 * In other words, it converts index to visual representation | |
| 341 * as if digits are given characters. | |
| 342 */ | |
| 343 String getNextId() { | |
| 344 // It's like converting index in decimal to [chars] radix. | |
| 345 int index = nextIdIndex++; | |
| 346 StringBuffer resultBuilder = new StringBuffer(); | |
| 347 if (index < firstCharAlphabet.length) return firstCharAlphabet[index]; | |
| 348 resultBuilder.write(firstCharAlphabet[index % firstCharAlphabet.length]); | |
| 349 index ~/= firstCharAlphabet.length; | |
| 350 int length = otherCharsAlphabet.length; | |
| 351 while (index >= length) { | |
| 352 resultBuilder.write(otherCharsAlphabet[index % length]); | |
| 353 index ~/= length; | |
| 354 } | |
| 355 resultBuilder.write(otherCharsAlphabet[index]); | |
| 356 return resultBuilder.toString(); | |
| 357 } | |
| 358 } | |
| OLD | NEW |