| 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 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // TODO(floitsch): mangle, while preserving uniqueness. | 115 // TODO(floitsch): mangle, while preserving uniqueness. |
| 116 StringBuffer buffer = new StringBuffer(); | 116 StringBuffer buffer = new StringBuffer(); |
| 117 List<SourceString> names = selector.getOrderedNamedArguments(); | 117 List<SourceString> names = selector.getOrderedNamedArguments(); |
| 118 for (SourceString argumentName in names) { | 118 for (SourceString argumentName in names) { |
| 119 buffer.add(@'$'); | 119 buffer.add(@'$'); |
| 120 argumentName.printOn(buffer); | 120 argumentName.printOn(buffer); |
| 121 } | 121 } |
| 122 return '${privateName(lib, name)}\$${selector.argumentCount}$buffer'; | 122 return '${privateName(lib, name)}\$${selector.argumentCount}$buffer'; |
| 123 } | 123 } |
| 124 | 124 |
| 125 String instanceFieldName(ClassElement cls, SourceString name) { | 125 String instanceFieldName(LibraryElement libraryElement, SourceString name) { |
| 126 String proposedName = privateName(cls.getLibrary(), name); | 126 String proposedName = privateName(libraryElement, name); |
| 127 if (cls.lookupSuperMember(name) !== null) { | |
| 128 String libName = getName(cls.getLibrary()); | |
| 129 String clsName = getName(cls); | |
| 130 proposedName = '$libName\$$clsName\$$proposedName'; | |
| 131 } | |
| 132 return safeName(proposedName); | 127 return safeName(proposedName); |
| 133 } | 128 } |
| 134 | 129 |
| 130 String shadowedFieldName(Element fieldElement) { |
| 131 ClassElement cls = fieldElement.getEnclosingClass(); |
| 132 LibraryElement libraryElement = fieldElement.getLibrary(); |
| 133 String libName = getName(libraryElement); |
| 134 String clsName = getName(cls); |
| 135 String instanceName = instanceFieldName(libraryElement, fieldElement.name); |
| 136 return safeName('$libName\$$clsName\$$instanceName'); |
| 137 } |
| 138 |
| 135 String setterName(LibraryElement lib, SourceString name) { | 139 String setterName(LibraryElement lib, SourceString name) { |
| 136 // We dynamically create setters from the field-name. The setter name must | 140 // We dynamically create setters from the field-name. The setter name must |
| 137 // therefore be derived from the instance field-name. | 141 // therefore be derived from the instance field-name. |
| 138 String fieldName = safeName(privateName(lib, name)); | 142 String fieldName = safeName(privateName(lib, name)); |
| 139 return 'set\$$fieldName'; | 143 return 'set\$$fieldName'; |
| 140 } | 144 } |
| 141 | 145 |
| 142 String getterName(LibraryElement lib, SourceString name) { | 146 String getterName(LibraryElement lib, SourceString name) { |
| 143 // We dynamically create getters from the field-name. The getter name must | 147 // We dynamically create getters from the field-name. The getter name must |
| 144 // therefore be derived from the instance field-name. | 148 // therefore be derived from the instance field-name. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 } else if (element.kind == ElementKind.FUNCTION) { | 223 } else if (element.kind == ElementKind.FUNCTION) { |
| 220 FunctionElement functionElement = element; | 224 FunctionElement functionElement = element; |
| 221 return instanceMethodName(element.getLibrary(), | 225 return instanceMethodName(element.getLibrary(), |
| 222 element.name, | 226 element.name, |
| 223 functionElement.parameterCount(compiler)); | 227 functionElement.parameterCount(compiler)); |
| 224 } else if (element.kind == ElementKind.GETTER) { | 228 } else if (element.kind == ElementKind.GETTER) { |
| 225 return getterName(element.getLibrary(), element.name); | 229 return getterName(element.getLibrary(), element.name); |
| 226 } else if (element.kind == ElementKind.SETTER) { | 230 } else if (element.kind == ElementKind.SETTER) { |
| 227 return setterName(element.getLibrary(), element.name); | 231 return setterName(element.getLibrary(), element.name); |
| 228 } else { | 232 } else { |
| 229 return instanceFieldName(element.getEnclosingClass(), element.name); | 233 compiler.internalError('getName for bad kind: ${element.kind}', |
| 234 node: element.parseNode(compiler)); |
| 230 } | 235 } |
| 231 } else { | 236 } else { |
| 232 // Dealing with a top-level or static element. | 237 // Dealing with a top-level or static element. |
| 233 String cached = globals[element]; | 238 String cached = globals[element]; |
| 234 if (cached !== null) return cached; | 239 if (cached !== null) return cached; |
| 235 | 240 |
| 236 String guess = _computeGuess(element); | 241 String guess = _computeGuess(element); |
| 237 ElementKind kind = element.kind; | 242 ElementKind kind = element.kind; |
| 238 if (kind === ElementKind.VARIABLE || | 243 if (kind === ElementKind.VARIABLE || |
| 239 kind === ElementKind.PARAMETER) { | 244 kind === ElementKind.PARAMETER) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 283 } |
| 279 | 284 |
| 280 String safeName(String name) { | 285 String safeName(String name) { |
| 281 if (jsReserved.contains(name) || name.startsWith('\$')) { | 286 if (jsReserved.contains(name) || name.startsWith('\$')) { |
| 282 name = "\$$name"; | 287 name = "\$$name"; |
| 283 assert(!jsReserved.contains(name)); | 288 assert(!jsReserved.contains(name)); |
| 284 } | 289 } |
| 285 return name; | 290 return name; |
| 286 } | 291 } |
| 287 } | 292 } |
| OLD | NEW |