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

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

Issue 1864753002: Use isValidConst flag to distinguish between const / final expressions. (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 28 matching lines...) Expand all
39 } 39 }
40 40
41 /** 41 /**
42 * Instances of this class keep track of intermediate state during 42 * Instances of this class keep track of intermediate state during
43 * serialization of a single constant [Expression]. 43 * serialization of a single constant [Expression].
44 */ 44 */
45 abstract class AbstractConstExprSerializer { 45 abstract class AbstractConstExprSerializer {
46 /** 46 /**
47 * See [UnlinkedConstBuilder.isInvalid]. 47 * See [UnlinkedConstBuilder.isInvalid].
48 */ 48 */
49 bool isInvalid = false; 49 bool isValidConst = true;
50 50
51 /** 51 /**
52 * See [UnlinkedConstBuilder.operations]. 52 * See [UnlinkedConstBuilder.operations].
53 */ 53 */
54 final List<UnlinkedConstOperation> operations = <UnlinkedConstOperation>[]; 54 final List<UnlinkedConstOperation> operations = <UnlinkedConstOperation>[];
55 55
56 /** 56 /**
57 * See [UnlinkedConstBuilder.assignmentOperators]. 57 * See [UnlinkedConstBuilder.assignmentOperators].
58 */ 58 */
59 final List<UnlinkedExprAssignOperator> assignmentOperators = 59 final List<UnlinkedExprAssignOperator> assignmentOperators =
(...skipping 25 matching lines...) Expand all
85 */ 85 */
86 bool isConstructorParameterName(String name); 86 bool isConstructorParameterName(String name);
87 87
88 /** 88 /**
89 * Serialize the given [expr] expression into this serializer state. 89 * Serialize the given [expr] expression into this serializer state.
90 */ 90 */
91 void serialize(Expression expr) { 91 void serialize(Expression expr) {
92 try { 92 try {
93 _serialize(expr); 93 _serialize(expr);
94 } on StateError { 94 } on StateError {
95 isInvalid = true; 95 isValidConst = false;
96 } 96 }
97 } 97 }
98 98
99 /** 99 /**
100 * Serialize the given [annotation] into this serializer state. 100 * Serialize the given [annotation] into this serializer state.
101 */ 101 */
102 void serializeAnnotation(Annotation annotation); 102 void serializeAnnotation(Annotation annotation);
103 103
104 /** 104 /**
105 * Return [EntityRefBuilder] that corresponds to the constructor having name 105 * Return [EntityRefBuilder] that corresponds to the constructor having name
(...skipping 23 matching lines...) Expand all
129 /** 129 /**
130 * Return [EntityRefBuilder] that corresponds to the given [type]. 130 * Return [EntityRefBuilder] that corresponds to the given [type].
131 */ 131 */
132 EntityRefBuilder serializeType(TypeName type); 132 EntityRefBuilder serializeType(TypeName type);
133 133
134 /** 134 /**
135 * Return the [UnlinkedConstBuilder] that corresponds to the state of this 135 * Return the [UnlinkedConstBuilder] that corresponds to the state of this
136 * serializer. 136 * serializer.
137 */ 137 */
138 UnlinkedConstBuilder toBuilder() { 138 UnlinkedConstBuilder toBuilder() {
139 if (isInvalid) {
140 return new UnlinkedConstBuilder(isInvalid: true);
141 }
142 return new UnlinkedConstBuilder( 139 return new UnlinkedConstBuilder(
140 isValidConst: isValidConst,
143 operations: operations, 141 operations: operations,
144 assignmentOperators: assignmentOperators, 142 assignmentOperators: assignmentOperators,
145 ints: ints, 143 ints: ints,
146 doubles: doubles, 144 doubles: doubles,
147 strings: strings, 145 strings: strings,
148 references: references); 146 references: references);
149 } 147 }
150 148
151 /** 149 /**
152 * Push the operation for the given assignable [expr]. 150 * Push the operation for the given assignable [expr].
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 operations.add(UnlinkedConstOperation.pushNull); 216 operations.add(UnlinkedConstOperation.pushNull);
219 } else if (expr is Identifier) { 217 } else if (expr is Identifier) {
220 if (expr is SimpleIdentifier && isConstructorParameterName(expr.name)) { 218 if (expr is SimpleIdentifier && isConstructorParameterName(expr.name)) {
221 strings.add(expr.name); 219 strings.add(expr.name);
222 operations.add(UnlinkedConstOperation.pushConstructorParameter); 220 operations.add(UnlinkedConstOperation.pushConstructorParameter);
223 } else { 221 } else {
224 references.add(serializeIdentifier(expr)); 222 references.add(serializeIdentifier(expr));
225 operations.add(UnlinkedConstOperation.pushReference); 223 operations.add(UnlinkedConstOperation.pushReference);
226 } 224 }
227 } else if (expr is InstanceCreationExpression) { 225 } else if (expr is InstanceCreationExpression) {
226 if (!expr.isConst) {
227 isValidConst = false;
228 }
228 serializeInstanceCreation( 229 serializeInstanceCreation(
229 serializeConstructorName( 230 serializeConstructorName(
230 expr.constructorName.type, expr.constructorName.name), 231 expr.constructorName.type, expr.constructorName.name),
231 expr.argumentList); 232 expr.argumentList);
232 } else if (expr is ListLiteral) { 233 } else if (expr is ListLiteral) {
233 _serializeListLiteral(expr); 234 _serializeListLiteral(expr);
234 } else if (expr is MapLiteral) { 235 } else if (expr is MapLiteral) {
235 _serializeMapLiteral(expr); 236 _serializeMapLiteral(expr);
236 } else if (expr is MethodInvocation) { 237 } else if (expr is MethodInvocation) {
237 _serializeMethodInvocation(expr); 238 _serializeMethodInvocation(expr);
238 } else if (expr is BinaryExpression) { 239 } else if (expr is BinaryExpression) {
239 _serializeBinaryExpression(expr); 240 _serializeBinaryExpression(expr);
240 } else if (expr is ConditionalExpression) { 241 } else if (expr is ConditionalExpression) {
241 _serialize(expr.condition); 242 _serialize(expr.condition);
242 _serialize(expr.thenExpression); 243 _serialize(expr.thenExpression);
243 _serialize(expr.elseExpression); 244 _serialize(expr.elseExpression);
244 operations.add(UnlinkedConstOperation.conditional); 245 operations.add(UnlinkedConstOperation.conditional);
245 } else if (expr is PrefixExpression) { 246 } else if (expr is PrefixExpression) {
246 _serializePrefixExpression(expr); 247 _serializePrefixExpression(expr);
247 } else if (expr is PostfixExpression) { 248 } else if (expr is PostfixExpression) {
248 _serializePostfixExpression(expr); 249 _serializePostfixExpression(expr);
249 } else if (expr is PropertyAccess) { 250 } else if (expr is PropertyAccess) {
250 _serializePropertyAccess(expr); 251 _serializePropertyAccess(expr);
251 } else if (expr is ParenthesizedExpression) { 252 } else if (expr is ParenthesizedExpression) {
252 _serialize(expr.expression); 253 _serialize(expr.expression);
253 } else if (expr is IndexExpression) { 254 } else if (expr is IndexExpression) {
255 isValidConst = false;
254 _serialize(expr.target); 256 _serialize(expr.target);
255 _serialize(expr.index); 257 _serialize(expr.index);
256 operations.add(UnlinkedConstOperation.extractIndex); 258 operations.add(UnlinkedConstOperation.extractIndex);
257 } else if (expr is AssignmentExpression) { 259 } else if (expr is AssignmentExpression) {
258 _serializeAssignment(expr); 260 _serializeAssignment(expr);
259 } else if (expr is CascadeExpression) { 261 } else if (expr is CascadeExpression) {
260 _serializeCascadeExpression(expr); 262 _serializeCascadeExpression(expr);
261 } else { 263 } else {
262 throw new StateError('Unknown expression type: $expr'); 264 throw new StateError('Unknown expression type: $expr');
263 } 265 }
(...skipping 11 matching lines...) Expand all
275 _serialize(arg); 277 _serialize(arg);
276 } 278 }
277 }); 279 });
278 // Add numbers of named and positional arguments, and the op-code. 280 // Add numbers of named and positional arguments, and the op-code.
279 ints.add(argumentNames.length); 281 ints.add(argumentNames.length);
280 strings.addAll(argumentNames); 282 strings.addAll(argumentNames);
281 ints.add(arguments.length - argumentNames.length); 283 ints.add(arguments.length - argumentNames.length);
282 } 284 }
283 285
284 void _serializeAssignment(AssignmentExpression expr) { 286 void _serializeAssignment(AssignmentExpression expr) {
287 isValidConst = false;
285 // Push the value. 288 // Push the value.
286 _serialize(expr.rightHandSide); 289 _serialize(expr.rightHandSide);
287 // Push the assignment operator. 290 // Push the assignment operator.
288 TokenType operator = expr.operator.type; 291 TokenType operator = expr.operator.type;
289 UnlinkedExprAssignOperator assignmentOperator; 292 UnlinkedExprAssignOperator assignmentOperator;
290 if (operator == TokenType.EQ) { 293 if (operator == TokenType.EQ) {
291 assignmentOperator = UnlinkedExprAssignOperator.assign; 294 assignmentOperator = UnlinkedExprAssignOperator.assign;
292 } else if (operator == TokenType.QUESTION_QUESTION_EQ) { 295 } else if (operator == TokenType.QUESTION_QUESTION_EQ) {
293 assignmentOperator = UnlinkedExprAssignOperator.ifNull; 296 assignmentOperator = UnlinkedExprAssignOperator.ifNull;
294 } else if (operator == TokenType.STAR_EQ) { 297 } else if (operator == TokenType.STAR_EQ) {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 expr.typeArguments.arguments.length == 2) { 403 expr.typeArguments.arguments.length == 2) {
401 references.add(serializeType(expr.typeArguments.arguments[0])); 404 references.add(serializeType(expr.typeArguments.arguments[0]));
402 references.add(serializeType(expr.typeArguments.arguments[1])); 405 references.add(serializeType(expr.typeArguments.arguments[1]));
403 operations.add(UnlinkedConstOperation.makeTypedMap); 406 operations.add(UnlinkedConstOperation.makeTypedMap);
404 } else { 407 } else {
405 operations.add(UnlinkedConstOperation.makeUntypedMap); 408 operations.add(UnlinkedConstOperation.makeUntypedMap);
406 } 409 }
407 } 410 }
408 411
409 void _serializeMethodInvocation(MethodInvocation invocation) { 412 void _serializeMethodInvocation(MethodInvocation invocation) {
413 if (invocation.target != null ||
414 invocation.methodName.name != 'identical') {
415 isValidConst = false;
416 }
410 Expression target = invocation.target; 417 Expression target = invocation.target;
411 SimpleIdentifier methodName = invocation.methodName; 418 SimpleIdentifier methodName = invocation.methodName;
412 ArgumentList argumentList = invocation.argumentList; 419 ArgumentList argumentList = invocation.argumentList;
413 if (_isIdentifierSequence(methodName)) { 420 if (_isIdentifierSequence(methodName)) {
414 EntityRefBuilder ref = serializeIdentifierSequence(methodName); 421 EntityRefBuilder ref = serializeIdentifierSequence(methodName);
415 references.add(ref); 422 references.add(ref);
416 _serializeArguments(argumentList); 423 _serializeArguments(argumentList);
417 operations.add(UnlinkedConstOperation.invokeMethodRef); 424 operations.add(UnlinkedConstOperation.invokeMethodRef);
418 } else { 425 } else {
419 if (!invocation.isCascaded) { 426 if (!invocation.isCascaded) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 } else if (operator == TokenType.MINUS_MINUS) { 464 } else if (operator == TokenType.MINUS_MINUS) {
458 _serializePrefixPostfixIncDec( 465 _serializePrefixPostfixIncDec(
459 operand, UnlinkedExprAssignOperator.prefixDecrement); 466 operand, UnlinkedExprAssignOperator.prefixDecrement);
460 } else { 467 } else {
461 throw new StateError('Unknown operator: $operator'); 468 throw new StateError('Unknown operator: $operator');
462 } 469 }
463 } 470 }
464 471
465 void _serializePrefixPostfixIncDec( 472 void _serializePrefixPostfixIncDec(
466 Expression operand, UnlinkedExprAssignOperator operator) { 473 Expression operand, UnlinkedExprAssignOperator operator) {
474 isValidConst = false;
467 assignmentOperators.add(operator); 475 assignmentOperators.add(operator);
468 _pushAssignable(operand); 476 _pushAssignable(operand);
469 } 477 }
470 478
471 void _serializePropertyAccess(PropertyAccess expr) { 479 void _serializePropertyAccess(PropertyAccess expr) {
472 if (_isIdentifierSequence(expr)) { 480 if (_isIdentifierSequence(expr)) {
473 EntityRefBuilder ref = serializeIdentifierSequence(expr); 481 EntityRefBuilder ref = serializeIdentifierSequence(expr);
474 references.add(ref); 482 references.add(ref);
475 operations.add(UnlinkedConstOperation.pushReference); 483 operations.add(UnlinkedConstOperation.pushReference);
476 } else { 484 } else {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 expr = (expr as PrefixedIdentifier).prefix; 534 expr = (expr as PrefixedIdentifier).prefix;
527 } else if (expr is PropertyAccess) { 535 } else if (expr is PropertyAccess) {
528 expr = (expr as PropertyAccess).target; 536 expr = (expr as PropertyAccess).target;
529 } else { 537 } else {
530 return false; 538 return false;
531 } 539 }
532 } 540 }
533 return false; 541 return false;
534 } 542 }
535 } 543 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698