| 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 16 matching lines...) Expand all Loading... |
| 27 Namer(this.compiler) | 27 Namer(this.compiler) |
| 28 : globals = new Map<Element, String>(), | 28 : globals = new Map<Element, String>(), |
| 29 usedGlobals = new Map<String, int>(); | 29 usedGlobals = new Map<String, int>(); |
| 30 | 30 |
| 31 final String CURRENT_ISOLATE = "\$"; | 31 final String CURRENT_ISOLATE = "\$"; |
| 32 final String ISOLATE = "Isolate"; | 32 final String ISOLATE = "Isolate"; |
| 33 | 33 |
| 34 | 34 |
| 35 String closureInvocationName(Selector selector) { | 35 String closureInvocationName(Selector selector) { |
| 36 // TODO(floitsch): mangle, while not conflicting with instance names. | 36 // TODO(floitsch): mangle, while not conflicting with instance names. |
| 37 return instanceMethodInvocationName(CLOSURE_INVOCATION_NAME, selector); | 37 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME, |
| 38 selector); |
| 38 } | 39 } |
| 39 | 40 |
| 40 String instanceMethodName(SourceString name, int arity) { | 41 String privateName(LibraryElement lib, SourceString name) { |
| 41 // TODO(floitsch): mangle, while preserving uniqueness. | 42 if (name.isPrivate()) { |
| 42 return '${name.slowToString()}\$$arity'; | 43 return '_${getName(lib)}${name.slowToString()}'; |
| 44 } else { |
| 45 return '${name.slowToString()}'; |
| 46 } |
| 43 } | 47 } |
| 44 | 48 |
| 45 String instanceMethodInvocationName(SourceString name, Selector selector) { | 49 String instanceMethodName(LibraryElement lib, SourceString name, int arity) { |
| 50 return '${privateName(lib, name)}\$$arity'; |
| 51 } |
| 52 |
| 53 String instanceMethodInvocationName(LibraryElement lib, SourceString name, |
| 54 Selector selector) { |
| 46 // TODO(floitsch): mangle, while preserving uniqueness. | 55 // TODO(floitsch): mangle, while preserving uniqueness. |
| 47 StringBuffer buffer = new StringBuffer(); | 56 StringBuffer buffer = new StringBuffer(); |
| 48 List<SourceString> names = selector.getOrderedNamedArguments(); | 57 List<SourceString> names = selector.getOrderedNamedArguments(); |
| 49 for (SourceString argumentName in names) { | 58 for (SourceString argumentName in names) { |
| 50 buffer.add(@'$'); | 59 buffer.add(@'$'); |
| 51 argumentName.printOn(buffer); | 60 argumentName.printOn(buffer); |
| 52 } | 61 } |
| 53 return '${name.slowToString()}\$${selector.argumentCount}$buffer'; | 62 return '${privateName(lib, name)}\$${selector.argumentCount}$buffer'; |
| 54 } | 63 } |
| 55 | 64 |
| 56 String instanceFieldName(SourceString name) { | 65 String instanceFieldName(LibraryElement lib, SourceString name) { |
| 57 return name.slowToString(); | 66 return privateName(lib, name); |
| 58 } | 67 } |
| 59 | 68 |
| 60 String setterName(SourceString name) { | 69 String setterName(LibraryElement lib, SourceString name) { |
| 61 return 'set\$${name.slowToString()}'; | 70 return 'set\$${privateName(lib, name)}'; |
| 62 } | 71 } |
| 63 | 72 |
| 64 String getterName(SourceString name) { | 73 String getterName(LibraryElement lib, SourceString name) { |
| 65 return 'get\$${name.slowToString()}'; | 74 return 'get\$${privateName(lib, name)}'; |
| 66 } | 75 } |
| 67 | 76 |
| 68 String getFreshGlobalName(String proposedName) { | 77 String getFreshGlobalName(String proposedName) { |
| 69 int usedCount = usedGlobals[proposedName]; | 78 int usedCount = usedGlobals[proposedName]; |
| 70 if (usedCount === null) { | 79 if (usedCount === null) { |
| 71 // No element with this name has been used before. | 80 // No element with this name has been used before. |
| 72 usedGlobals[proposedName] = 1; | 81 usedGlobals[proposedName] = 1; |
| 73 return proposedName; | 82 return proposedName; |
| 74 } else { | 83 } else { |
| 75 // Not the first time we see this name. Append a number to make it unique. | 84 // Not the first time we see this name. Append a number to make it unique. |
| 76 String name; | 85 String name; |
| 77 do { | 86 do { |
| 78 usedCount++; | 87 usedCount++; |
| 79 name = '$proposedName$usedCount'; | 88 name = '$proposedName$usedCount'; |
| 80 } while (usedGlobals[name] !== null); | 89 } while (usedGlobals[name] !== null); |
| 81 usedGlobals[proposedName] = usedCount; | 90 usedGlobals[proposedName] = usedCount; |
| 82 return name; | 91 return name; |
| 83 } | 92 } |
| 84 } | 93 } |
| 85 | 94 |
| 86 /** | 95 /** |
| 87 * Returns a preferred JS-id for the given top-level or static element. | 96 * Returns a preferred JS-id for the given top-level or static element. |
| 88 * The returned id is guaranteed to be a valid JS-id. | 97 * The returned id is guaranteed to be a valid JS-id. |
| 89 */ | 98 */ |
| 90 String _computeGuess(Element element) { | 99 String _computeGuess(Element element) { |
| 91 assert(!element.isInstanceMember()); | 100 assert(!element.isInstanceMember()); |
| 101 LibraryElement lib = element.getLibrary(); |
| 92 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { | 102 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 93 FunctionElement functionElement = element; | 103 FunctionElement functionElement = element; |
| 94 return instanceMethodName( | 104 return instanceMethodName(lib, element.name, |
| 95 element.name, functionElement.parameterCount(compiler)); | 105 functionElement.parameterCount(compiler)); |
| 96 } else { | 106 } else { |
| 97 // TODO(floitsch): deal with named constructors. | 107 // TODO(floitsch): deal with named constructors. |
| 98 String name; | 108 String name; |
| 99 if (element.kind == ElementKind.GETTER) { | 109 if (Elements.isStaticOrTopLevel(element)) { |
| 100 name = getterName(element.name); | 110 name = element.name.slowToString(); |
| 111 } else if (element.kind == ElementKind.GETTER) { |
| 112 name = getterName(lib, element.name); |
| 101 } else if (element.kind == ElementKind.SETTER) { | 113 } else if (element.kind == ElementKind.SETTER) { |
| 102 name = setterName(element.name); | 114 name = setterName(lib, element.name); |
| 103 } else if (element.kind == ElementKind.FUNCTION) { | 115 } else if (element.kind == ElementKind.FUNCTION) { |
| 104 FunctionElement functionElement = element; | 116 FunctionElement functionElement = element; |
| 105 name = element.name.slowToString(); | 117 name = element.name.slowToString(); |
| 106 name = '$name\$${functionElement.parameterCount(compiler)}'; | 118 name = '$name\$${functionElement.parameterCount(compiler)}'; |
| 119 } else if (element.kind === ElementKind.LIBRARY) { |
| 120 name = 'lib'; |
| 107 } else { | 121 } else { |
| 108 name = '${element.name.slowToString()}'; | 122 name = element.name.slowToString(); |
| 109 } | 123 } |
| 110 // Prefix the name with '$' if it is reserved. | 124 // Prefix the name with '$' if it is reserved. |
| 111 if (jsReserved.contains(name)) { | 125 if (jsReserved.contains(name)) { |
| 112 name = "\$$name"; | 126 name = "\$$name"; |
| 113 assert(!jsReserved.contains(name)); | 127 assert(!jsReserved.contains(name)); |
| 114 } | 128 } |
| 115 return name; | 129 return name; |
| 116 } | 130 } |
| 117 } | 131 } |
| 118 | 132 |
| 119 String getBailoutName(Element element) { | 133 String getBailoutName(Element element) { |
| 120 return '${getName(element)}\$bailout'; | 134 return '${getName(element)}\$bailout'; |
| 121 } | 135 } |
| 122 | 136 |
| 123 /** | 137 /** |
| 124 * Returns a preferred JS-id for the given element. The returned id is | 138 * Returns a preferred JS-id for the given element. The returned id is |
| 125 * guaranteed to be a valid JS-id. Globals and static fields are furthermore | 139 * guaranteed to be a valid JS-id. Globals and static fields are furthermore |
| 126 * guaranteed to be unique. | 140 * guaranteed to be unique. |
| 127 * | 141 * |
| 128 * For accessing statics consider calling | 142 * For accessing statics consider calling |
| 129 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. | 143 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. |
| 130 */ | 144 */ |
| 131 String getName(Element element) { | 145 String getName(Element element) { |
| 132 if (element.isInstanceMember()) { | 146 if (element.isInstanceMember()) { |
| 133 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { | 147 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { |
| 134 ConstructorBodyElement bodyElement = element; | 148 ConstructorBodyElement bodyElement = element; |
| 135 SourceString name = bodyElement.constructor.name; | 149 SourceString name = bodyElement.constructor.name; |
| 136 return instanceMethodName(name, bodyElement.parameterCount(compiler)); | 150 return instanceMethodName(element.getLibrary(), |
| 151 name, bodyElement.parameterCount(compiler)); |
| 137 } else if (element.kind == ElementKind.FUNCTION) { | 152 } else if (element.kind == ElementKind.FUNCTION) { |
| 138 FunctionElement functionElement = element; | 153 FunctionElement functionElement = element; |
| 139 return instanceMethodName( | 154 return instanceMethodName(element.getLibrary(), |
| 140 element.name, functionElement.parameterCount(compiler)); | 155 element.name, |
| 156 functionElement.parameterCount(compiler)); |
| 141 } else if (element.kind == ElementKind.GETTER) { | 157 } else if (element.kind == ElementKind.GETTER) { |
| 142 return getterName(element.name); | 158 return getterName(element.getLibrary(), element.name); |
| 143 } else if (element.kind == ElementKind.SETTER) { | 159 } else if (element.kind == ElementKind.SETTER) { |
| 144 return setterName(element.name); | 160 return setterName(element.getLibrary(), element.name); |
| 145 } else { | 161 } else { |
| 146 return instanceFieldName(element.name); | 162 return instanceFieldName(element.getLibrary(), element.name); |
| 147 } | 163 } |
| 148 } else { | 164 } else { |
| 149 // Dealing with a top-level or static element. | 165 // Dealing with a top-level or static element. |
| 150 String cached = globals[element]; | 166 String cached = globals[element]; |
| 151 if (cached !== null) return cached; | 167 if (cached !== null) return cached; |
| 152 | 168 |
| 153 String guess = _computeGuess(element); | 169 String guess = _computeGuess(element); |
| 154 switch (element.kind) { | 170 switch (element.kind) { |
| 155 case ElementKind.VARIABLE: | 171 case ElementKind.VARIABLE: |
| 156 case ElementKind.PARAMETER: | 172 case ElementKind.PARAMETER: |
| 157 // The name is not guaranteed to be unique. | 173 // The name is not guaranteed to be unique. |
| 158 return guess; | 174 return guess; |
| 159 | 175 |
| 160 case ElementKind.GENERATIVE_CONSTRUCTOR: | 176 case ElementKind.GENERATIVE_CONSTRUCTOR: |
| 161 case ElementKind.FUNCTION: | 177 case ElementKind.FUNCTION: |
| 162 case ElementKind.CLASS: | 178 case ElementKind.CLASS: |
| 163 case ElementKind.FIELD: | 179 case ElementKind.FIELD: |
| 164 case ElementKind.GETTER: | 180 case ElementKind.GETTER: |
| 165 case ElementKind.SETTER: | 181 case ElementKind.SETTER: |
| 166 case ElementKind.TYPEDEF: | 182 case ElementKind.TYPEDEF: |
| 183 case ElementKind.LIBRARY: |
| 167 String result = getFreshGlobalName(guess); | 184 String result = getFreshGlobalName(guess); |
| 168 globals[element] = result; | 185 globals[element] = result; |
| 169 return result; | 186 return result; |
| 170 | 187 |
| 171 default: | 188 default: |
| 172 compiler.internalError('getName for unknown kind: ${element.kind}', | 189 compiler.internalError('getName for unknown kind: ${element.kind}', |
| 173 node: element.parseNode(compiler)); | 190 node: element.parseNode(compiler)); |
| 174 } | 191 } |
| 175 } | 192 } |
| 176 } | 193 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 188 } | 205 } |
| 189 | 206 |
| 190 String isolateBailoutAccess(Element element) { | 207 String isolateBailoutAccess(Element element) { |
| 191 return '${isolateAccess(element)}\$bailout'; | 208 return '${isolateAccess(element)}\$bailout'; |
| 192 } | 209 } |
| 193 | 210 |
| 194 String operatorIs(Element element) { | 211 String operatorIs(Element element) { |
| 195 return 'is\$${getName(element)}'; | 212 return 'is\$${getName(element)}'; |
| 196 } | 213 } |
| 197 } | 214 } |
| OLD | NEW |