| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.elements; | 5 library serialization.elements; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; | 8 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/src/dart/element/element.dart'; | 9 import 'package:analyzer/src/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/dart/element/type.dart'; | 10 import 'package:analyzer/src/dart/element/type.dart'; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 LibrarySerializationResult serializeLibrary( | 22 LibrarySerializationResult serializeLibrary( |
| 23 LibraryElement lib, TypeProvider typeProvider, bool strongMode) { | 23 LibraryElement lib, TypeProvider typeProvider, bool strongMode) { |
| 24 var serializer = new _LibrarySerializer(lib, typeProvider, strongMode); | 24 var serializer = new _LibrarySerializer(lib, typeProvider, strongMode); |
| 25 LinkedLibraryBuilder linked = serializer.serializeLibrary(); | 25 LinkedLibraryBuilder linked = serializer.serializeLibrary(); |
| 26 return new LibrarySerializationResult( | 26 return new LibrarySerializationResult( |
| 27 linked, serializer.unlinkedUnits, serializer.unitUris); | 27 linked, serializer.unlinkedUnits, serializer.unitUris); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ReferenceKind _getReferenceKind(Element element) { | 30 ReferenceKind _getReferenceKind(Element element) { |
| 31 ReferenceKind kind; | 31 ReferenceKind kind; |
| 32 if (element is PropertyAccessorElement) { | 32 if (element == null || |
| 33 kind = ReferenceKind.topLevelPropertyAccessor; | |
| 34 } else if (element is FunctionTypeAliasElement) { | |
| 35 kind = ReferenceKind.typedef; | |
| 36 } else if (element == null || | |
| 37 element is ClassElement || | 33 element is ClassElement || |
| 38 element is DynamicElementImpl) { | 34 element is DynamicElementImpl) { |
| 39 kind = ReferenceKind.classOrEnum; | 35 kind = ReferenceKind.classOrEnum; |
| 36 } else if (element is ConstructorElement) { |
| 37 kind = ReferenceKind.constructor; |
| 40 } else if (element is FunctionElement) { | 38 } else if (element is FunctionElement) { |
| 41 kind = ReferenceKind.topLevelFunction; | 39 kind = ReferenceKind.topLevelFunction; |
| 40 } else if (element is FunctionTypeAliasElement) { |
| 41 kind = ReferenceKind.typedef; |
| 42 } else if (element is PropertyAccessorElement) { |
| 43 kind = ReferenceKind.topLevelPropertyAccessor; |
| 42 } else { | 44 } else { |
| 43 throw new Exception('Unexpected element kind: ${element.runtimeType}'); | 45 throw new Exception('Unexpected element kind: ${element.runtimeType}'); |
| 44 } | 46 } |
| 45 return kind; | 47 return kind; |
| 46 } | 48 } |
| 47 | 49 |
| 48 /** | 50 /** |
| 49 * Type of closures used by [_LibrarySerializer] to defer generation of | 51 * Type of closures used by [_LibrarySerializer] to defer generation of |
| 50 * [EntityRefBuilder] objects until the end of serialization of a | 52 * [EntityRefBuilder] objects until the end of serialization of a |
| 51 * compilation unit. | 53 * compilation unit. |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 int slot = ++numSlots; | 780 int slot = ++numSlots; |
| 779 if (type != null) { | 781 if (type != null) { |
| 780 deferredLinkedTypes | 782 deferredLinkedTypes |
| 781 .add(() => serializeTypeRef(type, context, linked: true, slot: slot)); | 783 .add(() => serializeTypeRef(type, context, linked: true, slot: slot)); |
| 782 } | 784 } |
| 783 return slot; | 785 return slot; |
| 784 } | 786 } |
| 785 | 787 |
| 786 int _getElementReferenceId(Element element, {bool linked: false}) { | 788 int _getElementReferenceId(Element element, {bool linked: false}) { |
| 787 return referenceMap.putIfAbsent(element, () { | 789 return referenceMap.putIfAbsent(element, () { |
| 790 if (element is ConstructorElement && element.displayName.isEmpty) { |
| 791 return _getElementReferenceId(element.enclosingElement, linked: linked); |
| 792 } |
| 788 LibraryElement dependentLibrary = element?.library; | 793 LibraryElement dependentLibrary = element?.library; |
| 789 int unit; | 794 int unit; |
| 790 if (dependentLibrary == null) { | 795 if (dependentLibrary == null) { |
| 791 assert(element == librarySerializer.typeProvider.dynamicType.element || | 796 assert(element == librarySerializer.typeProvider.dynamicType.element || |
| 792 element == null); | 797 element == null); |
| 793 unit = 0; | 798 unit = 0; |
| 794 dependentLibrary = librarySerializer.libraryElement; | 799 dependentLibrary = librarySerializer.libraryElement; |
| 795 } else { | 800 } else { |
| 796 CompilationUnitElement unitElement = | 801 CompilationUnitElement unitElement = |
| 797 element.getAncestor((Element e) => e is CompilationUnitElement); | 802 element.getAncestor((Element e) => e is CompilationUnitElement); |
| 798 unit = dependentLibrary.units.indexOf(unitElement); | 803 unit = dependentLibrary.units.indexOf(unitElement); |
| 799 assert(unit != -1); | 804 assert(unit != -1); |
| 800 } | 805 } |
| 801 int numTypeParameters = 0; | 806 int numTypeParameters = 0; |
| 802 if (element is TypeParameterizedElement) { | 807 if (element is TypeParameterizedElement) { |
| 803 numTypeParameters = element.typeParameters.length; | 808 numTypeParameters = element.typeParameters.length; |
| 804 } | 809 } |
| 805 LinkedReferenceBuilder linkedReference = new LinkedReferenceBuilder( | 810 LinkedReferenceBuilder linkedReference = new LinkedReferenceBuilder( |
| 806 dependency: librarySerializer.serializeDependency(dependentLibrary), | 811 dependency: librarySerializer.serializeDependency(dependentLibrary), |
| 807 kind: _getReferenceKind(element), | 812 kind: _getReferenceKind(element), |
| 808 unit: unit, | 813 unit: unit, |
| 809 numTypeParameters: numTypeParameters); | 814 numTypeParameters: numTypeParameters); |
| 810 String name = element == null ? 'void' : element.name; | 815 String name = element == null ? 'void' : element.name; |
| 811 if (linked) { | 816 if (linked) { |
| 812 linkedReference.name = name; | 817 linkedReference.name = name; |
| 813 } else { | 818 } else { |
| 814 assert(unlinkedReferences.length == linkedReferences.length); | 819 assert(unlinkedReferences.length == linkedReferences.length); |
| 815 // Figure out a prefix that may be used to refer to the given type. | |
| 816 // TODO(paulberry): to avoid subtle relinking inconsistencies we | |
| 817 // should use the actual prefix from the AST (a given type may be | |
| 818 // reachable via multiple prefixes), but sadly, this information is | |
| 819 // not recorded in the element model. | |
| 820 int prefixReference = 0; | 820 int prefixReference = 0; |
| 821 PrefixElement prefix = librarySerializer.prefixMap[element]; | 821 Element enclosing = element?.enclosingElement; |
| 822 if (prefix != null) { | 822 if (enclosing == null || enclosing is CompilationUnitElement) { |
| 823 prefixReference = serializePrefix(prefix); | 823 // Figure out a prefix that may be used to refer to the given element. |
| 824 // TODO(paulberry): to avoid subtle relinking inconsistencies we |
| 825 // should use the actual prefix from the AST (a given type may be |
| 826 // reachable via multiple prefixes), but sadly, this information is |
| 827 // not recorded in the element model. |
| 828 PrefixElement prefix = librarySerializer.prefixMap[element]; |
| 829 if (prefix != null) { |
| 830 prefixReference = serializePrefix(prefix); |
| 831 } |
| 832 } else { |
| 833 prefixReference = _getElementReferenceId(enclosing, linked: linked); |
| 824 } | 834 } |
| 825 unlinkedReferences.add(new UnlinkedReferenceBuilder( | 835 unlinkedReferences.add(new UnlinkedReferenceBuilder( |
| 826 name: name, prefixReference: prefixReference)); | 836 name: name, prefixReference: prefixReference)); |
| 827 } | 837 } |
| 828 int index = linkedReferences.length; | 838 int index = linkedReferences.length; |
| 829 linkedReferences.add(linkedReference); | 839 linkedReferences.add(linkedReference); |
| 830 return index; | 840 return index; |
| 831 }); | 841 }); |
| 832 } | 842 } |
| 833 } | 843 } |
| 834 | 844 |
| 835 /** | 845 /** |
| 836 * Instances of this class keep track of intermediate state during | 846 * Instances of this class keep track of intermediate state during |
| 837 * serialization of a single constant [Expression]. | 847 * serialization of a single constant [Expression]. |
| 838 */ | 848 */ |
| 839 class _ConstExprSerializer extends AbstractConstExprSerializer { | 849 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 840 final _CompilationUnitSerializer serializer; | 850 final _CompilationUnitSerializer serializer; |
| 841 | 851 |
| 842 _ConstExprSerializer(this.serializer); | 852 _ConstExprSerializer(this.serializer); |
| 843 | 853 |
| 854 @override |
| 855 EntityRefBuilder serializeConstructorName(ConstructorName constructor) { |
| 856 ConstructorElement element = constructor.staticElement; |
| 857 assert(element != null); |
| 858 int referenceId = serializer._getElementReferenceId(element); |
| 859 return new EntityRefBuilder(reference: referenceId); |
| 860 } |
| 861 |
| 844 EntityRefBuilder serializeIdentifier(Identifier identifier) { | 862 EntityRefBuilder serializeIdentifier(Identifier identifier) { |
| 845 Element element = identifier.staticElement; | 863 Element element = identifier.staticElement; |
| 846 assert(element != null); | 864 assert(element != null); |
| 847 // TODO(scheglov) how to serialize element references? | 865 // TODO(scheglov) how to serialize element references? |
| 848 return new EntityRefBuilder( | 866 return new EntityRefBuilder( |
| 849 reference: serializer._getElementReferenceId(element)); | 867 reference: serializer._getElementReferenceId(element)); |
| 850 } | 868 } |
| 851 | 869 |
| 852 @override | 870 @override |
| 853 EntityRefBuilder serializeType(TypeName typeName) { | 871 EntityRefBuilder serializeType(TypeName typeName) { |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 exportNames.add(new LinkedExportNameBuilder( | 1057 exportNames.add(new LinkedExportNameBuilder( |
| 1040 name: name, | 1058 name: name, |
| 1041 dependency: serializeDependency(dependentLibrary), | 1059 dependency: serializeDependency(dependentLibrary), |
| 1042 unit: unit, | 1060 unit: unit, |
| 1043 kind: kind)); | 1061 kind: kind)); |
| 1044 } | 1062 } |
| 1045 pb.exportNames = exportNames; | 1063 pb.exportNames = exportNames; |
| 1046 return pb; | 1064 return pb; |
| 1047 } | 1065 } |
| 1048 } | 1066 } |
| OLD | NEW |