| 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 ConstExp getConstantForVariable(VariableElement element) { | 26 ConstantExpression getConstantForVariable(VariableElement element) { |
| 27 return dartConstantCompiler.getConstantForVariable(element); | 27 return dartConstantCompiler.getConstantForVariable(element); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ConstExp compileConstant(VariableElement element) { | 30 ConstantExpression compileConstant(VariableElement element) { |
| 31 return measure(() { | 31 return measure(() { |
| 32 ConstExp result = dartConstantCompiler.compileConstant(element); | 32 ConstantExpression 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 ConstExp compileNode(Node node, TreeElements elements) { | 44 ConstantExpression compileNode(Node node, TreeElements elements) { |
| 45 return measure(() { | 45 return measure(() { |
| 46 ConstExp result = | 46 ConstantExpression 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 ConstExp compileMetadata(MetadataAnnotation metadata, | 53 ConstantExpression compileMetadata(MetadataAnnotation metadata, |
| 54 Node node, | 54 Node node, |
| 55 TreeElements elements) { | 55 TreeElements elements) { |
| 56 return measure(() { | 56 return measure(() { |
| 57 ConstExp constant = | 57 ConstantExpression 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<ConstantValue> compiledConstants = new Set<ConstantValue>(); |
| 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, ConstExp> nodeConstantMap = new Map<Node, ConstExp>(); | 81 final Map<Node, ConstantExpression> nodeConstantMap = |
| 82 new Map<Node, ConstantExpression>(); |
| 82 | 83 |
| 83 // Constants computed for metadata. | 84 // Constants computed for metadata. |
| 84 final Map<MetadataAnnotation, ConstExp> metadataConstantMap = | 85 final Map<MetadataAnnotation, ConstantExpression> metadataConstantMap = |
| 85 new Map<MetadataAnnotation, ConstExp>(); | 86 new Map<MetadataAnnotation, ConstantExpression>(); |
| 86 | 87 |
| 87 JavaScriptConstantCompiler(Compiler compiler) | 88 JavaScriptConstantCompiler(Compiler compiler) |
| 88 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM); | 89 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM); |
| 89 | 90 |
| 90 ConstExp compileVariableWithDefinitions(VariableElement element, | 91 ConstantExpression compileVariableWithDefinitions(VariableElement element, |
| 91 TreeElements definitions, | 92 TreeElements definitions, |
| 92 {bool isConst: false}) { | 93 {bool isConst: false}) { |
| 93 if (!isConst && lazyStatics.contains(element)) { | 94 if (!isConst && lazyStatics.contains(element)) { |
| 94 return null; | 95 return null; |
| 95 } | 96 } |
| 96 ConstExp value = super.compileVariableWithDefinitions( | 97 ConstantExpression value = super.compileVariableWithDefinitions( |
| 97 element, definitions, isConst: isConst); | 98 element, definitions, isConst: isConst); |
| 98 if (!isConst && value == null) { | 99 if (!isConst && value == null) { |
| 99 lazyStatics.add(element); | 100 lazyStatics.add(element); |
| 100 } | 101 } |
| 101 return value; | 102 return value; |
| 102 } | 103 } |
| 103 | 104 |
| 104 void addCompileTimeConstantForEmission(Constant constant) { | 105 void addCompileTimeConstantForEmission(ConstantValue constant) { |
| 105 compiledConstants.add(constant); | 106 compiledConstants.add(constant); |
| 106 } | 107 } |
| 107 | 108 |
| 108 /** | 109 /** |
| 109 * Returns an [Iterable] of static non final fields that need to be | 110 * Returns an [Iterable] of static non final fields that need to be |
| 110 * initialized. The fields list must be evaluated in order since they might | 111 * initialized. The fields list must be evaluated in order since they might |
| 111 * depend on each other. | 112 * depend on each other. |
| 112 */ | 113 */ |
| 113 Iterable<VariableElement> getStaticNonFinalFieldsForEmission() { | 114 Iterable<VariableElement> getStaticNonFinalFieldsForEmission() { |
| 114 return initialVariableValues.keys.where((element) { | 115 return initialVariableValues.keys.where((element) { |
| 115 return element.kind == ElementKind.FIELD && | 116 return element.kind == ElementKind.FIELD && |
| 116 !element.isInstanceMember && | 117 !element.isInstanceMember && |
| 117 !element.modifiers.isFinal && | 118 !element.modifiers.isFinal && |
| 118 // The const fields are all either emitted elsewhere or inlined. | 119 // The const fields are all either emitted elsewhere or inlined. |
| 119 !element.modifiers.isConst; | 120 !element.modifiers.isConst; |
| 120 }); | 121 }); |
| 121 } | 122 } |
| 122 | 123 |
| 123 List<VariableElement> getLazilyInitializedFieldsForEmission() { | 124 List<VariableElement> getLazilyInitializedFieldsForEmission() { |
| 124 return new List<VariableElement>.from(lazyStatics); | 125 return new List<VariableElement>.from(lazyStatics); |
| 125 } | 126 } |
| 126 | 127 |
| 127 /** | 128 /** |
| 128 * Returns a list of constants topologically sorted so that dependencies | 129 * Returns a list of constants topologically sorted so that dependencies |
| 129 * appear before the dependent constant. [preSortCompare] is a comparator | 130 * appear before the dependent constant. [preSortCompare] is a comparator |
| 130 * function that gives the constants a consistent order prior to the | 131 * function that gives the constants a consistent order prior to the |
| 131 * topological sort which gives the constants an ordering that is less | 132 * topological sort which gives the constants an ordering that is less |
| 132 * sensitive to perturbations in the source code. | 133 * sensitive to perturbations in the source code. |
| 133 */ | 134 */ |
| 134 List<Constant> getConstantsForEmission([preSortCompare]) { | 135 List<ConstantValue> getConstantsForEmission([preSortCompare]) { |
| 135 // We must emit dependencies before their uses. | 136 // We must emit dependencies before their uses. |
| 136 Set<Constant> seenConstants = new Set<Constant>(); | 137 Set<ConstantValue> seenConstants = new Set<ConstantValue>(); |
| 137 List<Constant> result = new List<Constant>(); | 138 List<ConstantValue> result = new List<ConstantValue>(); |
| 138 | 139 |
| 139 void addConstant(Constant constant) { | 140 void addConstant(ConstantValue constant) { |
| 140 if (!seenConstants.contains(constant)) { | 141 if (!seenConstants.contains(constant)) { |
| 141 constant.getDependencies().forEach(addConstant); | 142 constant.getDependencies().forEach(addConstant); |
| 142 assert(!seenConstants.contains(constant)); | 143 assert(!seenConstants.contains(constant)); |
| 143 result.add(constant); | 144 result.add(constant); |
| 144 seenConstants.add(constant); | 145 seenConstants.add(constant); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 148 List<Constant> sorted = compiledConstants.toList(); | 149 List<ConstantValue> sorted = compiledConstants.toList(); |
| 149 if (preSortCompare != null) { | 150 if (preSortCompare != null) { |
| 150 sorted.sort(preSortCompare); | 151 sorted.sort(preSortCompare); |
| 151 } | 152 } |
| 152 sorted.forEach(addConstant); | 153 sorted.forEach(addConstant); |
| 153 return result; | 154 return result; |
| 154 } | 155 } |
| 155 | 156 |
| 156 ConstExp getInitialValueFor(VariableElement element) { | 157 ConstantExpression getInitialValueFor(VariableElement element) { |
| 157 ConstExp initialValue = initialVariableValues[element.declaration]; | 158 ConstantExpression initialValue = |
| 159 initialVariableValues[element.declaration]; |
| 158 if (initialValue == null) { | 160 if (initialValue == null) { |
| 159 compiler.internalError(element, "No initial value for given element."); | 161 compiler.internalError(element, "No initial value for given element."); |
| 160 } | 162 } |
| 161 return initialValue; | 163 return initialValue; |
| 162 } | 164 } |
| 163 | 165 |
| 164 ConstExp compileNode(Node node, TreeElements elements) { | 166 ConstantExpression compileNode(Node node, TreeElements elements) { |
| 165 return compileNodeWithDefinitions(node, elements); | 167 return compileNodeWithDefinitions(node, elements); |
| 166 } | 168 } |
| 167 | 169 |
| 168 ConstExp compileNodeWithDefinitions(Node node, | 170 ConstantExpression compileNodeWithDefinitions(Node node, |
| 169 TreeElements definitions, | 171 TreeElements definitions, |
| 170 {bool isConst: true}) { | 172 {bool isConst: true}) { |
| 171 ConstExp constant = nodeConstantMap[node]; | 173 ConstantExpression constant = nodeConstantMap[node]; |
| 172 if (constant != null) { | 174 if (constant != null) { |
| 173 return constant; | 175 return constant; |
| 174 } | 176 } |
| 175 constant = | 177 constant = |
| 176 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | 178 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); |
| 177 if (constant != null) { | 179 if (constant != null) { |
| 178 nodeConstantMap[node] = constant; | 180 nodeConstantMap[node] = constant; |
| 179 } | 181 } |
| 180 return constant; | 182 return constant; |
| 181 } | 183 } |
| 182 | 184 |
| 183 ConstExp getConstantForNode(Node node, TreeElements definitions) { | 185 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { |
| 184 ConstExp constant = nodeConstantMap[node]; | 186 ConstantExpression constant = nodeConstantMap[node]; |
| 185 if (constant != null) { | 187 if (constant != null) { |
| 186 return constant; | 188 return constant; |
| 187 } | 189 } |
| 188 return definitions.getConstant(node); | 190 return definitions.getConstant(node); |
| 189 } | 191 } |
| 190 | 192 |
| 191 ConstExp getConstantForMetadata(MetadataAnnotation metadata) { | 193 ConstantExpression getConstantForMetadata(MetadataAnnotation metadata) { |
| 192 return metadataConstantMap[metadata]; | 194 return metadataConstantMap[metadata]; |
| 193 } | 195 } |
| 194 | 196 |
| 195 ConstExp compileMetadata(MetadataAnnotation metadata, | 197 ConstantExpression compileMetadata(MetadataAnnotation metadata, |
| 196 Node node, | 198 Node node, |
| 197 TreeElements elements) { | 199 TreeElements elements) { |
| 198 ConstExp constant = super.compileMetadata(metadata, node, elements); | 200 ConstantExpression constant = |
| 201 super.compileMetadata(metadata, node, elements); |
| 199 metadataConstantMap[metadata] = constant; | 202 metadataConstantMap[metadata] = constant; |
| 200 return constant; | 203 return constant; |
| 201 } | 204 } |
| 202 | 205 |
| 203 ConstExp createTypeConstant(TypeDeclarationElement element) { | 206 ConstantExpression createTypeConstant(TypeDeclarationElement element) { |
| 204 DartType elementType = element.rawType; | 207 DartType elementType = element.rawType; |
| 205 DartType constantType = | 208 DartType constantType = |
| 206 compiler.backend.typeImplementation.computeType(compiler); | 209 compiler.backend.typeImplementation.computeType(compiler); |
| 207 return new TypeConstExp( | 210 return new TypeConstantExpression( |
| 208 new TypeConstant(elementType, constantType), elementType); | 211 new TypeConstantValue(elementType, constantType), elementType); |
| 209 } | 212 } |
| 210 | 213 |
| 211 void forgetElement(Element element) { | 214 void forgetElement(Element element) { |
| 212 element.accept(new ForgetConstantVisitor(this)); | 215 element.accept(new ForgetConstantVisitor(this)); |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 | 218 |
| 216 class ForgetConstantVisitor extends ElementVisitor { | 219 class ForgetConstantVisitor extends ElementVisitor { |
| 217 final JavaScriptConstantCompiler constants; | 220 final JavaScriptConstantCompiler constants; |
| 218 | 221 |
| 219 ForgetConstantVisitor(this.constants); | 222 ForgetConstantVisitor(this.constants); |
| 220 | 223 |
| 221 void visitElement(Element e) { | 224 void visitElement(Element e) { |
| 222 for (MetadataAnnotation data in e.metadata) { | 225 for (MetadataAnnotation data in e.metadata) { |
| 223 constants.metadataConstantMap.remove(data); | 226 constants.metadataConstantMap.remove(data); |
| 224 } | 227 } |
| 225 } | 228 } |
| 226 | 229 |
| 227 void visitFunctionElement(FunctionElement e) { | 230 void visitFunctionElement(FunctionElement e) { |
| 228 super.visitFunctionElement(e); | 231 super.visitFunctionElement(e); |
| 229 if (e.hasFunctionSignature) { | 232 if (e.hasFunctionSignature) { |
| 230 e.functionSignature.forEachParameter(this.visit); | 233 e.functionSignature.forEachParameter(this.visit); |
| 231 } | 234 } |
| 232 } | 235 } |
| 233 } | 236 } |
| OLD | NEW |