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 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 | 339 |
340 /** | 340 /** |
341 * If an expression is being serialized which can contain closures, map whose | 341 * If an expression is being serialized which can contain closures, map whose |
342 * keys are the offsets of local function nodes representing those closures, | 342 * keys are the offsets of local function nodes representing those closures, |
343 * and whose values are indices of those local functions relative to their | 343 * and whose values are indices of those local functions relative to their |
344 * siblings. | 344 * siblings. |
345 */ | 345 */ |
346 Map<int, int> _localClosureIndexMap; | 346 Map<int, int> _localClosureIndexMap; |
347 | 347 |
348 /** | 348 /** |
| 349 * Indicates whether closure function bodies should be serialized. This flag |
| 350 * is set while visiting the bodies of initializer expressions that will be |
| 351 * needed by type inference. |
| 352 */ |
| 353 bool _serializeClosureBodyExprs = false; |
| 354 |
| 355 /** |
349 * Create a slot id for storing a propagated or inferred type or const cycle | 356 * Create a slot id for storing a propagated or inferred type or const cycle |
350 * info. | 357 * info. |
351 */ | 358 */ |
352 int assignSlot() => ++numSlots; | 359 int assignSlot() => ++numSlots; |
353 | 360 |
354 /** | 361 /** |
355 * Build a [_Scope] object containing the names defined within the body of a | 362 * Build a [_Scope] object containing the names defined within the body of a |
356 * class declaration. | 363 * class declaration. |
357 */ | 364 */ |
358 _Scope buildClassMemberScope( | 365 _Scope buildClassMemberScope( |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 .replaceAll('\r\n', '\n'); | 580 .replaceAll('\r\n', '\n'); |
574 return new UnlinkedDocumentationCommentBuilder( | 581 return new UnlinkedDocumentationCommentBuilder( |
575 text: text, | 582 text: text, |
576 offset: documentationComment.offset, | 583 offset: documentationComment.offset, |
577 length: documentationComment.length); | 584 length: documentationComment.length); |
578 } | 585 } |
579 | 586 |
580 /** | 587 /** |
581 * Serialize a [FunctionDeclaration] or [MethodDeclaration] into an | 588 * Serialize a [FunctionDeclaration] or [MethodDeclaration] into an |
582 * [UnlinkedExecutable]. | 589 * [UnlinkedExecutable]. |
| 590 * |
| 591 * If [serializeBodyExpr] is `true`, then the function definition is stored |
| 592 * in [UnlinkedExecutableBuilder.bodyExpr]. |
583 */ | 593 */ |
584 UnlinkedExecutableBuilder serializeExecutable( | 594 UnlinkedExecutableBuilder serializeExecutable( |
585 AstNode node, | 595 AstNode node, |
586 String name, | 596 String name, |
587 int nameOffset, | 597 int nameOffset, |
588 bool isGetter, | 598 bool isGetter, |
589 bool isSetter, | 599 bool isSetter, |
590 TypeName returnType, | 600 TypeName returnType, |
591 FormalParameterList formalParameters, | 601 FormalParameterList formalParameters, |
592 FunctionBody body, | 602 FunctionBody body, |
593 bool isTopLevel, | 603 bool isTopLevel, |
594 bool isDeclaredStatic, | 604 bool isDeclaredStatic, |
595 Comment documentationComment, | 605 Comment documentationComment, |
596 NodeList<Annotation> annotations, | 606 NodeList<Annotation> annotations, |
597 TypeParameterList typeParameters, | 607 TypeParameterList typeParameters, |
598 bool isExternal) { | 608 bool isExternal, |
| 609 bool serializeBodyExpr) { |
599 int oldScopesLength = scopes.length; | 610 int oldScopesLength = scopes.length; |
600 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); | 611 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); |
601 scopes.add(typeParameterScope); | 612 scopes.add(typeParameterScope); |
602 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); | 613 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); |
603 String nameString = name; | 614 String nameString = name; |
604 if (isGetter) { | 615 if (isGetter) { |
605 b.kind = UnlinkedExecutableKind.getter; | 616 b.kind = UnlinkedExecutableKind.getter; |
606 } else if (isSetter) { | 617 } else if (isSetter) { |
607 b.kind = UnlinkedExecutableKind.setter; | 618 b.kind = UnlinkedExecutableKind.setter; |
608 nameString = '$nameString='; | 619 nameString = '$nameString='; |
(...skipping 27 matching lines...) Expand all Loading... |
636 } | 647 } |
637 } | 648 } |
638 b.documentationComment = serializeDocumentation(documentationComment); | 649 b.documentationComment = serializeDocumentation(documentationComment); |
639 b.annotations = serializeAnnotations(annotations); | 650 b.annotations = serializeAnnotations(annotations); |
640 b.codeRange = serializeCodeRange(node); | 651 b.codeRange = serializeCodeRange(node); |
641 if (returnType == null && !isSemanticallyStatic) { | 652 if (returnType == null && !isSemanticallyStatic) { |
642 b.inferredReturnTypeSlot = assignSlot(); | 653 b.inferredReturnTypeSlot = assignSlot(); |
643 } | 654 } |
644 b.visibleOffset = enclosingBlock?.offset; | 655 b.visibleOffset = enclosingBlock?.offset; |
645 b.visibleLength = enclosingBlock?.length; | 656 b.visibleLength = enclosingBlock?.length; |
646 serializeFunctionBody(b, null, body, false); | 657 serializeFunctionBody(b, null, body, serializeBodyExpr); |
647 scopes.removeLast(); | 658 scopes.removeLast(); |
648 assert(scopes.length == oldScopesLength); | 659 assert(scopes.length == oldScopesLength); |
649 return b; | 660 return b; |
650 } | 661 } |
651 | 662 |
652 /** | 663 /** |
653 * Record local functions and variables into the given executable. The given | 664 * Record local functions and variables into the given executable. The given |
654 * [body] is usually an actual [FunctionBody], but may be an [Expression] | 665 * [body] is usually an actual [FunctionBody], but may be an [Expression] |
655 * when we process a synthetic variable initializer function. | 666 * when we process a synthetic variable initializer function. |
656 * | 667 * |
657 * If [initializers] is non-`null`, closures occurring inside the initializers | 668 * If [initializers] is non-`null`, closures occurring inside the initializers |
658 * are serialized first. | 669 * are serialized first. |
659 * | 670 * |
660 * If [serializeBodyExpr] is `true`, then the function definition is stored | 671 * If [serializeBodyExpr] is `true`, then the function definition is stored |
661 * in [UnlinkedExecutableBuilder.bodyExpr]. | 672 * in [UnlinkedExecutableBuilder.bodyExpr], and closures occurring inside |
| 673 * [initializers] and [body] have their function bodies serialized as well. |
662 * | 674 * |
663 * The return value is a map whose keys are the offsets of local function | 675 * The return value is a map whose keys are the offsets of local function |
664 * nodes representing closures inside [initializers] and [body], and whose | 676 * nodes representing closures inside [initializers] and [body], and whose |
665 * values are the indices of those local functions relative to their siblings. | 677 * values are the indices of those local functions relative to their siblings. |
666 */ | 678 */ |
667 Map<int, int> serializeFunctionBody( | 679 Map<int, int> serializeFunctionBody( |
668 UnlinkedExecutableBuilder b, | 680 UnlinkedExecutableBuilder b, |
669 List<ConstructorInitializer> initializers, | 681 List<ConstructorInitializer> initializers, |
670 AstNode body, | 682 AstNode body, |
671 bool serializeBodyExpr) { | 683 bool serializeBodyExpr) { |
672 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { | 684 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { |
673 for (UnlinkedParamBuilder parameter in b.parameters) { | 685 for (UnlinkedParamBuilder parameter in b.parameters) { |
674 parameter.visibleOffset = body.offset; | 686 parameter.visibleOffset = body.offset; |
675 parameter.visibleLength = body.length; | 687 parameter.visibleLength = body.length; |
676 } | 688 } |
677 } | 689 } |
678 List<UnlinkedExecutableBuilder> oldExecutables = executables; | 690 List<UnlinkedExecutableBuilder> oldExecutables = executables; |
679 List<UnlinkedLabelBuilder> oldLabels = labels; | 691 List<UnlinkedLabelBuilder> oldLabels = labels; |
680 List<UnlinkedVariableBuilder> oldVariables = variables; | 692 List<UnlinkedVariableBuilder> oldVariables = variables; |
681 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap; | 693 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap; |
| 694 bool oldSerializeClosureBodyExprs = _serializeClosureBodyExprs; |
682 executables = <UnlinkedExecutableBuilder>[]; | 695 executables = <UnlinkedExecutableBuilder>[]; |
683 labels = <UnlinkedLabelBuilder>[]; | 696 labels = <UnlinkedLabelBuilder>[]; |
684 variables = <UnlinkedVariableBuilder>[]; | 697 variables = <UnlinkedVariableBuilder>[]; |
685 _localClosureIndexMap = <int, int>{}; | 698 _localClosureIndexMap = <int, int>{}; |
| 699 _serializeClosureBodyExprs = serializeBodyExpr; |
686 if (initializers != null) { | 700 if (initializers != null) { |
687 for (ConstructorInitializer initializer in initializers) { | 701 for (ConstructorInitializer initializer in initializers) { |
688 initializer.accept(this); | 702 initializer.accept(this); |
689 } | 703 } |
690 } | 704 } |
691 body.accept(this); | 705 body.accept(this); |
692 if (serializeBodyExpr) { | 706 if (serializeBodyExpr) { |
693 if (body is Expression) { | 707 if (body is Expression) { |
694 b.bodyExpr = serializeConstExpr(_localClosureIndexMap, body); | 708 b.bodyExpr = serializeConstExpr(_localClosureIndexMap, body); |
| 709 } else if (body is ExpressionFunctionBody) { |
| 710 b.bodyExpr = serializeConstExpr(_localClosureIndexMap, body.expression); |
695 } else { | 711 } else { |
696 // TODO(paulberry): serialize other types of function bodies. | 712 // TODO(paulberry): serialize other types of function bodies. |
697 } | 713 } |
698 } | 714 } |
699 b.localFunctions = executables; | 715 b.localFunctions = executables; |
700 b.localLabels = labels; | 716 b.localLabels = labels; |
701 b.localVariables = variables; | 717 b.localVariables = variables; |
702 Map<int, int> localClosureIndexMap = _localClosureIndexMap; | 718 Map<int, int> localClosureIndexMap = _localClosureIndexMap; |
703 executables = oldExecutables; | 719 executables = oldExecutables; |
704 labels = oldLabels; | 720 labels = oldLabels; |
705 variables = oldVariables; | 721 variables = oldVariables; |
706 _localClosureIndexMap = oldLocalClosureIndexMap; | 722 _localClosureIndexMap = oldLocalClosureIndexMap; |
| 723 _serializeClosureBodyExprs = oldSerializeClosureBodyExprs; |
707 return localClosureIndexMap; | 724 return localClosureIndexMap; |
708 } | 725 } |
709 | 726 |
710 /** | 727 /** |
711 * Serialize the return type and parameters of a function-typed formal | 728 * Serialize the return type and parameters of a function-typed formal |
712 * parameter and store them in [b]. | 729 * parameter and store them in [b]. |
713 */ | 730 */ |
714 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, | 731 void serializeFunctionTypedParameterDetails(UnlinkedParamBuilder b, |
715 TypeName returnType, FormalParameterList parameters) { | 732 TypeName returnType, FormalParameterList parameters) { |
716 EntityRefBuilder serializedReturnType = serializeTypeName(returnType); | 733 EntityRefBuilder serializedReturnType = serializeTypeName(returnType); |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 node.isGetter, | 1168 node.isGetter, |
1152 node.isSetter, | 1169 node.isSetter, |
1153 node.returnType, | 1170 node.returnType, |
1154 node.functionExpression.parameters, | 1171 node.functionExpression.parameters, |
1155 node.functionExpression.body, | 1172 node.functionExpression.body, |
1156 true, | 1173 true, |
1157 false, | 1174 false, |
1158 node.documentationComment, | 1175 node.documentationComment, |
1159 node.metadata, | 1176 node.metadata, |
1160 node.functionExpression.typeParameters, | 1177 node.functionExpression.typeParameters, |
1161 node.externalKeyword != null)); | 1178 node.externalKeyword != null, |
| 1179 false)); |
1162 } | 1180 } |
1163 | 1181 |
1164 @override | 1182 @override |
1165 void visitFunctionExpression(FunctionExpression node) { | 1183 void visitFunctionExpression(FunctionExpression node) { |
1166 if (node.parent is! FunctionDeclaration) { | 1184 if (node.parent is! FunctionDeclaration) { |
1167 if (_localClosureIndexMap != null) { | 1185 if (_localClosureIndexMap != null) { |
1168 _localClosureIndexMap[node.offset] = executables.length; | 1186 _localClosureIndexMap[node.offset] = executables.length; |
1169 } | 1187 } |
1170 executables.add(serializeExecutable( | 1188 executables.add(serializeExecutable( |
1171 node, | 1189 node, |
1172 null, | 1190 null, |
1173 node.offset, | 1191 node.offset, |
1174 false, | 1192 false, |
1175 false, | 1193 false, |
1176 null, | 1194 null, |
1177 node.parameters, | 1195 node.parameters, |
1178 node.body, | 1196 node.body, |
1179 false, | 1197 false, |
1180 false, | 1198 false, |
1181 null, | 1199 null, |
1182 null, | 1200 null, |
1183 node.typeParameters, | 1201 node.typeParameters, |
1184 false)); | 1202 false, |
| 1203 _serializeClosureBodyExprs)); |
1185 } | 1204 } |
1186 } | 1205 } |
1187 | 1206 |
1188 @override | 1207 @override |
1189 void visitFunctionTypeAlias(FunctionTypeAlias node) { | 1208 void visitFunctionTypeAlias(FunctionTypeAlias node) { |
1190 int oldScopesLength = scopes.length; | 1209 int oldScopesLength = scopes.length; |
1191 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); | 1210 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); |
1192 scopes.add(typeParameterScope); | 1211 scopes.add(typeParameterScope); |
1193 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 1212 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
1194 b.name = node.name.name; | 1213 b.name = node.name.name; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1273 node.isGetter, | 1292 node.isGetter, |
1274 node.isSetter, | 1293 node.isSetter, |
1275 node.returnType, | 1294 node.returnType, |
1276 node.parameters, | 1295 node.parameters, |
1277 node.body, | 1296 node.body, |
1278 false, | 1297 false, |
1279 node.isStatic, | 1298 node.isStatic, |
1280 node.documentationComment, | 1299 node.documentationComment, |
1281 node.metadata, | 1300 node.metadata, |
1282 node.typeParameters, | 1301 node.typeParameters, |
1283 node.externalKeyword != null)); | 1302 node.externalKeyword != null, |
| 1303 false)); |
1284 } | 1304 } |
1285 | 1305 |
1286 @override | 1306 @override |
1287 void visitPartDirective(PartDirective node) { | 1307 void visitPartDirective(PartDirective node) { |
1288 parts.add(new UnlinkedPartBuilder( | 1308 parts.add(new UnlinkedPartBuilder( |
1289 uriOffset: node.uri.offset, | 1309 uriOffset: node.uri.offset, |
1290 uriEnd: node.uri.end, | 1310 uriEnd: node.uri.end, |
1291 annotations: serializeAnnotations(node.metadata))); | 1311 annotations: serializeAnnotations(node.metadata))); |
1292 } | 1312 } |
1293 | 1313 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1340 /** | 1360 /** |
1341 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 1361 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
1342 */ | 1362 */ |
1343 class _TypeParameterScope extends _Scope { | 1363 class _TypeParameterScope extends _Scope { |
1344 /** | 1364 /** |
1345 * Get the number of [_ScopedTypeParameter]s defined in this | 1365 * Get the number of [_ScopedTypeParameter]s defined in this |
1346 * [_TypeParameterScope]. | 1366 * [_TypeParameterScope]. |
1347 */ | 1367 */ |
1348 int get length => _definedNames.length; | 1368 int get length => _definedNames.length; |
1349 } | 1369 } |
OLD | NEW |