| 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 'universe.dart' show | 17 import 'universe.dart' show |
| 18 UniverseSelector; | 18 UniverseSelector; |
| 19 import 'use.dart' show |
| 20 StaticUse; |
| 19 | 21 |
| 20 class WorldImpact { | 22 class WorldImpact { |
| 21 const WorldImpact(); | 23 const WorldImpact(); |
| 22 | 24 |
| 23 Iterable<UniverseSelector> get dynamicInvocations => | 25 Iterable<UniverseSelector> get dynamicUses => |
| 24 const <UniverseSelector>[]; | 26 const <UniverseSelector>[]; |
| 25 Iterable<UniverseSelector> get dynamicGetters => const <UniverseSelector>[]; | |
| 26 Iterable<UniverseSelector> get dynamicSetters => const <UniverseSelector>[]; | |
| 27 | 27 |
| 28 // TODO(johnniwinther): Split this into more precise subsets. | 28 Iterable<StaticUse> get staticUses => const <StaticUse>[]; |
| 29 Iterable<Element> get staticUses => const <Element>[]; | |
| 30 | 29 |
| 31 // TODO(johnniwinther): Replace this by called constructors with type | 30 // TODO(johnniwinther): Replace this by called constructors with type |
| 32 // arguments. | 31 // arguments. |
| 33 Iterable<InterfaceType> get instantiatedTypes => const <InterfaceType>[]; | 32 Iterable<InterfaceType> get instantiatedTypes => const <InterfaceType>[]; |
| 34 | 33 |
| 35 // TODO(johnniwinther): Collect checked types for checked mode separately to | 34 // TODO(johnniwinther): Collect checked types for checked mode separately to |
| 36 // support serialization. | 35 // support serialization. |
| 37 Iterable<DartType> get isChecks => const <DartType>[]; | 36 Iterable<DartType> get isChecks => const <DartType>[]; |
| 38 | 37 |
| 39 Iterable<DartType> get checkedModeChecks => const <DartType>[]; | 38 Iterable<DartType> get checkedModeChecks => const <DartType>[]; |
| 40 | 39 |
| 41 Iterable<DartType> get asCasts => const <DartType>[]; | 40 Iterable<DartType> get asCasts => const <DartType>[]; |
| 42 | 41 |
| 43 Iterable<DartType> get onCatchTypes => const <DartType>[]; | 42 Iterable<DartType> get onCatchTypes => const <DartType>[]; |
| 44 | 43 |
| 45 Iterable<MethodElement> get closurizedFunctions => const <MethodElement>[]; | |
| 46 | |
| 47 Iterable<LocalFunctionElement> get closures => const <LocalFunctionElement>[]; | 44 Iterable<LocalFunctionElement> get closures => const <LocalFunctionElement>[]; |
| 48 | 45 |
| 49 Iterable<DartType> get typeLiterals => const <DartType>[]; | 46 Iterable<DartType> get typeLiterals => const <DartType>[]; |
| 50 | 47 |
| 51 String toString() => dump(this); | 48 String toString() => dump(this); |
| 52 | 49 |
| 53 static String dump(WorldImpact worldImpact) { | 50 static String dump(WorldImpact worldImpact) { |
| 54 StringBuffer sb = new StringBuffer(); | 51 StringBuffer sb = new StringBuffer(); |
| 55 printOn(sb, worldImpact); | 52 printOn(sb, worldImpact); |
| 56 return sb.toString(); | 53 return sb.toString(); |
| 57 } | 54 } |
| 58 | 55 |
| 59 static void printOn(StringBuffer sb, WorldImpact worldImpact) { | 56 static void printOn(StringBuffer sb, WorldImpact worldImpact) { |
| 60 void add(String title, Iterable iterable) { | 57 void add(String title, Iterable iterable) { |
| 61 if (iterable.isNotEmpty) { | 58 if (iterable.isNotEmpty) { |
| 62 sb.write('\n $title:'); | 59 sb.write('\n $title:'); |
| 63 iterable.forEach((e) => sb.write('\n $e')); | 60 iterable.forEach((e) => sb.write('\n $e')); |
| 64 } | 61 } |
| 65 } | 62 } |
| 66 | 63 |
| 67 add('dynamic invocations', worldImpact.dynamicInvocations); | 64 add('dynamic uses', worldImpact.dynamicUses); |
| 68 add('dynamic getters', worldImpact.dynamicGetters); | |
| 69 add('dynamic setters', worldImpact.dynamicSetters); | |
| 70 add('static uses', worldImpact.staticUses); | 65 add('static uses', worldImpact.staticUses); |
| 71 add('instantiated types', worldImpact.instantiatedTypes); | 66 add('instantiated types', worldImpact.instantiatedTypes); |
| 72 add('is-checks', worldImpact.isChecks); | 67 add('is-checks', worldImpact.isChecks); |
| 73 add('checked-mode checks', worldImpact.checkedModeChecks); | 68 add('checked-mode checks', worldImpact.checkedModeChecks); |
| 74 add('as-casts', worldImpact.asCasts); | 69 add('as-casts', worldImpact.asCasts); |
| 75 add('on-catch-types', worldImpact.onCatchTypes); | 70 add('on-catch-types', worldImpact.onCatchTypes); |
| 76 add('closurized functions', worldImpact.closurizedFunctions); | |
| 77 add('closures', worldImpact.closures); | 71 add('closures', worldImpact.closures); |
| 78 add('type literals', worldImpact.typeLiterals); | 72 add('type literals', worldImpact.typeLiterals); |
| 79 } | 73 } |
| 80 } | 74 } |
| 81 | 75 |
| 82 class WorldImpactBuilder { | 76 class WorldImpactBuilder { |
| 83 // TODO(johnniwinther): Do we benefit from lazy initialization of the | 77 // TODO(johnniwinther): Do we benefit from lazy initialization of the |
| 84 // [Setlet]s? | 78 // [Setlet]s? |
| 85 Setlet<UniverseSelector> _dynamicInvocations; | 79 Setlet<UniverseSelector> _dynamicUses; |
| 86 Setlet<UniverseSelector> _dynamicGetters; | |
| 87 Setlet<UniverseSelector> _dynamicSetters; | |
| 88 Setlet<InterfaceType> _instantiatedTypes; | 80 Setlet<InterfaceType> _instantiatedTypes; |
| 89 Setlet<Element> _staticUses; | 81 Setlet<StaticUse> _staticUses; |
| 90 Setlet<DartType> _isChecks; | 82 Setlet<DartType> _isChecks; |
| 91 Setlet<DartType> _asCasts; | 83 Setlet<DartType> _asCasts; |
| 92 Setlet<DartType> _checkedModeChecks; | 84 Setlet<DartType> _checkedModeChecks; |
| 93 Setlet<DartType> _onCatchTypes; | 85 Setlet<DartType> _onCatchTypes; |
| 94 Setlet<MethodElement> _closurizedFunctions; | |
| 95 Setlet<LocalFunctionElement> _closures; | 86 Setlet<LocalFunctionElement> _closures; |
| 96 Setlet<DartType> _typeLiterals; | 87 Setlet<DartType> _typeLiterals; |
| 97 | 88 |
| 98 void registerDynamicGetter(UniverseSelector selector) { | 89 void registerDynamicUse(UniverseSelector dynamicUse) { |
| 99 assert(selector != null); | 90 assert(dynamicUse != null); |
| 100 if (_dynamicGetters == null) { | 91 if (_dynamicUses == null) { |
| 101 _dynamicGetters = new Setlet<UniverseSelector>(); | 92 _dynamicUses = new Setlet<UniverseSelector>(); |
| 102 } | 93 } |
| 103 _dynamicGetters.add(selector); | 94 _dynamicUses.add(dynamicUse); |
| 104 } | 95 } |
| 105 | 96 |
| 106 Iterable<UniverseSelector> get dynamicGetters { | 97 Iterable<UniverseSelector> get dynamicUses { |
| 107 return _dynamicGetters != null | 98 return _dynamicUses != null |
| 108 ? _dynamicGetters : const <UniverseSelector>[]; | 99 ? _dynamicUses : const <UniverseSelector>[]; |
| 109 } | |
| 110 | |
| 111 void registerDynamicInvocation(UniverseSelector selector) { | |
| 112 assert(selector != null); | |
| 113 if (_dynamicInvocations == null) { | |
| 114 _dynamicInvocations = new Setlet<UniverseSelector>(); | |
| 115 } | |
| 116 _dynamicInvocations.add(selector); | |
| 117 } | |
| 118 | |
| 119 Iterable<UniverseSelector> get dynamicInvocations { | |
| 120 return _dynamicInvocations != null | |
| 121 ? _dynamicInvocations : const <UniverseSelector>[]; | |
| 122 } | |
| 123 | |
| 124 void registerDynamicSetter(UniverseSelector selector) { | |
| 125 assert(selector != null); | |
| 126 if (_dynamicSetters == null) { | |
| 127 _dynamicSetters = new Setlet<UniverseSelector>(); | |
| 128 } | |
| 129 _dynamicSetters.add(selector); | |
| 130 } | |
| 131 | |
| 132 Iterable<UniverseSelector> get dynamicSetters { | |
| 133 return _dynamicSetters != null | |
| 134 ? _dynamicSetters : const <UniverseSelector>[]; | |
| 135 } | 100 } |
| 136 | 101 |
| 137 void registerInstantiatedType(InterfaceType type) { | 102 void registerInstantiatedType(InterfaceType type) { |
| 138 assert(type != null); | 103 assert(type != null); |
| 139 if (_instantiatedTypes == null) { | 104 if (_instantiatedTypes == null) { |
| 140 _instantiatedTypes = new Setlet<InterfaceType>(); | 105 _instantiatedTypes = new Setlet<InterfaceType>(); |
| 141 } | 106 } |
| 142 _instantiatedTypes.add(type); | 107 _instantiatedTypes.add(type); |
| 143 } | 108 } |
| 144 | 109 |
| 145 Iterable<InterfaceType> get instantiatedTypes { | 110 Iterable<InterfaceType> get instantiatedTypes { |
| 146 return _instantiatedTypes != null | 111 return _instantiatedTypes != null |
| 147 ? _instantiatedTypes : const <InterfaceType>[]; | 112 ? _instantiatedTypes : const <InterfaceType>[]; |
| 148 } | 113 } |
| 149 | 114 |
| 150 void registerTypeLiteral(DartType type) { | 115 void registerTypeLiteral(DartType type) { |
| 151 assert(type != null); | 116 assert(type != null); |
| 152 if (_typeLiterals == null) { | 117 if (_typeLiterals == null) { |
| 153 _typeLiterals = new Setlet<DartType>(); | 118 _typeLiterals = new Setlet<DartType>(); |
| 154 } | 119 } |
| 155 _typeLiterals.add(type); | 120 _typeLiterals.add(type); |
| 156 } | 121 } |
| 157 | 122 |
| 158 Iterable<DartType> get typeLiterals { | 123 Iterable<DartType> get typeLiterals { |
| 159 return _typeLiterals != null | 124 return _typeLiterals != null |
| 160 ? _typeLiterals : const <DartType>[]; | 125 ? _typeLiterals : const <DartType>[]; |
| 161 } | 126 } |
| 162 | 127 |
| 163 void registerStaticUse(Element element) { | 128 void registerStaticUse(StaticUse staticUse) { |
| 164 assert(element != null); | 129 assert(staticUse != null); |
| 165 if (_staticUses == null) { | 130 if (_staticUses == null) { |
| 166 _staticUses = new Setlet<Element>(); | 131 _staticUses = new Setlet<StaticUse>(); |
| 167 } | 132 } |
| 168 _staticUses.add(element); | 133 _staticUses.add(staticUse); |
| 169 } | 134 } |
| 170 | 135 |
| 171 Iterable<Element> get staticUses { | 136 Iterable<StaticUse> get staticUses { |
| 172 return _staticUses != null ? _staticUses : const <Element>[]; | 137 return _staticUses != null ? _staticUses : const <StaticUse>[]; |
| 173 } | 138 } |
| 174 | 139 |
| 175 void registerIsCheck(DartType type) { | 140 void registerIsCheck(DartType type) { |
| 176 assert(type != null); | 141 assert(type != null); |
| 177 if (_isChecks == null) { | 142 if (_isChecks == null) { |
| 178 _isChecks = new Setlet<DartType>(); | 143 _isChecks = new Setlet<DartType>(); |
| 179 } | 144 } |
| 180 _isChecks.add(type); | 145 _isChecks.add(type); |
| 181 } | 146 } |
| 182 | 147 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 _onCatchTypes = new Setlet<DartType>(); | 180 _onCatchTypes = new Setlet<DartType>(); |
| 216 } | 181 } |
| 217 _onCatchTypes.add(type); | 182 _onCatchTypes.add(type); |
| 218 } | 183 } |
| 219 | 184 |
| 220 Iterable<DartType> get onCatchTypes { | 185 Iterable<DartType> get onCatchTypes { |
| 221 return _onCatchTypes != null | 186 return _onCatchTypes != null |
| 222 ? _onCatchTypes : const <DartType>[]; | 187 ? _onCatchTypes : const <DartType>[]; |
| 223 } | 188 } |
| 224 | 189 |
| 225 void registerClosurizedFunction(MethodElement element) { | |
| 226 if (_closurizedFunctions == null) { | |
| 227 _closurizedFunctions = new Setlet<MethodElement>(); | |
| 228 } | |
| 229 _closurizedFunctions.add(element); | |
| 230 } | |
| 231 | |
| 232 Iterable<MethodElement> get closurizedFunctions { | |
| 233 return _closurizedFunctions != null | |
| 234 ? _closurizedFunctions : const <MethodElement>[]; | |
| 235 } | |
| 236 | |
| 237 void registerClosure(LocalFunctionElement element) { | 190 void registerClosure(LocalFunctionElement element) { |
| 238 if (_closures == null) { | 191 if (_closures == null) { |
| 239 _closures = new Setlet<LocalFunctionElement>(); | 192 _closures = new Setlet<LocalFunctionElement>(); |
| 240 } | 193 } |
| 241 _closures.add(element); | 194 _closures.add(element); |
| 242 } | 195 } |
| 243 | 196 |
| 244 Iterable<LocalFunctionElement> get closures { | 197 Iterable<LocalFunctionElement> get closures { |
| 245 return _closures != null | 198 return _closures != null |
| 246 ? _closures : const <LocalFunctionElement>[]; | 199 ? _closures : const <LocalFunctionElement>[]; |
| 247 } | 200 } |
| 248 } | 201 } |
| 249 | 202 |
| 250 /// Mutable implementation of [WorldImpact] used to transform | 203 /// Mutable implementation of [WorldImpact] used to transform |
| 251 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. | 204 /// [ResolutionImpact] or [CodegenImpact] to [WorldImpact]. |
| 252 class TransformedWorldImpact implements WorldImpact { | 205 class TransformedWorldImpact implements WorldImpact { |
| 253 final WorldImpact worldImpact; | 206 final WorldImpact worldImpact; |
| 254 | 207 |
| 255 Setlet<Element> _staticUses; | 208 Setlet<StaticUse> _staticUses; |
| 256 Setlet<InterfaceType> _instantiatedTypes; | 209 Setlet<InterfaceType> _instantiatedTypes; |
| 257 Setlet<UniverseSelector> _dynamicGetters; | 210 Setlet<UniverseSelector> _dynamicUses; |
| 258 Setlet<UniverseSelector> _dynamicInvocations; | |
| 259 Setlet<UniverseSelector> _dynamicSetters; | |
| 260 | 211 |
| 261 TransformedWorldImpact(this.worldImpact); | 212 TransformedWorldImpact(this.worldImpact); |
| 262 | 213 |
| 263 @override | 214 @override |
| 264 Iterable<DartType> get asCasts => worldImpact.asCasts; | 215 Iterable<DartType> get asCasts => worldImpact.asCasts; |
| 265 | 216 |
| 266 @override | 217 @override |
| 267 Iterable<DartType> get checkedModeChecks => worldImpact.checkedModeChecks; | 218 Iterable<DartType> get checkedModeChecks => worldImpact.checkedModeChecks; |
| 268 | 219 |
| 269 @override | 220 @override |
| 270 Iterable<MethodElement> get closurizedFunctions { | 221 Iterable<UniverseSelector> get dynamicUses { |
| 271 return worldImpact.closurizedFunctions; | 222 return _dynamicUses != null |
| 223 ? _dynamicUses : worldImpact.dynamicUses; |
| 272 } | 224 } |
| 273 | 225 |
| 274 @override | 226 @override |
| 275 Iterable<UniverseSelector> get dynamicGetters { | |
| 276 return _dynamicGetters != null | |
| 277 ? _dynamicGetters : worldImpact.dynamicGetters; | |
| 278 } | |
| 279 | |
| 280 @override | |
| 281 Iterable<UniverseSelector> get dynamicInvocations { | |
| 282 return _dynamicInvocations != null | |
| 283 ? _dynamicInvocations : worldImpact.dynamicInvocations; | |
| 284 } | |
| 285 | |
| 286 @override | |
| 287 Iterable<UniverseSelector> get dynamicSetters { | |
| 288 return _dynamicSetters != null | |
| 289 ? _dynamicSetters : worldImpact.dynamicSetters; | |
| 290 } | |
| 291 | |
| 292 @override | |
| 293 Iterable<DartType> get isChecks => worldImpact.isChecks; | 227 Iterable<DartType> get isChecks => worldImpact.isChecks; |
| 294 | 228 |
| 295 @override | 229 @override |
| 296 Iterable<DartType> get onCatchTypes => worldImpact.onCatchTypes; | 230 Iterable<DartType> get onCatchTypes => worldImpact.onCatchTypes; |
| 297 | 231 |
| 298 _unsupported(String message) => throw new UnsupportedError(message); | 232 _unsupported(String message) => throw new UnsupportedError(message); |
| 299 | 233 |
| 300 void registerDynamicGetter(UniverseSelector selector) { | 234 void registerDynamicUse(UniverseSelector selector) { |
| 301 if (_dynamicGetters == null) { | 235 if (_dynamicUses == null) { |
| 302 _dynamicGetters = new Setlet<UniverseSelector>(); | 236 _dynamicUses = new Setlet<UniverseSelector>(); |
| 303 _dynamicGetters.addAll(worldImpact.dynamicGetters); | 237 _dynamicUses.addAll(worldImpact.dynamicUses); |
| 304 } | 238 } |
| 305 _dynamicGetters.add(selector); | 239 _dynamicUses.add(selector); |
| 306 } | |
| 307 | |
| 308 void registerDynamicInvocation(UniverseSelector selector) { | |
| 309 if (_dynamicInvocations == null) { | |
| 310 _dynamicInvocations = new Setlet<UniverseSelector>(); | |
| 311 _dynamicInvocations.addAll(worldImpact.dynamicInvocations); | |
| 312 } | |
| 313 _dynamicInvocations.add(selector); | |
| 314 } | |
| 315 | |
| 316 void registerDynamicSetter(UniverseSelector selector) { | |
| 317 if (_dynamicSetters == null) { | |
| 318 _dynamicSetters = new Setlet<UniverseSelector>(); | |
| 319 _dynamicSetters.addAll(worldImpact.dynamicSetters); | |
| 320 } | |
| 321 _dynamicSetters.add(selector); | |
| 322 } | 240 } |
| 323 | 241 |
| 324 void registerInstantiatedType(InterfaceType type) { | 242 void registerInstantiatedType(InterfaceType type) { |
| 325 if (_instantiatedTypes == null) { | 243 if (_instantiatedTypes == null) { |
| 326 _instantiatedTypes = new Setlet<InterfaceType>(); | 244 _instantiatedTypes = new Setlet<InterfaceType>(); |
| 327 _instantiatedTypes.addAll(worldImpact.instantiatedTypes); | 245 _instantiatedTypes.addAll(worldImpact.instantiatedTypes); |
| 328 } | 246 } |
| 329 _instantiatedTypes.add(type); | 247 _instantiatedTypes.add(type); |
| 330 } | 248 } |
| 331 | 249 |
| 332 @override | 250 @override |
| 333 Iterable<InterfaceType> get instantiatedTypes { | 251 Iterable<InterfaceType> get instantiatedTypes { |
| 334 return _instantiatedTypes != null | 252 return _instantiatedTypes != null |
| 335 ? _instantiatedTypes : worldImpact.instantiatedTypes; | 253 ? _instantiatedTypes : worldImpact.instantiatedTypes; |
| 336 } | 254 } |
| 337 | 255 |
| 338 @override | 256 @override |
| 339 Iterable<DartType> get typeLiterals { | 257 Iterable<DartType> get typeLiterals { |
| 340 return worldImpact.typeLiterals; | 258 return worldImpact.typeLiterals; |
| 341 } | 259 } |
| 342 | 260 |
| 343 void registerStaticUse(Element element) { | 261 void registerStaticUse(StaticUse staticUse) { |
| 344 if (_staticUses == null) { | 262 if (_staticUses == null) { |
| 345 _staticUses = new Setlet<Element>(); | 263 _staticUses = new Setlet<StaticUse>(); |
| 346 _staticUses.addAll(worldImpact.staticUses); | 264 _staticUses.addAll(worldImpact.staticUses); |
| 347 } | 265 } |
| 348 _staticUses.add(element); | 266 _staticUses.add(staticUse); |
| 349 } | 267 } |
| 350 | 268 |
| 351 @override | 269 @override |
| 352 Iterable<Element> get staticUses { | 270 Iterable<StaticUse> get staticUses { |
| 353 return _staticUses != null ? _staticUses : worldImpact.staticUses; | 271 return _staticUses != null ? _staticUses : worldImpact.staticUses; |
| 354 } | 272 } |
| 355 | 273 |
| 356 @override | 274 @override |
| 357 Iterable<LocalFunctionElement> get closures => worldImpact.closures; | 275 Iterable<LocalFunctionElement> get closures => worldImpact.closures; |
| 358 | 276 |
| 359 String toString() { | 277 String toString() { |
| 360 StringBuffer sb = new StringBuffer(); | 278 StringBuffer sb = new StringBuffer(); |
| 361 sb.write('TransformedWorldImpact($worldImpact)'); | 279 sb.write('TransformedWorldImpact($worldImpact)'); |
| 362 WorldImpact.printOn(sb, this); | 280 WorldImpact.printOn(sb, this); |
| 363 return sb.toString(); | 281 return sb.toString(); |
| 364 } | 282 } |
| 365 } | 283 } |
| OLD | NEW |