| 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 /// Analysis to determine how to generate code for `LookupMap`s. | 5 /// Analysis to determine how to generate code for `LookupMap`s. |
| 6 library compiler.src.js_backend.lookup_map_analysis; | 6 library compiler.src.js_backend.lookup_map_analysis; |
| 7 | 7 |
| 8 import '../common/registry.dart' show Registry; | 8 import '../common/registry.dart' show Registry; |
| 9 import '../compiler.dart' show Compiler; | 9 import '../compiler.dart' show Compiler; |
| 10 import '../diagnostics/messages.dart' show MessageKind; | 10 import '../diagnostics/messages.dart' show MessageKind; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 Element, | 21 Element, |
| 22 Elements, | 22 Elements, |
| 23 FieldElement, | 23 FieldElement, |
| 24 FunctionElement, | 24 FunctionElement, |
| 25 FunctionSignature, | 25 FunctionSignature, |
| 26 LibraryElement, | 26 LibraryElement, |
| 27 VariableElement; | 27 VariableElement; |
| 28 import '../enqueue.dart' show Enqueuer; | 28 import '../enqueue.dart' show Enqueuer; |
| 29 import 'js_backend.dart' show JavaScriptBackend; | 29 import 'js_backend.dart' show JavaScriptBackend; |
| 30 import '../dart_types.dart' show DynamicType, InterfaceType; | 30 import '../dart_types.dart' show DynamicType, InterfaceType; |
| 31 import 'package:pub_semver/pub_semver.dart'; |
| 31 | 32 |
| 32 /// An analysis and optimization to remove unused entries from a `LookupMap`. | 33 /// An analysis and optimization to remove unused entries from a `LookupMap`. |
| 33 /// | 34 /// |
| 34 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are | 35 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are |
| 35 /// simple maps that contain constant expressions as keys, and that only support | 36 /// simple maps that contain constant expressions as keys, and that only support |
| 36 /// the lookup operation. | 37 /// the lookup operation. |
| 37 /// | 38 /// |
| 38 /// This analysis and optimization will tree-shake the contents of the maps by | 39 /// This analysis and optimization will tree-shake the contents of the maps by |
| 39 /// looking at the program and finding which keys are clearly unused. Not all | 40 /// looking at the program and finding which keys are clearly unused. Not all |
| 40 /// constants can be approximated statically, so this optimization is limited to | 41 /// constants can be approximated statically, so this optimization is limited to |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 StringConstantValue value = | 156 StringConstantValue value = |
| 156 backend.constants.getConstantValueForVariable(lookupMapVersionVariable); | 157 backend.constants.getConstantValueForVariable(lookupMapVersionVariable); |
| 157 if (value == null) { | 158 if (value == null) { |
| 158 backend.compiler.reportInfo(lookupMapVersionVariable, | 159 backend.compiler.reportInfo(lookupMapVersionVariable, |
| 159 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); | 160 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); |
| 160 return; | 161 return; |
| 161 } | 162 } |
| 162 | 163 |
| 163 // TODO(sigmund): add proper version resolution using the pub_semver package | 164 // TODO(sigmund): add proper version resolution using the pub_semver package |
| 164 // when we introduce the next version. | 165 // when we introduce the next version. |
| 165 String version = value.primitiveValue.slowToString(); | 166 Version version; |
| 166 if (version != '0.0.1') { | 167 try { |
| 168 version = new Version.parse(value.primitiveValue.slowToString()); |
| 169 } catch (e) {} |
| 170 |
| 171 if (version == null || !_validLookupMapVersionConstraint.allows(version)) { |
| 167 backend.compiler.reportInfo(lookupMapVersionVariable, | 172 backend.compiler.reportInfo(lookupMapVersionVariable, |
| 168 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); | 173 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); |
| 169 return; | 174 return; |
| 170 } | 175 } |
| 171 | 176 |
| 172 ClassElement cls = lookupMapLibrary.findLocal('LookupMap'); | 177 ClassElement cls = lookupMapLibrary.findLocal('LookupMap'); |
| 173 cls.computeType(backend.compiler); | 178 cls.computeType(backend.compiler); |
| 174 entriesField = cls.lookupMember('_entries'); | 179 entriesField = cls.lookupMember('_entries'); |
| 175 keyField = cls.lookupMember('_key'); | 180 keyField = cls.lookupMember('_key'); |
| 176 valueField = cls.lookupMember('_value'); | 181 valueField = cls.lookupMember('_value'); |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 if (keyValuePairs.length == 2) { | 428 if (keyValuePairs.length == 2) { |
| 424 original.fields[analysis.keyField] = keyValuePairs[0]; | 429 original.fields[analysis.keyField] = keyValuePairs[0]; |
| 425 original.fields[analysis.valueField] = keyValuePairs[1]; | 430 original.fields[analysis.valueField] = keyValuePairs[1]; |
| 426 } | 431 } |
| 427 } else { | 432 } else { |
| 428 original.fields[analysis.entriesField] = | 433 original.fields[analysis.entriesField] = |
| 429 new ListConstantValue(listType, keyValuePairs); | 434 new ListConstantValue(listType, keyValuePairs); |
| 430 } | 435 } |
| 431 } | 436 } |
| 432 } | 437 } |
| 438 |
| 439 final _validLookupMapVersionConstraint = |
| 440 new VersionConstraint.parse('^0.0.1'); |
| OLD | NEW |