| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 name = "\$$name"; | 84 name = "\$$name"; |
| 85 assert(!jsReserved.contains(name)); | 85 assert(!jsReserved.contains(name)); |
| 86 } | 86 } |
| 87 return name; | 87 return name; |
| 88 } | 88 } |
| 89 | 89 |
| 90 String getBailoutName(Element element) { | 90 String getBailoutName(Element element) { |
| 91 return '${getName(element)}\$bailout'; | 91 return '${getName(element)}\$bailout'; |
| 92 } | 92 } |
| 93 | 93 |
| 94 SourceString getConstructorName(FunctionElement constructor) { |
| 95 String dartName = constructor.name.stringValue; |
| 96 return new SourceString(dartName.replaceFirst('\.', '\$')); |
| 97 } |
| 98 |
| 94 /** | 99 /** |
| 95 * Returns a preferred JS-id for the given element. The returned id is | 100 * Returns a preferred JS-id for the given element. The returned id is |
| 96 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 101 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 97 * guaranteed to be unique. | 102 * guaranteed to be unique. |
| 98 * | 103 * |
| 99 * For accessing statics consider calling | 104 * For accessing statics consider calling |
| 100 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 105 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
| 101 */ | 106 */ |
| 102 String getName(Element element) { | 107 String getName(Element element) { |
| 103 if (element.isInstanceMember()) { | 108 if (element.isInstanceMember()) { |
| 104 return instanceName(element.name); | 109 SourceString name; |
| 110 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { |
| 111 ConstructorBodyElement bodyElement = element; |
| 112 name = getConstructorName(bodyElement.constructor); |
| 113 } else { |
| 114 name = element.name; |
| 115 } |
| 116 return instanceName(name); |
| 105 } | 117 } |
| 106 | |
| 107 String cached = globals[element]; | 118 String cached = globals[element]; |
| 108 if (cached !== null) return cached; | 119 if (cached !== null) return cached; |
| 109 | 120 |
| 110 String guess = _computeGuess(element); | 121 String guess; |
| 122 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 123 guess = getConstructorName(element).stringValue; |
| 124 } else { |
| 125 guess = _computeGuess(element); |
| 126 } |
| 111 switch (element.kind) { | 127 switch (element.kind) { |
| 112 case ElementKind.VARIABLE: | 128 case ElementKind.VARIABLE: |
| 113 case ElementKind.PARAMETER: | 129 case ElementKind.PARAMETER: |
| 114 // The name is not guaranteed to be unique. | 130 // The name is not guaranteed to be unique. |
| 115 return guess; | 131 return guess; |
| 116 | 132 |
| 133 case ElementKind.GENERATIVE_CONSTRUCTOR: |
| 117 case ElementKind.FUNCTION: | 134 case ElementKind.FUNCTION: |
| 118 case ElementKind.CLASS: | 135 case ElementKind.CLASS: |
| 119 case ElementKind.GENERATIVE_CONSTRUCTOR: | |
| 120 case ElementKind.FIELD: | 136 case ElementKind.FIELD: |
| 121 // We need to make sure the name is unique. | 137 // We need to make sure the name is unique. |
| 122 int usedCount = usedGlobals[guess]; | 138 int usedCount = usedGlobals[guess]; |
| 123 if (usedCount === null) { | 139 if (usedCount === null) { |
| 124 // No element with this name has been used before. | 140 // No element with this name has been used before. |
| 125 usedGlobals[guess] = 1; | 141 usedGlobals[guess] = 1; |
| 126 globals[element] = guess; | 142 globals[element] = guess; |
| 127 return guess; | 143 return guess; |
| 128 } else { | 144 } else { |
| 129 // Not the first time we see an element with this name. Append a | 145 // Not the first time we see an element with this name. Append a |
| (...skipping 27 matching lines...) Expand all Loading... |
| 157 } | 173 } |
| 158 | 174 |
| 159 String isolateBailoutAccess(Element element) { | 175 String isolateBailoutAccess(Element element) { |
| 160 return '${isolateAccess(element)}\$bailout'; | 176 return '${isolateAccess(element)}\$bailout'; |
| 161 } | 177 } |
| 162 | 178 |
| 163 String operatorIs(ClassElement element) { | 179 String operatorIs(ClassElement element) { |
| 164 return 'is\$${getName(element)}'; | 180 return 'is\$${getName(element)}'; |
| 165 } | 181 } |
| 166 } | 182 } |
| OLD | NEW |