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 serialization.elements; | 5 library serialization.elements; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
11 import 'package:analyzer/dart/element/type.dart'; | 11 import 'package:analyzer/dart/element/type.dart'; |
12 import 'package:analyzer/src/dart/element/element.dart'; | 12 import 'package:analyzer/src/dart/element/element.dart'; |
13 import 'package:analyzer/src/dart/element/member.dart'; | 13 import 'package:analyzer/src/dart/element/member.dart'; |
14 import 'package:analyzer/src/dart/element/type.dart'; | 14 import 'package:analyzer/src/dart/element/type.dart'; |
15 import 'package:analyzer/src/generated/java_engine.dart'; | |
16 import 'package:analyzer/src/generated/resolver.dart'; | 15 import 'package:analyzer/src/generated/resolver.dart'; |
17 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
18 import 'package:analyzer/src/generated/utilities_dart.dart'; | 17 import 'package:analyzer/src/generated/utilities_dart.dart'; |
19 import 'package:analyzer/src/summary/api_signature.dart'; | 18 import 'package:analyzer/src/summary/api_signature.dart'; |
20 import 'package:analyzer/src/summary/format.dart'; | 19 import 'package:analyzer/src/summary/format.dart'; |
21 import 'package:analyzer/src/summary/idl.dart'; | 20 import 'package:analyzer/src/summary/idl.dart'; |
22 import 'package:analyzer/src/summary/name_filter.dart'; | 21 import 'package:analyzer/src/summary/name_filter.dart'; |
22 import 'package:analyzer/src/summary/package_bundle_reader.dart'; | |
23 import 'package:analyzer/src/summary/summarize_const_expr.dart'; | 23 import 'package:analyzer/src/summary/summarize_const_expr.dart'; |
24 import 'package:convert/convert.dart'; | 24 import 'package:convert/convert.dart'; |
25 import 'package:crypto/crypto.dart'; | 25 import 'package:crypto/crypto.dart'; |
26 import 'package:path/path.dart' as path; | 26 import 'package:path/path.dart' as path; |
27 import 'package:analyzer/src/summary/package_bundle_reader.dart'; | |
28 | 27 |
29 /** | 28 /** |
30 * Serialize all the elements in [lib] to a summary using [ctx] as the context | 29 * Serialize all the elements in [lib] to a summary using [ctx] as the context |
31 * for building the summary, and using [typeProvider] to find built-in types. | 30 * for building the summary, and using [typeProvider] to find built-in types. |
32 */ | 31 */ |
33 LibrarySerializationResult serializeLibrary( | 32 LibrarySerializationResult serializeLibrary( |
34 LibraryElement lib, TypeProvider typeProvider, bool strongMode) { | 33 LibraryElement lib, TypeProvider typeProvider, bool strongMode) { |
35 _LibrarySerializer serializer = | 34 _LibrarySerializer serializer = |
36 new _LibrarySerializer(lib, typeProvider, strongMode); | 35 new _LibrarySerializer(lib, typeProvider, strongMode); |
37 LinkedLibraryBuilder linked = serializer.serializeLibrary(); | 36 LinkedLibraryBuilder linked = serializer.serializeLibrary(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 * format that clients might need to be aware of (such as adding a kind of | 132 * format that clients might need to be aware of (such as adding a kind of |
134 * data that was previously not summarized), this value should be incremented | 133 * data that was previously not summarized), this value should be incremented |
135 * by 1. | 134 * by 1. |
136 */ | 135 */ |
137 static const int currentMinorVersion = 0; | 136 static const int currentMinorVersion = 0; |
138 | 137 |
139 final List<String> _linkedLibraryUris = <String>[]; | 138 final List<String> _linkedLibraryUris = <String>[]; |
140 final List<LinkedLibraryBuilder> _linkedLibraries = <LinkedLibraryBuilder>[]; | 139 final List<LinkedLibraryBuilder> _linkedLibraries = <LinkedLibraryBuilder>[]; |
141 final List<String> _unlinkedUnitUris = <String>[]; | 140 final List<String> _unlinkedUnitUris = <String>[]; |
142 final List<UnlinkedUnitBuilder> _unlinkedUnits = <UnlinkedUnitBuilder>[]; | 141 final List<UnlinkedUnitBuilder> _unlinkedUnits = <UnlinkedUnitBuilder>[]; |
142 final Map<String, UnlinkedUnitBuilder> _unlinkedUnitMap = | |
143 <String, UnlinkedUnitBuilder>{}; | |
143 final List<String> _unlinkedUnitHashes; | 144 final List<String> _unlinkedUnitHashes; |
144 final List<PackageDependencyInfoBuilder> _dependencies = | 145 final List<PackageDependencyInfoBuilder> _dependencies = |
145 <PackageDependencyInfoBuilder>[]; | 146 <PackageDependencyInfoBuilder>[]; |
146 final bool _excludeHashes; | 147 final bool _excludeHashes; |
147 | 148 |
148 /** | 149 /** |
149 * Create a [PackageBundleAssembler]. If [excludeHashes] is `true`, hash | 150 * Create a [PackageBundleAssembler]. If [excludeHashes] is `true`, hash |
150 * computation will be skipped. | 151 * computation will be skipped. |
151 */ | 152 */ |
152 PackageBundleAssembler({bool excludeHashes: false}) | 153 PackageBundleAssembler({bool excludeHashes: false}) |
153 : _excludeHashes = excludeHashes, | 154 : _excludeHashes = excludeHashes, |
154 _unlinkedUnitHashes = excludeHashes ? null : <String>[]; | 155 _unlinkedUnitHashes = excludeHashes ? null : <String>[]; |
155 | 156 |
156 /** | 157 /** |
157 * Add a fallback library to the package bundle, corresponding to the library | 158 * Add a fallback library to the package bundle, corresponding to the library |
158 * whose defining compilation unit is located at [source]. Caller must also | 159 * whose defining compilation unit is located at [source]. Caller must also |
159 * call [addFallbackUnit] for all compilation units contained in the library | 160 * call [addFallbackUnit] for all compilation units contained in the library |
160 * (including the defining compilation unit). | 161 * (including the defining compilation unit). |
161 */ | 162 */ |
162 void addFallbackLibrary(Source source) { | 163 void addFallbackLibrary(Source source) { |
163 String uri = source.uri.toString(); | 164 String uri = source.uri.toString(); |
164 _linkedLibraryUris.add(uri); | 165 _linkedLibraryUris.add(uri); |
165 _linkedLibraries.add(new LinkedLibraryBuilder(fallbackMode: true)); | 166 _linkedLibraries.add(new LinkedLibraryBuilder(fallbackMode: true)); |
166 } | 167 } |
167 | 168 |
168 /** | 169 /** |
169 * Use the dependency information in [summaryDataStore] to populate the | |
170 * dependencies in the package bundle being assembled. | |
171 */ | |
172 void recordDependencies(SummaryDataStore summaryDataStore) { | |
173 _dependencies.addAll(summaryDataStore.dependencies); | |
174 } | |
175 | |
176 /** | |
177 * Add a fallback compilation unit to the package bundle, corresponding to | 170 * Add a fallback compilation unit to the package bundle, corresponding to |
178 * the compilation unit located at [source]. | 171 * the compilation unit located at [source]. |
179 */ | 172 */ |
180 void addFallbackUnit(Source source) { | 173 void addFallbackUnit(Source source) { |
181 String uri = source.uri.toString(); | 174 String uri = source.uri.toString(); |
175 UnlinkedUnitBuilder unit = new UnlinkedUnitBuilder( | |
176 fallbackModePath: path.relative(source.fullName)); | |
182 _unlinkedUnitUris.add(uri); | 177 _unlinkedUnitUris.add(uri); |
183 _unlinkedUnits.add(new UnlinkedUnitBuilder( | 178 _unlinkedUnits.add(unit); |
184 fallbackModePath: path.relative(source.fullName))); | 179 _unlinkedUnitMap[uri] = unit; |
185 } | 180 } |
186 | 181 |
187 void addLinkedLibrary(String uri, LinkedLibraryBuilder library) { | 182 void addLinkedLibrary(String uri, LinkedLibraryBuilder library) { |
188 _linkedLibraries.add(library); | 183 _linkedLibraries.add(library); |
189 _linkedLibraryUris.add(uri); | 184 _linkedLibraryUris.add(uri); |
190 } | 185 } |
191 | 186 |
192 void addUnlinkedUnit(Source source, UnlinkedUnitBuilder unit) { | 187 void addUnlinkedUnit(Source source, UnlinkedUnitBuilder unit) { |
193 addUnlinkedUnitWithHash(source.uri.toString(), unit, | 188 addUnlinkedUnitWithHash(source.uri.toString(), unit, |
194 _excludeHashes ? null : _hash(source.contents.data)); | 189 _excludeHashes ? null : _hash(source.contents.data)); |
195 } | 190 } |
196 | 191 |
197 void addUnlinkedUnitWithHash( | 192 void addUnlinkedUnitWithHash( |
198 String uri, UnlinkedUnitBuilder unit, String hash) { | 193 String uri, UnlinkedUnitBuilder unit, String hash) { |
199 _unlinkedUnitUris.add(uri); | 194 _unlinkedUnitUris.add(uri); |
200 _unlinkedUnits.add(unit); | 195 _unlinkedUnits.add(unit); |
196 _unlinkedUnitMap[uri] = unit; | |
201 _unlinkedUnitHashes?.add(hash); | 197 _unlinkedUnitHashes?.add(hash); |
202 } | 198 } |
203 | 199 |
204 /** | 200 /** |
205 * Assemble a new [PackageBundleBuilder] using the gathered information. | 201 * Assemble a new [PackageBundleBuilder] using the gathered information. |
206 */ | 202 */ |
207 PackageBundleBuilder assemble() { | 203 PackageBundleBuilder assemble() { |
208 PackageBundleBuilder packageBundle = new PackageBundleBuilder( | 204 return new PackageBundleBuilder( |
209 linkedLibraryUris: _linkedLibraryUris, | 205 linkedLibraryUris: _linkedLibraryUris, |
210 linkedLibraries: _linkedLibraries, | 206 linkedLibraries: _linkedLibraries, |
211 unlinkedUnitUris: _unlinkedUnitUris, | 207 unlinkedUnitUris: _unlinkedUnitUris, |
212 unlinkedUnits: _unlinkedUnits, | 208 unlinkedUnits: _unlinkedUnits, |
213 unlinkedUnitHashes: _unlinkedUnitHashes, | 209 unlinkedUnitHashes: _unlinkedUnitHashes, |
214 majorVersion: currentMajorVersion, | 210 majorVersion: currentMajorVersion, |
215 minorVersion: currentMinorVersion, | 211 minorVersion: currentMinorVersion, |
216 dependencies: _dependencies); | 212 dependencies: _dependencies, |
217 ApiSignature apiSignature = new ApiSignature(); | 213 apiSignature: _computeApiSignature()); |
218 packageBundle.collectApiSignature(apiSignature); | |
219 packageBundle.apiSignature = apiSignature.toHex(); | |
220 return packageBundle; | |
221 } | 214 } |
222 | 215 |
223 /** | 216 /** |
217 * Use the dependency information in [summaryDataStore] to populate the | |
218 * dependencies in the package bundle being assembled. | |
219 */ | |
220 void recordDependencies(SummaryDataStore summaryDataStore) { | |
221 _dependencies.addAll(summaryDataStore.dependencies); | |
222 } | |
223 | |
224 /** | |
224 * Serialize the library with the given [element]. | 225 * Serialize the library with the given [element]. |
225 */ | 226 */ |
226 void serializeLibraryElement(LibraryElement element) { | 227 void serializeLibraryElement(LibraryElement element) { |
227 String uri = element.source.uri.toString(); | 228 String uri = element.source.uri.toString(); |
228 LibrarySerializationResult libraryResult = serializeLibrary( | 229 LibrarySerializationResult libraryResult = serializeLibrary( |
229 element, | 230 element, |
230 element.context.typeProvider, | 231 element.context.typeProvider, |
231 element.context.analysisOptions.strongMode); | 232 element.context.analysisOptions.strongMode); |
232 _linkedLibraryUris.add(uri); | 233 _linkedLibraryUris.add(uri); |
233 _linkedLibraries.add(libraryResult.linked); | 234 _linkedLibraries.add(libraryResult.linked); |
234 _unlinkedUnitUris.addAll(libraryResult.unitUris); | 235 _unlinkedUnitUris.addAll(libraryResult.unitUris); |
235 _unlinkedUnits.addAll(libraryResult.unlinkedUnits); | 236 _unlinkedUnits.addAll(libraryResult.unlinkedUnits); |
237 for (int i = 0; i < libraryResult.unitUris.length; i++) { | |
238 _unlinkedUnitMap[libraryResult.unitUris[i]] = | |
239 libraryResult.unlinkedUnits[i]; | |
240 } | |
236 for (Source source in libraryResult.unitSources) { | 241 for (Source source in libraryResult.unitSources) { |
237 _unlinkedUnitHashes?.add(_hash(source.contents.data)); | 242 _unlinkedUnitHashes?.add(_hash(source.contents.data)); |
238 } | 243 } |
239 } | 244 } |
240 | 245 |
241 /** | 246 /** |
247 * Compute the API signature for this package bundle. | |
248 */ | |
249 String _computeApiSignature() { | |
250 ApiSignature apiSignature = new ApiSignature(); | |
251 for (String unitUri in _unlinkedUnitUris.toList()..sort()) { | |
scheglov
2016/08/09 17:12:36
It's probably equivalent, but if we ask values fro
Paul Berry
2016/08/09 17:18:02
Done.
| |
252 apiSignature.addString(unitUri); | |
253 _unlinkedUnitMap[unitUri].collectApiSignature(apiSignature); | |
254 } | |
255 return apiSignature.toHex(); | |
256 } | |
257 | |
258 /** | |
242 * Compute a hash of the given file contents. | 259 * Compute a hash of the given file contents. |
243 */ | 260 */ |
244 String _hash(String contents) { | 261 String _hash(String contents) { |
245 return hex.encode(md5.convert(UTF8.encode(contents)).bytes); | 262 return hex.encode(md5.convert(UTF8.encode(contents)).bytes); |
246 } | 263 } |
247 } | 264 } |
248 | 265 |
249 /** | 266 /** |
250 * Instances of this class keep track of intermediate state during | 267 * Instances of this class keep track of intermediate state during |
251 * serialization of a single compilation unit. | 268 * serialization of a single compilation unit. |
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1695 exportNames.add(new LinkedExportNameBuilder( | 1712 exportNames.add(new LinkedExportNameBuilder( |
1696 name: name, | 1713 name: name, |
1697 dependency: serializeDependency(dependentLibrary), | 1714 dependency: serializeDependency(dependentLibrary), |
1698 unit: unit, | 1715 unit: unit, |
1699 kind: kind)); | 1716 kind: kind)); |
1700 } | 1717 } |
1701 pb.exportNames = exportNames; | 1718 pb.exportNames = exportNames; |
1702 return pb; | 1719 return pb; |
1703 } | 1720 } |
1704 } | 1721 } |
OLD | NEW |