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 sanitizeForNatives, bool sanitizeForAnnotations}) { |
floitsch
2015/02/06 12:59:55
Add comment that sanitizeForNatives/sanitizeForAnn
asgerf
2015/02/06 13:27:06
Done.
| |
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 return new String.fromCharCodes([_letterNumber(id)]); |
50 } | 50 } |
51 return "${getMappedInstanceName('closure')}_$id"; | 51 // Fall back to a slightly longer name. |
52 String basename = _disambiguateMember(null, 'closure'); | |
53 return '${basename}_$id'; | |
52 } | 54 } |
53 | 55 |
54 // From issue 7554. These should not be used on objects (as instance | 56 // 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 | 57 // 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 | 58 // OK to use them as fields, as we only access fields directly if we know |
57 // the receiver type. | 59 // the receiver type. |
58 static const _reservedNativeProperties = const <String>[ | 60 static const List<String> _reservedNativeProperties = const <String>[ |
59 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', | 61 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', |
60 // 2-letter: | 62 // 2-letter: |
61 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', | 63 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', |
62 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', | 64 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', |
63 // 3-letter: | 65 // 3-letter: |
64 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', | 66 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', |
65 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', | 67 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', |
66 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', | 68 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', |
67 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', | 69 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', |
68 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', | 70 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', |
69 // 4-letter: | 71 // 4-letter: |
70 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', | 72 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', |
71 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', | 73 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', |
72 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', | 74 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', |
73 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', | 75 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', |
74 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', | 76 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', |
75 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', | 77 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', |
76 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', | 78 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', |
77 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', | 79 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', |
78 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', | 80 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', |
79 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', | 81 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', |
80 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; | 82 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; |
81 | 83 |
82 void reserveBackendNames() { | 84 void reserveBackendNames() { |
83 for (var name in _reservedNativeProperties) { | 85 for (String name in _reservedNativeProperties) { |
84 if (name.length < 2) { | 86 if (name.length < 2) { |
85 instanceNameMap[name] = name; | 87 // Ensure the 1-letter names are disambiguated to the same name. |
88 String disambiguatedName = name; | |
89 reservePublicMemberName(name, disambiguatedName); | |
86 } | 90 } |
87 usedInstanceNames.add(name); | 91 usedInstanceNames.add(name); |
88 // Getter and setter names are autogenerated by prepending 'g' and 's' to | 92 // 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 | 93 // 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 | 94 // use. It is implicit in the next line that the banned prefix is |
91 // only one character. | 95 // only one character. |
92 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); | 96 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); |
93 } | 97 } |
94 | 98 |
95 // These popular names are present in most programs and deserve | 99 // These popular names are present in most programs and deserve |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 Iterable<String> suggestions) { | 163 Iterable<String> suggestions) { |
160 int hash = _calculateHash(proposedName); | 164 int hash = _calculateHash(proposedName); |
161 // Avoid very small hashes that won't try many names. | 165 // Avoid very small hashes that won't try many names. |
162 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime. | 166 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime. |
163 | 167 |
164 // Try other n-character names based on the hash. We try one to three | 168 // 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 | 169 // 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 | 170 // 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 | 171 // to make the renamer stable: small changes in the input should nornally |
168 // result in relatively small changes in the output. | 172 // result in relatively small changes in the output. |
169 for (var n = 1; n <= 3; n++) { | 173 for (int n = 1; n <= 3; n++) { |
170 int h = hash; | 174 int h = hash; |
171 while (h > 10) { | 175 while (h > 10) { |
172 var codes = <int>[_letterNumber(h)]; | 176 List<int> codes = <int>[_letterNumber(h)]; |
173 int h2 = h ~/ ALPHABET_CHARACTERS; | 177 int h2 = h ~/ ALPHABET_CHARACTERS; |
174 for (var i = 1; i < n; i++) { | 178 for (int i = 1; i < n; i++) { |
175 codes.add(_alphaNumericNumber(h2)); | 179 codes.add(_alphaNumericNumber(h2)); |
176 h2 ~/= ALPHANUMERIC_CHARACTERS; | 180 h2 ~/= ALPHANUMERIC_CHARACTERS; |
177 } | 181 } |
178 final candidate = new String.fromCharCodes(codes); | 182 final candidate = new String.fromCharCodes(codes); |
179 if (!usedNames.contains(candidate) && | 183 if (!usedNames.contains(candidate) && |
180 !jsReserved.contains(candidate) && | 184 !jsReserved.contains(candidate) && |
181 !_hasBannedPrefix(candidate) && | 185 !_hasBannedPrefix(candidate) && |
182 (n != 1 || !suggestions.contains(candidate))) { | 186 (n != 1 || !suggestions.contains(candidate))) { |
183 return candidate; | 187 return candidate; |
184 } | 188 } |
(...skipping 21 matching lines...) Expand all Loading... | |
206 h &= 0xffffffff; | 210 h &= 0xffffffff; |
207 h ^= h >> 6; | 211 h ^= h >> 6; |
208 h &= 0xffffffff; | 212 h &= 0xffffffff; |
209 } | 213 } |
210 return h; | 214 return h; |
211 } | 215 } |
212 | 216 |
213 /// If we can't find a hash based name in the three-letter space, then base | 217 /// 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. | 218 /// the name on a letter and a counter. |
215 String _badName(int hash, Set<String> usedNames) { | 219 String _badName(int hash, Set<String> usedNames) { |
216 var startLetter = new String.fromCharCodes([_letterNumber(hash)]); | 220 String startLetter = new String.fromCharCodes([_letterNumber(hash)]); |
217 var name; | 221 String name; |
218 var i = 0; | 222 int i = 0; |
219 do { | 223 do { |
220 name = "$startLetter${i++}"; | 224 name = "$startLetter${i++}"; |
221 } while (usedNames.contains(name)); | 225 } while (usedNames.contains(name)); |
222 // We don't need to check for banned prefix because the name is in the form | 226 // 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 | 227 // xnnn, where nnn is a number. There can be no getter or setter called |
224 // gnnn since that would imply a numeric field name. | 228 // gnnn since that would imply a numeric field name. |
225 return name; | 229 return name; |
226 } | 230 } |
227 | 231 |
228 int _letterNumber(int x) { | 232 int _letterNumber(int x) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 if (count >= nameStore.length) { | 288 if (count >= nameStore.length) { |
285 // The namer usually does not use certain names as they clash with | 289 // The namer usually does not use certain names as they clash with |
286 // existing properties on JS objects (see [_reservedNativeProperties]). | 290 // existing properties on JS objects (see [_reservedNativeProperties]). |
287 // However, some of them are really short and safe to use for fields. | 291 // However, some of them are really short and safe to use for fields. |
288 // Thus, we shortcut the namer to use those first. | 292 // Thus, we shortcut the namer to use those first. |
289 if (count < MinifyNamer._reservedNativeProperties.length && | 293 if (count < MinifyNamer._reservedNativeProperties.length && |
290 MinifyNamer._reservedNativeProperties[count].length <= 2) { | 294 MinifyNamer._reservedNativeProperties[count].length <= 2) { |
291 nameStore.add(MinifyNamer._reservedNativeProperties[count]); | 295 nameStore.add(MinifyNamer._reservedNativeProperties[count]); |
292 } else { | 296 } else { |
293 nameStore.add(namer.getFreshName("field$count", | 297 nameStore.add(namer.getFreshName("field$count", |
294 namer.usedInstanceNames, namer.suggestedInstanceNames, | 298 namer.usedInstanceNames, namer.suggestedInstanceNames)); |
295 ensureSafe: true)); | |
296 } | 299 } |
297 } | 300 } |
298 | 301 |
299 return nameStore[count]; | 302 return nameStore[count]; |
300 } | 303 } |
301 } | 304 } |
302 | 305 |
303 /** | 306 /** |
304 * A [_FieldNamingScope] encodes a node in the inheritance tree of the current | 307 * A [_FieldNamingScope] encodes a node in the inheritance tree of the current |
305 * class hierarchy. The root node typically is the node corresponding to the | 308 * class hierarchy. The root node typically is the node corresponding to the |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
415 Map<Entity, String> get names => registry.globalNames; | 418 Map<Entity, String> get names => registry.globalNames; |
416 | 419 |
417 _MixinFieldNamingScope.mixin(ClassElement cls, _FieldNamingRegistry registry) | 420 _MixinFieldNamingScope.mixin(ClassElement cls, _FieldNamingRegistry registry) |
418 : super.rootScope(cls, registry); | 421 : super.rootScope(cls, registry); |
419 | 422 |
420 _MixinFieldNamingScope.mixedIn(MixinApplicationElement container, | 423 _MixinFieldNamingScope.mixedIn(MixinApplicationElement container, |
421 _FieldNamingScope superScope, _FieldNamingRegistry registry) | 424 _FieldNamingScope superScope, _FieldNamingRegistry registry) |
422 : super.inherit(container, superScope, registry); | 425 : super.inherit(container, superScope, registry); |
423 | 426 |
424 String _nextName() { | 427 String _nextName() { |
425 var proposed = super._nextName(); | 428 String proposed = super._nextName(); |
426 return proposed + r'$'; | 429 return proposed + r'$'; |
427 } | 430 } |
428 } | 431 } |
429 | 432 |
430 /** | 433 /** |
431 * [BoxFieldElement] fields work differently in that they do not belong to an | 434 * [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 | 435 * 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 | 436 * inheritance chain, we do not need to compute fields a priori but can assign |
434 * names on the fly. | 437 * names on the fly. |
435 */ | 438 */ |
436 class _BoxFieldNamingScope extends _FieldNamingScope { | 439 class _BoxFieldNamingScope extends _FieldNamingScope { |
437 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : | 440 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : |
438 super.rootScope(box, registry); | 441 super.rootScope(box, registry); |
439 | 442 |
440 bool containsField(_) => true; | 443 bool containsField(_) => true; |
441 | 444 |
442 String operator[](Element field) { | 445 String operator[](Element field) { |
443 if (!names.containsKey(field)) add(field); | 446 if (!names.containsKey(field)) add(field); |
444 return names[field]; | 447 return names[field]; |
445 } | 448 } |
446 } | 449 } |
OLD | NEW |