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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/ordered_typeset.dart

Issue 52263003: Implement least upper bound. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments. Created 7 years 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
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
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.
4
5 library resolution.ordered_typeset_builder;
6
7 import 'dart2jslib.dart' show Compiler, MessageKind;
8 import 'dart_types.dart';
9 import 'elements/elements.dart' show ClassElement;
10 import 'util/util.dart' show Link, LinkBuilder;
11 import 'util/util_implementation.dart' show LinkEntry;
12
13 /**
14 * An ordered set of the supertypes of a class. The supertypes of a class are
15 * ordered by decreasing hierarchy depth and by the order they are extended,
16 * mixed in, or implemented.
17 *
18 * For these classes
19 *
20 * class A {} // Depth = 1.
21 * class B {} // Depth = 1.
22 * class C extends B implements A {} // Depth 2.
23 *
24 * the ordered supertypes are
25 *
26 * A: [A, Object]
27 * B: [B, Object]
28 * C: [C, B, A, Object]
29 */
30 class OrderedTypeSet {
31 final List<Link<DartType>> _levels;
32 final Link<DartType> types;
33 final Link<DartType> _supertypes;
34
35 OrderedTypeSet._internal(List<Link<DartType>> this._levels,
36 Link<DartType> this.types,
37 Link<DartType> this._supertypes);
38
39 factory OrderedTypeSet.singleton(DartType type) {
40 Link<DartType> types =
41 new LinkEntry<DartType>(type, const Link<DartType>());
42 List<Link<DartType>> list = new List<Link<DartType>>(1);
43 list[0] = types;
44 return new OrderedTypeSet._internal(list, types, const Link<DartType>());
45 }
46
47 /// Creates a new [OrderedTypeSet] for [type] when it directly extends the
48 /// class which this set represents. This is for instance used to create the
49 /// type set for [ClosureClassElement] which extends [Closure].
50 OrderedTypeSet extendClass(InterfaceType type) {
51 assert(type.treatAsRaw);
52 Link<DartType> extendedTypes =
53 new LinkEntry<DartType>(type, types);
54 List<Link<DartType>> list = new List<Link<DartType>>(levels + 1);
55 for (int i = 0; i < levels; i++) {
56 list[i] = _levels[i];
57 }
58 list[levels] = extendedTypes;
59 return new OrderedTypeSet._internal(
60 list, extendedTypes, _supertypes.prepend(types.head));
61 }
62
63 Link<DartType> get supertypes => _supertypes;
64
65 int get levels => _levels.length;
66
67 int get maxDepth => levels - 1;
68
69 Link<DartType> operator [](int index) {
70 if (index < levels) {
71 return _levels[index];
72 }
73 return const Link<DartType>();
74 }
75
76 void forEach(int level, void f(DartType type)) {
77 if (level < levels) {
78 Link<DartType> pointer = _levels[level];
79 Link<DartType> end =
80 level > 0 ? _levels[level - 1] : const Link<DartType>();
81 while (!identical(pointer, end)) {
82 f(pointer.head);
83 pointer = pointer.tail;
84 }
85 }
86 }
87
88 String toString() => types.toString();
89 }
90
91 /**
92 * Builder for creation an ordered set of the supertypes of a class. The
93 * supertypes are ordered by decreasing hierarchy depth and by the order they
94 * are extended, mixed in, or implemented.
95 *
96 * For these classes
97 *
98 * class A {} // Depth = 1.
99 * class B {} // Depth = 1.
100 * class C extends B implements A {} // Depth 2.
101 *
102 * the ordered supertypes are
103 *
104 * A: [A, Object]
105 * B: [B, Object]
106 * C: [C, B, A, Object]
107 */
108 class OrderedTypeSetBuilder {
109 Map<int, LinkEntry<DartType>> map = new Map<int, LinkEntry<DartType>>();
110 // TODO(15296): Avoid computing this order on the side when member
111 // lookup handles multiply inherited members correctly.
112 LinkBuilder<DartType> allSupertypes = new LinkBuilder<DartType>();
113 int maxDepth = -1;
114
115 final ClassElement cls;
116
117 OrderedTypeSetBuilder(this.cls);
118
119 void add(Compiler compiler, InterfaceType type) {
120 if (type.element == cls) {
121 if (type.element != compiler.objectClass) {
122 allSupertypes.addLast(compiler.objectClass.computeType(compiler));
123 }
124 _addAtDepth(compiler, type, maxDepth + 1);
125 } else {
126 if (type.element != compiler.objectClass) {
127 allSupertypes.addLast(type);
128 }
129 _addAtDepth(compiler, type, type.element.hierarchyDepth);
130 }
131 }
132
133 void _addAtDepth(Compiler compiler, InterfaceType type, int depth) {
134 LinkEntry<DartType> prev = null;
135 LinkEntry<DartType> link = map[depth];
136 while (link != null) {
137 DartType existingType = link.head;
138 if (existingType == type) return;
139 if (existingType.element == type.element) {
140 compiler.reportError(cls,
141 MessageKind.MULTI_INHERITANCE,
142 {'thisType': cls.computeType(compiler),
143 'firstType': existingType,
144 'secondType': type});
145 return;
146 }
147 prev = link;
148 link = link.tail;
149 }
150 LinkEntry<DartType> next = new LinkEntry<DartType>(type);
151 next.tail = null;
152 if (prev == null) {
153 map[depth] = next;
154 } else {
155 prev.tail = next;
156 }
157 if (depth > maxDepth) {
158 maxDepth = depth;
159 }
160 }
161
162 OrderedTypeSet toTypeSet() {
163 List<Link<DartType>> levels = new List<Link<DartType>>(maxDepth + 1);
164 if (maxDepth < 0) {
165 return new OrderedTypeSet._internal(
166 levels, const Link<DartType>(), const Link<DartType>());
167 }
168 Link<DartType> next = const Link<DartType>();
169 for (int depth = 0; depth <= maxDepth; depth++) {
170 LinkEntry<DartType> first = map[depth];
171 if (first == null) {
172 levels[depth] = next;
173 } else {
174 levels[depth] = first;
175 LinkEntry<DartType> last = first;
176 while (last.tail != null) {
177 last = last.tail;
178 }
179 last.tail = next;
180 next = first;
181 }
182 }
183 return new OrderedTypeSet._internal(
184 levels, levels.last, allSupertypes.toLink());
185 }
186
187 String toString() {
188 StringBuffer sb = new StringBuffer();
189 for (int depth = 0; depth <= maxDepth; depth++) {
190 sb.write('$depth: ');
191 LinkEntry<DartType> first = map[depth];
192 if (first != null) {
193 sb.write('${first.head}');
194 while (first.tail != null) {
195 sb.write(', ${first.tail.head}');
196 first = first.tail;
197 }
198 }
199 sb.write('\n');
200 }
201 return sb.toString();
202 }
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698