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 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
| 9 */ | 9 */ |
| 10 class MinifyNamer extends Namer { | 10 class MinifyNamer extends Namer { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 final ALPHABET_CHARACTERS = 52; // a-zA-Z. | 24 final ALPHABET_CHARACTERS = 52; // a-zA-Z. |
| 25 final ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. | 25 final ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. |
| 26 | 26 |
| 27 _FieldNamingRegistry fieldRegistry; | 27 _FieldNamingRegistry fieldRegistry; |
| 28 | 28 |
| 29 // You can pass an invalid identifier to this and unlike its non-minifying | 29 // You can pass an invalid identifier to this and unlike its non-minifying |
| 30 // counterpart it will never return the proposedName as the new fresh name. | 30 // counterpart it will never return the proposedName as the new fresh name. |
| 31 String getFreshName(String proposedName, | 31 String getFreshName(String proposedName, |
| 32 Set<String> usedNames, | 32 Set<String> usedNames, |
| 33 Map<String, String> suggestedNames, | 33 Map<String, String> suggestedNames, |
| 34 {bool ensureSafe: true}) { | 34 {bool ensureSafe, bool sanitizeForAnnotations}) { |
| 35 var freshName; | 35 String freshName; |
| 36 var suggestion = suggestedNames[proposedName]; | 36 String suggestion = suggestedNames[proposedName]; |
| 37 if (suggestion != null && !usedNames.contains(suggestion)) { | 37 if (suggestion != null && !usedNames.contains(suggestion)) { |
| 38 freshName = suggestion; | 38 freshName = suggestion; |
| 39 } else { | 39 } else { |
| 40 freshName = _getUnusedName(proposedName, usedNames, | 40 freshName = _getUnusedName(proposedName, usedNames, |
| 41 suggestedNames.values); | 41 suggestedNames.values); |
| 42 } | 42 } |
| 43 usedNames.add(freshName); | 43 usedNames.add(freshName); |
| 44 return freshName; | 44 return freshName; |
| 45 } | 45 } |
| 46 | 46 |
| 47 String getClosureVariableName(String name, int id) { | 47 String getClosureVariableName(String _, int id) { |
| 48 if (id < ALPHABET_CHARACTERS) { | 48 if (id < ALPHABET_CHARACTERS) { |
| 49 return new String.fromCharCodes([_letterNumber(id)]); | 49 String name = new String.fromCharCodes([_letterNumber(id)]); |
| 50 // The names 'g' and 's' are the getter/setter for the 'call' property. | |
| 51 // These cannot be used as a closure fields. | |
| 52 if (!_hasBannedPrefix(name)) return name; | |
|
asgerf
2015/01/30 15:20:29
I believe this was a bug, although I admit I was n
| |
| 50 } | 53 } |
| 51 return "${getMappedInstanceName('closure')}_$id"; | 54 // Fall back to a slightly longer name. |
| 55 String basename = disambiguateMember(null, 'closure'); | |
| 56 return '${basename}_$id'; | |
| 52 } | 57 } |
| 53 | 58 |
| 54 // From issue 7554. These should not be used on objects (as instance | 59 // From issue 7554. These should not be used on objects (as instance |
| 55 // variables) because they clash with names from the DOM. However, it is | 60 // variables) because they clash with names from the DOM. However, it is |
| 56 // OK to use them as fields, as we only access fields directly if we know | 61 // OK to use them as fields, as we only access fields directly if we know |
| 57 // the receiver type. | 62 // the receiver type. |
| 58 static const _reservedNativeProperties = const <String>[ | 63 static const List<String> _reservedNativeProperties = const <String>[ |
| 59 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', | 64 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', |
| 60 // 2-letter: | 65 // 2-letter: |
| 61 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', | 66 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', |
| 62 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', | 67 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', |
| 63 // 3-letter: | 68 // 3-letter: |
| 64 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', | 69 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', |
| 65 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', | 70 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', |
| 66 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', | 71 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', |
| 67 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', | 72 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', |
| 68 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', | 73 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', |
| 69 // 4-letter: | 74 // 4-letter: |
| 70 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', | 75 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', |
| 71 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', | 76 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', |
| 72 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', | 77 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', |
| 73 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', | 78 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', |
| 74 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', | 79 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', |
| 75 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', | 80 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', |
| 76 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', | 81 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', |
| 77 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', | 82 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', |
| 78 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', | 83 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', |
| 79 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', | 84 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', |
| 80 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; | 85 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; |
| 81 | 86 |
| 82 void reserveBackendNames() { | 87 void reserveBackendNames() { |
| 83 for (var name in _reservedNativeProperties) { | 88 Map<String,String> publicMembers = |
| 89 instanceMembers.putIfAbsent(null, () => new Map<String, String>()); | |
| 90 for (String name in _reservedNativeProperties) { | |
| 84 if (name.length < 2) { | 91 if (name.length < 2) { |
| 85 instanceNameMap[name] = name; | 92 publicMembers[name] = name; |
| 86 } | 93 } |
| 87 usedInstanceNames.add(name); | 94 usedInstanceNames.add(name); |
| 88 // Getter and setter names are autogenerated by prepending 'g' and 's' to | 95 // Getter and setter names are autogenerated by prepending 'g' and 's' to |
| 89 // field names. Therefore there are some field names we don't want to | 96 // field names. Therefore there are some field names we don't want to |
| 90 // use. It is implicit in the next line that the banned prefix is | 97 // use. It is implicit in the next line that the banned prefix is |
| 91 // only one character. | 98 // only one character. |
| 92 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); | 99 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); |
| 93 } | 100 } |
| 94 | 101 |
| 95 // These popular names are present in most programs and deserve | 102 // These popular names are present in most programs and deserve |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 void _populateSuggestedNames(Map<String, String> suggestionMap, | 144 void _populateSuggestedNames(Map<String, String> suggestionMap, |
| 138 Set<String> used, | 145 Set<String> used, |
| 139 List<String> suggestions) { | 146 List<String> suggestions) { |
| 140 int c = $a - 1; | 147 int c = $a - 1; |
| 141 String letter; | 148 String letter; |
| 142 for (String name in suggestions) { | 149 for (String name in suggestions) { |
| 143 do { | 150 do { |
| 144 assert(c != $Z); | 151 assert(c != $Z); |
| 145 c = (c == $z) ? $A : c + 1; | 152 c = (c == $z) ? $A : c + 1; |
| 146 letter = new String.fromCharCodes([c]); | 153 letter = new String.fromCharCodes([c]); |
| 147 } while (used.contains(letter)); | 154 } while (used.contains(letter) || _hasBannedPrefix(letter)); |
|
asgerf
2015/01/30 15:20:29
Same as above, it can clash with the getter for th
| |
| 148 assert(suggestionMap[name] == null); | 155 assert(suggestionMap[name] == null); |
| 149 suggestionMap[name] = letter; | 156 suggestionMap[name] = letter; |
| 150 } | 157 } |
| 151 } | 158 } |
| 152 | 159 |
| 153 | 160 |
| 154 // This gets a minified name based on a hash of the proposed name. This | 161 // This gets a minified name based on a hash of the proposed name. This |
| 155 // is slightly less efficient than just getting the next name in a series, | 162 // is slightly less efficient than just getting the next name in a series, |
| 156 // but it means that small changes in the input program will give smallish | 163 // but it means that small changes in the input program will give smallish |
| 157 // changes in the output, which can be useful for diffing etc. | 164 // changes in the output, which can be useful for diffing etc. |
| 158 String _getUnusedName(String proposedName, Set<String> usedNames, | 165 String _getUnusedName(String proposedName, Set<String> usedNames, |
| 159 Iterable<String> suggestions) { | 166 Iterable<String> suggestions) { |
| 160 int hash = _calculateHash(proposedName); | 167 int hash = _calculateHash(proposedName); |
| 161 // Avoid very small hashes that won't try many names. | 168 // Avoid very small hashes that won't try many names. |
| 162 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime. | 169 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime. |
| 163 | 170 |
| 164 // Try other n-character names based on the hash. We try one to three | 171 // Try other n-character names based on the hash. We try one to three |
| 165 // character identifiers. For each length we try around 10 different names | 172 // character identifiers. For each length we try around 10 different names |
| 166 // in a predictable order determined by the proposed name. This is in order | 173 // in a predictable order determined by the proposed name. This is in order |
| 167 // to make the renamer stable: small changes in the input should nornally | 174 // to make the renamer stable: small changes in the input should nornally |
| 168 // result in relatively small changes in the output. | 175 // result in relatively small changes in the output. |
| 169 for (var n = 1; n <= 3; n++) { | 176 for (int n = 1; n <= 3; n++) { |
| 170 int h = hash; | 177 int h = hash; |
| 171 while (h > 10) { | 178 while (h > 10) { |
| 172 var codes = <int>[_letterNumber(h)]; | 179 List<int> codes = <int>[_letterNumber(h)]; |
| 173 int h2 = h ~/ ALPHABET_CHARACTERS; | 180 int h2 = h ~/ ALPHABET_CHARACTERS; |
| 174 for (var i = 1; i < n; i++) { | 181 for (int i = 1; i < n; i++) { |
| 175 codes.add(_alphaNumericNumber(h2)); | 182 codes.add(_alphaNumericNumber(h2)); |
| 176 h2 ~/= ALPHANUMERIC_CHARACTERS; | 183 h2 ~/= ALPHANUMERIC_CHARACTERS; |
| 177 } | 184 } |
| 178 final candidate = new String.fromCharCodes(codes); | 185 final candidate = new String.fromCharCodes(codes); |
| 179 if (!usedNames.contains(candidate) && | 186 if (!usedNames.contains(candidate) && |
| 180 !jsReserved.contains(candidate) && | 187 !jsReserved.contains(candidate) && |
| 181 !_hasBannedPrefix(candidate) && | 188 !_hasBannedPrefix(candidate) && |
| 182 (n != 1 || !suggestions.contains(candidate))) { | 189 (n != 1 || !suggestions.contains(candidate))) { |
| 183 return candidate; | 190 return candidate; |
| 184 } | 191 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 206 h &= 0xffffffff; | 213 h &= 0xffffffff; |
| 207 h ^= h >> 6; | 214 h ^= h >> 6; |
| 208 h &= 0xffffffff; | 215 h &= 0xffffffff; |
| 209 } | 216 } |
| 210 return h; | 217 return h; |
| 211 } | 218 } |
| 212 | 219 |
| 213 /// If we can't find a hash based name in the three-letter space, then base | 220 /// If we can't find a hash based name in the three-letter space, then base |
| 214 /// the name on a letter and a counter. | 221 /// the name on a letter and a counter. |
| 215 String _badName(int hash, Set<String> usedNames) { | 222 String _badName(int hash, Set<String> usedNames) { |
| 216 var startLetter = new String.fromCharCodes([_letterNumber(hash)]); | 223 String startLetter = new String.fromCharCodes([_letterNumber(hash)]); |
| 217 var name; | 224 String name; |
| 218 var i = 0; | 225 int i = 0; |
| 219 do { | 226 do { |
| 220 name = "$startLetter${i++}"; | 227 name = "$startLetter${i++}"; |
| 221 } while (usedNames.contains(name)); | 228 } while (usedNames.contains(name)); |
| 222 // We don't need to check for banned prefix because the name is in the form | 229 // We don't need to check for banned prefix because the name is in the form |
| 223 // xnnn, where nnn is a number. There can be no getter or setter called | 230 // xnnn, where nnn is a number. There can be no getter or setter called |
| 224 // gnnn since that would imply a numeric field name. | 231 // gnnn since that would imply a numeric field name. |
| 225 return name; | 232 return name; |
| 226 } | 233 } |
| 227 | 234 |
| 228 int _letterNumber(int x) { | 235 int _letterNumber(int x) { |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 Map<Entity, String> get names => registry.globalNames; | 422 Map<Entity, String> get names => registry.globalNames; |
| 416 | 423 |
| 417 _MixinFieldNamingScope.mixin(ClassElement cls, _FieldNamingRegistry registry) | 424 _MixinFieldNamingScope.mixin(ClassElement cls, _FieldNamingRegistry registry) |
| 418 : super.rootScope(cls, registry); | 425 : super.rootScope(cls, registry); |
| 419 | 426 |
| 420 _MixinFieldNamingScope.mixedIn(MixinApplicationElement container, | 427 _MixinFieldNamingScope.mixedIn(MixinApplicationElement container, |
| 421 _FieldNamingScope superScope, _FieldNamingRegistry registry) | 428 _FieldNamingScope superScope, _FieldNamingRegistry registry) |
| 422 : super.inherit(container, superScope, registry); | 429 : super.inherit(container, superScope, registry); |
| 423 | 430 |
| 424 String _nextName() { | 431 String _nextName() { |
| 425 var proposed = super._nextName(); | 432 String proposed = super._nextName(); |
| 426 return proposed + r'$'; | 433 return proposed + r'$'; |
| 427 } | 434 } |
| 428 } | 435 } |
| 429 | 436 |
| 430 /** | 437 /** |
| 431 * [BoxFieldElement] fields work differently in that they do not belong to an | 438 * [BoxFieldElement] fields work differently in that they do not belong to an |
| 432 * actual class but an anonymous box associated to a [Local]. As there is no | 439 * actual class but an anonymous box associated to a [Local]. As there is no |
| 433 * inheritance chain, we do not need to compute fields a priori but can assign | 440 * inheritance chain, we do not need to compute fields a priori but can assign |
| 434 * names on the fly. | 441 * names on the fly. |
| 435 */ | 442 */ |
| 436 class _BoxFieldNamingScope extends _FieldNamingScope { | 443 class _BoxFieldNamingScope extends _FieldNamingScope { |
| 437 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : | 444 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : |
| 438 super.rootScope(box, registry); | 445 super.rootScope(box, registry); |
| 439 | 446 |
| 440 bool containsField(_) => true; | 447 bool containsField(_) => true; |
| 441 | 448 |
| 442 String operator[](Element field) { | 449 String operator[](Element field) { |
| 443 if (!names.containsKey(field)) add(field); | 450 if (!names.containsKey(field)) add(field); |
| 444 return names[field]; | 451 return names[field]; |
| 445 } | 452 } |
| 446 } | 453 } |
| OLD | NEW |