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

Side by Side Diff: lib/compiler/implementation/namer.dart

Issue 10693123: Use better names for closures and static variables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 5 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 | « lib/compiler/implementation/enqueue.dart ('k') | lib/compiler/implementation/ssa/closure.dart » ('j') | 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
(...skipping 24 matching lines...) Expand all
35 final String STATIC_CLOSURE_NAME_NAME = @'$name'; 35 final String STATIC_CLOSURE_NAME_NAME = @'$name';
36 final SourceString CLOSURE_INVOCATION_NAME = const SourceString(@'$call'); 36 final SourceString CLOSURE_INVOCATION_NAME = const SourceString(@'$call');
37 37
38 38
39 String closureInvocationName(Selector selector) { 39 String closureInvocationName(Selector selector) {
40 // TODO(floitsch): mangle, while not conflicting with instance names. 40 // TODO(floitsch): mangle, while not conflicting with instance names.
41 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME, 41 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME,
42 selector); 42 selector);
43 } 43 }
44 44
45 /** Returns a non-unique name for the given closure element. */
46 String closureName(Element element) {
47 List<String> parts = <String>[];
48 SourceString ownName = element.name;
49 if (ownName == null || ownName.stringValue == "") {
50 parts.add("anon");
51 } else {
52 parts.add(ownName.slowToString());
53 }
54 for (Element enclosingElement = element.enclosingElement;
55 enclosingElement != null &&
56 (enclosingElement.kind === ElementKind.GENERATIVE_CONSTRUCTOR_BODY
57 || enclosingElement.kind === ElementKind.CLASS
58 || enclosingElement.kind === ElementKind.FUNCTION
59 || enclosingElement.kind === ElementKind.GETTER
60 || enclosingElement.kind === ElementKind.SETTER);
61 enclosingElement = enclosingElement.enclosingElement) {
62 SourceString surroundingName = enclosingElement.name;
63 if (surroundingName != null) {
64 String surroundingNameString = surroundingName.slowToString();
65 if (surroundingNameString != "") parts.add(surroundingNameString);
66 }
67 }
68 // Invert the parts.
69 for (int i = 0, j = parts.length - 1; i < j; i++, j--) {
70 var tmp = parts[i];
71 parts[i] = parts[j];
72 parts[j] = tmp;
73 }
74 return safeName(Strings.join(parts, "_"));
75 }
76
45 String privateName(LibraryElement lib, SourceString name) { 77 String privateName(LibraryElement lib, SourceString name) {
46 if (name.isPrivate()) { 78 if (name.isPrivate()) {
47 String nameString = name.slowToString(); 79 String nameString = name.slowToString();
48 80
49 // The first library asking for a short private name wins. 81 // The first library asking for a short private name wins.
50 LibraryElement owner = 82 LibraryElement owner =
51 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); 83 shortPrivateNameOwners.putIfAbsent(nameString, () => lib);
52 // If a private name could clash with a mangled private name we don't 84 // If a private name could clash with a mangled private name we don't
53 // use the short name. For example a private name "_lib3_foo" would 85 // use the short name. For example a private name "_lib3_foo" would
54 // clash with "_foo" from "lib3". 86 // clash with "_foo" from "lib3".
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 156
125 static final String LIBRARY_PREFIX = "lib"; 157 static final String LIBRARY_PREFIX = "lib";
126 158
127 /** 159 /**
128 * Returns a preferred JS-id for the given top-level or static element. 160 * Returns a preferred JS-id for the given top-level or static element.
129 * The returned id is guaranteed to be a valid JS-id. 161 * The returned id is guaranteed to be a valid JS-id.
130 */ 162 */
131 String _computeGuess(Element element) { 163 String _computeGuess(Element element) {
132 assert(!element.isInstanceMember()); 164 assert(!element.isInstanceMember());
133 LibraryElement lib = element.getLibrary(); 165 LibraryElement lib = element.getLibrary();
134 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 166 String name;
135 FunctionElement functionElement = element; 167 if (element.isGenerativeConstructor()) {
136 return instanceMethodName(lib, element.name, 168 if (element.name == element.enclosingElement.name) {
137 functionElement.parameterCount(compiler)); 169 // Keep the class name for the class and not the factory.
138 } else { 170 name = "${element.name.slowToString()}\$";
139 // TODO(floitsch): deal with named constructors.
140 String name;
141 if (Elements.isStaticOrTopLevel(element)) {
142 name = element.name.slowToString();
143 } else if (element.kind == ElementKind.GETTER) {
144 name = getterName(lib, element.name);
145 } else if (element.kind == ElementKind.SETTER) {
146 name = setterName(lib, element.name);
147 } else if (element.kind == ElementKind.FUNCTION) {
148 FunctionElement functionElement = element;
149 name = element.name.slowToString();
150 name = '$name\$${functionElement.parameterCount(compiler)}';
151 } else if (element.kind === ElementKind.LIBRARY) {
152 name = LIBRARY_PREFIX;
153 } else { 171 } else {
154 name = element.name.slowToString(); 172 name = element.name.slowToString();
155 } 173 }
156 // Prefix the name with '$' if it is reserved. 174 } else if (Elements.isStaticOrTopLevel(element)) {
157 return safeName(name); 175 if (element.enclosingElement != null &&
176 element.enclosingElement.isClass()) {
177 name = "${element.enclosingElement.name.slowToString()}_"
178 "${element.name.slowToString()}";
179 } else {
180 name = element.name.slowToString();
181 }
182 } else if (element.kind === ElementKind.LIBRARY) {
183 name = LIBRARY_PREFIX;
184 } else {
185 name = element.name.slowToString();
158 } 186 }
187 // Prefix the name with '$' if it is reserved.
188 return safeName(name);
159 } 189 }
160 190
161 String getBailoutName(Element element) { 191 String getBailoutName(Element element) {
162 return '${getName(element)}\$bailout'; 192 return '${getName(element)}\$bailout';
163 } 193 }
164 194
165 /** 195 /**
166 * Returns a preferred JS-id for the given element. The returned id is 196 * Returns a preferred JS-id for the given element. The returned id is
167 * guaranteed to be a valid JS-id. Globals and static fields are furthermore 197 * guaranteed to be a valid JS-id. Globals and static fields are furthermore
168 * guaranteed to be unique. 198 * guaranteed to be unique.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 } 269 }
240 270
241 String safeName(String name) { 271 String safeName(String name) {
242 if (jsReserved.contains(name) || name.startsWith('\$')) { 272 if (jsReserved.contains(name) || name.startsWith('\$')) {
243 name = "\$$name"; 273 name = "\$$name";
244 assert(!jsReserved.contains(name)); 274 assert(!jsReserved.contains(name));
245 } 275 }
246 return name; 276 return name;
247 } 277 }
248 } 278 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/enqueue.dart ('k') | lib/compiler/implementation/ssa/closure.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698