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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js_backend/constant_handler_javascript.dart

Issue 221873002: Compute frontend/backend specific constants. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months 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 class JavaScriptConstantHandler extends CompilerTask
floitsch 2014/04/02 16:50:02 Since this is "just" a compiler would it make sens
Johnni Winther 2014/04/07 11:42:27 Done.
8 implements ConstantCompiler {
9 DartConstantCompiler dartConstantCompiler;
10 JavaScriptBackendConstantHandler jsConstantCompiler;
floitsch 2014/04/02 16:50:02 And this is really a ConstantHandler.
Johnni Winther 2014/04/07 11:42:27 Renamed.
11
12 JavaScriptConstantHandler(Compiler compiler)
13 : this.dartConstantCompiler = new DartConstantCompiler(compiler),
14 this.jsConstantCompiler =
15 new JavaScriptBackendConstantHandler(compiler),
16 super(compiler);
17
18 String get name => 'ConstantHandler';
19
20 Constant getConstantForVariable(VariableElement element) {
21 return dartConstantCompiler.getConstantForVariable(element);
22 }
23
24 Constant compileConstant(VariableElement element) {
25 return measure(() {
26 Constant result = dartConstantCompiler.compileConstant(element);
27 jsConstantCompiler.compileConstant(element);
28 return result;
29 });
30 }
31
32 Constant compileNode(Node node, TreeElements elements) {
33 return measure(() {
34 Constant result =
35 dartConstantCompiler.compileNode(node, elements);
36 jsConstantCompiler.compileNode(node, elements);
37 return result;
38 });
39 }
40
41 Constant compileMetadata(MetadataAnnotation metadata,
42 Node node,
43 TreeElements elements) {
44 return measure(() {
45 Constant constant =
46 dartConstantCompiler.compileMetadata(metadata, node, elements);
47 jsConstantCompiler.compileMetadata(metadata, node, elements);
48 return constant;
49 });
50 }
51 }
52
53 /**
54 * The [JavaScriptBackendConstantHandler] is used to keeps track of compile-time
55 * constants, initializations of global and static fields, and default values of
56 * optional parameters.
57 */
58 class JavaScriptBackendConstantHandler extends ConstantCompilerBase
59 implements BackendConstantHandler {
60
61 /** Set of all registered compiled constants. */
62 final Set<Constant> compiledConstants = new Set<Constant>();
63
64 // TODO(johnniwinther): Move this to the backend constant handler.
65 /** Caches the statics where the initial value cannot be eagerly compiled. */
66 final Set<VariableElement> lazyStatics = new Set<VariableElement>();
67
68 // Constants computed for constant expressions.
69 final Map<Node, Constant> nodeConstantMap = new Map<Node, Constant>();
70
71 // Constants computed for metadata.
72 final Map<MetadataAnnotation, Constant> metadataConstantMap =
73 new Map<MetadataAnnotation, Constant>();
74
75 JavaScriptBackendConstantHandler(Compiler compiler)
76 : super(compiler, JAVA_SCRIPT_CONSTANT_SYSTEM);
77
78 Constant compileVariableWithDefinitions(VariableElement element,
79 TreeElements definitions,
80 {bool isConst: false}) {
81 if (!isConst && lazyStatics.contains(element)) {
82 return null;
83 }
84 Constant value = super.compileVariableWithDefinitions(
85 element, definitions, isConst: isConst);
86 if (!isConst && value == null) {
87 lazyStatics.add(element);
88 }
89 return value;
90 }
91
92 void addCompileTimeConstantForEmission(Constant constant) {
93 compiledConstants.add(constant);
94 }
95
96 /**
97 * Returns an [Iterable] of static non final fields that need to be
98 * initialized. The fields list must be evaluated in order since they might
99 * depend on each other.
100 */
101 Iterable<VariableElement> getStaticNonFinalFieldsForEmission() {
102 return initialVariableValues.keys.where((element) {
103 return element.kind == ElementKind.FIELD
104 && !element.isInstanceMember()
karlklose 2014/04/02 13:04:44 This is not your code, but maybe break after opera
Johnni Winther 2014/04/07 11:42:27 Done.
105 && !element.modifiers.isFinal()
106 // The const fields are all either emitted elsewhere or inlined.
107 && !element.modifiers.isConst();
108 });
109 }
110
111 List<VariableElement> getLazilyInitializedFieldsForEmission() {
112 return new List<VariableElement>.from(lazyStatics);
113 }
114
115 /**
116 * Returns a list of constants topologically sorted so that dependencies
117 * appear before the dependent constant. [preSortCompare] is a comparator
118 * function that gives the constants a consistent order prior to the
119 * topological sort which gives the constants an ordering that is less
120 * sensitive to perturbations in the source code.
121 */
122 List<Constant> getConstantsForEmission([preSortCompare]) {
123 // We must emit dependencies before their uses.
124 Set<Constant> seenConstants = new Set<Constant>();
125 List<Constant> result = new List<Constant>();
126
127 void addConstant(Constant constant) {
128 if (!seenConstants.contains(constant)) {
129 constant.getDependencies().forEach(addConstant);
130 assert(!seenConstants.contains(constant));
131 result.add(constant);
132 seenConstants.add(constant);
133 }
134 }
135
136 List<Constant> sorted = compiledConstants.toList();
137 if (preSortCompare != null) {
138 sorted.sort(preSortCompare);
139 }
140 sorted.forEach(addConstant);
141 return result;
142 }
143
144 Constant getInitialValueFor(VariableElement element) {
145 Constant initialValue = initialVariableValues[element.declaration];
146 if (initialValue == null) {
147 compiler.internalError(element, "No initial value for given element.");
148 }
149 return initialValue;
150 }
151
152 Constant compileNode(Node node, TreeElements elements) {
153 return compileNodeWithDefinitions(node, elements);
154 }
155
156 Constant compileNodeWithDefinitions(Node node,
157 TreeElements definitions,
158 {bool isConst: true}) {
159 Constant constant = nodeConstantMap[node];
160 if (constant != null) {
161 return constant;
162 }
163 constant =
164 super.compileNodeWithDefinitions(node, definitions, isConst: isConst);
165 if (constant != null) {
166 nodeConstantMap[node] = constant;
167 }
168 return constant;
169 }
170
171 Constant getConstantForNode(Node node, TreeElements definitions) {
172 Constant constant = nodeConstantMap[node];
173 if (constant != null) {
174 return constant;
175 }
176 return definitions.getConstant(node);
177 }
178
179 Constant getConstantForMetadata(MetadataAnnotation metadata) {
180 return metadataConstantMap[metadata];
181 }
182
183 Constant compileMetadata(MetadataAnnotation metadata,
184 Node node,
185 TreeElements elements) {
186 Constant constant = super.compileMetadata(metadata, node, elements);
187 metadataConstantMap[metadata] = constant;
188 return constant;
189 }
190
191 Constant createTypeConstant(TypeDeclarationElement element) {
192 DartType elementType = element.rawType;
193 DartType constantType =
194 compiler.backend.typeImplementation.computeType(compiler);
195 return new TypeConstant(elementType, constantType);
196 }
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698