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

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

Issue 1585843009: Chaining not just element requests but also summaries access. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Check parent in the abstract implementation. Created 4 years, 11 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 // 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 abstract 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
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) {
109 List<String> components = location.components; 71 List<String> components = location.components;
110 String libraryUri = components[0]; 72 String libraryUri = components[0];
111 // Ask the parent resynthesizer. 73 // Ask the parent resynthesizer.
112 if (parent != null) { 74 if (parent != null && parent._hasLibrarySummary(libraryUri)) {
113 if (parent.hasLibrarySummary(libraryUri)) { 75 return parent.getElement(location);
114 return parent.getElement(location);
115 }
116 } 76 }
117 // Resynthesize locally. 77 // Resynthesize locally.
118 if (components.length == 1) { 78 if (components.length == 1) {
119 return getLibraryElement(libraryUri); 79 return getLibraryElement(libraryUri);
120 } else if (components.length == 3) { 80 } else if (components.length == 3) {
121 Map<String, Map<String, Element>> libraryMap = 81 Map<String, Map<String, Element>> libraryMap =
122 _resynthesizedElements[libraryUri]; 82 _resynthesizedElements[libraryUri];
123 if (libraryMap == null) { 83 if (libraryMap == null) {
124 getLibraryElement(libraryUri); 84 getLibraryElement(libraryUri);
125 libraryMap = _resynthesizedElements[libraryUri]; 85 libraryMap = _resynthesizedElements[libraryUri];
(...skipping 10 matching lines...) Expand all
136 } else { 96 } else {
137 throw new UnimplementedError(location.toString()); 97 throw new UnimplementedError(location.toString());
138 } 98 }
139 } 99 }
140 100
141 /** 101 /**
142 * Get the [LibraryElement] for the given [uri], resynthesizing it if it 102 * Get the [LibraryElement] for the given [uri], resynthesizing it if it
143 * hasn't been resynthesized already. 103 * hasn't been resynthesized already.
144 */ 104 */
145 LibraryElement getLibraryElement(String uri) { 105 LibraryElement getLibraryElement(String uri) {
146 if (parent != null && parent.hasLibrarySummary(uri)) { 106 if (parent != null && parent._hasLibrarySummary(uri)) {
147 return parent.getLibraryElement(uri); 107 return parent.getLibraryElement(uri);
148 } 108 }
149 return _resynthesizedLibraries.putIfAbsent(uri, () { 109 return _resynthesizedLibraries.putIfAbsent(uri, () {
150 LinkedLibrary serializedLibrary = getLinkedSummary(uri); 110 LinkedLibrary serializedLibrary = _getLinkedSummaryOrThrow(uri);
151 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[ 111 List<UnlinkedUnit> serializedUnits = <UnlinkedUnit>[
152 getUnlinkedSummary(uri) 112 _getUnlinkedSummaryOrThrow(uri)
153 ]; 113 ];
154 Source librarySource = _getSource(uri); 114 Source librarySource = _getSource(uri);
155 for (String part in serializedUnits[0].publicNamespace.parts) { 115 for (String part in serializedUnits[0].publicNamespace.parts) {
156 Source partSource = sourceFactory.resolveUri(librarySource, part); 116 Source partSource = sourceFactory.resolveUri(librarySource, part);
157 String partAbsUri = partSource.uri.toString(); 117 String partAbsUri = partSource.uri.toString();
158 serializedUnits.add(getUnlinkedSummary(partAbsUri)); 118 serializedUnits.add(_getUnlinkedSummaryOrThrow(partAbsUri));
159 } 119 }
160 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer( 120 _LibraryResynthesizer libraryResynthesizer = new _LibraryResynthesizer(
161 this, serializedLibrary, serializedUnits, librarySource); 121 this, serializedLibrary, serializedUnits, librarySource);
162 LibraryElement library = libraryResynthesizer.buildLibrary(); 122 LibraryElement library = libraryResynthesizer.buildLibrary();
163 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements; 123 _resynthesizedElements[uri] = libraryResynthesizer.resummarizedElements;
164 return library; 124 return library;
165 }); 125 });
166 } 126 }
167 127
168 /** 128 /**
129 * Return the [LinkedLibrary] for the given [uri] or `null` if it could not
130 * be found. Caller has already checked that `parent.hasLibrarySummary(uri)`
131 * returns `false`.
132 */
133 LinkedLibrary getLinkedSummary(String uri);
134
135 /**
136 * Return the [UnlinkedUnit] for the given [uri] or `null` if it could not
137 * be found. Caller has already checked that `parent.hasLibrarySummary(uri)`
138 * returns `false`.
139 */
140 UnlinkedUnit getUnlinkedSummary(String uri);
141
142 /**
143 * Return `true` if this resynthesizer can provide summaries of the libraries
144 * with the given [uri]. Caller has already checked that
145 * `parent.hasLibrarySummary(uri)` returns `false`.
146 */
147 bool hasLibrarySummary(String uri);
148
149 /**
150 * Return the [LinkedLibrary] for the given [uri] or throw [StateError] if it
151 * could not be found.
152 */
153 LinkedLibrary _getLinkedSummaryOrThrow(String uri) {
154 if (parent != null && parent._hasLibrarySummary(uri)) {
155 return parent._getLinkedSummaryOrThrow(uri);
156 }
157 LinkedLibrary summary = getLinkedSummary(uri);
158 if (summary != null) {
159 return summary;
160 }
161 throw new StateError('Unable to find linked summary: $uri');
162 }
163
164 /**
169 * Get the [Source] object for the given [uri]. 165 * Get the [Source] object for the given [uri].
170 */ 166 */
171 Source _getSource(String uri) { 167 Source _getSource(String uri) {
172 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri)); 168 return _sources.putIfAbsent(uri, () => sourceFactory.forUri(uri));
173 } 169 }
170
171 /**
172 * Return the [UnlinkedUnit] for the given [uri] or throw [StateError] if it
173 * could not be found.
174 */
175 UnlinkedUnit _getUnlinkedSummaryOrThrow(String uri) {
176 if (parent != null && parent._hasLibrarySummary(uri)) {
177 return parent._getUnlinkedSummaryOrThrow(uri);
178 }
179 UnlinkedUnit summary = getUnlinkedSummary(uri);
180 if (summary != null) {
181 return summary;
182 }
183 throw new StateError('Unable to find unlinked summary: $uri');
184 }
185
186 /**
187 * Return `true` if this resynthesizer can provide summaries of the libraries
188 * with the given [uri].
189 */
190 bool _hasLibrarySummary(String uri) {
191 if (parent != null && parent._hasLibrarySummary(uri)) {
192 return true;
193 }
194 return hasLibrarySummary(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
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
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 }
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