OLD | NEW |
---|---|
(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 { | |
karlklose
2013/11/21 13:29:10
Where do you enforce that this really is a set? Is
Johnni Winther
2013/11/25 13:58:27
Yes. Made the default constructor private to furth
| |
31 final List<Link<DartType>> _levels; | |
32 final Link<DartType> _head; | |
karlklose
2013/11/21 13:29:10
What is this used for? Can you find a better name?
Johnni Winther
2013/11/25 13:58:27
It holds the list of the types. Renamed to types (
| |
33 final Link<DartType> _supertypes; | |
34 | |
35 OrderedTypeSet(List<Link<DartType>> this._levels, | |
36 Link<DartType> this._head, | |
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(list, types, const Link<DartType>()); | |
45 } | |
46 | |
47 OrderedTypeSet extendClass(InterfaceType type) { | |
karlklose
2013/11/21 13:29:10
Please add a comment explaining what this method d
Johnni Winther
2013/11/25 13:58:27
Done.
| |
48 assert(type.treatAsRaw); | |
49 Link<DartType> types = | |
50 new LinkEntry<DartType>(type, _head); | |
51 List<Link<DartType>> list = new List<Link<DartType>>(levels+1); | |
karlklose
2013/11/21 13:29:10
Missing spaces around operator 'levels_+_1'.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
52 for (int i = 0 ; i < levels ; i++) { | |
karlklose
2013/11/21 13:29:10
No space before ';'.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
53 list[i] = _levels[i]; | |
54 } | |
55 list[levels] = types; | |
56 return new OrderedTypeSet(list, types, _supertypes.prepend(_head.head)); | |
57 } | |
58 | |
59 | |
karlklose
2013/11/21 13:29:10
Remove empty line.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
60 Link<DartType> get types => _head; | |
61 | |
62 Link<DartType> get supertypes => _supertypes; | |
63 | |
64 int get levels => _levels.length; | |
65 | |
66 int get maxDepth => levels-1; | |
karlklose
2013/11/21 13:29:10
Missing spaces around operator.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
67 | |
68 Link<DartType> operator [](int index) { | |
69 if (index < levels) { | |
70 return _levels[index]; | |
71 } | |
72 return const Link<DartType>(); | |
73 } | |
74 | |
75 void forEach(int level, void f(DartType type)) { | |
76 if (level < levels) { | |
77 Link<DartType> pointer = _levels[level]; | |
78 Link<DartType> end = | |
79 level > 0 ? _levels[level-1] : const Link<DartType>(); | |
karlklose
2013/11/21 13:29:10
Ditto.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
80 while (!identical(pointer, end)) { | |
81 f(pointer.head); | |
82 pointer = pointer.tail; | |
83 } | |
84 } | |
85 } | |
86 | |
87 String toString() => types.toString(); | |
88 } | |
89 | |
90 /** | |
91 * Builder for creation an ordered set of the supertypes of a class. The | |
92 * supertypes are ordered by decreasing hierarchy depth and by the order they | |
93 * are extended, mixed in, or implemented. | |
94 * | |
95 * For these classes | |
96 * | |
97 * class A {} // Depth = 1. | |
98 * class B {} // Depth = 1. | |
99 * class C extends B implements A {} // Depth 2. | |
100 * | |
101 * the ordered supertypes are | |
102 * | |
103 * A: [A, Object] | |
104 * B: [B, Object] | |
105 * C: [C, B, A, Object] | |
106 */ | |
107 class OrderedTypeSetBuilder { | |
108 Map<int, LinkEntry<DartType>> map = new Map<int, LinkEntry<DartType>>(); | |
109 // TODO(johnniwinther): Avoid computing this order on the side when member | |
110 // lookup handles multiply inherited members correctly. | |
111 LinkBuilder<DartType> allSupertypes = new LinkBuilder<DartType>(); | |
112 int maxDepth = -1; | |
113 | |
114 final ClassElement cls; | |
115 | |
116 OrderedTypeSetBuilder(this.cls); | |
117 | |
118 void add(Compiler compiler, InterfaceType type) { | |
119 if (type.element == cls) { | |
120 if (type.element != compiler.objectClass) { | |
121 allSupertypes.addLast(compiler.objectClass.computeType(compiler)); | |
122 } | |
123 _addAtDepth(compiler, type, maxDepth+1); | |
karlklose
2013/11/21 13:29:10
Spaces around operator.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
124 } else { | |
125 if (type.element != compiler.objectClass) { | |
126 allSupertypes.addLast(type); | |
127 } | |
128 _addAtDepth(compiler, type, type.element.hierarchyDepth); | |
129 } | |
130 } | |
131 | |
132 void _addAtDepth(Compiler compiler, InterfaceType type, int depth) { | |
133 LinkEntry<DartType> head = map[depth]; | |
karlklose
2013/11/21 13:29:10
Is [head] used somewhere else than in l. 135? If
Johnni Winther
2013/11/25 13:58:27
Done.
| |
134 LinkEntry<DartType> prev = null; | |
135 LinkEntry<DartType> link = head; | |
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 toSet() { | |
karlklose
2013/11/21 13:29:10
toSet -> toTypeSet?
Johnni Winther
2013/11/25 13:58:27
Done.
| |
163 List<Link<DartType>> levels = new List<Link<DartType>>(maxDepth+1); | |
karlklose
2013/11/21 13:29:10
Spaces.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
164 if (maxDepth < 0) { | |
165 return new OrderedTypeSet( | |
166 levels, const Link<DartType>(), const Link<DartType>()); | |
167 } | |
168 Link<DartType> next = const Link<DartType>(); | |
169 for (int depth = 0 ; depth <= maxDepth ; depth++) { | |
karlklose
2013/11/21 13:29:10
No spaces before ';'.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
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(levels, levels.last, allSupertypes.toLink()); | |
184 } | |
185 | |
186 String toString() { | |
187 StringBuffer sb = new StringBuffer(); | |
188 for (int depth = 0 ; depth <= maxDepth ; depth++) { | |
karlklose
2013/11/21 13:29:10
Ditto.
Johnni Winther
2013/11/25 13:58:27
Done.
| |
189 sb.write('$depth: '); | |
190 LinkEntry<DartType> first = map[depth]; | |
191 if (first != null) { | |
192 sb.write('${first.head}'); | |
193 while (first.tail != null) { | |
194 sb.write(', ${first.tail.head}'); | |
195 first = first.tail; | |
196 } | |
197 } | |
198 sb.write('\n'); | |
199 } | |
200 return sb.toString(); | |
201 } | |
202 } | |
OLD | NEW |