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 library dart2js.universe.world_impact; | 5 library dart2js.universe.world_impact; |
6 | 6 |
7 import '../dart_types.dart' show | 7 import '../dart_types.dart' show |
8 DartType, | 8 DartType, |
9 InterfaceType; | 9 InterfaceType; |
10 import '../elements/elements.dart' show | 10 import '../elements/elements.dart' show |
11 Element, | 11 Element, |
12 LocalFunctionElement, | 12 LocalFunctionElement, |
13 MethodElement; | 13 MethodElement; |
14 import '../util/util.dart' show | 14 import '../util/util.dart' show |
15 Setlet; | 15 Setlet; |
16 | 16 |
17 import 'use.dart' show | 17 import 'use.dart' show |
18 DynamicUse, | 18 DynamicUse, |
19 StaticUse; | 19 StaticUse, |
| 20 TypeUse; |
20 | 21 |
21 class WorldImpact { | 22 class WorldImpact { |
22 const WorldImpact(); | 23 const WorldImpact(); |
23 | 24 |
24 Iterable<DynamicUse> get dynamicUses => | 25 Iterable<DynamicUse> get dynamicUses => |
25 const <DynamicUse>[]; | 26 const <DynamicUse>[]; |
26 | 27 |
27 Iterable<StaticUse> get staticUses => const <StaticUse>[]; | 28 Iterable<StaticUse> get staticUses => const <StaticUse>[]; |
28 | 29 |
29 // TODO(johnniwinther): Replace this by called constructors with type | 30 // TODO(johnniwinther): Replace this by called constructors with type |
30 // arguments. | 31 // arguments. |
31 Iterable<InterfaceType> get instantiatedTypes => const <InterfaceType>[]; | 32 // TODO(johnniwinther): Collect all checked types for checked mode separately |
| 33 // to support serialization. |
32 | 34 |
33 // TODO(johnniwinther): Collect checked types for checked mode separately to | 35 Iterable<TypeUse> get typeUses => const <TypeUse>[]; |
34 // support serialization. | |
35 Iterable<DartType> get isChecks => const <DartType>[]; | |
36 | |
37 Iterable<DartType> get checkedModeChecks => const <DartType>[]; | |
38 | |
39 Iterable<DartType> get asCasts => const <DartType>[]; | |
40 | |
41 Iterable<DartType> get onCatchTypes => const <DartType>[]; | |
42 | 36 |
43 Iterable<LocalFunctionElement> get closures => const <LocalFunctionElement>[]; | 37 Iterable<LocalFunctionElement> get closures => const <LocalFunctionElement>[]; |
44 | 38 |
45 Iterable<DartType> get typeLiterals => const <DartType>[]; | |
46 | |
47 String toString() => dump(this); | 39 String toString() => dump(this); |
48 | 40 |
49 static String dump(WorldImpact worldImpact) { | 41 static String dump(WorldImpact worldImpact) { |
50 StringBuffer sb = new StringBuffer(); | 42 StringBuffer sb = new StringBuffer(); |
51 printOn(sb, worldImpact); | 43 printOn(sb, worldImpact); |
52 return sb.toString(); | 44 return sb.toString(); |
53 } | 45 } |
54 | 46 |
55 static void printOn(StringBuffer sb, WorldImpact worldImpact) { | 47 static void printOn(StringBuffer sb, WorldImpact worldImpact) { |
56 void add(String title, Iterable iterable) { | 48 void add(String title, Iterable iterable) { |
57 if (iterable.isNotEmpty) { | 49 if (iterable.isNotEmpty) { |
58 sb.write('\n $title:'); | 50 sb.write('\n $title:'); |
59 iterable.forEach((e) => sb.write('\n $e')); | 51 iterable.forEach((e) => sb.write('\n $e')); |
60 } | 52 } |
61 } | 53 } |
62 | 54 |
63 add('dynamic uses', worldImpact.dynamicUses); | 55 add('dynamic uses', worldImpact.dynamicUses); |
64 add('static uses', worldImpact.staticUses); | 56 add('static uses', worldImpact.staticUses); |
65 add('instantiated types', worldImpact.instantiatedTypes); | 57 add('type uses', worldImpact.typeUses); |
66 add('is-checks', worldImpact.isChecks); | |
67 add('checked-mode checks', worldImpact.checkedModeChecks); | |
68 add('as-casts', worldImpact.asCasts); | |
69 add('on-catch-types', worldImpact.onCatchTypes); | |
70 add('closures', worldImpact.closures); | 58 add('closures', worldImpact.closures); |
71 add('type literals', worldImpact.typeLiterals); | |
72 } | 59 } |
73 } | 60 } |
74 | 61 |
75 class WorldImpactBuilder { | 62 class WorldImpactBuilder { |
76 // TODO(johnniwinther): Do we benefit from lazy initialization of the | 63 // TODO(johnniwinther): Do we benefit from lazy initialization of the |
77 // [Setlet]s? | 64 // [Setlet]s? |
78 Setlet<DynamicUse> _dynamicUses; | 65 Setlet<DynamicUse> _dynamicUses; |
79 Setlet<InterfaceType> _instantiatedTypes; | |
80 Setlet<StaticUse> _staticUses; | 66 Setlet<StaticUse> _staticUses; |
81 Setlet<DartType> _isChecks; | 67 Setlet<TypeUse> _typeUses; |
82 Setlet<DartType> _asCasts; | |
83 Setlet<DartType> _checkedModeChecks; | |
84 Setlet<DartType> _onCatchTypes; | |
85 Setlet<LocalFunctionElement> _closures; | 68 Setlet<LocalFunctionElement> _closures; |
86 Setlet<DartType> _typeLiterals; | |
87 | 69 |
88 void registerDynamicUse(DynamicUse dynamicUse) { | 70 void registerDynamicUse(DynamicUse dynamicUse) { |
89 assert(dynamicUse != null); | 71 assert(dynamicUse != null); |
90 if (_dynamicUses == null) { | 72 if (_dynamicUses == null) { |
91 _dynamicUses = new Setlet<DynamicUse>(); | 73 _dynamicUses = new Setlet<DynamicUse>(); |
92 } | 74 } |
93 _dynamicUses.add(dynamicUse); | 75 _dynamicUses.add(dynamicUse); |
94 } | 76 } |
95 | 77 |
96 Iterable<DynamicUse> get dynamicUses { | 78 Iterable<DynamicUse> get dynamicUses { |
97 return _dynamicUses != null | 79 return _dynamicUses != null |
98 ? _dynamicUses : const <DynamicUse>[]; | 80 ? _dynamicUses : const <DynamicUse>[]; |
99 } | 81 } |
100 | 82 |
101 void registerInstantiatedType(InterfaceType type) { | 83 void registerTypeUse(TypeUse typeUse) { |
102 assert(type != null); | 84 assert(typeUse != null); |
103 if (_instantiatedTypes == null) { | 85 if (_typeUses == null) { |
104 _instantiatedTypes = new Setlet<InterfaceType>(); | 86 _typeUses = new Setlet<TypeUse>(); |
105 } | 87 } |
106 _instantiatedTypes.add(type); | 88 _typeUses.add(typeUse); |
107 } | 89 } |
108 | 90 |
109 Iterable<InterfaceType> get instantiatedTypes { | 91 Iterable<TypeUse> get typeUses { |
110 return _instantiatedTypes != null | 92 return _typeUses != null |
111 ? _instantiatedTypes : const <InterfaceType>[]; | 93 ? _typeUses : const <TypeUse>[]; |
112 } | |
113 | |
114 void registerTypeLiteral(DartType type) { | |
115 assert(type != null); | |
116 if (_typeLiterals == null) { | |
117 _typeLiterals = new Setlet<DartType>(); | |
118 } | |
119 _typeLiterals.add(type); | |
120 } | |
121 | |
122 Iterable<DartType> get typeLiterals { | |
123 return _typeLiterals != null | |
124 ? _typeLiterals : const <DartType>[]; | |
125 } | 94 } |
126 | 95 |
127 void registerStaticUse(StaticUse staticUse) { | 96 void registerStaticUse(StaticUse staticUse) { |
128 assert(staticUse != null); | 97 assert(staticUse != null); |
129 if (_staticUses == null) { | 98 if (_staticUses == null) { |
130 _staticUses = new Setlet<StaticUse>(); | 99 _staticUses = new Setlet<StaticUse>(); |
131 } | 100 } |
132 _staticUses.add(staticUse); | 101 _staticUses.add(staticUse); |
133 } | 102 } |
134 | 103 |
135 Iterable<StaticUse> get staticUses { | 104 Iterable<StaticUse> get staticUses { |
136 return _staticUses != null ? _staticUses : const <StaticUse>[]; | 105 return _staticUses != null ? _staticUses : const <StaticUse>[]; |
137 } | 106 } |
138 | 107 |
139 void registerIsCheck(DartType type) { | |
140 assert(type != null); | |
141 if (_isChecks == null) { | |
142 _isChecks = new Setlet<DartType>(); | |
143 } | |
144 _isChecks.add(type); | |
145 } | |
146 | |
147 Iterable<DartType> get isChecks { | |
148 return _isChecks != null | |
149 ? _isChecks : const <DartType>[]; | |
150 } | |
151 | |
152 void registerAsCast(DartType type) { | |
153 if (_asCasts == null) { | |
154 _asCasts = new Setlet<DartType>(); | |
155 } | |
156 _asCasts.add(type); | |
157 } | |
158 | |
159 Iterable<DartType> get asCasts { | |
160 return _asCasts != null | |
161 ? _asCasts : const <DartType>[]; | |
162 } | |
163 | |
164 void registerCheckedModeCheckedType(DartType type) { | |
165 if (_checkedModeChecks == null) { | |
166 _checkedModeChecks = new Setlet<DartType>(); | |
167 } | |
168 _checkedModeChecks.add(type); | |
169 } | |
170 | |
171 Iterable<DartType> get checkedModeChecks { | |
172 return _checkedModeChecks != null | |
173 ? _checkedModeChecks : const <DartType>[]; | |
174 } | |
175 | |
176 void registerOnCatchType(DartType type) { | |
177 assert(type != null); | |
178 if (_onCatchTypes == null) { | |
179 _onCatchTypes = new Setlet<DartType>(); | |
180 } | |
181 _onCatchTypes.add(type); | |
182 } | |
183 | |
184 Iterable<DartType> get onCatchTypes { | |
185 return _onCatchTypes != null | |
186 ? _onCatchTypes : const <DartType>[]; | |
187 } | |
188 | |
189 void registerClosure(LocalFunctionElement element) { | 108 void registerClosure(LocalFunctionElement element) { |
190 if (_closures == null) { | 109 if (_closures == null) { |
191 _closures = new Setlet<LocalFunctionElement>(); | 110 _closures = new Setlet<LocalFunctionElement>(); |
192 } | 111 } |
193 _closures.add(element); | 112 _closures.add(element); |
194 } | 113 } |
195 | 114 |
196 Iterable<LocalFunctionElement> get closures { | 115 Iterable<LocalFunctionElement> get closures { |
197 return _closures != null | 116 return _closures != null |
198 ? _closures : const <LocalFunctionElement>[]; | 117 ? _closures : const <LocalFunctionElement>[]; |
199 } | 118 } |
200 } | 119 } |
201 | 120 |
202 /// Mutable implementation of [WorldImpact] used to transform | 121 /// Mutable implementation of [WorldImpact] used to transform |
203 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. | 122 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. |
204 class TransformedWorldImpact implements WorldImpact { | 123 class TransformedWorldImpact implements WorldImpact { |
205 final WorldImpact worldImpact; | 124 final WorldImpact worldImpact; |
206 | 125 |
207 Setlet<StaticUse> _staticUses; | 126 Setlet<StaticUse> _staticUses; |
208 Setlet<InterfaceType> _instantiatedTypes; | 127 Setlet<TypeUse> _typeUses; |
209 Setlet<DynamicUse> _dynamicUses; | 128 Setlet<DynamicUse> _dynamicUses; |
210 | 129 |
211 TransformedWorldImpact(this.worldImpact); | 130 TransformedWorldImpact(this.worldImpact); |
212 | 131 |
213 @override | 132 @override |
214 Iterable<DartType> get asCasts => worldImpact.asCasts; | |
215 | |
216 @override | |
217 Iterable<DartType> get checkedModeChecks => worldImpact.checkedModeChecks; | |
218 | |
219 @override | |
220 Iterable<DynamicUse> get dynamicUses { | 133 Iterable<DynamicUse> get dynamicUses { |
221 return _dynamicUses != null | 134 return _dynamicUses != null |
222 ? _dynamicUses : worldImpact.dynamicUses; | 135 ? _dynamicUses : worldImpact.dynamicUses; |
223 } | 136 } |
224 | 137 |
225 @override | |
226 Iterable<DartType> get isChecks => worldImpact.isChecks; | |
227 | |
228 @override | |
229 Iterable<DartType> get onCatchTypes => worldImpact.onCatchTypes; | |
230 | |
231 _unsupported(String message) => throw new UnsupportedError(message); | |
232 | |
233 void registerDynamicUse(DynamicUse dynamicUse) { | 138 void registerDynamicUse(DynamicUse dynamicUse) { |
234 if (_dynamicUses == null) { | 139 if (_dynamicUses == null) { |
235 _dynamicUses = new Setlet<DynamicUse>(); | 140 _dynamicUses = new Setlet<DynamicUse>(); |
236 _dynamicUses.addAll(worldImpact.dynamicUses); | 141 _dynamicUses.addAll(worldImpact.dynamicUses); |
237 } | 142 } |
238 _dynamicUses.add(dynamicUse); | 143 _dynamicUses.add(dynamicUse); |
239 } | 144 } |
240 | 145 |
241 void registerInstantiatedType(InterfaceType type) { | 146 void registerTypeUse(TypeUse typeUse) { |
242 if (_instantiatedTypes == null) { | 147 if (_typeUses == null) { |
243 _instantiatedTypes = new Setlet<InterfaceType>(); | 148 _typeUses = new Setlet<TypeUse>(); |
244 _instantiatedTypes.addAll(worldImpact.instantiatedTypes); | 149 _typeUses.addAll(worldImpact.typeUses); |
245 } | 150 } |
246 _instantiatedTypes.add(type); | 151 _typeUses.add(typeUse); |
247 } | 152 } |
248 | 153 |
249 @override | 154 @override |
250 Iterable<InterfaceType> get instantiatedTypes { | 155 Iterable<TypeUse> get typeUses { |
251 return _instantiatedTypes != null | 156 return _typeUses != null |
252 ? _instantiatedTypes : worldImpact.instantiatedTypes; | 157 ? _typeUses : worldImpact.typeUses; |
253 } | |
254 | |
255 @override | |
256 Iterable<DartType> get typeLiterals { | |
257 return worldImpact.typeLiterals; | |
258 } | 158 } |
259 | 159 |
260 void registerStaticUse(StaticUse staticUse) { | 160 void registerStaticUse(StaticUse staticUse) { |
261 if (_staticUses == null) { | 161 if (_staticUses == null) { |
262 _staticUses = new Setlet<StaticUse>(); | 162 _staticUses = new Setlet<StaticUse>(); |
263 _staticUses.addAll(worldImpact.staticUses); | 163 _staticUses.addAll(worldImpact.staticUses); |
264 } | 164 } |
265 _staticUses.add(staticUse); | 165 _staticUses.add(staticUse); |
266 } | 166 } |
267 | 167 |
268 @override | 168 @override |
269 Iterable<StaticUse> get staticUses { | 169 Iterable<StaticUse> get staticUses { |
270 return _staticUses != null ? _staticUses : worldImpact.staticUses; | 170 return _staticUses != null ? _staticUses : worldImpact.staticUses; |
271 } | 171 } |
272 | 172 |
273 @override | 173 @override |
274 Iterable<LocalFunctionElement> get closures => worldImpact.closures; | 174 Iterable<LocalFunctionElement> get closures => worldImpact.closures; |
275 | 175 |
276 String toString() { | 176 String toString() { |
277 StringBuffer sb = new StringBuffer(); | 177 StringBuffer sb = new StringBuffer(); |
278 sb.write('TransformedWorldImpact($worldImpact)'); | 178 sb.write('TransformedWorldImpact($worldImpact)'); |
279 WorldImpact.printOn(sb, this); | 179 WorldImpact.printOn(sb, this); |
280 return sb.toString(); | 180 return sb.toString(); |
281 } | 181 } |
282 } | 182 } |
OLD | NEW |