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