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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 * Assigns JavaScript identifiers to Dart variables, class-names and members. 6 * Assigns JavaScript identifiers to Dart variables, class-names and members.
7 */ 7 */
8 class Namer { 8 class Namer {
9 final Compiler compiler; 9 final Compiler compiler;
10 10
11 static final CLOSURE_INVOCATION_NAME = const SourceString('\$call'); 11 static final CLOSURE_INVOCATION_NAME = const SourceString('\$call');
12 static final OPERATOR_EQUALS = const SourceString('operator\$eq'); 12 static final OPERATOR_EQUALS = const SourceString('operator\$eq');
13 13
14 static Set<String> _jsReserved = null; 14 static Set<String> _jsReserved = null;
15 Set<String> get jsReserved() { 15 Set<String> get jsReserved() {
16 if (_jsReserved === null) { 16 if (_jsReserved === null) {
17 _jsReserved = new Set<String>(); 17 _jsReserved = new Set<String>();
18 _jsReserved.addAll(JsNames.javaScriptKeywords); 18 _jsReserved.addAll(JsNames.javaScriptKeywords);
19 _jsReserved.addAll(JsNames.reservedPropertySymbols); 19 _jsReserved.addAll(JsNames.reservedPropertySymbols);
20 } 20 }
21 return _jsReserved; 21 return _jsReserved;
22 } 22 }
23 23
24 Map<Element, String> globals; 24 Map<Element, String> globals;
25 Map<String, int> usedGlobals; 25 Map<String, int> usedGlobals;
26 Map<String, LibraryElement> shortPrivateNameOwners;
26 27
27 Namer(this.compiler) 28 Namer(this.compiler)
28 : globals = new Map<Element, String>(), 29 : globals = new Map<Element, String>(),
29 usedGlobals = new Map<String, int>(); 30 usedGlobals = new Map<String, int>(),
31 shortPrivateNameOwners = new Map<String, LibraryElement>();
30 32
31 final String CURRENT_ISOLATE = "\$"; 33 final String CURRENT_ISOLATE = "\$";
32 final String ISOLATE = "Isolate"; 34 final String ISOLATE = "Isolate";
33 final String ISOLATE_PROPERTIES = "\$isolateProperties"; 35 final String ISOLATE_PROPERTIES = "\$isolateProperties";
34 36
35 37
36 String closureInvocationName(Selector selector) { 38 String closureInvocationName(Selector selector) {
37 // TODO(floitsch): mangle, while not conflicting with instance names. 39 // TODO(floitsch): mangle, while not conflicting with instance names.
38 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME, 40 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME,
39 selector); 41 selector);
40 } 42 }
41 43
42 String privateName(LibraryElement lib, SourceString name) { 44 String privateName(LibraryElement lib, SourceString name) {
45 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.
46 return !proposedName.startsWith('_$LIBRARY_PREFIX');
47 }
48
43 if (name.isPrivate()) { 49 if (name.isPrivate()) {
44 return '_${getName(lib)}${name.slowToString()}'; 50 String nameString = name.slowToString();
51
52 // The first library asking for a short private name wins.
53 LibraryElement owner =
54 shortPrivateNameOwners.putIfAbsent(nameString, () => lib);
55 if (owner === lib && canNotClashWithMangledPrivateNames(nameString)) {
56 return nameString;
57 }
58 String libName = getName(lib);
59 // If a library name does not start with the [LIBRARY_PREFIX] then our
60 // assumptions about clashing with mangled private members does not hold.
61 assert(libName.startsWith(LIBRARY_PREFIX));
62 return '_$libName$nameString';
45 } else { 63 } else {
46 return '${name.slowToString()}'; 64 return name.slowToString();
47 } 65 }
48 } 66 }
49 67
50 String instanceMethodName(LibraryElement lib, SourceString name, int arity) { 68 String instanceMethodName(LibraryElement lib, SourceString name, int arity) {
51 return '${privateName(lib, name)}\$$arity'; 69 return '${privateName(lib, name)}\$$arity';
52 } 70 }
53 71
54 String instanceMethodInvocationName(LibraryElement lib, SourceString name, 72 String instanceMethodInvocationName(LibraryElement lib, SourceString name,
55 Selector selector) { 73 Selector selector) {
56 // TODO(floitsch): mangle, while preserving uniqueness. 74 // TODO(floitsch): mangle, while preserving uniqueness.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 String name; 109 String name;
92 do { 110 do {
93 usedCount++; 111 usedCount++;
94 name = '$proposedName$usedCount'; 112 name = '$proposedName$usedCount';
95 } while (usedGlobals[name] !== null); 113 } while (usedGlobals[name] !== null);
96 usedGlobals[proposedName] = usedCount; 114 usedGlobals[proposedName] = usedCount;
97 return name; 115 return name;
98 } 116 }
99 } 117 }
100 118
119 static final String LIBRARY_PREFIX = "lib";
120
101 /** 121 /**
102 * Returns a preferred JS-id for the given top-level or static element. 122 * Returns a preferred JS-id for the given top-level or static element.
103 * The returned id is guaranteed to be a valid JS-id. 123 * The returned id is guaranteed to be a valid JS-id.
104 */ 124 */
105 String _computeGuess(Element element) { 125 String _computeGuess(Element element) {
106 assert(!element.isInstanceMember()); 126 assert(!element.isInstanceMember());
107 LibraryElement lib = element.getLibrary(); 127 LibraryElement lib = element.getLibrary();
108 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 128 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
109 FunctionElement functionElement = element; 129 FunctionElement functionElement = element;
110 return instanceMethodName(lib, element.name, 130 return instanceMethodName(lib, element.name,
111 functionElement.parameterCount(compiler)); 131 functionElement.parameterCount(compiler));
112 } else { 132 } else {
113 // TODO(floitsch): deal with named constructors. 133 // TODO(floitsch): deal with named constructors.
114 String name; 134 String name;
115 if (Elements.isStaticOrTopLevel(element)) { 135 if (Elements.isStaticOrTopLevel(element)) {
116 name = element.name.slowToString(); 136 name = element.name.slowToString();
117 } else if (element.kind == ElementKind.GETTER) { 137 } else if (element.kind == ElementKind.GETTER) {
118 name = getterName(lib, element.name); 138 name = getterName(lib, element.name);
119 } else if (element.kind == ElementKind.SETTER) { 139 } else if (element.kind == ElementKind.SETTER) {
120 name = setterName(lib, element.name); 140 name = setterName(lib, element.name);
121 } else if (element.kind == ElementKind.FUNCTION) { 141 } else if (element.kind == ElementKind.FUNCTION) {
122 FunctionElement functionElement = element; 142 FunctionElement functionElement = element;
123 name = element.name.slowToString(); 143 name = element.name.slowToString();
124 name = '$name\$${functionElement.parameterCount(compiler)}'; 144 name = '$name\$${functionElement.parameterCount(compiler)}';
125 } else if (element.kind === ElementKind.LIBRARY) { 145 } else if (element.kind === ElementKind.LIBRARY) {
126 name = 'lib'; 146 name = LIBRARY_PREFIX;
127 } else { 147 } else {
128 name = element.name.slowToString(); 148 name = element.name.slowToString();
129 } 149 }
130 // Prefix the name with '$' if it is reserved. 150 // Prefix the name with '$' if it is reserved.
131 return safeName(name); 151 return safeName(name);
132 } 152 }
133 } 153 }
134 154
135 String getBailoutName(Element element) { 155 String getBailoutName(Element element) {
136 return '${getName(element)}\$bailout'; 156 return '${getName(element)}\$bailout';
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 235 }
216 236
217 String safeName(String name) { 237 String safeName(String name) {
218 if (jsReserved.contains(name) || name.startsWith('\$')) { 238 if (jsReserved.contains(name) || name.startsWith('\$')) {
219 name = "\$$name"; 239 name = "\$$name";
220 assert(!jsReserved.contains(name)); 240 assert(!jsReserved.contains(name));
221 } 241 }
222 return name; 242 return name;
223 } 243 }
224 } 244 }
OLDNEW
« 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