Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Unified Diff: lib/compiler/implementation/namer.dart

Issue 10386071: Assign a short name to private variables (first one wins). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/namer.dart
diff --git a/lib/compiler/implementation/namer.dart b/lib/compiler/implementation/namer.dart
index e28958b886ca876eb8ed4d3ae8a68e281cfceb7c..5db362c1f87fd68a0ab7990728f5041f7e32fcfc 100644
--- a/lib/compiler/implementation/namer.dart
+++ b/lib/compiler/implementation/namer.dart
@@ -23,10 +23,12 @@ class Namer {
Map<Element, String> globals;
Map<String, int> usedGlobals;
+ Map<String, LibraryElement> shortPrivateNameOwners;
Namer(this.compiler)
: globals = new Map<Element, String>(),
- usedGlobals = new Map<String, int>();
+ usedGlobals = new Map<String, int>(),
+ shortPrivateNameOwners = new Map<String, LibraryElement>();
final String CURRENT_ISOLATE = "\$";
final String ISOLATE = "Isolate";
@@ -40,10 +42,26 @@ class Namer {
}
String privateName(LibraryElement lib, SourceString name) {
+ bool canNotClashWithMangledPrivateNames(String proposedName) {
ngeoffray 2012/05/10 14:20:41 I'd inline the closure, and add a comment instead
floitsch 2012/05/10 14:33:01 Done.
+ return !proposedName.startsWith('_$LIBRARY_PREFIX');
+ }
+
if (name.isPrivate()) {
- return '_${getName(lib)}${name.slowToString()}';
+ String nameString = name.slowToString();
+
+ // The first library asking for a short private name wins.
+ LibraryElement owner =
+ shortPrivateNameOwners.putIfAbsent(nameString, () => lib);
+ if (owner === lib && canNotClashWithMangledPrivateNames(nameString)) {
+ return nameString;
+ }
+ String libName = getName(lib);
+ // If a library name does not start with the [LIBRARY_PREFIX] then our
+ // assumptions about clashing with mangled private members does not hold.
+ assert(libName.startsWith(LIBRARY_PREFIX));
+ return '_$libName$nameString';
} else {
- return '${name.slowToString()}';
+ return name.slowToString();
}
}
@@ -98,6 +116,8 @@ class Namer {
}
}
+ static final String LIBRARY_PREFIX = "lib";
+
/**
* Returns a preferred JS-id for the given top-level or static element.
* The returned id is guaranteed to be a valid JS-id.
@@ -123,7 +143,7 @@ class Namer {
name = element.name.slowToString();
name = '$name\$${functionElement.parameterCount(compiler)}';
} else if (element.kind === ElementKind.LIBRARY) {
- name = 'lib';
+ name = LIBRARY_PREFIX;
} else {
name = element.name.slowToString();
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698