| 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; | |
| 24 Map<String, LibraryElement> shortPrivateNameOwners; | 23 Map<String, LibraryElement> shortPrivateNameOwners; |
| 25 | 24 |
| 26 Namer(this.compiler) | 25 Namer(this.compiler) |
| 27 : globals = new Map<Element, String>(), | 26 : globals = new Map<Element, String>(), |
| 28 usedGlobals = new Map<String, int>(), | 27 usedGlobals = new Map<String, int>(), |
| 29 usedPrivateNames = new Map<String, Set<LibraryElement>>(), | |
| 30 shortPrivateNameOwners = new Map<String, LibraryElement>(); | 28 shortPrivateNameOwners = new Map<String, LibraryElement>(); |
| 31 | 29 |
| 32 final String CURRENT_ISOLATE = @'$'; | 30 final String CURRENT_ISOLATE = @'$'; |
| 33 final String ISOLATE = 'Isolate'; | 31 final String ISOLATE = 'Isolate'; |
| 34 final String ISOLATE_PROPERTIES = @"$isolateProperties"; | 32 final String ISOLATE_PROPERTIES = @"$isolateProperties"; |
| 35 /** Some closures must contain their name. The name is stored in | 33 /** Some closures must contain their name. The name is stored in |
| 36 * [STATIC_CLOSURE_NAME_NAME]. */ | 34 * [STATIC_CLOSURE_NAME_NAME]. */ |
| 37 final String STATIC_CLOSURE_NAME_NAME = @'$name'; | 35 final String STATIC_CLOSURE_NAME_NAME = @'$name'; |
| 38 static final SourceString CLOSURE_INVOCATION_NAME = | 36 static final SourceString CLOSURE_INVOCATION_NAME = |
| 39 Compiler.CALL_OPERATOR_NAME; | 37 Compiler.CALL_OPERATOR_NAME; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 var tmp = parts[i]; | 89 var tmp = parts[i]; |
| 92 parts[i] = parts[j]; | 90 parts[i] = parts[j]; |
| 93 parts[j] = tmp; | 91 parts[j] = tmp; |
| 94 } | 92 } |
| 95 return safeName(Strings.join(parts, "_")); | 93 return safeName(Strings.join(parts, "_")); |
| 96 } | 94 } |
| 97 | 95 |
| 98 String privateName(LibraryElement lib, SourceString name) { | 96 String privateName(LibraryElement lib, SourceString name) { |
| 99 if (name.isPrivate()) { | 97 if (name.isPrivate()) { |
| 100 String nameString = name.slowToString(); | 98 String nameString = name.slowToString(); |
| 101 | |
| 102 // TODO(kasperl): This is a bit of a hacky way of keeping track | |
| 103 // of the libraries that use a particular private name. | |
| 104 Set<LibraryElement> usedBy = | |
| 105 usedPrivateNames.putIfAbsent(nameString, () => | |
| 106 new Set<LibraryElement>()); | |
| 107 usedBy.add(lib); | |
| 108 | |
| 109 // The first library asking for a short private name wins. | 99 // The first library asking for a short private name wins. |
| 110 LibraryElement owner = | 100 LibraryElement owner = |
| 111 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); | 101 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); |
| 112 // If a private name could clash with a mangled private name we don't | 102 // If a private name could clash with a mangled private name we don't |
| 113 // use the short name. For example a private name "_lib3_foo" would | 103 // use the short name. For example a private name "_lib3_foo" would |
| 114 // clash with "_foo" from "lib3". | 104 // clash with "_foo" from "lib3". |
| 115 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) { | 105 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) { |
| 116 return nameString; | 106 return nameString; |
| 117 } | 107 } |
| 118 String libName = getName(lib); | 108 String libName = getName(lib); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 292 } |
| 303 | 293 |
| 304 String safeName(String name) { | 294 String safeName(String name) { |
| 305 if (jsReserved.contains(name) || name.startsWith('\$')) { | 295 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 306 name = "\$$name"; | 296 name = "\$$name"; |
| 307 assert(!jsReserved.contains(name)); | 297 assert(!jsReserved.contains(name)); |
| 308 } | 298 } |
| 309 return name; | 299 return name; |
| 310 } | 300 } |
| 311 } | 301 } |
| OLD | NEW |