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 /** | 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 12 matching lines...) Expand all Loading... | |
| 89 } | 107 } |
| 90 // Prefix the name with '$' if it is reserved. | 108 // Prefix the name with '$' if it is reserved. |
| 91 if (jsReserved.contains(name)) { | 109 if (jsReserved.contains(name)) { |
| 92 name = "\$$name"; | 110 name = "\$$name"; |
| 93 assert(!jsReserved.contains(name)); | 111 assert(!jsReserved.contains(name)); |
| 94 } | 112 } |
| 95 return name; | 113 return name; |
| 96 } | 114 } |
| 97 } | 115 } |
| 98 | 116 |
| 117 String _computeFreshGlobalName(Element element) { | |
|
ngeoffray
2012/02/16 12:38:46
I would remove this method, it's only called once,
floitsch
2012/02/16 13:43:07
Done.
| |
| 118 String guess = _computeGuess(element); | |
| 119 return getFreshGlobalName(guess); | |
| 120 } | |
| 121 | |
| 99 String getBailoutName(Element element) { | 122 String getBailoutName(Element element) { |
| 100 return '${getName(element)}\$bailout'; | 123 return '${getName(element)}\$bailout'; |
| 101 } | 124 } |
| 102 | 125 |
| 103 /** | 126 /** |
| 104 * Returns a preferred JS-id for the given element. The returned id is | 127 * 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 | 128 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 106 * guaranteed to be unique. | 129 * guaranteed to be unique. |
| 107 * | 130 * |
| 108 * For accessing statics consider calling | 131 * For accessing statics consider calling |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 124 } else if (element.kind == ElementKind.SETTER) { | 147 } else if (element.kind == ElementKind.SETTER) { |
| 125 return setterName(element.name); | 148 return setterName(element.name); |
| 126 } else { | 149 } else { |
| 127 return instanceFieldName(element.name); | 150 return instanceFieldName(element.name); |
| 128 } | 151 } |
| 129 } else { | 152 } else { |
| 130 // Dealing with a top-level or static element. | 153 // Dealing with a top-level or static element. |
| 131 String cached = globals[element]; | 154 String cached = globals[element]; |
| 132 if (cached !== null) return cached; | 155 if (cached !== null) return cached; |
| 133 | 156 |
| 134 String guess = _computeGuess(element); | |
| 135 switch (element.kind) { | 157 switch (element.kind) { |
| 136 case ElementKind.VARIABLE: | 158 case ElementKind.VARIABLE: |
| 137 case ElementKind.PARAMETER: | 159 case ElementKind.PARAMETER: |
| 138 // The name is not guaranteed to be unique. | 160 // The name is not guaranteed to be unique. |
| 139 return guess; | 161 return _computeGuess(element); |
| 140 | 162 |
| 141 case ElementKind.GENERATIVE_CONSTRUCTOR: | 163 case ElementKind.GENERATIVE_CONSTRUCTOR: |
| 142 case ElementKind.FUNCTION: | 164 case ElementKind.FUNCTION: |
| 143 case ElementKind.CLASS: | 165 case ElementKind.CLASS: |
| 144 case ElementKind.FIELD: | 166 case ElementKind.FIELD: |
| 145 case ElementKind.GETTER: | 167 case ElementKind.GETTER: |
| 146 case ElementKind.SETTER: | 168 case ElementKind.SETTER: |
| 147 // We need to make sure the name is unique. | 169 String result = _computeFreshGlobalName(element); |
| 148 int usedCount = usedGlobals[guess]; | 170 globals[element] = result; |
| 149 if (usedCount === null) { | 171 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 | 172 |
| 167 default: | 173 default: |
| 168 compiler.internalError('getName for unknown kind: ${element.kind}', | 174 compiler.internalError('getName for unknown kind: ${element.kind}', |
| 169 node: element.parseNode(compiler)); | 175 node: element.parseNode(compiler)); |
| 170 } | 176 } |
| 171 } | 177 } |
| 172 } | 178 } |
| 173 | 179 |
| 174 String isolateAccess(Element element) { | 180 String isolateAccess(Element element) { |
| 175 return "$CURRENT_ISOLATE.${getName(element)}"; | 181 return "$CURRENT_ISOLATE.${getName(element)}"; |
| 176 } | 182 } |
| 177 | 183 |
| 178 String isolatePropertyAccess(Element element) { | 184 String isolatePropertyAccess(Element element) { |
| 179 return "$ISOLATE.prototype.${getName(element)}"; | 185 return "$ISOLATE.prototype.${getName(element)}"; |
| 180 } | 186 } |
| 181 | 187 |
| 182 String isolateBailoutPropertyAccess(Element element) { | 188 String isolateBailoutPropertyAccess(Element element) { |
| 183 return '${isolatePropertyAccess(element)}\$bailout'; | 189 return '${isolatePropertyAccess(element)}\$bailout'; |
| 184 } | 190 } |
| 185 | 191 |
| 186 String isolateBailoutAccess(Element element) { | 192 String isolateBailoutAccess(Element element) { |
| 187 return '${isolateAccess(element)}\$bailout'; | 193 return '${isolateAccess(element)}\$bailout'; |
| 188 } | 194 } |
| 189 | 195 |
| 190 String operatorIs(ClassElement element) { | 196 String operatorIs(ClassElement element) { |
| 191 return 'is\$${getName(element)}'; | 197 return 'is\$${getName(element)}'; |
| 192 } | 198 } |
| 193 } | 199 } |
| OLD | NEW |