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

Side by Side Diff: pkg/compiler/lib/src/serialization/constant_serialization.dart

Issue 1192103002: Support serialization of the compiler backbone. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments Created 5 years, 5 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
OLDNEW
(Empty)
1 // Copyright (c) 2015, 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 library dart2js.serialization.constants;
6
7 import '../constants/expressions.dart';
8 import '../dart_types.dart';
9 import '../elements/elements.dart' show FieldElement;
10 import '../resolution/operators.dart';
11 import '../universe/universe.dart';
12 import 'serialization.dart';
13 import 'keys.dart';
14
15 /// Visitor that serializes a [ConstantExpression] by encoding it into an
16 /// [ObjectEncoder].
17 ///
18 /// This class is called from the [Serializer] when a [ConstantExpression] needs
19 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType],
20 /// and other [ConstantExpression] that the serialized [ConstantExpression]
21 /// depends upon are also serialized.
22 class ConstantSerializer
23 extends ConstantExpressionVisitor<dynamic, ObjectEncoder> {
24 const ConstantSerializer();
25
26 @override
27 void visitBinary(BinaryConstantExpression exp,
28 ObjectEncoder encoder) {
29 encoder.setEnum(Key.OPERATOR, exp.operator.kind);
30 encoder.setConstant(Key.LEFT, exp.left);
31 encoder.setConstant(Key.RIGHT, exp.right);
32 }
33
34 @override
35 void visitConcatenate(ConcatenateConstantExpression exp,
36 ObjectEncoder encoder) {
37 encoder.setConstants(Key.ARGUMENTS, exp.expressions);
38 }
39
40 @override
41 void visitConditional(ConditionalConstantExpression exp,
42 ObjectEncoder encoder) {
43 encoder.setConstant(Key.CONDITION, exp.condition);
44 encoder.setConstant(Key.TRUE, exp.trueExp);
45 encoder.setConstant(Key.FALSE, exp.falseExp);
46 }
47
48 @override
49 void visitConstructed(ConstructedConstantExpression exp,
50 ObjectEncoder encoder) {
51 encoder.setElement(Key.ELEMENT, exp.target);
52 encoder.setType(Key.TYPE, exp.type);
53 encoder.setStrings(Key.NAMES, exp.callStructure.namedArguments);
54 encoder.setConstants(Key.ARGUMENTS, exp.arguments);
55 }
56
57 @override
58 void visitFunction(FunctionConstantExpression exp,
59 ObjectEncoder encoder) {
60 encoder.setElement(Key.ELEMENT, exp.element);
61 }
62
63 @override
64 void visitIdentical(IdenticalConstantExpression exp,
65 ObjectEncoder encoder) {
66 encoder.setConstant(Key.LEFT, exp.left);
67 encoder.setConstant(Key.RIGHT, exp.right);
68 }
69
70 @override
71 void visitList(ListConstantExpression exp, ObjectEncoder encoder) {
72 encoder.setType(Key.TYPE, exp.type);
73 encoder.setConstants(Key.VALUES, exp.values);
74 }
75
76 @override
77 void visitMap(MapConstantExpression exp, ObjectEncoder encoder) {
78 encoder.setType(Key.TYPE, exp.type);
79 encoder.setConstants(Key.KEYS, exp.keys);
80 encoder.setConstants(Key.VALUES, exp.values);
81 }
82
83 @override
84 void visitBool(BoolConstantExpression exp, ObjectEncoder encoder) {
85 encoder.setBool(Key.VALUE, exp.primitiveValue);
86 }
87
88 @override
89 void visitInt(IntConstantExpression exp, ObjectEncoder encoder) {
90 encoder.setInt(Key.VALUE, exp.primitiveValue);
91 }
92
93 @override
94 void visitDouble(DoubleConstantExpression exp, ObjectEncoder encoder) {
95 encoder.setDouble(Key.VALUE, exp.primitiveValue);
96 }
97
98 @override
99 void visitString(StringConstantExpression exp, ObjectEncoder encoder) {
100 encoder.setString(Key.VALUE, exp.primitiveValue);
101 }
102
103 @override
104 void visitNull(NullConstantExpression exp, ObjectEncoder encoder) {
105 // No additional data needed.
106 }
107
108 @override
109 void visitSymbol(SymbolConstantExpression exp,
110 ObjectEncoder encoder) {
111 throw new UnsupportedError(
112 "ConstantSerializer.visitSymbol: ${exp.getText()}");
113 }
114
115 @override
116 void visitType(TypeConstantExpression exp,
117 ObjectEncoder encoder) {
118 encoder.setType(Key.TYPE, exp.type);
119 }
120
121 @override
122 void visitUnary(UnaryConstantExpression exp,
123 ObjectEncoder encoder) {
124 encoder.setEnum(Key.OPERATOR, exp.operator.kind);
125 encoder.setConstant(Key.EXPRESSION, exp.expression);
126 }
127
128 @override
129 void visitVariable(VariableConstantExpression exp,
130 ObjectEncoder encoder) {
131 encoder.setElement(Key.ELEMENT, exp.element);
132 }
133
134 @override
135 void visitPositional(PositionalArgumentReference exp,
136 ObjectEncoder encoder) {
137 encoder.setInt(Key.INDEX, exp.index);
138 }
139
140 @override
141 void visitNamed(NamedArgumentReference exp, ObjectEncoder encoder) {
142 encoder.setString(Key.NAME, exp.name);
143 }
144
145 @override
146 void visitBoolFromEnvironment(BoolFromEnvironmentConstantExpression exp,
147 ObjectEncoder encoder) {
148 encoder.setConstant(Key.NAME, exp.name);
149 if (exp.defaultValue != null) {
150 encoder.setConstant(Key.DEFAULT, exp.defaultValue);
151 }
152 }
153
154 @override
155 void visitIntFromEnvironment(IntFromEnvironmentConstantExpression exp,
156 ObjectEncoder encoder) {
157 encoder.setConstant(Key.NAME, exp.name);
158 if (exp.defaultValue != null) {
159 encoder.setConstant(Key.DEFAULT, exp.defaultValue);
160 }
161 }
162
163 @override
164 void visitStringFromEnvironment(StringFromEnvironmentConstantExpression exp,
165 ObjectEncoder encoder) {
166 encoder.setConstant(Key.NAME, exp.name);
167 if (exp.defaultValue != null) {
168 encoder.setConstant(Key.DEFAULT, exp.defaultValue);
169 }
170 }
171
172 @override
173 void visitStringLength(StringLengthConstantExpression exp,
174 ObjectEncoder encoder) {
175 encoder.setConstant(Key.EXPRESSION, exp.expression);
176 }
177
178
179 @override
180 void visitDeferred(DeferredConstantExpression exp,
181 ObjectEncoder encoder) {
182 throw new UnsupportedError(
183 "ConstantSerializer.visitDeferred: ${exp.getText()}");
184 }
185 }
186
187 /// Utility class for deserializing [ConstantExpression]s.
188 ///
189 /// This is used by the [Deserializer].
190 class ConstantDeserializer {
191
192 /// Deserializes a [ConstantExpression] from an [ObjectDecoder].
193 ///
194 /// The class is called from the [Deserializer] when a [ConstantExpression]
195 /// needs deserialization. The [ObjectDecoder] ensures that any [Element],
196 /// [DartType], and other [ConstantExpression] that the deserialized
197 /// [ConstantExpression] depends upon are available.
198 static ConstantExpression deserialize(ObjectDecoder decoder) {
199 ConstantExpressionKind kind =
200 decoder.getEnum(Key.KIND, ConstantExpressionKind.values);
201 switch (kind) {
202 case ConstantExpressionKind.BINARY:
203 BinaryOperator operator = BinaryOperator.fromKind(decoder.getEnum(
204 Key.OPERATOR, BinaryOperatorKind.values));
205 return new BinaryConstantExpression(
206 decoder.getConstant(Key.LEFT),
207 operator,
208 decoder.getConstant(Key.RIGHT));
209 case ConstantExpressionKind.BOOL:
210 return new BoolConstantExpression(
211 decoder.getBool(Key.VALUE));
212 case ConstantExpressionKind.BOOL_FROM_ENVIRONMENT:
213 return new BoolFromEnvironmentConstantExpression(
214 decoder.getConstant(Key.NAME),
215 decoder.getConstant(Key.DEFAULT, isOptional: true));
216 case ConstantExpressionKind.CONCATENATE:
217 return new ConcatenateConstantExpression(
218 decoder.getConstants(Key.ARGUMENTS));
219 case ConstantExpressionKind.CONDITIONAL:
220 return new ConditionalConstantExpression(
221 decoder.getConstant(Key.CONDITION),
222 decoder.getConstant(Key.TRUE),
223 decoder.getConstant(Key.FALSE));
224 case ConstantExpressionKind.CONSTRUCTED:
225 List<String> names =
226 decoder.getStrings(Key.NAMES, isOptional: true);
227 List<ConstantExpression> arguments =
228 decoder.getConstants(Key.ARGUMENTS, isOptional: true);
229 return new ConstructedConstantExpression(
230 decoder.getType(Key.TYPE),
231 decoder.getElement(Key.ELEMENT),
232 new CallStructure(arguments.length, names),
233 arguments);
234 case ConstantExpressionKind.DOUBLE:
235 return new DoubleConstantExpression(decoder.getDouble(Key.VALUE));
236 case ConstantExpressionKind.ERRONEOUS:
237 break;
238 case ConstantExpressionKind.FUNCTION:
239 return new FunctionConstantExpression(
240 decoder.getElement(Key.ELEMENT));
241 case ConstantExpressionKind.IDENTICAL:
242 return new IdenticalConstantExpression(
243 decoder.getConstant(Key.LEFT),
244 decoder.getConstant(Key.RIGHT));
245 case ConstantExpressionKind.INT:
246 return new IntConstantExpression(decoder.getInt(Key.VALUE));
247 case ConstantExpressionKind.INT_FROM_ENVIRONMENT:
248 return new IntFromEnvironmentConstantExpression(
249 decoder.getConstant(Key.NAME),
250 decoder.getConstant(Key.DEFAULT, isOptional: true));
251 case ConstantExpressionKind.LIST:
252 return new ListConstantExpression(
253 decoder.getType(Key.TYPE),
254 decoder.getConstants(Key.VALUES));
255 case ConstantExpressionKind.MAP:
256 return new MapConstantExpression(
257 decoder.getType(Key.TYPE),
258 decoder.getConstants(Key.KEYS),
259 decoder.getConstants(Key.VALUES));
260 case ConstantExpressionKind.NULL:
261 return new NullConstantExpression();
262 case ConstantExpressionKind.STRING:
263 return new StringConstantExpression(
264 decoder.getString(Key.VALUE));
265 case ConstantExpressionKind.STRING_FROM_ENVIRONMENT:
266 return new StringFromEnvironmentConstantExpression(
267 decoder.getConstant(Key.NAME),
268 decoder.getConstant(Key.DEFAULT, isOptional: true));
269 case ConstantExpressionKind.STRING_LENGTH:
270 return new StringLengthConstantExpression(
271 decoder.getConstant(Key.EXPRESSION));
272 case ConstantExpressionKind.SYMBOL:
273 break;
274 case ConstantExpressionKind.TYPE:
275 return new TypeConstantExpression(decoder.getType(Key.TYPE));
276 case ConstantExpressionKind.UNARY:
277 UnaryOperator operator = UnaryOperator.fromKind(
278 decoder.getEnum(Key.OPERATOR, UnaryOperatorKind.values));
279 return new UnaryConstantExpression(
280 operator,
281 decoder.getConstant(Key.EXPRESSION));
282 case ConstantExpressionKind.VARIABLE:
283 return new VariableConstantExpression(
284 decoder.getElement(Key.ELEMENT));
285
286 case ConstantExpressionKind.POSITIONAL_REFERENCE:
287 return new PositionalArgumentReference(
288 decoder.getInt(Key.INDEX));
289 case ConstantExpressionKind.NAMED_REFERENCE:
290 return new NamedArgumentReference(
291 decoder.getString(Key.NAME));
292 case ConstantExpressionKind.DEFERRED:
293 case ConstantExpressionKind.SYNTHETIC:
294 }
295 throw new UnsupportedError(
296 "Unexpected constant kind: ${kind} in $decoder");
297 }
298 }
299
300 /// Visitor that serializes a [ConstantConstructor] by encoding it into an
301 /// [ObjectEncoder].
302 ///
303 /// This class is called from the [ConstructorSerializer] when the [Serializer]
304 /// is serializing constant constructor. The [ObjectEncoder] ensures that any
305 /// [Element], [DartType], and [ConstantExpression] that the serialized
306 /// [ConstantConstructor] depends upon are also serialized.
307 class ConstantConstructorSerializer
308 extends ConstantConstructorVisitor<dynamic, ObjectEncoder> {
309 const ConstantConstructorSerializer();
310
311 @override
312 void visit(ConstantConstructor constantConstructor,
313 ObjectEncoder encoder) {
314 encoder.setEnum(Key.KIND, constantConstructor.kind);
315 constantConstructor.accept(this, encoder);
316 }
317
318 @override
319 void visitGenerative(
320 GenerativeConstantConstructor constructor,
321 ObjectEncoder encoder) {
322 encoder.setType(Key.TYPE, constructor.type);
323 MapEncoder defaults = encoder.createMap(Key.DEFAULTS);
324 constructor.defaultValues.forEach((key, e) {
325 defaults.setConstant('$key', e);
326 });
327 ListEncoder fields = encoder.createList(Key.FIELDS);
328 constructor.fieldMap.forEach((FieldElement f, ConstantExpression e) {
329 ObjectEncoder fieldSerializer = fields.createObject();
330 fieldSerializer.setElement(Key.FIELD, f);
331 fieldSerializer.setConstant(Key.CONSTANT, e);
332 });
333 if (constructor.superConstructorInvocation != null) {
334 encoder.setConstant(Key.CONSTRUCTOR,
335 constructor.superConstructorInvocation);
336 }
337 }
338
339 @override
340 void visitRedirectingFactory(
341 RedirectingFactoryConstantConstructor constructor,
342 ObjectEncoder encoder) {
343 encoder.setConstant(Key.CONSTRUCTOR,
344 constructor.targetConstructorInvocation);
345 }
346
347 @override
348 void visitRedirectingGenerative(
349 RedirectingGenerativeConstantConstructor constructor,
350 ObjectEncoder encoder) {
351 MapEncoder defaults = encoder.createMap(Key.DEFAULTS);
352 constructor.defaultValues.forEach((key, ConstantExpression e) {
353 defaults.setConstant('$key', e);
354 });
355 encoder.setConstant(Key.CONSTRUCTOR,
356 constructor.thisConstructorInvocation);
357 }
358 }
359 /// Utility class for deserializing [ConstantConstructor]s.
360 ///
361 /// This is used by the [ConstructorElementZ].
362 class ConstantConstructorDeserializer {
363 /// Deserializes a [ConstantConstructor] from an [ObjectDecoder].
364 ///
365 /// The class is called from the [Deserializer] when a constant constructor
366 /// needs deserialization. The [ObjectDecoder] ensures that any [Element],
367 /// [DartType], and [ConstantExpression] that the deserialized
368 /// [ConstantConstructor] depends upon are available.
369 static ConstantConstructor deserialize(ObjectDecoder decoder) {
370
371 ConstantConstructorKind kind =
372 decoder.getEnum(Key.KIND, ConstantConstructorKind.values);
373
374 DartType readType() {
375 return decoder.getType(Key.TYPE);
376 }
377
378 Map<dynamic/*int|String*/, ConstantExpression> readDefaults() {
379 Map<dynamic, ConstantExpression> defaultValues =
380 <dynamic, ConstantExpression>{};
381 if (decoder.containsKey(Key.DEFAULTS)) {
382 MapDecoder defaultsMap = decoder.getMap(Key.DEFAULTS);
383 defaultsMap.forEachKey((String key) {
384 int index = int.parse(key, onError: (_) => null);
385 if (index != null) {
386 defaultValues[index] = defaultsMap.getConstant(key);
387 } else {
388 defaultValues[key] = defaultsMap.getConstant(key);
389 }
390 });
391 }
392 return defaultValues;
393 }
394
395 Map<FieldElement, ConstantExpression> readFields() {
396 Map<FieldElement, ConstantExpression> fieldMap =
397 <FieldElement, ConstantExpression>{};
398 if (decoder.containsKey(Key.FIELDS)) {
399 ListDecoder fieldsList = decoder.getList(Key.FIELDS);
400 for (int i = 0; i < fieldsList.length; i++) {
401 ObjectDecoder object = fieldsList.getObject(i);
402 FieldElement field = object.getElement(Key.FIELD);
403 ConstantExpression constant = object.getConstant(Key.CONSTANT);
404 fieldMap[field] = constant;
405 }
406 }
407 return fieldMap;
408 }
409
410 ConstructedConstantExpression readConstructorInvocation() {
411 return decoder.getConstant(Key.CONSTRUCTOR, isOptional: true);
412 }
413
414 switch (kind) {
415 case ConstantConstructorKind.GENERATIVE:
416 return new GenerativeConstantConstructor(
417 readType(),
418 readDefaults(),
419 readFields(),
420 readConstructorInvocation());
421 case ConstantConstructorKind.REDIRECTING_GENERATIVE:
422 return new RedirectingGenerativeConstantConstructor(
423 readDefaults(),
424 readConstructorInvocation());
425 case ConstantConstructorKind.REDIRECTING_FACTORY:
426 return new RedirectingFactoryConstantConstructor(
427 readConstructorInvocation());
428 }
429 }
430 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698