| 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 import '../compile_time_constants.dart'; | 5 import '../compile_time_constants.dart'; |
| 6 import '../compiler.dart' show Compiler; | 6 import '../compiler.dart' show Compiler; |
| 7 import '../constants/constant_system.dart'; | 7 import '../constants/constant_system.dart'; |
| 8 import '../constants/expressions.dart'; | 8 import '../constants/expressions.dart'; |
| 9 import '../constants/values.dart'; | 9 import '../constants/values.dart'; |
| 10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * The [JavaScriptConstantCompiler] is used to keep track of compile-time | 110 * The [JavaScriptConstantCompiler] is used to keep track of compile-time |
| 111 * constants, initializations of global and static fields, and default values of | 111 * constants, initializations of global and static fields, and default values of |
| 112 * optional parameters for the JavaScript interpretation of constants. | 112 * optional parameters for the JavaScript interpretation of constants. |
| 113 */ | 113 */ |
| 114 class JavaScriptConstantCompiler extends ConstantCompilerBase | 114 class JavaScriptConstantCompiler extends ConstantCompilerBase |
| 115 implements BackendConstantEnvironment { | 115 implements BackendConstantEnvironment { |
| 116 /** Set of all registered compiled constants. */ | |
| 117 final Set<ConstantValue> compiledConstants = new Set<ConstantValue>(); | |
| 118 | |
| 119 // TODO(johnniwinther): Move this to the backend constant handler. | 116 // TODO(johnniwinther): Move this to the backend constant handler. |
| 120 /** Caches the statics where the initial value cannot be eagerly compiled. */ | 117 /** Caches the statics where the initial value cannot be eagerly compiled. */ |
| 121 final Set<FieldElement> lazyStatics = new Set<FieldElement>(); | 118 final Set<FieldElement> lazyStatics = new Set<FieldElement>(); |
| 122 | 119 |
| 123 // Constants computed for constant expressions. | 120 // Constants computed for constant expressions. |
| 124 final Map<Node, ConstantExpression> nodeConstantMap = | 121 final Map<Node, ConstantExpression> nodeConstantMap = |
| 125 new Map<Node, ConstantExpression>(); | 122 new Map<Node, ConstantExpression>(); |
| 126 | 123 |
| 127 // Constants computed for metadata. | 124 // Constants computed for metadata. |
| 128 // TODO(johnniwinther): Remove this when no longer used by | 125 // TODO(johnniwinther): Remove this when no longer used by |
| (...skipping 17 matching lines...) Expand all Loading... |
| 146 registerLazyStatic(element); | 143 registerLazyStatic(element); |
| 147 } | 144 } |
| 148 return value; | 145 return value; |
| 149 } | 146 } |
| 150 | 147 |
| 151 @override | 148 @override |
| 152 void registerLazyStatic(FieldElement element) { | 149 void registerLazyStatic(FieldElement element) { |
| 153 lazyStatics.add(element); | 150 lazyStatics.add(element); |
| 154 } | 151 } |
| 155 | 152 |
| 156 void addCompileTimeConstantForEmission(ConstantValue constant) { | |
| 157 compiledConstants.add(constant); | |
| 158 } | |
| 159 | |
| 160 List<FieldElement> getLazilyInitializedFieldsForEmission() { | 153 List<FieldElement> getLazilyInitializedFieldsForEmission() { |
| 161 return new List<FieldElement>.from(lazyStatics); | 154 return new List<FieldElement>.from(lazyStatics); |
| 162 } | 155 } |
| 163 | 156 |
| 164 /** | |
| 165 * Returns a list of constants topologically sorted so that dependencies | |
| 166 * appear before the dependent constant. [preSortCompare] is a comparator | |
| 167 * function that gives the constants a consistent order prior to the | |
| 168 * topological sort which gives the constants an ordering that is less | |
| 169 * sensitive to perturbations in the source code. | |
| 170 */ | |
| 171 List<ConstantValue> getConstantsForEmission([preSortCompare]) { | |
| 172 // We must emit dependencies before their uses. | |
| 173 Set<ConstantValue> seenConstants = new Set<ConstantValue>(); | |
| 174 List<ConstantValue> result = new List<ConstantValue>(); | |
| 175 | |
| 176 void addConstant(ConstantValue constant) { | |
| 177 if (!seenConstants.contains(constant)) { | |
| 178 constant.getDependencies().forEach(addConstant); | |
| 179 assert(!seenConstants.contains(constant)); | |
| 180 result.add(constant); | |
| 181 seenConstants.add(constant); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 List<ConstantValue> sorted = compiledConstants.toList(); | |
| 186 if (preSortCompare != null) { | |
| 187 sorted.sort(preSortCompare); | |
| 188 } | |
| 189 sorted.forEach(addConstant); | |
| 190 return result; | |
| 191 } | |
| 192 | |
| 193 ConstantExpression compileNode(Node node, TreeElements elements, | 157 ConstantExpression compileNode(Node node, TreeElements elements, |
| 194 {bool enforceConst: true}) { | 158 {bool enforceConst: true}) { |
| 195 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); | 159 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); |
| 196 } | 160 } |
| 197 | 161 |
| 198 ConstantExpression compileNodeWithDefinitions( | 162 ConstantExpression compileNodeWithDefinitions( |
| 199 Node node, TreeElements definitions, | 163 Node node, TreeElements definitions, |
| 200 {bool isConst: true}) { | 164 {bool isConst: true}) { |
| 201 ConstantExpression constant = nodeConstantMap[node]; | 165 ConstantExpression constant = nodeConstantMap[node]; |
| 202 if (constant != null && getConstantValue(constant) != null) { | 166 if (constant != null && getConstantValue(constant) != null) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 class ForgetConstantNodeVisitor extends Visitor { | 225 class ForgetConstantNodeVisitor extends Visitor { |
| 262 final JavaScriptConstantCompiler constants; | 226 final JavaScriptConstantCompiler constants; |
| 263 | 227 |
| 264 ForgetConstantNodeVisitor(this.constants); | 228 ForgetConstantNodeVisitor(this.constants); |
| 265 | 229 |
| 266 void visitNode(Node node) { | 230 void visitNode(Node node) { |
| 267 node.visitChildren(this); | 231 node.visitChildren(this); |
| 268 constants.nodeConstantMap.remove(node); | 232 constants.nodeConstantMap.remove(node); |
| 269 } | 233 } |
| 270 } | 234 } |
| OLD | NEW |