| 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/src/generated/utilities_dart.dart'; | 11 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 11 import 'package:analyzer/src/summary/format.dart'; | 12 import 'package:analyzer/src/summary/format.dart'; |
| 12 import 'package:analyzer/src/summary/idl.dart'; | 13 import 'package:analyzer/src/summary/idl.dart'; |
| 13 import 'package:analyzer/src/summary/public_namespace_computer.dart'; | 14 import 'package:analyzer/src/summary/public_namespace_computer.dart'; |
| 14 import 'package:analyzer/src/summary/summarize_const_expr.dart'; | 15 import 'package:analyzer/src/summary/summarize_const_expr.dart'; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Serialize all the declarations in [compilationUnit] to an unlinked summary. | 18 * Serialize all the declarations in [compilationUnit] to an unlinked summary. |
| 18 */ | 19 */ |
| 19 UnlinkedUnitBuilder serializeAstUnlinked(CompilationUnit compilationUnit) { | 20 UnlinkedUnitBuilder serializeAstUnlinked(CompilationUnit compilationUnit) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 42 | 43 |
| 43 @override | 44 @override |
| 44 void serializeAnnotation(Annotation annotation) { | 45 void serializeAnnotation(Annotation annotation) { |
| 45 if (annotation.arguments == null) { | 46 if (annotation.arguments == null) { |
| 46 assert(annotation.constructorName == null); | 47 assert(annotation.constructorName == null); |
| 47 serialize(annotation.name); | 48 serialize(annotation.name); |
| 48 } else { | 49 } else { |
| 49 Identifier name = annotation.name; | 50 Identifier name = annotation.name; |
| 50 EntityRefBuilder constructor; | 51 EntityRefBuilder constructor; |
| 51 if (name is PrefixedIdentifier && annotation.constructorName == null) { | 52 if (name is PrefixedIdentifier && annotation.constructorName == null) { |
| 52 constructor = serializeConstructorName( | 53 constructor = |
| 53 new TypeName(name.prefix, null), name.identifier); | 54 serializeConstructorRef(null, name.prefix, null, name.identifier); |
| 54 } else { | 55 } else { |
| 55 constructor = serializeConstructorName( | 56 constructor = serializeConstructorRef( |
| 56 new TypeName(annotation.name, null), annotation.constructorName); | 57 null, annotation.name, null, annotation.constructorName); |
| 57 } | 58 } |
| 58 serializeInstanceCreation(constructor, annotation.arguments); | 59 serializeInstanceCreation(constructor, annotation.arguments); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 @override | 63 @override |
| 63 EntityRefBuilder serializeConstructorName( | 64 EntityRefBuilder serializeConstructorRef(DartType type, Identifier typeName, |
| 64 TypeName type, SimpleIdentifier name) { | 65 TypeArgumentList typeArguments, SimpleIdentifier name) { |
| 65 EntityRefBuilder typeBuilder = serializeType(type); | 66 EntityRefBuilder typeBuilder = serializeType(type, typeName, typeArguments); |
| 66 if (name == null) { | 67 if (name == null) { |
| 67 return typeBuilder; | 68 return typeBuilder; |
| 68 } else { | 69 } else { |
| 69 int nameRef = | 70 int nameRef = |
| 70 visitor.serializeReference(typeBuilder.reference, name.name); | 71 visitor.serializeReference(typeBuilder.reference, name.name); |
| 71 return new EntityRefBuilder( | 72 return new EntityRefBuilder( |
| 72 reference: nameRef, typeArguments: typeBuilder.typeArguments); | 73 reference: nameRef, typeArguments: typeBuilder.typeArguments); |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (expr is PropertyAccess) { | 111 if (expr is PropertyAccess) { |
| 111 int targetId = serializeIdentifierSequence(expr.target).reference; | 112 int targetId = serializeIdentifierSequence(expr.target).reference; |
| 112 int nameId = visitor.serializeReference(targetId, expr.propertyName.name); | 113 int nameId = visitor.serializeReference(targetId, expr.propertyName.name); |
| 113 return new EntityRefBuilder(reference: nameId); | 114 return new EntityRefBuilder(reference: nameId); |
| 114 } else { | 115 } else { |
| 115 throw new StateError('Unexpected node type: ${expr.runtimeType}'); | 116 throw new StateError('Unexpected node type: ${expr.runtimeType}'); |
| 116 } | 117 } |
| 117 } | 118 } |
| 118 | 119 |
| 119 @override | 120 @override |
| 120 EntityRefBuilder serializeType(TypeName node) { | 121 EntityRefBuilder serializeType( |
| 121 return visitor.serializeTypeName(node); | 122 DartType type, Identifier name, TypeArgumentList arguments) { |
| 123 return visitor.serializeType(name, arguments); |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 /** | 127 /** |
| 126 * A [_Scope] represents a set of name/value pairs defined locally within a | 128 * A [_Scope] represents a set of name/value pairs defined locally within a |
| 127 * limited span of a compilation unit. (Note that the spec also uses the term | 129 * limited span of a compilation unit. (Note that the spec also uses the term |
| 128 * "scope" to refer to the set of names defined at top level within a | 130 * "scope" to refer to the set of names defined at top level within a |
| 129 * compilation unit, but we do not use [_Scope] for that purpose). | 131 * compilation unit, but we do not use [_Scope] for that purpose). |
| 130 */ | 132 */ |
| 131 class _Scope { | 133 class _Scope { |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 } | 745 } |
| 744 return serializeReference(null, name); | 746 return serializeReference(null, name); |
| 745 } | 747 } |
| 746 | 748 |
| 747 /** | 749 /** |
| 748 * Serialize a type name (which might be defined in a nested scope, at top | 750 * Serialize a type name (which might be defined in a nested scope, at top |
| 749 * level within this library, or at top level within an imported library) to | 751 * level within this library, or at top level within an imported library) to |
| 750 * a [EntityRef]. Note that this method does the right thing if the | 752 * a [EntityRef]. Note that this method does the right thing if the |
| 751 * name doesn't refer to an entity other than a type (e.g. a class member). | 753 * name doesn't refer to an entity other than a type (e.g. a class member). |
| 752 */ | 754 */ |
| 753 EntityRefBuilder serializeTypeName(TypeName node) { | 755 EntityRefBuilder serializeType( |
| 754 if (node == null) { | 756 Identifier identifier, TypeArgumentList typeArguments) { |
| 757 if (identifier == null) { |
| 755 return null; | 758 return null; |
| 756 } else { | 759 } else { |
| 757 EntityRefBuilder b = new EntityRefBuilder(); | 760 EntityRefBuilder b = new EntityRefBuilder(); |
| 758 Identifier identifier = node.name; | |
| 759 if (identifier is SimpleIdentifier) { | 761 if (identifier is SimpleIdentifier) { |
| 760 String name = identifier.name; | 762 String name = identifier.name; |
| 761 int indexOffset = 0; | 763 int indexOffset = 0; |
| 762 for (int i = scopes.length - 1; i >= 0; i--) { | 764 for (int i = scopes.length - 1; i >= 0; i--) { |
| 763 _Scope scope = scopes[i]; | 765 _Scope scope = scopes[i]; |
| 764 _ScopedEntity entity = scope[name]; | 766 _ScopedEntity entity = scope[name]; |
| 765 if (entity != null) { | 767 if (entity != null) { |
| 766 if (entity is _ScopedTypeParameter) { | 768 if (entity is _ScopedTypeParameter) { |
| 767 b.paramReference = indexOffset + entity.index; | 769 b.paramReference = indexOffset + entity.index; |
| 768 return b; | 770 return b; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 781 b.reference = serializeReference(null, name); | 783 b.reference = serializeReference(null, name); |
| 782 } else if (identifier is PrefixedIdentifier) { | 784 } else if (identifier is PrefixedIdentifier) { |
| 783 int prefixIndex = prefixIndices.putIfAbsent(identifier.prefix.name, | 785 int prefixIndex = prefixIndices.putIfAbsent(identifier.prefix.name, |
| 784 () => serializeSimpleReference(identifier.prefix.name)); | 786 () => serializeSimpleReference(identifier.prefix.name)); |
| 785 b.reference = | 787 b.reference = |
| 786 serializeReference(prefixIndex, identifier.identifier.name); | 788 serializeReference(prefixIndex, identifier.identifier.name); |
| 787 } else { | 789 } else { |
| 788 throw new StateError( | 790 throw new StateError( |
| 789 'Unexpected identifier type: ${identifier.runtimeType}'); | 791 'Unexpected identifier type: ${identifier.runtimeType}'); |
| 790 } | 792 } |
| 791 if (node.typeArguments != null) { | 793 if (typeArguments != null) { |
| 792 // Trailing type arguments of type 'dynamic' should be omitted. | 794 // Trailing type arguments of type 'dynamic' should be omitted. |
| 793 NodeList<TypeName> args = node.typeArguments.arguments; | 795 NodeList<TypeName> args = typeArguments.arguments; |
| 794 int numArgsToSerialize = args.length; | 796 int numArgsToSerialize = args.length; |
| 795 while ( | 797 while ( |
| 796 numArgsToSerialize > 0 && isDynamic(args[numArgsToSerialize - 1])) { | 798 numArgsToSerialize > 0 && isDynamic(args[numArgsToSerialize - 1])) { |
| 797 --numArgsToSerialize; | 799 --numArgsToSerialize; |
| 798 } | 800 } |
| 799 if (numArgsToSerialize > 0) { | 801 if (numArgsToSerialize > 0) { |
| 800 List<EntityRefBuilder> serializedArguments = <EntityRefBuilder>[]; | 802 List<EntityRefBuilder> serializedArguments = <EntityRefBuilder>[]; |
| 801 for (int i = 0; i < numArgsToSerialize; i++) { | 803 for (int i = 0; i < numArgsToSerialize; i++) { |
| 802 serializedArguments.add(serializeTypeName(args[i])); | 804 serializedArguments.add(serializeTypeName(args[i])); |
| 803 } | 805 } |
| 804 b.typeArguments = serializedArguments; | 806 b.typeArguments = serializedArguments; |
| 805 } | 807 } |
| 806 } | 808 } |
| 807 return b; | 809 return b; |
| 808 } | 810 } |
| 809 } | 811 } |
| 810 | 812 |
| 811 /** | 813 /** |
| 814 * Serialize a type name (which might be defined in a nested scope, at top |
| 815 * level within this library, or at top level within an imported library) to |
| 816 * a [EntityRef]. Note that this method does the right thing if the |
| 817 * name doesn't refer to an entity other than a type (e.g. a class member). |
| 818 */ |
| 819 EntityRefBuilder serializeTypeName(TypeName node) { |
| 820 return serializeType(node?.name, node?.typeArguments); |
| 821 } |
| 822 |
| 823 /** |
| 812 * Serialize the given [typeParameters] into a list of [UnlinkedTypeParam]s, | 824 * Serialize the given [typeParameters] into a list of [UnlinkedTypeParam]s, |
| 813 * and also store them in [typeParameterScope]. | 825 * and also store them in [typeParameterScope]. |
| 814 */ | 826 */ |
| 815 List<UnlinkedTypeParamBuilder> serializeTypeParameters( | 827 List<UnlinkedTypeParamBuilder> serializeTypeParameters( |
| 816 TypeParameterList typeParameters, | 828 TypeParameterList typeParameters, |
| 817 _TypeParameterScope typeParameterScope) { | 829 _TypeParameterScope typeParameterScope) { |
| 818 if (typeParameters != null) { | 830 if (typeParameters != null) { |
| 819 for (int i = 0; i < typeParameters.typeParameters.length; i++) { | 831 for (int i = 0; i < typeParameters.typeParameters.length; i++) { |
| 820 TypeParameter typeParameter = typeParameters.typeParameters[i]; | 832 TypeParameter typeParameter = typeParameters.typeParameters[i]; |
| 821 typeParameterScope[typeParameter.name.name] = | 833 typeParameterScope[typeParameter.name.name] = |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 b.nameOffset = node.returnType.offset; | 955 b.nameOffset = node.returnType.offset; |
| 944 } | 956 } |
| 945 b.parameters = node.parameters.parameters | 957 b.parameters = node.parameters.parameters |
| 946 .map((FormalParameter p) => p.accept(this)) | 958 .map((FormalParameter p) => p.accept(this)) |
| 947 .toList(); | 959 .toList(); |
| 948 b.kind = UnlinkedExecutableKind.constructor; | 960 b.kind = UnlinkedExecutableKind.constructor; |
| 949 if (node.factoryKeyword != null) { | 961 if (node.factoryKeyword != null) { |
| 950 b.isFactory = true; | 962 b.isFactory = true; |
| 951 if (node.redirectedConstructor != null) { | 963 if (node.redirectedConstructor != null) { |
| 952 b.isRedirectedConstructor = true; | 964 b.isRedirectedConstructor = true; |
| 965 TypeName typeName = node.redirectedConstructor.type; |
| 953 b.redirectedConstructor = new _ConstExprSerializer(this, null) | 966 b.redirectedConstructor = new _ConstExprSerializer(this, null) |
| 954 .serializeConstructorName(node.redirectedConstructor.type, | 967 .serializeConstructorRef(null, typeName.name, |
| 955 node.redirectedConstructor.name); | 968 typeName.typeArguments, node.redirectedConstructor.name); |
| 956 } | 969 } |
| 957 } else { | 970 } else { |
| 958 for (ConstructorInitializer initializer in node.initializers) { | 971 for (ConstructorInitializer initializer in node.initializers) { |
| 959 if (initializer is RedirectingConstructorInvocation) { | 972 if (initializer is RedirectingConstructorInvocation) { |
| 960 b.isRedirectedConstructor = true; | 973 b.isRedirectedConstructor = true; |
| 961 b.redirectedConstructorName = initializer.constructorName?.name; | 974 b.redirectedConstructorName = initializer.constructorName?.name; |
| 962 } | 975 } |
| 963 } | 976 } |
| 964 } | 977 } |
| 965 if (node.constKeyword != null) { | 978 if (node.constKeyword != null) { |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1265 /** | 1278 /** |
| 1266 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 1279 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 1267 */ | 1280 */ |
| 1268 class _TypeParameterScope extends _Scope { | 1281 class _TypeParameterScope extends _Scope { |
| 1269 /** | 1282 /** |
| 1270 * Get the number of [_ScopedTypeParameter]s defined in this | 1283 * Get the number of [_ScopedTypeParameter]s defined in this |
| 1271 * [_TypeParameterScope]. | 1284 * [_TypeParameterScope]. |
| 1272 */ | 1285 */ |
| 1273 int get length => _definedNames.length; | 1286 int get length => _definedNames.length; |
| 1274 } | 1287 } |
| OLD | NEW |