| 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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [ConstantHandler] keeps track of compile-time constants, | 8 * The [ConstantHandler] keeps track of compile-time constants, |
| 9 * initializations of global and static fields, and default values of | 9 * initializations of global and static fields, and default values of |
| 10 * optional parameters. | 10 * optional parameters. |
| 11 */ | 11 */ |
| 12 class ConstantHandler extends CompilerTask { | 12 class ConstantHandler extends CompilerTask { |
| 13 final ConstantSystem constantSystem; | 13 final ConstantSystem constantSystem; |
| 14 final bool isMetadata; | 14 final bool isMetadata; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Contains the initial value of fields. Must contain all static and global | 17 * Contains the initial value of fields. Must contain all static and global |
| 18 * initializations of const fields. May contain eagerly compiled values for | 18 * initializations of const fields. May contain eagerly compiled values for |
| 19 * statics and instance fields. | 19 * statics and instance fields. |
| 20 * |
| 21 * Invariant: The keys in this map are declarations. |
| 20 */ | 22 */ |
| 21 final Map<VariableElement, Constant> initialVariableValues; | 23 final Map<VariableElement, Constant> initialVariableValues; |
| 22 | 24 |
| 23 /** Set of all registered compiled constants. */ | 25 /** Set of all registered compiled constants. */ |
| 24 final Set<Constant> compiledConstants; | 26 final Set<Constant> compiledConstants; |
| 25 | 27 |
| 26 /** The set of variable elements that are in the process of being computed. */ | 28 /** The set of variable elements that are in the process of being computed. */ |
| 27 final Set<VariableElement> pendingVariables; | 29 final Set<VariableElement> pendingVariables; |
| 28 | 30 |
| 29 /** Caches the statics where the initial value cannot be eagerly compiled. */ | 31 /** Caches the statics where the initial value cannot be eagerly compiled. */ |
| 30 final Set<VariableElement> lazyStatics; | 32 final Set<VariableElement> lazyStatics; |
| 31 | 33 |
| 32 ConstantHandler(Compiler compiler, this.constantSystem, | 34 ConstantHandler(Compiler compiler, this.constantSystem, |
| 33 { bool this.isMetadata: false }) | 35 { bool this.isMetadata: false }) |
| 34 : initialVariableValues = new Map<VariableElement, dynamic>(), | 36 : initialVariableValues = new Map<VariableElement, dynamic>(), |
| 35 compiledConstants = new Set<Constant>(), | 37 compiledConstants = new Set<Constant>(), |
| 36 pendingVariables = new Set<VariableElement>(), | 38 pendingVariables = new Set<VariableElement>(), |
| 37 lazyStatics = new Set<VariableElement>(), | 39 lazyStatics = new Set<VariableElement>(), |
| 38 super(compiler); | 40 super(compiler); |
| 39 | 41 |
| 40 String get name => 'ConstantHandler'; | 42 String get name => 'ConstantHandler'; |
| 41 | 43 |
| 42 void addCompileTimeConstantForEmission(Constant constant) { | 44 void addCompileTimeConstantForEmission(Constant constant) { |
| 43 compiledConstants.add(constant); | 45 compiledConstants.add(constant); |
| 44 } | 46 } |
| 45 | 47 |
| 46 Constant getConstantForVariable(VariableElement element) { | 48 Constant getConstantForVariable(VariableElement element) { |
| 47 return initialVariableValues[element]; | 49 return initialVariableValues[element.declaration]; |
| 48 } | 50 } |
| 49 | 51 |
| 50 /** | 52 /** |
| 51 * Returns a compile-time constant, or reports an error if the element is not | 53 * Returns a compile-time constant, or reports an error if the element is not |
| 52 * a compile-time constant. | 54 * a compile-time constant. |
| 53 */ | 55 */ |
| 54 Constant compileConstant(VariableElement element) { | 56 Constant compileConstant(VariableElement element) { |
| 55 return compileVariable(element, isConst: true); | 57 return compileVariable(element, isConst: true); |
| 56 } | 58 } |
| 57 | 59 |
| 58 /** | 60 /** |
| 59 * Returns the a compile-time constant if the variable could be compiled | 61 * Returns the a compile-time constant if the variable could be compiled |
| 60 * eagerly. Otherwise returns `null`. | 62 * eagerly. Otherwise returns `null`. |
| 61 */ | 63 */ |
| 62 Constant compileVariable(VariableElement element, {bool isConst: false}) { | 64 Constant compileVariable(VariableElement element, {bool isConst: false}) { |
| 63 return measure(() { | 65 return measure(() { |
| 64 if (initialVariableValues.containsKey(element)) { | 66 if (initialVariableValues.containsKey(element.declaration)) { |
| 65 Constant result = initialVariableValues[element]; | 67 Constant result = initialVariableValues[element.declaration]; |
| 66 return result; | 68 return result; |
| 67 } | 69 } |
| 68 Element currentElement = element; | 70 Element currentElement = element; |
| 69 if (element.isParameter() | 71 if (element.isParameter() |
| 70 || element.isFieldParameter() | 72 || element.isFieldParameter() |
| 71 || element.isVariable()) { | 73 || element.isVariable()) { |
| 72 currentElement = element.enclosingElement; | 74 currentElement = element.enclosingElement; |
| 73 } | 75 } |
| 74 return compiler.withCurrentElement(currentElement, () { | 76 return compiler.withCurrentElement(currentElement, () { |
| 75 TreeElements definitions = | 77 TreeElements definitions = |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } else { | 140 } else { |
| 139 // If the field cannot be lazily initialized, we will throw | 141 // If the field cannot be lazily initialized, we will throw |
| 140 // the exception at runtime. | 142 // the exception at runtime. |
| 141 value = null; | 143 value = null; |
| 142 } | 144 } |
| 143 } | 145 } |
| 144 } | 146 } |
| 145 } | 147 } |
| 146 } | 148 } |
| 147 if (value != null) { | 149 if (value != null) { |
| 148 initialVariableValues[element] = value; | 150 initialVariableValues[element.declaration] = value; |
| 149 } else { | 151 } else { |
| 150 assert(!isConst); | 152 assert(!isConst); |
| 151 lazyStatics.add(element); | 153 lazyStatics.add(element); |
| 152 } | 154 } |
| 153 pendingVariables.remove(element); | 155 pendingVariables.remove(element); |
| 154 return value; | 156 return value; |
| 155 }); | 157 }); |
| 156 } | 158 } |
| 157 | 159 |
| 158 Constant compileNodeWithDefinitions(Node node, | 160 Constant compileNodeWithDefinitions(Node node, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 218 |
| 217 List<Constant> sorted = compiledConstants.toList(); | 219 List<Constant> sorted = compiledConstants.toList(); |
| 218 if (preSortCompare != null) { | 220 if (preSortCompare != null) { |
| 219 sorted.sort(preSortCompare); | 221 sorted.sort(preSortCompare); |
| 220 } | 222 } |
| 221 sorted.forEach(addConstant); | 223 sorted.forEach(addConstant); |
| 222 return result; | 224 return result; |
| 223 } | 225 } |
| 224 | 226 |
| 225 Constant getInitialValueFor(VariableElement element) { | 227 Constant getInitialValueFor(VariableElement element) { |
| 226 Constant initialValue = initialVariableValues[element]; | 228 Constant initialValue = initialVariableValues[element.declaration]; |
| 227 if (initialValue == null) { | 229 if (initialValue == null) { |
| 228 compiler.internalError("No initial value for given element", | 230 compiler.internalError("No initial value for given element", |
| 229 element: element); | 231 element: element); |
| 230 } | 232 } |
| 231 return initialValue; | 233 return initialValue; |
| 232 } | 234 } |
| 233 } | 235 } |
| 234 | 236 |
| 235 class CompileTimeConstantEvaluator extends Visitor { | 237 class CompileTimeConstantEvaluator extends Visitor { |
| 236 bool isEvaluatingConstant; | 238 bool isEvaluatingConstant; |
| (...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 951 if (fieldValue == null) { | 953 if (fieldValue == null) { |
| 952 // Use the default value. | 954 // Use the default value. |
| 953 fieldValue = handler.compileConstant(field); | 955 fieldValue = handler.compileConstant(field); |
| 954 } | 956 } |
| 955 jsNewArguments.add(fieldValue); | 957 jsNewArguments.add(fieldValue); |
| 956 }, | 958 }, |
| 957 includeSuperAndInjectedMembers: true); | 959 includeSuperAndInjectedMembers: true); |
| 958 return jsNewArguments; | 960 return jsNewArguments; |
| 959 } | 961 } |
| 960 } | 962 } |
| OLD | NEW |