| 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 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 SummaryResynthesizer(AnalysisContext context, this.typeProvider, | 75 SummaryResynthesizer(AnalysisContext context, this.typeProvider, |
| 76 this.getPrelinkedSummary, this.getUnlinkedSummary, this.sourceFactory) | 76 this.getPrelinkedSummary, this.getUnlinkedSummary, this.sourceFactory) |
| 77 : super(context); | 77 : super(context); |
| 78 | 78 |
| 79 /** | 79 /** |
| 80 * Number of libraries that have been resynthesized so far. | 80 * Number of libraries that have been resynthesized so far. |
| 81 */ | 81 */ |
| 82 int get resynthesisCount => _resynthesizedLibraries.length; | 82 int get resynthesisCount => _resynthesizedLibraries.length; |
| 83 | 83 |
| 84 /** |
| 85 * Perform delayed finalization of the `dart:core` and `dart:async` libraries. |
| 86 */ |
| 87 void finalizeCoreAsyncLibraries() { |
| 88 (_resynthesizedLibraries['dart:core'] as LibraryElementImpl) |
| 89 .createLoadLibraryFunction(typeProvider); |
| 90 (_resynthesizedLibraries['dart:async'] as LibraryElementImpl) |
| 91 .createLoadLibraryFunction(typeProvider); |
| 92 } |
| 93 |
| 84 @override | 94 @override |
| 85 Element getElement(ElementLocation location) { | 95 Element getElement(ElementLocation location) { |
| 86 if (location.components.length == 1) { | 96 if (location.components.length == 1) { |
| 87 return getLibraryElement(location.components[0]); | 97 return getLibraryElement(location.components[0]); |
| 88 } else if (location.components.length == 3) { | 98 } else if (location.components.length == 3) { |
| 89 String uri = location.components[0]; | 99 String uri = location.components[0]; |
| 90 Map<String, Map<String, Element>> libraryMap = | 100 Map<String, Map<String, Element>> libraryMap = |
| 91 _resynthesizedElements[uri]; | 101 _resynthesizedElements[uri]; |
| 92 if (libraryMap == null) { | 102 if (libraryMap == null) { |
| 93 getLibraryElement(uri); | 103 getLibraryElement(uri); |
| (...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 importElement.combinators = | 712 importElement.combinators = |
| 703 serializedImport.combinators.map(buildCombinator).toList(); | 713 serializedImport.combinators.map(buildCombinator).toList(); |
| 704 return importElement; | 714 return importElement; |
| 705 } | 715 } |
| 706 | 716 |
| 707 /** | 717 /** |
| 708 * Main entry point. Resynthesize the [LibraryElement] and return it. | 718 * Main entry point. Resynthesize the [LibraryElement] and return it. |
| 709 */ | 719 */ |
| 710 LibraryElement buildLibrary() { | 720 LibraryElement buildLibrary() { |
| 711 bool hasName = unlinkedUnits[0].libraryName.isNotEmpty; | 721 bool hasName = unlinkedUnits[0].libraryName.isNotEmpty; |
| 712 LibraryElementImpl libraryElement = new LibraryElementImpl( | 722 LibraryElementImpl library = new LibraryElementImpl( |
| 713 summaryResynthesizer.context, | 723 summaryResynthesizer.context, |
| 714 unlinkedUnits[0].libraryName, | 724 unlinkedUnits[0].libraryName, |
| 715 hasName ? unlinkedUnits[0].libraryNameOffset : -1, | 725 hasName ? unlinkedUnits[0].libraryNameOffset : -1, |
| 716 unlinkedUnits[0].libraryNameLength); | 726 unlinkedUnits[0].libraryNameLength); |
| 717 buildDocumentation( | 727 buildDocumentation(library, unlinkedUnits[0].libraryDocumentationComment); |
| 718 libraryElement, unlinkedUnits[0].libraryDocumentationComment); | |
| 719 CompilationUnitElementImpl definingCompilationUnit = | 728 CompilationUnitElementImpl definingCompilationUnit = |
| 720 new CompilationUnitElementImpl(librarySource.shortName); | 729 new CompilationUnitElementImpl(librarySource.shortName); |
| 721 libraryElement.definingCompilationUnit = definingCompilationUnit; | 730 library.definingCompilationUnit = definingCompilationUnit; |
| 722 definingCompilationUnit.source = librarySource; | 731 definingCompilationUnit.source = librarySource; |
| 723 definingCompilationUnit.librarySource = librarySource; | 732 definingCompilationUnit.librarySource = librarySource; |
| 724 List<CompilationUnitElement> parts = <CompilationUnitElement>[]; | 733 List<CompilationUnitElement> parts = <CompilationUnitElement>[]; |
| 725 UnlinkedUnit unlinkedDefiningUnit = unlinkedUnits[0]; | 734 UnlinkedUnit unlinkedDefiningUnit = unlinkedUnits[0]; |
| 726 assert(unlinkedDefiningUnit.publicNamespace.parts.length + 1 == | 735 assert(unlinkedDefiningUnit.publicNamespace.parts.length + 1 == |
| 727 prelinkedLibrary.units.length); | 736 prelinkedLibrary.units.length); |
| 728 for (int i = 1; i < prelinkedLibrary.units.length; i++) { | 737 for (int i = 1; i < prelinkedLibrary.units.length; i++) { |
| 729 CompilationUnitElementImpl part = buildPart( | 738 CompilationUnitElementImpl part = buildPart( |
| 730 unlinkedDefiningUnit.publicNamespace.parts[i - 1], | 739 unlinkedDefiningUnit.publicNamespace.parts[i - 1], |
| 731 unlinkedDefiningUnit.parts[i - 1], | 740 unlinkedDefiningUnit.parts[i - 1], |
| 732 unlinkedUnits[i]); | 741 unlinkedUnits[i]); |
| 733 parts.add(part); | 742 parts.add(part); |
| 734 } | 743 } |
| 735 libraryElement.parts = parts; | 744 library.parts = parts; |
| 736 List<ImportElement> imports = <ImportElement>[]; | 745 List<ImportElement> imports = <ImportElement>[]; |
| 737 for (int i = 0; i < unlinkedDefiningUnit.imports.length; i++) { | 746 for (int i = 0; i < unlinkedDefiningUnit.imports.length; i++) { |
| 738 imports.add(buildImport(unlinkedDefiningUnit.imports[i], | 747 imports.add(buildImport(unlinkedDefiningUnit.imports[i], |
| 739 prelinkedLibrary.importDependencies[i])); | 748 prelinkedLibrary.importDependencies[i])); |
| 740 } | 749 } |
| 741 libraryElement.imports = imports; | 750 library.imports = imports; |
| 742 List<ExportElement> exports = <ExportElement>[]; | 751 List<ExportElement> exports = <ExportElement>[]; |
| 743 assert(unlinkedDefiningUnit.exports.length == | 752 assert(unlinkedDefiningUnit.exports.length == |
| 744 unlinkedDefiningUnit.publicNamespace.exports.length); | 753 unlinkedDefiningUnit.publicNamespace.exports.length); |
| 745 for (int i = 0; i < unlinkedDefiningUnit.exports.length; i++) { | 754 for (int i = 0; i < unlinkedDefiningUnit.exports.length; i++) { |
| 746 exports.add(buildExport(unlinkedDefiningUnit.publicNamespace.exports[i], | 755 exports.add(buildExport(unlinkedDefiningUnit.publicNamespace.exports[i], |
| 747 unlinkedDefiningUnit.exports[i])); | 756 unlinkedDefiningUnit.exports[i])); |
| 748 } | 757 } |
| 749 libraryElement.exports = exports; | 758 library.exports = exports; |
| 750 populateUnit(definingCompilationUnit, 0); | 759 populateUnit(definingCompilationUnit, 0); |
| 751 for (int i = 0; i < parts.length; i++) { | 760 for (int i = 0; i < parts.length; i++) { |
| 752 populateUnit(parts[i], i + 1); | 761 populateUnit(parts[i], i + 1); |
| 753 } | 762 } |
| 754 BuildLibraryElementUtils.patchTopLevelAccessors(libraryElement); | 763 BuildLibraryElementUtils.patchTopLevelAccessors(library); |
| 755 // Update delayed Object class references. | 764 // Update delayed Object class references. |
| 756 if (isCoreLibrary) { | 765 if (isCoreLibrary) { |
| 757 ClassElement objectElement = libraryElement.getType('Object'); | 766 ClassElement objectElement = library.getType('Object'); |
| 758 assert(objectElement != null); | 767 assert(objectElement != null); |
| 759 for (ClassElementImpl classElement in delayedObjectSubclasses) { | 768 for (ClassElementImpl classElement in delayedObjectSubclasses) { |
| 760 classElement.supertype = objectElement.type; | 769 classElement.supertype = objectElement.type; |
| 761 } | 770 } |
| 762 } | 771 } |
| 763 // Compute namespaces. | 772 // Compute namespaces. |
| 764 libraryElement.publicNamespace = | 773 library.publicNamespace = |
| 765 new NamespaceBuilder().createPublicNamespaceForLibrary(libraryElement); | 774 new NamespaceBuilder().createPublicNamespaceForLibrary(library); |
| 766 libraryElement.exportNamespace = buildExportNamespace( | 775 library.exportNamespace = buildExportNamespace( |
| 767 libraryElement.publicNamespace, prelinkedLibrary.exportNames); | 776 library.publicNamespace, prelinkedLibrary.exportNames); |
| 768 // Find the entry point. Note: we can't use element.isEntryPoint because | 777 // Find the entry point. Note: we can't use element.isEntryPoint because |
| 769 // that will trigger resynthesis of exported libraries. | 778 // that will trigger resynthesis of exported libraries. |
| 770 Element entryPoint = | 779 Element entryPoint = |
| 771 libraryElement.exportNamespace.get(FunctionElement.MAIN_FUNCTION_NAME); | 780 library.exportNamespace.get(FunctionElement.MAIN_FUNCTION_NAME); |
| 772 if (entryPoint is FunctionElement) { | 781 if (entryPoint is FunctionElement) { |
| 773 libraryElement.entryPoint = entryPoint; | 782 library.entryPoint = entryPoint; |
| 774 } | 783 } |
| 775 // Create the synthetic element for `loadLibrary`. | 784 // Create the synthetic element for `loadLibrary`. |
| 776 libraryElement.createLoadLibraryFunction(summaryResynthesizer.typeProvider); | 785 // Until the client received dart:core and dart:async, we cannot do this, |
| 786 // because the TypeProvider is not fully initialized. So, it is up to the |
| 787 // Dart SDK client to initialize TypeProvider and finish the dart:core and |
| 788 // dart:async libraries creation. |
| 789 if (library.name != 'dart.core' && library.name != 'dart.async') { |
| 790 library.createLoadLibraryFunction(summaryResynthesizer.typeProvider); |
| 791 } |
| 777 // Done. | 792 // Done. |
| 778 return libraryElement; | 793 return library; |
| 779 } | 794 } |
| 780 | 795 |
| 781 /** | 796 /** |
| 782 * Resynthesize a [ParameterElement]. | 797 * Resynthesize a [ParameterElement]. |
| 783 */ | 798 */ |
| 784 ParameterElement buildParameter(UnlinkedParam serializedParameter) { | 799 ParameterElement buildParameter(UnlinkedParam serializedParameter) { |
| 785 ParameterElementImpl parameterElement = new ParameterElementImpl( | 800 ParameterElementImpl parameterElement = new ParameterElementImpl( |
| 786 serializedParameter.name, serializedParameter.nameOffset); | 801 serializedParameter.name, serializedParameter.nameOffset); |
| 787 if (serializedParameter.isFunctionTyped) { | 802 if (serializedParameter.isFunctionTyped) { |
| 788 FunctionElementImpl parameterTypeElement = | 803 FunctionElementImpl parameterTypeElement = |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 /** | 853 /** |
| 839 * Build a [DartType] object based on an [UnlinkedTypeRef]. This [DartType] | 854 * Build a [DartType] object based on an [UnlinkedTypeRef]. This [DartType] |
| 840 * may refer to elements in other libraries than the library being | 855 * may refer to elements in other libraries than the library being |
| 841 * deserialized, so handles are used to avoid having to deserialize other | 856 * deserialized, so handles are used to avoid having to deserialize other |
| 842 * libraries in the process. | 857 * libraries in the process. |
| 843 */ | 858 */ |
| 844 DartType buildType(UnlinkedTypeRef type) { | 859 DartType buildType(UnlinkedTypeRef type) { |
| 845 if (type.paramReference != 0) { | 860 if (type.paramReference != 0) { |
| 846 // TODO(paulberry): make this work for generic methods. | 861 // TODO(paulberry): make this work for generic methods. |
| 847 return currentTypeParameters[ | 862 return currentTypeParameters[ |
| 848 currentTypeParameters.length - type.paramReference].type; | 863 currentTypeParameters.length - type.paramReference] |
| 864 .type; |
| 849 } else { | 865 } else { |
| 850 // TODO(paulberry): handle references to things other than classes (note: | 866 // TODO(paulberry): handle references to things other than classes (note: |
| 851 // this should only occur in the case of erroneous code). | 867 // this should only occur in the case of erroneous code). |
| 852 // TODO(paulberry): test reference to something inside a part. | 868 // TODO(paulberry): test reference to something inside a part. |
| 853 // TODO(paulberry): test reference to something inside a part of the | 869 // TODO(paulberry): test reference to something inside a part of the |
| 854 // current lib. | 870 // current lib. |
| 855 UnlinkedReference reference = unlinkedUnit.references[type.reference]; | 871 UnlinkedReference reference = unlinkedUnit.references[type.reference]; |
| 856 PrelinkedReference referenceResolution = | 872 PrelinkedReference referenceResolution = |
| 857 prelinkedUnit.references[type.reference]; | 873 prelinkedUnit.references[type.reference]; |
| 858 ElementLocationImpl location; | 874 ElementLocationImpl location; |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1076 } | 1092 } |
| 1077 for (PropertyAccessorElementImpl accessor in unit.accessors) { | 1093 for (PropertyAccessorElementImpl accessor in unit.accessors) { |
| 1078 elementMap[accessor.identifier] = accessor; | 1094 elementMap[accessor.identifier] = accessor; |
| 1079 } | 1095 } |
| 1080 resummarizedElements[absoluteUri] = elementMap; | 1096 resummarizedElements[absoluteUri] = elementMap; |
| 1081 unitHolder = null; | 1097 unitHolder = null; |
| 1082 prelinkedUnit = null; | 1098 prelinkedUnit = null; |
| 1083 unlinkedUnit = null; | 1099 unlinkedUnit = null; |
| 1084 } | 1100 } |
| 1085 } | 1101 } |
| OLD | NEW |