Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1253)

Side by Side Diff: pkg/compiler/lib/src/js_backend/minify_namer.dart

Issue 891673003: dart2js: Refactoring, documentation, and a few bugfixes in Namer class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
11 MinifyNamer(Compiler compiler) : super(compiler) { 11 MinifyNamer(Compiler compiler) : super(compiler) {
12 reserveBackendNames(); 12 reserveBackendNames();
13 fieldRegistry = new _FieldNamingRegistry(this); 13 fieldRegistry = new _FieldNamingRegistry(this);
14 } 14 }
15 15
16 String get isolateName => 'I'; 16 String get isolateName => 'I';
17 String get isolatePropertiesName => 'p'; 17 String get isolatePropertiesName => 'p';
18 bool get shouldMinify => true; 18 bool get shouldMinify => true;
19 19
20 final String getterPrefix = 'g'; 20 final String getterPrefix = 'g';
21 final String setterPrefix = 's'; 21 final String setterPrefix = 's';
22 final String callPrefix = ''; // this will create function names $<n> 22 final String callPrefix = ''; // this will create function names $<n>
23 23
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 ///
32 /// [sanitizeForNatives] and [sanitizeForAnnotations] are ignored because the
33 /// minified names will always avoid clashing with annotated names or natives.
31 String getFreshName(String proposedName, 34 String getFreshName(String proposedName,
32 Set<String> usedNames, 35 Set<String> usedNames,
33 Map<String, String> suggestedNames, 36 Map<String, String> suggestedNames,
34 {bool ensureSafe: true}) { 37 {bool sanitizeForNatives: false,
35 var freshName; 38 bool sanitizeForAnnotations: false}) {
36 var suggestion = suggestedNames[proposedName]; 39 String freshName;
40 String suggestion = suggestedNames[proposedName];
37 if (suggestion != null && !usedNames.contains(suggestion)) { 41 if (suggestion != null && !usedNames.contains(suggestion)) {
38 freshName = suggestion; 42 freshName = suggestion;
39 } else { 43 } else {
40 freshName = _getUnusedName(proposedName, usedNames, 44 freshName = _getUnusedName(proposedName, usedNames,
41 suggestedNames.values); 45 suggestedNames.values);
42 } 46 }
43 usedNames.add(freshName); 47 usedNames.add(freshName);
44 return freshName; 48 return freshName;
45 } 49 }
46 50
47 String getClosureVariableName(String name, int id) { 51 String getClosureVariableName(String _, int id) {
48 if (id < ALPHABET_CHARACTERS) { 52 if (id < ALPHABET_CHARACTERS) {
49 return new String.fromCharCodes([_letterNumber(id)]); 53 return new String.fromCharCodes([_letterNumber(id)]);
50 } 54 }
51 return "${getMappedInstanceName('closure')}_$id"; 55 // Fall back to a slightly longer name.
56 String basename = _disambiguateMember(null, 'closure');
57 return '${basename}_$id';
52 } 58 }
53 59
54 // From issue 7554. These should not be used on objects (as instance 60 // 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 61 // 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 62 // OK to use them as fields, as we only access fields directly if we know
57 // the receiver type. 63 // the receiver type.
58 static const _reservedNativeProperties = const <String>[ 64 static const List<String> _reservedNativeProperties = const <String>[
59 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', 65 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z',
60 // 2-letter: 66 // 2-letter:
61 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', 67 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1',
62 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', 68 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2',
63 // 3-letter: 69 // 3-letter:
64 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', 70 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1',
65 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', 71 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21',
66 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', 72 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43',
67 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', 73 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB',
68 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', 74 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL',
69 // 4-letter: 75 // 4-letter:
70 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', 76 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias',
71 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', 77 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE',
72 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', 78 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face',
73 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', 79 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head',
74 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', 80 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT',
75 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', 81 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line',
76 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', 82 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE',
77 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', 83 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX',
78 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', 84 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send',
79 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', 85 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text',
80 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; 86 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO'];
81 87
82 void reserveBackendNames() { 88 void reserveBackendNames() {
83 for (var name in _reservedNativeProperties) { 89 for (String name in _reservedNativeProperties) {
84 if (name.length < 2) { 90 if (name.length < 2) {
85 instanceNameMap[name] = name; 91 // Ensure the 1-letter names are disambiguated to the same name.
92 String disambiguatedName = name;
93 reservePublicMemberName(name, disambiguatedName);
86 } 94 }
87 usedInstanceNames.add(name); 95 usedInstanceNames.add(name);
88 // Getter and setter names are autogenerated by prepending 'g' and 's' to 96 // 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 97 // 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 98 // use. It is implicit in the next line that the banned prefix is
91 // only one character. 99 // only one character.
92 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); 100 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1));
93 } 101 }
94 102
95 // These popular names are present in most programs and deserve 103 // These popular names are present in most programs and deserve
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 Iterable<String> suggestions) { 167 Iterable<String> suggestions) {
160 int hash = _calculateHash(proposedName); 168 int hash = _calculateHash(proposedName);
161 // Avoid very small hashes that won't try many names. 169 // Avoid very small hashes that won't try many names.
162 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime. 170 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime.
163 171
164 // Try other n-character names based on the hash. We try one to three 172 // 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 173 // 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 174 // 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 175 // to make the renamer stable: small changes in the input should nornally
168 // result in relatively small changes in the output. 176 // result in relatively small changes in the output.
169 for (var n = 1; n <= 3; n++) { 177 for (int n = 1; n <= 3; n++) {
170 int h = hash; 178 int h = hash;
171 while (h > 10) { 179 while (h > 10) {
172 var codes = <int>[_letterNumber(h)]; 180 List<int> codes = <int>[_letterNumber(h)];
173 int h2 = h ~/ ALPHABET_CHARACTERS; 181 int h2 = h ~/ ALPHABET_CHARACTERS;
174 for (var i = 1; i < n; i++) { 182 for (int i = 1; i < n; i++) {
175 codes.add(_alphaNumericNumber(h2)); 183 codes.add(_alphaNumericNumber(h2));
176 h2 ~/= ALPHANUMERIC_CHARACTERS; 184 h2 ~/= ALPHANUMERIC_CHARACTERS;
177 } 185 }
178 final candidate = new String.fromCharCodes(codes); 186 final candidate = new String.fromCharCodes(codes);
179 if (!usedNames.contains(candidate) && 187 if (!usedNames.contains(candidate) &&
180 !jsReserved.contains(candidate) && 188 !jsReserved.contains(candidate) &&
181 !_hasBannedPrefix(candidate) && 189 !_hasBannedPrefix(candidate) &&
182 (n != 1 || !suggestions.contains(candidate))) { 190 (n != 1 || !suggestions.contains(candidate))) {
183 return candidate; 191 return candidate;
184 } 192 }
(...skipping 21 matching lines...) Expand all
206 h &= 0xffffffff; 214 h &= 0xffffffff;
207 h ^= h >> 6; 215 h ^= h >> 6;
208 h &= 0xffffffff; 216 h &= 0xffffffff;
209 } 217 }
210 return h; 218 return h;
211 } 219 }
212 220
213 /// If we can't find a hash based name in the three-letter space, then base 221 /// 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. 222 /// the name on a letter and a counter.
215 String _badName(int hash, Set<String> usedNames) { 223 String _badName(int hash, Set<String> usedNames) {
216 var startLetter = new String.fromCharCodes([_letterNumber(hash)]); 224 String startLetter = new String.fromCharCodes([_letterNumber(hash)]);
217 var name; 225 String name;
218 var i = 0; 226 int i = 0;
219 do { 227 do {
220 name = "$startLetter${i++}"; 228 name = "$startLetter${i++}";
221 } while (usedNames.contains(name)); 229 } while (usedNames.contains(name));
222 // We don't need to check for banned prefix because the name is in the form 230 // 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 231 // xnnn, where nnn is a number. There can be no getter or setter called
224 // gnnn since that would imply a numeric field name. 232 // gnnn since that would imply a numeric field name.
225 return name; 233 return name;
226 } 234 }
227 235
228 int _letterNumber(int x) { 236 int _letterNumber(int x) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 if (count >= nameStore.length) { 292 if (count >= nameStore.length) {
285 // The namer usually does not use certain names as they clash with 293 // The namer usually does not use certain names as they clash with
286 // existing properties on JS objects (see [_reservedNativeProperties]). 294 // existing properties on JS objects (see [_reservedNativeProperties]).
287 // However, some of them are really short and safe to use for fields. 295 // However, some of them are really short and safe to use for fields.
288 // Thus, we shortcut the namer to use those first. 296 // Thus, we shortcut the namer to use those first.
289 if (count < MinifyNamer._reservedNativeProperties.length && 297 if (count < MinifyNamer._reservedNativeProperties.length &&
290 MinifyNamer._reservedNativeProperties[count].length <= 2) { 298 MinifyNamer._reservedNativeProperties[count].length <= 2) {
291 nameStore.add(MinifyNamer._reservedNativeProperties[count]); 299 nameStore.add(MinifyNamer._reservedNativeProperties[count]);
292 } else { 300 } else {
293 nameStore.add(namer.getFreshName("field$count", 301 nameStore.add(namer.getFreshName("field$count",
294 namer.usedInstanceNames, namer.suggestedInstanceNames, 302 namer.usedInstanceNames, namer.suggestedInstanceNames));
295 ensureSafe: true));
296 } 303 }
297 } 304 }
298 305
299 return nameStore[count]; 306 return nameStore[count];
300 } 307 }
301 } 308 }
302 309
303 /** 310 /**
304 * A [_FieldNamingScope] encodes a node in the inheritance tree of the current 311 * 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 312 * class hierarchy. The root node typically is the node corresponding to the
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_backend/constant_emitter.dart ('k') | pkg/compiler/lib/src/js_backend/namer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698