| 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 | 7 import '../elements/elements.dart' |
| 8 Element, | 8 show Element, LocalFunctionElement, MethodElement; |
| 9 LocalFunctionElement, | 9 import '../util/util.dart' show Setlet; |
| 10 MethodElement; | |
| 11 import '../util/util.dart' show | |
| 12 Setlet; | |
| 13 | 10 |
| 14 import 'use.dart' show | 11 import 'use.dart' show DynamicUse, StaticUse, TypeUse; |
| 15 DynamicUse, | |
| 16 StaticUse, | |
| 17 TypeUse; | |
| 18 | 12 |
| 19 class WorldImpact { | 13 class WorldImpact { |
| 20 const WorldImpact(); | 14 const WorldImpact(); |
| 21 | 15 |
| 22 Iterable<DynamicUse> get dynamicUses => | 16 Iterable<DynamicUse> get dynamicUses => const <DynamicUse>[]; |
| 23 const <DynamicUse>[]; | |
| 24 | 17 |
| 25 Iterable<StaticUse> get staticUses => const <StaticUse>[]; | 18 Iterable<StaticUse> get staticUses => const <StaticUse>[]; |
| 26 | 19 |
| 27 // TODO(johnniwinther): Replace this by called constructors with type | 20 // TODO(johnniwinther): Replace this by called constructors with type |
| 28 // arguments. | 21 // arguments. |
| 29 // TODO(johnniwinther): Collect all checked types for checked mode separately | 22 // TODO(johnniwinther): Collect all checked types for checked mode separately |
| 30 // to support serialization. | 23 // to support serialization. |
| 31 | 24 |
| 32 Iterable<TypeUse> get typeUses => const <TypeUse>[]; | 25 Iterable<TypeUse> get typeUses => const <TypeUse>[]; |
| 33 | 26 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 61 |
| 69 void registerDynamicUse(DynamicUse dynamicUse) { | 62 void registerDynamicUse(DynamicUse dynamicUse) { |
| 70 assert(dynamicUse != null); | 63 assert(dynamicUse != null); |
| 71 if (_dynamicUses == null) { | 64 if (_dynamicUses == null) { |
| 72 _dynamicUses = new Setlet<DynamicUse>(); | 65 _dynamicUses = new Setlet<DynamicUse>(); |
| 73 } | 66 } |
| 74 _dynamicUses.add(dynamicUse); | 67 _dynamicUses.add(dynamicUse); |
| 75 } | 68 } |
| 76 | 69 |
| 77 Iterable<DynamicUse> get dynamicUses { | 70 Iterable<DynamicUse> get dynamicUses { |
| 78 return _dynamicUses != null | 71 return _dynamicUses != null ? _dynamicUses : const <DynamicUse>[]; |
| 79 ? _dynamicUses : const <DynamicUse>[]; | |
| 80 } | 72 } |
| 81 | 73 |
| 82 void registerTypeUse(TypeUse typeUse) { | 74 void registerTypeUse(TypeUse typeUse) { |
| 83 assert(typeUse != null); | 75 assert(typeUse != null); |
| 84 if (_typeUses == null) { | 76 if (_typeUses == null) { |
| 85 _typeUses = new Setlet<TypeUse>(); | 77 _typeUses = new Setlet<TypeUse>(); |
| 86 } | 78 } |
| 87 _typeUses.add(typeUse); | 79 _typeUses.add(typeUse); |
| 88 } | 80 } |
| 89 | 81 |
| 90 Iterable<TypeUse> get typeUses { | 82 Iterable<TypeUse> get typeUses { |
| 91 return _typeUses != null | 83 return _typeUses != null ? _typeUses : const <TypeUse>[]; |
| 92 ? _typeUses : const <TypeUse>[]; | |
| 93 } | 84 } |
| 94 | 85 |
| 95 void registerStaticUse(StaticUse staticUse) { | 86 void registerStaticUse(StaticUse staticUse) { |
| 96 assert(staticUse != null); | 87 assert(staticUse != null); |
| 97 if (_staticUses == null) { | 88 if (_staticUses == null) { |
| 98 _staticUses = new Setlet<StaticUse>(); | 89 _staticUses = new Setlet<StaticUse>(); |
| 99 } | 90 } |
| 100 _staticUses.add(staticUse); | 91 _staticUses.add(staticUse); |
| 101 } | 92 } |
| 102 | 93 |
| 103 Iterable<StaticUse> get staticUses { | 94 Iterable<StaticUse> get staticUses { |
| 104 return _staticUses != null ? _staticUses : const <StaticUse>[]; | 95 return _staticUses != null ? _staticUses : const <StaticUse>[]; |
| 105 } | 96 } |
| 106 } | 97 } |
| 107 | 98 |
| 108 /// Mutable implementation of [WorldImpact] used to transform | 99 /// Mutable implementation of [WorldImpact] used to transform |
| 109 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. | 100 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. |
| 110 class TransformedWorldImpact implements WorldImpact { | 101 class TransformedWorldImpact implements WorldImpact { |
| 111 final WorldImpact worldImpact; | 102 final WorldImpact worldImpact; |
| 112 | 103 |
| 113 Setlet<StaticUse> _staticUses; | 104 Setlet<StaticUse> _staticUses; |
| 114 Setlet<TypeUse> _typeUses; | 105 Setlet<TypeUse> _typeUses; |
| 115 Setlet<DynamicUse> _dynamicUses; | 106 Setlet<DynamicUse> _dynamicUses; |
| 116 | 107 |
| 117 TransformedWorldImpact(this.worldImpact); | 108 TransformedWorldImpact(this.worldImpact); |
| 118 | 109 |
| 119 @override | 110 @override |
| 120 Iterable<DynamicUse> get dynamicUses { | 111 Iterable<DynamicUse> get dynamicUses { |
| 121 return _dynamicUses != null | 112 return _dynamicUses != null ? _dynamicUses : worldImpact.dynamicUses; |
| 122 ? _dynamicUses : worldImpact.dynamicUses; | |
| 123 } | 113 } |
| 124 | 114 |
| 125 void registerDynamicUse(DynamicUse dynamicUse) { | 115 void registerDynamicUse(DynamicUse dynamicUse) { |
| 126 if (_dynamicUses == null) { | 116 if (_dynamicUses == null) { |
| 127 _dynamicUses = new Setlet<DynamicUse>(); | 117 _dynamicUses = new Setlet<DynamicUse>(); |
| 128 _dynamicUses.addAll(worldImpact.dynamicUses); | 118 _dynamicUses.addAll(worldImpact.dynamicUses); |
| 129 } | 119 } |
| 130 _dynamicUses.add(dynamicUse); | 120 _dynamicUses.add(dynamicUse); |
| 131 } | 121 } |
| 132 | 122 |
| 133 void registerTypeUse(TypeUse typeUse) { | 123 void registerTypeUse(TypeUse typeUse) { |
| 134 if (_typeUses == null) { | 124 if (_typeUses == null) { |
| 135 _typeUses = new Setlet<TypeUse>(); | 125 _typeUses = new Setlet<TypeUse>(); |
| 136 _typeUses.addAll(worldImpact.typeUses); | 126 _typeUses.addAll(worldImpact.typeUses); |
| 137 } | 127 } |
| 138 _typeUses.add(typeUse); | 128 _typeUses.add(typeUse); |
| 139 } | 129 } |
| 140 | 130 |
| 141 @override | 131 @override |
| 142 Iterable<TypeUse> get typeUses { | 132 Iterable<TypeUse> get typeUses { |
| 143 return _typeUses != null | 133 return _typeUses != null ? _typeUses : worldImpact.typeUses; |
| 144 ? _typeUses : worldImpact.typeUses; | |
| 145 } | 134 } |
| 146 | 135 |
| 147 void registerStaticUse(StaticUse staticUse) { | 136 void registerStaticUse(StaticUse staticUse) { |
| 148 if (_staticUses == null) { | 137 if (_staticUses == null) { |
| 149 _staticUses = new Setlet<StaticUse>(); | 138 _staticUses = new Setlet<StaticUse>(); |
| 150 _staticUses.addAll(worldImpact.staticUses); | 139 _staticUses.addAll(worldImpact.staticUses); |
| 151 } | 140 } |
| 152 _staticUses.add(staticUse); | 141 _staticUses.add(staticUse); |
| 153 } | 142 } |
| 154 | 143 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 178 const ImpactUseCase(this.name); | 167 const ImpactUseCase(this.name); |
| 179 | 168 |
| 180 String toString() => 'ImpactUseCase($name)'; | 169 String toString() => 'ImpactUseCase($name)'; |
| 181 } | 170 } |
| 182 | 171 |
| 183 /// Strategy used for processing [WorldImpact] object in various use cases. | 172 /// Strategy used for processing [WorldImpact] object in various use cases. |
| 184 class ImpactStrategy { | 173 class ImpactStrategy { |
| 185 const ImpactStrategy(); | 174 const ImpactStrategy(); |
| 186 | 175 |
| 187 /// Applies [impact] to [visitor] for the [impactUseCase] of [element]. | 176 /// Applies [impact] to [visitor] for the [impactUseCase] of [element]. |
| 188 void visitImpact(Element element, | 177 void visitImpact(Element element, WorldImpact impact, |
| 189 WorldImpact impact, | 178 WorldImpactVisitor visitor, ImpactUseCase impactUseCase) { |
| 190 WorldImpactVisitor visitor, | |
| 191 ImpactUseCase impactUseCase) { | |
| 192 // Apply unconditionally. | 179 // Apply unconditionally. |
| 193 impact.apply(visitor); | 180 impact.apply(visitor); |
| 194 } | 181 } |
| 195 | 182 |
| 196 /// Notifies the strategy that no more impacts of [impactUseCase] will be | 183 /// Notifies the strategy that no more impacts of [impactUseCase] will be |
| 197 /// applied. | 184 /// applied. |
| 198 void onImpactUsed(ImpactUseCase impactUseCase) { | 185 void onImpactUsed(ImpactUseCase impactUseCase) { |
| 199 // Do nothing. | 186 // Do nothing. |
| 200 } | 187 } |
| 201 } | 188 } |
| 202 | 189 |
| 203 /// Visitor used to process the uses of a [WorldImpact]. | 190 /// Visitor used to process the uses of a [WorldImpact]. |
| 204 abstract class WorldImpactVisitor { | 191 abstract class WorldImpactVisitor { |
| 205 void visitStaticUse(StaticUse staticUse); | 192 void visitStaticUse(StaticUse staticUse); |
| 206 void visitDynamicUse(DynamicUse dynamicUse); | 193 void visitDynamicUse(DynamicUse dynamicUse); |
| 207 void visitTypeUse(TypeUse typeUse); | 194 void visitTypeUse(TypeUse typeUse); |
| 208 } | 195 } |
| 209 | 196 |
| 210 // TODO(johnniwinther): Remove these when we get anonymous local classes. | 197 // TODO(johnniwinther): Remove these when we get anonymous local classes. |
| 211 typedef void VisitUse<U>(U use); | 198 typedef void VisitUse<U>(U use); |
| 212 | 199 |
| 213 class WorldImpactVisitorImpl implements WorldImpactVisitor { | 200 class WorldImpactVisitorImpl implements WorldImpactVisitor { |
| 214 final VisitUse<StaticUse> _visitStaticUse; | 201 final VisitUse<StaticUse> _visitStaticUse; |
| 215 final VisitUse<DynamicUse> _visitDynamicUse; | 202 final VisitUse<DynamicUse> _visitDynamicUse; |
| 216 final VisitUse<TypeUse> _visitTypeUse; | 203 final VisitUse<TypeUse> _visitTypeUse; |
| 217 | 204 |
| 218 WorldImpactVisitorImpl( | 205 WorldImpactVisitorImpl( |
| 219 {VisitUse<StaticUse> visitStaticUse, | 206 {VisitUse<StaticUse> visitStaticUse, |
| 220 VisitUse<DynamicUse> visitDynamicUse, | 207 VisitUse<DynamicUse> visitDynamicUse, |
| 221 VisitUse<TypeUse> visitTypeUse}) | 208 VisitUse<TypeUse> visitTypeUse}) |
| 222 : _visitStaticUse = visitStaticUse, | 209 : _visitStaticUse = visitStaticUse, |
| 223 _visitDynamicUse = visitDynamicUse, | 210 _visitDynamicUse = visitDynamicUse, |
| 224 _visitTypeUse = visitTypeUse; | 211 _visitTypeUse = visitTypeUse; |
| 225 | 212 |
| 226 @override | 213 @override |
| 227 void visitStaticUse(StaticUse use) { | 214 void visitStaticUse(StaticUse use) { |
| 228 if (_visitStaticUse != null) { | 215 if (_visitStaticUse != null) { |
| 229 _visitStaticUse(use); | 216 _visitStaticUse(use); |
| 230 } | 217 } |
| 231 } | 218 } |
| 232 | 219 |
| 233 @override | 220 @override |
| 234 void visitDynamicUse(DynamicUse use) { | 221 void visitDynamicUse(DynamicUse use) { |
| 235 if (_visitDynamicUse != null) { | 222 if (_visitDynamicUse != null) { |
| 236 _visitDynamicUse(use); | 223 _visitDynamicUse(use); |
| 237 } | 224 } |
| 238 } | 225 } |
| 239 | 226 |
| 240 @override | 227 @override |
| 241 void visitTypeUse(TypeUse use) { | 228 void visitTypeUse(TypeUse use) { |
| 242 if (_visitTypeUse != null) { | 229 if (_visitTypeUse != null) { |
| 243 _visitTypeUse(use); | 230 _visitTypeUse(use); |
| 244 } | 231 } |
| 245 } | 232 } |
| 246 } | 233 } |
| 247 | |
| OLD | NEW |