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 class ConflictingRenamer { | 9 class ConflictingRenamer { |
|
Anton Muhin
2012/08/15 13:45:46
Now you don't need ConflictingRenamer, all you rea
Roman
2012/08/15 13:59:14
Yes, as discussed, I will in next CL first fix Emi
| |
| 10 final Compiler compiler; | 10 final Compiler compiler; |
| 11 final PlaceholderCollector placeholderCollector; | |
| 11 final Map<LibraryElement, Map<String, String>> renamed; | 12 final Map<LibraryElement, Map<String, String>> renamed; |
| 12 final Set<String> usedTopLevelIdentifiers; | 13 final Set<String> usedTopLevelIdentifiers; |
| 13 final Map<LibraryElement, String> imports; | 14 final Map<LibraryElement, String> imports; |
| 14 final Map<Node, Placeholder> placeholders; | 15 final Map<Node, String> renames; |
| 15 int privateNameCounter = 0; | 16 int privateNameCounter = 0; |
| 16 | 17 |
| 17 ConflictingRenamer(this.compiler, this.placeholders) : | 18 ConflictingRenamer(this.compiler, this.placeholderCollector) : |
| 18 renamed = new Map<LibraryElement, Map<String, String>>(), | 19 renamed = new Map<LibraryElement, Map<String, String>>(), |
| 19 usedTopLevelIdentifiers = new Set<String>(), | 20 usedTopLevelIdentifiers = new Set<String>(), |
| 20 imports = new Map<LibraryElement, String>() { | 21 imports = new Map<LibraryElement, String>(), |
| 22 renames = new Map<Node, String>() { | |
| 21 // Rename main() right now so that nobody takes its place. | 23 // Rename main() right now so that nobody takes its place. |
| 22 renameElement(compiler.mainApp.find(Compiler.MAIN)); | 24 renameElement(compiler.mainApp.find(Compiler.MAIN)); |
| 25 | |
| 26 placeholderCollector.nullNodes.forEach((Node node) { | |
| 27 renames[node] = ''; | |
| 28 }); | |
| 29 placeholderCollector.unresolvedNodes.forEach((Node node) { | |
| 30 renames[node] = generateUniqueName('Unresolved'); | |
| 31 }); | |
| 32 placeholderCollector.elementNodes.forEach((Element element, Set<Node> nodes) { | |
|
Anton Muhin
2012/08/15 13:45:46
nit: too long string?
Roman
2012/08/15 13:59:14
Done.
| |
| 33 String renamedElement = renameElement(element); | |
| 34 nodes.forEach((Node node) { | |
| 35 renames[node] = renamedElement; | |
| 36 }); | |
| 37 }); | |
| 38 placeholderCollector.localPlaceholders.forEach( | |
| 39 (FunctionElement element, Set<LocalPlaceholder> localPlaceholders) { | |
| 40 // TODO(smok): Check for conflicts with class fields and take usages | |
| 41 // into account. | |
| 42 localPlaceholders.forEach((LocalPlaceholder placeholder) { | |
| 43 placeholder.nodes.forEach((Node node) { | |
| 44 renames[node] = placeholder.identifier; | |
| 45 }); | |
| 46 }); | |
| 47 }); | |
| 48 placeholderCollector.privateNodes.forEach( | |
| 49 (LibraryElement library, Set<Identifier> nodes) { | |
| 50 nodes.forEach((Identifier node) { | |
| 51 renames[node] = | |
| 52 renamePrivateIdentifier(library, node.source.slowToString()); | |
| 53 }); | |
| 54 }); | |
| 55 } | |
| 56 | |
| 57 void renamePlaceholders(Map<Node, Placeholder> placeholders, | |
| 58 String rename(Placeholder placeholder)) { | |
| 59 placeholders.forEach((Node node, Placeholder placeholder) { | |
| 60 renames[node] = rename(placeholder); | |
| 61 }); | |
| 23 } | 62 } |
| 24 | 63 |
| 25 // Renamer implementation. | 64 // Renamer implementation. |
| 26 String rename(Node node) { | 65 String rename(Node node) => renames[node]; |
| 27 Placeholder placeholder = placeholders[node]; | |
| 28 return (placeholder !== null) ? placeholder.rename(this) : null; | |
| 29 } | |
| 30 | 66 |
| 31 String getName(LibraryElement library, String originalName, renamer) => | 67 String getName(LibraryElement library, String originalName, renamer) => |
| 32 renamed.putIfAbsent(library, () => <String>{}) | 68 renamed.putIfAbsent(library, () => <String>{}) |
| 33 .putIfAbsent(originalName, renamer); | 69 .putIfAbsent(originalName, renamer); |
| 34 | 70 |
| 35 String renamePrivateIdentifier(LibraryElement library, String id) => | 71 String renamePrivateIdentifier(LibraryElement library, String id) => |
| 36 getName(library, id, () => '_${privateNameCounter++}${id}'); | 72 getName(library, id, () => '_${privateNameCounter++}${id}'); |
| 37 | 73 |
| 38 String generateUniqueName(name) { | 74 String generateUniqueName(name) { |
| 39 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name'; | 75 while (usedTopLevelIdentifiers.contains(name)) name = 'p_$name'; |
| 40 usedTopLevelIdentifiers.add(name); | 76 usedTopLevelIdentifiers.add(name); |
| 41 return name; | 77 return name; |
| 42 } | 78 } |
| 43 | 79 |
| 44 // TODO(smok): Check for conflicts with class fields and take usages | |
| 45 // into account. | |
| 46 String renameLocalIdentifier(FunctionElement scope, String identifier) => | |
| 47 identifier; | |
| 48 | |
| 49 String renameElement(Element element) { | 80 String renameElement(Element element) { |
| 50 assert(element.isTopLevel()); | 81 assert(element.isTopLevel()); |
| 51 // TODO(smok): Make sure that the new name does not conflict with existing | 82 // TODO(smok): Make sure that the new name does not conflict with existing |
| 52 // local identifiers. | 83 // local identifiers. |
| 53 String originalName = element.name.slowToString(); | 84 String originalName = element.name.slowToString(); |
| 54 LibraryElement library = element.getLibrary(); | 85 LibraryElement library = element.getLibrary(); |
| 55 if (library === compiler.coreLibrary) return originalName; | 86 if (library === compiler.coreLibrary) return originalName; |
| 56 if (isDartCoreLib(compiler, library)) { | 87 if (isDartCoreLib(compiler, library)) { |
| 57 final prefix = | 88 final prefix = |
| 58 imports.putIfAbsent(library, () => generateUniqueName('p')); | 89 imports.putIfAbsent(library, () => generateUniqueName('p')); |
| 59 return '$prefix.$originalName'; | 90 return '$prefix.$originalName'; |
| 60 } | 91 } |
| 61 | 92 |
| 62 return getName(library, originalName, | 93 return getName(library, originalName, |
| 63 () => generateUniqueName(originalName)); | 94 () => generateUniqueName(originalName)); |
| 64 } | 95 } |
| 65 } | 96 } |
| OLD | NEW |