Chromium Code Reviews| 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'; |
| 11 import 'package:analyzer/src/generated/element_handle.dart'; | 11 import 'package:analyzer/src/generated/element_handle.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/resolver.dart'; | 13 import 'package:analyzer/src/generated/resolver.dart'; |
| 14 import 'package:analyzer/src/generated/source_io.dart'; | 14 import 'package:analyzer/src/generated/source_io.dart'; |
| 15 import 'package:analyzer/src/summary/format.dart'; | 15 import 'package:analyzer/src/summary/format.dart'; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Callback used by [SummaryResynthesizer] to obtain the linked summary for | |
| 19 * a given URI. | |
| 20 */ | |
| 21 typedef LinkedLibrary GetLinkedSummaryCallback(String uri); | |
| 22 | |
| 23 /** | |
| 24 * Callback used by [SummaryResynthesizer] to obtain the unlinked summary for a | |
| 25 * given URI. | |
| 26 */ | |
| 27 typedef UnlinkedUnit GetUnlinkedSummaryCallback(String uri); | |
| 28 | |
| 29 /** | |
| 30 * Callback used by [SummaryResynthesizer] to check whether it can access | |
| 31 * summaries of the library with the given [uri]. | |
| 32 */ | |
| 33 typedef bool HasLibrarySummaryCallback(String uri); | |
| 34 | |
| 35 /** | |
| 36 * Implementation of [ElementResynthesizer] used when resynthesizing an element | 18 * Implementation of [ElementResynthesizer] used when resynthesizing an element |
| 37 * model from summaries. | 19 * model from summaries. |
| 38 */ | 20 */ |
| 39 class SummaryResynthesizer extends ElementResynthesizer { | 21 class SummaryResynthesizer extends ElementResynthesizer { |
| 40 /** | 22 /** |
| 41 * The parent [SummaryResynthesizer] which is asked to resynthesis elements | 23 * The parent [SummaryResynthesizer] which is asked to resynthesize elements |
| 42 * before this resynthesizer attempts to do this. Can be `null`. | 24 * and get summaries before this resynthesizer attempts to do this. |
| 25 * Can be `null`. | |
| 43 */ | 26 */ |
| 44 final SummaryResynthesizer parent; | 27 final SummaryResynthesizer parent; |
| 45 | 28 |
| 46 /** | 29 /** |
| 47 * Callback used to check whether summaries for a given URI can be accessed. | |
| 48 */ | |
| 49 final HasLibrarySummaryCallback hasLibrarySummary; | |
| 50 | |
| 51 /** | |
| 52 * Callback used to obtain the linked summary for a given URI. | |
| 53 */ | |
| 54 final GetLinkedSummaryCallback getLinkedSummary; | |
| 55 | |
| 56 /** | |
| 57 * Callback used to obtain the unlinked summary for a given URI. | |
| 58 */ | |
| 59 final GetUnlinkedSummaryCallback getUnlinkedSummary; | |
| 60 | |
| 61 /** | |
| 62 * Source factory used to convert URIs to [Source] objects. | 30 * Source factory used to convert URIs to [Source] objects. |
| 63 */ | 31 */ |
| 64 final SourceFactory sourceFactory; | 32 final SourceFactory sourceFactory; |
| 65 | 33 |
| 66 /** | 34 /** |
| 67 * Cache of [Source] objects that have already been converted from URIs. | 35 * Cache of [Source] objects that have already been converted from URIs. |
| 68 */ | 36 */ |
| 69 final Map<String, Source> _sources = <String, Source>{}; | 37 final Map<String, Source> _sources = <String, Source>{}; |
| 70 | 38 |
| 71 /** | 39 /** |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 82 final Map<String, Map<String, Map<String, Element>>> _resynthesizedElements = | 50 final Map<String, Map<String, Map<String, Element>>> _resynthesizedElements = |
| 83 <String, Map<String, Map<String, Element>>>{}; | 51 <String, Map<String, Map<String, Element>>>{}; |
| 84 | 52 |
| 85 /** | 53 /** |
| 86 * Map of libraries which have been resynthesized from summaries. The map | 54 * Map of libraries which have been resynthesized from summaries. The map |
| 87 * key is the library URI. | 55 * key is the library URI. |
| 88 */ | 56 */ |
| 89 final Map<String, LibraryElement> _resynthesizedLibraries = | 57 final Map<String, LibraryElement> _resynthesizedLibraries = |
| 90 <String, LibraryElement>{}; | 58 <String, LibraryElement>{}; |
| 91 | 59 |
| 92 SummaryResynthesizer( | 60 SummaryResynthesizer(this.parent, AnalysisContext context, this.typeProvider, |
| 93 this.parent, | |
| 94 AnalysisContext context, | |
| 95 this.typeProvider, | |
| 96 this.hasLibrarySummary, | |
| 97 this.getLinkedSummary, | |
| 98 this.getUnlinkedSummary, | |
| 99 this.sourceFactory) | 61 this.sourceFactory) |
| 100 : super(context); | 62 : super(context); |
| 101 | 63 |
| 102 /** | 64 /** |
| 103 * Number of libraries that have been resynthesized so far. | 65 * Number of libraries that have been resynthesized so far. |
| 104 */ | 66 */ |
| 105 int get resynthesisCount => _resynthesizedLibraries.length; | 67 int get resynthesisCount => _resynthesizedLibraries.length; |
| 106 | 68 |
| 107 @override | 69 @override |
| 108 Element getElement(ElementLocation location) { | 70 Element getElement(ElementLocation location) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 | 102 |
| 141 /** | 103 /** |
| 142 * Get the [LibraryElement] for the given [uri], resynthesizing it if it | 104 * Get the [LibraryElement] for the given [uri], resynthesizing it if it |
| 143 * hasn't been resynthesized already. | 105 * hasn't been resynthesized already. |
| 144 */ | 106 */ |
| 145 LibraryElement getLibraryElement(String uri) { | 107 LibraryElement getLibraryElement(String uri) { |
| 146 if (parent != null && parent.hasLibrarySummary(uri)) { | 108 if (parent != null && parent.hasLibrarySummary(uri)) { |
| 147 return parent.getLibraryElement(uri); | 109 return parent.getLibraryElement(uri); |
| 148 } | 110 } |
| 149 return _resynthesizedLibraries.putIfAbsent(uri, () { | 111 return _resynthesizedLibraries.putIfAbsent(uri, () { |
| 150 LinkedLibrary serializedLibrary = getLinkedSummary(uri); | 112 LinkedLibrary serializedLibrary = _getLinkedSummaryOrThrow(uri); |
| 151 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[ | 113 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[ |
| 152 getUnlinkedSummary(uri) | 114 _getUnlinkedSummaryOrThrow(uri) |
| 153 ]; | 115 ]; |
| 154 Source librarySource = _getSource(uri); | 116 Source librarySource = _getSource(uri); |
| 155 for (String part in serializedUnits[0].publicNamespace.parts) { | 117 for (String part in serializedUnits[0].publicNamespace.parts) { |
| 156 Source partSource = sourceFactory.resolveUri(librarySource, part); | 118 Source partSource = sourceFactory.resolveUri(librarySource, part); |
| 157 String partAbsUri = partSource.uri.toString(); | 119 String partAbsUri = partSource.uri.toString(); |
| 158 serializedUnits.add(getUnlinkedSummary(partAbsUri)); | 120 serializedUnits.add(_getUnlinkedSummaryOrThrow(partAbsUri)); |
| 159 } | 121 } |
| 160 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( | 122 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( |
| 161 this, serializedLibrary, serializedUnits, librarySource); | 123 this, serializedLibrary, serializedUnits, librarySource); |
| 162 LibraryElement library = libraryResynthesizer.buildLibrary(); | 124 LibraryElement library = libraryResynthesizer.buildLibrary(); |
| 163 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements; | 125 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements; |
| 164 return library; | 126 return library; |
| 165 }); | 127 }); |
| 166 } | 128 } |
| 167 | 129 |
| 168 /** | 130 /** |
| 131 * Return the [LinkedLibrary] for the given [uri] or `null` if it could not | |
| 132 * be found. | |
| 133 */ | |
| 134 LinkedLibrary getLinkedSummary(String uri) { | |
|
Paul Berry
2016/01/15 16:37:38
I'm concerned about the interface contract implied
scheglov
2016/01/15 16:54:50
Done.
| |
| 135 if (parent != null) { | |
| 136 LinkedLibrary summary = parent.getLinkedSummary(uri); | |
| 137 if (summary != null) { | |
| 138 return summary; | |
| 139 } | |
| 140 } | |
| 141 return null; | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * Return the [UnlinkedUnit] for the given [uri] or `null` if it could not | |
| 146 * be found. | |
| 147 */ | |
| 148 UnlinkedUnit getUnlinkedSummary(String uri) { | |
| 149 if (parent != null) { | |
| 150 UnlinkedUnit summary = parent.getUnlinkedSummary(uri); | |
| 151 if (summary != null) { | |
| 152 return summary; | |
| 153 } | |
| 154 } | |
| 155 return null; | |
| 156 } | |
| 157 | |
| 158 /** | |
| 159 * Return `true` if this resynthesizer get provide summaries of the libraries | |
|
Paul Berry
2016/01/15 16:37:38
s/get/can/
| |
| 160 * with the given [uri]. | |
| 161 */ | |
| 162 bool hasLibrarySummary(String uri) { | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Return the [LinkedLibrary] for the given [uri] or throw [StateError] if it | |
| 168 * could not be found. | |
| 169 */ | |
| 170 LinkedLibrary _getLinkedSummaryOrThrow(String uri) { | |
| 171 LinkedLibrary summary = getLinkedSummary(uri); | |
| 172 if (summary != null) { | |
| 173 return summary; | |
| 174 } | |
| 175 throw new StateError('Unable to find linked summary: $uri'); | |
| 176 } | |
| 177 | |
| 178 /** | |
| 169 * Get the [Source] object for the given [uri]. | 179 * Get the [Source] object for the given [uri]. |
| 170 */ | 180 */ |
| 171 Source _getSource(String uri) { | 181 Source _getSource(String uri) { |
| 172 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri)); | 182 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri)); |
| 173 } | 183 } |
| 184 | |
| 185 /** | |
| 186 * Return the [UnlinkedUnit] for the given [uri] or throw [StateError] if it | |
| 187 * could not be found. | |
| 188 */ | |
| 189 UnlinkedUnit _getUnlinkedSummaryOrThrow(String uri) { | |
| 190 UnlinkedUnit summary = getUnlinkedSummary(uri); | |
| 191 if (summary != null) { | |
| 192 return summary; | |
| 193 } | |
| 194 throw new StateError('Unable to find unlinked summary: $uri'); | |
| 195 } | |
| 174 } | 196 } |
| 175 | 197 |
| 176 /** | 198 /** |
| 177 * An instance of [_LibraryResynthesizer] is responsible for resynthesizing the | 199 * An instance of [_LibraryResynthesizer] is responsible for resynthesizing the |
| 178 * elements in a single library from that library's summary. | 200 * elements in a single library from that library's summary. |
| 179 */ | 201 */ |
| 180 class _LibraryResynthesizer { | 202 class _LibraryResynthesizer { |
| 181 /** | 203 /** |
| 182 * The [SummaryResynthesizer] which is being used to obtain summaries. | 204 * The [SummaryResynthesizer] which is being used to obtain summaries. |
| 183 */ | 205 */ |
| (...skipping 863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1047 LinkedDependency dependency, int unit, String name) { | 1069 LinkedDependency dependency, int unit, String name) { |
| 1048 Source referencedLibrarySource = summaryResynthesizer.sourceFactory | 1070 Source referencedLibrarySource = summaryResynthesizer.sourceFactory |
| 1049 .resolveUri(librarySource, dependency.uri); | 1071 .resolveUri(librarySource, dependency.uri); |
| 1050 String referencedLibraryUri = referencedLibrarySource.uri.toString(); | 1072 String referencedLibraryUri = referencedLibrarySource.uri.toString(); |
| 1051 // TODO(paulberry): consider changing Location format so that this is | 1073 // TODO(paulberry): consider changing Location format so that this is |
| 1052 // not necessary (2nd string in location should just be the unit | 1074 // not necessary (2nd string in location should just be the unit |
| 1053 // number). | 1075 // number). |
| 1054 String partUri; | 1076 String partUri; |
| 1055 if (unit != 0) { | 1077 if (unit != 0) { |
| 1056 UnlinkedUnit referencedLibraryDefiningUnit = | 1078 UnlinkedUnit referencedLibraryDefiningUnit = |
| 1057 summaryResynthesizer.getUnlinkedSummary(referencedLibraryUri); | 1079 summaryResynthesizer._getUnlinkedSummaryOrThrow(referencedLibraryUri); |
| 1058 String uri = | 1080 String uri = |
| 1059 referencedLibraryDefiningUnit.publicNamespace.parts[unit - 1]; | 1081 referencedLibraryDefiningUnit.publicNamespace.parts[unit - 1]; |
| 1060 Source partSource = summaryResynthesizer.sourceFactory | 1082 Source partSource = summaryResynthesizer.sourceFactory |
| 1061 .resolveUri(referencedLibrarySource, uri); | 1083 .resolveUri(referencedLibrarySource, uri); |
| 1062 partUri = partSource.uri.toString(); | 1084 partUri = partSource.uri.toString(); |
| 1063 } else { | 1085 } else { |
| 1064 partUri = referencedLibraryUri; | 1086 partUri = referencedLibraryUri; |
| 1065 } | 1087 } |
| 1066 return new ElementLocationImpl.con3( | 1088 return new ElementLocationImpl.con3( |
| 1067 <String>[referencedLibraryUri, partUri, name]); | 1089 <String>[referencedLibraryUri, partUri, name]); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1108 } | 1130 } |
| 1109 for (PropertyAccessorElementImpl accessor in unit.accessors) { | 1131 for (PropertyAccessorElementImpl accessor in unit.accessors) { |
| 1110 elementMap[accessor.identifier] = accessor; | 1132 elementMap[accessor.identifier] = accessor; |
| 1111 } | 1133 } |
| 1112 resummarizedElements[absoluteUri] = elementMap; | 1134 resummarizedElements[absoluteUri] = elementMap; |
| 1113 unitHolder = null; | 1135 unitHolder = null; |
| 1114 linkedUnit = null; | 1136 linkedUnit = null; |
| 1115 unlinkedUnit = null; | 1137 unlinkedUnit = null; |
| 1116 } | 1138 } |
| 1117 } | 1139 } |
| OLD | NEW |