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/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/dart/element/type.dart' show DartType; | 10 import 'package:analyzer/dart/element/type.dart' show DartType; |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 * List of objects which should be written to [UnlinkedUnit.parts]. | 244 * List of objects which should be written to [UnlinkedUnit.parts]. |
245 */ | 245 */ |
246 final List<UnlinkedPartBuilder> parts = <UnlinkedPartBuilder>[]; | 246 final List<UnlinkedPartBuilder> parts = <UnlinkedPartBuilder>[]; |
247 | 247 |
248 /** | 248 /** |
249 * List of objects which should be written to [UnlinkedUnit.typedefs]. | 249 * List of objects which should be written to [UnlinkedUnit.typedefs]. |
250 */ | 250 */ |
251 final List<UnlinkedTypedefBuilder> typedefs = <UnlinkedTypedefBuilder>[]; | 251 final List<UnlinkedTypedefBuilder> typedefs = <UnlinkedTypedefBuilder>[]; |
252 | 252 |
253 /** | 253 /** |
254 * List of objects which should be written to [UnlinkedUnit.variables], | 254 * List of objects which should be written to [UnlinkedUnit.variables] or |
255 * [UnlinkedClass.fields] or [UnlinkedExecutable.localVariables]. | 255 * [UnlinkedClass.fields]. |
256 */ | 256 */ |
257 List<UnlinkedVariableBuilder> variables = <UnlinkedVariableBuilder>[]; | 257 List<UnlinkedVariableBuilder> variables = <UnlinkedVariableBuilder>[]; |
258 | 258 |
259 /** | 259 /** |
260 * The unlinked portion of the "imports table". This is the list of objects | 260 * The unlinked portion of the "imports table". This is the list of objects |
261 * which should be written to [UnlinkedUnit.imports]. | 261 * which should be written to [UnlinkedUnit.imports]. |
262 */ | 262 */ |
263 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; | 263 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; |
264 | 264 |
265 /** | 265 /** |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 UnlinkedExprBuilder serializeConstExpr( | 552 UnlinkedExprBuilder serializeConstExpr( |
553 bool forConst, Map<int, int> localClosureIndexMap, Expression expression, | 553 bool forConst, Map<int, int> localClosureIndexMap, Expression expression, |
554 [Set<String> parameterNames]) { | 554 [Set<String> parameterNames]) { |
555 _ConstExprSerializer serializer = new _ConstExprSerializer( | 555 _ConstExprSerializer serializer = new _ConstExprSerializer( |
556 forConst, this, localClosureIndexMap, parameterNames); | 556 forConst, this, localClosureIndexMap, parameterNames); |
557 serializer.serialize(expression); | 557 serializer.serialize(expression); |
558 return serializer.toBuilder(); | 558 return serializer.toBuilder(); |
559 } | 559 } |
560 | 560 |
561 /** | 561 /** |
562 * Serialize the given [declaredIdentifier] into [UnlinkedVariable], and | |
563 * store it in [variables]. | |
564 */ | |
565 void serializeDeclaredIdentifier( | |
566 AstNode scopeNode, | |
567 Comment documentationComment, | |
568 NodeList<Annotation> annotations, | |
569 bool isFinal, | |
570 bool isConst, | |
571 TypeAnnotation type, | |
572 bool assignPropagatedTypeSlot, | |
573 SimpleIdentifier declaredIdentifier) { | |
574 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); | |
575 b.isFinal = isFinal; | |
576 b.isConst = isConst; | |
577 b.name = declaredIdentifier.name; | |
578 b.nameOffset = declaredIdentifier.offset; | |
579 b.type = serializeTypeName(type); | |
580 b.documentationComment = serializeDocumentation(documentationComment); | |
581 b.annotations = serializeAnnotations(annotations); | |
582 b.codeRange = serializeCodeRange(declaredIdentifier); | |
583 if (assignPropagatedTypeSlot) { | |
584 b.propagatedTypeSlot = assignSlot(); | |
585 } | |
586 b.visibleOffset = scopeNode?.offset; | |
587 b.visibleLength = scopeNode?.length; | |
588 this.variables.add(b); | |
589 } | |
590 | |
591 /** | |
592 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. | 562 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. |
593 */ | 563 */ |
594 UnlinkedDocumentationCommentBuilder serializeDocumentation( | 564 UnlinkedDocumentationCommentBuilder serializeDocumentation( |
595 Comment documentationComment) { | 565 Comment documentationComment) { |
596 if (documentationComment == null) { | 566 if (documentationComment == null) { |
597 return null; | 567 return null; |
598 } | 568 } |
599 String text = documentationComment.tokens | 569 String text = documentationComment.tokens |
600 .map((Token t) => t.toString()) | 570 .map((Token t) => t.toString()) |
601 .join('\n') | 571 .join('\n') |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { | 697 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { |
728 for (UnlinkedParamBuilder parameter in b.parameters) { | 698 for (UnlinkedParamBuilder parameter in b.parameters) { |
729 if (!parameter.isInitializingFormal) { | 699 if (!parameter.isInitializingFormal) { |
730 parameter.visibleOffset = body.offset; | 700 parameter.visibleOffset = body.offset; |
731 parameter.visibleLength = body.length; | 701 parameter.visibleLength = body.length; |
732 } | 702 } |
733 } | 703 } |
734 } | 704 } |
735 List<UnlinkedExecutableBuilder> oldExecutables = executables; | 705 List<UnlinkedExecutableBuilder> oldExecutables = executables; |
736 List<UnlinkedLabelBuilder> oldLabels = labels; | 706 List<UnlinkedLabelBuilder> oldLabels = labels; |
737 List<UnlinkedVariableBuilder> oldVariables = variables; | |
738 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap; | 707 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap; |
739 bool oldSerializeClosureBodyExprs = _serializeClosureBodyExprs; | 708 bool oldSerializeClosureBodyExprs = _serializeClosureBodyExprs; |
740 executables = <UnlinkedExecutableBuilder>[]; | 709 executables = <UnlinkedExecutableBuilder>[]; |
741 labels = <UnlinkedLabelBuilder>[]; | 710 labels = <UnlinkedLabelBuilder>[]; |
742 variables = <UnlinkedVariableBuilder>[]; | |
743 _localClosureIndexMap = <int, int>{}; | 711 _localClosureIndexMap = <int, int>{}; |
744 _serializeClosureBodyExprs = serializeBodyExpr; | 712 _serializeClosureBodyExprs = serializeBodyExpr; |
745 if (initializers != null) { | 713 if (initializers != null) { |
746 for (ConstructorInitializer initializer in initializers) { | 714 for (ConstructorInitializer initializer in initializers) { |
747 initializer.accept(this); | 715 initializer.accept(this); |
748 } | 716 } |
749 } | 717 } |
750 if (serializeBody) { | 718 if (serializeBody) { |
751 body.accept(this); | 719 body.accept(this); |
752 } | 720 } |
753 if (serializeBodyExpr) { | 721 if (serializeBodyExpr) { |
754 if (body is Expression) { | 722 if (body is Expression) { |
755 b.bodyExpr = serializeConstExpr( | 723 b.bodyExpr = serializeConstExpr( |
756 forConst, _localClosureIndexMap, body, _parameterNames); | 724 forConst, _localClosureIndexMap, body, _parameterNames); |
757 } else if (body is ExpressionFunctionBody) { | 725 } else if (body is ExpressionFunctionBody) { |
758 b.bodyExpr = serializeConstExpr( | 726 b.bodyExpr = serializeConstExpr( |
759 forConst, _localClosureIndexMap, body.expression, _parameterNames); | 727 forConst, _localClosureIndexMap, body.expression, _parameterNames); |
760 } else { | 728 } else { |
761 // TODO(paulberry): serialize other types of function bodies. | 729 // TODO(paulberry): serialize other types of function bodies. |
762 } | 730 } |
763 } | 731 } |
764 b.localFunctions = executables; | 732 b.localFunctions = executables; |
765 b.localLabels = labels; | 733 b.localLabels = labels; |
766 b.localVariables = variables; | |
767 Map<int, int> localClosureIndexMap = _localClosureIndexMap; | 734 Map<int, int> localClosureIndexMap = _localClosureIndexMap; |
768 executables = oldExecutables; | 735 executables = oldExecutables; |
769 labels = oldLabels; | 736 labels = oldLabels; |
770 variables = oldVariables; | |
771 _localClosureIndexMap = oldLocalClosureIndexMap; | 737 _localClosureIndexMap = oldLocalClosureIndexMap; |
772 _serializeClosureBodyExprs = oldSerializeClosureBodyExprs; | 738 _serializeClosureBodyExprs = oldSerializeClosureBodyExprs; |
773 return localClosureIndexMap; | 739 return localClosureIndexMap; |
774 } | 740 } |
775 | 741 |
776 /** | 742 /** |
777 * Serialize the return type and parameters of a function-typed formal | 743 * Serialize the return type and parameters of a function-typed formal |
778 * parameter and store them in [b]. | 744 * parameter and store them in [b]. |
779 */ | 745 */ |
780 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, | 746 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 return typeParameters.typeParameters.map(visitTypeParameter).toList(); | 961 return typeParameters.typeParameters.map(visitTypeParameter).toList(); |
996 } | 962 } |
997 return const <UnlinkedTypeParamBuilder>[]; | 963 return const <UnlinkedTypeParamBuilder>[]; |
998 } | 964 } |
999 | 965 |
1000 /** | 966 /** |
1001 * Serialize the given [variables] into [UnlinkedVariable]s, and store them | 967 * Serialize the given [variables] into [UnlinkedVariable]s, and store them |
1002 * in [this.variables]. | 968 * in [this.variables]. |
1003 */ | 969 */ |
1004 void serializeVariables( | 970 void serializeVariables( |
1005 AstNode scopeNode, | |
1006 VariableDeclarationList variables, | 971 VariableDeclarationList variables, |
1007 bool isDeclaredStatic, | 972 bool isDeclaredStatic, |
1008 Comment documentationComment, | 973 Comment documentationComment, |
1009 NodeList<Annotation> annotations, | 974 NodeList<Annotation> annotations, |
1010 bool isField) { | 975 bool isField) { |
1011 bool isCovariant = isField | 976 bool isCovariant = isField |
1012 ? (variables.parent as FieldDeclaration).covariantKeyword != null | 977 ? (variables.parent as FieldDeclaration).covariantKeyword != null |
1013 : false; | 978 : false; |
1014 for (VariableDeclaration variable in variables.variables) { | 979 for (VariableDeclaration variable in variables.variables) { |
1015 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); | 980 UnlinkedVariableBuilder b = new UnlinkedVariableBuilder(); |
(...skipping 17 matching lines...) Expand all Loading... |
1033 } | 998 } |
1034 if (variable.initializer != null && | 999 if (variable.initializer != null && |
1035 (variables.isFinal || variables.isConst)) { | 1000 (variables.isFinal || variables.isConst)) { |
1036 b.propagatedTypeSlot = assignSlot(); | 1001 b.propagatedTypeSlot = assignSlot(); |
1037 } | 1002 } |
1038 bool isSemanticallyStatic = !isField || isDeclaredStatic; | 1003 bool isSemanticallyStatic = !isField || isDeclaredStatic; |
1039 if (variables.type == null && | 1004 if (variables.type == null && |
1040 (variable.initializer != null || !isSemanticallyStatic)) { | 1005 (variable.initializer != null || !isSemanticallyStatic)) { |
1041 b.inferredTypeSlot = assignSlot(); | 1006 b.inferredTypeSlot = assignSlot(); |
1042 } | 1007 } |
1043 b.visibleOffset = scopeNode?.offset; | |
1044 b.visibleLength = scopeNode?.length; | |
1045 this.variables.add(b); | 1008 this.variables.add(b); |
1046 } | 1009 } |
1047 } | 1010 } |
1048 | 1011 |
1049 @override | 1012 @override |
1050 void visitBlock(Block node) { | 1013 void visitBlock(Block node) { |
1051 Block oldBlock = enclosingBlock; | 1014 Block oldBlock = enclosingBlock; |
1052 enclosingBlock = node; | 1015 enclosingBlock = node; |
1053 super.visitBlock(node); | 1016 super.visitBlock(node); |
1054 enclosingBlock = oldBlock; | 1017 enclosingBlock = oldBlock; |
1055 } | 1018 } |
1056 | 1019 |
1057 @override | 1020 @override |
1058 void visitCatchClause(CatchClause node) { | |
1059 SimpleIdentifier exception = node.exceptionParameter; | |
1060 SimpleIdentifier st = node.stackTraceParameter; | |
1061 if (exception != null) { | |
1062 serializeDeclaredIdentifier( | |
1063 node, null, null, false, false, node.exceptionType, false, exception); | |
1064 } | |
1065 if (st != null) { | |
1066 serializeDeclaredIdentifier( | |
1067 node, null, null, false, false, null, false, st); | |
1068 } | |
1069 super.visitCatchClause(node); | |
1070 } | |
1071 | |
1072 @override | |
1073 void visitClassDeclaration(ClassDeclaration node) { | 1021 void visitClassDeclaration(ClassDeclaration node) { |
1074 TypeName superclass = | 1022 TypeName superclass = |
1075 node.extendsClause == null ? null : node.extendsClause.superclass; | 1023 node.extendsClause == null ? null : node.extendsClause.superclass; |
1076 serializeClass( | 1024 serializeClass( |
1077 node, | 1025 node, |
1078 node.abstractKeyword, | 1026 node.abstractKeyword, |
1079 node.name.name, | 1027 node.name.name, |
1080 node.name.offset, | 1028 node.name.offset, |
1081 node.typeParameters, | 1029 node.typeParameters, |
1082 superclass, | 1030 superclass, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1200 @override | 1148 @override |
1201 void visitExportDirective(ExportDirective node) { | 1149 void visitExportDirective(ExportDirective node) { |
1202 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder( | 1150 UnlinkedExportNonPublicBuilder b = new UnlinkedExportNonPublicBuilder( |
1203 uriOffset: node.uri.offset, uriEnd: node.uri.end, offset: node.offset); | 1151 uriOffset: node.uri.offset, uriEnd: node.uri.end, offset: node.offset); |
1204 b.annotations = serializeAnnotations(node.metadata); | 1152 b.annotations = serializeAnnotations(node.metadata); |
1205 exports.add(b); | 1153 exports.add(b); |
1206 } | 1154 } |
1207 | 1155 |
1208 @override | 1156 @override |
1209 void visitFieldDeclaration(FieldDeclaration node) { | 1157 void visitFieldDeclaration(FieldDeclaration node) { |
1210 serializeVariables(null, node.fields, node.staticKeyword != null, | 1158 serializeVariables(node.fields, node.staticKeyword != null, |
1211 node.documentationComment, node.metadata, true); | 1159 node.documentationComment, node.metadata, true); |
1212 } | 1160 } |
1213 | 1161 |
1214 @override | 1162 @override |
1215 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) { | 1163 UnlinkedParamBuilder visitFieldFormalParameter(FieldFormalParameter node) { |
1216 UnlinkedParamBuilder b = serializeParameter(node); | 1164 UnlinkedParamBuilder b = serializeParameter(node); |
1217 b.isInitializingFormal = true; | 1165 b.isInitializingFormal = true; |
1218 if (node.type != null || node.parameters != null) { | 1166 if (node.type != null || node.parameters != null) { |
1219 b.isFunctionTyped = node.parameters != null; | 1167 b.isFunctionTyped = node.parameters != null; |
1220 if (node.parameters != null) { | 1168 if (node.parameters != null) { |
1221 serializeFunctionTypedParameterDetails(b, node.type, node.parameters); | 1169 serializeFunctionTypedParameterDetails(b, node.type, node.parameters); |
1222 } else { | 1170 } else { |
1223 b.type = serializeTypeName(node.type); | 1171 b.type = serializeTypeName(node.type); |
1224 } | 1172 } |
1225 } | 1173 } |
1226 return b; | 1174 return b; |
1227 } | 1175 } |
1228 | 1176 |
1229 @override | 1177 @override |
1230 void visitForEachStatement(ForEachStatement node) { | |
1231 DeclaredIdentifier loopVariable = node.loopVariable; | |
1232 if (loopVariable != null) { | |
1233 serializeDeclaredIdentifier( | |
1234 node, | |
1235 loopVariable.documentationComment, | |
1236 loopVariable.metadata, | |
1237 loopVariable.isFinal, | |
1238 loopVariable.isConst, | |
1239 loopVariable.type, | |
1240 true, | |
1241 loopVariable.identifier); | |
1242 } | |
1243 super.visitForEachStatement(node); | |
1244 } | |
1245 | |
1246 @override | |
1247 void visitForStatement(ForStatement node) { | |
1248 VariableDeclarationList declaredVariables = node.variables; | |
1249 if (declaredVariables != null) { | |
1250 serializeVariables(node, declaredVariables, false, null, null, false); | |
1251 } | |
1252 super.visitForStatement(node); | |
1253 } | |
1254 | |
1255 @override | |
1256 void visitFunctionDeclaration(FunctionDeclaration node) { | 1178 void visitFunctionDeclaration(FunctionDeclaration node) { |
1257 executables.add(serializeExecutable( | 1179 executables.add(serializeExecutable( |
1258 node, | 1180 node, |
1259 node.name.name, | 1181 node.name.name, |
1260 node.name.offset, | 1182 node.name.offset, |
1261 node.isGetter, | 1183 node.isGetter, |
1262 node.isSetter, | 1184 node.isSetter, |
1263 node.returnType, | 1185 node.returnType, |
1264 node.functionExpression.parameters, | 1186 node.functionExpression.parameters, |
1265 node.functionExpression.body, | 1187 node.functionExpression.body, |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1444 | 1366 |
1445 @override | 1367 @override |
1446 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) { | 1368 UnlinkedParamBuilder visitSimpleFormalParameter(SimpleFormalParameter node) { |
1447 UnlinkedParamBuilder b = serializeParameter(node); | 1369 UnlinkedParamBuilder b = serializeParameter(node); |
1448 b.type = serializeTypeName(node.type); | 1370 b.type = serializeTypeName(node.type); |
1449 return b; | 1371 return b; |
1450 } | 1372 } |
1451 | 1373 |
1452 @override | 1374 @override |
1453 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { | 1375 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
1454 serializeVariables(null, node.variables, false, node.documentationComment, | 1376 serializeVariables( |
1455 node.metadata, false); | 1377 node.variables, false, node.documentationComment, node.metadata, false); |
1456 } | 1378 } |
1457 | 1379 |
1458 @override | 1380 @override |
1459 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) { | 1381 UnlinkedTypeParamBuilder visitTypeParameter(TypeParameter node) { |
1460 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); | 1382 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); |
1461 b.name = node.name.name; | 1383 b.name = node.name.name; |
1462 b.nameOffset = node.name.offset; | 1384 b.nameOffset = node.name.offset; |
1463 if (node.bound != null) { | 1385 if (node.bound != null) { |
1464 b.bound = serializeTypeName(node.bound); | 1386 b.bound = serializeTypeName(node.bound); |
1465 } | 1387 } |
1466 b.annotations = serializeAnnotations(node.metadata); | 1388 b.annotations = serializeAnnotations(node.metadata); |
1467 b.codeRange = serializeCodeRange(node); | 1389 b.codeRange = serializeCodeRange(node); |
1468 return b; | 1390 return b; |
1469 } | 1391 } |
1470 | 1392 |
1471 @override | 1393 @override |
1472 void visitVariableDeclarationStatement(VariableDeclarationStatement node) { | 1394 void visitVariableDeclarationStatement(VariableDeclarationStatement node) { |
1473 serializeVariables( | 1395 // TODO(scheglov) Remove when we stop serializing local functions. |
1474 enclosingBlock, node.variables, false, null, null, false); | |
1475 } | 1396 } |
1476 | 1397 |
1477 /** | 1398 /** |
1478 * Compute the API signature of the unit and record it. | 1399 * Compute the API signature of the unit and record it. |
1479 */ | 1400 */ |
1480 static void _computeApiSignature(UnlinkedUnitBuilder b) { | 1401 static void _computeApiSignature(UnlinkedUnitBuilder b) { |
1481 ApiSignature apiSignature = new ApiSignature(); | 1402 ApiSignature apiSignature = new ApiSignature(); |
1482 b.collectApiSignature(apiSignature); | 1403 b.collectApiSignature(apiSignature); |
1483 b.apiSignature = apiSignature.toByteList(); | 1404 b.apiSignature = apiSignature.toByteList(); |
1484 } | 1405 } |
1485 } | 1406 } |
1486 | 1407 |
1487 /** | 1408 /** |
1488 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 1409 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
1489 */ | 1410 */ |
1490 class _TypeParameterScope extends _Scope { | 1411 class _TypeParameterScope extends _Scope { |
1491 /** | 1412 /** |
1492 * Get the number of [_ScopedTypeParameter]s defined in this | 1413 * Get the number of [_ScopedTypeParameter]s defined in this |
1493 * [_TypeParameterScope]. | 1414 * [_TypeParameterScope]. |
1494 */ | 1415 */ |
1495 int get length => _definedNames.length; | 1416 int get length => _definedNames.length; |
1496 } | 1417 } |
OLD | NEW |