Chromium Code Reviews| 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 /** | 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)); | |
|
Roman
2012/08/21 07:34:17
I don't understand this. Earlier we had different
| |
| 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 (library === compiler.coreLibrary | 37 if (library === compiler.coreLibrary |
| 42 || element == compiler.mainApp.find(Compiler.MAIN)) return originalName; | 38 || element == compiler.mainApp.find(Compiler.MAIN)) return originalName; |
| 43 if (isDartCoreLib(compiler, library)) { | 39 if (isDartCoreLib(compiler, library)) { |
| 44 final prefix = | 40 final prefix = |
| 45 imports.putIfAbsent(library, () => generateUniqueName('p')); | 41 imports.putIfAbsent(library, () => generateUniqueName('p')); |
| 46 return '$prefix.$originalName'; | 42 return '$prefix.$originalName'; |
| 47 } | 43 } |
| 48 | 44 |
| 49 return getName(library, originalName, | 45 return rename(library, originalName); |
| 50 () => generateUniqueName(originalName)); | |
| 51 } | 46 } |
| 52 | 47 |
| 53 placeholderCollector.nullNodes.forEach((Node node) { | 48 placeholderCollector.nullNodes.forEach((Node node) { |
| 54 renames[node] = ''; | 49 renames[node] = ''; |
| 55 }); | 50 }); |
| 56 placeholderCollector.unresolvedNodes.forEach((Node node) { | 51 placeholderCollector.unresolvedNodes.forEach((Node node) { |
| 57 renames[node] = generateUniqueName('Unresolved'); | 52 renames[node] = generateUniqueName('Unresolved'); |
| 58 }); | 53 }); |
| 59 placeholderCollector.elementNodes.forEach( | 54 placeholderCollector.elementNodes.forEach( |
| 60 (Element element, Set<Node> nodes) { | 55 (Element element, Set<Node> nodes) { |
| 61 String renamedElement = renameElement(element); | 56 String renamedElement = renameElement(element); |
| 62 nodes.forEach((Node node) { | 57 nodes.forEach((Node node) { |
| 63 renames[node] = renamedElement; | 58 renames[node] = renamedElement; |
| 64 }); | 59 }); |
| 65 }); | 60 }); |
| 66 placeholderCollector.localPlaceholders.forEach( | 61 placeholderCollector.localPlaceholders.forEach( |
| 67 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) { | 62 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) { |
| 68 // TODO(smok): Check for conflicts with class fields and take usages | 63 // TODO(smok): Check for conflicts with class fields and take usages |
| 69 // into account. | 64 // into account. |
| 70 localPlaceholders.forEach((LocalPlaceholder placeholder) { | 65 localPlaceholders.forEach((LocalPlaceholder placeholder) { |
| 71 placeholder.nodes.forEach((Node node) { | 66 placeholder.nodes.forEach((Node node) { |
| 72 renames[node] = placeholder.identifier; | 67 renames[node] = placeholder.identifier; |
| 73 }); | 68 }); |
| 74 }); | 69 }); |
| 75 }); | 70 }); |
| 76 placeholderCollector.privateNodes.forEach( | 71 placeholderCollector.privateNodes.forEach( |
| 77 (LibraryElement library, Set<Identifier> nodes) { | 72 (LibraryElement library, Set<Identifier> nodes) { |
| 78 nodes.forEach((Identifier node) { | 73 nodes.forEach((Identifier node) { |
| 79 renames[node] = | 74 renames[node] = rename(library, node.source.slowToString()); |
| 80 renamePrivateIdentifier(library, node.source.slowToString()); | |
| 81 }); | 75 }); |
| 82 }); | 76 }); |
| 83 } | 77 } |
| 84 | 78 |
| 85 typedef String Generator(String originalName, bool isForbidden(String name)); | 79 typedef String Generator(String originalName, bool isForbidden(String name)); |
| 86 | 80 |
| 87 /** Always tries to return original identifier name unless it is forbidden. */ | 81 /** Always tries to return original identifier name unless it is forbidden. */ |
| 88 String conservativeGenerator( | 82 String conservativeGenerator( |
| 89 String originalName, bool isForbidden(String name)) { | 83 String originalName, bool isForbidden(String name)) { |
| 90 String newName = originalName; | 84 String newName = originalName; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 121 int length = alphabet.length; | 115 int length = alphabet.length; |
| 122 StringBuffer resultBuilder = new StringBuffer(); | 116 StringBuffer resultBuilder = new StringBuffer(); |
| 123 while (index >= length) { | 117 while (index >= length) { |
| 124 resultBuilder.add(alphabet[index % length]); | 118 resultBuilder.add(alphabet[index % length]); |
| 125 index ~/= length; | 119 index ~/= length; |
| 126 } | 120 } |
| 127 resultBuilder.add(alphabet[index]); | 121 resultBuilder.add(alphabet[index]); |
| 128 return resultBuilder.toString(); | 122 return resultBuilder.toString(); |
| 129 } | 123 } |
| 130 } | 124 } |
| OLD | NEW |