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

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

Issue 1707073002: Serialize and resynthesize variable initializers. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fixes for review comments. Created 4 years, 10 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_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/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/ast/visitor.dart'; 9 import 'package:analyzer/dart/ast/visitor.dart';
10 import 'package:analyzer/src/generated/utilities_dart.dart'; 10 import 'package:analyzer/src/generated/utilities_dart.dart';
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 text: text, 468 text: text,
469 offset: documentationComment.offset, 469 offset: documentationComment.offset,
470 length: documentationComment.length); 470 length: documentationComment.length);
471 } 471 }
472 472
473 /** 473 /**
474 * Serialize a [FunctionDeclaration] or [MethodDeclaration] into an 474 * Serialize a [FunctionDeclaration] or [MethodDeclaration] into an
475 * [UnlinkedExecutable]. 475 * [UnlinkedExecutable].
476 */ 476 */
477 UnlinkedExecutableBuilder serializeExecutable( 477 UnlinkedExecutableBuilder serializeExecutable(
478 SimpleIdentifier name, 478 String name,
479 int nameOffset,
479 bool isGetter, 480 bool isGetter,
480 bool isSetter, 481 bool isSetter,
481 TypeName returnType, 482 TypeName returnType,
482 FormalParameterList formalParameters, 483 FormalParameterList formalParameters,
483 FunctionBody body, 484 FunctionBody body,
484 bool isTopLevel, 485 bool isTopLevel,
485 bool isDeclaredStatic, 486 bool isDeclaredStatic,
486 Comment documentationComment, 487 Comment documentationComment,
487 NodeList<Annotation> annotations, 488 NodeList<Annotation> annotations,
488 TypeParameterList typeParameters, 489 TypeParameterList typeParameters,
489 bool isExternal) { 490 bool isExternal) {
490 int oldScopesLength = scopes.length; 491 int oldScopesLength = scopes.length;
491 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); 492 _TypeParameterScope typeParameterScope = new _TypeParameterScope();
492 scopes.add(typeParameterScope); 493 scopes.add(typeParameterScope);
493 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); 494 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder();
494 String nameString = name.name; 495 String nameString = name;
495 if (isGetter) { 496 if (isGetter) {
496 b.kind = UnlinkedExecutableKind.getter; 497 b.kind = UnlinkedExecutableKind.getter;
497 } else if (isSetter) { 498 } else if (isSetter) {
498 b.kind = UnlinkedExecutableKind.setter; 499 b.kind = UnlinkedExecutableKind.setter;
499 nameString = '$nameString='; 500 nameString = '$nameString=';
500 } else { 501 } else {
501 b.kind = UnlinkedExecutableKind.functionOrMethod; 502 b.kind = UnlinkedExecutableKind.functionOrMethod;
502 } 503 }
503 b.isAbstract = body is EmptyFunctionBody; 504 b.isAbstract = body is EmptyFunctionBody;
504 b.name = nameString; 505 b.name = nameString;
505 b.nameOffset = name.offset; 506 b.nameOffset = nameOffset;
506 b.typeParameters = 507 b.typeParameters =
507 serializeTypeParameters(typeParameters, typeParameterScope); 508 serializeTypeParameters(typeParameters, typeParameterScope);
508 if (!isTopLevel) { 509 if (!isTopLevel) {
509 b.isStatic = isDeclaredStatic; 510 b.isStatic = isDeclaredStatic;
510 } 511 }
511 b.returnType = serializeTypeName(returnType); 512 b.returnType = serializeTypeName(returnType);
512 b.isExternal = isExternal; 513 b.isExternal = isExternal;
513 bool isSemanticallyStatic = isTopLevel || isDeclaredStatic; 514 bool isSemanticallyStatic = isTopLevel || isDeclaredStatic;
514 if (formalParameters != null) { 515 if (formalParameters != null) {
515 b.parameters = formalParameters.parameters 516 b.parameters = formalParameters.parameters
(...skipping 14 matching lines...) Expand all
530 b.inferredReturnTypeSlot = assignTypeSlot(); 531 b.inferredReturnTypeSlot = assignTypeSlot();
531 } 532 }
532 b.visibleOffset = enclosingBlock?.offset; 533 b.visibleOffset = enclosingBlock?.offset;
533 b.visibleLength = enclosingBlock?.length; 534 b.visibleLength = enclosingBlock?.length;
534 serializeFunctionBody(b, body); 535 serializeFunctionBody(b, body);
535 scopes.removeLast(); 536 scopes.removeLast();
536 assert(scopes.length == oldScopesLength); 537 assert(scopes.length == oldScopesLength);
537 return b; 538 return b;
538 } 539 }
539 540
540 void serializeFunctionBody(UnlinkedExecutableBuilder b, FunctionBody body) { 541 /**
542 * Record local functions and variables into the given executable. The given
543 * [body] is usually an actual [FunctionBody], but may be an [Expression]
544 * when we process a synthetic variable initializer function.
545 */
546 void serializeFunctionBody(UnlinkedExecutableBuilder b, AstNode body) {
541 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { 547 if (body is BlockFunctionBody || body is ExpressionFunctionBody) {
542 for (UnlinkedParamBuilder parameter in b.parameters) { 548 for (UnlinkedParamBuilder parameter in b.parameters) {
543 parameter.visibleOffset = body.offset; 549 parameter.visibleOffset = body.offset;
544 parameter.visibleLength = body.length; 550 parameter.visibleLength = body.length;
545 } 551 }
546 } 552 }
547 List<UnlinkedExecutableBuilder> oldExecutables = executables; 553 List<UnlinkedExecutableBuilder> oldExecutables = executables;
548 List<UnlinkedVariableBuilder> oldVariables = variables; 554 List<UnlinkedVariableBuilder> oldVariables = variables;
549 executables = <UnlinkedExecutableBuilder>[]; 555 executables = <UnlinkedExecutableBuilder>[];
550 variables = <UnlinkedVariableBuilder>[]; 556 variables = <UnlinkedVariableBuilder>[];
(...skipping 13 matching lines...) Expand all
564 EntityRefBuilder serializedReturnType = serializeTypeName(returnType); 570 EntityRefBuilder serializedReturnType = serializeTypeName(returnType);
565 if (serializedReturnType != null) { 571 if (serializedReturnType != null) {
566 b.type = serializedReturnType; 572 b.type = serializedReturnType;
567 } 573 }
568 b.parameters = parameters.parameters 574 b.parameters = parameters.parameters
569 .map((FormalParameter p) => p.accept(this)) 575 .map((FormalParameter p) => p.accept(this))
570 .toList(); 576 .toList();
571 } 577 }
572 578
573 /** 579 /**
580 * If the given [expression] is not `null`, serialize it as an
581 * [UnlinkedExecutableBuilder], otherwise return `null`.
582 */
583 UnlinkedExecutableBuilder serializeInitializerFunction(
584 Expression expression) {
585 if (expression == null) {
586 return null;
587 }
588 UnlinkedExecutableBuilder initializer =
589 new UnlinkedExecutableBuilder(nameOffset: expression.offset);
590 serializeFunctionBody(initializer, expression);
591 return initializer;
592 }
593
594 /**
574 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or 595 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or
575 * [SimpleFormalParameter] into an [UnlinkedParam]. 596 * [SimpleFormalParameter] into an [UnlinkedParam].
576 */ 597 */
577 UnlinkedParamBuilder serializeParameter(NormalFormalParameter node) { 598 UnlinkedParamBuilder serializeParameter(NormalFormalParameter node) {
578 UnlinkedParamBuilder b = new UnlinkedParamBuilder(); 599 UnlinkedParamBuilder b = new UnlinkedParamBuilder();
579 b.name = node.identifier.name; 600 b.name = node.identifier.name;
580 b.nameOffset = node.identifier.offset; 601 b.nameOffset = node.identifier.offset;
581 b.annotations = serializeAnnotations(node.metadata); 602 b.annotations = serializeAnnotations(node.metadata);
582 switch (node.kind) { 603 switch (node.kind) {
583 case ParameterKind.REQUIRED: 604 case ParameterKind.REQUIRED:
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 (variables.isFinal || variables.isConst)) { 768 (variables.isFinal || variables.isConst)) {
748 b.propagatedTypeSlot = assignTypeSlot(); 769 b.propagatedTypeSlot = assignTypeSlot();
749 } 770 }
750 bool isSemanticallyStatic = !isField || isDeclaredStatic; 771 bool isSemanticallyStatic = !isField || isDeclaredStatic;
751 if (variables.type == null && 772 if (variables.type == null &&
752 (variable.initializer != null || !isSemanticallyStatic)) { 773 (variable.initializer != null || !isSemanticallyStatic)) {
753 b.inferredTypeSlot = assignTypeSlot(); 774 b.inferredTypeSlot = assignTypeSlot();
754 } 775 }
755 b.visibleOffset = enclosingBlock?.offset; 776 b.visibleOffset = enclosingBlock?.offset;
756 b.visibleLength = enclosingBlock?.length; 777 b.visibleLength = enclosingBlock?.length;
778 b.initializer = serializeInitializerFunction(variable.initializer);
757 this.variables.add(b); 779 this.variables.add(b);
758 } 780 }
759 } 781 }
760 782
761 @override 783 @override
762 void visitBlock(Block node) { 784 void visitBlock(Block node) {
763 Block oldBlock = enclosingBlock; 785 Block oldBlock = enclosingBlock;
764 enclosingBlock = node; 786 enclosingBlock = node;
765 super.visitBlock(node); 787 super.visitBlock(node);
766 enclosingBlock = oldBlock; 788 enclosingBlock = oldBlock;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 executables.add(b); 869 executables.add(b);
848 } 870 }
849 871
850 @override 872 @override
851 UnlinkedParamBuilder visitDefaultFormalParameter( 873 UnlinkedParamBuilder visitDefaultFormalParameter(
852 DefaultFormalParameter node) { 874 DefaultFormalParameter node) {
853 UnlinkedParamBuilder b = node.parameter.accept(this); 875 UnlinkedParamBuilder b = node.parameter.accept(this);
854 if (node.defaultValue != null) { 876 if (node.defaultValue != null) {
855 b.defaultValue = serializeConstExpr(node.defaultValue); 877 b.defaultValue = serializeConstExpr(node.defaultValue);
856 } 878 }
879 b.initializer = serializeInitializerFunction(node.defaultValue);
857 return b; 880 return b;
858 } 881 }
859 882
860 @override 883 @override
861 void visitEnumDeclaration(EnumDeclaration node) { 884 void visitEnumDeclaration(EnumDeclaration node) {
862 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); 885 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder();
863 b.name = node.name.name; 886 b.name = node.name.name;
864 b.nameOffset = node.name.offset; 887 b.nameOffset = node.name.offset;
865 b.values = node.constants 888 b.values = node.constants
866 .map((EnumConstantDeclaration value) => new UnlinkedEnumValueBuilder( 889 .map((EnumConstantDeclaration value) => new UnlinkedEnumValueBuilder(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 } else { 922 } else {
900 b.type = serializeTypeName(node.type); 923 b.type = serializeTypeName(node.type);
901 } 924 }
902 } 925 }
903 return b; 926 return b;
904 } 927 }
905 928
906 @override 929 @override
907 void visitFunctionDeclaration(FunctionDeclaration node) { 930 void visitFunctionDeclaration(FunctionDeclaration node) {
908 executables.add(serializeExecutable( 931 executables.add(serializeExecutable(
909 node.name, 932 node.name.name,
933 node.name.offset,
910 node.isGetter, 934 node.isGetter,
911 node.isSetter, 935 node.isSetter,
912 node.returnType, 936 node.returnType,
913 node.functionExpression.parameters, 937 node.functionExpression.parameters,
914 node.functionExpression.body, 938 node.functionExpression.body,
915 true, 939 true,
916 false, 940 false,
917 node.documentationComment, 941 node.documentationComment,
918 node.metadata, 942 node.metadata,
919 node.functionExpression.typeParameters, 943 node.functionExpression.typeParameters,
920 node.externalKeyword != null)); 944 node.externalKeyword != null));
921 } 945 }
922 946
923 @override 947 @override
948 void visitFunctionExpression(FunctionExpression node) {
949 if (node.parent is! FunctionDeclaration) {
950 executables.add(serializeExecutable(
951 null,
952 node.offset,
953 false,
954 false,
955 null,
956 node.parameters,
957 node.body,
958 false,
959 false,
960 null,
961 null,
962 node.typeParameters,
963 false));
964 }
965 }
966
967 @override
924 void visitFunctionTypeAlias(FunctionTypeAlias node) { 968 void visitFunctionTypeAlias(FunctionTypeAlias node) {
925 int oldScopesLength = scopes.length; 969 int oldScopesLength = scopes.length;
926 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); 970 _TypeParameterScope typeParameterScope = new _TypeParameterScope();
927 scopes.add(typeParameterScope); 971 scopes.add(typeParameterScope);
928 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); 972 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder();
929 b.name = node.name.name; 973 b.name = node.name.name;
930 b.nameOffset = node.name.offset; 974 b.nameOffset = node.name.offset;
931 b.typeParameters = 975 b.typeParameters =
932 serializeTypeParameters(node.typeParameters, typeParameterScope); 976 serializeTypeParameters(node.typeParameters, typeParameterScope);
933 EntityRefBuilder serializedReturnType = serializeTypeName(node.returnType); 977 EntityRefBuilder serializedReturnType = serializeTypeName(node.returnType);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 libraryNameOffset = node.name.offset; 1024 libraryNameOffset = node.name.offset;
981 libraryNameLength = node.name.length; 1025 libraryNameLength = node.name.length;
982 libraryDocumentationComment = 1026 libraryDocumentationComment =
983 serializeDocumentation(node.documentationComment); 1027 serializeDocumentation(node.documentationComment);
984 libraryAnnotations = serializeAnnotations(node.metadata); 1028 libraryAnnotations = serializeAnnotations(node.metadata);
985 } 1029 }
986 1030
987 @override 1031 @override
988 void visitMethodDeclaration(MethodDeclaration node) { 1032 void visitMethodDeclaration(MethodDeclaration node) {
989 executables.add(serializeExecutable( 1033 executables.add(serializeExecutable(
990 node.name, 1034 node.name.name,
1035 node.name.offset,
991 node.isGetter, 1036 node.isGetter,
992 node.isSetter, 1037 node.isSetter,
993 node.returnType, 1038 node.returnType,
994 node.parameters, 1039 node.parameters,
995 node.body, 1040 node.body,
996 false, 1041 false,
997 node.isStatic, 1042 node.isStatic,
998 node.documentationComment, 1043 node.documentationComment,
999 node.metadata, 1044 node.metadata,
1000 node.typeParameters, 1045 node.typeParameters,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 /** 1099 /**
1055 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. 1100 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s.
1056 */ 1101 */
1057 class _TypeParameterScope extends _Scope { 1102 class _TypeParameterScope extends _Scope {
1058 /** 1103 /**
1059 * Get the number of [_ScopedTypeParameter]s defined in this 1104 * Get the number of [_ScopedTypeParameter]s defined in this
1060 * [_TypeParameterScope]. 1105 * [_TypeParameterScope].
1061 */ 1106 */
1062 int get length => _definedNames.length; 1107 int get length => _definedNames.length;
1063 } 1108 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/resynthesize.dart ('k') | pkg/analyzer/lib/src/summary/summarize_elements.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698