Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: pkg/compiler/lib/src/js_backend/constant_handler_javascript.dart

Issue 693183006: Revert "Move dart2js from sdk/lib/_internal/compiler to pkg/compiler" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ConstantExpression getConstantForVariable(VariableElement element) {
27 return dartConstantCompiler.getConstantForVariable(element);
28 }
29
30 ConstantExpression compileConstant(VariableElement element) {
31 return measure(() {
32 ConstantExpression 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 ConstantExpression compileNode(Node node, TreeElements elements) {
45 return measure(() {
46 ConstantExpression result =
47 dartConstantCompiler.compileNode(node, elements);
48 jsConstantCompiler.compileNode(node, elements);
49 return result;
50 });
51 }
52
53 ConstantExpression compileMetadata(MetadataAnnotation metadata,
54 Node node,
55 TreeElements elements) {
56 return measure(() {
57 ConstantExpression 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<ConstantValue> compiledConstants = new Set<ConstantValue>();
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, ConstantExpression> nodeConstantMap =
82 new Map<Node, ConstantExpression>();
83
84 // Constants computed for metadata.
85 final Map<MetadataAnnotation, ConstantExpression> metadataConstantMap =
86 new Map<MetadataAnnotation, ConstantExpression>();
87
88 JavaScriptConstantCompiler(Compiler compiler)
89 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM);
90
91 ConstantExpression compileVariableWithDefinitions(VariableElement element,
92 TreeElements definitions,
93 {bool isConst: false}) {
94 if (!isConst && lazyStatics.contains(element)) {
95 return null;
96 }
97 ConstantExpression value = super.compileVariableWithDefinitions(
98 element, definitions, isConst: isConst);
99 if (!isConst && value == null) {
100 lazyStatics.add(element);
101 }
102 return value;
103 }
104
105 void addCompileTimeConstantForEmission(ConstantValue constant) {
106 compiledConstants.add(constant);
107 }
108
109 /**
110 * Returns an [Iterable] of static non final fields that need to be
111 * initialized. The fields list must be evaluated in order since they might
112 * depend on each other.
113 */
114 Iterable<VariableElement> getStaticNonFinalFieldsForEmission() {
115 return initialVariableValues.keys.where((element) {
116 return element.kind == ElementKind.FIELD &&
117 !element.isInstanceMember &&
118 !element.modifiers.isFinal &&
119 // The const fields are all either emitted elsewhere or inlined.
120 !element.modifiers.isConst;
121 });
122 }
123
124 List<VariableElement> getLazilyInitializedFieldsForEmission() {
125 return new List<VariableElement>.from(lazyStatics);
126 }
127
128 /**
129 * Returns a list of constants topologically sorted so that dependencies
130 * appear before the dependent constant. [preSortCompare] is a comparator
131 * function that gives the constants a consistent order prior to the
132 * topological sort which gives the constants an ordering that is less
133 * sensitive to perturbations in the source code.
134 */
135 List<ConstantValue> getConstantsForEmission([preSortCompare]) {
136 // We must emit dependencies before their uses.
137 Set<ConstantValue> seenConstants = new Set<ConstantValue>();
138 List<ConstantValue> result = new List<ConstantValue>();
139
140 void addConstant(ConstantValue constant) {
141 if (!seenConstants.contains(constant)) {
142 constant.getDependencies().forEach(addConstant);
143 assert(!seenConstants.contains(constant));
144 result.add(constant);
145 seenConstants.add(constant);
146 }
147 }
148
149 List<ConstantValue> sorted = compiledConstants.toList();
150 if (preSortCompare != null) {
151 sorted.sort(preSortCompare);
152 }
153 sorted.forEach(addConstant);
154 return result;
155 }
156
157 ConstantExpression getInitialValueFor(VariableElement element) {
158 ConstantExpression initialValue =
159 initialVariableValues[element.declaration];
160 if (initialValue == null) {
161 compiler.internalError(element, "No initial value for given element.");
162 }
163 return initialValue;
164 }
165
166 ConstantExpression compileNode(Node node, TreeElements elements) {
167 return compileNodeWithDefinitions(node, elements);
168 }
169
170 ConstantExpression compileNodeWithDefinitions(Node node,
171 TreeElements definitions,
172 {bool isConst: true}) {
173 ConstantExpression constant = nodeConstantMap[node];
174 if (constant != null) {
175 return constant;
176 }
177 constant =
178 super.compileNodeWithDefinitions(node, definitions, isConst: isConst);
179 if (constant != null) {
180 nodeConstantMap[node] = constant;
181 }
182 return constant;
183 }
184
185 ConstantExpression getConstantForNode(Node node, TreeElements definitions) {
186 ConstantExpression constant = nodeConstantMap[node];
187 if (constant != null) {
188 return constant;
189 }
190 return definitions.getConstant(node);
191 }
192
193 ConstantExpression getConstantForMetadata(MetadataAnnotation metadata) {
194 return metadataConstantMap[metadata];
195 }
196
197 ConstantExpression compileMetadata(MetadataAnnotation metadata,
198 Node node,
199 TreeElements elements) {
200 ConstantExpression constant =
201 super.compileMetadata(metadata, node, elements);
202 metadataConstantMap[metadata] = constant;
203 return constant;
204 }
205
206 ConstantExpression createTypeConstant(TypeDeclarationElement element) {
207 DartType elementType = element.rawType;
208 DartType constantType =
209 compiler.backend.typeImplementation.computeType(compiler);
210 return new TypeConstantExpression(
211 new TypeConstantValue(elementType, constantType), elementType);
212 }
213
214 void forgetElement(Element element) {
215 super.forgetElement(element);
216 element.accept(new ForgetConstantElementVisitor(this));
217 if (element is AstElement && element.hasNode) {
218 element.node.accept(new ForgetConstantNodeVisitor(this));
219 }
220 }
221 }
222
223 class ForgetConstantElementVisitor extends ElementVisitor {
224 final JavaScriptConstantCompiler constants;
225
226 ForgetConstantElementVisitor(this.constants);
227
228 void visitElement(Element e) {
229 for (MetadataAnnotation data in e.metadata) {
230 constants.metadataConstantMap.remove(data);
231 if (data.hasNode) {
232 data.node.accept(new ForgetConstantNodeVisitor(constants));
233 }
234 }
235 }
236
237 void visitFunctionElement(FunctionElement e) {
238 super.visitFunctionElement(e);
239 if (e.hasFunctionSignature) {
240 e.functionSignature.forEachParameter(this.visit);
241 }
242 }
243 }
244
245 class ForgetConstantNodeVisitor extends Visitor {
246 final JavaScriptConstantCompiler constants;
247
248 ForgetConstantNodeVisitor(this.constants);
249
250 void visitNode(Node node) {
251 node.visitChildren(this);
252 constants.nodeConstantMap.remove(node);
253
254 // TODO(ahe): This doesn't belong here. Rename this class and generalize.
255 var closureClassMap =
256 constants.compiler.closureToClassMapper.closureMappingCache
257 .remove(node);
258 if (closureClassMap != null) {
259 closureClassMap.removeMyselfFrom(
260 constants.compiler.enqueuer.codegen.universe);
261 }
262 }
263 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_backend/constant_emitter.dart ('k') | pkg/compiler/lib/src/js_backend/constant_system_javascript.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698