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

Side by Side Diff: pkg/compiler/lib/src/universe/class_set.dart

Issue 1413613010: Normalize type masks to use the least upper instantiated subclass/type. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fix try/poi Created 5 years, 1 month 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
OLDNEW
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 library dart2js.world.class_set; 5 library dart2js.world.class_set;
6 6
7 import 'dart:collection' show IterableBase; 7 import 'dart:collection' show IterableBase;
8 import '../elements/elements.dart' show ClassElement; 8 import '../elements/elements.dart' show ClassElement;
9 import '../util/util.dart' show Link; 9 import '../util/util.dart' show Link;
10 10
(...skipping 17 matching lines...) Expand all
28 /// A 28 /// A
29 /// / \ 29 /// / \
30 /// B C 30 /// B C
31 /// | 31 /// |
32 /// D 32 /// D
33 /// | 33 /// |
34 /// E 34 /// E
35 /// 35 ///
36 class ClassHierarchyNode { 36 class ClassHierarchyNode {
37 final ClassElement cls; 37 final ClassElement cls;
38 ClassElement _leastUpperInstantiatedSubclass;
38 39
39 /// `true` if [cls] has been directly instantiated. 40 /// `true` if [cls] has been directly instantiated.
40 /// 41 ///
41 /// For instance `C` but _not_ `B` in: 42 /// For instance `C` but _not_ `B` in:
42 /// class B {} 43 /// class B {}
43 /// class C extends B {} 44 /// class C extends B {}
44 /// main() => new C(); 45 /// main() => new C();
45 /// 46 ///
46 bool isDirectlyInstantiated = false; 47 bool isDirectlyInstantiated = false;
47 48
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 bool includeUninstantiated: true, 95 bool includeUninstantiated: true,
95 bool strict: false}) { 96 bool strict: false}) {
96 return new ClassHierarchyNodeIterable( 97 return new ClassHierarchyNodeIterable(
97 this, 98 this,
98 includeRoot: !strict, 99 includeRoot: !strict,
99 includeDirectlyInstantiated: includeDirectlyInstantiated, 100 includeDirectlyInstantiated: includeDirectlyInstantiated,
100 includeIndirectlyInstantiated: includeIndirectlyInstantiated, 101 includeIndirectlyInstantiated: includeIndirectlyInstantiated,
101 includeUninstantiated: includeUninstantiated); 102 includeUninstantiated: includeUninstantiated);
102 } 103 }
103 104
105 /// Returns the most specific subclass of [cls] (including [cls]) that is
106 /// directly instantiated or a superclass of all directly instantiated
107 /// subclasses. If [cls] is not instantiated, `null` is returned.
108 ClassElement getLubOfInstantiatedSubclasses() {
109 if (!isInstantiated) return null;
110 if (_leastUpperInstantiatedSubclass == null) {
111 _leastUpperInstantiatedSubclass =
112 _computeLeastUpperInstantiatedSubclass();
113 }
114 return _leastUpperInstantiatedSubclass;
115 }
116
117 ClassElement _computeLeastUpperInstantiatedSubclass() {
118 if (isDirectlyInstantiated) {
119 return cls;
120 }
121 ClassHierarchyNode subclass;
122 for (Link<ClassHierarchyNode> link = _directSubclasses;
123 !link.isEmpty;
124 link = link.tail) {
125 if (link.head.isInstantiated) {
126 if (subclass == null) {
127 subclass = link.head;
128 } else {
129 return cls;
130 }
131 }
132 }
133 if (subclass != null) {
134 return subclass.getLubOfInstantiatedSubclasses();
135 }
136 return cls;
137 }
138
104 void printOn(StringBuffer sb, String indentation, 139 void printOn(StringBuffer sb, String indentation,
105 {bool instantiatedOnly: false, 140 {bool instantiatedOnly: false,
141 bool sorted: true,
106 ClassElement withRespectTo}) { 142 ClassElement withRespectTo}) {
107 143
108 bool isRelatedTo(ClassElement subclass) { 144 bool isRelatedTo(ClassElement subclass) {
109 return subclass.implementsInterface(withRespectTo); 145 return subclass == withRespectTo ||
146 subclass.implementsInterface(withRespectTo);
110 } 147 }
111 148
112 sb.write('$indentation$cls'); 149 sb.write(indentation);
150 if (cls.isAbstract) {
151 sb.write('abstract ');
152 }
153 sb.write('class ${cls.name}:');
113 if (isDirectlyInstantiated) { 154 if (isDirectlyInstantiated) {
114 sb.write(' directly'); 155 sb.write(' directly');
115 } 156 }
116 if (isIndirectlyInstantiated) { 157 if (isIndirectlyInstantiated) {
117 sb.write(' indirectly'); 158 sb.write(' indirectly');
118 } 159 }
119 sb.write(' ['); 160 sb.write(' [');
120 if (_directSubclasses.isEmpty) { 161 if (_directSubclasses.isEmpty) {
121 sb.write(']'); 162 sb.write(']');
122 } else { 163 } else {
164 var subclasses = _directSubclasses;
165 if (sorted) {
166 subclasses = _directSubclasses.toList()..sort((a, b) {
167 return a.cls.name.compareTo(b.cls.name);
168 });
169 }
123 bool needsComma = false; 170 bool needsComma = false;
124 for (Link<ClassHierarchyNode> link = _directSubclasses; 171 for (ClassHierarchyNode child in subclasses) {
125 !link.isEmpty;
126 link = link.tail) {
127 ClassHierarchyNode child = link.head;
128 if (instantiatedOnly && !child.isInstantiated) { 172 if (instantiatedOnly && !child.isInstantiated) {
129 continue; 173 continue;
130 } 174 }
131 if (withRespectTo != null && !child.subclasses().any(isRelatedTo)) { 175 if (withRespectTo != null && !child.subclasses().any(isRelatedTo)) {
132 continue; 176 continue;
133 } 177 }
134 if (needsComma) { 178 if (needsComma) {
135 sb.write(',\n'); 179 sb.write(',\n');
136 } else { 180 } else {
137 sb.write('\n'); 181 sb.write('\n');
138 } 182 }
139 child.printOn( 183 child.printOn(
140 sb, 184 sb,
141 '$indentation ', 185 '$indentation ',
142 instantiatedOnly: instantiatedOnly, 186 instantiatedOnly: instantiatedOnly,
187 sorted: sorted,
143 withRespectTo: withRespectTo); 188 withRespectTo: withRespectTo);
144 needsComma = true; 189 needsComma = true;
145 } 190 }
146 if (needsComma) { 191 if (needsComma) {
147 sb.write('\n'); 192 sb.write('\n');
148 sb.write('$indentation]'); 193 sb.write('$indentation]');
149 } else { 194 } else {
150 sb.write(']'); 195 sb.write(']');
151 } 196 }
152 } 197 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 /// The subtypes `B` and `E` are not directly modeled because they are implied 249 /// The subtypes `B` and `E` are not directly modeled because they are implied
205 /// by their subclass relation to `A` and `D`, repectively. This can be seen 250 /// by their subclass relation to `A` and `D`, repectively. This can be seen
206 /// if we expand the subclass subtrees: 251 /// if we expand the subclass subtrees:
207 /// 252 ///
208 /// A -> [C, D, F] 253 /// A -> [C, D, F]
209 /// | | 254 /// | |
210 /// B E 255 /// B E
211 /// 256 ///
212 class ClassSet { 257 class ClassSet {
213 final ClassHierarchyNode node; 258 final ClassHierarchyNode node;
259 ClassElement _leastUpperInstantiatedSubtype;
214 260
215 List<ClassHierarchyNode> _directSubtypes; 261 List<ClassHierarchyNode> _directSubtypes;
216 262
217 ClassSet(this.node); 263 ClassSet(this.node);
218 264
219 ClassElement get cls => node.cls; 265 ClassElement get cls => node.cls;
220 266
221 /// Returns an [Iterable] of the subclasses of [cls] possibly including [cls]. 267 /// Returns an [Iterable] of the subclasses of [cls] possibly including [cls].
222 /// 268 ///
223 /// The directly instantiated, indirectly instantiated and uninstantiated 269 /// The directly instantiated, indirectly instantiated and uninstantiated
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 } 350 }
305 } 351 }
306 } 352 }
307 if (!added) { 353 if (!added) {
308 newSubtypes.add(subtype); 354 newSubtypes.add(subtype);
309 } 355 }
310 _directSubtypes = newSubtypes; 356 _directSubtypes = newSubtypes;
311 } 357 }
312 } 358 }
313 359
360 /// Returns the most specific subtype of [cls] (including [cls]) that is
361 /// directly instantiated or a superclass of all directly instantiated
362 /// subtypes. If no subtypes of [cls] are instantiated, `null` is returned.
363 ClassElement getLubOfInstantiatedSubtypes() {
364 if (_leastUpperInstantiatedSubtype == null) {
365 _leastUpperInstantiatedSubtype = _computeLeastUpperInstantiatedSubtype();
366 }
367 return _leastUpperInstantiatedSubtype;
368 }
369
370 ClassElement _computeLeastUpperInstantiatedSubtype() {
371 if (node.isDirectlyInstantiated) {
372 return cls;
373 }
374 if (_directSubtypes == null) {
375 return node.getLubOfInstantiatedSubclasses();
376 }
377 ClassHierarchyNode subtype;
378 if (node.isInstantiated) {
379 subtype = node;
380 }
381 for (ClassHierarchyNode subnode in _directSubtypes) {
382 if (subnode.isInstantiated) {
383 if (subtype == null) {
384 subtype = subnode;
385 } else {
386 return cls;
387 }
388 }
389 }
390 if (subtype != null) {
391 return subtype.getLubOfInstantiatedSubclasses();
392 }
393 return null;
394 }
395
314 String toString() { 396 String toString() {
315 StringBuffer sb = new StringBuffer(); 397 StringBuffer sb = new StringBuffer();
316 sb.write('[\n'); 398 sb.write('[\n');
317 node.printOn(sb, ' '); 399 node.printOn(sb, ' ');
318 sb.write('\n'); 400 sb.write('\n');
319 if (_directSubtypes != null) { 401 if (_directSubtypes != null) {
320 for (ClassHierarchyNode node in _directSubtypes) { 402 for (ClassHierarchyNode node in _directSubtypes) {
321 node.printOn(sb, ' '); 403 node.printOn(sb, ' ');
322 sb.write('\n'); 404 sb.write('\n');
323 } 405 }
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 includeDirectlyInstantiated: includeDirectlyInstantiated, 591 includeDirectlyInstantiated: includeDirectlyInstantiated,
510 includeIndirectlyInstantiated: includeIndirectlyInstantiated, 592 includeIndirectlyInstantiated: includeIndirectlyInstantiated,
511 includeUninstantiated: includeUninstantiated).iterator; 593 includeUninstantiated: includeUninstantiated).iterator;
512 if (elements.moveNext()) { 594 if (elements.moveNext()) {
513 return true; 595 return true;
514 } 596 }
515 } 597 }
516 return false; 598 return false;
517 } 599 }
518 } 600 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/types/type_mask.dart ('k') | pkg/compiler/lib/src/universe/function_set.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698