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 { |
| 11 MinifyNamer(Compiler compiler) : super(compiler) { | 11 MinifyNamer(Compiler compiler) : super(compiler) { |
| 12 reserveBackendNames(); | 12 reserveBackendNames(); |
| 13 fieldRegistry = new _FieldNamingRegistry(this); | |
| 13 } | 14 } |
| 14 | 15 |
| 15 String get isolateName => 'I'; | 16 String get isolateName => 'I'; |
| 16 String get isolatePropertiesName => 'p'; | 17 String get isolatePropertiesName => 'p'; |
| 17 bool get shouldMinify => true; | 18 bool get shouldMinify => true; |
| 18 | 19 |
| 19 final String getterPrefix = 'g'; | 20 final String getterPrefix = 'g'; |
| 20 final String setterPrefix = 's'; | 21 final String setterPrefix = 's'; |
| 21 final String callPrefix = ''; // this will create function names $<n> | 22 final String callPrefix = ''; // this will create function names $<n> |
| 22 | 23 |
| 23 static const ALPHABET_CHARACTERS = 52; // a-zA-Z. | 24 final ALPHABET_CHARACTERS = 52; // a-zA-Z. |
| 24 static const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. | 25 final ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. |
| 26 | |
| 27 _FieldNamingRegistry fieldRegistry; | |
| 25 | 28 |
| 26 // 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 |
| 27 // 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. |
| 28 String getFreshName(String proposedName, | 31 String getFreshName(String proposedName, |
| 29 Set<String> usedNames, | 32 Set<String> usedNames, |
| 30 Map<String, String> suggestedNames, | 33 Map<String, String> suggestedNames, |
| 31 {bool ensureSafe: true}) { | 34 {bool ensureSafe: true}) { |
| 32 var freshName; | 35 var freshName; |
| 33 var suggestion = suggestedNames[proposedName]; | 36 var suggestion = suggestedNames[proposedName]; |
| 34 if (suggestion != null && !usedNames.contains(suggestion)) { | 37 if (suggestion != null && !usedNames.contains(suggestion)) { |
| 35 freshName = suggestion; | 38 freshName = suggestion; |
| 36 } else { | 39 } else { |
| 37 freshName = _getUnusedName(proposedName, usedNames); | 40 freshName = _getUnusedName(proposedName, usedNames); |
| 38 } | 41 } |
| 39 usedNames.add(freshName); | 42 usedNames.add(freshName); |
| 40 return freshName; | 43 return freshName; |
| 41 } | 44 } |
| 42 | 45 |
| 43 String getClosureVariableName(String name, int id) { | 46 String getClosureVariableName(String name, int id) { |
| 44 if (id < ALPHABET_CHARACTERS) { | 47 if (id < ALPHABET_CHARACTERS) { |
| 45 return new String.fromCharCodes([_letterNumber(id)]); | 48 return new String.fromCharCodes([_letterNumber(id)]); |
| 46 } | 49 } |
| 47 return "${getMappedInstanceName('closure')}_$id"; | 50 return "${getMappedInstanceName('closure')}_$id"; |
| 48 } | 51 } |
| 49 | 52 |
| 53 // From issue 7554. These should not be used on objects (as instance | |
| 54 // variables) because they clash with names from the DOM. However, it is | |
| 55 // OK to use them as fields, as we only access fields directly if we know | |
| 56 // the receiver type. | |
| 57 static const _reservedNativeProperties = const <String>[ | |
| 58 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', | |
| 59 // 2-letter: | |
| 60 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', | |
| 61 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', | |
| 62 // 3-letter: | |
| 63 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', | |
| 64 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', | |
| 65 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', | |
| 66 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', | |
| 67 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', | |
| 68 // 4-letter: | |
| 69 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', | |
| 70 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', | |
| 71 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', | |
| 72 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', | |
| 73 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', | |
| 74 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', | |
| 75 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', | |
| 76 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', | |
| 77 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', | |
| 78 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', | |
| 79 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; | |
| 80 | |
| 50 void reserveBackendNames() { | 81 void reserveBackendNames() { |
| 51 // From issue 7554. These should not be used on objects (as instance | 82 for (var name in _reservedNativeProperties) { |
| 52 // variables) because they clash with names from the DOM. | |
| 53 const reservedNativeProperties = const <String>[ | |
| 54 'Q', 'a', 'b', 'c', 'd', 'e', 'f', 'r', 'x', 'y', 'z', | |
| 55 // 2-letter: | |
| 56 'ch', 'cx', 'cy', 'db', 'dx', 'dy', 'fr', 'fx', 'fy', 'go', 'id', 'k1', | |
| 57 'k2', 'k3', 'k4', 'r1', 'r2', 'rx', 'ry', 'x1', 'x2', 'y1', 'y2', | |
| 58 // 3-letter: | |
| 59 'add', 'all', 'alt', 'arc', 'CCW', 'cmp', 'dir', 'end', 'get', 'in1', | |
| 60 'in2', 'INT', 'key', 'log', 'low', 'm11', 'm12', 'm13', 'm14', 'm21', | |
| 61 'm22', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm41', 'm42', 'm43', | |
| 62 'm44', 'max', 'min', 'now', 'ONE', 'put', 'red', 'rel', 'rev', 'RGB', | |
| 63 'sdp', 'set', 'src', 'tag', 'top', 'uid', 'uri', 'url', 'URL', | |
| 64 // 4-letter: | |
| 65 'abbr', 'atob', 'Attr', 'axes', 'axis', 'back', 'BACK', 'beta', 'bias', | |
| 66 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', | |
| 67 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', | |
| 68 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', | |
| 69 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', | |
| 70 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', | |
| 71 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', | |
| 72 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', | |
| 73 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', | |
| 74 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', | |
| 75 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; | |
| 76 | |
| 77 for (var name in reservedNativeProperties) { | |
| 78 if (name.length < 2) { | 83 if (name.length < 2) { |
| 79 instanceNameMap[name] = name; | 84 instanceNameMap[name] = name; |
| 80 } | 85 } |
| 81 usedInstanceNames.add(name); | 86 usedInstanceNames.add(name); |
| 82 // Getter and setter names are autogenerated by prepending 'g' and 's' to | 87 // Getter and setter names are autogenerated by prepending 'g' and 's' to |
| 83 // field names. Therefore there are some field names we don't want to | 88 // field names. Therefore there are some field names we don't want to |
| 84 // use. It is implicit in the next line that the banned prefix is | 89 // use. It is implicit in the next line that the banned prefix is |
| 85 // only one character. | 90 // only one character. |
| 86 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); | 91 if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1)); |
| 87 } | 92 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 return $A + x - 26; | 220 return $A + x - 26; |
| 216 } | 221 } |
| 217 | 222 |
| 218 int _alphaNumericNumber(int x) { | 223 int _alphaNumericNumber(int x) { |
| 219 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS; | 224 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS; |
| 220 if (x < 26) return $a + x; | 225 if (x < 26) return $a + x; |
| 221 if (x < 52) return $A + x - 26; | 226 if (x < 52) return $A + x - 26; |
| 222 return $0 + x - 52; | 227 return $0 + x - 52; |
| 223 } | 228 } |
| 224 | 229 |
| 225 } | 230 String instanceFieldPropertyName(Element element) { |
| 231 if (element.hasFixedBackendName) { | |
| 232 return element.fixedBackendName; | |
| 233 } | |
| 234 | |
| 235 _FieldNamingScope names; | |
| 236 if (element is BoxFieldElement) { | |
| 237 names = new _FieldNamingScope.forBox(element.box, fieldRegistry); | |
| 238 } else { | |
| 239 ClassElement cls = element is ClosureFieldElement | |
| 240 ? element.closureClass : element.enclosingClass; | |
| 241 names = new _FieldNamingScope.forClass(cls, compiler.world, | |
| 242 fieldRegistry); | |
| 243 } | |
| 244 | |
| 245 // The inheritance scope based naming did not yield a name. For instance, | |
| 246 // this could be because the field belongs to a mixin. | |
| 247 if (!names.containsField(element)) { | |
| 248 return super.instanceFieldPropertyName(element); | |
| 249 } | |
| 250 | |
| 251 return names[element]; | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 /** | |
| 256 * Encapsulates the global state of field naming. | |
| 257 */ | |
| 258 class _FieldNamingRegistry { | |
| 259 final MinifyNamer namer; | |
| 260 | |
| 261 final Map<Entity, _FieldNamingScope> scopes = | |
| 262 new Map<Entity, _FieldNamingScope>(); | |
| 263 | |
| 264 final Map<Entity, String> globalNames = new Map<Entity, String>(); | |
| 265 | |
| 266 int globalCount = 0; | |
| 267 | |
| 268 final List<String> nameStore = new List<String>(); | |
| 269 | |
| 270 _FieldNamingRegistry(this.namer); | |
| 271 | |
| 272 String getName(int count) { | |
| 273 if (count >= nameStore.length) { | |
| 274 // Try reuse short reserved names (which are safe for fields). Otherwise | |
|
floitsch
2014/11/19 13:04:26
Start by saying that the namer doesn't use reserve
herhut
2014/11/20 10:36:37
Done.
| |
| 275 // go through the normal namer. | |
| 276 if (count < MinifyNamer._reservedNativeProperties.length && | |
| 277 MinifyNamer._reservedNativeProperties[count].length <= 2) { | |
| 278 nameStore.add(MinifyNamer._reservedNativeProperties[count]); | |
| 279 } else { | |
| 280 nameStore.add(namer.getFreshName("field$count", | |
| 281 namer.usedInstanceNames, namer.suggestedInstanceNames, | |
| 282 ensureSafe: true)); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 return nameStore[count]; | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 /** | |
| 291 * A [_FieldNamingScope] encodes a node in the inheritance tree of the current | |
| 292 * class hierarchy. The root node typically is the node corresponding to the | |
| 293 * `Object` class. It is used to assign a unique name to each field of a class. | |
| 294 * Unique here means unique wrt. all fields along the path back to the root. | |
| 295 * This is achieved at construction time via the [_count] field that counts the | |
| 296 * number of fields on the path to the root node that have been encountered so | |
| 297 * far. | |
| 298 * | |
| 299 * Obviously, this only works if no fields are added to a parent node after its | |
| 300 * children have added their first field. | |
| 301 */ | |
| 302 class _FieldNamingScope { | |
| 303 final _FieldNamingScope superScope; | |
| 304 final Entity container; | |
| 305 final Map<Element, String> names = new Maplet<Element, String>(); | |
| 306 final _FieldNamingRegistry registry; | |
| 307 int _count; | |
|
floitsch
2014/11/19 13:04:26
Document _count.
Apparently it serves as both maxi
herhut
2014/11/20 10:36:37
Done.
| |
| 308 | |
| 309 // The maxium count along the superclass chain. | |
|
floitsch
2014/11/19 13:04:26
maximum
herhut
2014/11/20 10:36:37
Done.
| |
| 310 int get maxCount => _count; | |
| 311 | |
| 312 // The local count. This is separate to be able to override it in subclasses. | |
|
floitsch
2014/11/19 13:04:26
What's a "local" count?
Why is it the same as the
herhut
2014/11/20 10:36:37
Done.
| |
| 313 int get _localCount => _count; | |
| 314 void set _localCount(int val) { _count = val; } | |
| 315 | |
| 316 factory _FieldNamingScope.forClass(ClassElement cls, ClassWorld world, | |
| 317 _FieldNamingRegistry registry) { | |
| 318 _FieldNamingScope computeFieldNames() { | |
|
floitsch
2014/11/19 13:04:25
new line before nested function.
herhut
2014/11/20 10:36:37
Done.
| |
| 319 _FieldNamingScope result; | |
| 320 // For a minin class, we have to generate globally unique names | |
|
floitsch
2014/11/19 13:04:26
"mixin".
finish with a ".".
Nit:
I'm not sure how
herhut
2014/11/20 10:36:37
Done.
| |
| 321 if (world.isUsedAsMixin(cls)) { | |
| 322 result = new _MixinFieldNamingScope.mixin(cls, registry); | |
| 323 } else { | |
| 324 ClassElement superClass = cls.superclass; | |
|
floitsch
2014/11/19 13:04:26
apparently we generally use "superclass" and not "
herhut
2014/11/20 10:36:37
Done.
| |
| 325 if (superClass != null) { | |
| 326 _FieldNamingScope superScope = new _FieldNamingScope.forClass( | |
| 327 superClass, world, registry); | |
| 328 // Mixin applications use the global scope. | |
|
floitsch
2014/11/19 13:04:26
Same nit for comment: it's not clear how the "new
herhut
2014/11/20 10:36:37
Done.
| |
| 329 if (cls.isMixinApplication) { | |
| 330 result = new _MixinFieldNamingScope.mixedin(cls, superScope, | |
|
floitsch
2014/11/19 13:04:26
mixedIn ?
herhut
2014/11/20 10:36:37
Done.
| |
| 331 registry); | |
| 332 } else { | |
| 333 result = new _FieldNamingScope.inherit(cls, superScope, registry); | |
| 334 } | |
| 335 } else { | |
|
floitsch
2014/11/19 13:04:26
I think it's easier to read if this is the default
herhut
2014/11/20 10:36:37
I have reordered them.
| |
| 336 result = new _FieldNamingScope.rootScope(cls, registry); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 cls.forEachInstanceField((cls, field) => result.add(field)); | |
| 341 return result; | |
| 342 } | |
| 343 | |
| 344 return registry.scopes.putIfAbsent(cls, computeFieldNames); | |
|
floitsch
2014/11/19 13:04:26
I prefer the more common pattern:
_FieldNamingSco
herhut
2014/11/20 10:36:37
Done.
| |
| 345 } | |
| 346 | |
| 347 factory _FieldNamingScope.forBox(Local box, _FieldNamingRegistry registry) { | |
| 348 return registry.scopes.putIfAbsent(box, | |
| 349 () => new _BoxFieldNamingScope(box, registry)); | |
| 350 } | |
| 351 | |
| 352 _FieldNamingScope.rootScope(this.container, this.registry) | |
| 353 : superScope = null, | |
| 354 _count = 0; | |
| 355 | |
| 356 _FieldNamingScope.inherit(this.container, this.superScope, this.registry) { | |
| 357 _count = superScope.maxCount; | |
| 358 } | |
| 359 | |
| 360 _isValueUnused(String name) { | |
|
floitsch
2014/11/19 13:04:26
comment what values we are talking about.
I would
herhut
2014/11/20 10:36:38
Done.
| |
| 361 return !names.values.contains(name) && | |
| 362 ((superScope == null) || superScope._isValueUnused(name)); | |
| 363 } | |
| 364 | |
| 365 String _nextName() => registry.getName(_localCount++); | |
| 366 | |
| 367 String operator[](Element field) { | |
| 368 String name = names[field]; | |
| 369 if (name == null && superScope != null) return superScope[field]; | |
| 370 return name; | |
| 371 } | |
| 372 | |
| 373 void add(Element field) { | |
| 374 if (names.containsKey(field)) return; | |
| 375 | |
| 376 String value = _nextName(); | |
| 377 assert(invariant(field, _isValueUnused(value))); | |
| 378 names[field] = value; | |
| 379 } | |
| 380 | |
| 381 bool containsField(Element field) => names.containsKey(field); | |
| 382 } | |
| 383 | |
| 384 /** | |
| 385 * Field names for mixins have two constraints: They need to be unique in the | |
| 386 * hierarchy of each application of a mixin and they need to be the same for | |
| 387 * all applications of a mixin. To achieve this, we use global naming for | |
| 388 * mixins and add a `$` at the end to ensure they are unqiue across all field | |
|
floitsch
2014/11/19 13:04:26
how does the "$" guarantee that it's unique across
herhut
2014/11/20 10:36:37
I have expanded the comment to explain this.
| |
| 389 * names. | |
| 390 */ | |
| 391 class _MixinFieldNamingScope extends _FieldNamingScope { | |
| 392 int get _localCount => registry.globalCount; | |
| 393 void set _localCount(int val) { registry.globalCount = val; } | |
| 394 | |
| 395 Map<Element, String> get names => registry.globalNames; | |
| 396 | |
| 397 _MixinFieldNamingScope.mixin(ClassElement cls, _FieldNamingRegistry registry) | |
| 398 : super.rootScope(cls, registry); | |
| 399 | |
| 400 _MixinFieldNamingScope.mixedin(MixinApplicationElement container, | |
| 401 _FieldNamingScope superScope, _FieldNamingRegistry registry) | |
| 402 : super.inherit(container, superScope, registry); | |
| 403 | |
| 404 String _nextName() { | |
| 405 var proposed = super._nextName(); | |
| 406 return proposed + r'$'; | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 /** | |
| 411 * [BoxFieldElement] fields work differently in that they do not belong to an | |
| 412 * actual class but an anonymous box associated to a [Local]. As there is no | |
| 413 * inheritance chain, we do not need to compute fields a priori but can assign | |
| 414 * names on the fly. | |
| 415 */ | |
| 416 class _BoxFieldNamingScope extends _FieldNamingScope { | |
| 417 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) : | |
| 418 super.rootScope(box, registry); | |
| 419 | |
| 420 bool containsField(_) => true; | |
| 421 | |
| 422 String operator[](Element field) { | |
| 423 if (!names.containsKey(field)) add(field); | |
| 424 return names[field]; | |
| 425 } | |
| 426 } | |
| OLD | NEW |