| 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/visitor.dart'; | 8 import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/utilities_dart.dart'; | 10 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 11 import 'package:analyzer/src/summary/format.dart'; | 11 import 'package:analyzer/src/summary/format.dart'; |
| 12 import 'package:analyzer/src/summary/idl.dart'; | 12 import 'package:analyzer/src/summary/idl.dart'; |
| 13 import 'package:analyzer/src/summary/public_namespace_computer.dart'; | 13 import 'package:analyzer/src/summary/public_namespace_computer.dart'; |
| 14 import 'package:analyzer/src/summary/summarize_const_expr.dart'; | 14 import 'package:analyzer/src/summary/summarize_const_expr.dart'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Serialize all the declarations in [compilationUnit] to an unlinked summary. | 17 * Serialize all the declarations in [compilationUnit] to an unlinked summary. |
| 18 */ | 18 */ |
| 19 UnlinkedUnitBuilder serializeAstUnlinked(CompilationUnit compilationUnit) { | 19 UnlinkedUnitBuilder serializeAstUnlinked(CompilationUnit compilationUnit) { |
| 20 return new _SummarizeAstVisitor().serializeCompilationUnit(compilationUnit); | 20 return new _SummarizeAstVisitor().serializeCompilationUnit(compilationUnit); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Instances of this class keep track of intermediate state during | 24 * Instances of this class keep track of intermediate state during |
| 25 * serialization of a single constant [Expression]. | 25 * serialization of a single constant [Expression]. |
| 26 */ | 26 */ |
| 27 class _ConstExprSerializer extends AbstractConstExprSerializer { | 27 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 28 final _SummarizeAstVisitor visitor; | 28 final _SummarizeAstVisitor visitor; |
| 29 | 29 |
| 30 _ConstExprSerializer(this.visitor); | 30 /** |
| 31 * If a constructor initializer expression is being serialized, the names of |
| 32 * the constructor parameters. Otherwise `null`. |
| 33 */ |
| 34 final Set<String> constructorParameterNames; |
| 35 |
| 36 _ConstExprSerializer(this.visitor, this.constructorParameterNames); |
| 37 |
| 38 @override |
| 39 bool isConstructorParameterName(String name) { |
| 40 return constructorParameterNames?.contains(name) ?? false; |
| 41 } |
| 31 | 42 |
| 32 @override | 43 @override |
| 33 void serializeAnnotation(Annotation annotation) { | 44 void serializeAnnotation(Annotation annotation) { |
| 34 if (annotation.arguments == null) { | 45 if (annotation.arguments == null) { |
| 35 assert(annotation.constructorName == null); | 46 assert(annotation.constructorName == null); |
| 36 serialize(annotation.name); | 47 serialize(annotation.name); |
| 37 } else { | 48 } else { |
| 38 Identifier name = annotation.name; | 49 Identifier name = annotation.name; |
| 39 EntityRefBuilder constructor; | 50 EntityRefBuilder constructor; |
| 40 if (name is PrefixedIdentifier && annotation.constructorName == null) { | 51 if (name is PrefixedIdentifier && annotation.constructorName == null) { |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 /** | 326 /** |
| 316 * Serialize the given list of [annotations]. If there are no annotations, | 327 * Serialize the given list of [annotations]. If there are no annotations, |
| 317 * the empty list is returned. | 328 * the empty list is returned. |
| 318 */ | 329 */ |
| 319 List<UnlinkedConstBuilder> serializeAnnotations( | 330 List<UnlinkedConstBuilder> serializeAnnotations( |
| 320 NodeList<Annotation> annotations) { | 331 NodeList<Annotation> annotations) { |
| 321 if (annotations.isEmpty) { | 332 if (annotations.isEmpty) { |
| 322 return const <UnlinkedConstBuilder>[]; | 333 return const <UnlinkedConstBuilder>[]; |
| 323 } | 334 } |
| 324 return annotations.map((Annotation a) { | 335 return annotations.map((Annotation a) { |
| 325 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 336 _ConstExprSerializer serializer = new _ConstExprSerializer(this, null); |
| 326 serializer.serializeAnnotation(a); | 337 serializer.serializeAnnotation(a); |
| 327 return serializer.toBuilder(); | 338 return serializer.toBuilder(); |
| 328 }).toList(); | 339 }).toList(); |
| 329 } | 340 } |
| 330 | 341 |
| 331 /** | 342 /** |
| 332 * Serialize a [ClassDeclaration] or [ClassTypeAlias] into an [UnlinkedClass] | 343 * Serialize a [ClassDeclaration] or [ClassTypeAlias] into an [UnlinkedClass] |
| 333 * and store the result in [classes]. | 344 * and store the result in [classes]. |
| 334 */ | 345 */ |
| 335 void serializeClass( | 346 void serializeClass( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 b.references = unlinkedReferences; | 440 b.references = unlinkedReferences; |
| 430 b.typedefs = typedefs; | 441 b.typedefs = typedefs; |
| 431 b.variables = variables; | 442 b.variables = variables; |
| 432 b.publicNamespace = computePublicNamespace(compilationUnit); | 443 b.publicNamespace = computePublicNamespace(compilationUnit); |
| 433 return b; | 444 return b; |
| 434 } | 445 } |
| 435 | 446 |
| 436 /** | 447 /** |
| 437 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 448 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 438 */ | 449 */ |
| 439 UnlinkedConstBuilder serializeConstExpr(Expression expression) { | 450 UnlinkedConstBuilder serializeConstExpr(Expression expression, |
| 440 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 451 [Set<String> constructorParameterNames]) { |
| 452 _ConstExprSerializer serializer = |
| 453 new _ConstExprSerializer(this, constructorParameterNames); |
| 441 serializer.serialize(expression); | 454 serializer.serialize(expression); |
| 442 return serializer.toBuilder(); | 455 return serializer.toBuilder(); |
| 443 } | 456 } |
| 444 | 457 |
| 445 /** | 458 /** |
| 446 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. | 459 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. |
| 447 */ | 460 */ |
| 448 UnlinkedDocumentationCommentBuilder serializeDocumentation( | 461 UnlinkedDocumentationCommentBuilder serializeDocumentation( |
| 449 Comment documentationComment) { | 462 Comment documentationComment) { |
| 450 if (documentationComment == null) { | 463 if (documentationComment == null) { |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 } | 758 } |
| 746 b.parameters = node.parameters.parameters | 759 b.parameters = node.parameters.parameters |
| 747 .map((FormalParameter p) => p.accept(this)) | 760 .map((FormalParameter p) => p.accept(this)) |
| 748 .toList(); | 761 .toList(); |
| 749 b.kind = UnlinkedExecutableKind.constructor; | 762 b.kind = UnlinkedExecutableKind.constructor; |
| 750 b.isFactory = node.factoryKeyword != null; | 763 b.isFactory = node.factoryKeyword != null; |
| 751 b.isConst = node.constKeyword != null; | 764 b.isConst = node.constKeyword != null; |
| 752 b.isExternal = node.externalKeyword != null; | 765 b.isExternal = node.externalKeyword != null; |
| 753 b.documentationComment = serializeDocumentation(node.documentationComment); | 766 b.documentationComment = serializeDocumentation(node.documentationComment); |
| 754 b.annotations = serializeAnnotations(node.metadata); | 767 b.annotations = serializeAnnotations(node.metadata); |
| 768 if (node.constKeyword != null) { |
| 769 Set<String> constructorParameterNames = |
| 770 node.parameters.parameters.map((p) => p.identifier.name).toSet(); |
| 771 b.constantInitializers = node.initializers |
| 772 .map((ConstructorInitializer initializer) => |
| 773 serializeConstructorInitializer(initializer, (Expression expr) { |
| 774 return serializeConstExpr(expr, constructorParameterNames); |
| 775 })) |
| 776 .toList(); |
| 777 } |
| 755 executables.add(b); | 778 executables.add(b); |
| 756 } | 779 } |
| 757 | 780 |
| 758 @override | 781 @override |
| 759 UnlinkedParamBuilder visitDefaultFormalParameter( | 782 UnlinkedParamBuilder visitDefaultFormalParameter( |
| 760 DefaultFormalParameter node) { | 783 DefaultFormalParameter node) { |
| 761 UnlinkedParamBuilder b = node.parameter.accept(this); | 784 UnlinkedParamBuilder b = node.parameter.accept(this); |
| 762 if (node.defaultValue != null) { | 785 if (node.defaultValue != null) { |
| 763 b.defaultValue = serializeConstExpr(node.defaultValue); | 786 b.defaultValue = serializeConstExpr(node.defaultValue); |
| 764 } | 787 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 /** | 982 /** |
| 960 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 983 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 961 */ | 984 */ |
| 962 class _TypeParameterScope extends _Scope { | 985 class _TypeParameterScope extends _Scope { |
| 963 /** | 986 /** |
| 964 * Get the number of [_ScopedTypeParameter]s defined in this | 987 * Get the number of [_ScopedTypeParameter]s defined in this |
| 965 * [_TypeParameterScope]. | 988 * [_TypeParameterScope]. |
| 966 */ | 989 */ |
| 967 int get length => _definedNames.length; | 990 int get length => _definedNames.length; |
| 968 } | 991 } |
| OLD | NEW |