| 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 summary_resynthesizer; | 5 library summary_resynthesizer; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/element_handle.dart'; | 9 import 'package:analyzer/src/generated/element_handle.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 * Get the [LibraryElement] for the given [uri], resynthesizing it if it | 110 * Get the [LibraryElement] for the given [uri], resynthesizing it if it |
| 111 * hasn't been resynthesized already. | 111 * hasn't been resynthesized already. |
| 112 */ | 112 */ |
| 113 LibraryElement getLibraryElement(String uri) { | 113 LibraryElement getLibraryElement(String uri) { |
| 114 return _resynthesizedLibraries.putIfAbsent(uri, () { | 114 return _resynthesizedLibraries.putIfAbsent(uri, () { |
| 115 PrelinkedLibrary serializedLibrary = getPrelinkedSummary(uri); | 115 PrelinkedLibrary serializedLibrary = getPrelinkedSummary(uri); |
| 116 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[ | 116 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[ |
| 117 getUnlinkedSummary(uri) | 117 getUnlinkedSummary(uri) |
| 118 ]; | 118 ]; |
| 119 Source librarySource = _getSource(uri); | 119 Source librarySource = _getSource(uri); |
| 120 for (UnlinkedPart part in serializedUnits[0].publicNamespace.parts) { | 120 for (String part in serializedUnits[0].publicNamespace.parts) { |
| 121 Source partSource = sourceFactory.resolveUri(librarySource, part.uri); | 121 Source partSource = sourceFactory.resolveUri(librarySource, part); |
| 122 String partAbsUri = partSource.uri.toString(); | 122 String partAbsUri = partSource.uri.toString(); |
| 123 serializedUnits.add(getUnlinkedSummary(partAbsUri)); | 123 serializedUnits.add(getUnlinkedSummary(partAbsUri)); |
| 124 } | 124 } |
| 125 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( | 125 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( |
| 126 this, serializedLibrary, serializedUnits, librarySource); | 126 this, serializedLibrary, serializedUnits, librarySource); |
| 127 LibraryElement library = libraryResynthesizer.buildLibrary(); | 127 LibraryElement library = libraryResynthesizer.buildLibrary(); |
| 128 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements; | 128 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements; |
| 129 return library; | 129 return library; |
| 130 }); | 130 }); |
| 131 } | 131 } |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 executableElement.type = new FunctionTypeImpl.elementWithNameAndArgs( | 464 executableElement.type = new FunctionTypeImpl.elementWithNameAndArgs( |
| 465 executableElement, null, currentTypeArguments); | 465 executableElement, null, currentTypeArguments); |
| 466 executableElement.hasImplicitReturnType = | 466 executableElement.hasImplicitReturnType = |
| 467 serializedExecutable.hasImplicitReturnType; | 467 serializedExecutable.hasImplicitReturnType; |
| 468 executableElement.external = serializedExecutable.isExternal; | 468 executableElement.external = serializedExecutable.isExternal; |
| 469 } | 469 } |
| 470 | 470 |
| 471 /** | 471 /** |
| 472 * Resynthesize an [ExportElement], | 472 * Resynthesize an [ExportElement], |
| 473 */ | 473 */ |
| 474 ExportElement buildExport(UnlinkedExport serializedExport) { | 474 ExportElement buildExport(UnlinkedExportPublic serializedExportPublic, |
| 475 UnlinkedExportNonPublic serializedExportNonPublic) { |
| 475 ExportElementImpl exportElement = new ExportElementImpl(0); | 476 ExportElementImpl exportElement = new ExportElementImpl(0); |
| 476 String exportedLibraryUri = summaryResynthesizer.sourceFactory | 477 String exportedLibraryUri = summaryResynthesizer.sourceFactory |
| 477 .resolveUri(librarySource, serializedExport.uri) | 478 .resolveUri(librarySource, serializedExportPublic.uri) |
| 478 .uri | 479 .uri |
| 479 .toString(); | 480 .toString(); |
| 480 exportElement.exportedLibrary = new LibraryElementHandle( | 481 exportElement.exportedLibrary = new LibraryElementHandle( |
| 481 summaryResynthesizer, | 482 summaryResynthesizer, |
| 482 new ElementLocationImpl.con3(<String>[exportedLibraryUri])); | 483 new ElementLocationImpl.con3(<String>[exportedLibraryUri])); |
| 483 exportElement.uri = serializedExport.uri; | 484 exportElement.uri = serializedExportPublic.uri; |
| 484 exportElement.combinators = | 485 exportElement.combinators = |
| 485 serializedExport.combinators.map(buildCombinator).toList(); | 486 serializedExportPublic.combinators.map(buildCombinator).toList(); |
| 487 exportElement.uriOffset = serializedExportNonPublic.uriOffset; |
| 488 exportElement.uriEnd = serializedExportNonPublic.uriEnd; |
| 486 return exportElement; | 489 return exportElement; |
| 487 } | 490 } |
| 488 | 491 |
| 489 /** | 492 /** |
| 490 * Resynthesize a [FieldElement]. | 493 * Resynthesize a [FieldElement]. |
| 491 */ | 494 */ |
| 492 FieldElement buildField(UnlinkedVariable serializedField) { | 495 FieldElement buildField(UnlinkedVariable serializedField) { |
| 493 FieldElementImpl fieldElement = | 496 FieldElementImpl fieldElement = |
| 494 new FieldElementImpl(serializedField.name, -1); | 497 new FieldElementImpl(serializedField.name, -1); |
| 495 fieldElement.type = buildType(serializedField.type); | 498 fieldElement.type = buildType(serializedField.type); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 librarySource, prelinkedLibrary.dependencies[dependency].uri) | 595 librarySource, prelinkedLibrary.dependencies[dependency].uri) |
| 593 .uri | 596 .uri |
| 594 .toString(); | 597 .toString(); |
| 595 importElement.importedLibrary = new LibraryElementHandle( | 598 importElement.importedLibrary = new LibraryElementHandle( |
| 596 summaryResynthesizer, | 599 summaryResynthesizer, |
| 597 new ElementLocationImpl.con3(<String>[absoluteUri])); | 600 new ElementLocationImpl.con3(<String>[absoluteUri])); |
| 598 if (isSynthetic) { | 601 if (isSynthetic) { |
| 599 importElement.synthetic = true; | 602 importElement.synthetic = true; |
| 600 } else { | 603 } else { |
| 601 importElement.uri = serializedImport.uri; | 604 importElement.uri = serializedImport.uri; |
| 605 importElement.uriOffset = serializedImport.uriOffset; |
| 606 importElement.uriEnd = serializedImport.uriEnd; |
| 602 } | 607 } |
| 603 if (serializedImport.prefixReference != 0) { | 608 if (serializedImport.prefixReference != 0) { |
| 604 UnlinkedReference serializedPrefix = | 609 UnlinkedReference serializedPrefix = |
| 605 unlinkedUnits[0].references[serializedImport.prefixReference]; | 610 unlinkedUnits[0].references[serializedImport.prefixReference]; |
| 606 importElement.prefix = new PrefixElementImpl(serializedPrefix.name, -1); | 611 importElement.prefix = new PrefixElementImpl(serializedPrefix.name, -1); |
| 607 } | 612 } |
| 608 importElement.combinators = | 613 importElement.combinators = |
| 609 serializedImport.combinators.map(buildCombinator).toList(); | 614 serializedImport.combinators.map(buildCombinator).toList(); |
| 610 return importElement; | 615 return importElement; |
| 611 } | 616 } |
| 612 | 617 |
| 613 /** | 618 /** |
| 614 * Main entry point. Resynthesize the [LibraryElement] and return it. | 619 * Main entry point. Resynthesize the [LibraryElement] and return it. |
| 615 */ | 620 */ |
| 616 LibraryElement buildLibrary() { | 621 LibraryElement buildLibrary() { |
| 617 // TODO(paulberry): is it ok to pass -1 for offset and nameLength? | 622 // TODO(paulberry): is it ok to pass -1 for offset and nameLength? |
| 618 LibraryElementImpl libraryElement = new LibraryElementImpl( | 623 LibraryElementImpl libraryElement = new LibraryElementImpl( |
| 619 summaryResynthesizer.context, unlinkedUnits[0].libraryName, -1, -1); | 624 summaryResynthesizer.context, unlinkedUnits[0].libraryName, -1, -1); |
| 620 CompilationUnitElementImpl definingCompilationUnit = | 625 CompilationUnitElementImpl definingCompilationUnit = |
| 621 new CompilationUnitElementImpl(librarySource.shortName); | 626 new CompilationUnitElementImpl(librarySource.shortName); |
| 622 libraryElement.definingCompilationUnit = definingCompilationUnit; | 627 libraryElement.definingCompilationUnit = definingCompilationUnit; |
| 623 definingCompilationUnit.source = librarySource; | 628 definingCompilationUnit.source = librarySource; |
| 624 definingCompilationUnit.librarySource = librarySource; | 629 definingCompilationUnit.librarySource = librarySource; |
| 625 List<CompilationUnitElement> parts = <CompilationUnitElement>[]; | 630 List<CompilationUnitElement> parts = <CompilationUnitElement>[]; |
| 626 UnlinkedUnit unlinkedDefiningUnit = unlinkedUnits[0]; | 631 UnlinkedUnit unlinkedDefiningUnit = unlinkedUnits[0]; |
| 627 assert(unlinkedDefiningUnit.publicNamespace.parts.length + 1 == | 632 assert(unlinkedDefiningUnit.publicNamespace.parts.length + 1 == |
| 628 prelinkedLibrary.units.length); | 633 prelinkedLibrary.units.length); |
| 629 for (int i = 1; i < prelinkedLibrary.units.length; i++) { | 634 for (int i = 1; i < prelinkedLibrary.units.length; i++) { |
| 630 CompilationUnitElementImpl part = buildPart( | 635 CompilationUnitElementImpl part = buildPart( |
| 631 unlinkedDefiningUnit.publicNamespace.parts[i - 1].uri, | 636 unlinkedDefiningUnit.publicNamespace.parts[i - 1], |
| 637 unlinkedDefiningUnit.parts[i - 1], |
| 632 unlinkedUnits[i]); | 638 unlinkedUnits[i]); |
| 633 parts.add(part); | 639 parts.add(part); |
| 634 } | 640 } |
| 635 libraryElement.parts = parts; | 641 libraryElement.parts = parts; |
| 636 List<ImportElement> imports = <ImportElement>[]; | 642 List<ImportElement> imports = <ImportElement>[]; |
| 637 for (int i = 0; i < unlinkedDefiningUnit.imports.length; i++) { | 643 for (int i = 0; i < unlinkedDefiningUnit.imports.length; i++) { |
| 638 imports.add(buildImport(unlinkedDefiningUnit.imports[i], | 644 imports.add(buildImport(unlinkedDefiningUnit.imports[i], |
| 639 prelinkedLibrary.importDependencies[i])); | 645 prelinkedLibrary.importDependencies[i])); |
| 640 } | 646 } |
| 641 libraryElement.imports = imports; | 647 libraryElement.imports = imports; |
| 642 libraryElement.exports = | 648 List<ExportElement> exports = <ExportElement>[]; |
| 643 unlinkedDefiningUnit.publicNamespace.exports.map(buildExport).toList(); | 649 assert(unlinkedDefiningUnit.exports.length == |
| 650 unlinkedDefiningUnit.publicNamespace.exports.length); |
| 651 for (int i = 0; i < unlinkedDefiningUnit.exports.length; i++) { |
| 652 exports.add(buildExport(unlinkedDefiningUnit.publicNamespace.exports[i], |
| 653 unlinkedDefiningUnit.exports[i])); |
| 654 } |
| 655 libraryElement.exports = exports; |
| 644 populateUnit(definingCompilationUnit, 0); | 656 populateUnit(definingCompilationUnit, 0); |
| 645 for (int i = 0; i < parts.length; i++) { | 657 for (int i = 0; i < parts.length; i++) { |
| 646 populateUnit(parts[i], i + 1); | 658 populateUnit(parts[i], i + 1); |
| 647 } | 659 } |
| 648 if (isCoreLibrary) { | 660 if (isCoreLibrary) { |
| 649 ClassElement objectElement = libraryElement.getType('Object'); | 661 ClassElement objectElement = libraryElement.getType('Object'); |
| 650 assert(objectElement != null); | 662 assert(objectElement != null); |
| 651 for (ClassElementImpl classElement in delayedObjectSubclasses) { | 663 for (ClassElementImpl classElement in delayedObjectSubclasses) { |
| 652 classElement.supertype = objectElement.type; | 664 classElement.supertype = objectElement.type; |
| 653 } | 665 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 break; | 704 break; |
| 693 } | 705 } |
| 694 return parameterElement; | 706 return parameterElement; |
| 695 } | 707 } |
| 696 | 708 |
| 697 /** | 709 /** |
| 698 * Create, but do not populate, the [CompilationUnitElement] for a part other | 710 * Create, but do not populate, the [CompilationUnitElement] for a part other |
| 699 * than the defining compilation unit. | 711 * than the defining compilation unit. |
| 700 */ | 712 */ |
| 701 CompilationUnitElementImpl buildPart( | 713 CompilationUnitElementImpl buildPart( |
| 702 String uri, UnlinkedUnit serializedPart) { | 714 String uri, UnlinkedPart partDecl, UnlinkedUnit serializedPart) { |
| 703 Source unitSource = | 715 Source unitSource = |
| 704 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri); | 716 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri); |
| 705 CompilationUnitElementImpl partUnit = | 717 CompilationUnitElementImpl partUnit = |
| 706 new CompilationUnitElementImpl(unitSource.shortName); | 718 new CompilationUnitElementImpl(unitSource.shortName); |
| 719 partUnit.uriOffset = partDecl.uriOffset; |
| 720 partUnit.uriEnd = partDecl.uriEnd; |
| 707 partUnit.source = unitSource; | 721 partUnit.source = unitSource; |
| 708 partUnit.librarySource = librarySource; | 722 partUnit.librarySource = librarySource; |
| 709 partUnit.uri = uri; | 723 partUnit.uri = uri; |
| 710 return partUnit; | 724 return partUnit; |
| 711 } | 725 } |
| 712 | 726 |
| 713 /** | 727 /** |
| 714 * Build a [DartType] object based on an [UnlinkedTypeRef]. This [DartType] | 728 * Build a [DartType] object based on an [UnlinkedTypeRef]. This [DartType] |
| 715 * may refer to elements in other libraries than the library being | 729 * may refer to elements in other libraries than the library being |
| 716 * deserialized, so handles are used to avoid having to deserialize other | 730 * deserialized, so handles are used to avoid having to deserialize other |
| 717 * libraries in the process. | 731 * libraries in the process. |
| 718 */ | 732 */ |
| 719 DartType buildType(UnlinkedTypeRef type) { | 733 DartType buildType(UnlinkedTypeRef type) { |
| 720 if (type.paramReference != 0) { | 734 if (type.paramReference != 0) { |
| 721 // TODO(paulberry): make this work for generic methods. | 735 // TODO(paulberry): make this work for generic methods. |
| 722 return currentTypeParameters[ | 736 return currentTypeParameters[ |
| 723 currentTypeParameters.length - type.paramReference] | 737 currentTypeParameters.length - type.paramReference].type; |
| 724 .type; | |
| 725 } else { | 738 } else { |
| 726 // TODO(paulberry): handle references to things other than classes (note: | 739 // TODO(paulberry): handle references to things other than classes (note: |
| 727 // this should only occur in the case of erroneous code). | 740 // this should only occur in the case of erroneous code). |
| 728 // TODO(paulberry): test reference to something inside a part. | 741 // TODO(paulberry): test reference to something inside a part. |
| 729 // TODO(paulberry): test reference to something inside a part of the | 742 // TODO(paulberry): test reference to something inside a part of the |
| 730 // current lib. | 743 // current lib. |
| 731 UnlinkedReference reference = unlinkedUnit.references[type.reference]; | 744 UnlinkedReference reference = unlinkedUnit.references[type.reference]; |
| 732 PrelinkedReference referenceResolution = | 745 PrelinkedReference referenceResolution = |
| 733 prelinkedUnit.references[type.reference]; | 746 prelinkedUnit.references[type.reference]; |
| 734 String referencedLibraryUri; | 747 String referencedLibraryUri; |
| 735 String partUri; | 748 String partUri; |
| 736 if (referenceResolution.dependency != 0) { | 749 if (referenceResolution.dependency != 0) { |
| 737 PrelinkedDependency dependency = | 750 PrelinkedDependency dependency = |
| 738 prelinkedLibrary.dependencies[referenceResolution.dependency]; | 751 prelinkedLibrary.dependencies[referenceResolution.dependency]; |
| 739 Source referencedLibrarySource = summaryResynthesizer.sourceFactory | 752 Source referencedLibrarySource = summaryResynthesizer.sourceFactory |
| 740 .resolveUri(librarySource, dependency.uri); | 753 .resolveUri(librarySource, dependency.uri); |
| 741 referencedLibraryUri = referencedLibrarySource.uri.toString(); | 754 referencedLibraryUri = referencedLibrarySource.uri.toString(); |
| 742 // TODO(paulberry): consider changing Location format so that this is | 755 // TODO(paulberry): consider changing Location format so that this is |
| 743 // not necessary (2nd string in location should just be the unit | 756 // not necessary (2nd string in location should just be the unit |
| 744 // number). | 757 // number). |
| 745 if (referenceResolution.unit != 0) { | 758 if (referenceResolution.unit != 0) { |
| 746 UnlinkedUnit referencedLibraryDefiningUnit = | 759 UnlinkedUnit referencedLibraryDefiningUnit = |
| 747 summaryResynthesizer.getUnlinkedSummary(referencedLibraryUri); | 760 summaryResynthesizer.getUnlinkedSummary(referencedLibraryUri); |
| 748 String uri = referencedLibraryDefiningUnit | 761 String uri = referencedLibraryDefiningUnit.publicNamespace.parts[ |
| 749 .publicNamespace.parts[referenceResolution.unit - 1].uri; | 762 referenceResolution.unit - 1]; |
| 750 Source partSource = summaryResynthesizer.sourceFactory | 763 Source partSource = summaryResynthesizer.sourceFactory |
| 751 .resolveUri(referencedLibrarySource, uri); | 764 .resolveUri(referencedLibrarySource, uri); |
| 752 partUri = partSource.uri.toString(); | 765 partUri = partSource.uri.toString(); |
| 753 } else { | 766 } else { |
| 754 partUri = referencedLibraryUri; | 767 partUri = referencedLibraryUri; |
| 755 } | 768 } |
| 756 } else if (referenceResolution.kind == | 769 } else if (referenceResolution.kind == |
| 757 PrelinkedReferenceKind.unresolved) { | 770 PrelinkedReferenceKind.unresolved) { |
| 758 return summaryResynthesizer.typeProvider.undefinedType; | 771 return summaryResynthesizer.typeProvider.undefinedType; |
| 759 } else if (reference.name.isEmpty) { | 772 } else if (reference.name.isEmpty) { |
| 760 return summaryResynthesizer.typeProvider.dynamicType; | 773 return summaryResynthesizer.typeProvider.dynamicType; |
| 761 } else { | 774 } else { |
| 762 referencedLibraryUri = librarySource.uri.toString(); | 775 referencedLibraryUri = librarySource.uri.toString(); |
| 763 if (referenceResolution.unit != 0) { | 776 if (referenceResolution.unit != 0) { |
| 764 String uri = unlinkedUnits[0] | 777 String uri = unlinkedUnits[0].publicNamespace.parts[ |
| 765 .publicNamespace | 778 referenceResolution.unit - 1]; |
| 766 .parts[referenceResolution.unit - 1] | |
| 767 .uri; | |
| 768 Source partSource = | 779 Source partSource = |
| 769 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri); | 780 summaryResynthesizer.sourceFactory.resolveUri(librarySource, uri); |
| 770 partUri = partSource.uri.toString(); | 781 partUri = partSource.uri.toString(); |
| 771 } else { | 782 } else { |
| 772 partUri = referencedLibraryUri; | 783 partUri = referencedLibraryUri; |
| 773 } | 784 } |
| 774 } | 785 } |
| 775 ElementLocationImpl location = new ElementLocationImpl.con3( | 786 ElementLocationImpl location = new ElementLocationImpl.con3( |
| 776 <String>[referencedLibraryUri, partUri, reference.name]); | 787 <String>[referencedLibraryUri, partUri, reference.name]); |
| 777 List<DartType> typeArguments = const <DartType>[]; | 788 List<DartType> typeArguments = const <DartType>[]; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 928 } | 939 } |
| 929 for (FunctionTypeAliasElement typeAlias in unit.functionTypeAliases) { | 940 for (FunctionTypeAliasElement typeAlias in unit.functionTypeAliases) { |
| 930 elementMap[typeAlias.name] = typeAlias; | 941 elementMap[typeAlias.name] = typeAlias; |
| 931 } | 942 } |
| 932 resummarizedElements[absoluteUri] = elementMap; | 943 resummarizedElements[absoluteUri] = elementMap; |
| 933 unitHolder = null; | 944 unitHolder = null; |
| 934 prelinkedUnit = null; | 945 prelinkedUnit = null; |
| 935 unlinkedUnit = null; | 946 unlinkedUnit = null; |
| 936 } | 947 } |
| 937 } | 948 } |
| OLD | NEW |