Chromium Code Reviews| 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 usedGlobals[name] = 0; | 199 usedGlobals[name] = 0; |
| 200 return name; | 200 return name; |
| 201 } | 201 } |
| 202 | 202 |
| 203 static const String LIBRARY_PREFIX = "lib"; | 203 static const String LIBRARY_PREFIX = "lib"; |
| 204 | 204 |
| 205 /** | 205 /** |
| 206 * Returns a preferred JS-id for the given top-level or static element. | 206 * Returns a preferred JS-id for the given top-level or static element. |
| 207 * The returned id is guaranteed to be a valid JS-id. | 207 * The returned id is guaranteed to be a valid JS-id. |
| 208 */ | 208 */ |
| 209 String _computeGuess(Element element) { | 209 String _computeGuess(Element element, {allowUnsafeName: false}) { |
|
ngeoffray
2012/11/15 08:58:09
bool and no need to make it optional
karlklose
2012/11/16 13:37:19
Done.
| |
| 210 assert(!element.isInstanceMember()); | 210 assert(!element.isInstanceMember()); |
| 211 LibraryElement lib = element.getLibrary(); | 211 LibraryElement lib = element.getLibrary(); |
| 212 String name; | 212 String name; |
| 213 if (element.isGenerativeConstructor()) { | 213 if (element.isGenerativeConstructor()) { |
| 214 if (element.name == element.getEnclosingClass().name) { | 214 if (element.name == element.getEnclosingClass().name) { |
| 215 // Keep the class name for the class and not the factory. | 215 // Keep the class name for the class and not the factory. |
| 216 name = "${element.name.slowToString()}\$"; | 216 name = "${element.name.slowToString()}\$"; |
| 217 } else { | 217 } else { |
| 218 name = element.name.slowToString(); | 218 name = element.name.slowToString(); |
| 219 } | 219 } |
| 220 } else if (Elements.isStaticOrTopLevel(element)) { | 220 } else if (Elements.isStaticOrTopLevel(element)) { |
| 221 if (element.isMember()) { | 221 if (element.isMember()) { |
| 222 ClassElement enclosingClass = element.getEnclosingClass(); | 222 ClassElement enclosingClass = element.getEnclosingClass(); |
| 223 name = "${enclosingClass.name.slowToString()}_" | 223 name = "${enclosingClass.name.slowToString()}_" |
| 224 "${element.name.slowToString()}"; | 224 "${element.name.slowToString()}"; |
| 225 } else { | 225 } else { |
| 226 name = element.name.slowToString(); | 226 name = element.name.slowToString(); |
| 227 } | 227 } |
| 228 } else if (identical(element.kind, ElementKind.LIBRARY)) { | 228 } else if (identical(element.kind, ElementKind.LIBRARY)) { |
| 229 name = LIBRARY_PREFIX; | 229 name = LIBRARY_PREFIX; |
| 230 } else { | 230 } else { |
| 231 name = element.name.slowToString(); | 231 name = element.name.slowToString(); |
| 232 } | 232 } |
| 233 // Prefix the name with '$' if it is reserved. | 233 // Prefix the name with '$' if it is reserved and we generate safe names. |
| 234 return safeName(name); | 234 return allowUnsafeName ? name : safeName(name); |
| 235 } | 235 } |
| 236 | 236 |
| 237 String getBailoutName(Element element) { | 237 String getBailoutName(Element element) { |
| 238 return '${getName(element)}\$bailout'; | 238 return '${getName(element)}\$bailout'; |
| 239 } | 239 } |
| 240 | 240 |
| 241 /** | 241 /** |
| 242 * Returns a preferred JS-id for the given element. The returned id is | 242 * Returns a preferred JS-id for the given element. The returned id is |
| 243 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 243 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 244 * guaranteed to be unique. | 244 * guaranteed to be unique. |
| 245 * | 245 * |
| 246 * For accessing statics consider calling | 246 * For accessing statics consider calling |
| 247 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 247 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
| 248 */ | 248 */ |
| 249 String getName(Element element) { | 249 String getName(Element element, {allowUnsafeName: false}) { |
|
ngeoffray
2012/11/15 08:58:09
bool
karlklose
2012/11/16 13:37:19
Done.
| |
| 250 if (element.isInstanceMember()) { | 250 if (element.isInstanceMember()) { |
| 251 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY | 251 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY |
| 252 || element.kind == ElementKind.FUNCTION) { | 252 || element.kind == ElementKind.FUNCTION) { |
| 253 return instanceMethodName(element); | 253 return instanceMethodName(element); |
| 254 } else if (element.kind == ElementKind.GETTER) { | 254 } else if (element.kind == ElementKind.GETTER) { |
| 255 return getterName(element.getLibrary(), element.name); | 255 return getterName(element.getLibrary(), element.name); |
| 256 } else if (element.kind == ElementKind.SETTER) { | 256 } else if (element.kind == ElementKind.SETTER) { |
| 257 return setterName(element.getLibrary(), element.name); | 257 return setterName(element.getLibrary(), element.name); |
| 258 } else if (element.kind == ElementKind.FIELD) { | 258 } else if (element.kind == ElementKind.FIELD) { |
| 259 return instanceFieldName(element.getLibrary(), element.name); | 259 return instanceFieldName(element.getLibrary(), element.name); |
| 260 } else { | 260 } else { |
| 261 compiler.internalError('getName for bad kind: ${element.kind}', | 261 compiler.internalError('getName for bad kind: ${element.kind}', |
| 262 node: element.parseNode(compiler)); | 262 node: element.parseNode(compiler)); |
| 263 } | 263 } |
| 264 } else { | 264 } else { |
| 265 // Use declaration element to ensure invariant on [globals]. | 265 // Use declaration element to ensure invariant on [globals]. |
| 266 element = element.declaration; | 266 element = element.declaration; |
| 267 | |
| 268 // Dealing with a top-level or static element. | 267 // Dealing with a top-level or static element. |
| 269 String cached = globals[element]; | 268 String cached = globals[element]; |
| 270 if (cached != null) return cached; | 269 if (cached != null) return cached; |
| 271 | 270 |
| 272 String guess = _computeGuess(element); | 271 String guess = _computeGuess(element, allowUnsafeName: allowUnsafeName); |
| 273 ElementKind kind = element.kind; | 272 ElementKind kind = element.kind; |
| 274 if (identical(kind, ElementKind.VARIABLE) || | 273 if (identical(kind, ElementKind.VARIABLE) || |
| 275 identical(kind, ElementKind.PARAMETER)) { | 274 identical(kind, ElementKind.PARAMETER)) { |
| 276 // The name is not guaranteed to be unique. | 275 // The name is not guaranteed to be unique. |
| 277 return guess; | 276 return guess; |
| 278 } | 277 } |
| 279 if (identical(kind, ElementKind.GENERATIVE_CONSTRUCTOR) || | 278 if (identical(kind, ElementKind.GENERATIVE_CONSTRUCTOR) || |
| 280 identical(kind, ElementKind.FUNCTION) || | 279 identical(kind, ElementKind.FUNCTION) || |
| 281 identical(kind, ElementKind.CLASS) || | 280 identical(kind, ElementKind.CLASS) || |
| 282 identical(kind, ElementKind.FIELD) || | 281 identical(kind, ElementKind.FIELD) || |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 } | 323 } |
| 325 | 324 |
| 326 String safeName(String name) { | 325 String safeName(String name) { |
| 327 if (jsReserved.contains(name) || name.startsWith('\$')) { | 326 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 328 name = "\$$name"; | 327 name = "\$$name"; |
| 329 assert(!jsReserved.contains(name)); | 328 assert(!jsReserved.contains(name)); |
| 330 } | 329 } |
| 331 return name; | 330 return name; |
| 332 } | 331 } |
| 333 } | 332 } |
| OLD | NEW |