| 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 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 renames[node] = renamer(node); | 76 renames[node] = renamer(node); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 sortedForEach(Map<Element, Dynamic> map, f) { | 80 sortedForEach(Map<Element, Dynamic> map, f) { |
| 81 for (Element element in sortElements(map.keys)) { | 81 for (Element element in sortElements(map.keys)) { |
| 82 f(element, map[element]); | 82 f(element, map[element]); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | 85 |
| 86 String renameType(DartType type, Function renameElement) { |
| 87 // TODO(smok): Do not rename type if it is in platform library or |
| 88 // js-helpers. |
| 89 StringBuffer result = new StringBuffer(renameElement(type.element)); |
| 90 if (type is InterfaceType && !type.arguments.isEmpty) { |
| 91 result.add('<'); |
| 92 Link<DartType> argumentsLink = type.arguments; |
| 93 result.add(renameType(argumentsLink.head, renameElement)); |
| 94 for (Link<DartType> link = argumentsLink.tail; !link.isEmpty; |
| 95 link = link.tail) { |
| 96 result.add(','); |
| 97 result.add(renameType(link.head, renameElement)); |
| 98 } |
| 99 result.add('>'); |
| 100 } |
| 101 return result.toString(); |
| 102 } |
| 103 |
| 104 String renameConstructor(Element element, DartType type, |
| 105 Function renameString, Function renameElement) { |
| 106 assert(element.isConstructor()); |
| 107 StringBuffer result = new StringBuffer(); |
| 108 String name = element.name.slowToString(); |
| 109 if (element.name != element.getEnclosingClass().name) { |
| 110 // Named constructor or factory. Is there a more reliable way to check |
| 111 // this case? |
| 112 result.add(renameType(type, renameElement)); |
| 113 result.add('.'); |
| 114 String prefix = '${element.getEnclosingClass().name.slowToString()}\$'; |
| 115 if (!name.startsWith(prefix)) { |
| 116 // Factory for another interface (that is going away soon). |
| 117 compiler.internalErrorOnElement(element, |
| 118 "Factory constructors for external interfaces are not supported."); |
| 119 } |
| 120 assert(name.startsWith(prefix)); |
| 121 name = name.substring(prefix.length); |
| 122 // result.add(renameString(element.getLibrary(), name)); |
| 123 result.add(name); |
| 124 } else { |
| 125 result.add(renameType(type, renameElement)); |
| 126 } |
| 127 return result.toString(); |
| 128 } |
| 129 |
| 86 Function makeElementRenamer(rename, generateUniqueName) => (element) { | 130 Function makeElementRenamer(rename, generateUniqueName) => (element) { |
| 87 assert(Elements.isStaticOrTopLevel(element) | 131 assert(Elements.isStaticOrTopLevel(element) |
| 88 || element is TypeVariableElement); | 132 || element is TypeVariableElement); |
| 89 // TODO(smok): We may want to reuse class static field and method names. | 133 // TODO(smok): We may want to reuse class static field and method names. |
| 90 String originalName = element.name.slowToString(); | 134 String originalName = element.name.slowToString(); |
| 91 LibraryElement library = element.getLibrary(); | 135 LibraryElement library = element.getLibrary(); |
| 136 if (identical(element.getLibrary(), compiler.coreLibrary)) { |
| 137 return originalName; |
| 138 } |
| 92 if (library.isPlatformLibrary) { | 139 if (library.isPlatformLibrary) { |
| 93 assert(element.isTopLevel()); | 140 assert(element.isTopLevel()); |
| 94 final prefix = | 141 final prefix = |
| 95 imports.putIfAbsent(library, () => generateUniqueName('p')); | 142 imports.putIfAbsent(library, () => generateUniqueName('p')); |
| 96 return '$prefix.$originalName'; | 143 return '$prefix.$originalName'; |
| 97 } | 144 } |
| 98 | 145 |
| 99 return rename(library, originalName); | 146 return rename(library, originalName); |
| 100 }; | 147 }; |
| 101 | 148 |
| 102 Function makeRenamer(generateUniqueName) => | 149 Function makeRenamer(generateUniqueName) => |
| 103 (library, originalName) => | 150 (library, originalName) => |
| 104 renamed.putIfAbsent(library, () => <String>{}) | 151 renamed.putIfAbsent(library, () => <String>{}) |
| 105 .putIfAbsent(originalName, | 152 .putIfAbsent(originalName, |
| 106 () => generateUniqueName(originalName)); | 153 () => generateUniqueName(originalName)); |
| 107 | 154 |
| 108 // Renamer function that takes library and original name and returns a new | 155 // Renamer function that takes library and original name and returns a new |
| 109 // name for given identifier. | 156 // name for given identifier. |
| 110 Function rename; | 157 Function rename; |
| 158 Function renameElement; |
| 111 // A function that takes original identifier name and generates a new unique | 159 // A function that takes original identifier name and generates a new unique |
| 112 // identifier. | 160 // identifier. |
| 113 Function generateUniqueName; | 161 Function generateUniqueName; |
| 114 if (compiler.enableMinification) { | 162 if (compiler.enableMinification) { |
| 115 MinifyingGenerator generator = new MinifyingGenerator(); | 163 MinifyingGenerator generator = new MinifyingGenerator(); |
| 116 Set<String> forbiddenIdentifiers = new Set<String>.from(['main']); | 164 Set<String> forbiddenIdentifiers = new Set<String>.from(['main']); |
| 117 forbiddenIdentifiers.addAll(Keyword.keywords.keys); | 165 forbiddenIdentifiers.addAll(Keyword.keywords.keys); |
| 118 forbiddenIdentifiers.addAll(fixedMemberNames); | 166 forbiddenIdentifiers.addAll(fixedMemberNames); |
| 119 generateUniqueName = (_) => | 167 generateUniqueName = (_) => |
| 120 generator.generate(forbiddenIdentifiers.contains); | 168 generator.generate(forbiddenIdentifiers.contains); |
| 121 rename = makeRenamer(generateUniqueName); | 169 rename = makeRenamer(generateUniqueName); |
| 122 Function renameElement = makeElementRenamer(rename, generateUniqueName); | 170 renameElement = makeElementRenamer(rename, generateUniqueName); |
| 123 | 171 |
| 124 Set<String> allParameterIdentifiers = new Set<String>(); | 172 Set<String> allParameterIdentifiers = new Set<String>(); |
| 125 for (var functionScope in placeholderCollector.functionScopes.values) { | 173 for (var functionScope in placeholderCollector.functionScopes.values) { |
| 126 allParameterIdentifiers.addAll(functionScope.parameterIdentifiers); | 174 allParameterIdentifiers.addAll(functionScope.parameterIdentifiers); |
| 127 } | 175 } |
| 128 // Build a sorted (by usage) list of local nodes that will be renamed to | 176 // Build a sorted (by usage) list of local nodes that will be renamed to |
| 129 // the same identifier. So the top-used local variables in all functions | 177 // the same identifier. So the top-used local variables in all functions |
| 130 // will be renamed first and will all share the same new identifier. | 178 // will be renamed first and will all share the same new identifier. |
| 131 List<Set<Node>> allSortedLocals = new List<Set<Node>>(); | 179 List<Set<Node>> allSortedLocals = new List<Set<Node>>(); |
| 132 for (var functionScope in placeholderCollector.functionScopes.values) { | 180 for (var functionScope in placeholderCollector.functionScopes.values) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 final usedTopLevelOrMemberIdentifiers = new Set<String>(); | 229 final usedTopLevelOrMemberIdentifiers = new Set<String>(); |
| 182 usedTopLevelOrMemberIdentifiers.add('main'); | 230 usedTopLevelOrMemberIdentifiers.add('main'); |
| 183 usedTopLevelOrMemberIdentifiers.addAll(fixedMemberNames); | 231 usedTopLevelOrMemberIdentifiers.addAll(fixedMemberNames); |
| 184 generateUniqueName = (originalName) { | 232 generateUniqueName = (originalName) { |
| 185 String newName = conservativeGenerator( | 233 String newName = conservativeGenerator( |
| 186 originalName, usedTopLevelOrMemberIdentifiers.contains); | 234 originalName, usedTopLevelOrMemberIdentifiers.contains); |
| 187 usedTopLevelOrMemberIdentifiers.add(newName); | 235 usedTopLevelOrMemberIdentifiers.add(newName); |
| 188 return newName; | 236 return newName; |
| 189 }; | 237 }; |
| 190 rename = makeRenamer(generateUniqueName); | 238 rename = makeRenamer(generateUniqueName); |
| 191 Function renameElement = makeElementRenamer(rename, generateUniqueName); | 239 renameElement = makeElementRenamer(rename, generateUniqueName); |
| 192 // Rename elements. | 240 // Rename elements. |
| 193 sortedForEach(placeholderCollector.elementNodes, | 241 sortedForEach(placeholderCollector.elementNodes, |
| 194 (Element element, Set<Node> nodes) { | 242 (Element element, Set<Node> nodes) { |
| 195 renameNodes(nodes, (_) => renameElement(element)); | 243 renameNodes(nodes, (_) => renameElement(element)); |
| 196 }); | 244 }); |
| 197 | 245 |
| 198 // Rename locals. | 246 // Rename locals. |
| 199 sortedForEach(placeholderCollector.functionScopes, | 247 sortedForEach(placeholderCollector.functionScopes, |
| 200 (functionElement, functionScope) { | 248 (functionElement, functionScope) { |
| 201 Set<LocalPlaceholder> placeholders = functionScope.localPlaceholders; | 249 Set<LocalPlaceholder> placeholders = functionScope.localPlaceholders; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 223 // Do not rename members to top-levels, that allows to avoid renaming | 271 // Do not rename members to top-levels, that allows to avoid renaming |
| 224 // members to constructors. | 272 // members to constructors. |
| 225 usedMemberIdentifiers.addAll(usedTopLevelOrMemberIdentifiers); | 273 usedMemberIdentifiers.addAll(usedTopLevelOrMemberIdentifiers); |
| 226 placeholderCollector.memberPlaceholders.forEach((identifier, nodes) { | 274 placeholderCollector.memberPlaceholders.forEach((identifier, nodes) { |
| 227 String newIdentifier = conservativeGenerator( | 275 String newIdentifier = conservativeGenerator( |
| 228 identifier, usedMemberIdentifiers.contains); | 276 identifier, usedMemberIdentifiers.contains); |
| 229 renameNodes(nodes, (_) => newIdentifier); | 277 renameNodes(nodes, (_) => newIdentifier); |
| 230 }); | 278 }); |
| 231 } | 279 } |
| 232 | 280 |
| 281 // Rename constructors. |
| 282 placeholderCollector.constructorPlaceholders.forEach( |
| 283 (Element constructor, List<ConstructorPlaceholder> placeholders) { |
| 284 for (ConstructorPlaceholder ph in placeholders) { |
| 285 renames[ph.node] = |
| 286 renameConstructor(constructor, ph.type, rename, renameElement); |
| 287 } |
| 288 }); |
| 233 sortedForEach(placeholderCollector.privateNodes, (library, nodes) { | 289 sortedForEach(placeholderCollector.privateNodes, (library, nodes) { |
| 234 renameNodes(nodes, (node) => rename(library, node.source.slowToString())); | 290 renameNodes(nodes, (node) => rename(library, node.source.slowToString())); |
| 235 }); | 291 }); |
| 236 renameNodes(placeholderCollector.unresolvedNodes, | 292 renameNodes(placeholderCollector.unresolvedNodes, |
| 237 (_) => generateUniqueName('Unresolved')); | 293 (_) => generateUniqueName('Unresolved')); |
| 238 renameNodes(placeholderCollector.nullNodes, (_) => ''); | 294 renameNodes(placeholderCollector.nullNodes, (_) => ''); |
| 239 if (cutDeclarationTypes) { | 295 if (cutDeclarationTypes) { |
| 240 for (DeclarationTypePlaceholder placeholder in | 296 for (DeclarationTypePlaceholder placeholder in |
| 241 placeholderCollector.declarationTypePlaceholders) { | 297 placeholderCollector.declarationTypePlaceholders) { |
| 242 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : ''; | 298 renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : ''; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 index ~/= firstCharAlphabet.length; | 343 index ~/= firstCharAlphabet.length; |
| 288 int length = otherCharsAlphabet.length; | 344 int length = otherCharsAlphabet.length; |
| 289 while (index >= length) { | 345 while (index >= length) { |
| 290 resultBuilder.add(otherCharsAlphabet[index % length]); | 346 resultBuilder.add(otherCharsAlphabet[index % length]); |
| 291 index ~/= length; | 347 index ~/= length; |
| 292 } | 348 } |
| 293 resultBuilder.add(otherCharsAlphabet[index]); | 349 resultBuilder.add(otherCharsAlphabet[index]); |
| 294 return resultBuilder.toString(); | 350 return resultBuilder.toString(); |
| 295 } | 351 } |
| 296 } | 352 } |
| OLD | NEW |