| 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 'constant_system_dart.dart'; | 7 import 'constant_system_dart.dart'; |
| 8 import 'constants/constant_system.dart'; | 8 import 'constants/constant_system.dart'; |
| 9 import 'constants/expressions.dart'; | 9 import 'constants/expressions.dart'; |
| 10 import 'constants/values.dart'; | 10 import 'constants/values.dart'; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 /// Compiles the compile-time constant for the value [metadata], or reports an | 70 /// Compiles the compile-time constant for the value [metadata], or reports an |
| 71 /// error if the value is not a compile-time constant. | 71 /// error if the value is not a compile-time constant. |
| 72 /// | 72 /// |
| 73 /// Depending on implementation, the constant compiler might also compute | 73 /// Depending on implementation, the constant compiler might also compute |
| 74 /// the compile-time constant for the backend interpretation of constants. | 74 /// the compile-time constant for the backend interpretation of constants. |
| 75 /// | 75 /// |
| 76 /// The returned constant is always of the frontend interpretation. | 76 /// The returned constant is always of the frontend interpretation. |
| 77 ConstantExpression compileMetadata(MetadataAnnotation metadata, | 77 ConstantExpression compileMetadata(MetadataAnnotation metadata, |
| 78 Node node, | 78 Node node, |
| 79 TreeElements elements); | 79 TreeElements elements); |
| 80 |
| 81 /// Evaluates [constant] and caches the result. |
| 82 // TODO(johnniwinther): Remove when all constants are evaluated. |
| 83 void evaluate(ConstantExpression constant); |
| 80 } | 84 } |
| 81 | 85 |
| 82 /// A [BackendConstantEnvironment] provides access to constants needed for | 86 /// A [BackendConstantEnvironment] provides access to constants needed for |
| 83 /// backend implementation. | 87 /// backend implementation. |
| 84 abstract class BackendConstantEnvironment extends ConstantEnvironment { | 88 abstract class BackendConstantEnvironment extends ConstantEnvironment { |
| 85 /// Returns the compile-time constant value associated with [node]. | 89 /// Returns the compile-time constant value associated with [node]. |
| 86 /// | 90 /// |
| 87 /// Depending on implementation, the constant might be stored in [elements]. | 91 /// Depending on implementation, the constant might be stored in [elements]. |
| 88 ConstantValue getConstantValueForNode(Node node, TreeElements elements); | 92 ConstantValue getConstantValueForNode(Node node, TreeElements elements); |
| 89 | 93 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 154 |
| 151 @override | 155 @override |
| 152 ConstantExpression getConstantForVariable(VariableElement element) { | 156 ConstantExpression getConstantForVariable(VariableElement element) { |
| 153 return initialVariableValues[element.declaration]; | 157 return initialVariableValues[element.declaration]; |
| 154 } | 158 } |
| 155 | 159 |
| 156 ConstantExpression compileConstant(VariableElement element) { | 160 ConstantExpression compileConstant(VariableElement element) { |
| 157 return compileVariable(element, isConst: true); | 161 return compileVariable(element, isConst: true); |
| 158 } | 162 } |
| 159 | 163 |
| 164 @override |
| 165 void evaluate(ConstantExpression constant) { |
| 166 constantValueMap.putIfAbsent(constant, () { |
| 167 return constant.evaluate( |
| 168 new _CompilerEnvironment(compiler), |
| 169 constantSystem); |
| 170 }); |
| 171 } |
| 172 |
| 160 ConstantExpression compileVariable(VariableElement element, | 173 ConstantExpression compileVariable(VariableElement element, |
| 161 {bool isConst: false}) { | 174 {bool isConst: false}) { |
| 162 | 175 |
| 163 if (initialVariableValues.containsKey(element.declaration)) { | 176 if (initialVariableValues.containsKey(element.declaration)) { |
| 164 ConstantExpression result = initialVariableValues[element.declaration]; | 177 ConstantExpression result = initialVariableValues[element.declaration]; |
| 165 return result; | 178 return result; |
| 166 } | 179 } |
| 167 AstElement currentElement = element.analyzableElement; | 180 AstElement currentElement = element.analyzableElement; |
| 168 return compiler.withCurrentElement(currentElement, () { | 181 return compiler.withCurrentElement(currentElement, () { |
| 169 // TODO(johnniwinther): Avoid this eager analysis. | 182 // TODO(johnniwinther): Avoid this eager analysis. |
| (...skipping 1165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 // TODO(johnniwinther): Return a [NonConstantValue] instead. | 1348 // TODO(johnniwinther): Return a [NonConstantValue] instead. |
| 1336 new ErroneousConstantExpression(), new NullConstantValue()); | 1349 new ErroneousConstantExpression(), new NullConstantValue()); |
| 1337 } | 1350 } |
| 1338 | 1351 |
| 1339 // TODO(johnniwinther): Avoid the need for this hack. | 1352 // TODO(johnniwinther): Avoid the need for this hack. |
| 1340 TreeElements _analyzeElementEagerly(Compiler compiler, AstElement element) { | 1353 TreeElements _analyzeElementEagerly(Compiler compiler, AstElement element) { |
| 1341 WorldImpact worldImpact = compiler.analyzeElement(element.declaration); | 1354 WorldImpact worldImpact = compiler.analyzeElement(element.declaration); |
| 1342 compiler.enqueuer.resolution.applyImpact(element.declaration, worldImpact); | 1355 compiler.enqueuer.resolution.applyImpact(element.declaration, worldImpact); |
| 1343 return element.resolvedAst.elements; | 1356 return element.resolvedAst.elements; |
| 1344 } | 1357 } |
| 1358 |
| 1359 class _CompilerEnvironment implements Environment { |
| 1360 final Compiler compiler; |
| 1361 |
| 1362 _CompilerEnvironment(this.compiler); |
| 1363 |
| 1364 @override |
| 1365 String readFromEnvironment(String name) { |
| 1366 return compiler.fromEnvironment(name); |
| 1367 } |
| 1368 } |
| OLD | NEW |