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

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

Issue 1842433002: When summarize AST, include local variables in loops and try-catch. (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_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 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 */ 472 */
473 UnlinkedConstBuilder serializeConstExpr(Expression expression, 473 UnlinkedConstBuilder serializeConstExpr(Expression expression,
474 [Set<String> constructorParameterNames]) { 474 [Set<String> constructorParameterNames]) {
475 _ConstExprSerializer serializer = 475 _ConstExprSerializer serializer =
476 new _ConstExprSerializer(this, constructorParameterNames); 476 new _ConstExprSerializer(this, constructorParameterNames);
477 serializer.serialize(expression); 477 serializer.serialize(expression);
478 return serializer.toBuilder(); 478 return serializer.toBuilder();
479 } 479 }
480 480
481 /** 481 /**
482 * Serialize the given [declaredIdentifier] into [UnlinkedVariable], and
483 * store it in [variables].
484 */
485 void serializeDeclaredIdentifier(
486 AstNode scopeNode,
487 Comment documentationComment,
488 NodeList<Annotation> annotations,
489 bool isFinal,
490 bool isConst,
491 TypeName type,
492 bool assignPropagatedTypeSlot,
493 SimpleIdentifier declaredIdentifier) {
494 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder();
495 b.isFinal = isFinal;
496 b.isConst = isConst;
497 b.name = declaredIdentifier.name;
498 b.nameOffset = declaredIdentifier.offset;
499 b.type = serializeTypeName(type);
500 b.documentationComment = serializeDocumentation(documentationComment);
501 b.annotations = serializeAnnotations(annotations);
502 b.codeRange = serializeCodeRange(declaredIdentifier);
503 if (assignPropagatedTypeSlot) {
504 b.propagatedTypeSlot = assignSlot();
505 }
506 b.visibleOffset = scopeNode?.offset;
507 b.visibleLength = scopeNode?.length;
508 this.variables.add(b);
509 }
510
511 /**
482 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. 512 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object.
483 */ 513 */
484 UnlinkedDocumentationCommentBuilder serializeDocumentation( 514 UnlinkedDocumentationCommentBuilder serializeDocumentation(
485 Comment documentationComment) { 515 Comment documentationComment) {
486 if (documentationComment == null) { 516 if (documentationComment == null) {
487 return null; 517 return null;
488 } 518 }
489 String text = documentationComment.tokens 519 String text = documentationComment.tokens
490 .map((Token t) => t.toString()) 520 .map((Token t) => t.toString())
491 .join() 521 .join()
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 return typeParameters.typeParameters.map(visitTypeParameter).toList(); 799 return typeParameters.typeParameters.map(visitTypeParameter).toList();
770 } 800 }
771 return const <UnlinkedTypeParamBuilder>[]; 801 return const <UnlinkedTypeParamBuilder>[];
772 } 802 }
773 803
774 /** 804 /**
775 * Serialize the given [variables] into [UnlinkedVariable]s, and store them 805 * Serialize the given [variables] into [UnlinkedVariable]s, and store them
776 * in [this.variables]. 806 * in [this.variables].
777 */ 807 */
778 void serializeVariables( 808 void serializeVariables(
809 AstNode scopeNode,
779 VariableDeclarationList variables, 810 VariableDeclarationList variables,
780 bool isDeclaredStatic, 811 bool isDeclaredStatic,
781 Comment documentationComment, 812 Comment documentationComment,
782 NodeList<Annotation> annotations, 813 NodeList<Annotation> annotations,
783 bool isField) { 814 bool isField) {
784 for (VariableDeclaration variable in variables.variables) { 815 for (VariableDeclaration variable in variables.variables) {
785 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); 816 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder();
786 b.isFinal = variables.isFinal; 817 b.isFinal = variables.isFinal;
787 b.isConst = variables.isConst; 818 b.isConst = variables.isConst;
788 b.isStatic = isDeclaredStatic; 819 b.isStatic = isDeclaredStatic;
(...skipping 12 matching lines...) Expand all
801 } 832 }
802 if (variable.initializer != null && 833 if (variable.initializer != null &&
803 (variables.isFinal || variables.isConst)) { 834 (variables.isFinal || variables.isConst)) {
804 b.propagatedTypeSlot = assignSlot(); 835 b.propagatedTypeSlot = assignSlot();
805 } 836 }
806 bool isSemanticallyStatic = !isField || isDeclaredStatic; 837 bool isSemanticallyStatic = !isField || isDeclaredStatic;
807 if (variables.type == null && 838 if (variables.type == null &&
808 (variable.initializer != null || !isSemanticallyStatic)) { 839 (variable.initializer != null || !isSemanticallyStatic)) {
809 b.inferredTypeSlot = assignSlot(); 840 b.inferredTypeSlot = assignSlot();
810 } 841 }
811 b.visibleOffset = enclosingBlock?.offset; 842 b.visibleOffset = scopeNode?.offset;
812 b.visibleLength = enclosingBlock?.length; 843 b.visibleLength = scopeNode?.length;
813 b.initializer = serializeInitializerFunction(variable.initializer); 844 b.initializer = serializeInitializerFunction(variable.initializer);
814 this.variables.add(b); 845 this.variables.add(b);
815 } 846 }
816 } 847 }
817 848
818 @override 849 @override
819 void visitBlock(Block node) { 850 void visitBlock(Block node) {
820 Block oldBlock = enclosingBlock; 851 Block oldBlock = enclosingBlock;
821 enclosingBlock = node; 852 enclosingBlock = node;
822 super.visitBlock(node); 853 super.visitBlock(node);
823 enclosingBlock = oldBlock; 854 enclosingBlock = oldBlock;
824 } 855 }
825 856
826 @override 857 @override
858 void visitCatchClause(CatchClause node) {
859 SimpleIdentifier exception = node.exceptionParameter;
860 SimpleIdentifier st = node.stackTraceParameter;
861 serializeDeclaredIdentifier(
862 node, null, null, false, false, node.exceptionType, false, exception);
863 if (st != null) {
864 serializeDeclaredIdentifier(
865 node, null, null, false, false, null, false, st);
866 }
867 super.visitCatchClause(node);
868 }
869
870 @override
827 void visitClassDeclaration(ClassDeclaration node) { 871 void visitClassDeclaration(ClassDeclaration node) {
828 TypeName superclass = 872 TypeName superclass =
829 node.extendsClause == null ? null : node.extendsClause.superclass; 873 node.extendsClause == null ? null : node.extendsClause.superclass;
830 serializeClass( 874 serializeClass(
831 node, 875 node,
832 node.abstractKeyword, 876 node.abstractKeyword,
833 node.name.name, 877 node.name.name,
834 node.name.offset, 878 node.name.offset,
835 node.typeParameters, 879 node.typeParameters,
836 superclass, 880 superclass,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 @override 990 @override
947 void visitExportDirective(ExportDirective node) { 991 void visitExportDirective(ExportDirective node) {
948 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder( 992 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder(
949 uriOffset: node.uri.offset, uriEnd: node.uri.end, offset: node.offset); 993 uriOffset: node.uri.offset, uriEnd: node.uri.end, offset: node.offset);
950 b.annotations = serializeAnnotations(node.metadata); 994 b.annotations = serializeAnnotations(node.metadata);
951 exports.add(b); 995 exports.add(b);
952 } 996 }
953 997
954 @override 998 @override
955 void visitFieldDeclaration(FieldDeclaration node) { 999 void visitFieldDeclaration(FieldDeclaration node) {
956 serializeVariables(node.fields, node.staticKeyword != null, 1000 serializeVariables(null, node.fields, node.staticKeyword != null,
957 node.documentationComment, node.metadata, true); 1001 node.documentationComment, node.metadata, true);
958 } 1002 }
959 1003
960 @override 1004 @override
961 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) { 1005 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) {
962 UnlinkedParamBuilder b = serializeParameter(node); 1006 UnlinkedParamBuilder b = serializeParameter(node);
963 b.isInitializingFormal = true; 1007 b.isInitializingFormal = true;
964 if (node.type != null || node.parameters != null) { 1008 if (node.type != null || node.parameters != null) {
965 b.isFunctionTyped = node.parameters != null; 1009 b.isFunctionTyped = node.parameters != null;
966 if (node.parameters != null) { 1010 if (node.parameters != null) {
967 serializeFunctionTypedParameterDetails(b, node.type, node.parameters); 1011 serializeFunctionTypedParameterDetails(b, node.type, node.parameters);
968 } else { 1012 } else {
969 b.type = serializeTypeName(node.type); 1013 b.type = serializeTypeName(node.type);
970 } 1014 }
971 } 1015 }
972 return b; 1016 return b;
973 } 1017 }
974 1018
975 @override 1019 @override
1020 void visitForEachStatement(ForEachStatement node) {
1021 DeclaredIdentifier loopVariable = node.loopVariable;
1022 serializeDeclaredIdentifier(
1023 node,
1024 loopVariable.documentationComment,
1025 loopVariable.metadata,
1026 loopVariable.isFinal,
1027 loopVariable.isConst,
1028 loopVariable.type,
1029 true,
1030 loopVariable.identifier);
1031 super.visitForEachStatement(node);
1032 }
1033
1034 @override
1035 void visitForStatement(ForStatement node) {
1036 serializeVariables(node, node.variables, false, null, null, false);
1037 super.visitForStatement(node);
1038 }
1039
1040 @override
976 void visitFunctionDeclaration(FunctionDeclaration node) { 1041 void visitFunctionDeclaration(FunctionDeclaration node) {
977 executables.add(serializeExecutable( 1042 executables.add(serializeExecutable(
978 node, 1043 node,
979 node.name.name, 1044 node.name.name,
980 node.name.offset, 1045 node.name.offset,
981 node.isGetter, 1046 node.isGetter,
982 node.isSetter, 1047 node.isSetter,
983 node.returnType, 1048 node.returnType,
984 node.functionExpression.parameters, 1049 node.functionExpression.parameters,
985 node.functionExpression.body, 1050 node.functionExpression.body,
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 1188
1124 @override 1189 @override
1125 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) { 1190 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) {
1126 UnlinkedParamBuilder b = serializeParameter(node); 1191 UnlinkedParamBuilder b = serializeParameter(node);
1127 b.type = serializeTypeName(node.type); 1192 b.type = serializeTypeName(node.type);
1128 return b; 1193 return b;
1129 } 1194 }
1130 1195
1131 @override 1196 @override
1132 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { 1197 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
1133 serializeVariables( 1198 serializeVariables(null, node.variables, false, node.documentationComment,
1134 node.variables, false, node.documentationComment, node.metadata, false); 1199 node.metadata, false);
1135 } 1200 }
1136 1201
1137 @override 1202 @override
1138 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) { 1203 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) {
1139 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); 1204 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder();
1140 b.name = node.name.name; 1205 b.name = node.name.name;
1141 b.nameOffset = node.name.offset; 1206 b.nameOffset = node.name.offset;
1142 if (node.bound != null) { 1207 if (node.bound != null) {
1143 b.bound = serializeTypeName(node.bound); 1208 b.bound = serializeTypeName(node.bound);
1144 } 1209 }
1145 b.annotations = serializeAnnotations(node.metadata); 1210 b.annotations = serializeAnnotations(node.metadata);
1146 b.codeRange = serializeCodeRange(node); 1211 b.codeRange = serializeCodeRange(node);
1147 return b; 1212 return b;
1148 } 1213 }
1149 1214
1150 @override 1215 @override
1151 void visitVariableDeclarationStatement(VariableDeclarationStatement node) { 1216 void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
1152 serializeVariables(node.variables, false, null, null, false); 1217 serializeVariables(
1218 enclosingBlock, node.variables, false, null, null, false);
1153 } 1219 }
1154 1220
1155 /** 1221 /**
1156 * Helper method to determine if a given [typeName] refers to `dynamic`. 1222 * Helper method to determine if a given [typeName] refers to `dynamic`.
1157 */ 1223 */
1158 static bool isDynamic(TypeName typeName) { 1224 static bool isDynamic(TypeName typeName) {
1159 Identifier name = typeName.name; 1225 Identifier name = typeName.name;
1160 return name is SimpleIdentifier && name.name == 'dynamic'; 1226 return name is SimpleIdentifier && name.name == 'dynamic';
1161 } 1227 }
1162 } 1228 }
1163 1229
1164 /** 1230 /**
1165 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. 1231 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s.
1166 */ 1232 */
1167 class _TypeParameterScope extends _Scope { 1233 class _TypeParameterScope extends _Scope {
1168 /** 1234 /**
1169 * Get the number of [_ScopedTypeParameter]s defined in this 1235 * Get the number of [_ScopedTypeParameter]s defined in this
1170 * [_TypeParameterScope]. 1236 * [_TypeParameterScope].
1171 */ 1237 */
1172 int get length => _definedNames.length; 1238 int get length => _definedNames.length;
1173 } 1239 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | pkg/analyzer/test/generated/all_the_rest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698