OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 serialization.summarize_const_expr; | 5 library serialization.summarize_const_expr; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
9 import 'package:analyzer/dart/element/type.dart' show DartType; | 9 import 'package:analyzer/dart/element/type.dart' show DartType; |
10 import 'package:analyzer/src/summary/format.dart'; | 10 import 'package:analyzer/src/summary/format.dart'; |
11 import 'package:analyzer/src/summary/idl.dart'; | 11 import 'package:analyzer/src/summary/idl.dart'; |
12 | 12 |
13 /** | 13 /** |
14 * Serialize the given constructor initializer [node]. | 14 * Serialize the given constructor initializer [node]. |
15 */ | 15 */ |
16 UnlinkedConstructorInitializer serializeConstructorInitializer( | 16 UnlinkedConstructorInitializer serializeConstructorInitializer( |
17 ConstructorInitializer node, | 17 ConstructorInitializer node, |
18 UnlinkedConstBuilder serializeConstExpr(Expression expr)) { | 18 UnlinkedExprBuilder serializeConstExpr(Expression expr)) { |
19 if (node is ConstructorFieldInitializer) { | 19 if (node is ConstructorFieldInitializer) { |
20 return new UnlinkedConstructorInitializerBuilder( | 20 return new UnlinkedConstructorInitializerBuilder( |
21 kind: UnlinkedConstructorInitializerKind.field, | 21 kind: UnlinkedConstructorInitializerKind.field, |
22 name: node.fieldName.name, | 22 name: node.fieldName.name, |
23 expression: serializeConstExpr(node.expression)); | 23 expression: serializeConstExpr(node.expression)); |
24 } | 24 } |
25 | 25 |
26 List<UnlinkedConstBuilder> arguments = <UnlinkedConstBuilder>[]; | 26 List<UnlinkedExprBuilder> arguments = <UnlinkedExprBuilder>[]; |
27 List<String> argumentNames = <String>[]; | 27 List<String> argumentNames = <String>[]; |
28 void serializeArguments(List<Expression> args) { | 28 void serializeArguments(List<Expression> args) { |
29 for (Expression arg in args) { | 29 for (Expression arg in args) { |
30 if (arg is NamedExpression) { | 30 if (arg is NamedExpression) { |
31 NamedExpression namedExpression = arg; | 31 NamedExpression namedExpression = arg; |
32 argumentNames.add(namedExpression.name.label.name); | 32 argumentNames.add(namedExpression.name.label.name); |
33 arg = namedExpression.expression; | 33 arg = namedExpression.expression; |
34 } | 34 } |
35 arguments.add(serializeConstExpr(arg)); | 35 arguments.add(serializeConstExpr(arg)); |
36 } | 36 } |
(...skipping 17 matching lines...) Expand all Loading... |
54 } | 54 } |
55 throw new StateError('Unexpected initializer type ${node.runtimeType}'); | 55 throw new StateError('Unexpected initializer type ${node.runtimeType}'); |
56 } | 56 } |
57 | 57 |
58 /** | 58 /** |
59 * Instances of this class keep track of intermediate state during | 59 * Instances of this class keep track of intermediate state during |
60 * serialization of a single constant [Expression]. | 60 * serialization of a single constant [Expression]. |
61 */ | 61 */ |
62 abstract class AbstractConstExprSerializer { | 62 abstract class AbstractConstExprSerializer { |
63 /** | 63 /** |
64 * See [UnlinkedConstBuilder.isValidConst]. | 64 * See [UnlinkedExprBuilder.isValidConst]. |
65 */ | 65 */ |
66 bool isValidConst = true; | 66 bool isValidConst = true; |
67 | 67 |
68 /** | 68 /** |
69 * See [UnlinkedConstBuilder.nmae]. | 69 * See [UnlinkedExprBuilder.nmae]. |
70 */ | 70 */ |
71 String name = null; | 71 String name = null; |
72 | 72 |
73 /** | 73 /** |
74 * See [UnlinkedConstBuilder.operations]. | 74 * See [UnlinkedExprBuilder.operations]. |
75 */ | 75 */ |
76 final List<UnlinkedConstOperation> operations = <UnlinkedConstOperation>[]; | 76 final List<UnlinkedExprOperation> operations = <UnlinkedExprOperation>[]; |
77 | 77 |
78 /** | 78 /** |
79 * See [UnlinkedConstBuilder.assignmentOperators]. | 79 * See [UnlinkedExprBuilder.assignmentOperators]. |
80 */ | 80 */ |
81 final List<UnlinkedExprAssignOperator> assignmentOperators = | 81 final List<UnlinkedExprAssignOperator> assignmentOperators = |
82 <UnlinkedExprAssignOperator>[]; | 82 <UnlinkedExprAssignOperator>[]; |
83 | 83 |
84 /** | 84 /** |
85 * See [UnlinkedConstBuilder.ints]. | 85 * See [UnlinkedExprBuilder.ints]. |
86 */ | 86 */ |
87 final List<int> ints = <int>[]; | 87 final List<int> ints = <int>[]; |
88 | 88 |
89 /** | 89 /** |
90 * See [UnlinkedConstBuilder.doubles]. | 90 * See [UnlinkedExprBuilder.doubles]. |
91 */ | 91 */ |
92 final List<double> doubles = <double>[]; | 92 final List<double> doubles = <double>[]; |
93 | 93 |
94 /** | 94 /** |
95 * See [UnlinkedConstBuilder.strings]. | 95 * See [UnlinkedExprBuilder.strings]. |
96 */ | 96 */ |
97 final List<String> strings = <String>[]; | 97 final List<String> strings = <String>[]; |
98 | 98 |
99 /** | 99 /** |
100 * See [UnlinkedConstBuilder.references]. | 100 * See [UnlinkedExprBuilder.references]. |
101 */ | 101 */ |
102 final List<EntityRefBuilder> references = <EntityRefBuilder>[]; | 102 final List<EntityRefBuilder> references = <EntityRefBuilder>[]; |
103 | 103 |
104 /** | 104 /** |
105 * Return `true` if the given [name] is a parameter reference. | 105 * Return `true` if the given [name] is a parameter reference. |
106 */ | 106 */ |
107 bool isParameterName(String name); | 107 bool isParameterName(String name); |
108 | 108 |
109 /** | 109 /** |
110 * Serialize the given [expr] expression into this serializer state. | 110 * Serialize the given [expr] expression into this serializer state. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 /** | 156 /** |
157 * Return [EntityRefBuilder] that corresponds to the given [expr], which | 157 * Return [EntityRefBuilder] that corresponds to the given [expr], which |
158 * must be a sequence of identifiers. | 158 * must be a sequence of identifiers. |
159 */ | 159 */ |
160 EntityRefBuilder serializeIdentifierSequence(Expression expr); | 160 EntityRefBuilder serializeIdentifierSequence(Expression expr); |
161 | 161 |
162 void serializeInstanceCreation( | 162 void serializeInstanceCreation( |
163 EntityRefBuilder constructor, ArgumentList argumentList) { | 163 EntityRefBuilder constructor, ArgumentList argumentList) { |
164 _serializeArguments(argumentList); | 164 _serializeArguments(argumentList); |
165 references.add(constructor); | 165 references.add(constructor); |
166 operations.add(UnlinkedConstOperation.invokeConstructor); | 166 operations.add(UnlinkedExprOperation.invokeConstructor); |
167 } | 167 } |
168 | 168 |
169 /** | 169 /** |
170 * Return [EntityRefBuilder] that corresponds to the [type] with the given | 170 * Return [EntityRefBuilder] that corresponds to the [type] with the given |
171 * [name] and [arguments]. It is expected that [type] corresponds to the | 171 * [name] and [arguments]. It is expected that [type] corresponds to the |
172 * given [name] and [arguments]. The parameter [type] might be `null` if the | 172 * given [name] and [arguments]. The parameter [type] might be `null` if the |
173 * type is not resolved. | 173 * type is not resolved. |
174 */ | 174 */ |
175 EntityRefBuilder serializeType( | 175 EntityRefBuilder serializeType( |
176 DartType type, Identifier name, TypeArgumentList arguments); | 176 DartType type, Identifier name, TypeArgumentList arguments); |
177 | 177 |
178 /** | 178 /** |
179 * Return [EntityRefBuilder] that corresponds to the given [type]. | 179 * Return [EntityRefBuilder] that corresponds to the given [type]. |
180 */ | 180 */ |
181 EntityRefBuilder serializeTypeName(TypeName type) { | 181 EntityRefBuilder serializeTypeName(TypeName type) { |
182 return serializeType(type?.type, type?.name, type?.typeArguments); | 182 return serializeType(type?.type, type?.name, type?.typeArguments); |
183 } | 183 } |
184 | 184 |
185 /** | 185 /** |
186 * Return the [UnlinkedConstBuilder] that corresponds to the state of this | 186 * Return the [UnlinkedExprBuilder] that corresponds to the state of this |
187 * serializer. | 187 * serializer. |
188 */ | 188 */ |
189 UnlinkedConstBuilder toBuilder() { | 189 UnlinkedExprBuilder toBuilder() { |
190 return new UnlinkedConstBuilder( | 190 return new UnlinkedExprBuilder( |
191 isValidConst: isValidConst, | 191 isValidConst: isValidConst, |
192 operations: operations, | 192 operations: operations, |
193 assignmentOperators: assignmentOperators, | 193 assignmentOperators: assignmentOperators, |
194 ints: ints, | 194 ints: ints, |
195 doubles: doubles, | 195 doubles: doubles, |
196 strings: strings, | 196 strings: strings, |
197 references: references); | 197 references: references); |
198 } | 198 } |
199 | 199 |
200 /** | 200 /** |
(...skipping 24 matching lines...) Expand all Loading... |
225 return false; | 225 return false; |
226 } | 226 } |
227 | 227 |
228 /** | 228 /** |
229 * Push the operation for the given assignable [expr]. | 229 * Push the operation for the given assignable [expr]. |
230 */ | 230 */ |
231 void _pushAssignable(Expression expr) { | 231 void _pushAssignable(Expression expr) { |
232 if (_isIdentifierSequence(expr)) { | 232 if (_isIdentifierSequence(expr)) { |
233 EntityRefBuilder ref = serializeIdentifierSequence(expr); | 233 EntityRefBuilder ref = serializeIdentifierSequence(expr); |
234 references.add(ref); | 234 references.add(ref); |
235 operations.add(UnlinkedConstOperation.assignToRef); | 235 operations.add(UnlinkedExprOperation.assignToRef); |
236 } else if (expr is PropertyAccess) { | 236 } else if (expr is PropertyAccess) { |
237 if (!expr.isCascaded) { | 237 if (!expr.isCascaded) { |
238 _serialize(expr.target); | 238 _serialize(expr.target); |
239 } | 239 } |
240 strings.add(expr.propertyName.name); | 240 strings.add(expr.propertyName.name); |
241 operations.add(UnlinkedConstOperation.assignToProperty); | 241 operations.add(UnlinkedExprOperation.assignToProperty); |
242 } else if (expr is IndexExpression) { | 242 } else if (expr is IndexExpression) { |
243 if (!expr.isCascaded) { | 243 if (!expr.isCascaded) { |
244 _serialize(expr.target); | 244 _serialize(expr.target); |
245 } | 245 } |
246 _serialize(expr.index); | 246 _serialize(expr.index); |
247 operations.add(UnlinkedConstOperation.assignToIndex); | 247 operations.add(UnlinkedExprOperation.assignToIndex); |
248 } else if (expr is PrefixedIdentifier) { | 248 } else if (expr is PrefixedIdentifier) { |
249 strings.add(expr.prefix.name); | 249 strings.add(expr.prefix.name); |
250 operations.add(UnlinkedConstOperation.pushParameter); | 250 operations.add(UnlinkedExprOperation.pushParameter); |
251 strings.add(expr.identifier.name); | 251 strings.add(expr.identifier.name); |
252 operations.add(UnlinkedConstOperation.assignToProperty); | 252 operations.add(UnlinkedExprOperation.assignToProperty); |
253 } else { | 253 } else { |
254 throw new StateError('Unsupported assignable: $expr'); | 254 throw new StateError('Unsupported assignable: $expr'); |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 void _pushInt(int value) { | 258 void _pushInt(int value) { |
259 assert(value >= 0); | 259 assert(value >= 0); |
260 if (value >= (1 << 32)) { | 260 if (value >= (1 << 32)) { |
261 int numOfComponents = 0; | 261 int numOfComponents = 0; |
262 ints.add(numOfComponents); | 262 ints.add(numOfComponents); |
263 void pushComponents(int value) { | 263 void pushComponents(int value) { |
264 if (value >= (1 << 32)) { | 264 if (value >= (1 << 32)) { |
265 pushComponents(value >> 32); | 265 pushComponents(value >> 32); |
266 } | 266 } |
267 numOfComponents++; | 267 numOfComponents++; |
268 ints.add(value & 0xFFFFFFFF); | 268 ints.add(value & 0xFFFFFFFF); |
269 } | 269 } |
| 270 |
270 pushComponents(value); | 271 pushComponents(value); |
271 ints[ints.length - 1 - numOfComponents] = numOfComponents; | 272 ints[ints.length - 1 - numOfComponents] = numOfComponents; |
272 operations.add(UnlinkedConstOperation.pushLongInt); | 273 operations.add(UnlinkedExprOperation.pushLongInt); |
273 } else { | 274 } else { |
274 operations.add(UnlinkedConstOperation.pushInt); | 275 operations.add(UnlinkedExprOperation.pushInt); |
275 ints.add(value); | 276 ints.add(value); |
276 } | 277 } |
277 } | 278 } |
278 | 279 |
279 /** | 280 /** |
280 * Serialize the given [expr] expression into this serializer state. | 281 * Serialize the given [expr] expression into this serializer state. |
281 */ | 282 */ |
282 void _serialize(Expression expr) { | 283 void _serialize(Expression expr) { |
283 if (expr is IntegerLiteral) { | 284 if (expr is IntegerLiteral) { |
284 _pushInt(expr.value); | 285 _pushInt(expr.value); |
285 } else if (expr is DoubleLiteral) { | 286 } else if (expr is DoubleLiteral) { |
286 operations.add(UnlinkedConstOperation.pushDouble); | 287 operations.add(UnlinkedExprOperation.pushDouble); |
287 doubles.add(expr.value); | 288 doubles.add(expr.value); |
288 } else if (expr is BooleanLiteral) { | 289 } else if (expr is BooleanLiteral) { |
289 if (expr.value) { | 290 if (expr.value) { |
290 operations.add(UnlinkedConstOperation.pushTrue); | 291 operations.add(UnlinkedExprOperation.pushTrue); |
291 } else { | 292 } else { |
292 operations.add(UnlinkedConstOperation.pushFalse); | 293 operations.add(UnlinkedExprOperation.pushFalse); |
293 } | 294 } |
294 } else if (expr is StringLiteral) { | 295 } else if (expr is StringLiteral) { |
295 _serializeString(expr); | 296 _serializeString(expr); |
296 } else if (expr is SymbolLiteral) { | 297 } else if (expr is SymbolLiteral) { |
297 strings.add(expr.components.map((token) => token.lexeme).join('.')); | 298 strings.add(expr.components.map((token) => token.lexeme).join('.')); |
298 operations.add(UnlinkedConstOperation.makeSymbol); | 299 operations.add(UnlinkedExprOperation.makeSymbol); |
299 } else if (expr is NullLiteral) { | 300 } else if (expr is NullLiteral) { |
300 operations.add(UnlinkedConstOperation.pushNull); | 301 operations.add(UnlinkedExprOperation.pushNull); |
301 } else if (expr is Identifier) { | 302 } else if (expr is Identifier) { |
302 if (expr is SimpleIdentifier && isParameterName(expr.name)) { | 303 if (expr is SimpleIdentifier && isParameterName(expr.name)) { |
303 strings.add(expr.name); | 304 strings.add(expr.name); |
304 operations.add(UnlinkedConstOperation.pushParameter); | 305 operations.add(UnlinkedExprOperation.pushParameter); |
305 } else if (expr is PrefixedIdentifier && | 306 } else if (expr is PrefixedIdentifier && |
306 isParameterName(expr.prefix.name)) { | 307 isParameterName(expr.prefix.name)) { |
307 strings.add(expr.prefix.name); | 308 strings.add(expr.prefix.name); |
308 operations.add(UnlinkedConstOperation.pushParameter); | 309 operations.add(UnlinkedExprOperation.pushParameter); |
309 strings.add(expr.identifier.name); | 310 strings.add(expr.identifier.name); |
310 operations.add(UnlinkedConstOperation.extractProperty); | 311 operations.add(UnlinkedExprOperation.extractProperty); |
311 } else { | 312 } else { |
312 references.add(serializeIdentifier(expr)); | 313 references.add(serializeIdentifier(expr)); |
313 operations.add(UnlinkedConstOperation.pushReference); | 314 operations.add(UnlinkedExprOperation.pushReference); |
314 } | 315 } |
315 } else if (expr is InstanceCreationExpression) { | 316 } else if (expr is InstanceCreationExpression) { |
316 if (!expr.isConst) { | 317 if (!expr.isConst) { |
317 isValidConst = false; | 318 isValidConst = false; |
318 } | 319 } |
319 TypeName typeName = expr.constructorName.type; | 320 TypeName typeName = expr.constructorName.type; |
320 serializeInstanceCreation( | 321 serializeInstanceCreation( |
321 serializeConstructorRef(typeName.type, typeName.name, | 322 serializeConstructorRef(typeName.type, typeName.name, |
322 typeName.typeArguments, expr.constructorName.name), | 323 typeName.typeArguments, expr.constructorName.name), |
323 expr.argumentList); | 324 expr.argumentList); |
324 } else if (expr is ListLiteral) { | 325 } else if (expr is ListLiteral) { |
325 _serializeListLiteral(expr); | 326 _serializeListLiteral(expr); |
326 } else if (expr is MapLiteral) { | 327 } else if (expr is MapLiteral) { |
327 _serializeMapLiteral(expr); | 328 _serializeMapLiteral(expr); |
328 } else if (expr is MethodInvocation) { | 329 } else if (expr is MethodInvocation) { |
329 _serializeMethodInvocation(expr); | 330 _serializeMethodInvocation(expr); |
330 } else if (expr is BinaryExpression) { | 331 } else if (expr is BinaryExpression) { |
331 _serializeBinaryExpression(expr); | 332 _serializeBinaryExpression(expr); |
332 } else if (expr is ConditionalExpression) { | 333 } else if (expr is ConditionalExpression) { |
333 _serialize(expr.condition); | 334 _serialize(expr.condition); |
334 _serialize(expr.thenExpression); | 335 _serialize(expr.thenExpression); |
335 _serialize(expr.elseExpression); | 336 _serialize(expr.elseExpression); |
336 operations.add(UnlinkedConstOperation.conditional); | 337 operations.add(UnlinkedExprOperation.conditional); |
337 } else if (expr is PrefixExpression) { | 338 } else if (expr is PrefixExpression) { |
338 _serializePrefixExpression(expr); | 339 _serializePrefixExpression(expr); |
339 } else if (expr is PostfixExpression) { | 340 } else if (expr is PostfixExpression) { |
340 _serializePostfixExpression(expr); | 341 _serializePostfixExpression(expr); |
341 } else if (expr is PropertyAccess) { | 342 } else if (expr is PropertyAccess) { |
342 _serializePropertyAccess(expr); | 343 _serializePropertyAccess(expr); |
343 } else if (expr is ParenthesizedExpression) { | 344 } else if (expr is ParenthesizedExpression) { |
344 _serialize(expr.expression); | 345 _serialize(expr.expression); |
345 } else if (expr is IndexExpression) { | 346 } else if (expr is IndexExpression) { |
346 isValidConst = false; | 347 isValidConst = false; |
347 _serialize(expr.target); | 348 _serialize(expr.target); |
348 _serialize(expr.index); | 349 _serialize(expr.index); |
349 operations.add(UnlinkedConstOperation.extractIndex); | 350 operations.add(UnlinkedExprOperation.extractIndex); |
350 } else if (expr is AssignmentExpression) { | 351 } else if (expr is AssignmentExpression) { |
351 _serializeAssignment(expr); | 352 _serializeAssignment(expr); |
352 } else if (expr is CascadeExpression) { | 353 } else if (expr is CascadeExpression) { |
353 _serializeCascadeExpression(expr); | 354 _serializeCascadeExpression(expr); |
354 } else if (expr is FunctionExpression) { | 355 } else if (expr is FunctionExpression) { |
355 isValidConst = false; | 356 isValidConst = false; |
356 List<int> indices = serializeFunctionExpression(expr); | 357 List<int> indices = serializeFunctionExpression(expr); |
357 if (indices != null) { | 358 if (indices != null) { |
358 ints.addAll(serializeFunctionExpression(expr)); | 359 ints.addAll(serializeFunctionExpression(expr)); |
359 operations.add(UnlinkedConstOperation.pushLocalFunctionReference); | 360 operations.add(UnlinkedExprOperation.pushLocalFunctionReference); |
360 } else { | 361 } else { |
361 // Invalid expression; just push null. | 362 // Invalid expression; just push null. |
362 operations.add(UnlinkedConstOperation.pushNull); | 363 operations.add(UnlinkedExprOperation.pushNull); |
363 } | 364 } |
364 } else if (expr is FunctionExpressionInvocation) { | 365 } else if (expr is FunctionExpressionInvocation) { |
365 isValidConst = false; | 366 isValidConst = false; |
366 // TODO(scheglov) implement | 367 // TODO(scheglov) implement |
367 operations.add(UnlinkedConstOperation.pushNull); | 368 operations.add(UnlinkedExprOperation.pushNull); |
368 } else if (expr is AsExpression) { | 369 } else if (expr is AsExpression) { |
369 isValidConst = false; | 370 isValidConst = false; |
370 _serialize(expr.expression); | 371 _serialize(expr.expression); |
371 references.add(serializeTypeName(expr.type)); | 372 references.add(serializeTypeName(expr.type)); |
372 operations.add(UnlinkedConstOperation.typeCast); | 373 operations.add(UnlinkedExprOperation.typeCast); |
373 } else if (expr is IsExpression) { | 374 } else if (expr is IsExpression) { |
374 isValidConst = false; | 375 isValidConst = false; |
375 _serialize(expr.expression); | 376 _serialize(expr.expression); |
376 references.add(serializeTypeName(expr.type)); | 377 references.add(serializeTypeName(expr.type)); |
377 operations.add(UnlinkedConstOperation.typeCheck); | 378 operations.add(UnlinkedExprOperation.typeCheck); |
378 } else if (expr is ThrowExpression) { | 379 } else if (expr is ThrowExpression) { |
379 isValidConst = false; | 380 isValidConst = false; |
380 _serialize(expr.expression); | 381 _serialize(expr.expression); |
381 operations.add(UnlinkedConstOperation.throwException); | 382 operations.add(UnlinkedExprOperation.throwException); |
382 } else { | 383 } else { |
383 throw new StateError('Unknown expression type: $expr'); | 384 throw new StateError('Unknown expression type: $expr'); |
384 } | 385 } |
385 } | 386 } |
386 | 387 |
387 void _serializeArguments(ArgumentList argumentList) { | 388 void _serializeArguments(ArgumentList argumentList) { |
388 List<Expression> arguments = argumentList.arguments; | 389 List<Expression> arguments = argumentList.arguments; |
389 // Serialize the arguments. | 390 // Serialize the arguments. |
390 List<String> argumentNames = <String>[]; | 391 List<String> argumentNames = <String>[]; |
391 arguments.forEach((arg) { | 392 arguments.forEach((arg) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 assignmentOperators.add(assignmentOperator); | 442 assignmentOperators.add(assignmentOperator); |
442 // Push the assignment to the LHS. | 443 // Push the assignment to the LHS. |
443 _pushAssignable(expr.leftHandSide); | 444 _pushAssignable(expr.leftHandSide); |
444 } | 445 } |
445 | 446 |
446 void _serializeBinaryExpression(BinaryExpression expr) { | 447 void _serializeBinaryExpression(BinaryExpression expr) { |
447 _serialize(expr.leftOperand); | 448 _serialize(expr.leftOperand); |
448 _serialize(expr.rightOperand); | 449 _serialize(expr.rightOperand); |
449 TokenType operator = expr.operator.type; | 450 TokenType operator = expr.operator.type; |
450 if (operator == TokenType.EQ_EQ) { | 451 if (operator == TokenType.EQ_EQ) { |
451 operations.add(UnlinkedConstOperation.equal); | 452 operations.add(UnlinkedExprOperation.equal); |
452 } else if (operator == TokenType.BANG_EQ) { | 453 } else if (operator == TokenType.BANG_EQ) { |
453 operations.add(UnlinkedConstOperation.notEqual); | 454 operations.add(UnlinkedExprOperation.notEqual); |
454 } else if (operator == TokenType.AMPERSAND_AMPERSAND) { | 455 } else if (operator == TokenType.AMPERSAND_AMPERSAND) { |
455 operations.add(UnlinkedConstOperation.and); | 456 operations.add(UnlinkedExprOperation.and); |
456 } else if (operator == TokenType.BAR_BAR) { | 457 } else if (operator == TokenType.BAR_BAR) { |
457 operations.add(UnlinkedConstOperation.or); | 458 operations.add(UnlinkedExprOperation.or); |
458 } else if (operator == TokenType.CARET) { | 459 } else if (operator == TokenType.CARET) { |
459 operations.add(UnlinkedConstOperation.bitXor); | 460 operations.add(UnlinkedExprOperation.bitXor); |
460 } else if (operator == TokenType.AMPERSAND) { | 461 } else if (operator == TokenType.AMPERSAND) { |
461 operations.add(UnlinkedConstOperation.bitAnd); | 462 operations.add(UnlinkedExprOperation.bitAnd); |
462 } else if (operator == TokenType.BAR) { | 463 } else if (operator == TokenType.BAR) { |
463 operations.add(UnlinkedConstOperation.bitOr); | 464 operations.add(UnlinkedExprOperation.bitOr); |
464 } else if (operator == TokenType.GT_GT) { | 465 } else if (operator == TokenType.GT_GT) { |
465 operations.add(UnlinkedConstOperation.bitShiftRight); | 466 operations.add(UnlinkedExprOperation.bitShiftRight); |
466 } else if (operator == TokenType.LT_LT) { | 467 } else if (operator == TokenType.LT_LT) { |
467 operations.add(UnlinkedConstOperation.bitShiftLeft); | 468 operations.add(UnlinkedExprOperation.bitShiftLeft); |
468 } else if (operator == TokenType.PLUS) { | 469 } else if (operator == TokenType.PLUS) { |
469 operations.add(UnlinkedConstOperation.add); | 470 operations.add(UnlinkedExprOperation.add); |
470 } else if (operator == TokenType.MINUS) { | 471 } else if (operator == TokenType.MINUS) { |
471 operations.add(UnlinkedConstOperation.subtract); | 472 operations.add(UnlinkedExprOperation.subtract); |
472 } else if (operator == TokenType.STAR) { | 473 } else if (operator == TokenType.STAR) { |
473 operations.add(UnlinkedConstOperation.multiply); | 474 operations.add(UnlinkedExprOperation.multiply); |
474 } else if (operator == TokenType.SLASH) { | 475 } else if (operator == TokenType.SLASH) { |
475 operations.add(UnlinkedConstOperation.divide); | 476 operations.add(UnlinkedExprOperation.divide); |
476 } else if (operator == TokenType.TILDE_SLASH) { | 477 } else if (operator == TokenType.TILDE_SLASH) { |
477 operations.add(UnlinkedConstOperation.floorDivide); | 478 operations.add(UnlinkedExprOperation.floorDivide); |
478 } else if (operator == TokenType.GT) { | 479 } else if (operator == TokenType.GT) { |
479 operations.add(UnlinkedConstOperation.greater); | 480 operations.add(UnlinkedExprOperation.greater); |
480 } else if (operator == TokenType.LT) { | 481 } else if (operator == TokenType.LT) { |
481 operations.add(UnlinkedConstOperation.less); | 482 operations.add(UnlinkedExprOperation.less); |
482 } else if (operator == TokenType.GT_EQ) { | 483 } else if (operator == TokenType.GT_EQ) { |
483 operations.add(UnlinkedConstOperation.greaterEqual); | 484 operations.add(UnlinkedExprOperation.greaterEqual); |
484 } else if (operator == TokenType.LT_EQ) { | 485 } else if (operator == TokenType.LT_EQ) { |
485 operations.add(UnlinkedConstOperation.lessEqual); | 486 operations.add(UnlinkedExprOperation.lessEqual); |
486 } else if (operator == TokenType.PERCENT) { | 487 } else if (operator == TokenType.PERCENT) { |
487 operations.add(UnlinkedConstOperation.modulo); | 488 operations.add(UnlinkedExprOperation.modulo); |
488 } else { | 489 } else { |
489 throw new StateError('Unknown operator: $operator'); | 490 throw new StateError('Unknown operator: $operator'); |
490 } | 491 } |
491 } | 492 } |
492 | 493 |
493 void _serializeCascadeExpression(CascadeExpression expr) { | 494 void _serializeCascadeExpression(CascadeExpression expr) { |
494 _serialize(expr.target); | 495 _serialize(expr.target); |
495 for (Expression section in expr.cascadeSections) { | 496 for (Expression section in expr.cascadeSections) { |
496 operations.add(UnlinkedConstOperation.cascadeSectionBegin); | 497 operations.add(UnlinkedExprOperation.cascadeSectionBegin); |
497 _serialize(section); | 498 _serialize(section); |
498 operations.add(UnlinkedConstOperation.cascadeSectionEnd); | 499 operations.add(UnlinkedExprOperation.cascadeSectionEnd); |
499 } | 500 } |
500 } | 501 } |
501 | 502 |
502 void _serializeListLiteral(ListLiteral expr) { | 503 void _serializeListLiteral(ListLiteral expr) { |
503 List<Expression> elements = expr.elements; | 504 List<Expression> elements = expr.elements; |
504 elements.forEach(_serialize); | 505 elements.forEach(_serialize); |
505 ints.add(elements.length); | 506 ints.add(elements.length); |
506 if (expr.typeArguments != null && | 507 if (expr.typeArguments != null && |
507 expr.typeArguments.arguments.length == 1) { | 508 expr.typeArguments.arguments.length == 1) { |
508 references.add(serializeTypeName(expr.typeArguments.arguments[0])); | 509 references.add(serializeTypeName(expr.typeArguments.arguments[0])); |
509 operations.add(UnlinkedConstOperation.makeTypedList); | 510 operations.add(UnlinkedExprOperation.makeTypedList); |
510 } else { | 511 } else { |
511 operations.add(UnlinkedConstOperation.makeUntypedList); | 512 operations.add(UnlinkedExprOperation.makeUntypedList); |
512 } | 513 } |
513 } | 514 } |
514 | 515 |
515 void _serializeMapLiteral(MapLiteral expr) { | 516 void _serializeMapLiteral(MapLiteral expr) { |
516 for (MapLiteralEntry entry in expr.entries) { | 517 for (MapLiteralEntry entry in expr.entries) { |
517 _serialize(entry.key); | 518 _serialize(entry.key); |
518 _serialize(entry.value); | 519 _serialize(entry.value); |
519 } | 520 } |
520 ints.add(expr.entries.length); | 521 ints.add(expr.entries.length); |
521 if (expr.typeArguments != null && | 522 if (expr.typeArguments != null && |
522 expr.typeArguments.arguments.length == 2) { | 523 expr.typeArguments.arguments.length == 2) { |
523 references.add(serializeTypeName(expr.typeArguments.arguments[0])); | 524 references.add(serializeTypeName(expr.typeArguments.arguments[0])); |
524 references.add(serializeTypeName(expr.typeArguments.arguments[1])); | 525 references.add(serializeTypeName(expr.typeArguments.arguments[1])); |
525 operations.add(UnlinkedConstOperation.makeTypedMap); | 526 operations.add(UnlinkedExprOperation.makeTypedMap); |
526 } else { | 527 } else { |
527 operations.add(UnlinkedConstOperation.makeUntypedMap); | 528 operations.add(UnlinkedExprOperation.makeUntypedMap); |
528 } | 529 } |
529 } | 530 } |
530 | 531 |
531 void _serializeMethodInvocation(MethodInvocation invocation) { | 532 void _serializeMethodInvocation(MethodInvocation invocation) { |
532 if (invocation.target != null || | 533 if (invocation.target != null || |
533 invocation.methodName.name != 'identical') { | 534 invocation.methodName.name != 'identical') { |
534 isValidConst = false; | 535 isValidConst = false; |
535 } | 536 } |
536 Expression target = invocation.target; | 537 Expression target = invocation.target; |
537 SimpleIdentifier methodName = invocation.methodName; | 538 SimpleIdentifier methodName = invocation.methodName; |
538 ArgumentList argumentList = invocation.argumentList; | 539 ArgumentList argumentList = invocation.argumentList; |
539 if (_isIdentifierSequence(methodName)) { | 540 if (_isIdentifierSequence(methodName)) { |
540 EntityRefBuilder ref = serializeIdentifierSequence(methodName); | 541 EntityRefBuilder ref = serializeIdentifierSequence(methodName); |
541 _serializeArguments(argumentList); | 542 _serializeArguments(argumentList); |
542 references.add(ref); | 543 references.add(ref); |
543 _serializeTypeArguments(invocation.typeArguments); | 544 _serializeTypeArguments(invocation.typeArguments); |
544 operations.add(UnlinkedConstOperation.invokeMethodRef); | 545 operations.add(UnlinkedExprOperation.invokeMethodRef); |
545 } else { | 546 } else { |
546 if (!invocation.isCascaded) { | 547 if (!invocation.isCascaded) { |
547 _serialize(target); | 548 _serialize(target); |
548 } | 549 } |
549 _serializeArguments(argumentList); | 550 _serializeArguments(argumentList); |
550 strings.add(methodName.name); | 551 strings.add(methodName.name); |
551 _serializeTypeArguments(invocation.typeArguments); | 552 _serializeTypeArguments(invocation.typeArguments); |
552 operations.add(UnlinkedConstOperation.invokeMethod); | 553 operations.add(UnlinkedExprOperation.invokeMethod); |
553 } | 554 } |
554 } | 555 } |
555 | 556 |
556 void _serializePostfixExpression(PostfixExpression expr) { | 557 void _serializePostfixExpression(PostfixExpression expr) { |
557 TokenType operator = expr.operator.type; | 558 TokenType operator = expr.operator.type; |
558 Expression operand = expr.operand; | 559 Expression operand = expr.operand; |
559 if (operator == TokenType.PLUS_PLUS) { | 560 if (operator == TokenType.PLUS_PLUS) { |
560 _serializePrefixPostfixIncDec( | 561 _serializePrefixPostfixIncDec( |
561 operand, UnlinkedExprAssignOperator.postfixIncrement); | 562 operand, UnlinkedExprAssignOperator.postfixIncrement); |
562 } else if (operator == TokenType.MINUS_MINUS) { | 563 } else if (operator == TokenType.MINUS_MINUS) { |
563 _serializePrefixPostfixIncDec( | 564 _serializePrefixPostfixIncDec( |
564 operand, UnlinkedExprAssignOperator.postfixDecrement); | 565 operand, UnlinkedExprAssignOperator.postfixDecrement); |
565 } else { | 566 } else { |
566 throw new StateError('Unknown operator: $operator'); | 567 throw new StateError('Unknown operator: $operator'); |
567 } | 568 } |
568 } | 569 } |
569 | 570 |
570 void _serializePrefixExpression(PrefixExpression expr) { | 571 void _serializePrefixExpression(PrefixExpression expr) { |
571 TokenType operator = expr.operator.type; | 572 TokenType operator = expr.operator.type; |
572 Expression operand = expr.operand; | 573 Expression operand = expr.operand; |
573 if (operator == TokenType.BANG) { | 574 if (operator == TokenType.BANG) { |
574 _serialize(operand); | 575 _serialize(operand); |
575 operations.add(UnlinkedConstOperation.not); | 576 operations.add(UnlinkedExprOperation.not); |
576 } else if (operator == TokenType.MINUS) { | 577 } else if (operator == TokenType.MINUS) { |
577 _serialize(operand); | 578 _serialize(operand); |
578 operations.add(UnlinkedConstOperation.negate); | 579 operations.add(UnlinkedExprOperation.negate); |
579 } else if (operator == TokenType.TILDE) { | 580 } else if (operator == TokenType.TILDE) { |
580 _serialize(operand); | 581 _serialize(operand); |
581 operations.add(UnlinkedConstOperation.complement); | 582 operations.add(UnlinkedExprOperation.complement); |
582 } else if (operator == TokenType.PLUS_PLUS) { | 583 } else if (operator == TokenType.PLUS_PLUS) { |
583 _serializePrefixPostfixIncDec( | 584 _serializePrefixPostfixIncDec( |
584 operand, UnlinkedExprAssignOperator.prefixIncrement); | 585 operand, UnlinkedExprAssignOperator.prefixIncrement); |
585 } else if (operator == TokenType.MINUS_MINUS) { | 586 } else if (operator == TokenType.MINUS_MINUS) { |
586 _serializePrefixPostfixIncDec( | 587 _serializePrefixPostfixIncDec( |
587 operand, UnlinkedExprAssignOperator.prefixDecrement); | 588 operand, UnlinkedExprAssignOperator.prefixDecrement); |
588 } else { | 589 } else { |
589 throw new StateError('Unknown operator: $operator'); | 590 throw new StateError('Unknown operator: $operator'); |
590 } | 591 } |
591 } | 592 } |
592 | 593 |
593 void _serializePrefixPostfixIncDec( | 594 void _serializePrefixPostfixIncDec( |
594 Expression operand, UnlinkedExprAssignOperator operator) { | 595 Expression operand, UnlinkedExprAssignOperator operator) { |
595 isValidConst = false; | 596 isValidConst = false; |
596 assignmentOperators.add(operator); | 597 assignmentOperators.add(operator); |
597 _pushAssignable(operand); | 598 _pushAssignable(operand); |
598 } | 599 } |
599 | 600 |
600 void _serializePropertyAccess(PropertyAccess expr) { | 601 void _serializePropertyAccess(PropertyAccess expr) { |
601 if (_isIdentifierSequence(expr)) { | 602 if (_isIdentifierSequence(expr)) { |
602 EntityRefBuilder ref = serializeIdentifierSequence(expr); | 603 EntityRefBuilder ref = serializeIdentifierSequence(expr); |
603 references.add(ref); | 604 references.add(ref); |
604 operations.add(UnlinkedConstOperation.pushReference); | 605 operations.add(UnlinkedExprOperation.pushReference); |
605 } else { | 606 } else { |
606 _serialize(expr.target); | 607 _serialize(expr.target); |
607 strings.add(expr.propertyName.name); | 608 strings.add(expr.propertyName.name); |
608 operations.add(UnlinkedConstOperation.extractProperty); | 609 operations.add(UnlinkedExprOperation.extractProperty); |
609 } | 610 } |
610 } | 611 } |
611 | 612 |
612 void _serializeString(StringLiteral expr) { | 613 void _serializeString(StringLiteral expr) { |
613 if (expr is AdjacentStrings) { | 614 if (expr is AdjacentStrings) { |
614 if (expr.strings.every((string) => string is SimpleStringLiteral)) { | 615 if (expr.strings.every((string) => string is SimpleStringLiteral)) { |
615 operations.add(UnlinkedConstOperation.pushString); | 616 operations.add(UnlinkedExprOperation.pushString); |
616 strings.add(expr.stringValue); | 617 strings.add(expr.stringValue); |
617 } else { | 618 } else { |
618 expr.strings.forEach(_serializeString); | 619 expr.strings.forEach(_serializeString); |
619 operations.add(UnlinkedConstOperation.concatenate); | 620 operations.add(UnlinkedExprOperation.concatenate); |
620 ints.add(expr.strings.length); | 621 ints.add(expr.strings.length); |
621 } | 622 } |
622 } else if (expr is SimpleStringLiteral) { | 623 } else if (expr is SimpleStringLiteral) { |
623 operations.add(UnlinkedConstOperation.pushString); | 624 operations.add(UnlinkedExprOperation.pushString); |
624 strings.add(expr.value); | 625 strings.add(expr.value); |
625 } else { | 626 } else { |
626 StringInterpolation interpolation = expr as StringInterpolation; | 627 StringInterpolation interpolation = expr as StringInterpolation; |
627 for (InterpolationElement element in interpolation.elements) { | 628 for (InterpolationElement element in interpolation.elements) { |
628 if (element is InterpolationString) { | 629 if (element is InterpolationString) { |
629 operations.add(UnlinkedConstOperation.pushString); | 630 operations.add(UnlinkedExprOperation.pushString); |
630 strings.add(element.value); | 631 strings.add(element.value); |
631 } else { | 632 } else { |
632 _serialize((element as InterpolationExpression).expression); | 633 _serialize((element as InterpolationExpression).expression); |
633 } | 634 } |
634 } | 635 } |
635 operations.add(UnlinkedConstOperation.concatenate); | 636 operations.add(UnlinkedExprOperation.concatenate); |
636 ints.add(interpolation.elements.length); | 637 ints.add(interpolation.elements.length); |
637 } | 638 } |
638 } | 639 } |
639 | 640 |
640 void _serializeTypeArguments(TypeArgumentList typeArguments) { | 641 void _serializeTypeArguments(TypeArgumentList typeArguments) { |
641 if (typeArguments == null) { | 642 if (typeArguments == null) { |
642 ints.add(0); | 643 ints.add(0); |
643 } else { | 644 } else { |
644 ints.add(typeArguments.arguments.length); | 645 ints.add(typeArguments.arguments.length); |
645 for (TypeName typeName in typeArguments.arguments) { | 646 for (TypeName typeName in typeArguments.arguments) { |
646 references.add(serializeTypeName(typeName)); | 647 references.add(serializeTypeName(typeName)); |
647 } | 648 } |
648 } | 649 } |
649 } | 650 } |
650 } | 651 } |
OLD | NEW |