| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 name = bodyElement.constructor.name; | 112 name = bodyElement.constructor.name; |
| 107 } | 113 } |
| 108 FunctionSignature signature = element.computeSignature(compiler); | 114 FunctionSignature signature = element.computeSignature(compiler); |
| 109 String methodName = | 115 String methodName = |
| 110 '${privateName(lib, name)}\$${signature.parameterCount}'; | 116 '${privateName(lib, name)}\$${signature.parameterCount}'; |
| 111 if (!signature.optionalParametersAreNamed) { | 117 if (!signature.optionalParametersAreNamed) { |
| 112 return methodName; | 118 return methodName; |
| 113 } else { | 119 } else { |
| 114 StringBuffer suffix = new StringBuffer(); | 120 StringBuffer suffix = new StringBuffer(); |
| 115 signature.forEachOptionalParameter((Element element) { | 121 signature.forEachOptionalParameter((Element element) { |
| 116 String jsName = JsNames.getValid(element.name.slowToString()); | 122 String jsName = JsNames.getValid(element.name.slowToString()); |
| 117 suffix.add('\$$jsName'); | 123 suffix.add('\$$jsName'); |
| 118 }); | 124 }); |
| 119 return '$methodName$suffix'; | 125 return '$methodName$suffix'; |
| 120 } | 126 } |
| 121 } | 127 } |
| 122 | 128 |
| 123 String publicInstanceMethodNameByArity(SourceString name, int arity) { | 129 String publicInstanceMethodNameByArity(SourceString name, int arity) { |
| 124 assert(!name.isPrivate()); | 130 assert(!name.isPrivate()); |
| 125 return '${name.slowToString()}\$$arity'; | 131 return '${name.slowToString()}\$$arity'; |
| 126 } | 132 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 return getterName(element.getLibrary(), element.name); | 243 return getterName(element.getLibrary(), element.name); |
| 238 } else if (element.kind == ElementKind.SETTER) { | 244 } else if (element.kind == ElementKind.SETTER) { |
| 239 return setterName(element.getLibrary(), element.name); | 245 return setterName(element.getLibrary(), element.name); |
| 240 } else if (element.kind == ElementKind.FIELD) { | 246 } else if (element.kind == ElementKind.FIELD) { |
| 241 return instanceFieldName(element.getLibrary(), element.name); | 247 return instanceFieldName(element.getLibrary(), element.name); |
| 242 } else { | 248 } else { |
| 243 compiler.internalError('getName for bad kind: ${element.kind}', | 249 compiler.internalError('getName for bad kind: ${element.kind}', |
| 244 node: element.parseNode(compiler)); | 250 node: element.parseNode(compiler)); |
| 245 } | 251 } |
| 246 } else { | 252 } else { |
| 253 // Use declaration element to ensure invariant on [globals]. |
| 254 element = element.declaration; |
| 255 |
| 247 // Dealing with a top-level or static element. | 256 // Dealing with a top-level or static element. |
| 248 String cached = globals[element]; | 257 String cached = globals[element]; |
| 249 if (cached !== null) return cached; | 258 if (cached !== null) return cached; |
| 250 | 259 |
| 251 String guess = _computeGuess(element); | 260 String guess = _computeGuess(element); |
| 252 ElementKind kind = element.kind; | 261 ElementKind kind = element.kind; |
| 253 if (kind === ElementKind.VARIABLE || | 262 if (kind === ElementKind.VARIABLE || |
| 254 kind === ElementKind.PARAMETER) { | 263 kind === ElementKind.PARAMETER) { |
| 255 // The name is not guaranteed to be unique. | 264 // The name is not guaranteed to be unique. |
| 256 return guess; | 265 return guess; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 312 } |
| 304 | 313 |
| 305 String safeName(String name) { | 314 String safeName(String name) { |
| 306 if (jsReserved.contains(name) || name.startsWith('\$')) { | 315 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 307 name = "\$$name"; | 316 name = "\$$name"; |
| 308 assert(!jsReserved.contains(name)); | 317 assert(!jsReserved.contains(name)); |
| 309 } | 318 } |
| 310 return name; | 319 return name; |
| 311 } | 320 } |
| 312 } | 321 } |
| OLD | NEW |