OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.class_hierarchy; | 4 library kernel.class_hierarchy; |
5 | 5 |
6 import 'ast.dart'; | 6 import 'ast.dart'; |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 import 'src/heap.dart'; | 9 import 'src/heap.dart'; |
10 import 'type_algebra.dart'; | 10 import 'type_algebra.dart'; |
11 | 11 |
12 /// Interface for answering various subclassing queries. | 12 /// Interface for answering various subclassing queries. |
13 /// TODO(scheglov) Several methods are not used, or used only in tests. | 13 /// TODO(scheglov) Several methods are not used, or used only in tests. |
14 /// Check if these methods are not useful and should be removed . | 14 /// Check if these methods are not useful and should be removed . |
15 abstract class ClassHierarchy { | 15 abstract class ClassHierarchy { |
16 factory ClassHierarchy(Program program) => | 16 factory ClassHierarchy(Program program) => |
17 new ClosedWorldClassHierarchy(program); | 17 new ClosedWorldClassHierarchy(program); |
18 | 18 |
19 /// All classes in the program. | 19 /// Given the [unordered] classes, return them in such order that classes |
20 /// | 20 /// occur after their superclasses. If some superclasses are not in |
21 /// The iterable is ordered so that classes occur after their super classes. | 21 /// [unordered], they are not included. |
22 Iterable<Class> get classes; | 22 Iterable<Class> getOrderedClasses(Iterable<Class> unordered); |
23 | 23 |
24 /// Returns the index of [class_] in the [classes] list. | 24 /// Returns the unique index of the [class_]. |
25 int getClassIndex(Class class_); | 25 int getClassIndex(Class class_); |
26 | 26 |
27 /// True if the program contains another class that is a subtype of given one. | 27 /// True if the program contains another class that is a subtype of given one. |
28 bool hasProperSubtypes(Class class_); | 28 bool hasProperSubtypes(Class class_); |
29 | 29 |
30 /// Returns the number of steps in the longest inheritance path from [class_] | 30 /// Returns the number of steps in the longest inheritance path from [class_] |
31 /// to [Object]. | 31 /// to [Object]. |
32 int getClassDepth(Class class_); | 32 int getClassDepth(Class class_); |
33 | 33 |
34 /// Returns a list of classes appropriate for use in calculating a least upper | 34 /// Returns a list of classes appropriate for use in calculating a least upper |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 final List<Class> classes; | 130 final List<Class> classes; |
131 | 131 |
132 final Map<Class, _ClassInfo> _infoFor = <Class, _ClassInfo>{}; | 132 final Map<Class, _ClassInfo> _infoFor = <Class, _ClassInfo>{}; |
133 | 133 |
134 ClosedWorldClassHierarchy(Program program) | 134 ClosedWorldClassHierarchy(Program program) |
135 : this._internal(program, _countClasses(program)); | 135 : this._internal(program, _countClasses(program)); |
136 | 136 |
137 @override | 137 @override |
138 int getClassIndex(Class class_) => _infoFor[class_].topologicalIndex; | 138 int getClassIndex(Class class_) => _infoFor[class_].topologicalIndex; |
139 | 139 |
| 140 @override |
| 141 Iterable<Class> getOrderedClasses(Iterable<Class> unordered) { |
| 142 var unorderedSet = unordered.toSet(); |
| 143 return classes.where(unorderedSet.contains); |
| 144 } |
| 145 |
140 /// True if [subclass] inherits from [superclass] though zero or more | 146 /// True if [subclass] inherits from [superclass] though zero or more |
141 /// `extends` relationships. | 147 /// `extends` relationships. |
142 bool isSubclassOf(Class subclass, Class superclass) { | 148 bool isSubclassOf(Class subclass, Class superclass) { |
143 if (identical(subclass, superclass)) return true; | 149 if (identical(subclass, superclass)) return true; |
144 return _infoFor[subclass].isSubclassOf(_infoFor[superclass]); | 150 return _infoFor[subclass].isSubclassOf(_infoFor[superclass]); |
145 } | 151 } |
146 | 152 |
147 /// True if [submixture] inherits from [superclass] though zero or more | 153 /// True if [submixture] inherits from [superclass] though zero or more |
148 /// `extends` and `with` relationships. | 154 /// `extends` and `with` relationships. |
149 bool isSubmixtureOf(Class submixture, Class superclass) { | 155 bool isSubmixtureOf(Class submixture, Class superclass) { |
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 class _LubHeap extends Heap<_ClassInfo> { | 1118 class _LubHeap extends Heap<_ClassInfo> { |
1113 @override | 1119 @override |
1114 bool sortsBefore(_ClassInfo a, _ClassInfo b) => sortsBeforeStatic(a, b); | 1120 bool sortsBefore(_ClassInfo a, _ClassInfo b) => sortsBeforeStatic(a, b); |
1115 | 1121 |
1116 static bool sortsBeforeStatic(_ClassInfo a, _ClassInfo b) { | 1122 static bool sortsBeforeStatic(_ClassInfo a, _ClassInfo b) { |
1117 if (a.depth > b.depth) return true; | 1123 if (a.depth > b.depth) return true; |
1118 if (a.depth < b.depth) return false; | 1124 if (a.depth < b.depth) return false; |
1119 return a.topologicalIndex < b.topologicalIndex; | 1125 return a.topologicalIndex < b.topologicalIndex; |
1120 } | 1126 } |
1121 } | 1127 } |
OLD | NEW |