| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 | 58 |
| 59 String setterName(SourceString name) { | 59 String setterName(SourceString name) { |
| 60 return 'set\$$name'; | 60 return 'set\$$name'; |
| 61 } | 61 } |
| 62 | 62 |
| 63 String getterName(SourceString name) { | 63 String getterName(SourceString name) { |
| 64 return 'get\$$name'; | 64 return 'get\$$name'; |
| 65 } | 65 } |
| 66 | 66 |
| 67 String getFreshGlobalName(String proposedName) { |
| 68 int usedCount = usedGlobals[proposedName]; |
| 69 if (usedCount === null) { |
| 70 // No element with this name has been used before. |
| 71 usedGlobals[proposedName] = 1; |
| 72 return proposedName; |
| 73 } else { |
| 74 // Not the first time we see this name. Append a number to make it unique. |
| 75 String name; |
| 76 do { |
| 77 usedCount++; |
| 78 name = '$proposedName$usedCount'; |
| 79 } while (usedGlobals[name] !== null); |
| 80 usedGlobals[proposedName] = usedCount; |
| 81 return name; |
| 82 } |
| 83 } |
| 84 |
| 67 /** | 85 /** |
| 68 * Returns a preferred JS-id for the given top-level or static element. | 86 * Returns a preferred JS-id for the given top-level or static element. |
| 69 * The returned id is guaranteed to be a valid JS-id. | 87 * The returned id is guaranteed to be a valid JS-id. |
| 70 */ | 88 */ |
| 71 String _computeGuess(Element element) { | 89 String _computeGuess(Element element) { |
| 72 assert(!element.isInstanceMember()); | 90 assert(!element.isInstanceMember()); |
| 73 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { | 91 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 74 FunctionElement functionElement = element; | 92 FunctionElement functionElement = element; |
| 75 return instanceMethodName( | 93 return instanceMethodName( |
| 76 element.name, functionElement.parameterCount(compiler)); | 94 element.name, functionElement.parameterCount(compiler)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 103 /** | 121 /** |
| 104 * Returns a preferred JS-id for the given element. The returned id is | 122 * Returns a preferred JS-id for the given element. The returned id is |
| 105 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 123 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 106 * guaranteed to be unique. | 124 * guaranteed to be unique. |
| 107 * | 125 * |
| 108 * For accessing statics consider calling | 126 * For accessing statics consider calling |
| 109 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 127 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
| 110 */ | 128 */ |
| 111 String getName(Element element) { | 129 String getName(Element element) { |
| 112 if (element.isInstanceMember()) { | 130 if (element.isInstanceMember()) { |
| 113 SourceString name; | |
| 114 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { | 131 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { |
| 115 ConstructorBodyElement bodyElement = element; | 132 ConstructorBodyElement bodyElement = element; |
| 116 SourceString name = bodyElement.constructor.name; | 133 SourceString name = bodyElement.constructor.name; |
| 117 return instanceMethodName(name, bodyElement.parameterCount(compiler)); | 134 return instanceMethodName(name, bodyElement.parameterCount(compiler)); |
| 118 } else if (element.kind == ElementKind.FUNCTION) { | 135 } else if (element.kind == ElementKind.FUNCTION) { |
| 119 FunctionElement functionElement = element; | 136 FunctionElement functionElement = element; |
| 120 return instanceMethodName( | 137 return instanceMethodName( |
| 121 element.name, functionElement.parameterCount(compiler)); | 138 element.name, functionElement.parameterCount(compiler)); |
| 122 } else if (element.kind == ElementKind.GETTER) { | 139 } else if (element.kind == ElementKind.GETTER) { |
| 123 return getterName(element.name); | 140 return getterName(element.name); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 137 case ElementKind.PARAMETER: | 154 case ElementKind.PARAMETER: |
| 138 // The name is not guaranteed to be unique. | 155 // The name is not guaranteed to be unique. |
| 139 return guess; | 156 return guess; |
| 140 | 157 |
| 141 case ElementKind.GENERATIVE_CONSTRUCTOR: | 158 case ElementKind.GENERATIVE_CONSTRUCTOR: |
| 142 case ElementKind.FUNCTION: | 159 case ElementKind.FUNCTION: |
| 143 case ElementKind.CLASS: | 160 case ElementKind.CLASS: |
| 144 case ElementKind.FIELD: | 161 case ElementKind.FIELD: |
| 145 case ElementKind.GETTER: | 162 case ElementKind.GETTER: |
| 146 case ElementKind.SETTER: | 163 case ElementKind.SETTER: |
| 147 // We need to make sure the name is unique. | 164 String result = getFreshGlobalName(guess); |
| 148 int usedCount = usedGlobals[guess]; | 165 globals[element] = result; |
| 149 if (usedCount === null) { | 166 return result; |
| 150 // No element with this name has been used before. | |
| 151 usedGlobals[guess] = 1; | |
| 152 globals[element] = guess; | |
| 153 return guess; | |
| 154 } else { | |
| 155 // Not the first time we see an element with this name. Append a | |
| 156 // number to make it unique. | |
| 157 String name; | |
| 158 do { | |
| 159 usedCount++; | |
| 160 name = '$guess$usedCount'; | |
| 161 } while (usedGlobals[name] !== null); | |
| 162 usedGlobals[guess] = usedCount; | |
| 163 globals[element] = name; | |
| 164 return name; | |
| 165 } | |
| 166 | 167 |
| 167 default: | 168 default: |
| 168 compiler.internalError('getName for unknown kind: ${element.kind}', | 169 compiler.internalError('getName for unknown kind: ${element.kind}', |
| 169 node: element.parseNode(compiler)); | 170 node: element.parseNode(compiler)); |
| 170 } | 171 } |
| 171 } | 172 } |
| 172 } | 173 } |
| 173 | 174 |
| 174 String isolateAccess(Element element) { | 175 String isolateAccess(Element element) { |
| 175 return "$CURRENT_ISOLATE.${getName(element)}"; | 176 return "$CURRENT_ISOLATE.${getName(element)}"; |
| 176 } | 177 } |
| 177 | 178 |
| 178 String isolatePropertyAccess(Element element) { | 179 String isolatePropertyAccess(Element element) { |
| 179 return "$ISOLATE.prototype.${getName(element)}"; | 180 return "$ISOLATE.prototype.${getName(element)}"; |
| 180 } | 181 } |
| 181 | 182 |
| 182 String isolateBailoutPropertyAccess(Element element) { | 183 String isolateBailoutPropertyAccess(Element element) { |
| 183 return '${isolatePropertyAccess(element)}\$bailout'; | 184 return '${isolatePropertyAccess(element)}\$bailout'; |
| 184 } | 185 } |
| 185 | 186 |
| 186 String isolateBailoutAccess(Element element) { | 187 String isolateBailoutAccess(Element element) { |
| 187 return '${isolateAccess(element)}\$bailout'; | 188 return '${isolateAccess(element)}\$bailout'; |
| 188 } | 189 } |
| 189 | 190 |
| 190 String operatorIs(ClassElement element) { | 191 String operatorIs(ClassElement element) { |
| 191 return 'is\$${getName(element)}'; | 192 return 'is\$${getName(element)}'; |
| 192 } | 193 } |
| 193 } | 194 } |
| OLD | NEW |