| Index: pkg/compiler/lib/src/dart_backend/renamer.dart
|
| diff --git a/pkg/compiler/lib/src/dart_backend/renamer.dart b/pkg/compiler/lib/src/dart_backend/renamer.dart
|
| index 6465af8a2fd1b5d94de0d2327821a40dd4cfa6e2..29b26d707ecbaf65beecc12eada88e15be160c85 100644
|
| --- a/pkg/compiler/lib/src/dart_backend/renamer.dart
|
| +++ b/pkg/compiler/lib/src/dart_backend/renamer.dart
|
| @@ -4,8 +4,7 @@
|
|
|
| part of dart_backend;
|
|
|
| -Comparator get _compareNodes =>
|
| - compareBy((n) => n.getBeginToken().charOffset);
|
| +Comparator get _compareNodes => compareBy((n) => n.getBeginToken().charOffset);
|
|
|
| abstract class Renamable implements Comparable {
|
| final int RENAMABLE_TYPE_ELEMENT = 1;
|
| @@ -31,8 +30,7 @@ abstract class Renamable implements Comparable {
|
| class GlobalRenamable extends Renamable {
|
| final Entity entity;
|
|
|
| - GlobalRenamable(this.entity, Set<Node> nodes)
|
| - : super(nodes);
|
| + GlobalRenamable(this.entity, Set<Node> nodes) : super(nodes);
|
|
|
| int compareInternals(GlobalRenamable other) =>
|
| compareElements(this.entity, other.entity);
|
| @@ -44,8 +42,7 @@ class GlobalRenamable extends Renamable {
|
|
|
| class MemberRenamable extends Renamable {
|
| final String identifier;
|
| - MemberRenamable(this.identifier, Set<Node> nodes)
|
| - : super(nodes);
|
| + MemberRenamable(this.identifier, Set<Node> nodes) : super(nodes);
|
| int compareInternals(MemberRenamable other) =>
|
| this.identifier.compareTo(other.identifier);
|
| int get kind => RENAMABLE_TYPE_MEMBER;
|
| @@ -55,11 +52,10 @@ class MemberRenamable extends Renamable {
|
| }
|
|
|
| class LocalRenamable extends Renamable {
|
| - LocalRenamable(Set<Node> nodes)
|
| - : super(nodes);
|
| - int compareInternals(LocalRenamable other) =>
|
| - _compareNodes(sorted(this.nodes, _compareNodes)[0],
|
| - sorted(other.nodes, _compareNodes)[0]);
|
| + LocalRenamable(Set<Node> nodes) : super(nodes);
|
| + int compareInternals(LocalRenamable other) => _compareNodes(
|
| + sorted(this.nodes, _compareNodes)[0],
|
| + sorted(other.nodes, _compareNodes)[0]);
|
| int get kind => RENAMABLE_TYPE_LOCAL;
|
| String createNewName(PlaceholderRenamer placeholderRenamer) {
|
| return placeholderRenamer._generateUniqueTopLevelName("");
|
| @@ -72,6 +68,7 @@ class LocalRenamable extends Renamable {
|
| class PlaceholderRenamer {
|
| /// After running [computeRenames] this will contain the computed renames.
|
| final Map<Node, String> renames = new Map<Node, String>();
|
| +
|
| /// After running [computeRenames] this will map the used platform
|
| /// libraries to their respective prefixes.
|
| final Map<LibraryElement, String> platformImports =
|
| @@ -94,10 +91,9 @@ class PlaceholderRenamer {
|
|
|
| Generator _generator;
|
|
|
| - PlaceholderRenamer(this.fixedDynamicNames,
|
| - this.fixedStaticNames,
|
| - this.reexportingLibraries,
|
| - {this.enableMinification, this.cutDeclarationTypes});
|
| + PlaceholderRenamer(
|
| + this.fixedDynamicNames, this.fixedStaticNames, this.reexportingLibraries,
|
| + {this.enableMinification, this.cutDeclarationTypes});
|
|
|
| void _renameNodes(Iterable<Node> nodes, String renamer(Node node)) {
|
| for (Node node in sorted(nodes, _compareNodes)) {
|
| @@ -108,7 +104,7 @@ class PlaceholderRenamer {
|
| String _generateUniqueTopLevelName(String originalName) {
|
| String newName = _generator.generate(originalName, (name) {
|
| return _forbiddenIdentifiers.contains(name) ||
|
| - _allNamedParameterIdentifiers.contains(name);
|
| + _allNamedParameterIdentifiers.contains(name);
|
| });
|
| _forbiddenIdentifiers.add(newName);
|
| return newName;
|
| @@ -121,9 +117,10 @@ class PlaceholderRenamer {
|
| /// Looks up [originalName] in the [_privateCache] cache of [library].
|
| /// If [originalName] was not renamed before, generate a new name.
|
| String _getPrivateName(LibraryElement library, String originalName) {
|
| - return _privateCache.putIfAbsent(library, () => new Map<String, String>())
|
| - .putIfAbsent(originalName,
|
| - () => _generateUniqueTopLevelName(originalName));
|
| + return _privateCache
|
| + .putIfAbsent(library, () => new Map<String, String>())
|
| + .putIfAbsent(
|
| + originalName, () => _generateUniqueTopLevelName(originalName));
|
| }
|
|
|
| String _renameConstructor(ConstructorPlaceholder placeholder) {
|
| @@ -135,9 +132,9 @@ class PlaceholderRenamer {
|
|
|
| String _renameGlobal(Entity entity) {
|
| assert(entity is! Element ||
|
| - Elements.isMalformed(entity) ||
|
| - Elements.isStaticOrTopLevel(entity) ||
|
| - entity is TypeVariableElement);
|
| + Elements.isMalformed(entity) ||
|
| + Elements.isStaticOrTopLevel(entity) ||
|
| + entity is TypeVariableElement);
|
| // TODO(smok): We may want to reuse class static field and method names.
|
| if (entity is Element) {
|
| LibraryElement library = entity.library;
|
| @@ -148,14 +145,14 @@ class PlaceholderRenamer {
|
| // TODO(johnniwinther): Handle prefixes for dart:core.
|
| if (library.canonicalUri == Uris.dart_core) return entity.name;
|
| if (library.isInternalLibrary) {
|
| - throw new SpannableAssertionFailure(entity,
|
| + throw new SpannableAssertionFailure(
|
| + entity,
|
| "Internal library $library should never have been imported from "
|
| "the code compiled by dart2dart.");
|
| }
|
|
|
| String prefix = platformImports.putIfAbsent(library, () => null);
|
| - if (entity.isTopLevel &&
|
| - fixedDynamicNames.contains(entity.name)) {
|
| + if (entity.isTopLevel && fixedDynamicNames.contains(entity.name)) {
|
| if (prefix == null) {
|
| prefix = _generateUniqueTopLevelName('');
|
| platformImports[library] = prefix;
|
| @@ -165,8 +162,8 @@ class PlaceholderRenamer {
|
| return entity.name;
|
| }
|
| }
|
| - String name = _renamedCache.putIfAbsent(entity,
|
| - () => _generateUniqueTopLevelName(entity.name));
|
| + String name = _renamedCache.putIfAbsent(
|
| + entity, () => _generateUniqueTopLevelName(entity.name));
|
| // Look up in [_renamedCache] for a name for [entity] .
|
| // If it was not renamed before, generate a new name.
|
| return name;
|
| @@ -178,22 +175,23 @@ class PlaceholderRenamer {
|
| // Build a list sorted by usage of local nodes that will be renamed to
|
| // the same identifier. So the top-used local variables in all functions
|
| // will be renamed first and will all share the same new identifier.
|
| - int maxLength = placeholderCollector.functionScopes.values.fold(0,
|
| - (a, b) => max(a, b.localPlaceholders.length));
|
| + int maxLength = placeholderCollector.functionScopes.values
|
| + .fold(0, (a, b) => max(a, b.localPlaceholders.length));
|
|
|
| - List<Set<Node>> allLocals = new List<Set<Node>>
|
| - .generate(maxLength, (_) => new Set<Node>());
|
| + List<Set<Node>> allLocals =
|
| + new List<Set<Node>>.generate(maxLength, (_) => new Set<Node>());
|
|
|
| for (FunctionScope functionScope
|
| in placeholderCollector.functionScopes.values) {
|
| // Add current sorted local identifiers to the whole sorted list
|
| // of all local identifiers for all functions.
|
| - List<LocalPlaceholder> currentSortedPlaceholders =
|
| - sorted(functionScope.localPlaceholders,
|
| - compareBy((LocalPlaceholder ph) => -ph.nodes.length));
|
| + List<LocalPlaceholder> currentSortedPlaceholders = sorted(
|
| + functionScope.localPlaceholders,
|
| + compareBy((LocalPlaceholder ph) => -ph.nodes.length));
|
|
|
| List<Set<Node>> currentSortedNodes = currentSortedPlaceholders
|
| - .map((LocalPlaceholder ph) => ph.nodes).toList();
|
| + .map((LocalPlaceholder ph) => ph.nodes)
|
| + .toList();
|
|
|
| for (int i = 0; i < currentSortedNodes.length; i++) {
|
| allLocals[i].addAll(currentSortedNodes[i]);
|
| @@ -204,12 +202,12 @@ class PlaceholderRenamer {
|
| // count, otherwise when we rename elements first there will be no good
|
| // identifiers left for members even if they are used often.
|
| List<Renamable> renamables = new List<Renamable>();
|
| - placeholderCollector.elementNodes.forEach(
|
| - (Element element, Set<Node> nodes) {
|
| + placeholderCollector.elementNodes
|
| + .forEach((Element element, Set<Node> nodes) {
|
| renamables.add(new GlobalRenamable(element, nodes));
|
| });
|
| - placeholderCollector.memberPlaceholders.forEach(
|
| - (String memberName, Set<Identifier> identifiers) {
|
| + placeholderCollector.memberPlaceholders
|
| + .forEach((String memberName, Set<Identifier> identifiers) {
|
| renamables.add(new MemberRenamable(memberName, identifiers));
|
| });
|
| for (Set<Node> localIdentifiers in allLocals) {
|
| @@ -225,30 +223,28 @@ class PlaceholderRenamer {
|
| void _computeNonMinifiedRenames(PlaceholderCollector placeholderCollector) {
|
| _generator = new ConservativeGenerator();
|
| // Rename elements.
|
| - placeholderCollector.elementNodes.forEach(
|
| - (Element element, Set<Node> nodes) {
|
| + placeholderCollector.elementNodes
|
| + .forEach((Element element, Set<Node> nodes) {
|
| _renameNodes(nodes, (_) => _renameGlobal(element));
|
| });
|
|
|
| // Rename locals.
|
| - placeholderCollector.functionScopes.forEach(
|
| - (functionElement, functionScope) {
|
| -
|
| + placeholderCollector.functionScopes
|
| + .forEach((functionElement, functionScope) {
|
| Set<String> memberIdentifiers = new Set<String>();
|
| Set<LocalPlaceholder> placeholders = functionScope.localPlaceholders;
|
| if (functionElement != null && functionElement.enclosingClass != null) {
|
| - functionElement.enclosingClass.forEachMember(
|
| - (enclosingClass, member) {
|
| - memberIdentifiers.add(member.name);
|
| - });
|
| + functionElement.enclosingClass.forEachMember((enclosingClass, member) {
|
| + memberIdentifiers.add(member.name);
|
| + });
|
| }
|
| Set<String> usedLocalIdentifiers = new Set<String>();
|
| for (LocalPlaceholder placeholder in placeholders) {
|
| String nextId = _generator.generate(placeholder.identifier, (name) {
|
| - return functionScope.parameterIdentifiers.contains(name)
|
| - || _forbiddenIdentifiers.contains(name)
|
| - || usedLocalIdentifiers.contains(name)
|
| - || memberIdentifiers.contains(name);
|
| + return functionScope.parameterIdentifiers.contains(name) ||
|
| + _forbiddenIdentifiers.contains(name) ||
|
| + usedLocalIdentifiers.contains(name) ||
|
| + memberIdentifiers.contains(name);
|
| });
|
| usedLocalIdentifiers.add(nextId);
|
| _renameNodes(placeholder.nodes, (_) => nextId);
|
| @@ -268,8 +264,8 @@ class PlaceholderRenamer {
|
| /// Also adds to [platformImports] all the platform-libraries that are used.
|
| void computeRenames(PlaceholderCollector placeholderCollector) {
|
| _allNamedParameterIdentifiers = new Set<String>();
|
| - for (FunctionScope functionScope in
|
| - placeholderCollector.functionScopes.values) {
|
| + for (FunctionScope functionScope
|
| + in placeholderCollector.functionScopes.values) {
|
| _allNamedParameterIdentifiers.addAll(functionScope.parameterIdentifiers);
|
| }
|
|
|
| @@ -285,15 +281,15 @@ class PlaceholderRenamer {
|
| }
|
|
|
| // Rename constructors.
|
| - for (ConstructorPlaceholder placeholder in
|
| - placeholderCollector.constructorPlaceholders) {
|
| - renames[placeholder.node] =
|
| - _renameConstructor(placeholder);
|
| - };
|
| + for (ConstructorPlaceholder placeholder
|
| + in placeholderCollector.constructorPlaceholders) {
|
| + renames[placeholder.node] = _renameConstructor(placeholder);
|
| + }
|
| + ;
|
|
|
| // Rename private identifiers uniquely for each library.
|
| - placeholderCollector.privateNodes.forEach(
|
| - (LibraryElement library, Set<Identifier> identifiers) {
|
| + placeholderCollector.privateNodes
|
| + .forEach((LibraryElement library, Set<Identifier> identifiers) {
|
| for (Identifier identifier in identifiers) {
|
| renames[identifier] = _getPrivateName(library, identifier.source);
|
| }
|
| @@ -310,8 +306,8 @@ class PlaceholderRenamer {
|
| }
|
|
|
| if (cutDeclarationTypes) {
|
| - for (DeclarationTypePlaceholder placeholder in
|
| - placeholderCollector.declarationTypePlaceholders) {
|
| + for (DeclarationTypePlaceholder placeholder
|
| + in placeholderCollector.declarationTypePlaceholders) {
|
| renames[placeholder.typeNode] = placeholder.requiresVar ? 'var' : '';
|
| }
|
| }
|
| @@ -332,7 +328,7 @@ String generateMiniId(int index) {
|
| if (index < firstCharAlphabet.length) return firstCharAlphabet[index];
|
| StringBuffer resultBuilder = new StringBuffer();
|
| resultBuilder.writeCharCode(
|
| - firstCharAlphabet.codeUnitAt(index % firstCharAlphabet.length));
|
| + firstCharAlphabet.codeUnitAt(index % firstCharAlphabet.length));
|
| index ~/= firstCharAlphabet.length;
|
| int length = otherCharsAlphabet.length;
|
| while (index >= length) {
|
| @@ -352,7 +348,8 @@ class ConservativeGenerator implements Generator {
|
| String generate(String originalName, bool isForbidden(String name)) {
|
| String result = originalName;
|
| int index = 0;
|
| - while (isForbidden(result) ){ //|| result == originalName) {
|
| + while (isForbidden(result)) {
|
| + //|| result == originalName) {
|
| result = '${originalName}_${generateMiniId(index++)}';
|
| }
|
| return result;
|
| @@ -372,4 +369,4 @@ class MinifyingGenerator implements Generator {
|
| } while (isForbidden(result));
|
| return result;
|
| }
|
| -}
|
| +}
|
|
|