| 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 /** | 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 Set<String> _jsReserved = null; | 11 static Set<String> _jsReserved = null; |
| 12 Set<String> get jsReserved() { | 12 Set<String> get jsReserved() { |
| 13 if (_jsReserved === null) { | 13 if (_jsReserved === null) { |
| 14 _jsReserved = new Set<String>(); | 14 _jsReserved = new Set<String>(); |
| 15 _jsReserved.addAll(JsNames.javaScriptKeywords); | 15 _jsReserved.addAll(JsNames.javaScriptKeywords); |
| 16 _jsReserved.addAll(JsNames.reservedPropertySymbols); | 16 _jsReserved.addAll(JsNames.reservedPropertySymbols); |
| 17 } | 17 } |
| 18 return _jsReserved; | 18 return _jsReserved; |
| 19 } | 19 } |
| 20 | 20 |
| 21 Map<Element, String> globals; | 21 Map<Element, String> globals; |
| 22 Map<String, int> usedGlobals; | 22 Map<String, int> usedGlobals; |
| 23 Map<String, Set<LibraryElement>> usedPrivateNames; |
| 23 Map<String, LibraryElement> shortPrivateNameOwners; | 24 Map<String, LibraryElement> shortPrivateNameOwners; |
| 24 | 25 |
| 25 Namer(this.compiler) | 26 Namer(this.compiler) |
| 26 : globals = new Map<Element, String>(), | 27 : globals = new Map<Element, String>(), |
| 27 usedGlobals = new Map<String, int>(), | 28 usedGlobals = new Map<String, int>(), |
| 29 usedPrivateNames = new Map<String, Set<LibraryElement>>(), |
| 28 shortPrivateNameOwners = new Map<String, LibraryElement>(); | 30 shortPrivateNameOwners = new Map<String, LibraryElement>(); |
| 29 | 31 |
| 30 final String CURRENT_ISOLATE = @'$'; | 32 final String CURRENT_ISOLATE = @'$'; |
| 31 final String ISOLATE = 'Isolate'; | 33 final String ISOLATE = 'Isolate'; |
| 32 final String ISOLATE_PROPERTIES = @"$isolateProperties"; | 34 final String ISOLATE_PROPERTIES = @"$isolateProperties"; |
| 33 /** Some closures must contain their name. The name is stored in | 35 /** Some closures must contain their name. The name is stored in |
| 34 * [STATIC_CLOSURE_NAME_NAME]. */ | 36 * [STATIC_CLOSURE_NAME_NAME]. */ |
| 35 final String STATIC_CLOSURE_NAME_NAME = @'$name'; | 37 final String STATIC_CLOSURE_NAME_NAME = @'$name'; |
| 36 final SourceString CLOSURE_INVOCATION_NAME = const SourceString(@'$call'); | 38 final SourceString CLOSURE_INVOCATION_NAME = const SourceString(@'$call'); |
| 37 | 39 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 parts[i] = parts[j]; | 73 parts[i] = parts[j]; |
| 72 parts[j] = tmp; | 74 parts[j] = tmp; |
| 73 } | 75 } |
| 74 return safeName(Strings.join(parts, "_")); | 76 return safeName(Strings.join(parts, "_")); |
| 75 } | 77 } |
| 76 | 78 |
| 77 String privateName(LibraryElement lib, SourceString name) { | 79 String privateName(LibraryElement lib, SourceString name) { |
| 78 if (name.isPrivate()) { | 80 if (name.isPrivate()) { |
| 79 String nameString = name.slowToString(); | 81 String nameString = name.slowToString(); |
| 80 | 82 |
| 83 // TODO(kasperl): This is a bit of a hacky way of keeping track |
| 84 // of the libraries that use a particular private name. |
| 85 Set<LibraryElement> usedBy = |
| 86 usedPrivateNames.putIfAbsent(nameString, () => |
| 87 new Set<LibraryElement>()); |
| 88 usedBy.add(lib); |
| 89 |
| 81 // The first library asking for a short private name wins. | 90 // The first library asking for a short private name wins. |
| 82 LibraryElement owner = | 91 LibraryElement owner = |
| 83 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); | 92 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); |
| 84 // If a private name could clash with a mangled private name we don't | 93 // If a private name could clash with a mangled private name we don't |
| 85 // use the short name. For example a private name "_lib3_foo" would | 94 // use the short name. For example a private name "_lib3_foo" would |
| 86 // clash with "_foo" from "lib3". | 95 // clash with "_foo" from "lib3". |
| 87 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) { | 96 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) { |
| 88 return nameString; | 97 return nameString; |
| 89 } | 98 } |
| 90 String libName = getName(lib); | 99 String libName = getName(lib); |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 278 } |
| 270 | 279 |
| 271 String safeName(String name) { | 280 String safeName(String name) { |
| 272 if (jsReserved.contains(name) || name.startsWith('\$')) { | 281 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 273 name = "\$$name"; | 282 name = "\$$name"; |
| 274 assert(!jsReserved.contains(name)); | 283 assert(!jsReserved.contains(name)); |
| 275 } | 284 } |
| 276 return name; | 285 return name; |
| 277 } | 286 } |
| 278 } | 287 } |
| OLD | NEW |