| 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 Constant getConstantForVariable(VariableElement element) { | 26 ConstExp getConstantForVariable(VariableElement element) { |
| 27 return dartConstantCompiler.getConstantForVariable(element); | 27 return dartConstantCompiler.getConstantForVariable(element); |
| 28 } | 28 } |
| 29 | 29 |
| 30 Constant compileConstant(VariableElement element) { | 30 ConstExp compileConstant(VariableElement element) { |
| 31 return measure(() { | 31 return measure(() { |
| 32 Constant result = dartConstantCompiler.compileConstant(element); | 32 ConstExp result = dartConstantCompiler.compileConstant(element); |
| 33 jsConstantCompiler.compileConstant(element); | 33 jsConstantCompiler.compileConstant(element); |
| 34 return result; | 34 return result; |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void compileVariable(VariableElement element) { | 38 void compileVariable(VariableElement element) { |
| 39 measure(() { | 39 measure(() { |
| 40 jsConstantCompiler.compileVariable(element); | 40 jsConstantCompiler.compileVariable(element); |
| 41 }); | 41 }); |
| 42 } | 42 } |
| 43 | 43 |
| 44 Constant compileNode(Node node, TreeElements elements) { | 44 ConstExp compileNode(Node node, TreeElements elements) { |
| 45 return measure(() { | 45 return measure(() { |
| 46 Constant result = | 46 ConstExp result = |
| 47 dartConstantCompiler.compileNode(node, elements); | 47 dartConstantCompiler.compileNode(node, elements); |
| 48 jsConstantCompiler.compileNode(node, elements); | 48 jsConstantCompiler.compileNode(node, elements); |
| 49 return result; | 49 return result; |
| 50 }); | 50 }); |
| 51 } | 51 } |
| 52 | 52 |
| 53 Constant compileMetadata(MetadataAnnotation metadata, | 53 ConstExp compileMetadata(MetadataAnnotation metadata, |
| 54 Node node, | 54 Node node, |
| 55 TreeElements elements) { | 55 TreeElements elements) { |
| 56 return measure(() { | 56 return measure(() { |
| 57 Constant constant = | 57 ConstExp constant = |
| 58 dartConstantCompiler.compileMetadata(metadata, node, elements); | 58 dartConstantCompiler.compileMetadata(metadata, node, elements); |
| 59 jsConstantCompiler.compileMetadata(metadata, node, elements); | 59 jsConstantCompiler.compileMetadata(metadata, node, elements); |
| 60 return constant; | 60 return constant; |
| 61 }); | 61 }); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * The [JavaScriptConstantCompiler] is used to keep track of compile-time | 66 * The [JavaScriptConstantCompiler] is used to keep track of compile-time |
| 67 * constants, initializations of global and static fields, and default values of | 67 * constants, initializations of global and static fields, and default values of |
| 68 * optional parameters for the JavaScript interpretation of constants. | 68 * optional parameters for the JavaScript interpretation of constants. |
| 69 */ | 69 */ |
| 70 class JavaScriptConstantCompiler extends ConstantCompilerBase | 70 class JavaScriptConstantCompiler extends ConstantCompilerBase |
| 71 implements BackendConstantEnvironment { | 71 implements BackendConstantEnvironment { |
| 72 | 72 |
| 73 /** Set of all registered compiled constants. */ | 73 /** Set of all registered compiled constants. */ |
| 74 final Set<Constant> compiledConstants = new Set<Constant>(); | 74 final Set<Constant> compiledConstants = new Set<Constant>(); |
| 75 | 75 |
| 76 // TODO(johnniwinther): Move this to the backend constant handler. | 76 // TODO(johnniwinther): Move this to the backend constant handler. |
| 77 /** Caches the statics where the initial value cannot be eagerly compiled. */ | 77 /** Caches the statics where the initial value cannot be eagerly compiled. */ |
| 78 final Set<VariableElement> lazyStatics = new Set<VariableElement>(); | 78 final Set<VariableElement> lazyStatics = new Set<VariableElement>(); |
| 79 | 79 |
| 80 // Constants computed for constant expressions. | 80 // Constants computed for constant expressions. |
| 81 final Map<Node, Constant> nodeConstantMap = new Map<Node, Constant>(); | 81 final Map<Node, ConstExp> nodeConstantMap = new Map<Node, ConstExp>(); |
| 82 | 82 |
| 83 // Constants computed for metadata. | 83 // Constants computed for metadata. |
| 84 final Map<MetadataAnnotation, Constant> metadataConstantMap = | 84 final Map<MetadataAnnotation, ConstExp> metadataConstantMap = |
| 85 new Map<MetadataAnnotation, Constant>(); | 85 new Map<MetadataAnnotation, ConstExp>(); |
| 86 | 86 |
| 87 JavaScriptConstantCompiler(Compiler compiler) | 87 JavaScriptConstantCompiler(Compiler compiler) |
| 88 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM); | 88 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM); |
| 89 | 89 |
| 90 Constant compileVariableWithDefinitions(VariableElement element, | 90 ConstExp compileVariableWithDefinitions(VariableElement element, |
| 91 TreeElements definitions, | 91 TreeElements definitions, |
| 92 {bool isConst: false}) { | 92 {bool isConst: false}) { |
| 93 if (!isConst && lazyStatics.contains(element)) { | 93 if (!isConst && lazyStatics.contains(element)) { |
| 94 return null; | 94 return null; |
| 95 } | 95 } |
| 96 Constant value = super.compileVariableWithDefinitions( | 96 ConstExp value = super.compileVariableWithDefinitions( |
| 97 element, definitions, isConst: isConst); | 97 element, definitions, isConst: isConst); |
| 98 if (!isConst && value == null) { | 98 if (!isConst && value == null) { |
| 99 lazyStatics.add(element); | 99 lazyStatics.add(element); |
| 100 } | 100 } |
| 101 return value; | 101 return value; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void addCompileTimeConstantForEmission(Constant constant) { | 104 void addCompileTimeConstantForEmission(Constant constant) { |
| 105 compiledConstants.add(constant); | 105 compiledConstants.add(constant); |
| 106 } | 106 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 146 } |
| 147 | 147 |
| 148 List<Constant> sorted = compiledConstants.toList(); | 148 List<Constant> sorted = compiledConstants.toList(); |
| 149 if (preSortCompare != null) { | 149 if (preSortCompare != null) { |
| 150 sorted.sort(preSortCompare); | 150 sorted.sort(preSortCompare); |
| 151 } | 151 } |
| 152 sorted.forEach(addConstant); | 152 sorted.forEach(addConstant); |
| 153 return result; | 153 return result; |
| 154 } | 154 } |
| 155 | 155 |
| 156 Constant getInitialValueFor(VariableElement element) { | 156 ConstExp getInitialValueFor(VariableElement element) { |
| 157 Constant initialValue = initialVariableValues[element.declaration]; | 157 ConstExp initialValue = initialVariableValues[element.declaration]; |
| 158 if (initialValue == null) { | 158 if (initialValue == null) { |
| 159 compiler.internalError(element, "No initial value for given element."); | 159 compiler.internalError(element, "No initial value for given element."); |
| 160 } | 160 } |
| 161 return initialValue; | 161 return initialValue; |
| 162 } | 162 } |
| 163 | 163 |
| 164 Constant compileNode(Node node, TreeElements elements) { | 164 ConstExp compileNode(Node node, TreeElements elements) { |
| 165 return compileNodeWithDefinitions(node, elements); | 165 return compileNodeWithDefinitions(node, elements); |
| 166 } | 166 } |
| 167 | 167 |
| 168 Constant compileNodeWithDefinitions(Node node, | 168 ConstExp compileNodeWithDefinitions(Node node, |
| 169 TreeElements definitions, | 169 TreeElements definitions, |
| 170 {bool isConst: true}) { | 170 {bool isConst: true}) { |
| 171 Constant constant = nodeConstantMap[node]; | 171 ConstExp constant = nodeConstantMap[node]; |
| 172 if (constant != null) { | 172 if (constant != null) { |
| 173 return constant; | 173 return constant; |
| 174 } | 174 } |
| 175 constant = | 175 constant = |
| 176 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | 176 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); |
| 177 if (constant != null) { | 177 if (constant != null) { |
| 178 nodeConstantMap[node] = constant; | 178 nodeConstantMap[node] = constant; |
| 179 } | 179 } |
| 180 return constant; | 180 return constant; |
| 181 } | 181 } |
| 182 | 182 |
| 183 Constant getConstantForNode(Node node, TreeElements definitions) { | 183 ConstExp getConstantForNode(Node node, TreeElements definitions) { |
| 184 Constant constant = nodeConstantMap[node]; | 184 ConstExp constant = nodeConstantMap[node]; |
| 185 if (constant != null) { | 185 if (constant != null) { |
| 186 return constant; | 186 return constant; |
| 187 } | 187 } |
| 188 return definitions.getConstant(node); | 188 return definitions.getConstant(node); |
| 189 } | 189 } |
| 190 | 190 |
| 191 Constant getConstantForMetadata(MetadataAnnotation metadata) { | 191 ConstExp getConstantForMetadata(MetadataAnnotation metadata) { |
| 192 return metadataConstantMap[metadata]; | 192 return metadataConstantMap[metadata]; |
| 193 } | 193 } |
| 194 | 194 |
| 195 Constant compileMetadata(MetadataAnnotation metadata, | 195 ConstExp compileMetadata(MetadataAnnotation metadata, |
| 196 Node node, | 196 Node node, |
| 197 TreeElements elements) { | 197 TreeElements elements) { |
| 198 Constant constant = super.compileMetadata(metadata, node, elements); | 198 ConstExp constant = super.compileMetadata(metadata, node, elements); |
| 199 metadataConstantMap[metadata] = constant; | 199 metadataConstantMap[metadata] = constant; |
| 200 return constant; | 200 return constant; |
| 201 } | 201 } |
| 202 | 202 |
| 203 Constant createTypeConstant(TypeDeclarationElement element) { | 203 ConstExp createTypeConstant(TypeDeclarationElement element) { |
| 204 DartType elementType = element.rawType; | 204 DartType elementType = element.rawType; |
| 205 DartType constantType = | 205 DartType constantType = |
| 206 compiler.backend.typeImplementation.computeType(compiler); | 206 compiler.backend.typeImplementation.computeType(compiler); |
| 207 return new TypeConstant(elementType, constantType); | 207 return new TypeConstExp( |
| 208 new TypeConstant(elementType, constantType), elementType); |
| 208 } | 209 } |
| 209 } | 210 } |
| OLD | NEW |