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

Side by Side Diff: pkg/analyzer/lib/src/summary/summarize_const_expr.dart

Issue 1859723002: Support for serializing prefix/postfix increment/decrement. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/src/summary/format.dart'; 9 import 'package:analyzer/src/summary/format.dart';
10 import 'package:analyzer/src/summary/idl.dart'; 10 import 'package:analyzer/src/summary/idl.dart';
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 156 }
157 return new UnlinkedConstBuilder( 157 return new UnlinkedConstBuilder(
158 operations: operations, 158 operations: operations,
159 assignmentOperators: assignmentOperators, 159 assignmentOperators: assignmentOperators,
160 ints: ints, 160 ints: ints,
161 doubles: doubles, 161 doubles: doubles,
162 strings: strings, 162 strings: strings,
163 references: references); 163 references: references);
164 } 164 }
165 165
166 /**
167 * Push the given assignable [expr] and return the kind of assignment
168 * operation that should be used.
169 */
170 UnlinkedConstOperation _pushAssignable(Expression expr) {
171 if (_isIdentifierSequence(expr)) {
172 EntityRefBuilder ref = serializeIdentifierSequence(expr);
173 references.add(ref);
174 return UnlinkedConstOperation.assignToRef;
175 } else if (expr is PropertyAccess) {
176 _serialize(expr.target);
177 strings.add(expr.propertyName.name);
178 return UnlinkedConstOperation.assignToProperty;
179 } else if (expr is IndexExpression) {
180 _serialize(expr.target);
181 _serialize(expr.index);
182 return UnlinkedConstOperation.assignToIndex;
183 } else {
184 throw new StateError('Unsupported assignable: $expr');
185 }
186 }
187
166 void _pushInt(int value) { 188 void _pushInt(int value) {
167 assert(value >= 0); 189 assert(value >= 0);
168 if (value >= (1 << 32)) { 190 if (value >= (1 << 32)) {
169 int numOfComponents = 0; 191 int numOfComponents = 0;
170 ints.add(numOfComponents); 192 ints.add(numOfComponents);
171 void pushComponents(int value) { 193 void pushComponents(int value) {
172 if (value >= (1 << 32)) { 194 if (value >= (1 << 32)) {
173 pushComponents(value >> 32); 195 pushComponents(value >> 32);
174 } 196 }
175 numOfComponents++; 197 numOfComponents++;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 operations.add(UnlinkedConstOperation.identical); 259 operations.add(UnlinkedConstOperation.identical);
238 } else if (expr is BinaryExpression) { 260 } else if (expr is BinaryExpression) {
239 _serializeBinaryExpression(expr); 261 _serializeBinaryExpression(expr);
240 } else if (expr is ConditionalExpression) { 262 } else if (expr is ConditionalExpression) {
241 _serialize(expr.condition); 263 _serialize(expr.condition);
242 _serialize(expr.thenExpression); 264 _serialize(expr.thenExpression);
243 _serialize(expr.elseExpression); 265 _serialize(expr.elseExpression);
244 operations.add(UnlinkedConstOperation.conditional); 266 operations.add(UnlinkedConstOperation.conditional);
245 } else if (expr is PrefixExpression) { 267 } else if (expr is PrefixExpression) {
246 _serializePrefixExpression(expr); 268 _serializePrefixExpression(expr);
269 } else if (expr is PostfixExpression) {
270 _serializePostfixExpression(expr);
247 } else if (expr is PropertyAccess) { 271 } else if (expr is PropertyAccess) {
248 _serializePropertyAccess(expr); 272 _serializePropertyAccess(expr);
249 } else if (expr is ParenthesizedExpression) { 273 } else if (expr is ParenthesizedExpression) {
250 _serialize(expr.expression); 274 _serialize(expr.expression);
251 } else if (expr is IndexExpression) { 275 } else if (expr is IndexExpression) {
252 _serialize(expr.target); 276 _serialize(expr.target);
253 _serialize(expr.index); 277 _serialize(expr.index);
254 operations.add(UnlinkedConstOperation.extractIndex); 278 operations.add(UnlinkedConstOperation.extractIndex);
255 } else if (expr is AssignmentExpression) { 279 } else if (expr is AssignmentExpression) {
256 _serializeAssignment(expr); 280 _serializeAssignment(expr);
257 } else { 281 } else {
258 throw new StateError('Unknown expression type: $expr'); 282 throw new StateError('Unknown expression type: $expr');
259 } 283 }
260 } 284 }
261 285
262 void _serializeAssignment(AssignmentExpression expr) { 286 void _serializeAssignment(AssignmentExpression expr) {
263 // Push the assignment operator. 287 // Push the assignment operator.
264 TokenType operator = expr.operator.type; 288 TokenType operator = expr.operator.type;
289 UnlinkedExprAssignOperator assignmentOperator;
265 if (operator == TokenType.EQ) { 290 if (operator == TokenType.EQ) {
266 assignmentOperators.add(UnlinkedExprAssignOperator.assign); 291 assignmentOperator = UnlinkedExprAssignOperator.assign;
267 } else if (operator == TokenType.QUESTION_QUESTION_EQ) { 292 } else if (operator == TokenType.QUESTION_QUESTION_EQ) {
268 assignmentOperators.add(UnlinkedExprAssignOperator.ifNull); 293 assignmentOperator = UnlinkedExprAssignOperator.ifNull;
269 } else if (operator == TokenType.STAR_EQ) { 294 } else if (operator == TokenType.STAR_EQ) {
270 assignmentOperators.add(UnlinkedExprAssignOperator.multiply); 295 assignmentOperator = UnlinkedExprAssignOperator.multiply;
271 } else if (operator == TokenType.SLASH_EQ) { 296 } else if (operator == TokenType.SLASH_EQ) {
272 assignmentOperators.add(UnlinkedExprAssignOperator.divide); 297 assignmentOperator = UnlinkedExprAssignOperator.divide;
273 } else if (operator == TokenType.TILDE_SLASH_EQ) { 298 } else if (operator == TokenType.TILDE_SLASH_EQ) {
274 assignmentOperators.add(UnlinkedExprAssignOperator.floorDivide); 299 assignmentOperator = UnlinkedExprAssignOperator.floorDivide;
275 } else if (operator == TokenType.PERCENT_EQ) { 300 } else if (operator == TokenType.PERCENT_EQ) {
276 assignmentOperators.add(UnlinkedExprAssignOperator.modulo); 301 assignmentOperator = UnlinkedExprAssignOperator.modulo;
277 } else if (operator == TokenType.PLUS_EQ) { 302 } else if (operator == TokenType.PLUS_EQ) {
278 assignmentOperators.add(UnlinkedExprAssignOperator.plus); 303 assignmentOperator = UnlinkedExprAssignOperator.plus;
279 } else if (operator == TokenType.MINUS_EQ) { 304 } else if (operator == TokenType.MINUS_EQ) {
280 assignmentOperators.add(UnlinkedExprAssignOperator.minus); 305 assignmentOperator = UnlinkedExprAssignOperator.minus;
281 } else if (operator == TokenType.LT_LT_EQ) { 306 } else if (operator == TokenType.LT_LT_EQ) {
282 assignmentOperators.add(UnlinkedExprAssignOperator.shiftLeft); 307 assignmentOperator = UnlinkedExprAssignOperator.shiftLeft;
283 } else if (operator == TokenType.GT_GT_EQ) { 308 } else if (operator == TokenType.GT_GT_EQ) {
284 assignmentOperators.add(UnlinkedExprAssignOperator.shiftRight); 309 assignmentOperator = UnlinkedExprAssignOperator.shiftRight;
285 } else if (operator == TokenType.AMPERSAND_EQ) { 310 } else if (operator == TokenType.AMPERSAND_EQ) {
286 assignmentOperators.add(UnlinkedExprAssignOperator.bitAnd); 311 assignmentOperator = UnlinkedExprAssignOperator.bitAnd;
287 } else if (operator == TokenType.CARET_EQ) { 312 } else if (operator == TokenType.CARET_EQ) {
288 assignmentOperators.add(UnlinkedExprAssignOperator.bitXor); 313 assignmentOperator = UnlinkedExprAssignOperator.bitXor;
289 } else if (operator == TokenType.BAR_EQ) { 314 } else if (operator == TokenType.BAR_EQ) {
290 assignmentOperators.add(UnlinkedExprAssignOperator.bitOr); 315 assignmentOperator = UnlinkedExprAssignOperator.bitOr;
291 } else { 316 } else {
292 throw new StateError('Unknown assignment operator: $operator'); 317 throw new StateError('Unknown assignment operator: $operator');
293 } 318 }
319 assignmentOperators.add(assignmentOperator);
294 // Push the target and prepare the assignment operation. 320 // Push the target and prepare the assignment operation.
295 Expression leftHandSide = expr.leftHandSide; 321 Expression leftHandSide = expr.leftHandSide;
296 UnlinkedConstOperation assignOperation; 322 UnlinkedConstOperation assignOperation = _pushAssignable(leftHandSide);
297 if (_isIdentifierSequence(leftHandSide)) {
298 EntityRefBuilder ref = serializeIdentifierSequence(leftHandSide);
299 references.add(ref);
300 assignOperation = UnlinkedConstOperation.assignToRef;
301 } else if (leftHandSide is PropertyAccess) {
302 _serialize(leftHandSide.target);
303 strings.add(leftHandSide.propertyName.name);
304 assignOperation = UnlinkedConstOperation.assignToProperty;
305 } else if (leftHandSide is IndexExpression) {
306 _serialize(leftHandSide.target);
307 _serialize(leftHandSide.index);
308 assignOperation = UnlinkedConstOperation.assignToIndex;
309 } else {
310 throw new StateError('Unsupported LHS: $leftHandSide');
311 }
312 // Push the value. 323 // Push the value.
313 _serialize(expr.rightHandSide); 324 _serialize(expr.rightHandSide);
314 // Push the target-specific assignment operation. 325 // Push the target-specific assignment operation.
315 operations.add(assignOperation); 326 operations.add(assignOperation);
316 } 327 }
317 328
318 void _serializeBinaryExpression(BinaryExpression expr) { 329 void _serializeBinaryExpression(BinaryExpression expr) {
319 _serialize(expr.leftOperand); 330 _serialize(expr.leftOperand);
320 _serialize(expr.rightOperand); 331 _serialize(expr.rightOperand);
321 TokenType operator = expr.operator.type; 332 TokenType operator = expr.operator.type;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 if (expr.typeArguments != null && 395 if (expr.typeArguments != null &&
385 expr.typeArguments.arguments.length == 2) { 396 expr.typeArguments.arguments.length == 2) {
386 references.add(serializeType(expr.typeArguments.arguments[0])); 397 references.add(serializeType(expr.typeArguments.arguments[0]));
387 references.add(serializeType(expr.typeArguments.arguments[1])); 398 references.add(serializeType(expr.typeArguments.arguments[1]));
388 operations.add(UnlinkedConstOperation.makeTypedMap); 399 operations.add(UnlinkedConstOperation.makeTypedMap);
389 } else { 400 } else {
390 operations.add(UnlinkedConstOperation.makeUntypedMap); 401 operations.add(UnlinkedConstOperation.makeUntypedMap);
391 } 402 }
392 } 403 }
393 404
394 void _serializePrefixExpression(PrefixExpression expr) { 405 void _serializePostfixExpression(PostfixExpression expr) {
395 _serialize(expr.operand);
396 TokenType operator = expr.operator.type; 406 TokenType operator = expr.operator.type;
397 if (operator == TokenType.BANG) { 407 Expression operand = expr.operand;
398 operations.add(UnlinkedConstOperation.not); 408 if (operator == TokenType.PLUS_PLUS) {
399 } else if (operator == TokenType.MINUS) { 409 _serializePrefixPostfixIncDec(
400 operations.add(UnlinkedConstOperation.negate); 410 operand, UnlinkedExprAssignOperator.postfixIncrement);
401 } else if (operator == TokenType.TILDE) { 411 } else if (operator == TokenType.MINUS_MINUS) {
402 operations.add(UnlinkedConstOperation.complement); 412 _serializePrefixPostfixIncDec(
413 operand, UnlinkedExprAssignOperator.postfixDecrement);
403 } else { 414 } else {
404 throw new StateError('Unknown operator: $operator'); 415 throw new StateError('Unknown operator: $operator');
405 } 416 }
406 } 417 }
407 418
419 void _serializePrefixExpression(PrefixExpression expr) {
420 TokenType operator = expr.operator.type;
421 Expression operand = expr.operand;
422 if (operator == TokenType.BANG) {
423 _serialize(operand);
424 operations.add(UnlinkedConstOperation.not);
425 } else if (operator == TokenType.MINUS) {
426 _serialize(operand);
427 operations.add(UnlinkedConstOperation.negate);
428 } else if (operator == TokenType.TILDE) {
429 _serialize(operand);
430 operations.add(UnlinkedConstOperation.complement);
431 } else if (operator == TokenType.PLUS_PLUS) {
432 _serializePrefixPostfixIncDec(
433 operand, UnlinkedExprAssignOperator.prefixIncrement);
434 } else if (operator == TokenType.MINUS_MINUS) {
435 _serializePrefixPostfixIncDec(
436 operand, UnlinkedExprAssignOperator.prefixDecrement);
437 } else {
438 throw new StateError('Unknown operator: $operator');
439 }
440 }
441
442 void _serializePrefixPostfixIncDec(
443 Expression operand, UnlinkedExprAssignOperator operator) {
444 assignmentOperators.add(operator);
445 UnlinkedConstOperation assignOperation = _pushAssignable(operand);
446 operations.add(assignOperation);
447 }
448
408 void _serializePropertyAccess(PropertyAccess expr) { 449 void _serializePropertyAccess(PropertyAccess expr) {
409 if (_isIdentifierSequence(expr)) { 450 if (_isIdentifierSequence(expr)) {
410 EntityRefBuilder ref = serializeIdentifierSequence(expr); 451 EntityRefBuilder ref = serializeIdentifierSequence(expr);
411 references.add(ref); 452 references.add(ref);
412 operations.add(UnlinkedConstOperation.pushReference); 453 operations.add(UnlinkedConstOperation.pushReference);
413 } else { 454 } else {
414 _serialize(expr.target); 455 _serialize(expr.target);
415 strings.add(expr.propertyName.name); 456 strings.add(expr.propertyName.name);
416 operations.add(UnlinkedConstOperation.extractProperty); 457 operations.add(UnlinkedConstOperation.extractProperty);
417 } 458 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 expr = (expr as PrefixedIdentifier).prefix; 498 expr = (expr as PrefixedIdentifier).prefix;
458 } else if (expr is PropertyAccess) { 499 } else if (expr is PropertyAccess) {
459 expr = (expr as PropertyAccess).target; 500 expr = (expr as PropertyAccess).target;
460 } else { 501 } else {
461 return false; 502 return false;
462 } 503 }
463 } 504 }
464 return false; 505 return false;
465 } 506 }
466 } 507 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | pkg/analyzer/test/src/summary/summary_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698