OLD | NEW |
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 part of js_backend; | 5 part of js_backend; |
6 | 6 |
7 /** | 7 /** |
8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
9 */ | 9 */ |
10 class Namer { | 10 class Namer { |
(...skipping 17 matching lines...) Expand all Loading... |
28 */ | 28 */ |
29 final Compiler compiler; | 29 final Compiler compiler; |
30 final Map<Element, String> globals; | 30 final Map<Element, String> globals; |
31 final Map<String, LibraryElement> shortPrivateNameOwners; | 31 final Map<String, LibraryElement> shortPrivateNameOwners; |
32 final Set<String> usedGlobalNames; | 32 final Set<String> usedGlobalNames; |
33 final Set<String> usedInstanceNames; | 33 final Set<String> usedInstanceNames; |
34 final Map<String, String> instanceNameMap; | 34 final Map<String, String> instanceNameMap; |
35 final Map<String, String> globalNameMap; | 35 final Map<String, String> globalNameMap; |
36 final Map<String, int> popularNameCounters; | 36 final Map<String, int> popularNameCounters; |
37 | 37 |
| 38 /** |
| 39 * A cache of names used for bailout methods. We make sure two |
| 40 * bailout methods cannot have the same name because if the two |
| 41 * bailout methods are in a class and a subclass, we would |
| 42 * call the wrong bailout method at runtime. To make it |
| 43 * simple, we don't keep track of inheritance and always avoid |
| 44 * similar names. |
| 45 */ |
| 46 final Set<String> usedBailoutInstanceNames; |
| 47 final Map<Element, String> bailoutNames; |
| 48 |
38 final Map<Constant, String> constantNames; | 49 final Map<Constant, String> constantNames; |
39 | 50 |
40 Namer(this.compiler) | 51 Namer(this.compiler) |
41 : globals = new Map<Element, String>(), | 52 : globals = new Map<Element, String>(), |
42 shortPrivateNameOwners = new Map<String, LibraryElement>(), | 53 shortPrivateNameOwners = new Map<String, LibraryElement>(), |
| 54 bailoutNames = new Map<Element, String>(), |
| 55 usedBailoutInstanceNames = new Set<String>(), |
43 usedGlobalNames = new Set<String>(), | 56 usedGlobalNames = new Set<String>(), |
44 usedInstanceNames = new Set<String>(), | 57 usedInstanceNames = new Set<String>(), |
45 instanceNameMap = new Map<String, String>(), | 58 instanceNameMap = new Map<String, String>(), |
46 globalNameMap = new Map<String, String>(), | 59 globalNameMap = new Map<String, String>(), |
47 constantNames = new Map<Constant, String>(), | 60 constantNames = new Map<Constant, String>(), |
48 popularNameCounters = new Map<String, int>(); | 61 popularNameCounters = new Map<String, int>(); |
49 | 62 |
50 String get ISOLATE => 'Isolate'; | 63 String get ISOLATE => 'Isolate'; |
51 String get ISOLATE_PROPERTIES => r'$isolateProperties'; | 64 String get ISOLATE_PROPERTIES => r'$isolateProperties'; |
52 /** Some closures must contain their name. The name is stored in | 65 /** |
53 * [STATIC_CLOSURE_NAME_NAME]. */ | 66 * Some closures must contain their name. The name is stored in |
| 67 * [STATIC_CLOSURE_NAME_NAME]. |
| 68 */ |
54 String get STATIC_CLOSURE_NAME_NAME => r'$name'; | 69 String get STATIC_CLOSURE_NAME_NAME => r'$name'; |
55 SourceString get CLOSURE_INVOCATION_NAME => Compiler.CALL_OPERATOR_NAME; | 70 SourceString get CLOSURE_INVOCATION_NAME => Compiler.CALL_OPERATOR_NAME; |
56 bool get shouldMinify => false; | 71 bool get shouldMinify => false; |
57 | 72 |
58 bool isReserved(String name) => name == ISOLATE; | 73 bool isReserved(String name) => name == ISOLATE; |
59 | 74 |
60 String constantName(Constant constant) { | 75 String constantName(Constant constant) { |
61 // In the current implementation it doesn't make sense to give names to | 76 // In the current implementation it doesn't make sense to give names to |
62 // function constants since the function-implementation itself serves as | 77 // function constants since the function-implementation itself serves as |
63 // constant and can be accessed directly. | 78 // constant and can be accessed directly. |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 } else if (element.isLibrary()) { | 311 } else if (element.isLibrary()) { |
297 name = LIBRARY_PREFIX; | 312 name = LIBRARY_PREFIX; |
298 } else { | 313 } else { |
299 name = element.name.slowToString(); | 314 name = element.name.slowToString(); |
300 } | 315 } |
301 // Prefix the name with '$' if it is reserved. | 316 // Prefix the name with '$' if it is reserved. |
302 return name; | 317 return name; |
303 } | 318 } |
304 | 319 |
305 String getBailoutName(Element element) { | 320 String getBailoutName(Element element) { |
| 321 String name = bailoutNames[element]; |
| 322 if (name != null) return name; |
306 bool global = !element.isInstanceMember(); | 323 bool global = !element.isInstanceMember(); |
307 var unminifiedName = '${getName(element)}\$bailout'; | 324 String unminifiedName = '${getName(element)}\$bailout'; |
308 if (global) { | 325 if (global) { |
309 return getMappedGlobalName(unminifiedName); | 326 name = getMappedGlobalName(unminifiedName); |
310 } else { | 327 } else { |
311 return getMappedInstanceName(unminifiedName); | 328 name = unminifiedName; |
| 329 int i = 0; |
| 330 while (usedBailoutInstanceNames.contains(name)) { |
| 331 name = '$unminifiedName${i++}'; |
| 332 } |
| 333 name = getMappedInstanceName(name); |
| 334 usedBailoutInstanceNames.add(name); |
312 } | 335 } |
| 336 bailoutNames[element] = name; |
| 337 return name; |
313 } | 338 } |
314 | 339 |
315 /** | 340 /** |
316 * Returns a preferred JS-id for the given element. The returned id is | 341 * Returns a preferred JS-id for the given element. The returned id is |
317 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 342 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
318 * guaranteed to be unique. | 343 * guaranteed to be unique. |
319 * | 344 * |
320 * For accessing statics consider calling | 345 * For accessing statics consider calling |
321 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 346 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
322 */ | 347 */ |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 432 } |
408 | 433 |
409 String safeName(String name) { | 434 String safeName(String name) { |
410 if (jsReserved.contains(name) || name.startsWith('\$')) { | 435 if (jsReserved.contains(name) || name.startsWith('\$')) { |
411 name = "\$$name"; | 436 name = "\$$name"; |
412 assert(!jsReserved.contains(name)); | 437 assert(!jsReserved.contains(name)); |
413 } | 438 } |
414 return name; | 439 return name; |
415 } | 440 } |
416 } | 441 } |
OLD | NEW |