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 '../elements/elements.dart' show Element; | 7 import '../elements/elements.dart' show Element; |
8 import '../util/util.dart' show Setlet; | 8 import '../util/util.dart' show Setlet; |
9 import 'use.dart' show DynamicUse, StaticUse, TypeUse; | 9 import 'use.dart' show DynamicUse, StaticUse, TypeUse; |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 iterable.forEach((e) => sb.write('\n $e')); | 54 iterable.forEach((e) => sb.write('\n $e')); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 add('dynamic uses', worldImpact.dynamicUses); | 58 add('dynamic uses', worldImpact.dynamicUses); |
59 add('static uses', worldImpact.staticUses); | 59 add('static uses', worldImpact.staticUses); |
60 add('type uses', worldImpact.typeUses); | 60 add('type uses', worldImpact.typeUses); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 class WorldImpactBuilder { | 64 abstract class WorldImpactBuilder { |
| 65 void registerDynamicUse(DynamicUse dynamicUse); |
| 66 void registerTypeUse(TypeUse typeUse); |
| 67 void registerStaticUse(StaticUse staticUse); |
| 68 } |
| 69 |
| 70 class WorldImpactBuilderImpl extends WorldImpact implements WorldImpactBuilder { |
65 // TODO(johnniwinther): Do we benefit from lazy initialization of the | 71 // TODO(johnniwinther): Do we benefit from lazy initialization of the |
66 // [Setlet]s? | 72 // [Setlet]s? |
67 Setlet<DynamicUse> _dynamicUses; | 73 Set<DynamicUse> _dynamicUses; |
68 Setlet<StaticUse> _staticUses; | 74 Set<StaticUse> _staticUses; |
69 Setlet<TypeUse> _typeUses; | 75 Set<TypeUse> _typeUses; |
70 | 76 |
71 void registerDynamicUse(DynamicUse dynamicUse) { | 77 void registerDynamicUse(DynamicUse dynamicUse) { |
72 assert(dynamicUse != null); | 78 assert(dynamicUse != null); |
73 if (_dynamicUses == null) { | 79 if (_dynamicUses == null) { |
74 _dynamicUses = new Setlet<DynamicUse>(); | 80 _dynamicUses = new Setlet<DynamicUse>(); |
75 } | 81 } |
76 _dynamicUses.add(dynamicUse); | 82 _dynamicUses.add(dynamicUse); |
77 } | 83 } |
78 | 84 |
79 Iterable<DynamicUse> get dynamicUses { | 85 Iterable<DynamicUse> get dynamicUses { |
(...skipping 18 matching lines...) Expand all Loading... |
98 _staticUses = new Setlet<StaticUse>(); | 104 _staticUses = new Setlet<StaticUse>(); |
99 } | 105 } |
100 _staticUses.add(staticUse); | 106 _staticUses.add(staticUse); |
101 } | 107 } |
102 | 108 |
103 Iterable<StaticUse> get staticUses { | 109 Iterable<StaticUse> get staticUses { |
104 return _staticUses != null ? _staticUses : const <StaticUse>[]; | 110 return _staticUses != null ? _staticUses : const <StaticUse>[]; |
105 } | 111 } |
106 } | 112 } |
107 | 113 |
| 114 /// [WorldImpactBuilder] that can create and collect a sequence of |
| 115 /// [WorldImpact]s. |
| 116 class StagedWorldImpactBuilder implements WorldImpactBuilder { |
| 117 final bool collectImpacts; |
| 118 WorldImpactBuilderImpl _currentBuilder; |
| 119 List<WorldImpactBuilderImpl> _builders = <WorldImpactBuilderImpl>[]; |
| 120 |
| 121 StagedWorldImpactBuilder({this.collectImpacts: false}); |
| 122 |
| 123 void _ensureBuilder() { |
| 124 if (_currentBuilder == null) { |
| 125 _currentBuilder = new WorldImpactBuilderImpl(); |
| 126 if (collectImpacts) { |
| 127 _builders.add(_currentBuilder); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 @override |
| 133 void registerTypeUse(TypeUse typeUse) { |
| 134 _ensureBuilder(); |
| 135 _currentBuilder.registerTypeUse(typeUse); |
| 136 } |
| 137 |
| 138 @override |
| 139 void registerDynamicUse(DynamicUse dynamicUse) { |
| 140 _ensureBuilder(); |
| 141 _currentBuilder.registerDynamicUse(dynamicUse); |
| 142 } |
| 143 |
| 144 @override |
| 145 void registerStaticUse(StaticUse staticUse) { |
| 146 _ensureBuilder(); |
| 147 _currentBuilder.registerStaticUse(staticUse); |
| 148 } |
| 149 |
| 150 /// Returns the [WorldImpact] built so far with this builder. The builder |
| 151 /// is reset, and if [collectImpacts] is `true` the impact is cached for |
| 152 /// [worldImpacts]. |
| 153 WorldImpact flush() { |
| 154 if (_currentBuilder == null) return const WorldImpact(); |
| 155 WorldImpact worldImpact = _currentBuilder; |
| 156 _currentBuilder = null; |
| 157 return worldImpact; |
| 158 } |
| 159 |
| 160 /// If [collectImpacts] is `true` this returns all [WorldImpact]s built with |
| 161 /// this builder. |
| 162 Iterable<WorldImpact> get worldImpacts => _builders; |
| 163 } |
| 164 |
108 /// Mutable implementation of [WorldImpact] used to transform | 165 /// Mutable implementation of [WorldImpact] used to transform |
109 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. | 166 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. |
110 class TransformedWorldImpact implements WorldImpact { | 167 class TransformedWorldImpact implements WorldImpact, WorldImpactBuilder { |
111 final WorldImpact worldImpact; | 168 final WorldImpact worldImpact; |
112 | 169 |
113 Setlet<StaticUse> _staticUses; | 170 Setlet<StaticUse> _staticUses; |
114 Setlet<TypeUse> _typeUses; | 171 Setlet<TypeUse> _typeUses; |
115 Setlet<DynamicUse> _dynamicUses; | 172 Setlet<DynamicUse> _dynamicUses; |
116 | 173 |
117 TransformedWorldImpact(this.worldImpact); | 174 TransformedWorldImpact(this.worldImpact); |
118 | 175 |
119 @override | 176 @override |
120 Iterable<DynamicUse> get dynamicUses { | 177 Iterable<DynamicUse> get dynamicUses { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 } | 290 } |
234 } | 291 } |
235 | 292 |
236 @override | 293 @override |
237 void visitTypeUse(TypeUse use) { | 294 void visitTypeUse(TypeUse use) { |
238 if (_visitTypeUse != null) { | 295 if (_visitTypeUse != null) { |
239 _visitTypeUse(use); | 296 _visitTypeUse(use); |
240 } | 297 } |
241 } | 298 } |
242 } | 299 } |
OLD | NEW |