OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 part of js_backend; | 5 part of js_backend; |
6 | 6 |
7 /// [ConstantCompilerTask] for compilation of constants for the JavaScript | 7 /// [ConstantCompilerTask] for compilation of constants for the JavaScript |
8 /// backend. | 8 /// backend. |
9 /// | 9 /// |
10 /// Since this task needs to distinguish between frontend and backend constants | 10 /// Since this task needs to distinguish between frontend and backend constants |
11 /// the actual compilation of the constants is forwarded to a | 11 /// the actual compilation of the constants is forwarded to a |
12 /// [DartConstantCompiler] for the frontend interpretation of the constants and | 12 /// [DartConstantCompiler] for the frontend interpretation of the constants and |
13 /// to a [JavaScriptConstantCompiler] for the backend interpretation. | 13 /// to a [JavaScriptConstantCompiler] for the backend interpretation. |
14 class JavaScriptConstantTask extends ConstantCompilerTask { | 14 class JavaScriptConstantTask extends ConstantCompilerTask { |
15 DartConstantCompiler dartConstantCompiler; | 15 DartConstantCompiler dartConstantCompiler; |
16 JavaScriptConstantCompiler jsConstantCompiler; | 16 JavaScriptConstantCompiler jsConstantCompiler; |
17 | 17 |
18 JavaScriptConstantTask(Compiler compiler) | 18 JavaScriptConstantTask(Compiler compiler) |
19 : this.dartConstantCompiler = new DartConstantCompiler(compiler), | 19 : this.dartConstantCompiler = new DartConstantCompiler(compiler), |
20 this.jsConstantCompiler = | 20 this.jsConstantCompiler = |
21 new JavaScriptConstantCompiler(compiler), | 21 new JavaScriptConstantCompiler(compiler), |
22 super(compiler); | 22 super(compiler); |
23 | 23 |
24 String get name => 'ConstantHandler'; | 24 String get name => 'ConstantHandler'; |
25 | 25 |
| 26 @override |
| 27 ConstantSystem get constantSystem => dartConstantCompiler.constantSystem; |
| 28 |
| 29 @override |
| 30 ConstantValue getConstantValue(ConstantExpression expression) { |
| 31 return dartConstantCompiler.getConstantValue(expression); |
| 32 } |
| 33 |
| 34 @override |
| 35 ConstantValue getConstantValueForVariable(VariableElement element) { |
| 36 return dartConstantCompiler.getConstantValueForVariable(element); |
| 37 } |
| 38 |
| 39 @override |
26 ConstantExpression getConstantForVariable(VariableElement element) { | 40 ConstantExpression getConstantForVariable(VariableElement element) { |
27 return dartConstantCompiler.getConstantForVariable(element); | 41 return dartConstantCompiler.getConstantForVariable(element); |
28 } | 42 } |
29 | 43 |
| 44 @override |
30 ConstantExpression compileConstant(VariableElement element) { | 45 ConstantExpression compileConstant(VariableElement element) { |
31 return measure(() { | 46 return measure(() { |
32 ConstantExpression result = dartConstantCompiler.compileConstant(element); | 47 ConstantExpression result = dartConstantCompiler.compileConstant(element); |
33 jsConstantCompiler.compileConstant(element); | 48 jsConstantCompiler.compileConstant(element); |
34 return result; | 49 return result; |
35 }); | 50 }); |
36 } | 51 } |
37 | 52 |
38 void compileVariable(VariableElement element) { | 53 void compileVariable(VariableElement element) { |
39 measure(() { | 54 measure(() { |
(...skipping 16 matching lines...) Expand all Loading... |
56 ConstantExpression compileMetadata(MetadataAnnotation metadata, | 71 ConstantExpression compileMetadata(MetadataAnnotation metadata, |
57 Node node, | 72 Node node, |
58 TreeElements elements) { | 73 TreeElements elements) { |
59 return measure(() { | 74 return measure(() { |
60 ConstantExpression constant = | 75 ConstantExpression constant = |
61 dartConstantCompiler.compileMetadata(metadata, node, elements); | 76 dartConstantCompiler.compileMetadata(metadata, node, elements); |
62 jsConstantCompiler.compileMetadata(metadata, node, elements); | 77 jsConstantCompiler.compileMetadata(metadata, node, elements); |
63 return constant; | 78 return constant; |
64 }); | 79 }); |
65 } | 80 } |
| 81 |
| 82 // TODO(johnniwinther): Remove this when values are computed from the |
| 83 // expressions. |
| 84 @override |
| 85 void copyConstantValues(JavaScriptConstantTask task) { |
| 86 jsConstantCompiler.constantValueMap.addAll( |
| 87 task.jsConstantCompiler.constantValueMap); |
| 88 dartConstantCompiler.constantValueMap.addAll( |
| 89 task.dartConstantCompiler.constantValueMap); |
| 90 } |
66 } | 91 } |
67 | 92 |
68 /** | 93 /** |
69 * The [JavaScriptConstantCompiler] is used to keep track of compile-time | 94 * The [JavaScriptConstantCompiler] is used to keep track of compile-time |
70 * constants, initializations of global and static fields, and default values of | 95 * constants, initializations of global and static fields, and default values of |
71 * optional parameters for the JavaScript interpretation of constants. | 96 * optional parameters for the JavaScript interpretation of constants. |
72 */ | 97 */ |
73 class JavaScriptConstantCompiler extends ConstantCompilerBase | 98 class JavaScriptConstantCompiler extends ConstantCompilerBase |
74 implements BackendConstantEnvironment { | 99 implements BackendConstantEnvironment { |
75 | 100 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 } | 175 } |
151 | 176 |
152 List<ConstantValue> sorted = compiledConstants.toList(); | 177 List<ConstantValue> sorted = compiledConstants.toList(); |
153 if (preSortCompare != null) { | 178 if (preSortCompare != null) { |
154 sorted.sort(preSortCompare); | 179 sorted.sort(preSortCompare); |
155 } | 180 } |
156 sorted.forEach(addConstant); | 181 sorted.forEach(addConstant); |
157 return result; | 182 return result; |
158 } | 183 } |
159 | 184 |
160 ConstantExpression getInitialValueFor(VariableElement element) { | 185 ConstantValue getInitialValueFor(VariableElement element) { |
161 ConstantExpression initialValue = | 186 ConstantExpression initialValue = |
162 initialVariableValues[element.declaration]; | 187 initialVariableValues[element.declaration]; |
163 if (initialValue == null) { | 188 if (initialValue == null) { |
164 compiler.internalError(element, "No initial value for given element."); | 189 compiler.internalError(element, "No initial value for given element."); |
165 } | 190 } |
166 return initialValue; | 191 return getConstantValue(initialValue); |
167 } | 192 } |
168 | 193 |
169 ConstantExpression compileNode(Node node, TreeElements elements, | 194 ConstantExpression compileNode(Node node, TreeElements elements, |
170 {bool enforceConst: true}) { | 195 {bool enforceConst: true}) { |
171 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); | 196 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); |
172 } | 197 } |
173 | 198 |
174 ConstantExpression compileNodeWithDefinitions(Node node, | 199 ConstantExpression compileNodeWithDefinitions(Node node, |
175 TreeElements definitions, | 200 TreeElements definitions, |
176 {bool isConst: true}) { | 201 {bool isConst: true}) { |
177 ConstantExpression constant = nodeConstantMap[node]; | 202 ConstantExpression constant = nodeConstantMap[node]; |
178 if (constant != null) { | 203 if (constant != null) { |
179 return constant; | 204 return constant; |
180 } | 205 } |
181 constant = | 206 constant = |
182 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | 207 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); |
183 if (constant != null) { | 208 if (constant != null) { |
184 nodeConstantMap[node] = constant; | 209 nodeConstantMap[node] = constant; |
185 } | 210 } |
186 return constant; | 211 return constant; |
187 } | 212 } |
188 | 213 |
| 214 ConstantValue getConstantValueForNode(Node node, TreeElements definitions) { |
| 215 return getConstantValue(getConstantForNode(node, definitions)); |
| 216 } |
| 217 |
189 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { | 218 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { |
190 ConstantExpression constant = nodeConstantMap[node]; | 219 ConstantExpression constant = nodeConstantMap[node]; |
191 if (constant != null) { | 220 if (constant != null) { |
192 return constant; | 221 return constant; |
193 } | 222 } |
194 return definitions.getConstant(node); | 223 return definitions.getConstant(node); |
195 } | 224 } |
196 | 225 |
197 ConstantExpression getConstantForMetadata(MetadataAnnotation metadata) { | 226 ConstantValue getConstantValueForMetadata(MetadataAnnotation metadata) { |
198 return metadataConstantMap[metadata]; | 227 return getConstantValue(metadataConstantMap[metadata]); |
199 } | 228 } |
200 | 229 |
201 ConstantExpression compileMetadata(MetadataAnnotation metadata, | 230 ConstantExpression compileMetadata( |
202 Node node, | 231 MetadataAnnotation metadata, |
203 TreeElements elements) { | 232 Node node, |
| 233 TreeElements elements) { |
204 ConstantExpression constant = | 234 ConstantExpression constant = |
205 super.compileMetadata(metadata, node, elements); | 235 super.compileMetadata(metadata, node, elements); |
206 metadataConstantMap[metadata] = constant; | 236 metadataConstantMap[metadata] = constant; |
207 return constant; | 237 return constant; |
208 } | 238 } |
209 | 239 |
210 void forgetElement(Element element) { | 240 void forgetElement(Element element) { |
211 super.forgetElement(element); | 241 super.forgetElement(element); |
212 const ForgetConstantElementVisitor().visit(element, this); | 242 const ForgetConstantElementVisitor().visit(element, this); |
213 if (element is AstElement && element.hasNode) { | 243 if (element is AstElement && element.hasNode) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // TODO(ahe): This doesn't belong here. Rename this class and generalize. | 280 // TODO(ahe): This doesn't belong here. Rename this class and generalize. |
251 var closureClassMap = | 281 var closureClassMap = |
252 constants.compiler.closureToClassMapper.closureMappingCache | 282 constants.compiler.closureToClassMapper.closureMappingCache |
253 .remove(node); | 283 .remove(node); |
254 if (closureClassMap != null) { | 284 if (closureClassMap != null) { |
255 closureClassMap.removeMyselfFrom( | 285 closureClassMap.removeMyselfFrom( |
256 constants.compiler.enqueuer.codegen.universe); | 286 constants.compiler.enqueuer.codegen.universe); |
257 } | 287 } |
258 } | 288 } |
259 } | 289 } |
OLD | NEW |