| 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 27 matching lines...) Expand all Loading... |
| 38 } else if (element is FunctionElement) { | 38 } else if (element is FunctionElement) { |
| 39 kind = ReferenceKind.topLevelFunction; | 39 kind = ReferenceKind.topLevelFunction; |
| 40 } else { | 40 } else { |
| 41 throw new Exception('Unexpected element kind: ${element.runtimeType}'); | 41 throw new Exception('Unexpected element kind: ${element.runtimeType}'); |
| 42 } | 42 } |
| 43 return kind; | 43 return kind; |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Type of closures used by [_LibrarySerializer] to defer generation of | 47 * Type of closures used by [_LibrarySerializer] to defer generation of |
| 48 * [TypeRefBuilder] objects until the end of serialization of a | 48 * [EntityRefBuilder] objects until the end of serialization of a |
| 49 * compilation unit. | 49 * compilation unit. |
| 50 */ | 50 */ |
| 51 typedef TypeRefBuilder _SerializeTypeRef(); | 51 typedef EntityRefBuilder _SerializeTypeRef(); |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Data structure holding the result of serializing a [LibraryElement]. | 54 * Data structure holding the result of serializing a [LibraryElement]. |
| 55 */ | 55 */ |
| 56 class LibrarySerializationResult { | 56 class LibrarySerializationResult { |
| 57 /** | 57 /** |
| 58 * Linked information the given library. | 58 * Linked information the given library. |
| 59 */ | 59 */ |
| 60 final LinkedLibraryBuilder linked; | 60 final LinkedLibraryBuilder linked; |
| 61 | 61 |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 assert(unlinkedReferences.length == linkedReferences.length); | 573 assert(unlinkedReferences.length == linkedReferences.length); |
| 574 int index = unlinkedReferences.length; | 574 int index = unlinkedReferences.length; |
| 575 unlinkedReferences.add(new UnlinkedReferenceBuilder(name: element.name)); | 575 unlinkedReferences.add(new UnlinkedReferenceBuilder(name: element.name)); |
| 576 linkedReferences | 576 linkedReferences |
| 577 .add(new LinkedReferenceBuilder(kind: ReferenceKind.prefix)); | 577 .add(new LinkedReferenceBuilder(kind: ReferenceKind.prefix)); |
| 578 return index; | 578 return index; |
| 579 }); | 579 }); |
| 580 } | 580 } |
| 581 | 581 |
| 582 /** | 582 /** |
| 583 * Compute the reference index which should be stored in a [TypeRef]. | 583 * Compute the reference index which should be stored in a [EntityRef]. |
| 584 * | 584 * |
| 585 * If [linked] is true, and a new reference has to be created, the reference | 585 * If [linked] is true, and a new reference has to be created, the reference |
| 586 * will only be stored in [linkedReferences]. | 586 * will only be stored in [linkedReferences]. |
| 587 */ | 587 */ |
| 588 int serializeReferenceForType(DartType type, bool linked) { | 588 int serializeReferenceForType(DartType type, bool linked) { |
| 589 Element element = type.element; | 589 Element element = type.element; |
| 590 LibraryElement dependentLibrary = element.library; | 590 LibraryElement dependentLibrary = element.library; |
| 591 if (dependentLibrary == null) { | 591 if (dependentLibrary == null) { |
| 592 assert(type.isDynamic); | 592 assert(type.isDynamic); |
| 593 if (type is UndefinedTypeImpl) { | 593 if (type is UndefinedTypeImpl) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); | 627 UnlinkedTypeParamBuilder b = new UnlinkedTypeParamBuilder(); |
| 628 b.name = typeParameter.name; | 628 b.name = typeParameter.name; |
| 629 b.nameOffset = typeParameter.nameOffset; | 629 b.nameOffset = typeParameter.nameOffset; |
| 630 if (typeParameter.bound != null) { | 630 if (typeParameter.bound != null) { |
| 631 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); | 631 b.bound = serializeTypeRef(typeParameter.bound, typeParameter); |
| 632 } | 632 } |
| 633 return b; | 633 return b; |
| 634 } | 634 } |
| 635 | 635 |
| 636 /** | 636 /** |
| 637 * Serialize the given [type] into a [TypeRef]. If [slot] is provided, | 637 * Serialize the given [type] into a [EntityRef]. If [slot] is provided, |
| 638 * it should be included in the [TypeRef]. If [linked] is true, any | 638 * it should be included in the [EntityRef]. If [linked] is true, any |
| 639 * references that are created will be populated into [linkedReferences] but | 639 * references that are created will be populated into [linkedReferences] but |
| 640 * [not [unlinkedReferences]. | 640 * [not [unlinkedReferences]. |
| 641 * | 641 * |
| 642 * [context] is the element within which the [TypeRef] will be | 642 * [context] is the element within which the [EntityRef] will be |
| 643 * interpreted; this is used to serialize type parameters. | 643 * interpreted; this is used to serialize type parameters. |
| 644 */ | 644 */ |
| 645 TypeRefBuilder serializeTypeRef(DartType type, Element context, | 645 EntityRefBuilder serializeTypeRef(DartType type, Element context, |
| 646 {bool linked: false, int slot}) { | 646 {bool linked: false, int slot}) { |
| 647 TypeRefBuilder b = new TypeRefBuilder(slot: slot); | 647 EntityRefBuilder b = new EntityRefBuilder(slot: slot); |
| 648 if (type is TypeParameterType) { | 648 if (type is TypeParameterType) { |
| 649 b.paramReference = findTypeParameterIndex(type, context); | 649 b.paramReference = findTypeParameterIndex(type, context); |
| 650 } else { | 650 } else { |
| 651 b.reference = serializeReferenceForType(type, linked); | 651 b.reference = serializeReferenceForType(type, linked); |
| 652 List<DartType> typeArguments = getTypeArguments(type); | 652 List<DartType> typeArguments = getTypeArguments(type); |
| 653 if (typeArguments != null) { | 653 if (typeArguments != null) { |
| 654 // Trailing type arguments of type 'dynamic' should be omitted. | 654 // Trailing type arguments of type 'dynamic' should be omitted. |
| 655 int numArgsToSerialize = typeArguments.length; | 655 int numArgsToSerialize = typeArguments.length; |
| 656 while (numArgsToSerialize > 0 && | 656 while (numArgsToSerialize > 0 && |
| 657 typeArguments[numArgsToSerialize - 1].isDynamic) { | 657 typeArguments[numArgsToSerialize - 1].isDynamic) { |
| 658 --numArgsToSerialize; | 658 --numArgsToSerialize; |
| 659 } | 659 } |
| 660 if (numArgsToSerialize > 0) { | 660 if (numArgsToSerialize > 0) { |
| 661 List<TypeRefBuilder> serializedArguments = <TypeRefBuilder>[]; | 661 List<EntityRefBuilder> serializedArguments = <EntityRefBuilder>[]; |
| 662 for (int i = 0; i < numArgsToSerialize; i++) { | 662 for (int i = 0; i < numArgsToSerialize; i++) { |
| 663 serializedArguments.add( | 663 serializedArguments.add( |
| 664 serializeTypeRef(typeArguments[i], context, linked: linked)); | 664 serializeTypeRef(typeArguments[i], context, linked: linked)); |
| 665 } | 665 } |
| 666 b.typeArguments = serializedArguments; | 666 b.typeArguments = serializedArguments; |
| 667 } | 667 } |
| 668 } | 668 } |
| 669 } | 669 } |
| 670 return b; | 670 return b; |
| 671 } | 671 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 | 781 |
| 782 /** | 782 /** |
| 783 * Instances of this class keep track of intermediate state during | 783 * Instances of this class keep track of intermediate state during |
| 784 * serialization of a single constant [Expression]. | 784 * serialization of a single constant [Expression]. |
| 785 */ | 785 */ |
| 786 class _ConstExprSerializer extends AbstractConstExprSerializer { | 786 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 787 final _CompilationUnitSerializer serializer; | 787 final _CompilationUnitSerializer serializer; |
| 788 | 788 |
| 789 _ConstExprSerializer(this.serializer); | 789 _ConstExprSerializer(this.serializer); |
| 790 | 790 |
| 791 TypeRefBuilder serializeIdentifier(Identifier identifier) { | 791 EntityRefBuilder serializeIdentifier(Identifier identifier) { |
| 792 Element element = identifier.staticElement; | 792 Element element = identifier.staticElement; |
| 793 assert(element != null); | 793 assert(element != null); |
| 794 // TODO(scheglov) how to serialize element references? | 794 // TODO(scheglov) how to serialize element references? |
| 795 return new TypeRefBuilder( | 795 return new EntityRefBuilder( |
| 796 reference: serializer._getElementReferenceId(element)); | 796 reference: serializer._getElementReferenceId(element)); |
| 797 } | 797 } |
| 798 | 798 |
| 799 @override | 799 @override |
| 800 TypeRefBuilder serializeType(TypeName typeName) { | 800 EntityRefBuilder serializeType(TypeName typeName) { |
| 801 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; | 801 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; |
| 802 return serializer.serializeTypeRef(type, null); | 802 return serializer.serializeTypeRef(type, null); |
| 803 } | 803 } |
| 804 } | 804 } |
| 805 | 805 |
| 806 /** | 806 /** |
| 807 * Instances of this class keep track of intermediate state during | 807 * Instances of this class keep track of intermediate state during |
| 808 * serialization of a single library. | 808 * serialization of a single library. |
| 809 */ | 809 */ |
| 810 class _LibrarySerializer { | 810 class _LibrarySerializer { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 980 exportNames.add(new LinkedExportNameBuilder( | 980 exportNames.add(new LinkedExportNameBuilder( |
| 981 name: name, | 981 name: name, |
| 982 dependency: serializeDependency(dependentLibrary), | 982 dependency: serializeDependency(dependentLibrary), |
| 983 unit: unit, | 983 unit: unit, |
| 984 kind: kind)); | 984 kind: kind)); |
| 985 } | 985 } |
| 986 pb.exportNames = exportNames; | 986 pb.exportNames = exportNames; |
| 987 return pb; | 987 return pb; |
| 988 } | 988 } |
| 989 } | 989 } |
| OLD | NEW |