OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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.compile_time_constant_evaluator; | 5 library dart2js.compile_time_constant_evaluator; |
6 | 6 |
7 import 'common.dart'; | 7 import 'common.dart'; |
8 import 'common/resolution.dart' show Resolution; | 8 import 'common/resolution.dart' show Resolution; |
9 import 'common/tasks.dart' show CompilerTask, Measurer; | 9 import 'common/tasks.dart' show CompilerTask, Measurer; |
10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 158 |
159 ConstantCompilerBase(this.compiler, this.constantSystem); | 159 ConstantCompilerBase(this.compiler, this.constantSystem); |
160 | 160 |
161 DiagnosticReporter get reporter => compiler.reporter; | 161 DiagnosticReporter get reporter => compiler.reporter; |
162 | 162 |
163 CoreTypes get coreTypes => compiler.coreTypes; | 163 CoreTypes get coreTypes => compiler.coreTypes; |
164 | 164 |
165 @override | 165 @override |
166 @deprecated | 166 @deprecated |
167 ConstantValue getConstantValueForVariable(VariableElement element) { | 167 ConstantValue getConstantValueForVariable(VariableElement element) { |
168 return getConstantValue(initialVariableValues[element.declaration]); | 168 ConstantExpression constant = initialVariableValues[element.declaration]; |
| 169 // TODO(johnniwinther): Support eager evaluation of the constant. |
| 170 return constant != null ? getConstantValue(constant) : null; |
169 } | 171 } |
170 | 172 |
171 ConstantExpression compileConstant(VariableElement element) { | 173 ConstantExpression compileConstant(VariableElement element) { |
172 return internalCompileVariable(element, true, true); | 174 return internalCompileVariable(element, true, true); |
173 } | 175 } |
174 | 176 |
175 @override | 177 @override |
176 void evaluate(ConstantExpression constant) { | 178 void evaluate(ConstantExpression constant) { |
177 constantValueMap.putIfAbsent(constant, () { | 179 constantValueMap.putIfAbsent(constant, () { |
178 return constant.evaluate( | 180 return constant.evaluate( |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 cacheConstantValue(constant.expression, constant.value); | 315 cacheConstantValue(constant.expression, constant.value); |
314 return constant.expression; | 316 return constant.expression; |
315 } | 317 } |
316 return null; | 318 return null; |
317 } | 319 } |
318 | 320 |
319 bool hasConstantValue(ConstantExpression expression) { | 321 bool hasConstantValue(ConstantExpression expression) { |
320 return constantValueMap.containsKey(expression); | 322 return constantValueMap.containsKey(expression); |
321 } | 323 } |
322 | 324 |
| 325 @override |
323 ConstantValue getConstantValue(ConstantExpression expression) { | 326 ConstantValue getConstantValue(ConstantExpression expression) { |
324 return constantValueMap[expression]; | 327 assert(invariant(CURRENT_ELEMENT_SPANNABLE, expression != null, |
| 328 message: "ConstantExpression is null in getConstantValue.")); |
| 329 // TODO(johnniwinther): ensure expressions have been evaluated at this |
| 330 // point. This can't be enabled today due to dartbug.com/26406. |
| 331 if (compiler.serialization.supportsDeserialization) { |
| 332 evaluate(expression); |
| 333 } |
| 334 ConstantValue value = constantValueMap[expression]; |
| 335 if (value == null && |
| 336 expression != null && |
| 337 expression.kind == ConstantExpressionKind.ERRONEOUS) { |
| 338 // TODO(johnniwinther): When the Dart constant system sees a constant |
| 339 // expression as erroneous but the JavaScript constant system finds it ok |
| 340 // we have store a constant value for the erroneous constant expression. |
| 341 // Ensure the computed constant expressions are always the same; that only |
| 342 // the constant values may be different. |
| 343 value = new NullConstantValue(); |
| 344 } |
| 345 return value; |
325 } | 346 } |
326 | 347 |
327 ConstantExpression compileNode(Node node, TreeElements elements, | 348 ConstantExpression compileNode(Node node, TreeElements elements, |
328 {bool enforceConst: true}) { | 349 {bool enforceConst: true}) { |
329 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); | 350 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); |
330 } | 351 } |
331 | 352 |
332 ConstantExpression compileMetadata( | 353 ConstantExpression compileMetadata( |
333 MetadataAnnotation metadata, Node node, TreeElements elements) { | 354 MetadataAnnotation metadata, Node node, TreeElements elements) { |
334 return compileNodeWithDefinitions(node, elements); | 355 return compileNodeWithDefinitions(node, elements); |
(...skipping 17 matching lines...) Expand all Loading... |
352 : super(compiler, const DartConstantSystem()); | 373 : super(compiler, const DartConstantSystem()); |
353 | 374 |
354 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { | 375 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { |
355 return definitions.getConstant(node); | 376 return definitions.getConstant(node); |
356 } | 377 } |
357 | 378 |
358 ConstantExpression compileNodeWithDefinitions( | 379 ConstantExpression compileNodeWithDefinitions( |
359 Node node, TreeElements definitions, | 380 Node node, TreeElements definitions, |
360 {bool isConst: true}) { | 381 {bool isConst: true}) { |
361 ConstantExpression constant = definitions.getConstant(node); | 382 ConstantExpression constant = definitions.getConstant(node); |
362 if (constant != null && getConstantValue(constant) != null) { | 383 if (constant != null && hasConstantValue(constant)) { |
363 return constant; | 384 return constant; |
364 } | 385 } |
365 constant = | 386 constant = |
366 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | 387 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); |
367 if (constant != null) { | 388 if (constant != null) { |
368 definitions.setConstant(node, constant); | 389 definitions.setConstant(node, constant); |
369 } | 390 } |
370 return constant; | 391 return constant; |
371 } | 392 } |
372 } | 393 } |
(...skipping 995 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 class _CompilerEnvironment implements Environment { | 1389 class _CompilerEnvironment implements Environment { |
1369 final Compiler compiler; | 1390 final Compiler compiler; |
1370 | 1391 |
1371 _CompilerEnvironment(this.compiler); | 1392 _CompilerEnvironment(this.compiler); |
1372 | 1393 |
1373 @override | 1394 @override |
1374 String readFromEnvironment(String name) { | 1395 String readFromEnvironment(String name) { |
1375 return compiler.fromEnvironment(name); | 1396 return compiler.fromEnvironment(name); |
1376 } | 1397 } |
1377 } | 1398 } |
OLD | NEW |