| 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 EntityRefBuilder serializeConstructorName(ConstructorName constructor) { | 44 EntityRefBuilder serializeConstructorName(ConstructorName constructor) { |
| 34 EntityRefBuilder typeBuilder = serializeType(constructor.type); | 45 EntityRefBuilder typeBuilder = serializeType(constructor.type); |
| 35 if (constructor.name == null) { | 46 if (constructor.name == null) { |
| 36 return typeBuilder; | 47 return typeBuilder; |
| 37 } else { | 48 } else { |
| 38 String name = constructor.name.name; | 49 String name = constructor.name.name; |
| 39 int nameRef = visitor.serializeReference(typeBuilder.reference, name); | 50 int nameRef = visitor.serializeReference(typeBuilder.reference, name); |
| 40 return new EntityRefBuilder( | 51 return new EntityRefBuilder( |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 b.references = unlinkedReferences; | 394 b.references = unlinkedReferences; |
| 384 b.typedefs = typedefs; | 395 b.typedefs = typedefs; |
| 385 b.variables = variables; | 396 b.variables = variables; |
| 386 b.publicNamespace = computePublicNamespace(compilationUnit); | 397 b.publicNamespace = computePublicNamespace(compilationUnit); |
| 387 return b; | 398 return b; |
| 388 } | 399 } |
| 389 | 400 |
| 390 /** | 401 /** |
| 391 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 402 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 392 */ | 403 */ |
| 393 UnlinkedConstBuilder serializeConstExpr(Expression expression) { | 404 UnlinkedConstBuilder serializeConstExpr(Expression expression, |
| 394 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 405 [Set<String> constructorParameterNames]) { |
| 406 _ConstExprSerializer serializer = |
| 407 new _ConstExprSerializer(this, constructorParameterNames); |
| 395 serializer.serialize(expression); | 408 serializer.serialize(expression); |
| 396 return serializer.toBuilder(); | 409 return serializer.toBuilder(); |
| 397 } | 410 } |
| 398 | 411 |
| 399 /** | 412 /** |
| 400 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. | 413 * Serialize a [Comment] node into an [UnlinkedDocumentationComment] object. |
| 401 */ | 414 */ |
| 402 UnlinkedDocumentationCommentBuilder serializeDocumentation( | 415 UnlinkedDocumentationCommentBuilder serializeDocumentation( |
| 403 Comment documentationComment) { | 416 Comment documentationComment) { |
| 404 if (documentationComment == null) { | 417 if (documentationComment == null) { |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 b.nameOffset = node.returnType.offset; | 701 b.nameOffset = node.returnType.offset; |
| 689 } | 702 } |
| 690 b.parameters = node.parameters.parameters | 703 b.parameters = node.parameters.parameters |
| 691 .map((FormalParameter p) => p.accept(this)) | 704 .map((FormalParameter p) => p.accept(this)) |
| 692 .toList(); | 705 .toList(); |
| 693 b.kind = UnlinkedExecutableKind.constructor; | 706 b.kind = UnlinkedExecutableKind.constructor; |
| 694 b.isFactory = node.factoryKeyword != null; | 707 b.isFactory = node.factoryKeyword != null; |
| 695 b.isConst = node.constKeyword != null; | 708 b.isConst = node.constKeyword != null; |
| 696 b.isExternal = node.externalKeyword != null; | 709 b.isExternal = node.externalKeyword != null; |
| 697 b.documentationComment = serializeDocumentation(node.documentationComment); | 710 b.documentationComment = serializeDocumentation(node.documentationComment); |
| 711 if (node.constKeyword != null) { |
| 712 Set<String> constructorParameterNames = |
| 713 node.parameters.parameters.map((p) => p.identifier.name).toSet(); |
| 714 b.constantInitializers = node.initializers |
| 715 .map((ConstructorInitializer initializer) => |
| 716 serializeConstructorInitializer(initializer, (Expression expr) { |
| 717 return serializeConstExpr(expr, constructorParameterNames); |
| 718 })) |
| 719 .toList(); |
| 720 } |
| 698 executables.add(b); | 721 executables.add(b); |
| 699 } | 722 } |
| 700 | 723 |
| 701 @override | 724 @override |
| 702 UnlinkedParamBuilder visitDefaultFormalParameter( | 725 UnlinkedParamBuilder visitDefaultFormalParameter( |
| 703 DefaultFormalParameter node) { | 726 DefaultFormalParameter node) { |
| 704 UnlinkedParamBuilder b = node.parameter.accept(this); | 727 UnlinkedParamBuilder b = node.parameter.accept(this); |
| 705 if (node.defaultValue != null) { | 728 if (node.defaultValue != null) { |
| 706 b.defaultValue = serializeConstExpr(node.defaultValue); | 729 b.defaultValue = serializeConstExpr(node.defaultValue); |
| 707 } | 730 } |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 /** | 914 /** |
| 892 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 915 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 893 */ | 916 */ |
| 894 class _TypeParameterScope extends _Scope { | 917 class _TypeParameterScope extends _Scope { |
| 895 /** | 918 /** |
| 896 * Get the number of [_ScopedTypeParameter]s defined in this | 919 * Get the number of [_ScopedTypeParameter]s defined in this |
| 897 * [_TypeParameterScope]. | 920 * [_TypeParameterScope]. |
| 898 */ | 921 */ |
| 899 int get length => _definedNames.length; | 922 int get length => _definedNames.length; |
| 900 } | 923 } |
| OLD | NEW |