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

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

Issue 2650783003: Remove parent from SummaryResynthesizer. (Closed)
Patch Set: Created 3 years, 10 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
OLDNEW
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/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; 10 import 'package:analyzer/dart/ast/standard_ast_factory.dart';
(...skipping 12 matching lines...) Expand all
23 import 'package:analyzer/src/summary/format.dart'; 23 import 'package:analyzer/src/summary/format.dart';
24 import 'package:analyzer/src/summary/idl.dart'; 24 import 'package:analyzer/src/summary/idl.dart';
25 import 'package:analyzer/src/summary/summary_sdk.dart'; 25 import 'package:analyzer/src/summary/summary_sdk.dart';
26 26
27 /** 27 /**
28 * Implementation of [ElementResynthesizer] used when resynthesizing an element 28 * Implementation of [ElementResynthesizer] used when resynthesizing an element
29 * model from summaries. 29 * model from summaries.
30 */ 30 */
31 abstract class SummaryResynthesizer extends ElementResynthesizer { 31 abstract class SummaryResynthesizer extends ElementResynthesizer {
32 /** 32 /**
33 * The parent [SummaryResynthesizer] which is asked to resynthesize elements
34 * and get summaries before this resynthesizer attempts to do this.
35 * Can be `null`.
36 */
37 final SummaryResynthesizer parent;
38
39 /**
40 * Source factory used to convert URIs to [Source] objects. 33 * Source factory used to convert URIs to [Source] objects.
41 */ 34 */
42 final SourceFactory sourceFactory; 35 final SourceFactory sourceFactory;
43 36
44 /** 37 /**
45 * Cache of [Source] objects that have already been converted from URIs. 38 * Cache of [Source] objects that have already been converted from URIs.
46 */ 39 */
47 final Map<String, Source> _sources = <String, Source>{}; 40 final Map<String, Source> _sources = <String, Source>{};
48 41
49 /** 42 /**
(...skipping 24 matching lines...) Expand all
74 <String, Map<String, Map<String, Element>>>{}; 67 <String, Map<String, Map<String, Element>>>{};
75 68
76 /** 69 /**
77 * Map of libraries which have been resynthesized from summaries. The map 70 * Map of libraries which have been resynthesized from summaries. The map
78 * key is the library URI. 71 * key is the library URI.
79 */ 72 */
80 final Map<String, LibraryElement> _resynthesizedLibraries = 73 final Map<String, LibraryElement> _resynthesizedLibraries =
81 <String, LibraryElement>{}; 74 <String, LibraryElement>{};
82 75
83 SummaryResynthesizer( 76 SummaryResynthesizer(
84 this.parent, AnalysisContext context, this.sourceFactory, this.strongMode) 77 AnalysisContext context, this.sourceFactory, this.strongMode)
85 : super(context) { 78 : super(context) {
86 _buildTypeProvider(); 79 _buildTypeProvider();
87 } 80 }
88 81
89 /** 82 /**
90 * Number of libraries that have been resynthesized so far. 83 * Number of libraries that have been resynthesized so far.
91 */ 84 */
92 int get resynthesisCount => _resynthesizedLibraries.length; 85 int get resynthesisCount => _resynthesizedLibraries.length;
93 86
94 /** 87 /**
95 * The [TypeProvider] used to obtain SDK types during resynthesis. 88 * The [TypeProvider] used to obtain SDK types during resynthesis.
96 */ 89 */
97 TypeProvider get typeProvider => _typeProvider; 90 TypeProvider get typeProvider => _typeProvider;
98 91
99 @override 92 @override
100 Element getElement(ElementLocation location) { 93 Element getElement(ElementLocation location) {
101 List<String> components = location.components; 94 List<String> components = location.components;
102 String libraryUri = components[0]; 95 String libraryUri = components[0];
103 // Ask the parent resynthesizer.
104 if (parent != null && parent._hasLibrarySummary(libraryUri)) {
105 return parent.getElement(location);
106 }
107 // Resynthesize locally. 96 // Resynthesize locally.
108 if (components.length == 1) { 97 if (components.length == 1) {
109 return getLibraryElement(libraryUri); 98 return getLibraryElement(libraryUri);
110 } else if (components.length == 2) { 99 } else if (components.length == 2) {
111 Map<String, CompilationUnitElement> libraryMap = 100 Map<String, CompilationUnitElement> libraryMap =
112 _resynthesizedUnits[libraryUri]; 101 _resynthesizedUnits[libraryUri];
113 if (libraryMap == null) { 102 if (libraryMap == null) {
114 getLibraryElement(libraryUri); 103 getLibraryElement(libraryUri);
115 libraryMap = _resynthesizedUnits[libraryUri]; 104 libraryMap = _resynthesizedUnits[libraryUri];
116 assert(libraryMap != null); 105 assert(libraryMap != null);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } else { 182 } else {
194 throw new UnimplementedError(location.toString()); 183 throw new UnimplementedError(location.toString());
195 } 184 }
196 } 185 }
197 186
198 /** 187 /**
199 * Get the [LibraryElement] for the given [uri], resynthesizing it if it 188 * Get the [LibraryElement] for the given [uri], resynthesizing it if it
200 * hasn't been resynthesized already. 189 * hasn't been resynthesized already.
201 */ 190 */
202 LibraryElement getLibraryElement(String uri) { 191 LibraryElement getLibraryElement(String uri) {
203 if (parent != null && parent._hasLibrarySummary(uri)) {
204 return parent.getLibraryElement(uri);
205 }
206 return _resynthesizedLibraries.putIfAbsent(uri, () { 192 return _resynthesizedLibraries.putIfAbsent(uri, () {
207 LinkedLibrary serializedLibrary = _getLinkedSummaryOrNull(uri); 193 LinkedLibrary serializedLibrary = getLinkedSummary(uri);
208 Source librarySource = _getSource(uri); 194 Source librarySource = _getSource(uri);
209 if (serializedLibrary == null) { 195 if (serializedLibrary == null) {
210 LibraryElementImpl libraryElement = 196 LibraryElementImpl libraryElement =
211 new LibraryElementImpl(context, '', -1, 0); 197 new LibraryElementImpl(context, '', -1, 0);
212 libraryElement.isSynthetic = true; 198 libraryElement.isSynthetic = true;
213 CompilationUnitElementImpl unitElement = 199 CompilationUnitElementImpl unitElement =
214 new CompilationUnitElementImpl(librarySource.shortName); 200 new CompilationUnitElementImpl(librarySource.shortName);
215 libraryElement.definingCompilationUnit = unitElement; 201 libraryElement.definingCompilationUnit = unitElement;
216 unitElement.source = librarySource; 202 unitElement.source = librarySource;
217 unitElement.librarySource = librarySource; 203 unitElement.librarySource = librarySource;
218 libraryElement.createLoadLibraryFunction(typeProvider); 204 libraryElement.createLoadLibraryFunction(typeProvider);
219 libraryElement.publicNamespace = new Namespace({}); 205 libraryElement.publicNamespace = new Namespace({});
220 libraryElement.exportNamespace = new Namespace({}); 206 libraryElement.exportNamespace = new Namespace({});
221 return libraryElement; 207 return libraryElement;
222 } 208 }
223 UnlinkedUnit unlinkedSummary = _getUnlinkedSummaryOrNull(uri); 209 UnlinkedUnit unlinkedSummary = getUnlinkedSummary(uri);
224 if (unlinkedSummary == null) { 210 if (unlinkedSummary == null) {
225 throw new StateError('Unable to find unlinked summary: $uri'); 211 throw new StateError('Unable to find unlinked summary: $uri');
226 } 212 }
227 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[unlinkedSummary]; 213 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[unlinkedSummary];
228 for (String part in serializedUnits[0].publicNamespace.parts) { 214 for (String part in serializedUnits[0].publicNamespace.parts) {
229 Source partSource = sourceFactory.resolveUri(librarySource, part); 215 Source partSource = sourceFactory.resolveUri(librarySource, part);
230 if (partSource == null) { 216 if (partSource == null) {
231 serializedUnits.add(null); 217 serializedUnits.add(null);
232 } else { 218 } else {
233 String partAbsUri = partSource.uri.toString(); 219 String partAbsUri = partSource.uri.toString();
234 serializedUnits.add(_getUnlinkedSummaryOrNull(partAbsUri) ?? 220 serializedUnits.add(getUnlinkedSummary(partAbsUri) ??
235 new UnlinkedUnitBuilder(codeRange: new CodeRangeBuilder())); 221 new UnlinkedUnitBuilder(codeRange: new CodeRangeBuilder()));
236 } 222 }
237 } 223 }
238 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( 224 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer(
239 this, serializedLibrary, serializedUnits, librarySource); 225 this, serializedLibrary, serializedUnits, librarySource);
240 LibraryElement library = libraryResynthesizer.buildLibrary(); 226 LibraryElement library = libraryResynthesizer.buildLibrary();
241 _resynthesizedUnits[uri] = libraryResynthesizer.resynthesizedUnits; 227 _resynthesizedUnits[uri] = libraryResynthesizer.resynthesizedUnits;
242 return library; 228 return library;
243 }); 229 });
244 } 230 }
(...skipping 24 matching lines...) Expand all
269 var asyncLibrary = getLibraryElement('dart:async') as LibraryElementImpl; 255 var asyncLibrary = getLibraryElement('dart:async') as LibraryElementImpl;
270 SummaryTypeProvider summaryTypeProvider = new SummaryTypeProvider(); 256 SummaryTypeProvider summaryTypeProvider = new SummaryTypeProvider();
271 summaryTypeProvider.initializeCore(coreLibrary); 257 summaryTypeProvider.initializeCore(coreLibrary);
272 summaryTypeProvider.initializeAsync(asyncLibrary); 258 summaryTypeProvider.initializeAsync(asyncLibrary);
273 coreLibrary.createLoadLibraryFunction(summaryTypeProvider); 259 coreLibrary.createLoadLibraryFunction(summaryTypeProvider);
274 asyncLibrary.createLoadLibraryFunction(summaryTypeProvider); 260 asyncLibrary.createLoadLibraryFunction(summaryTypeProvider);
275 _typeProvider = summaryTypeProvider; 261 _typeProvider = summaryTypeProvider;
276 } 262 }
277 263
278 /** 264 /**
279 * Return the [LinkedLibrary] for the given [uri] or return `null` if it
280 * could not be found.
281 */
282 LinkedLibrary _getLinkedSummaryOrNull(String uri) {
283 if (parent != null && parent._hasLibrarySummary(uri)) {
284 return parent._getLinkedSummaryOrNull(uri);
285 }
286 return getLinkedSummary(uri);
287 }
288
289 /**
290 * Get the [Source] object for the given [uri]. 265 * Get the [Source] object for the given [uri].
291 */ 266 */
292 Source _getSource(String uri) { 267 Source _getSource(String uri) {
293 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri)); 268 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri));
294 } 269 }
295
296 /**
297 * Return the [UnlinkedUnit] for the given [uri] or return `null` if it
298 * could not be found.
299 */
300 UnlinkedUnit _getUnlinkedSummaryOrNull(String uri) {
301 if (parent != null && parent._hasLibrarySummary(uri)) {
302 return parent._getUnlinkedSummaryOrNull(uri);
303 }
304 return getUnlinkedSummary(uri);
305 }
306
307 /**
308 * Return `true` if this resynthesizer can provide summaries of the libraries
309 * with the given [uri].
310 */
311 bool _hasLibrarySummary(String uri) {
312 if (parent != null && parent._hasLibrarySummary(uri)) {
313 return true;
314 }
315 return hasLibrarySummary(uri);
316 }
317 } 270 }
318 271
319 /** 272 /**
320 * Builder of [Expression]s from [UnlinkedExpr]s. 273 * Builder of [Expression]s from [UnlinkedExpr]s.
321 */ 274 */
322 class _ConstExprBuilder { 275 class _ConstExprBuilder {
323 static const ARGUMENT_LIST = 'ARGUMENT_LIST'; 276 static const ARGUMENT_LIST = 'ARGUMENT_LIST';
324 277
325 final _UnitResynthesizer resynthesizer; 278 final _UnitResynthesizer resynthesizer;
326 final ElementImpl context; 279 final ElementImpl context;
(...skipping 1590 matching lines...) Expand 10 before | Expand all | Expand 10 after
1917 static String _getElementIdentifier(String name, ReferenceKind kind) { 1870 static String _getElementIdentifier(String name, ReferenceKind kind) {
1918 if (kind == ReferenceKind.topLevelPropertyAccessor || 1871 if (kind == ReferenceKind.topLevelPropertyAccessor ||
1919 kind == ReferenceKind.propertyAccessor) { 1872 kind == ReferenceKind.propertyAccessor) {
1920 if (!name.endsWith('=')) { 1873 if (!name.endsWith('=')) {
1921 return name + '?'; 1874 return name + '?';
1922 } 1875 }
1923 } 1876 }
1924 return name; 1877 return name;
1925 } 1878 }
1926 } 1879 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/package_bundle_reader.dart ('k') | pkg/analyzer/test/src/summary/resynthesize_ast_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698