| 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 15 matching lines...) Expand all Loading... |
| 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 is PropertyAccessorElement) { |
| 33 kind = ReferenceKind.topLevelPropertyAccessor; | 33 kind = ReferenceKind.topLevelPropertyAccessor; |
| 34 } else if (element is FunctionTypeAliasElement) { | 34 } else if (element is FunctionTypeAliasElement) { |
| 35 kind = ReferenceKind.typedef; | 35 kind = ReferenceKind.typedef; |
| 36 } else if (element is ClassElement) { | 36 } else if (element is ClassElement || element is DynamicElementImpl) { |
| 37 kind = ReferenceKind.classOrEnum; | 37 kind = ReferenceKind.classOrEnum; |
| 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 /** |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 if (element.documentationComment == null) { | 403 if (element.documentationComment == null) { |
| 404 return null; | 404 return null; |
| 405 } | 405 } |
| 406 return new UnlinkedDocumentationCommentBuilder( | 406 return new UnlinkedDocumentationCommentBuilder( |
| 407 text: element.documentationComment, | 407 text: element.documentationComment, |
| 408 offset: element.docRange.offset, | 408 offset: element.docRange.offset, |
| 409 length: element.docRange.length); | 409 length: element.docRange.length); |
| 410 } | 410 } |
| 411 | 411 |
| 412 /** | 412 /** |
| 413 * Return the index of the entry in the references table | |
| 414 * ([UnlinkedLibrary.references] and [LinkedLibrary.references]) | |
| 415 * representing the pseudo-type `dynamic`. | |
| 416 */ | |
| 417 int serializeDynamicReference() => 0; | |
| 418 | |
| 419 /** | |
| 420 * Serialize the given [enumElement], creating an [UnlinkedEnum]. | 413 * Serialize the given [enumElement], creating an [UnlinkedEnum]. |
| 421 */ | 414 */ |
| 422 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { | 415 UnlinkedEnumBuilder serializeEnum(ClassElement enumElement) { |
| 423 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); | 416 UnlinkedEnumBuilder b = new UnlinkedEnumBuilder(); |
| 424 b.name = enumElement.name; | 417 b.name = enumElement.name; |
| 425 b.nameOffset = enumElement.nameOffset; | 418 b.nameOffset = enumElement.nameOffset; |
| 426 List<UnlinkedEnumValueBuilder> values = <UnlinkedEnumValueBuilder>[]; | 419 List<UnlinkedEnumValueBuilder> values = <UnlinkedEnumValueBuilder>[]; |
| 427 for (FieldElement field in enumElement.fields) { | 420 for (FieldElement field in enumElement.fields) { |
| 428 if (field.isConst && field.type.element == enumElement) { | 421 if (field.isConst && field.type.element == enumElement) { |
| 429 values.add(new UnlinkedEnumValueBuilder( | 422 values.add(new UnlinkedEnumValueBuilder( |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 * If [linked] is true, and a new reference has to be created, the reference | 586 * If [linked] is true, and a new reference has to be created, the reference |
| 594 * will only be stored in [linkedReferences]. | 587 * will only be stored in [linkedReferences]. |
| 595 */ | 588 */ |
| 596 int serializeReferenceForType(DartType type, bool linked) { | 589 int serializeReferenceForType(DartType type, bool linked) { |
| 597 Element element = type.element; | 590 Element element = type.element; |
| 598 LibraryElement dependentLibrary = element.library; | 591 LibraryElement dependentLibrary = element.library; |
| 599 if (dependentLibrary == null) { | 592 if (dependentLibrary == null) { |
| 600 assert(type.isDynamic); | 593 assert(type.isDynamic); |
| 601 if (type is UndefinedTypeImpl) { | 594 if (type is UndefinedTypeImpl) { |
| 602 return serializeUnresolvedReference(); | 595 return serializeUnresolvedReference(); |
| 603 } else { | |
| 604 return serializeDynamicReference(); | |
| 605 } | 596 } |
| 606 } else { | 597 // Note: for a type which is truly `dynamic`, fall through to use |
| 607 return _getElementReferenceId(element, linked: linked); | 598 // [_getElementReferenceId]. |
| 608 } | 599 } |
| 600 return _getElementReferenceId(element, linked: linked); |
| 609 } | 601 } |
| 610 | 602 |
| 611 /** | 603 /** |
| 612 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. | 604 * Serialize the given [typedefElement], creating an [UnlinkedTypedef]. |
| 613 */ | 605 */ |
| 614 UnlinkedTypedefBuilder serializeTypedef( | 606 UnlinkedTypedefBuilder serializeTypedef( |
| 615 FunctionTypeAliasElement typedefElement) { | 607 FunctionTypeAliasElement typedefElement) { |
| 616 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); | 608 UnlinkedTypedefBuilder b = new UnlinkedTypedefBuilder(); |
| 617 b.name = typedefElement.name; | 609 b.name = typedefElement.name; |
| 618 b.nameOffset = typedefElement.nameOffset; | 610 b.nameOffset = typedefElement.nameOffset; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 int storeLinkedType(DartType type, Element context) { | 742 int storeLinkedType(DartType type, Element context) { |
| 751 int slot = ++numSlots; | 743 int slot = ++numSlots; |
| 752 if (type != null) { | 744 if (type != null) { |
| 753 deferredLinkedTypes | 745 deferredLinkedTypes |
| 754 .add(() => serializeTypeRef(type, context, linked: true, slot: slot)); | 746 .add(() => serializeTypeRef(type, context, linked: true, slot: slot)); |
| 755 } | 747 } |
| 756 return slot; | 748 return slot; |
| 757 } | 749 } |
| 758 | 750 |
| 759 int _getElementReferenceId(Element element, {bool linked: false}) { | 751 int _getElementReferenceId(Element element, {bool linked: false}) { |
| 760 LibraryElement dependentLibrary = element.library; | |
| 761 return referenceMap.putIfAbsent(element, () { | 752 return referenceMap.putIfAbsent(element, () { |
| 762 CompilationUnitElement unitElement = | 753 LibraryElement dependentLibrary = element.library; |
| 763 element.getAncestor((Element e) => e is CompilationUnitElement); | 754 int unit; |
| 764 int unit = dependentLibrary.units.indexOf(unitElement); | 755 if (element.library == null) { |
| 765 assert(unit != -1); | 756 assert(element == librarySerializer.typeProvider.dynamicType.element); |
| 757 unit = 0; |
| 758 dependentLibrary = librarySerializer.libraryElement; |
| 759 } else { |
| 760 CompilationUnitElement unitElement = |
| 761 element.getAncestor((Element e) => e is CompilationUnitElement); |
| 762 unit = dependentLibrary.units.indexOf(unitElement); |
| 763 assert(unit != -1); |
| 764 } |
| 766 int numTypeParameters = 0; | 765 int numTypeParameters = 0; |
| 767 if (element is TypeParameterizedElement) { | 766 if (element is TypeParameterizedElement) { |
| 768 numTypeParameters = element.typeParameters.length; | 767 numTypeParameters = element.typeParameters.length; |
| 769 } | 768 } |
| 770 LinkedReferenceBuilder linkedReference = new LinkedReferenceBuilder( | 769 LinkedReferenceBuilder linkedReference = new LinkedReferenceBuilder( |
| 771 dependency: librarySerializer.serializeDependency(dependentLibrary), | 770 dependency: librarySerializer.serializeDependency(dependentLibrary), |
| 772 kind: _getReferenceKind(element), | 771 kind: _getReferenceKind(element), |
| 773 unit: unit, | 772 unit: unit, |
| 774 numTypeParameters: numTypeParameters); | 773 numTypeParameters: numTypeParameters); |
| 775 if (linked) { | 774 if (linked) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 exportNames.add(new LinkedExportNameBuilder( | 996 exportNames.add(new LinkedExportNameBuilder( |
| 998 name: name, | 997 name: name, |
| 999 dependency: serializeDependency(dependentLibrary), | 998 dependency: serializeDependency(dependentLibrary), |
| 1000 unit: unit, | 999 unit: unit, |
| 1001 kind: kind)); | 1000 kind: kind)); |
| 1002 } | 1001 } |
| 1003 pb.exportNames = exportNames; | 1002 pb.exportNames = exportNames; |
| 1004 return pb; | 1003 return pb; |
| 1005 } | 1004 } |
| 1006 } | 1005 } |
| OLD | NEW |