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

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

Issue 2669703003: Refactor ConstantExpression/ConstantConstructor to use entities. (Closed)
Patch Set: Created 3 years, 10 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
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 '../elements/resolution_types.dart'; 9 import '../elements/resolution_types.dart';
10 import '../elements/elements.dart' show FieldElement; 10 import '../elements/elements.dart'
11 show ConstructorElement, FieldElement, LocalVariableElement, MethodElement;
11 import '../resolution/operators.dart'; 12 import '../resolution/operators.dart';
12 import '../universe/call_structure.dart' show CallStructure; 13 import '../universe/call_structure.dart' show CallStructure;
13 import 'keys.dart'; 14 import 'keys.dart';
14 import 'serialization.dart'; 15 import 'serialization.dart';
15 16
16 /// Visitor that serializes a [ConstantExpression] by encoding it into an 17 /// Visitor that serializes a [ConstantExpression] by encoding it into an
17 /// [ObjectEncoder]. 18 /// [ObjectEncoder].
18 /// 19 ///
19 /// This class is called from the [Serializer] when a [ConstantExpression] needs 20 /// This class is called from the [Serializer] when a [ConstantExpression] needs
20 /// serialization. The [ObjectEncoder] ensures that any [Element], 21 /// serialization. The [ObjectEncoder] ensures that any [Element],
(...skipping 20 matching lines...) Expand all
41 void visitConditional( 42 void visitConditional(
42 ConditionalConstantExpression exp, ObjectEncoder encoder) { 43 ConditionalConstantExpression exp, ObjectEncoder encoder) {
43 encoder.setConstant(Key.CONDITION, exp.condition); 44 encoder.setConstant(Key.CONDITION, exp.condition);
44 encoder.setConstant(Key.TRUE, exp.trueExp); 45 encoder.setConstant(Key.TRUE, exp.trueExp);
45 encoder.setConstant(Key.FALSE, exp.falseExp); 46 encoder.setConstant(Key.FALSE, exp.falseExp);
46 } 47 }
47 48
48 @override 49 @override
49 void visitConstructed( 50 void visitConstructed(
50 ConstructedConstantExpression exp, ObjectEncoder encoder) { 51 ConstructedConstantExpression exp, ObjectEncoder encoder) {
51 encoder.setElement(Key.ELEMENT, exp.target); 52 ConstructorElement constructor = exp.target;
52 encoder.setType(Key.TYPE, exp.type); 53 ResolutionInterfaceType type = exp.type;
54 encoder.setElement(Key.ELEMENT, constructor);
55 encoder.setType(Key.TYPE, type);
53 encoder.setStrings(Key.NAMES, exp.callStructure.namedArguments); 56 encoder.setStrings(Key.NAMES, exp.callStructure.namedArguments);
54 encoder.setConstants(Key.ARGUMENTS, exp.arguments); 57 encoder.setConstants(Key.ARGUMENTS, exp.arguments);
55 } 58 }
56 59
57 @override 60 @override
58 void visitFunction(FunctionConstantExpression exp, ObjectEncoder encoder) { 61 void visitFunction(FunctionConstantExpression exp, ObjectEncoder encoder) {
59 encoder.setElement(Key.ELEMENT, exp.element); 62 MethodElement function = exp.element;
63 encoder.setElement(Key.ELEMENT, function);
60 } 64 }
61 65
62 @override 66 @override
63 void visitIdentical(IdenticalConstantExpression exp, ObjectEncoder encoder) { 67 void visitIdentical(IdenticalConstantExpression exp, ObjectEncoder encoder) {
64 encoder.setConstant(Key.LEFT, exp.left); 68 encoder.setConstant(Key.LEFT, exp.left);
65 encoder.setConstant(Key.RIGHT, exp.right); 69 encoder.setConstant(Key.RIGHT, exp.right);
66 } 70 }
67 71
68 @override 72 @override
69 void visitList(ListConstantExpression exp, ObjectEncoder encoder) { 73 void visitList(ListConstantExpression exp, ObjectEncoder encoder) {
70 encoder.setType(Key.TYPE, exp.type); 74 ResolutionInterfaceType type = exp.type;
75 encoder.setType(Key.TYPE, type);
71 encoder.setConstants(Key.VALUES, exp.values); 76 encoder.setConstants(Key.VALUES, exp.values);
72 } 77 }
73 78
74 @override 79 @override
75 void visitMap(MapConstantExpression exp, ObjectEncoder encoder) { 80 void visitMap(MapConstantExpression exp, ObjectEncoder encoder) {
76 encoder.setType(Key.TYPE, exp.type); 81 ResolutionInterfaceType type = exp.type;
82 encoder.setType(Key.TYPE, type);
77 encoder.setConstants(Key.KEYS, exp.keys); 83 encoder.setConstants(Key.KEYS, exp.keys);
78 encoder.setConstants(Key.VALUES, exp.values); 84 encoder.setConstants(Key.VALUES, exp.values);
79 } 85 }
80 86
81 @override 87 @override
82 void visitBool(BoolConstantExpression exp, ObjectEncoder encoder) { 88 void visitBool(BoolConstantExpression exp, ObjectEncoder encoder) {
83 encoder.setBool(Key.VALUE, exp.primitiveValue); 89 encoder.setBool(Key.VALUE, exp.primitiveValue);
84 } 90 }
85 91
86 @override 92 @override
(...skipping 17 matching lines...) Expand all
104 } 110 }
105 111
106 @override 112 @override
107 void visitSymbol(SymbolConstantExpression exp, ObjectEncoder encoder) { 113 void visitSymbol(SymbolConstantExpression exp, ObjectEncoder encoder) {
108 encoder.setString(Key.NAME, exp.name); 114 encoder.setString(Key.NAME, exp.name);
109 } 115 }
110 116
111 @override 117 @override
112 void visitType(TypeConstantExpression exp, ObjectEncoder encoder) { 118 void visitType(TypeConstantExpression exp, ObjectEncoder encoder) {
113 encoder.setType(Key.TYPE, exp.type); 119 encoder.setType(Key.TYPE, exp.type);
120 encoder.setString(Key.NAME, exp.name);
114 } 121 }
115 122
116 @override 123 @override
117 void visitUnary(UnaryConstantExpression exp, ObjectEncoder encoder) { 124 void visitUnary(UnaryConstantExpression exp, ObjectEncoder encoder) {
118 encoder.setEnum(Key.OPERATOR, exp.operator.kind); 125 encoder.setEnum(Key.OPERATOR, exp.operator.kind);
119 encoder.setConstant(Key.EXPRESSION, exp.expression); 126 encoder.setConstant(Key.EXPRESSION, exp.expression);
120 } 127 }
121 128
122 @override 129 @override
123 void visitVariable(VariableConstantExpression exp, ObjectEncoder encoder) { 130 void visitField(FieldConstantExpression exp, ObjectEncoder encoder) {
124 encoder.setElement(Key.ELEMENT, exp.element); 131 FieldElement field = exp.element;
132 encoder.setElement(Key.ELEMENT, field);
125 } 133 }
126 134
127 @override 135 @override
136 void visitLocalVariable(
137 LocalVariableConstantExpression exp, ObjectEncoder encoder) {
138 LocalVariableElement local = exp.element;
139 encoder.setElement(Key.ELEMENT, local);
140 }
141
142 @override
128 void visitPositional(PositionalArgumentReference exp, ObjectEncoder encoder) { 143 void visitPositional(PositionalArgumentReference exp, ObjectEncoder encoder) {
129 encoder.setInt(Key.INDEX, exp.index); 144 encoder.setInt(Key.INDEX, exp.index);
130 } 145 }
131 146
132 @override 147 @override
133 void visitNamed(NamedArgumentReference exp, ObjectEncoder encoder) { 148 void visitNamed(NamedArgumentReference exp, ObjectEncoder encoder) {
134 encoder.setString(Key.NAME, exp.name); 149 encoder.setString(Key.NAME, exp.name);
135 } 150 }
136 151
137 @override 152 @override
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 decoder.getConstant(Key.DEFAULT, isOptional: true)); 216 decoder.getConstant(Key.DEFAULT, isOptional: true));
202 case ConstantExpressionKind.CONCATENATE: 217 case ConstantExpressionKind.CONCATENATE:
203 return new ConcatenateConstantExpression( 218 return new ConcatenateConstantExpression(
204 decoder.getConstants(Key.ARGUMENTS)); 219 decoder.getConstants(Key.ARGUMENTS));
205 case ConstantExpressionKind.CONDITIONAL: 220 case ConstantExpressionKind.CONDITIONAL:
206 return new ConditionalConstantExpression( 221 return new ConditionalConstantExpression(
207 decoder.getConstant(Key.CONDITION), 222 decoder.getConstant(Key.CONDITION),
208 decoder.getConstant(Key.TRUE), 223 decoder.getConstant(Key.TRUE),
209 decoder.getConstant(Key.FALSE)); 224 decoder.getConstant(Key.FALSE));
210 case ConstantExpressionKind.CONSTRUCTED: 225 case ConstantExpressionKind.CONSTRUCTED:
226 ResolutionInterfaceType type = decoder.getType(Key.TYPE);
227 ConstructorElement constructor = decoder.getElement(Key.ELEMENT);
211 List<String> names = decoder.getStrings(Key.NAMES, isOptional: true); 228 List<String> names = decoder.getStrings(Key.NAMES, isOptional: true);
212 List<ConstantExpression> arguments = 229 List<ConstantExpression> arguments =
213 decoder.getConstants(Key.ARGUMENTS, isOptional: true); 230 decoder.getConstants(Key.ARGUMENTS, isOptional: true);
214 return new ConstructedConstantExpression( 231 return new ConstructedConstantExpression(type, constructor,
215 decoder.getType(Key.TYPE), 232 new CallStructure(arguments.length, names), arguments);
216 decoder.getElement(Key.ELEMENT),
217 new CallStructure(arguments.length, names),
218 arguments);
219 case ConstantExpressionKind.DOUBLE: 233 case ConstantExpressionKind.DOUBLE:
220 return new DoubleConstantExpression(decoder.getDouble(Key.VALUE)); 234 return new DoubleConstantExpression(decoder.getDouble(Key.VALUE));
221 case ConstantExpressionKind.ERRONEOUS: 235 case ConstantExpressionKind.ERRONEOUS:
222 break; 236 break;
223 case ConstantExpressionKind.FUNCTION: 237 case ConstantExpressionKind.FUNCTION:
224 return new FunctionConstantExpression(decoder.getElement(Key.ELEMENT)); 238 MethodElement function = decoder.getElement(Key.ELEMENT);
239 return new FunctionConstantExpression(function, function.type);
225 case ConstantExpressionKind.IDENTICAL: 240 case ConstantExpressionKind.IDENTICAL:
226 return new IdenticalConstantExpression( 241 return new IdenticalConstantExpression(
227 decoder.getConstant(Key.LEFT), decoder.getConstant(Key.RIGHT)); 242 decoder.getConstant(Key.LEFT), decoder.getConstant(Key.RIGHT));
228 case ConstantExpressionKind.INT: 243 case ConstantExpressionKind.INT:
229 return new IntConstantExpression(decoder.getInt(Key.VALUE)); 244 return new IntConstantExpression(decoder.getInt(Key.VALUE));
230 case ConstantExpressionKind.INT_FROM_ENVIRONMENT: 245 case ConstantExpressionKind.INT_FROM_ENVIRONMENT:
231 return new IntFromEnvironmentConstantExpression( 246 return new IntFromEnvironmentConstantExpression(
232 decoder.getConstant(Key.NAME), 247 decoder.getConstant(Key.NAME),
233 decoder.getConstant(Key.DEFAULT, isOptional: true)); 248 decoder.getConstant(Key.DEFAULT, isOptional: true));
234 case ConstantExpressionKind.LIST: 249 case ConstantExpressionKind.LIST:
235 return new ListConstantExpression(decoder.getType(Key.TYPE), 250 ResolutionInterfaceType type = decoder.getType(Key.TYPE);
236 decoder.getConstants(Key.VALUES, isOptional: true)); 251 return new ListConstantExpression(
252 type, decoder.getConstants(Key.VALUES, isOptional: true));
237 case ConstantExpressionKind.MAP: 253 case ConstantExpressionKind.MAP:
254 ResolutionInterfaceType type = decoder.getType(Key.TYPE);
238 return new MapConstantExpression( 255 return new MapConstantExpression(
239 decoder.getType(Key.TYPE), 256 type,
240 decoder.getConstants(Key.KEYS, isOptional: true), 257 decoder.getConstants(Key.KEYS, isOptional: true),
241 decoder.getConstants(Key.VALUES, isOptional: true)); 258 decoder.getConstants(Key.VALUES, isOptional: true));
242 case ConstantExpressionKind.NULL: 259 case ConstantExpressionKind.NULL:
243 return new NullConstantExpression(); 260 return new NullConstantExpression();
244 case ConstantExpressionKind.STRING: 261 case ConstantExpressionKind.STRING:
245 return new StringConstantExpression(decoder.getString(Key.VALUE)); 262 return new StringConstantExpression(decoder.getString(Key.VALUE));
246 case ConstantExpressionKind.STRING_FROM_ENVIRONMENT: 263 case ConstantExpressionKind.STRING_FROM_ENVIRONMENT:
247 return new StringFromEnvironmentConstantExpression( 264 return new StringFromEnvironmentConstantExpression(
248 decoder.getConstant(Key.NAME), 265 decoder.getConstant(Key.NAME),
249 decoder.getConstant(Key.DEFAULT, isOptional: true)); 266 decoder.getConstant(Key.DEFAULT, isOptional: true));
250 case ConstantExpressionKind.STRING_LENGTH: 267 case ConstantExpressionKind.STRING_LENGTH:
251 return new StringLengthConstantExpression( 268 return new StringLengthConstantExpression(
252 decoder.getConstant(Key.EXPRESSION)); 269 decoder.getConstant(Key.EXPRESSION));
253 case ConstantExpressionKind.SYMBOL: 270 case ConstantExpressionKind.SYMBOL:
254 return new SymbolConstantExpression(decoder.getString(Key.NAME)); 271 return new SymbolConstantExpression(decoder.getString(Key.NAME));
255 case ConstantExpressionKind.TYPE: 272 case ConstantExpressionKind.TYPE:
256 return new TypeConstantExpression(decoder.getType(Key.TYPE)); 273 return new TypeConstantExpression(
274 decoder.getType(Key.TYPE), decoder.getString(Key.NAME));
257 case ConstantExpressionKind.UNARY: 275 case ConstantExpressionKind.UNARY:
258 UnaryOperator operator = UnaryOperator 276 UnaryOperator operator = UnaryOperator
259 .fromKind(decoder.getEnum(Key.OPERATOR, UnaryOperatorKind.values)); 277 .fromKind(decoder.getEnum(Key.OPERATOR, UnaryOperatorKind.values));
260 return new UnaryConstantExpression( 278 return new UnaryConstantExpression(
261 operator, decoder.getConstant(Key.EXPRESSION)); 279 operator, decoder.getConstant(Key.EXPRESSION));
262 case ConstantExpressionKind.VARIABLE: 280 case ConstantExpressionKind.FIELD:
263 return new VariableConstantExpression(decoder.getElement(Key.ELEMENT)); 281 FieldElement field = decoder.getElement(Key.ELEMENT);
282 return new FieldConstantExpression(field);
283 case ConstantExpressionKind.LOCAL_VARIABLE:
284 LocalVariableElement local = decoder.getElement(Key.ELEMENT);
285 return new LocalVariableConstantExpression(local);
264 286
265 case ConstantExpressionKind.POSITIONAL_REFERENCE: 287 case ConstantExpressionKind.POSITIONAL_REFERENCE:
266 return new PositionalArgumentReference(decoder.getInt(Key.INDEX)); 288 return new PositionalArgumentReference(decoder.getInt(Key.INDEX));
267 case ConstantExpressionKind.NAMED_REFERENCE: 289 case ConstantExpressionKind.NAMED_REFERENCE:
268 return new NamedArgumentReference(decoder.getString(Key.NAME)); 290 return new NamedArgumentReference(decoder.getString(Key.NAME));
269 case ConstantExpressionKind.DEFERRED: 291 case ConstantExpressionKind.DEFERRED:
270 return new DeferredConstantExpression( 292 return new DeferredConstantExpression(
271 decoder.getConstant(Key.EXPRESSION), 293 decoder.getConstant(Key.EXPRESSION),
272 decoder.getElement(Key.PREFIX)); 294 decoder.getElement(Key.PREFIX));
273 case ConstantExpressionKind.SYNTHETIC: 295 case ConstantExpressionKind.SYNTHETIC:
(...skipping 15 matching lines...) Expand all
289 311
290 @override 312 @override
291 void visit(ConstantConstructor constantConstructor, ObjectEncoder encoder) { 313 void visit(ConstantConstructor constantConstructor, ObjectEncoder encoder) {
292 encoder.setEnum(Key.KIND, constantConstructor.kind); 314 encoder.setEnum(Key.KIND, constantConstructor.kind);
293 constantConstructor.accept(this, encoder); 315 constantConstructor.accept(this, encoder);
294 } 316 }
295 317
296 @override 318 @override
297 void visitGenerative( 319 void visitGenerative(
298 GenerativeConstantConstructor constructor, ObjectEncoder encoder) { 320 GenerativeConstantConstructor constructor, ObjectEncoder encoder) {
299 encoder.setType(Key.TYPE, constructor.type); 321 ResolutionInterfaceType type = constructor.type;
322 encoder.setType(Key.TYPE, type);
300 MapEncoder defaults = encoder.createMap(Key.DEFAULTS); 323 MapEncoder defaults = encoder.createMap(Key.DEFAULTS);
301 constructor.defaultValues.forEach((key, e) { 324 constructor.defaultValues.forEach((key, e) {
302 defaults.setConstant('$key', e); 325 defaults.setConstant('$key', e);
303 }); 326 });
304 ListEncoder fields = encoder.createList(Key.FIELDS); 327 ListEncoder fields = encoder.createList(Key.FIELDS);
305 constructor.fieldMap.forEach((FieldElement f, ConstantExpression e) { 328 constructor.fieldMap.forEach((FieldElement f, ConstantExpression e) {
306 ObjectEncoder fieldSerializer = fields.createObject(); 329 ObjectEncoder fieldSerializer = fields.createObject();
307 fieldSerializer.setElement(Key.FIELD, f); 330 fieldSerializer.setElement(Key.FIELD, f);
308 fieldSerializer.setConstant(Key.CONSTANT, e); 331 fieldSerializer.setConstant(Key.CONSTANT, e);
309 }); 332 });
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 } 405 }
383 return fieldMap; 406 return fieldMap;
384 } 407 }
385 408
386 ConstructedConstantExpression readConstructorInvocation() { 409 ConstructedConstantExpression readConstructorInvocation() {
387 return decoder.getConstant(Key.CONSTRUCTOR, isOptional: true); 410 return decoder.getConstant(Key.CONSTRUCTOR, isOptional: true);
388 } 411 }
389 412
390 switch (kind) { 413 switch (kind) {
391 case ConstantConstructorKind.GENERATIVE: 414 case ConstantConstructorKind.GENERATIVE:
392 return new GenerativeConstantConstructor(readType(), readDefaults(), 415 ResolutionInterfaceType type = readType();
393 readFields(), readConstructorInvocation()); 416 return new GenerativeConstantConstructor(
417 type, readDefaults(), readFields(), readConstructorInvocation());
394 case ConstantConstructorKind.REDIRECTING_GENERATIVE: 418 case ConstantConstructorKind.REDIRECTING_GENERATIVE:
395 return new RedirectingGenerativeConstantConstructor( 419 return new RedirectingGenerativeConstantConstructor(
396 readDefaults(), readConstructorInvocation()); 420 readDefaults(), readConstructorInvocation());
397 case ConstantConstructorKind.REDIRECTING_FACTORY: 421 case ConstantConstructorKind.REDIRECTING_FACTORY:
398 return new RedirectingFactoryConstantConstructor( 422 return new RedirectingFactoryConstantConstructor(
399 readConstructorInvocation()); 423 readConstructorInvocation());
400 } 424 }
401 } 425 }
402 } 426 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/resolution/resolution.dart ('k') | pkg/compiler/lib/src/serialization/equivalence.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698