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

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

Issue 2485043004: Don't serialize elements of top-level function/constructor/method bodies. (Closed)
Patch Set: Created 4 years, 1 month 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/dart/element/type.dart' show DartType; 10 import 'package:analyzer/dart/element/type.dart' show DartType;
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 bool isSetter, 617 bool isSetter,
618 TypeName returnType, 618 TypeName returnType,
619 FormalParameterList formalParameters, 619 FormalParameterList formalParameters,
620 FunctionBody body, 620 FunctionBody body,
621 bool isTopLevel, 621 bool isTopLevel,
622 bool isDeclaredStatic, 622 bool isDeclaredStatic,
623 Comment documentationComment, 623 Comment documentationComment,
624 NodeList<Annotation> annotations, 624 NodeList<Annotation> annotations,
625 TypeParameterList typeParameters, 625 TypeParameterList typeParameters,
626 bool isExternal, 626 bool isExternal,
627 bool serializeBodyExpr) { 627 bool serializeBodyExpr,
628 bool serializeBody) {
628 int oldScopesLength = scopes.length; 629 int oldScopesLength = scopes.length;
629 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); 630 _TypeParameterScope typeParameterScope = new _TypeParameterScope();
630 scopes.add(typeParameterScope); 631 scopes.add(typeParameterScope);
631 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); 632 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder();
632 String nameString = name; 633 String nameString = name;
633 if (isGetter) { 634 if (isGetter) {
634 b.kind = UnlinkedExecutableKind.getter; 635 b.kind = UnlinkedExecutableKind.getter;
635 } else if (isSetter) { 636 } else if (isSetter) {
636 b.kind = UnlinkedExecutableKind.setter; 637 b.kind = UnlinkedExecutableKind.setter;
637 nameString = '$nameString='; 638 nameString = '$nameString=';
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 } 676 }
676 b.visibleOffset = enclosingBlock?.offset; 677 b.visibleOffset = enclosingBlock?.offset;
677 b.visibleLength = enclosingBlock?.length; 678 b.visibleLength = enclosingBlock?.length;
678 Set<String> oldParameterNames = _parameterNames; 679 Set<String> oldParameterNames = _parameterNames;
679 if (formalParameters != null && formalParameters.parameters.isNotEmpty) { 680 if (formalParameters != null && formalParameters.parameters.isNotEmpty) {
680 _parameterNames = 681 _parameterNames =
681 _parameterNames == null ? new Set<String>() : _parameterNames.toSet(); 682 _parameterNames == null ? new Set<String>() : _parameterNames.toSet();
682 _parameterNames.addAll(formalParameters.parameters 683 _parameterNames.addAll(formalParameters.parameters
683 .map((FormalParameter p) => p.identifier.name)); 684 .map((FormalParameter p) => p.identifier.name));
684 } 685 }
685 serializeFunctionBody(b, null, body, serializeBodyExpr); 686 serializeFunctionBody(b, null, body, serializeBodyExpr, serializeBody);
686 _parameterNames = oldParameterNames; 687 _parameterNames = oldParameterNames;
687 scopes.removeLast(); 688 scopes.removeLast();
688 assert(scopes.length == oldScopesLength); 689 assert(scopes.length == oldScopesLength);
689 return b; 690 return b;
690 } 691 }
691 692
692 /** 693 /**
693 * Record local functions and variables into the given executable. The given 694 * Record local functions and variables into the given executable. The given
694 * [body] is usually an actual [FunctionBody], but may be an [Expression] 695 * [body] is usually an actual [FunctionBody], but may be an [Expression]
695 * when we process a synthetic variable initializer function. 696 * when we process a synthetic variable initializer function.
696 * 697 *
697 * If [initializers] is non-`null`, closures occurring inside the initializers 698 * If [initializers] is non-`null`, closures occurring inside the initializers
698 * are serialized first. 699 * are serialized first.
699 * 700 *
700 * If [serializeBodyExpr] is `true`, then the function definition is stored 701 * If [serializeBodyExpr] is `true`, then the function definition is stored
701 * in [UnlinkedExecutableBuilder.bodyExpr], and closures occurring inside 702 * in [UnlinkedExecutableBuilder.bodyExpr], and closures occurring inside
702 * [initializers] and [body] have their function bodies serialized as well. 703 * [initializers] and [body] have their function bodies serialized as well.
703 * 704 *
704 * The return value is a map whose keys are the offsets of local function 705 * The return value is a map whose keys are the offsets of local function
705 * nodes representing closures inside [initializers] and [body], and whose 706 * nodes representing closures inside [initializers] and [body], and whose
706 * values are the indices of those local functions relative to their siblings. 707 * values are the indices of those local functions relative to their siblings.
707 */ 708 */
708 Map<int, int> serializeFunctionBody( 709 Map<int, int> serializeFunctionBody(
709 UnlinkedExecutableBuilder b, 710 UnlinkedExecutableBuilder b,
710 List<ConstructorInitializer> initializers, 711 List<ConstructorInitializer> initializers,
711 AstNode body, 712 AstNode body,
712 bool serializeBodyExpr) { 713 bool serializeBodyExpr,
714 bool serializeBody) {
713 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { 715 if (body is BlockFunctionBody || body is ExpressionFunctionBody) {
714 for (UnlinkedParamBuilder parameter in b.parameters) { 716 for (UnlinkedParamBuilder parameter in b.parameters) {
715 parameter.visibleOffset = body.offset; 717 parameter.visibleOffset = body.offset;
716 parameter.visibleLength = body.length; 718 parameter.visibleLength = body.length;
717 } 719 }
718 } 720 }
719 List<UnlinkedExecutableBuilder> oldExecutables = executables; 721 List<UnlinkedExecutableBuilder> oldExecutables = executables;
720 List<UnlinkedLabelBuilder> oldLabels = labels; 722 List<UnlinkedLabelBuilder> oldLabels = labels;
721 List<UnlinkedVariableBuilder> oldVariables = variables; 723 List<UnlinkedVariableBuilder> oldVariables = variables;
722 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap; 724 Map<int, int> oldLocalClosureIndexMap = _localClosureIndexMap;
723 bool oldSerializeClosureBodyExprs = _serializeClosureBodyExprs; 725 bool oldSerializeClosureBodyExprs = _serializeClosureBodyExprs;
724 executables = <UnlinkedExecutableBuilder>[]; 726 executables = <UnlinkedExecutableBuilder>[];
725 labels = <UnlinkedLabelBuilder>[]; 727 labels = <UnlinkedLabelBuilder>[];
726 variables = <UnlinkedVariableBuilder>[]; 728 variables = <UnlinkedVariableBuilder>[];
727 _localClosureIndexMap = <int, int>{}; 729 _localClosureIndexMap = <int, int>{};
728 _serializeClosureBodyExprs = serializeBodyExpr; 730 _serializeClosureBodyExprs = serializeBodyExpr;
729 if (initializers != null) { 731 if (initializers != null) {
730 for (ConstructorInitializer initializer in initializers) { 732 for (ConstructorInitializer initializer in initializers) {
731 initializer.accept(this); 733 initializer.accept(this);
732 } 734 }
733 } 735 }
734 body.accept(this); 736 if (serializeBody) {
737 body.accept(this);
738 }
735 if (serializeBodyExpr) { 739 if (serializeBodyExpr) {
736 if (body is Expression) { 740 if (body is Expression) {
737 b.bodyExpr = 741 b.bodyExpr =
738 serializeConstExpr(_localClosureIndexMap, body, _parameterNames); 742 serializeConstExpr(_localClosureIndexMap, body, _parameterNames);
739 } else if (body is ExpressionFunctionBody) { 743 } else if (body is ExpressionFunctionBody) {
740 b.bodyExpr = serializeConstExpr( 744 b.bodyExpr = serializeConstExpr(
741 _localClosureIndexMap, body.expression, _parameterNames); 745 _localClosureIndexMap, body.expression, _parameterNames);
742 } else { 746 } else {
743 // TODO(paulberry): serialize other types of function bodies. 747 // TODO(paulberry): serialize other types of function bodies.
744 } 748 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 * If [serializeBodyExpr] is `true`, then the initializer expression is stored 784 * If [serializeBodyExpr] is `true`, then the initializer expression is stored
781 * in [UnlinkedExecutableBuilder.bodyExpr]. 785 * in [UnlinkedExecutableBuilder.bodyExpr].
782 */ 786 */
783 UnlinkedExecutableBuilder serializeInitializerFunction( 787 UnlinkedExecutableBuilder serializeInitializerFunction(
784 Expression expression, bool serializeBodyExpr) { 788 Expression expression, bool serializeBodyExpr) {
785 if (expression == null) { 789 if (expression == null) {
786 return null; 790 return null;
787 } 791 }
788 UnlinkedExecutableBuilder initializer = 792 UnlinkedExecutableBuilder initializer =
789 new UnlinkedExecutableBuilder(nameOffset: expression.offset); 793 new UnlinkedExecutableBuilder(nameOffset: expression.offset);
790 serializeFunctionBody(initializer, null, expression, serializeBodyExpr); 794 serializeFunctionBody(
795 initializer, null, expression, serializeBodyExpr, true);
791 initializer.inferredReturnTypeSlot = assignSlot(); 796 initializer.inferredReturnTypeSlot = assignSlot();
792 return initializer; 797 return initializer;
793 } 798 }
794 799
795 /** 800 /**
796 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or 801 * Serialize a [FieldFormalParameter], [FunctionTypedFormalParameter], or
797 * [SimpleFormalParameter] into an [UnlinkedParam]. 802 * [SimpleFormalParameter] into an [UnlinkedParam].
798 */ 803 */
799 UnlinkedParamBuilder serializeParameter(NormalFormalParameter node) { 804 UnlinkedParamBuilder serializeParameter(NormalFormalParameter node) {
800 UnlinkedParamBuilder b = new UnlinkedParamBuilder(); 805 UnlinkedParamBuilder b = new UnlinkedParamBuilder();
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 } 1094 }
1090 if (node.constKeyword != null) { 1095 if (node.constKeyword != null) {
1091 b.isConst = true; 1096 b.isConst = true;
1092 b.constCycleSlot = assignSlot(); 1097 b.constCycleSlot = assignSlot();
1093 } 1098 }
1094 b.isExternal = node.externalKeyword != null; 1099 b.isExternal = node.externalKeyword != null;
1095 b.documentationComment = serializeDocumentation(node.documentationComment); 1100 b.documentationComment = serializeDocumentation(node.documentationComment);
1096 b.annotations = serializeAnnotations(node.metadata); 1101 b.annotations = serializeAnnotations(node.metadata);
1097 b.codeRange = serializeCodeRange(node); 1102 b.codeRange = serializeCodeRange(node);
1098 Map<int, int> localClosureIndexMap = serializeFunctionBody( 1103 Map<int, int> localClosureIndexMap = serializeFunctionBody(
1099 b, node.initializers, node.body, node.constKeyword != null); 1104 b, node.initializers, node.body, node.constKeyword != null, false);
1100 if (node.constKeyword != null) { 1105 if (node.constKeyword != null) {
1101 Set<String> constructorParameterNames = 1106 Set<String> constructorParameterNames =
1102 node.parameters.parameters.map((p) => p.identifier.name).toSet(); 1107 node.parameters.parameters.map((p) => p.identifier.name).toSet();
1103 b.constantInitializers = node.initializers 1108 b.constantInitializers = node.initializers
1104 .map((ConstructorInitializer initializer) => 1109 .map((ConstructorInitializer initializer) =>
1105 serializeConstructorInitializer(initializer, (Expression expr) { 1110 serializeConstructorInitializer(initializer, (Expression expr) {
1106 return serializeConstExpr( 1111 return serializeConstExpr(
1107 localClosureIndexMap, expr, constructorParameterNames); 1112 localClosureIndexMap, expr, constructorParameterNames);
1108 })) 1113 }))
1109 .toList(); 1114 .toList();
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 node.isSetter, 1212 node.isSetter,
1208 node.returnType, 1213 node.returnType,
1209 node.functionExpression.parameters, 1214 node.functionExpression.parameters,
1210 node.functionExpression.body, 1215 node.functionExpression.body,
1211 true, 1216 true,
1212 false, 1217 false,
1213 node.documentationComment, 1218 node.documentationComment,
1214 node.metadata, 1219 node.metadata,
1215 node.functionExpression.typeParameters, 1220 node.functionExpression.typeParameters,
1216 node.externalKeyword != null, 1221 node.externalKeyword != null,
1217 false)); 1222 false,
1223 node.parent is FunctionDeclarationStatement));
1218 } 1224 }
1219 1225
1220 @override 1226 @override
1221 void visitFunctionExpression(FunctionExpression node) { 1227 void visitFunctionExpression(FunctionExpression node) {
1222 if (node.parent is! FunctionDeclaration) { 1228 if (node.parent is! FunctionDeclaration) {
1223 if (_localClosureIndexMap != null) { 1229 if (_localClosureIndexMap != null) {
1224 _localClosureIndexMap[node.offset] = executables.length; 1230 _localClosureIndexMap[node.offset] = executables.length;
1225 } 1231 }
1226 executables.add(serializeExecutable( 1232 executables.add(serializeExecutable(
1227 node, 1233 node,
1228 null, 1234 null,
1229 node.offset, 1235 node.offset,
1230 false, 1236 false,
1231 false, 1237 false,
1232 null, 1238 null,
1233 node.parameters, 1239 node.parameters,
1234 node.body, 1240 node.body,
1235 false, 1241 false,
1236 false, 1242 false,
1237 null, 1243 null,
1238 null, 1244 null,
1239 node.typeParameters, 1245 node.typeParameters,
1240 false, 1246 false,
1241 _serializeClosureBodyExprs)); 1247 _serializeClosureBodyExprs,
1248 true));
1242 } 1249 }
1243 } 1250 }
1244 1251
1245 @override 1252 @override
1246 void visitFunctionTypeAlias(FunctionTypeAlias node) { 1253 void visitFunctionTypeAlias(FunctionTypeAlias node) {
1247 int oldScopesLength = scopes.length; 1254 int oldScopesLength = scopes.length;
1248 _TypeParameterScope typeParameterScope = new _TypeParameterScope(); 1255 _TypeParameterScope typeParameterScope = new _TypeParameterScope();
1249 scopes.add(typeParameterScope); 1256 scopes.add(typeParameterScope);
1250 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); 1257 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder();
1251 b.name = node.name.name; 1258 b.name = node.name.name;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 node.isSetter, 1339 node.isSetter,
1333 node.returnType, 1340 node.returnType,
1334 node.parameters, 1341 node.parameters,
1335 node.body, 1342 node.body,
1336 false, 1343 false,
1337 node.isStatic, 1344 node.isStatic,
1338 node.documentationComment, 1345 node.documentationComment,
1339 node.metadata, 1346 node.metadata,
1340 node.typeParameters, 1347 node.typeParameters,
1341 node.externalKeyword != null, 1348 node.externalKeyword != null,
1349 false,
1342 false)); 1350 false));
1343 } 1351 }
1344 1352
1345 @override 1353 @override
1346 void visitPartDirective(PartDirective node) { 1354 void visitPartDirective(PartDirective node) {
1347 parts.add(new UnlinkedPartBuilder( 1355 parts.add(new UnlinkedPartBuilder(
1348 uriOffset: node.uri.offset, 1356 uriOffset: node.uri.offset,
1349 uriEnd: node.uri.end, 1357 uriEnd: node.uri.end,
1350 annotations: serializeAnnotations(node.metadata))); 1358 annotations: serializeAnnotations(node.metadata)));
1351 } 1359 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1409 /** 1417 /**
1410 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. 1418 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s.
1411 */ 1419 */
1412 class _TypeParameterScope extends _Scope { 1420 class _TypeParameterScope extends _Scope {
1413 /** 1421 /**
1414 * Get the number of [_ScopedTypeParameter]s defined in this 1422 * Get the number of [_ScopedTypeParameter]s defined in this
1415 * [_TypeParameterScope]. 1423 * [_TypeParameterScope].
1416 */ 1424 */
1417 int get length => _definedNames.length; 1425 int get length => _definedNames.length;
1418 } 1426 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698