| 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'; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 /** | 130 /** |
| 131 * Return [EntityRefBuilder] that corresponds to the constructor having name | 131 * Return [EntityRefBuilder] that corresponds to the constructor having name |
| 132 * [name] in the class identified by [typeName]. It is expected that [type] | 132 * [name] in the class identified by [typeName]. It is expected that [type] |
| 133 * corresponds to the given [typeName] and [typeArguments]. The parameter | 133 * corresponds to the given [typeName] and [typeArguments]. The parameter |
| 134 * [type] might be `null` if the type is not resolved. | 134 * [type] might be `null` if the type is not resolved. |
| 135 */ | 135 */ |
| 136 EntityRefBuilder serializeConstructorRef(DartType type, Identifier typeName, | 136 EntityRefBuilder serializeConstructorRef(DartType type, Identifier typeName, |
| 137 TypeArgumentList typeArguments, SimpleIdentifier name); | 137 TypeArgumentList typeArguments, SimpleIdentifier name); |
| 138 | 138 |
| 139 /** | 139 /** |
| 140 * Return [EntityRefBuilder] that corresponds to the given [identifier]. | |
| 141 */ | |
| 142 EntityRefBuilder serializeIdentifier(Identifier identifier); | |
| 143 | |
| 144 /** | |
| 145 * Return a pair of ints showing how the given [functionExpression] is nested | 140 * Return a pair of ints showing how the given [functionExpression] is nested |
| 146 * within the constant currently being serialized. The first int indicates | 141 * within the constant currently being serialized. The first int indicates |
| 147 * how many levels of function nesting must be popped in order to reach the | 142 * how many levels of function nesting must be popped in order to reach the |
| 148 * parent of the [functionExpression]. The second int is the index of the | 143 * parent of the [functionExpression]. The second int is the index of the |
| 149 * [functionExpression] within its parent element. | 144 * [functionExpression] within its parent element. |
| 150 * | 145 * |
| 151 * If the constant being summarized is in a context where local function | 146 * If the constant being summarized is in a context where local function |
| 152 * references are not allowed, return `null`. | 147 * references are not allowed, return `null`. |
| 153 */ | 148 */ |
| 154 List<int> serializeFunctionExpression(FunctionExpression functionExpression); | 149 List<int> serializeFunctionExpression(FunctionExpression functionExpression); |
| 155 | 150 |
| 156 /** | 151 /** |
| 152 * Return [EntityRefBuilder] that corresponds to the given [identifier]. |
| 153 */ |
| 154 EntityRefBuilder serializeIdentifier(Identifier identifier); |
| 155 |
| 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(UnlinkedConstOperation.invokeConstructor); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 /** |
| 201 * Return `true` if the given [expr] is a sequence of identifiers. |
| 202 */ |
| 203 bool _isIdentifierSequence(Expression expr) { |
| 204 while (expr != null) { |
| 205 if (expr is SimpleIdentifier) { |
| 206 AstNode parent = expr.parent; |
| 207 if (parent is MethodInvocation && parent.methodName == expr) { |
| 208 if (parent.isCascaded) { |
| 209 return false; |
| 210 } |
| 211 return parent.target == null || _isIdentifierSequence(parent.target); |
| 212 } |
| 213 if (isParameterName(expr.name)) { |
| 214 return false; |
| 215 } |
| 216 return true; |
| 217 } else if (expr is PrefixedIdentifier) { |
| 218 expr = (expr as PrefixedIdentifier).prefix; |
| 219 } else if (expr is PropertyAccess) { |
| 220 expr = (expr as PropertyAccess).target; |
| 221 } else { |
| 222 return false; |
| 223 } |
| 224 } |
| 225 return false; |
| 226 } |
| 227 |
| 228 /** |
| 201 * Push the operation for the given assignable [expr]. | 229 * Push the operation for the given assignable [expr]. |
| 202 */ | 230 */ |
| 203 void _pushAssignable(Expression expr) { | 231 void _pushAssignable(Expression expr) { |
| 204 if (_isIdentifierSequence(expr)) { | 232 if (_isIdentifierSequence(expr)) { |
| 205 EntityRefBuilder ref = serializeIdentifierSequence(expr); | 233 EntityRefBuilder ref = serializeIdentifierSequence(expr); |
| 206 references.add(ref); | 234 references.add(ref); |
| 207 operations.add(UnlinkedConstOperation.assignToRef); | 235 operations.add(UnlinkedConstOperation.assignToRef); |
| 208 } else if (expr is PropertyAccess) { | 236 } else if (expr is PropertyAccess) { |
| 209 if (!expr.isCascaded) { | 237 if (!expr.isCascaded) { |
| 210 _serialize(expr.target); | 238 _serialize(expr.target); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 _serializeString(expr); | 295 _serializeString(expr); |
| 268 } else if (expr is SymbolLiteral) { | 296 } else if (expr is SymbolLiteral) { |
| 269 strings.add(expr.components.map((token) => token.lexeme).join('.')); | 297 strings.add(expr.components.map((token) => token.lexeme).join('.')); |
| 270 operations.add(UnlinkedConstOperation.makeSymbol); | 298 operations.add(UnlinkedConstOperation.makeSymbol); |
| 271 } else if (expr is NullLiteral) { | 299 } else if (expr is NullLiteral) { |
| 272 operations.add(UnlinkedConstOperation.pushNull); | 300 operations.add(UnlinkedConstOperation.pushNull); |
| 273 } else if (expr is Identifier) { | 301 } else if (expr is Identifier) { |
| 274 if (expr is SimpleIdentifier && isParameterName(expr.name)) { | 302 if (expr is SimpleIdentifier && isParameterName(expr.name)) { |
| 275 strings.add(expr.name); | 303 strings.add(expr.name); |
| 276 operations.add(UnlinkedConstOperation.pushParameter); | 304 operations.add(UnlinkedConstOperation.pushParameter); |
| 277 } else if (expr is PrefixedIdentifier && isParameterName(expr.prefix.name)
) { | 305 } else if (expr is PrefixedIdentifier && |
| 306 isParameterName(expr.prefix.name)) { |
| 278 strings.add(expr.prefix.name); | 307 strings.add(expr.prefix.name); |
| 279 operations.add(UnlinkedConstOperation.pushParameter); | 308 operations.add(UnlinkedConstOperation.pushParameter); |
| 280 strings.add(expr.identifier.name); | 309 strings.add(expr.identifier.name); |
| 281 operations.add(UnlinkedConstOperation.extractProperty); | 310 operations.add(UnlinkedConstOperation.extractProperty); |
| 282 } else { | 311 } else { |
| 283 references.add(serializeIdentifier(expr)); | 312 references.add(serializeIdentifier(expr)); |
| 284 operations.add(UnlinkedConstOperation.pushReference); | 313 operations.add(UnlinkedConstOperation.pushReference); |
| 285 } | 314 } |
| 286 } else if (expr is InstanceCreationExpression) { | 315 } else if (expr is InstanceCreationExpression) { |
| 287 if (!expr.isConst) { | 316 if (!expr.isConst) { |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 invocation.methodName.name != 'identical') { | 533 invocation.methodName.name != 'identical') { |
| 505 isValidConst = false; | 534 isValidConst = false; |
| 506 } | 535 } |
| 507 Expression target = invocation.target; | 536 Expression target = invocation.target; |
| 508 SimpleIdentifier methodName = invocation.methodName; | 537 SimpleIdentifier methodName = invocation.methodName; |
| 509 ArgumentList argumentList = invocation.argumentList; | 538 ArgumentList argumentList = invocation.argumentList; |
| 510 if (_isIdentifierSequence(methodName)) { | 539 if (_isIdentifierSequence(methodName)) { |
| 511 EntityRefBuilder ref = serializeIdentifierSequence(methodName); | 540 EntityRefBuilder ref = serializeIdentifierSequence(methodName); |
| 512 _serializeArguments(argumentList); | 541 _serializeArguments(argumentList); |
| 513 references.add(ref); | 542 references.add(ref); |
| 543 _serializeTypeArguments(invocation.typeArguments); |
| 514 operations.add(UnlinkedConstOperation.invokeMethodRef); | 544 operations.add(UnlinkedConstOperation.invokeMethodRef); |
| 515 } else { | 545 } else { |
| 516 if (!invocation.isCascaded) { | 546 if (!invocation.isCascaded) { |
| 517 _serialize(target); | 547 _serialize(target); |
| 518 } | 548 } |
| 519 _serializeArguments(argumentList); | 549 _serializeArguments(argumentList); |
| 520 strings.add(methodName.name); | 550 strings.add(methodName.name); |
| 551 _serializeTypeArguments(invocation.typeArguments); |
| 521 operations.add(UnlinkedConstOperation.invokeMethod); | 552 operations.add(UnlinkedConstOperation.invokeMethod); |
| 522 } | 553 } |
| 523 } | 554 } |
| 524 | 555 |
| 525 void _serializePostfixExpression(PostfixExpression expr) { | 556 void _serializePostfixExpression(PostfixExpression expr) { |
| 526 TokenType operator = expr.operator.type; | 557 TokenType operator = expr.operator.type; |
| 527 Expression operand = expr.operand; | 558 Expression operand = expr.operand; |
| 528 if (operator == TokenType.PLUS_PLUS) { | 559 if (operator == TokenType.PLUS_PLUS) { |
| 529 _serializePrefixPostfixIncDec( | 560 _serializePrefixPostfixIncDec( |
| 530 operand, UnlinkedExprAssignOperator.postfixIncrement); | 561 operand, UnlinkedExprAssignOperator.postfixIncrement); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 strings.add(element.value); | 630 strings.add(element.value); |
| 600 } else { | 631 } else { |
| 601 _serialize((element as InterpolationExpression).expression); | 632 _serialize((element as InterpolationExpression).expression); |
| 602 } | 633 } |
| 603 } | 634 } |
| 604 operations.add(UnlinkedConstOperation.concatenate); | 635 operations.add(UnlinkedConstOperation.concatenate); |
| 605 ints.add(interpolation.elements.length); | 636 ints.add(interpolation.elements.length); |
| 606 } | 637 } |
| 607 } | 638 } |
| 608 | 639 |
| 609 /** | 640 void _serializeTypeArguments(TypeArgumentList typeArguments) { |
| 610 * Return `true` if the given [expr] is a sequence of identifiers. | 641 if (typeArguments == null) { |
| 611 */ | 642 ints.add(0); |
| 612 bool _isIdentifierSequence(Expression expr) { | 643 } else { |
| 613 while (expr != null) { | 644 ints.add(typeArguments.arguments.length); |
| 614 if (expr is SimpleIdentifier) { | 645 for (TypeName typeName in typeArguments.arguments) { |
| 615 AstNode parent = expr.parent; | 646 references.add(serializeTypeName(typeName)); |
| 616 if (parent is MethodInvocation && parent.methodName == expr) { | |
| 617 if (parent.isCascaded) { | |
| 618 return false; | |
| 619 } | |
| 620 return parent.target == null || _isIdentifierSequence(parent.target); | |
| 621 } | |
| 622 if (isParameterName(expr.name)) { | |
| 623 return false; | |
| 624 } | |
| 625 return true; | |
| 626 } else if (expr is PrefixedIdentifier) { | |
| 627 expr = (expr as PrefixedIdentifier).prefix; | |
| 628 } else if (expr is PropertyAccess) { | |
| 629 expr = (expr as PropertyAccess).target; | |
| 630 } else { | |
| 631 return false; | |
| 632 } | 647 } |
| 633 } | 648 } |
| 634 return false; | |
| 635 } | 649 } |
| 636 } | 650 } |
| OLD | NEW |