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 extends Renamer { | 9 class ConflictingRenamer extends Renamer { |
| 10 final Compiler compiler; | 10 final Compiler compiler; |
| 11 final Map<Element, String> renamed; | 11 final Map<Element, String> renamed; |
| 12 final Set<String> usedTopLevelIdentifiers; | 12 final Set<String> usedTopLevelIdentifiers; |
| 13 final Map<LibraryElement, String> imports; | |
| 13 TreeElements contextElements; | 14 TreeElements contextElements; |
| 14 Element context; | 15 Element context; |
| 15 | 16 |
| 16 Map<Element, TreeElements> get resolvedElements() => | 17 Map<Element, TreeElements> get resolvedElements() => |
| 17 compiler.enqueuer.resolution.resolvedElements; | 18 compiler.enqueuer.resolution.resolvedElements; |
| 18 | 19 |
| 19 ConflictingRenamer(this.compiler) : | 20 ConflictingRenamer(this.compiler) : |
| 20 renamed = new Map<Element, String>(), | 21 renamed = new Map<Element, String>(), |
| 21 usedTopLevelIdentifiers = new Set<String>(); | 22 usedTopLevelIdentifiers = new Set<String>(), |
| 23 imports = new Map<LibraryElement, String>(); | |
| 22 | 24 |
| 23 void setContext(Element element) { | 25 void setContext(Element element) { |
| 24 this.context = element; | 26 this.context = element; |
| 25 contextElements = resolvedElements[element]; | 27 contextElements = resolvedElements[element]; |
| 26 } | 28 } |
| 27 | 29 |
| 28 String getFactoryName(FunctionExpression node) => | 30 String getFactoryName(FunctionExpression node) => |
| 29 node.name.asSend().selector.asIdentifier().source.slowToString(); | 31 node.name.asSend().selector.asIdentifier().source.slowToString(); |
| 30 | 32 |
| 31 bool isNamedConstructor(Element element) => | 33 bool isNamedConstructor(Element element) => |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 return renameElement(enclosingClass); | 77 return renameElement(enclosingClass); |
| 76 } | 78 } |
| 77 } | 79 } |
| 78 if (context.isFunction() && context.cachedNode.name == node) { | 80 if (context.isFunction() && context.cachedNode.name == node) { |
| 79 return renameElement(context); | 81 return renameElement(context); |
| 80 } | 82 } |
| 81 return null; | 83 return null; |
| 82 } | 84 } |
| 83 | 85 |
| 84 String renameElement(Element element) { | 86 String renameElement(Element element) { |
| 87 generateUniqueName(name) { | |
| 88 while (usedTopLevelIdentifiers.contains(name)) { | |
| 89 name = "x$name"; | |
| 90 } | |
| 91 usedTopLevelIdentifiers.add(name); | |
| 92 return name; | |
| 93 } | |
| 94 | |
| 85 String originalName = element.name.slowToString(); | 95 String originalName = element.name.slowToString(); |
| 86 if (element.getLibrary() == compiler.coreLibrary || !element.isTopLevel()) { | 96 // TODO(antonm): we should rename lib private names as well. |
| 87 return originalName; | 97 if (!element.isTopLevel()) return originalName; |
| 98 final library = element.getLibrary(); | |
| 99 if (library === compiler.coreLibrary) return originalName; | |
| 100 if (isDartCoreLib(compiler, library)) { | |
| 101 final prefix = imports.putIfAbsent(library, () => generateUniqueName('p')) ; | |
|
Roman
2012/07/30 14:43:30
>80 chars
| |
| 102 return '$prefix.$originalName'; | |
| 88 } | 103 } |
| 89 if (renamed[element] !== null) { | 104 if (renamed[element] !== null) { |
| 90 return renamed[element]; | 105 return renamed[element]; |
| 91 } | 106 } |
| 92 | 107 |
| 93 // Not renamed and top element. | 108 // Not renamed and top element. |
| 94 // TODO(smok): Make sure that the new name does not conflict with existing | 109 // TODO(smok): Make sure that the new name does not conflict with existing |
| 95 // local identifiers. | 110 // local identifiers. |
| 96 String name = originalName; | 111 return renamed[element] = generateUniqueName(originalName); |
| 97 while (usedTopLevelIdentifiers.contains(name)) { | |
| 98 name = "_$name"; | |
| 99 } | |
| 100 usedTopLevelIdentifiers.add(name); | |
| 101 renamed[element] = name; | |
| 102 return name; | |
| 103 } | 112 } |
| 104 } | 113 } |
| OLD | NEW |