| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.namer; | 5 part of js_backend.namer; |
| 6 | 6 |
| 7 abstract class _MinifiedFieldNamer implements Namer { | 7 abstract class _MinifiedFieldNamer implements Namer { |
| 8 _FieldNamingRegistry get fieldRegistry; | 8 _FieldNamingRegistry get fieldRegistry; |
| 9 | 9 |
| 10 // Returns a minimal name for the field that is globally unique along | 10 // Returns a minimal name for the field that is globally unique along |
| 11 // the given element's class inheritance chain. | 11 // the given element's class inheritance chain. |
| 12 // | 12 // |
| 13 // The inheritance scope based naming might not yield a name. For instance, | 13 // The inheritance scope based naming might not yield a name. For instance, |
| 14 // this could be because the field belongs to a mixin. In such a case this | 14 // this could be because the field belongs to a mixin. In such a case this |
| 15 // will return `null` and a normal field name has to be used. | 15 // will return `null` and a normal field name has to be used. |
| 16 jsAst.Name _minifiedInstanceFieldPropertyName(FieldElement element) { | 16 jsAst.Name _minifiedInstanceFieldPropertyName(FieldEntity element) { |
| 17 if (_nativeData.hasFixedBackendName(element)) { | 17 if (_nativeData.hasFixedBackendName(element)) { |
| 18 return new StringBackedName(_nativeData.getFixedBackendName(element)); | 18 return new StringBackedName(_nativeData.getFixedBackendName(element)); |
| 19 } | 19 } |
| 20 | 20 |
| 21 _FieldNamingScope names; | 21 _FieldNamingScope names; |
| 22 if (element is BoxFieldElement) { | 22 if (element is BoxFieldElement) { |
| 23 names = new _FieldNamingScope.forBox(element.box, fieldRegistry); | 23 names = new _FieldNamingScope.forBox(element.box, fieldRegistry); |
| 24 } else if (element is JBoxedField) { |
| 25 names = new _FieldNamingScope.forBox(element.box, fieldRegistry); |
| 24 } else { | 26 } else { |
| 25 ClassElement cls = element.enclosingClass; | 27 ClassEntity cls = element.enclosingClass; |
| 26 names = new _FieldNamingScope.forClass(cls, _closedWorld, fieldRegistry); | 28 names = new _FieldNamingScope.forClass(cls, _closedWorld, fieldRegistry); |
| 27 } | 29 } |
| 28 | 30 |
| 29 if (names.containsField(element)) { | 31 if (names.containsField(element)) { |
| 30 return names[element]; | 32 return names[element]; |
| 31 } | 33 } |
| 32 return null; | 34 return null; |
| 33 } | 35 } |
| 34 } | 36 } |
| 35 | 37 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 111 |
| 110 /// The number of locally used fields. Depending on the naming source | 112 /// The number of locally used fields. Depending on the naming source |
| 111 /// (e.g. inheritance based or globally unique for mixixns) this | 113 /// (e.g. inheritance based or globally unique for mixixns) this |
| 112 /// might be different from [inheritanceBasedFieldNameCounter]. | 114 /// might be different from [inheritanceBasedFieldNameCounter]. |
| 113 int get _localFieldNameCounter => _fieldNameCounter; | 115 int get _localFieldNameCounter => _fieldNameCounter; |
| 114 void set _localFieldNameCounter(int val) { | 116 void set _localFieldNameCounter(int val) { |
| 115 _fieldNameCounter = val; | 117 _fieldNameCounter = val; |
| 116 } | 118 } |
| 117 | 119 |
| 118 factory _FieldNamingScope.forClass( | 120 factory _FieldNamingScope.forClass( |
| 119 ClassElement cls, ClosedWorld world, _FieldNamingRegistry registry) { | 121 ClassEntity cls, ClosedWorld world, _FieldNamingRegistry registry) { |
| 120 _FieldNamingScope result = registry.scopes[cls]; | 122 _FieldNamingScope result = registry.scopes[cls]; |
| 121 if (result != null) return result; | 123 if (result != null) return result; |
| 122 | 124 |
| 123 if (world.isUsedAsMixin(cls)) { | 125 if (world.isUsedAsMixin(cls)) { |
| 124 result = new _MixinFieldNamingScope.mixin(cls, registry); | 126 result = new _MixinFieldNamingScope.mixin(cls, registry); |
| 125 } else { | 127 } else { |
| 126 if (cls.superclass == null) { | 128 var superclass = world.elementEnvironment.getSuperClass(cls); |
| 129 if (superclass == null) { |
| 127 result = new _FieldNamingScope.rootScope(cls, registry); | 130 result = new _FieldNamingScope.rootScope(cls, registry); |
| 128 } else { | 131 } else { |
| 129 _FieldNamingScope superScope = | 132 _FieldNamingScope superScope = |
| 130 new _FieldNamingScope.forClass(cls.superclass, world, registry); | 133 new _FieldNamingScope.forClass(superclass, world, registry); |
| 131 if (cls.isMixinApplication) { | 134 if (world.elementEnvironment.isMixinApplication(cls)) { |
| 132 result = | 135 result = |
| 133 new _MixinFieldNamingScope.mixedIn(cls, superScope, registry); | 136 new _MixinFieldNamingScope.mixedIn(cls, superScope, registry); |
| 134 } else { | 137 } else { |
| 135 result = new _FieldNamingScope.inherit(cls, superScope, registry); | 138 result = new _FieldNamingScope.inherit(cls, superScope, registry); |
| 136 } | 139 } |
| 137 } | 140 } |
| 138 } | 141 } |
| 139 | 142 |
| 140 cls.forEachInstanceField((cls, field) => result.add(field)); | 143 world.elementEnvironment.forEachClassMember(cls, |
| 144 (ClassEntity declarer, MemberEntity member) { |
| 145 if (member.isField && member.isInstanceMember) result.add(member); |
| 146 }); |
| 141 | 147 |
| 142 registry.scopes[cls] = result; | 148 registry.scopes[cls] = result; |
| 143 return result; | 149 return result; |
| 144 } | 150 } |
| 145 | 151 |
| 146 factory _FieldNamingScope.forBox(Local box, _FieldNamingRegistry registry) { | 152 factory _FieldNamingScope.forBox(Local box, _FieldNamingRegistry registry) { |
| 147 return registry.scopes | 153 return registry.scopes |
| 148 .putIfAbsent(box, () => new _BoxFieldNamingScope(box, registry)); | 154 .putIfAbsent(box, () => new _BoxFieldNamingScope(box, registry)); |
| 149 } | 155 } |
| 150 | 156 |
| 151 _FieldNamingScope.rootScope(this.container, this.registry) | 157 _FieldNamingScope.rootScope(this.container, this.registry) |
| 152 : superScope = null, | 158 : superScope = null, |
| 153 _fieldNameCounter = 0; | 159 _fieldNameCounter = 0; |
| 154 | 160 |
| 155 _FieldNamingScope.inherit(this.container, this.superScope, this.registry) { | 161 _FieldNamingScope.inherit(this.container, this.superScope, this.registry) { |
| 156 _fieldNameCounter = superScope.inheritanceBasedFieldNameCounter; | 162 _fieldNameCounter = superScope.inheritanceBasedFieldNameCounter; |
| 157 } | 163 } |
| 158 | 164 |
| 159 /** | 165 /** |
| 160 * Checks whether [name] is already used in the current scope chain. | 166 * Checks whether [name] is already used in the current scope chain. |
| 161 */ | 167 */ |
| 162 _isNameUnused(jsAst.Name name) { | 168 _isNameUnused(jsAst.Name name) { |
| 163 return !names.values.contains(name) && | 169 return !names.values.contains(name) && |
| 164 ((superScope == null) || superScope._isNameUnused(name)); | 170 ((superScope == null) || superScope._isNameUnused(name)); |
| 165 } | 171 } |
| 166 | 172 |
| 167 jsAst.Name _nextName() => registry.getName(_localFieldNameCounter++); | 173 jsAst.Name _nextName() => registry.getName(_localFieldNameCounter++); |
| 168 | 174 |
| 169 jsAst.Name operator [](Element field) { | 175 jsAst.Name operator [](Entity field) { |
| 170 jsAst.Name name = names[field]; | 176 jsAst.Name name = names[field]; |
| 171 if (name == null && superScope != null) return superScope[field]; | 177 if (name == null && superScope != null) return superScope[field]; |
| 172 return name; | 178 return name; |
| 173 } | 179 } |
| 174 | 180 |
| 175 void add(Element field) { | 181 void add(Entity field) { |
| 176 if (names.containsKey(field)) return; | 182 if (names.containsKey(field)) return; |
| 177 | 183 |
| 178 jsAst.Name value = _nextName(); | 184 jsAst.Name value = _nextName(); |
| 179 assert(_isNameUnused(value), failedAt(field)); | 185 assert(_isNameUnused(value), failedAt(field)); |
| 180 names[field] = value; | 186 names[field] = value; |
| 181 } | 187 } |
| 182 | 188 |
| 183 bool containsField(Element field) => names.containsKey(field); | 189 bool containsField(Entity field) => names.containsKey(field); |
| 184 } | 190 } |
| 185 | 191 |
| 186 /** | 192 /** |
| 187 * Field names for mixins have two constraints: They need to be unique in the | 193 * Field names for mixins have two constraints: They need to be unique in the |
| 188 * hierarchy of each application of a mixin and they need to be the same for | 194 * hierarchy of each application of a mixin and they need to be the same for |
| 189 * all applications of a mixin. To achieve this, we use global naming for | 195 * all applications of a mixin. To achieve this, we use global naming for |
| 190 * mixins from the same name pool as fields and add a `$` at the end to ensure | 196 * mixins from the same name pool as fields and add a `$` at the end to ensure |
| 191 * they do not collide with normal field names. The `$` sign is typically used | 197 * they do not collide with normal field names. The `$` sign is typically used |
| 192 * as a separator between method names and argument counts and does not appear | 198 * as a separator between method names and argument counts and does not appear |
| 193 * in generated names themselves. | 199 * in generated names themselves. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 220 * inheritance chain, we do not need to compute fields a priori but can assign | 226 * inheritance chain, we do not need to compute fields a priori but can assign |
| 221 * names on the fly. | 227 * names on the fly. |
| 222 */ | 228 */ |
| 223 class _BoxFieldNamingScope extends _FieldNamingScope { | 229 class _BoxFieldNamingScope extends _FieldNamingScope { |
| 224 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) | 230 _BoxFieldNamingScope(Local box, _FieldNamingRegistry registry) |
| 225 : super.rootScope(box, registry); | 231 : super.rootScope(box, registry); |
| 226 | 232 |
| 227 @override | 233 @override |
| 228 bool containsField(_) => true; | 234 bool containsField(_) => true; |
| 229 | 235 |
| 230 jsAst.Name operator [](Element field) { | 236 jsAst.Name operator [](Entity field) { |
| 231 if (!names.containsKey(field)) add(field); | 237 if (!names.containsKey(field)) add(field); |
| 232 return names[field]; | 238 return names[field]; |
| 233 } | 239 } |
| 234 } | 240 } |
| OLD | NEW |