| 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_ast; | 5 library serialization.summarize_ast; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/utilities_dart.dart'; | 10 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; | 158 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; |
| 159 | 159 |
| 160 /** | 160 /** |
| 161 * The unlinked portion of the "references table". This is the list of | 161 * The unlinked portion of the "references table". This is the list of |
| 162 * objects which should be written to [UnlinkedUnit.references]. | 162 * objects which should be written to [UnlinkedUnit.references]. |
| 163 */ | 163 */ |
| 164 final List<UnlinkedReferenceBuilder> unlinkedReferences = | 164 final List<UnlinkedReferenceBuilder> unlinkedReferences = |
| 165 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()]; | 165 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()]; |
| 166 | 166 |
| 167 /** | 167 /** |
| 168 * The [ClassDeclaration] currently being summarized, or `null` if we are not | |
| 169 * currently visiting a [ClassDeclaration]. | |
| 170 */ | |
| 171 ClassDeclaration currentClassDeclaration; | |
| 172 | |
| 173 /** | |
| 174 * Map associating names used as prefixes in this compilation unit with their | 168 * Map associating names used as prefixes in this compilation unit with their |
| 175 * associated indices into [UnlinkedUnit.references]. | 169 * associated indices into [UnlinkedUnit.references]. |
| 176 */ | 170 */ |
| 177 final Map<String, int> prefixIndices = <String, int>{}; | 171 final Map<String, int> prefixIndices = <String, int>{}; |
| 178 | 172 |
| 179 /** | 173 /** |
| 180 * List of [_Scope]s currently in effect. This is used to resolve type names | 174 * List of [_Scope]s currently in effect. This is used to resolve type names |
| 181 * to type parameters within classes, typedefs, and executables. | 175 * to type parameters within classes, typedefs, and executables. |
| 182 */ | 176 */ |
| 183 final List<_Scope> scopes = <_Scope>[]; | 177 final List<_Scope> scopes = <_Scope>[]; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // We don't have to worry about the one with a trailing `=` because | 246 // We don't have to worry about the one with a trailing `=` because |
| 253 // the only thing we look up is type names. | 247 // the only thing we look up is type names. |
| 254 scope[field.name.name] = new _OtherScopedEntity(); | 248 scope[field.name.name] = new _OtherScopedEntity(); |
| 255 } | 249 } |
| 256 } | 250 } |
| 257 } | 251 } |
| 258 return scope; | 252 return scope; |
| 259 } | 253 } |
| 260 | 254 |
| 261 /** | 255 /** |
| 262 * Try to find a field with the given [name] in the current class declaration | |
| 263 * and return its type. If no field is found, or there isn't a current | |
| 264 * class declaration, return null. | |
| 265 */ | |
| 266 TypeName getFieldType(String name) { | |
| 267 if (currentClassDeclaration == null) { | |
| 268 return null; | |
| 269 } | |
| 270 for (ClassMember member in currentClassDeclaration.members) { | |
| 271 if (member is FieldDeclaration) { | |
| 272 for (VariableDeclaration variable in member.fields.variables) { | |
| 273 if (variable.name.name == name) { | |
| 274 return member.fields.type; | |
| 275 } | |
| 276 } | |
| 277 } | |
| 278 } | |
| 279 return null; | |
| 280 } | |
| 281 | |
| 282 /** | |
| 283 * Serialize a [ClassDeclaration] or [ClassTypeAlias] into an [UnlinkedClass] | 256 * Serialize a [ClassDeclaration] or [ClassTypeAlias] into an [UnlinkedClass] |
| 284 * and store the result in [classes]. | 257 * and store the result in [classes]. |
| 285 */ | 258 */ |
| 286 void serializeClass( | 259 void serializeClass( |
| 287 Token abstractKeyword, | 260 Token abstractKeyword, |
| 288 String name, | 261 String name, |
| 289 int nameOffset, | 262 int nameOffset, |
| 290 TypeParameterList typeParameters, | 263 TypeParameterList typeParameters, |
| 291 TypeName superclass, | 264 TypeName superclass, |
| 292 WithClause withClause, | 265 WithClause withClause, |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 } | 412 } |
| 440 b.isAbstract = body is EmptyFunctionBody; | 413 b.isAbstract = body is EmptyFunctionBody; |
| 441 b.name = nameString; | 414 b.name = nameString; |
| 442 b.nameOffset = name.offset; | 415 b.nameOffset = name.offset; |
| 443 b.typeParameters = | 416 b.typeParameters = |
| 444 serializeTypeParameters(typeParameters, typeParameterScope); | 417 serializeTypeParameters(typeParameters, typeParameterScope); |
| 445 if (!isTopLevel) { | 418 if (!isTopLevel) { |
| 446 b.isStatic = isStatic; | 419 b.isStatic = isStatic; |
| 447 } | 420 } |
| 448 b.returnType = serializeTypeName(returnType); | 421 b.returnType = serializeTypeName(returnType); |
| 449 b.hasImplicitReturnType = returnType == null; | |
| 450 b.isExternal = isExternal; | 422 b.isExternal = isExternal; |
| 451 if (formalParameters != null) { | 423 if (formalParameters != null) { |
| 452 b.parameters = formalParameters.parameters | 424 b.parameters = formalParameters.parameters |
| 453 .map((FormalParameter p) => p.accept(this)) | 425 .map((FormalParameter p) => p.accept(this)) |
| 454 .toList(); | 426 .toList(); |
| 455 } | 427 } |
| 456 b.documentationComment = serializeDocumentation(documentationComment); | 428 b.documentationComment = serializeDocumentation(documentationComment); |
| 457 scopes.removeLast(); | 429 scopes.removeLast(); |
| 458 assert(scopes.length == oldScopesLength); | 430 assert(scopes.length == oldScopesLength); |
| 459 return b; | 431 return b; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 return index; | 485 return index; |
| 514 }); | 486 }); |
| 515 | 487 |
| 516 /** | 488 /** |
| 517 * Serialize a type name (which might be defined in a nested scope, at top | 489 * Serialize a type name (which might be defined in a nested scope, at top |
| 518 * level within this library, or at top level within an imported library) to | 490 * level within this library, or at top level within an imported library) to |
| 519 * a [EntityRef]. Note that this method does the right thing if the | 491 * a [EntityRef]. Note that this method does the right thing if the |
| 520 * name doesn't refer to an entity other than a type (e.g. a class member). | 492 * name doesn't refer to an entity other than a type (e.g. a class member). |
| 521 */ | 493 */ |
| 522 EntityRefBuilder serializeTypeName(TypeName node) { | 494 EntityRefBuilder serializeTypeName(TypeName node) { |
| 523 EntityRefBuilder b = new EntityRefBuilder(); | |
| 524 if (node == null) { | 495 if (node == null) { |
| 525 b.reference = serializeReference(null, 'dynamic'); | 496 return null; |
| 526 } else { | 497 } else { |
| 498 EntityRefBuilder b = new EntityRefBuilder(); |
| 527 Identifier identifier = node.name; | 499 Identifier identifier = node.name; |
| 528 if (identifier is SimpleIdentifier) { | 500 if (identifier is SimpleIdentifier) { |
| 529 String name = identifier.name; | 501 String name = identifier.name; |
| 530 int indexOffset = 0; | 502 int indexOffset = 0; |
| 531 for (int i = scopes.length - 1; i >= 0; i--) { | 503 for (int i = scopes.length - 1; i >= 0; i--) { |
| 532 _Scope scope = scopes[i]; | 504 _Scope scope = scopes[i]; |
| 533 _ScopedEntity entity = scope[name]; | 505 _ScopedEntity entity = scope[name]; |
| 534 if (entity != null) { | 506 if (entity != null) { |
| 535 if (entity is _ScopedTypeParameter) { | 507 if (entity is _ScopedTypeParameter) { |
| 536 b.paramReference = indexOffset + entity.index; | 508 b.paramReference = indexOffset + entity.index; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 566 --numArgsToSerialize; | 538 --numArgsToSerialize; |
| 567 } | 539 } |
| 568 if (numArgsToSerialize > 0) { | 540 if (numArgsToSerialize > 0) { |
| 569 List<EntityRefBuilder> serializedArguments = <EntityRefBuilder>[]; | 541 List<EntityRefBuilder> serializedArguments = <EntityRefBuilder>[]; |
| 570 for (int i = 0; i < numArgsToSerialize; i++) { | 542 for (int i = 0; i < numArgsToSerialize; i++) { |
| 571 serializedArguments.add(serializeTypeName(args[i])); | 543 serializedArguments.add(serializeTypeName(args[i])); |
| 572 } | 544 } |
| 573 b.typeArguments = serializedArguments; | 545 b.typeArguments = serializedArguments; |
| 574 } | 546 } |
| 575 } | 547 } |
| 548 return b; |
| 576 } | 549 } |
| 577 return b; | |
| 578 } | 550 } |
| 579 | 551 |
| 580 /** | 552 /** |
| 581 * Serialize the given [typeParameters] into a list of [UnlinkedTypeParam]s, | 553 * Serialize the given [typeParameters] into a list of [UnlinkedTypeParam]s, |
| 582 * and also store them in [typeParameterScope]. | 554 * and also store them in [typeParameterScope]. |
| 583 */ | 555 */ |
| 584 List<UnlinkedTypeParamBuilder> serializeTypeParameters( | 556 List<UnlinkedTypeParamBuilder> serializeTypeParameters( |
| 585 TypeParameterList typeParameters, | 557 TypeParameterList typeParameters, |
| 586 _TypeParameterScope typeParameterScope) { | 558 _TypeParameterScope typeParameterScope) { |
| 587 if (typeParameters != null) { | 559 if (typeParameters != null) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 602 void serializeVariables(VariableDeclarationList variables, bool isStatic, | 574 void serializeVariables(VariableDeclarationList variables, bool isStatic, |
| 603 Comment documentationComment) { | 575 Comment documentationComment) { |
| 604 for (VariableDeclaration variable in variables.variables) { | 576 for (VariableDeclaration variable in variables.variables) { |
| 605 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); | 577 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); |
| 606 b.isFinal = variables.isFinal; | 578 b.isFinal = variables.isFinal; |
| 607 b.isConst = variables.isConst; | 579 b.isConst = variables.isConst; |
| 608 b.isStatic = isStatic; | 580 b.isStatic = isStatic; |
| 609 b.name = variable.name.name; | 581 b.name = variable.name.name; |
| 610 b.nameOffset = variable.name.offset; | 582 b.nameOffset = variable.name.offset; |
| 611 b.type = serializeTypeName(variables.type); | 583 b.type = serializeTypeName(variables.type); |
| 612 b.hasImplicitType = variables.type == null; | |
| 613 b.documentationComment = serializeDocumentation(documentationComment); | 584 b.documentationComment = serializeDocumentation(documentationComment); |
| 614 if (variable.isConst) { | 585 if (variable.isConst) { |
| 615 Expression initializer = variable.initializer; | 586 Expression initializer = variable.initializer; |
| 616 if (initializer != null) { | 587 if (initializer != null) { |
| 617 b.constExpr = serializeConstExpr(initializer); | 588 b.constExpr = serializeConstExpr(initializer); |
| 618 } | 589 } |
| 619 } | 590 } |
| 620 if (variable.initializer != null && | 591 if (variable.initializer != null && |
| 621 (variables.isFinal || variables.isConst)) { | 592 (variables.isFinal || variables.isConst)) { |
| 622 b.propagatedTypeSlot = assignTypeSlot(); | 593 b.propagatedTypeSlot = assignTypeSlot(); |
| 623 } | 594 } |
| 624 this.variables.add(b); | 595 this.variables.add(b); |
| 625 } | 596 } |
| 626 } | 597 } |
| 627 | 598 |
| 628 @override | 599 @override |
| 629 void visitClassDeclaration(ClassDeclaration node) { | 600 void visitClassDeclaration(ClassDeclaration node) { |
| 630 currentClassDeclaration = node; | |
| 631 TypeName superclass = | 601 TypeName superclass = |
| 632 node.extendsClause == null ? null : node.extendsClause.superclass; | 602 node.extendsClause == null ? null : node.extendsClause.superclass; |
| 633 serializeClass( | 603 serializeClass( |
| 634 node.abstractKeyword, | 604 node.abstractKeyword, |
| 635 node.name.name, | 605 node.name.name, |
| 636 node.name.offset, | 606 node.name.offset, |
| 637 node.typeParameters, | 607 node.typeParameters, |
| 638 superclass, | 608 superclass, |
| 639 node.withClause, | 609 node.withClause, |
| 640 node.implementsClause, | 610 node.implementsClause, |
| 641 node.members, | 611 node.members, |
| 642 false, | 612 false, |
| 643 node.documentationComment); | 613 node.documentationComment); |
| 644 currentClassDeclaration = null; | |
| 645 } | 614 } |
| 646 | 615 |
| 647 @override | 616 @override |
| 648 void visitClassTypeAlias(ClassTypeAlias node) { | 617 void visitClassTypeAlias(ClassTypeAlias node) { |
| 649 serializeClass( | 618 serializeClass( |
| 650 node.abstractKeyword, | 619 node.abstractKeyword, |
| 651 node.name.name, | 620 node.name.name, |
| 652 node.name.offset, | 621 node.name.offset, |
| 653 node.typeParameters, | 622 node.typeParameters, |
| 654 node.superclass, | 623 node.superclass, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 @override | 677 @override |
| 709 void visitFieldDeclaration(FieldDeclaration node) { | 678 void visitFieldDeclaration(FieldDeclaration node) { |
| 710 serializeVariables( | 679 serializeVariables( |
| 711 node.fields, node.staticKeyword != null, node.documentationComment); | 680 node.fields, node.staticKeyword != null, node.documentationComment); |
| 712 } | 681 } |
| 713 | 682 |
| 714 @override | 683 @override |
| 715 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) { | 684 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) { |
| 716 UnlinkedParamBuilder b = serializeParameter(node); | 685 UnlinkedParamBuilder b = serializeParameter(node); |
| 717 b.isInitializingFormal = true; | 686 b.isInitializingFormal = true; |
| 718 if (node.type == null && node.parameters == null) { | 687 if (node.type != null || node.parameters != null) { |
| 719 b.hasImplicitType = true; | |
| 720 } else { | |
| 721 b.isFunctionTyped = node.parameters != null; | 688 b.isFunctionTyped = node.parameters != null; |
| 722 if (node.parameters != null) { | 689 if (node.parameters != null) { |
| 723 serializeFunctionTypedParameterDetails(b, node.type, node.parameters); | 690 serializeFunctionTypedParameterDetails(b, node.type, node.parameters); |
| 724 } else { | 691 } else { |
| 725 TypeName typeName = node.type; | 692 b.type = serializeTypeName(node.type); |
| 726 if (typeName == null) { | |
| 727 typeName = getFieldType(node.identifier.name); | |
| 728 } | |
| 729 b.type = serializeTypeName(typeName); | |
| 730 } | 693 } |
| 731 } | 694 } |
| 732 return b; | 695 return b; |
| 733 } | 696 } |
| 734 | 697 |
| 735 @override | 698 @override |
| 736 void visitFunctionDeclaration(FunctionDeclaration node) { | 699 void visitFunctionDeclaration(FunctionDeclaration node) { |
| 737 executables.add(serializeExecutable( | 700 executables.add(serializeExecutable( |
| 738 node.name, | 701 node.name, |
| 739 node.isGetter, | 702 node.isGetter, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 uriOffset: node.uri.offset, uriEnd: node.uri.end)); | 794 uriOffset: node.uri.offset, uriEnd: node.uri.end)); |
| 832 } | 795 } |
| 833 | 796 |
| 834 @override | 797 @override |
| 835 void visitPartOfDirective(PartOfDirective node) {} | 798 void visitPartOfDirective(PartOfDirective node) {} |
| 836 | 799 |
| 837 @override | 800 @override |
| 838 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) { | 801 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) { |
| 839 UnlinkedParamBuilder b = serializeParameter(node); | 802 UnlinkedParamBuilder b = serializeParameter(node); |
| 840 b.type = serializeTypeName(node.type); | 803 b.type = serializeTypeName(node.type); |
| 841 b.hasImplicitType = node.type == null; | |
| 842 return b; | 804 return b; |
| 843 } | 805 } |
| 844 | 806 |
| 845 @override | 807 @override |
| 846 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { | 808 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| 847 serializeVariables(node.variables, false, node.documentationComment); | 809 serializeVariables(node.variables, false, node.documentationComment); |
| 848 } | 810 } |
| 849 | 811 |
| 850 @override | 812 @override |
| 851 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) { | 813 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 870 /** | 832 /** |
| 871 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 833 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 872 */ | 834 */ |
| 873 class _TypeParameterScope extends _Scope { | 835 class _TypeParameterScope extends _Scope { |
| 874 /** | 836 /** |
| 875 * Get the number of [_ScopedTypeParameter]s defined in this | 837 * Get the number of [_ScopedTypeParameter]s defined in this |
| 876 * [_TypeParameterScope]. | 838 * [_TypeParameterScope]. |
| 877 */ | 839 */ |
| 878 int get length => _definedNames.length; | 840 int get length => _definedNames.length; |
| 879 } | 841 } |
| OLD | NEW |