Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: pkg/analyzer/lib/src/summary/package_bundle_reader.dart

Issue 2035833002: Use ResynthesizerResultProvider for both SDK and packages. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: tweak Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/summary/summary_sdk.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import 'dart:io' as io; 1 import 'dart:io' as io;
2 2
3 import 'package:analyzer/dart/element/element.dart'; 3 import 'package:analyzer/dart/element/element.dart';
4 import 'package:analyzer/src/context/cache.dart'; 4 import 'package:analyzer/src/context/cache.dart';
5 import 'package:analyzer/src/context/context.dart'; 5 import 'package:analyzer/src/context/context.dart';
6 import 'package:analyzer/src/dart/element/element.dart'; 6 import 'package:analyzer/src/dart/element/element.dart';
7 import 'package:analyzer/src/generated/engine.dart'; 7 import 'package:analyzer/src/generated/engine.dart';
8 import 'package:analyzer/src/generated/java_io.dart'; 8 import 'package:analyzer/src/generated/java_io.dart';
9 import 'package:analyzer/src/generated/resolver.dart'; 9 import 'package:analyzer/src/generated/resolver.dart';
10 import 'package:analyzer/src/generated/source.dart'; 10 import 'package:analyzer/src/generated/source.dart';
11 import 'package:analyzer/src/generated/source_io.dart'; 11 import 'package:analyzer/src/generated/source_io.dart';
12 import 'package:analyzer/src/summary/idl.dart'; 12 import 'package:analyzer/src/summary/idl.dart';
13 import 'package:analyzer/src/summary/resynthesize.dart'; 13 import 'package:analyzer/src/summary/resynthesize.dart';
14 import 'package:analyzer/src/summary/summary_sdk.dart'; 14 import 'package:analyzer/src/summary/summary_sdk.dart';
15 import 'package:analyzer/src/task/dart.dart'; 15 import 'package:analyzer/src/task/dart.dart';
16 import 'package:analyzer/task/dart.dart'; 16 import 'package:analyzer/task/dart.dart';
17 import 'package:analyzer/task/model.dart'; 17 import 'package:analyzer/task/model.dart';
18 import 'package:path/path.dart' as pathos; 18 import 'package:path/path.dart' as pathos;
19 19
20 /** 20 /**
21 * The [ResultProvider] that provides results from input package summaries. 21 * The [ResultProvider] that provides results from input package summaries.
22 */ 22 */
23 class InputPackagesResultProvider extends ResultProvider { 23 class InputPackagesResultProvider extends ResynthesizerResultProvider {
24 final InternalAnalysisContext _context; 24 InputPackagesResultProvider(
25 25 InternalAnalysisContext context, SummaryDataStore dataStore)
26 _FileBasedSummaryResynthesizer _resynthesizer; 26 : super(context, dataStore) {
27 SummaryResultProvider _sdkProvider; 27 AnalysisContext sdkContext = context.sourceFactory.dartSdk.context;
28 28 createResynthesizer(sdkContext, sdkContext.typeProvider);
29 InputPackagesResultProvider(this._context, SummaryDataStore dataStore) {
30 InternalAnalysisContext sdkContext = _context.sourceFactory.dartSdk.context;
31 _sdkProvider = sdkContext.resultProvider;
32 // Set the type provider to prevent the context from computing it.
33 _context.typeProvider = sdkContext.typeProvider;
34 // Create a chained resynthesizer.
35 _resynthesizer = new _FileBasedSummaryResynthesizer(
36 _sdkProvider.resynthesizer,
37 _context,
38 _context.typeProvider,
39 _context.sourceFactory,
40 _context.analysisOptions.strongMode,
41 dataStore);
42 } 29 }
43 30
44 @override 31 @override
45 bool compute(CacheEntry entry, ResultDescriptor result) { 32 bool hasResultsForSource(Source source) {
46 if (_sdkProvider.compute(entry, result)) { 33 String uriString = source.uri.toString();
47 return true; 34 return resynthesizer.hasLibrarySummary(uriString);
48 }
49 AnalysisTarget target = entry.target;
50 // Only library results are supported for now.
51 if (target is Source) {
52 Uri uri = target.uri;
53 // We know how to server results to input packages.
54 String uriString = uri.toString();
55 if (!_resynthesizer.hasLibrarySummary(uriString)) {
56 return false;
57 }
58 // Provide known results.
59 if (result == LIBRARY_ELEMENT1 ||
60 result == LIBRARY_ELEMENT2 ||
61 result == LIBRARY_ELEMENT3 ||
62 result == LIBRARY_ELEMENT4 ||
63 result == LIBRARY_ELEMENT5 ||
64 result == LIBRARY_ELEMENT6 ||
65 result == LIBRARY_ELEMENT7 ||
66 result == LIBRARY_ELEMENT8 ||
67 result == LIBRARY_ELEMENT9 ||
68 result == LIBRARY_ELEMENT ||
69 false) {
70 LibraryElement libraryElement =
71 _resynthesizer.getLibraryElement(uriString);
72 entry.setValue(result, libraryElement, TargetedResult.EMPTY_LIST);
73 return true;
74 } else if (result == READY_LIBRARY_ELEMENT2 ||
75 result == READY_LIBRARY_ELEMENT6 ||
76 result == READY_LIBRARY_ELEMENT7) {
77 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
78 return true;
79 } else if (result == SOURCE_KIND) {
80 if (_resynthesizer._dataStore.linkedMap.containsKey(uriString)) {
81 entry.setValue(result, SourceKind.LIBRARY, TargetedResult.EMPTY_LIST);
82 return true;
83 }
84 if (_resynthesizer._dataStore.unlinkedMap.containsKey(uriString)) {
85 entry.setValue(result, SourceKind.PART, TargetedResult.EMPTY_LIST);
86 return true;
87 }
88 return false;
89 }
90 } else if (target is LibrarySpecificUnit) {
91 String uriString = target.library.uri.toString();
92 if (!_resynthesizer.hasLibrarySummary(uriString)) {
93 return false;
94 }
95 if (result == CREATED_RESOLVED_UNIT1 ||
96 result == CREATED_RESOLVED_UNIT2 ||
97 result == CREATED_RESOLVED_UNIT3 ||
98 result == CREATED_RESOLVED_UNIT4 ||
99 result == CREATED_RESOLVED_UNIT5 ||
100 result == CREATED_RESOLVED_UNIT6 ||
101 result == CREATED_RESOLVED_UNIT7 ||
102 result == CREATED_RESOLVED_UNIT8 ||
103 result == CREATED_RESOLVED_UNIT9 ||
104 result == CREATED_RESOLVED_UNIT10 ||
105 result == CREATED_RESOLVED_UNIT11 ||
106 result == CREATED_RESOLVED_UNIT12) {
107 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
108 return true;
109 }
110 if (result == COMPILATION_UNIT_ELEMENT) {
111 String libraryUri = target.library.uri.toString();
112 String unitUri = target.unit.uri.toString();
113 CompilationUnitElement unit = _resynthesizer.getElement(
114 new ElementLocationImpl.con3(<String>[libraryUri, unitUri]));
115 if (unit != null) {
116 entry.setValue(result, unit, TargetedResult.EMPTY_LIST);
117 return true;
118 }
119 }
120 } else if (target is VariableElement) {
121 if (!_resynthesizer
122 .hasLibrarySummary(target.library.source.uri.toString())) {
123 return false;
124 }
125 if (result == PROPAGATED_VARIABLE || result == INFERRED_STATIC_VARIABLE) {
126 entry.setValue(result, target, TargetedResult.EMPTY_LIST);
127 return true;
128 }
129 }
130 return false;
131 } 35 }
132 } 36 }
133 37
134 /** 38 /**
135 * The [UriResolver] that knows about sources that are served from their 39 * The [UriResolver] that knows about sources that are served from their
136 * summaries. 40 * summaries.
137 * 41 *
138 * TODO(scheglov) rename to `InSummaryUriResolver` - it's not `package:` specifi c. 42 * TODO(scheglov) rename to `InSummaryUriResolver` - it's not `package:` specifi c.
139 */ 43 */
140 class InSummaryPackageUriResolver extends UriResolver { 44 class InSummaryPackageUriResolver extends UriResolver {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 object is InSummarySource && object.uri == uri; 108 object is InSummarySource && object.uri == uri;
205 109
206 @override 110 @override
207 bool exists() => true; 111 bool exists() => true;
208 112
209 @override 113 @override
210 String toString() => uri.toString(); 114 String toString() => uri.toString();
211 } 115 }
212 116
213 /** 117 /**
118 * The [ResultProvider] that provides results using summary resynthesizer.
119 */
120 abstract class ResynthesizerResultProvider extends ResultProvider {
121 final InternalAnalysisContext _context;
122 final SummaryDataStore _dataStore;
123
124 _FileBasedSummaryResynthesizer _resynthesizer;
125 SummaryResultProvider _sdkProvider;
126
127 ResynthesizerResultProvider(this._context, this._dataStore);
128
129 SummaryResynthesizer get resynthesizer => _resynthesizer;
130
131 /**
132 * Add a new [bundle] to the resynthesizer.
133 */
134 void addBundle(String path, PackageBundle bundle) {
135 _dataStore.addBundle(path, bundle);
136 }
137
138 @override
139 bool compute(CacheEntry entry, ResultDescriptor result) {
140 if (_sdkProvider != null && _sdkProvider.compute(entry, result)) {
141 return true;
142 }
143 AnalysisTarget target = entry.target;
144 // Check whether there are results for the source.
145 if (!hasResultsForSource(target.source)) {
146 return false;
147 }
148 // Constant expressions are always resolved in summaries.
149 if (result == CONSTANT_EXPRESSION_RESOLVED &&
150 target is ConstantEvaluationTarget) {
151 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
152 return true;
153 }
154 // Provide results for Source.
155 if (target is Source) {
156 String uriString = target.uri.toString();
157 // Provide known results.
158 if (result == LIBRARY_ELEMENT1 ||
159 result == LIBRARY_ELEMENT2 ||
160 result == LIBRARY_ELEMENT3 ||
161 result == LIBRARY_ELEMENT4 ||
162 result == LIBRARY_ELEMENT5 ||
163 result == LIBRARY_ELEMENT6 ||
164 result == LIBRARY_ELEMENT7 ||
165 result == LIBRARY_ELEMENT8 ||
166 result == LIBRARY_ELEMENT9 ||
167 result == LIBRARY_ELEMENT) {
168 LibraryElement libraryElement =
169 resynthesizer.getLibraryElement(uriString);
170 entry.setValue(result, libraryElement, TargetedResult.EMPTY_LIST);
171 return true;
172 } else if (result == READY_LIBRARY_ELEMENT2 ||
173 result == READY_LIBRARY_ELEMENT6 ||
174 result == READY_LIBRARY_ELEMENT7) {
175 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
176 return true;
177 } else if (result == SOURCE_KIND) {
178 if (_dataStore.linkedMap.containsKey(uriString)) {
179 entry.setValue(result, SourceKind.LIBRARY, TargetedResult.EMPTY_LIST);
180 return true;
181 }
182 if (_dataStore.unlinkedMap.containsKey(uriString)) {
183 entry.setValue(result, SourceKind.PART, TargetedResult.EMPTY_LIST);
184 return true;
185 }
186 return false;
187 }
188 } else if (target is LibrarySpecificUnit) {
189 if (!hasResultsForSource(target.library)) {
190 return false;
191 }
192 if (result == CREATED_RESOLVED_UNIT1 ||
193 result == CREATED_RESOLVED_UNIT2 ||
194 result == CREATED_RESOLVED_UNIT3 ||
195 result == CREATED_RESOLVED_UNIT4 ||
196 result == CREATED_RESOLVED_UNIT5 ||
197 result == CREATED_RESOLVED_UNIT6 ||
198 result == CREATED_RESOLVED_UNIT7 ||
199 result == CREATED_RESOLVED_UNIT8 ||
200 result == CREATED_RESOLVED_UNIT9 ||
201 result == CREATED_RESOLVED_UNIT10 ||
202 result == CREATED_RESOLVED_UNIT11 ||
203 result == CREATED_RESOLVED_UNIT12) {
204 entry.setValue(result, true, TargetedResult.EMPTY_LIST);
205 return true;
206 }
207 if (result == COMPILATION_UNIT_ELEMENT) {
208 String libraryUri = target.library.uri.toString();
209 String unitUri = target.unit.uri.toString();
210 CompilationUnitElement unit = resynthesizer.getElement(
211 new ElementLocationImpl.con3(<String>[libraryUri, unitUri]));
212 if (unit != null) {
213 entry.setValue(result, unit, TargetedResult.EMPTY_LIST);
214 return true;
215 }
216 }
217 } else if (target is VariableElement) {
218 if (!hasResultsForSource(target.library.source)) {
219 return false;
220 }
221 if (result == PROPAGATED_VARIABLE || result == INFERRED_STATIC_VARIABLE) {
222 entry.setValue(result, target, TargetedResult.EMPTY_LIST);
223 return true;
224 }
225 }
226 // Unknown target.
227 return false;
228 }
229
230 /**
231 * Create the [resynthesizer] instance.
232 *
233 * Subclasses must call this method in their constructors.
234 */
235 void createResynthesizer(
236 InternalAnalysisContext sdkContext, TypeProvider typeProvider) {
237 // Set the type provider to prevent the context from computing it.
238 _context.typeProvider = typeProvider;
239 // Create a chained resynthesizer.
240 _sdkProvider = sdkContext?.resultProvider;
241 _resynthesizer = new _FileBasedSummaryResynthesizer(
242 _sdkProvider?.resynthesizer,
243 _context,
244 typeProvider,
245 _context.sourceFactory,
246 _context.analysisOptions.strongMode,
247 _dataStore);
248 }
249
250 /**
251 * Return `true` if this result provider can provide a result for the
252 * given [source]. The provider must ensure that [addBundle] is invoked for
253 * every bundle that would be required to provide results for the [source].
254 */
255 bool hasResultsForSource(Source source);
256 }
257
258 /**
214 * A [SummaryDataStore] is a container for the data extracted from a set of 259 * A [SummaryDataStore] is a container for the data extracted from a set of
215 * summary package bundles. It contains maps which can be used to find linked 260 * summary package bundles. It contains maps which can be used to find linked
216 * and unlinked summaries by URI. 261 * and unlinked summaries by URI.
217 */ 262 */
218 class SummaryDataStore { 263 class SummaryDataStore {
219 /** 264 /**
220 * List of all [PackageBundle]s. 265 * List of all [PackageBundle]s.
221 */ 266 */
222 final List<PackageBundle> bundles = <PackageBundle>[]; 267 final List<PackageBundle> bundles = <PackageBundle>[];
223 268
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 * also provides [summaryPath]. 348 * also provides [summaryPath].
304 */ 349 */
305 class _InSummaryFallbackSource extends FileBasedSource 350 class _InSummaryFallbackSource extends FileBasedSource
306 implements InSummarySource { 351 implements InSummarySource {
307 @override 352 @override
308 final String summaryPath; 353 final String summaryPath;
309 354
310 _InSummaryFallbackSource(JavaFile file, Uri uri, this.summaryPath) 355 _InSummaryFallbackSource(JavaFile file, Uri uri, this.summaryPath)
311 : super(file, uri); 356 : super(file, uri);
312 } 357 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/summary/summary_sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698