| 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 /** |
| 22 * Map from top-level or static elements to their unique identifiers provided |
| 23 * by [getName]. |
| 24 * |
| 25 * Invariant: Keys must be declaration elements. |
| 26 */ |
| 21 final Map<Element, String> globals; | 27 final Map<Element, String> globals; |
| 22 final Map<String, int> usedGlobals; | 28 final Map<String, int> usedGlobals; |
| 23 final Map<String, LibraryElement> shortPrivateNameOwners; | 29 final Map<String, LibraryElement> shortPrivateNameOwners; |
| 24 | 30 |
| 25 final Map<Constant, String> constantNames; | 31 final Map<Constant, String> constantNames; |
| 26 | 32 |
| 27 Namer(this.compiler) | 33 Namer(this.compiler) |
| 28 : globals = new Map<Element, String>(), | 34 : globals = new Map<Element, String>(), |
| 29 usedGlobals = new Map<String, int>(), | 35 usedGlobals = new Map<String, int>(), |
| 30 shortPrivateNameOwners = new Map<String, LibraryElement>(), | 36 shortPrivateNameOwners = new Map<String, LibraryElement>(), |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 return getterName(element.getLibrary(), element.name); | 242 return getterName(element.getLibrary(), element.name); |
| 237 } else if (element.kind == ElementKind.SETTER) { | 243 } else if (element.kind == ElementKind.SETTER) { |
| 238 return setterName(element.getLibrary(), element.name); | 244 return setterName(element.getLibrary(), element.name); |
| 239 } else if (element.kind == ElementKind.FIELD) { | 245 } else if (element.kind == ElementKind.FIELD) { |
| 240 return instanceFieldName(element.getLibrary(), element.name); | 246 return instanceFieldName(element.getLibrary(), element.name); |
| 241 } else { | 247 } else { |
| 242 compiler.internalError('getName for bad kind: ${element.kind}', | 248 compiler.internalError('getName for bad kind: ${element.kind}', |
| 243 node: element.parseNode(compiler)); | 249 node: element.parseNode(compiler)); |
| 244 } | 250 } |
| 245 } else { | 251 } else { |
| 252 // Use declaration element to ensure invariant on [globals]. |
| 253 element = element.declaration; |
| 254 |
| 246 // Dealing with a top-level or static element. | 255 // Dealing with a top-level or static element. |
| 247 String cached = globals[element]; | 256 String cached = globals[element]; |
| 248 if (cached !== null) return cached; | 257 if (cached !== null) return cached; |
| 249 | 258 |
| 250 String guess = _computeGuess(element); | 259 String guess = _computeGuess(element); |
| 251 ElementKind kind = element.kind; | 260 ElementKind kind = element.kind; |
| 252 if (kind === ElementKind.VARIABLE || | 261 if (kind === ElementKind.VARIABLE || |
| 253 kind === ElementKind.PARAMETER) { | 262 kind === ElementKind.PARAMETER) { |
| 254 // The name is not guaranteed to be unique. | 263 // The name is not guaranteed to be unique. |
| 255 return guess; | 264 return guess; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 311 } |
| 303 | 312 |
| 304 String safeName(String name) { | 313 String safeName(String name) { |
| 305 if (jsReserved.contains(name) || name.startsWith('\$')) { | 314 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 306 name = "\$$name"; | 315 name = "\$$name"; |
| 307 assert(!jsReserved.contains(name)); | 316 assert(!jsReserved.contains(name)); |
| 308 } | 317 } |
| 309 return name; | 318 return name; |
| 310 } | 319 } |
| 311 } | 320 } |
| OLD | NEW |