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; | |
11 import '../constants/values.dart' show | 10 import '../constants/values.dart' show |
12 ConstantValue, | 11 ConstantValue, |
13 ConstructedConstantValue, | 12 ConstructedConstantValue, |
14 ListConstantValue, | 13 ListConstantValue, |
15 NullConstantValue, | 14 NullConstantValue, |
16 TypeConstantValue; | 15 TypeConstantValue; |
17 import '../dart_types.dart' show DartType; | 16 import '../dart_types.dart' show DartType; |
18 import '../elements/elements.dart' show | 17 import '../elements/elements.dart' show Elements, Element, ClassElement, |
19 ClassElement, | 18 FieldElement, FunctionElement, FunctionSignature; |
20 Element, | |
21 Elements, | |
22 FieldElement, | |
23 FunctionElement, | |
24 FunctionSignature, | |
25 LibraryElement, | |
26 VariableElement; | |
27 import '../enqueue.dart' show Enqueuer; | 19 import '../enqueue.dart' show Enqueuer; |
28 import 'js_backend.dart' show JavaScriptBackend; | 20 import 'js_backend.dart' show JavaScriptBackend; |
29 import '../dart_types.dart' show DynamicType, InterfaceType; | 21 import '../dart_types.dart' show DynamicType, InterfaceType; |
30 | 22 |
31 /// An analysis and optimization to remove unused entries from a `LookupMap`. | 23 /// An analysis and optimization to remove unused entries from a `LookupMap`. |
32 /// | 24 /// |
33 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are | 25 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are |
34 /// simple maps that contain constant expressions as keys, and that only support | 26 /// simple maps that contain constant expressions as keys, and that only support |
35 /// the lookup operation. | 27 /// the lookup operation. |
36 /// | 28 /// |
(...skipping 27 matching lines...) Expand all Loading... |
64 // TODO(sigmund): add support for const expressions, currently this | 56 // TODO(sigmund): add support for const expressions, currently this |
65 // implementation only supports Type literals. To support const expressions we | 57 // implementation only supports Type literals. To support const expressions we |
66 // need to change some of the invariants below (e.g. we can no longer use the | 58 // need to change some of the invariants below (e.g. we can no longer use the |
67 // ClassElement of a type to refer to keys we need to discover). | 59 // ClassElement of a type to refer to keys we need to discover). |
68 // TODO(sigmund): detect uses of mirrors | 60 // TODO(sigmund): detect uses of mirrors |
69 class LookupMapAnalysis { | 61 class LookupMapAnalysis { |
70 /// Reference to [JavaScriptBackend] to be able to enqueue work when we | 62 /// Reference to [JavaScriptBackend] to be able to enqueue work when we |
71 /// discover that a key in a map is potentially used. | 63 /// discover that a key in a map is potentially used. |
72 final JavaScriptBackend backend; | 64 final JavaScriptBackend backend; |
73 | 65 |
74 /// The resolved [VariableElement] associated with the top-level `_version`. | |
75 VariableElement lookupMapVersionVariable; | |
76 | |
77 /// The resolved [LibraryElement] associated with | |
78 /// `package:lookup_map/lookup_map.dart`. | |
79 LibraryElement lookupMapLibrary; | |
80 | |
81 /// The resolved [ClassElement] associated with `LookupMap`. | 66 /// The resolved [ClassElement] associated with `LookupMap`. |
82 ClassElement typeLookupMapClass; | 67 ClassElement typeLookupMapClass; |
83 | 68 |
84 /// The resolved [FieldElement] for `LookupMap._entries`. | 69 /// The resolved [FieldElement] for `LookupMap._entries`. |
85 FieldElement entriesField; | 70 FieldElement entriesField; |
86 | 71 |
87 /// The resolved [FieldElement] for `LookupMap._key`. | 72 /// The resolved [FieldElement] for `LookupMap._key`. |
88 FieldElement keyField; | 73 FieldElement keyField; |
89 | 74 |
90 /// The resolved [FieldElement] for `LookupMap._value`. | 75 /// The resolved [FieldElement] for `LookupMap._value`. |
(...skipping 18 matching lines...) Expand all Loading... |
109 /// we could allow const instances of these types. However, we internally use | 94 /// we could allow const instances of these types. However, we internally use |
110 /// a hash map within lookup-maps today, so we need this restriction. | 95 /// a hash map within lookup-maps today, so we need this restriction. |
111 final _typesWithEquals = <ClassElement, bool>{}; | 96 final _typesWithEquals = <ClassElement, bool>{}; |
112 | 97 |
113 /// Pending work to do if we discover that a new key is in use. For each key | 98 /// Pending work to do if we discover that a new key is in use. For each key |
114 /// that we haven't seen, we record the list of lookup-maps that contain an | 99 /// that we haven't seen, we record the list of lookup-maps that contain an |
115 /// entry with that key. | 100 /// entry with that key. |
116 final _pending = <ConstantValue, List<_LookupMapInfo>>{}; | 101 final _pending = <ConstantValue, List<_LookupMapInfo>>{}; |
117 | 102 |
118 /// Whether the backend is currently processing the codegen queue. | 103 /// Whether the backend is currently processing the codegen queue. |
119 bool _inCodegen = false; | 104 // TODO(sigmund): is there a better way to do this. Do we need to plumb the |
| 105 // enqueuer on each callback? |
| 106 bool get _inCodegen => backend.compiler.phase == Compiler.PHASE_COMPILING; |
120 | 107 |
121 LookupMapAnalysis(this.backend); | 108 LookupMapAnalysis(this.backend); |
122 | 109 |
123 /// Whether this analysis and optimization is enabled. | 110 /// Whether this analysis and optimization is enabled. |
124 bool get _isEnabled { | 111 bool get _isEnabled { |
125 // `lookupMap==off` kept here to make it easy to test disabling this feature | 112 // `lookupMap==off` kept here to make it easy to test disabling this feature |
126 if (const String.fromEnvironment('lookupMap') == 'off') return false; | 113 if (const String.fromEnvironment('lookupMap') == 'off') return false; |
127 return typeLookupMapClass != null; | 114 return typeLookupMapClass != null; |
128 } | 115 } |
129 | 116 |
130 /// Initializes this analysis by providing the resolved library. This is | 117 /// Initializes this analysis by providing the resolver information of |
131 /// invoked during resolution when the `lookup_map` library is discovered. | 118 /// `LookupMap`. |
132 void init(LibraryElement library) { | 119 void initRuntimeClass(ClassElement cls) { |
133 lookupMapLibrary = library; | |
134 // We will enable the lookupMapAnalysis as long as we get a known version of | |
135 // the lookup_map package. We otherwise produce a warning. | |
136 lookupMapVersionVariable = library.implementation.findLocal('_version'); | |
137 if (lookupMapVersionVariable == null) { | |
138 backend.compiler.reportInfo(library, | |
139 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); | |
140 } else { | |
141 backend.compiler.enqueuer.resolution.addToWorkList( | |
142 lookupMapVersionVariable); | |
143 } | |
144 } | |
145 | |
146 /// Checks if the version of lookup_map is valid, and if so, enable this | |
147 /// analysis during codegen. | |
148 void onCodegenStart() { | |
149 _inCodegen = true; | |
150 // At this point, the lookupMapVersionVariable should be resolved and it's | |
151 // constant value should be available. | |
152 ConstantValue value = | |
153 backend.constants.getConstantValueForVariable(lookupMapVersionVariable); | |
154 if (value == null) { | |
155 backend.compiler.reportInfo(lookupMapVersionVariable, | |
156 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); | |
157 return; | |
158 } | |
159 | |
160 // TODO(sigmund): add proper version resolution using the pub_semver package | |
161 // when we introduce the next version. | |
162 String version = value.primitiveValue.slowToString(); | |
163 if (version != '0.0.1') { | |
164 backend.compiler.reportInfo(lookupMapVersionVariable, | |
165 MessageKind.UNRECOGNIZED_VERSION_OF_LOOKUP_MAP); | |
166 return; | |
167 } | |
168 | |
169 ClassElement cls = lookupMapLibrary.findLocal('LookupMap'); | |
170 cls.computeType(backend.compiler); | 120 cls.computeType(backend.compiler); |
171 entriesField = cls.lookupMember('_entries'); | 121 entriesField = cls.lookupMember('_entries'); |
172 keyField = cls.lookupMember('_key'); | 122 keyField = cls.lookupMember('_key'); |
173 valueField = cls.lookupMember('_value'); | 123 valueField = cls.lookupMember('_value'); |
174 // TODO(sigmund): Maybe inline nested maps to make the output code smaller? | 124 // TODO(sigmund): Maybe inline nested maps make the output code smaller? |
175 typeLookupMapClass = cls; | 125 typeLookupMapClass = cls; |
176 } | 126 } |
177 | 127 |
178 /// Whether [constant] is an instance of a `LookupMap`. | 128 /// Whether [constant] is an instance of a `LookupMap`. |
179 bool isLookupMap(ConstantValue constant) => | 129 bool isLookupMap(ConstantValue constant) => |
180 _isEnabled && | 130 _isEnabled && |
181 constant is ConstructedConstantValue && | 131 constant is ConstructedConstantValue && |
182 constant.type.asRaw().element.isSubclassOf(typeLookupMapClass); | 132 constant.type.asRaw().element.isSubclassOf(typeLookupMapClass); |
183 | 133 |
184 /// Registers an instance of a lookup-map with the analysis. | 134 /// Registers an instance of a lookup-map with the analysis. |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if (keyValuePairs.length == 2) { | 370 if (keyValuePairs.length == 2) { |
421 original.fields[analysis.keyField] = keyValuePairs[0]; | 371 original.fields[analysis.keyField] = keyValuePairs[0]; |
422 original.fields[analysis.valueField] = keyValuePairs[1]; | 372 original.fields[analysis.valueField] = keyValuePairs[1]; |
423 } | 373 } |
424 } else { | 374 } else { |
425 original.fields[analysis.entriesField] = | 375 original.fields[analysis.entriesField] = |
426 new ListConstantValue(listType, keyValuePairs); | 376 new ListConstantValue(listType, keyValuePairs); |
427 } | 377 } |
428 } | 378 } |
429 } | 379 } |
OLD | NEW |