| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of js_backend; | |
| 6 | |
| 7 /// [ConstantCompilerTask] for compilation of constants for the JavaScript | |
| 8 /// backend. | |
| 9 /// | |
| 10 /// Since this task needs to distinguish between frontend and backend constants | |
| 11 /// the actual compilation of the constants is forwarded to a | |
| 12 /// [DartConstantCompiler] for the frontend interpretation of the constants and | |
| 13 /// to a [JavaScriptConstantCompiler] for the backend interpretation. | |
| 14 class JavaScriptConstantTask extends ConstantCompilerTask { | |
| 15 DartConstantCompiler dartConstantCompiler; | |
| 16 JavaScriptConstantCompiler jsConstantCompiler; | |
| 17 | |
| 18 JavaScriptConstantTask(Compiler compiler) | |
| 19 : this.dartConstantCompiler = new DartConstantCompiler(compiler), | |
| 20 this.jsConstantCompiler = | |
| 21 new JavaScriptConstantCompiler(compiler), | |
| 22 super(compiler); | |
| 23 | |
| 24 String get name => 'ConstantHandler'; | |
| 25 | |
| 26 Constant getConstantForVariable(VariableElement element) { | |
| 27 return dartConstantCompiler.getConstantForVariable(element); | |
| 28 } | |
| 29 | |
| 30 Constant compileConstant(VariableElement element) { | |
| 31 return measure(() { | |
| 32 Constant result = dartConstantCompiler.compileConstant(element); | |
| 33 jsConstantCompiler.compileConstant(element); | |
| 34 return result; | |
| 35 }); | |
| 36 } | |
| 37 | |
| 38 void compileVariable(VariableElement element) { | |
| 39 measure(() { | |
| 40 jsConstantCompiler.compileVariable(element); | |
| 41 }); | |
| 42 } | |
| 43 | |
| 44 Constant compileNode(Node node, TreeElements elements) { | |
| 45 return measure(() { | |
| 46 Constant result = | |
| 47 dartConstantCompiler.compileNode(node, elements); | |
| 48 jsConstantCompiler.compileNode(node, elements); | |
| 49 return result; | |
| 50 }); | |
| 51 } | |
| 52 | |
| 53 Constant compileMetadata(MetadataAnnotation metadata, | |
| 54 Node node, | |
| 55 TreeElements elements) { | |
| 56 return measure(() { | |
| 57 Constant constant = | |
| 58 dartConstantCompiler.compileMetadata(metadata, node, elements); | |
| 59 jsConstantCompiler.compileMetadata(metadata, node, elements); | |
| 60 return constant; | |
| 61 }); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * The [JavaScriptConstantCompiler] is used to keep track of compile-time | |
| 67 * constants, initializations of global and static fields, and default values of | |
| 68 * optional parameters for the JavaScript interpretation of constants. | |
| 69 */ | |
| 70 class JavaScriptConstantCompiler extends ConstantCompilerBase | |
| 71 implements BackendConstantEnvironment { | |
| 72 | |
| 73 /** Set of all registered compiled constants. */ | |
| 74 final Set<Constant> compiledConstants = new Set<Constant>(); | |
| 75 | |
| 76 // TODO(johnniwinther): Move this to the backend constant handler. | |
| 77 /** Caches the statics where the initial value cannot be eagerly compiled. */ | |
| 78 final Set<VariableElement> lazyStatics = new Set<VariableElement>(); | |
| 79 | |
| 80 // Constants computed for constant expressions. | |
| 81 final Map<Node, Constant> nodeConstantMap = new Map<Node, Constant>(); | |
| 82 | |
| 83 // Constants computed for metadata. | |
| 84 final Map<MetadataAnnotation, Constant> metadataConstantMap = | |
| 85 new Map<MetadataAnnotation, Constant>(); | |
| 86 | |
| 87 JavaScriptConstantCompiler(Compiler compiler) | |
| 88 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM); | |
| 89 | |
| 90 Constant compileVariableWithDefinitions(VariableElement element, | |
| 91 TreeElements definitions, | |
| 92 {bool isConst: false}) { | |
| 93 if (!isConst && lazyStatics.contains(element)) { | |
| 94 return null; | |
| 95 } | |
| 96 Constant value = super.compileVariableWithDefinitions( | |
| 97 element, definitions, isConst: isConst); | |
| 98 if (!isConst && value == null) { | |
| 99 lazyStatics.add(element); | |
| 100 } | |
| 101 return value; | |
| 102 } | |
| 103 | |
| 104 void addCompileTimeConstantForEmission(Constant constant) { | |
| 105 compiledConstants.add(constant); | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * 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 * depend on each other. | |
| 112 */ | |
| 113 Iterable<VariableElement> getStaticNonFinalFieldsForEmission() { | |
| 114 return initialVariableValues.keys.where((element) { | |
| 115 return element.kind == ElementKind.FIELD && | |
| 116 !element.isInstanceMember() && | |
| 117 !element.modifiers.isFinal() && | |
| 118 // The const fields are all either emitted elsewhere or inlined. | |
| 119 !element.modifiers.isConst(); | |
| 120 }); | |
| 121 } | |
| 122 | |
| 123 List<VariableElement> getLazilyInitializedFieldsForEmission() { | |
| 124 return new List<VariableElement>.from(lazyStatics); | |
| 125 } | |
| 126 | |
| 127 /** | |
| 128 * Returns a list of constants topologically sorted so that dependencies | |
| 129 * appear before the dependent constant. [preSortCompare] is a comparator | |
| 130 * function that gives the constants a consistent order prior to the | |
| 131 * topological sort which gives the constants an ordering that is less | |
| 132 * sensitive to perturbations in the source code. | |
| 133 */ | |
| 134 List<Constant> getConstantsForEmission([preSortCompare]) { | |
| 135 // We must emit dependencies before their uses. | |
| 136 Set<Constant> seenConstants = new Set<Constant>(); | |
| 137 List<Constant> result = new List<Constant>(); | |
| 138 | |
| 139 void addConstant(Constant constant) { | |
| 140 if (!seenConstants.contains(constant)) { | |
| 141 constant.getDependencies().forEach(addConstant); | |
| 142 assert(!seenConstants.contains(constant)); | |
| 143 result.add(constant); | |
| 144 seenConstants.add(constant); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 List<Constant> sorted = compiledConstants.toList(); | |
| 149 if (preSortCompare != null) { | |
| 150 sorted.sort(preSortCompare); | |
| 151 } | |
| 152 sorted.forEach(addConstant); | |
| 153 return result; | |
| 154 } | |
| 155 | |
| 156 Constant getInitialValueFor(VariableElement element) { | |
| 157 Constant initialValue = initialVariableValues[element.declaration]; | |
| 158 if (initialValue == null) { | |
| 159 compiler.internalError(element, "No initial value for given element."); | |
| 160 } | |
| 161 return initialValue; | |
| 162 } | |
| 163 | |
| 164 Constant compileNode(Node node, TreeElements elements) { | |
| 165 return compileNodeWithDefinitions(node, elements); | |
| 166 } | |
| 167 | |
| 168 Constant compileNodeWithDefinitions(Node node, | |
| 169 TreeElements definitions, | |
| 170 {bool isConst: true}) { | |
| 171 Constant constant = nodeConstantMap[node]; | |
| 172 if (constant != null) { | |
| 173 return constant; | |
| 174 } | |
| 175 constant = | |
| 176 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | |
| 177 if (constant != null) { | |
| 178 nodeConstantMap[node] = constant; | |
| 179 } | |
| 180 return constant; | |
| 181 } | |
| 182 | |
| 183 Constant getConstantForNode(Node node, TreeElements definitions) { | |
| 184 Constant constant = nodeConstantMap[node]; | |
| 185 if (constant != null) { | |
| 186 return constant; | |
| 187 } | |
| 188 return definitions.getConstant(node); | |
| 189 } | |
| 190 | |
| 191 Constant getConstantForMetadata(MetadataAnnotation metadata) { | |
| 192 return metadataConstantMap[metadata]; | |
| 193 } | |
| 194 | |
| 195 Constant compileMetadata(MetadataAnnotation metadata, | |
| 196 Node node, | |
| 197 TreeElements elements) { | |
| 198 Constant constant = super.compileMetadata(metadata, node, elements); | |
| 199 metadataConstantMap[metadata] = constant; | |
| 200 return constant; | |
| 201 } | |
| 202 | |
| 203 Constant createTypeConstant(TypeDeclarationElement element) { | |
| 204 DartType elementType = element.rawType; | |
| 205 DartType constantType = | |
| 206 compiler.backend.typeImplementation.computeType(compiler); | |
| 207 return new TypeConstant(elementType, constantType); | |
| 208 } | |
| 209 } | |
| OLD | NEW |