]>
Commit | Line | Data |
---|---|---|
92a42be0 SL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
92a42be0 SL |
11 | use cstore; |
12 | use decoder; | |
13 | use encoder; | |
14 | use loader; | |
15 | ||
54a0048b | 16 | use middle::cstore::{CrateStore, CrateSource, ChildItem, ExternCrate, FoundAst, DefLike}; |
92a42be0 | 17 | use middle::cstore::{NativeLibraryKind, LinkMeta, LinkagePreference}; |
54a0048b | 18 | use rustc::hir::def; |
92a42be0 | 19 | use middle::lang_items; |
54a0048b SL |
20 | use rustc::ty::{self, Ty, TyCtxt, VariantKind}; |
21 | use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; | |
92a42be0 | 22 | |
54a0048b | 23 | use rustc::hir::map as hir_map; |
9cc50fc6 | 24 | use rustc::mir::repr::Mir; |
7453a54e | 25 | use rustc::mir::mir_map::MirMap; |
54a0048b | 26 | use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap}; |
92a42be0 SL |
27 | |
28 | use std::cell::RefCell; | |
29 | use std::rc::Rc; | |
30 | use std::path::PathBuf; | |
31 | use syntax::ast; | |
32 | use syntax::attr; | |
33 | use syntax::parse::token; | |
54a0048b | 34 | use rustc::hir::svh::Svh; |
92a42be0 | 35 | use rustc_back::target::Target; |
54a0048b | 36 | use rustc::hir; |
92a42be0 SL |
37 | |
38 | impl<'tcx> CrateStore<'tcx> for cstore::CStore { | |
39 | fn stability(&self, def: DefId) -> Option<attr::Stability> | |
40 | { | |
41 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 42 | decoder::get_stability(&cdata, def.index) |
92a42be0 SL |
43 | } |
44 | ||
9cc50fc6 SL |
45 | fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> |
46 | { | |
47 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 48 | decoder::get_deprecation(&cdata, def.index) |
9cc50fc6 SL |
49 | } |
50 | ||
54a0048b SL |
51 | fn visibility(&self, def: DefId) -> ty::Visibility { |
52 | let cdata = self.get_crate_data(def.krate); | |
53 | decoder::get_visibility(&cdata, def.index) | |
54 | } | |
55 | ||
56 | fn closure_kind(&self, _tcx: &TyCtxt<'tcx>, def_id: DefId) -> ty::ClosureKind | |
92a42be0 SL |
57 | { |
58 | assert!(!def_id.is_local()); | |
59 | let cdata = self.get_crate_data(def_id.krate); | |
7453a54e | 60 | decoder::closure_kind(&cdata, def_id.index) |
92a42be0 SL |
61 | } |
62 | ||
54a0048b | 63 | fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx> |
92a42be0 SL |
64 | { |
65 | assert!(!def_id.is_local()); | |
66 | let cdata = self.get_crate_data(def_id.krate); | |
7453a54e | 67 | decoder::closure_ty(&cdata, def_id.index, tcx) |
92a42be0 SL |
68 | } |
69 | ||
70 | fn item_variances(&self, def: DefId) -> ty::ItemVariances { | |
71 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 72 | decoder::get_item_variances(&cdata, def.index) |
92a42be0 SL |
73 | } |
74 | ||
75 | fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { | |
76 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 77 | decoder::get_repr_attrs(&cdata, def.index) |
92a42be0 SL |
78 | } |
79 | ||
54a0048b | 80 | fn item_type(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
81 | -> ty::TypeScheme<'tcx> |
82 | { | |
83 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 84 | decoder::get_type(&cdata, def.index, tcx) |
92a42be0 SL |
85 | } |
86 | ||
54a0048b | 87 | fn item_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
88 | -> ty::GenericPredicates<'tcx> |
89 | { | |
90 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 91 | decoder::get_predicates(&cdata, def.index, tcx) |
92a42be0 SL |
92 | } |
93 | ||
54a0048b | 94 | fn item_super_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
95 | -> ty::GenericPredicates<'tcx> |
96 | { | |
97 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 98 | decoder::get_super_predicates(&cdata, def.index, tcx) |
92a42be0 SL |
99 | } |
100 | ||
101 | fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> | |
102 | { | |
103 | let cdata = self.get_crate_data(def_id.krate); | |
7453a54e | 104 | decoder::get_item_attrs(&cdata, def_id.index) |
92a42be0 SL |
105 | } |
106 | ||
107 | fn item_symbol(&self, def: DefId) -> String | |
108 | { | |
109 | let cdata = self.get_crate_data(def.krate); | |
110 | decoder::get_symbol(&cdata, def.index) | |
111 | } | |
112 | ||
54a0048b | 113 | fn trait_def(&self, tcx: &TyCtxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx> |
92a42be0 SL |
114 | { |
115 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 116 | decoder::get_trait_def(&cdata, def.index, tcx) |
92a42be0 SL |
117 | } |
118 | ||
54a0048b | 119 | fn adt_def(&self, tcx: &TyCtxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx> |
92a42be0 SL |
120 | { |
121 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 122 | decoder::get_adt_def(&self.intr, &cdata, def.index, tcx) |
92a42be0 SL |
123 | } |
124 | ||
125 | fn method_arg_names(&self, did: DefId) -> Vec<String> | |
126 | { | |
127 | let cdata = self.get_crate_data(did.krate); | |
128 | decoder::get_method_arg_names(&cdata, did.index) | |
129 | } | |
130 | ||
92a42be0 SL |
131 | fn item_name(&self, def: DefId) -> ast::Name { |
132 | let cdata = self.get_crate_data(def.krate); | |
133 | decoder::get_item_name(&self.intr, &cdata, def.index) | |
134 | } | |
135 | ||
136 | ||
137 | fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> | |
138 | { | |
139 | let mut result = vec![]; | |
140 | let cdata = self.get_crate_data(def_id.krate); | |
7453a54e | 141 | decoder::each_inherent_implementation_for_type(&cdata, def_id.index, |
92a42be0 SL |
142 | |iid| result.push(iid)); |
143 | result | |
144 | } | |
145 | ||
146 | fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId> | |
147 | { | |
148 | let mut result = vec![]; | |
149 | self.iter_crate_data(|_, cdata| { | |
150 | decoder::each_implementation_for_trait(cdata, def_id, &mut |iid| { | |
151 | result.push(iid) | |
152 | }) | |
153 | }); | |
154 | result | |
155 | } | |
156 | ||
54a0048b | 157 | fn provided_trait_methods(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
158 | -> Vec<Rc<ty::Method<'tcx>>> |
159 | { | |
160 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 161 | decoder::get_provided_trait_methods(self.intr.clone(), &cdata, def.index, tcx) |
92a42be0 SL |
162 | } |
163 | ||
164 | fn trait_item_def_ids(&self, def: DefId) | |
165 | -> Vec<ty::ImplOrTraitItemId> | |
166 | { | |
167 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 168 | decoder::get_trait_item_def_ids(&cdata, def.index) |
92a42be0 SL |
169 | } |
170 | ||
171 | fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId> | |
172 | { | |
173 | let cdata = self.get_crate_data(impl_def_id.krate); | |
7453a54e | 174 | decoder::get_impl_items(&cdata, impl_def_id.index) |
92a42be0 SL |
175 | } |
176 | ||
177 | fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity> | |
178 | { | |
179 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 180 | decoder::get_impl_polarity(&cdata, def.index) |
92a42be0 SL |
181 | } |
182 | ||
54a0048b | 183 | fn impl_trait_ref(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
184 | -> Option<ty::TraitRef<'tcx>> |
185 | { | |
186 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 187 | decoder::get_impl_trait(&cdata, def.index, tcx) |
92a42be0 SL |
188 | } |
189 | ||
190 | fn custom_coerce_unsized_kind(&self, def: DefId) | |
191 | -> Option<ty::adjustment::CustomCoerceUnsized> | |
192 | { | |
193 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 194 | decoder::get_custom_coerce_unsized_kind(&cdata, def.index) |
92a42be0 SL |
195 | } |
196 | ||
197 | // FIXME: killme | |
54a0048b | 198 | fn associated_consts(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
199 | -> Vec<Rc<ty::AssociatedConst<'tcx>>> { |
200 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 201 | decoder::get_associated_consts(self.intr.clone(), &cdata, def.index, tcx) |
92a42be0 SL |
202 | } |
203 | ||
54a0048b SL |
204 | fn impl_parent(&self, impl_def: DefId) -> Option<DefId> { |
205 | let cdata = self.get_crate_data(impl_def.krate); | |
206 | decoder::get_parent_impl(&*cdata, impl_def.index) | |
207 | } | |
208 | ||
209 | fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId) -> Option<DefId> | |
92a42be0 SL |
210 | { |
211 | let cdata = self.get_crate_data(def_id.krate); | |
7453a54e | 212 | decoder::get_trait_of_item(&cdata, def_id.index, tcx) |
92a42be0 SL |
213 | } |
214 | ||
54a0048b SL |
215 | fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
216 | -> Option<ty::ImplOrTraitItem<'tcx>> | |
92a42be0 SL |
217 | { |
218 | let cdata = self.get_crate_data(def.krate); | |
219 | decoder::get_impl_or_trait_item( | |
220 | self.intr.clone(), | |
7453a54e | 221 | &cdata, |
92a42be0 SL |
222 | def.index, |
223 | tcx) | |
224 | } | |
225 | ||
226 | fn is_const_fn(&self, did: DefId) -> bool | |
227 | { | |
228 | let cdata = self.get_crate_data(did.krate); | |
229 | decoder::is_const_fn(&cdata, did.index) | |
230 | } | |
231 | ||
232 | fn is_defaulted_trait(&self, trait_def_id: DefId) -> bool | |
233 | { | |
234 | let cdata = self.get_crate_data(trait_def_id.krate); | |
7453a54e | 235 | decoder::is_defaulted_trait(&cdata, trait_def_id.index) |
92a42be0 SL |
236 | } |
237 | ||
238 | fn is_impl(&self, did: DefId) -> bool | |
239 | { | |
240 | let cdata = self.get_crate_data(did.krate); | |
7453a54e | 241 | decoder::is_impl(&cdata, did.index) |
92a42be0 SL |
242 | } |
243 | ||
244 | fn is_default_impl(&self, impl_did: DefId) -> bool { | |
245 | let cdata = self.get_crate_data(impl_did.krate); | |
7453a54e | 246 | decoder::is_default_impl(&cdata, impl_did.index) |
92a42be0 SL |
247 | } |
248 | ||
54a0048b | 249 | fn is_extern_item(&self, tcx: &TyCtxt<'tcx>, did: DefId) -> bool { |
92a42be0 | 250 | let cdata = self.get_crate_data(did.krate); |
7453a54e | 251 | decoder::is_extern_item(&cdata, did.index, tcx) |
92a42be0 SL |
252 | } |
253 | ||
254 | fn is_static_method(&self, def: DefId) -> bool | |
255 | { | |
256 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 257 | decoder::is_static_method(&cdata, def.index) |
92a42be0 SL |
258 | } |
259 | ||
260 | fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool | |
261 | { | |
262 | self.do_is_statically_included_foreign_item(id) | |
263 | } | |
264 | ||
265 | fn is_typedef(&self, did: DefId) -> bool { | |
266 | let cdata = self.get_crate_data(did.krate); | |
7453a54e | 267 | decoder::is_typedef(&cdata, did.index) |
92a42be0 SL |
268 | } |
269 | ||
270 | fn dylib_dependency_formats(&self, cnum: ast::CrateNum) | |
271 | -> Vec<(ast::CrateNum, LinkagePreference)> | |
272 | { | |
273 | let cdata = self.get_crate_data(cnum); | |
274 | decoder::get_dylib_dependency_formats(&cdata) | |
275 | } | |
276 | ||
277 | fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)> | |
278 | { | |
279 | let mut result = vec![]; | |
280 | let crate_data = self.get_crate_data(cnum); | |
7453a54e | 281 | decoder::each_lang_item(&crate_data, |did, lid| { |
92a42be0 SL |
282 | result.push((did, lid)); true |
283 | }); | |
284 | result | |
285 | } | |
286 | ||
287 | fn missing_lang_items(&self, cnum: ast::CrateNum) | |
288 | -> Vec<lang_items::LangItem> | |
289 | { | |
290 | let cdata = self.get_crate_data(cnum); | |
7453a54e | 291 | decoder::get_missing_lang_items(&cdata) |
92a42be0 SL |
292 | } |
293 | ||
294 | fn is_staged_api(&self, cnum: ast::CrateNum) -> bool | |
295 | { | |
296 | self.get_crate_data(cnum).staged_api | |
297 | } | |
298 | ||
299 | fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool | |
300 | { | |
301 | self.get_crate_data(cnum).explicitly_linked.get() | |
302 | } | |
303 | ||
304 | fn is_allocator(&self, cnum: ast::CrateNum) -> bool | |
305 | { | |
306 | self.get_crate_data(cnum).is_allocator() | |
307 | } | |
308 | ||
309 | fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute> | |
310 | { | |
311 | decoder::get_crate_attributes(self.get_crate_data(cnum).data()) | |
312 | } | |
313 | ||
54a0048b | 314 | fn crate_name(&self, cnum: ast::CrateNum) -> token::InternedString |
92a42be0 | 315 | { |
54a0048b SL |
316 | token::intern_and_get_ident(&self.get_crate_data(cnum).name[..]) |
317 | } | |
318 | ||
319 | fn original_crate_name(&self, cnum: ast::CrateNum) -> token::InternedString | |
320 | { | |
321 | token::intern_and_get_ident(&self.get_crate_data(cnum).name()) | |
322 | } | |
323 | ||
324 | fn extern_crate(&self, cnum: ast::CrateNum) -> Option<ExternCrate> | |
325 | { | |
326 | self.get_crate_data(cnum).extern_crate.get() | |
92a42be0 SL |
327 | } |
328 | ||
329 | fn crate_hash(&self, cnum: ast::CrateNum) -> Svh | |
330 | { | |
331 | let cdata = self.get_crate_data(cnum); | |
332 | decoder::get_crate_hash(cdata.data()) | |
333 | } | |
334 | ||
54a0048b SL |
335 | fn crate_disambiguator(&self, cnum: ast::CrateNum) -> token::InternedString |
336 | { | |
337 | let cdata = self.get_crate_data(cnum); | |
338 | token::intern_and_get_ident(decoder::get_crate_disambiguator(cdata.data())) | |
339 | } | |
340 | ||
92a42be0 SL |
341 | fn crate_struct_field_attrs(&self, cnum: ast::CrateNum) |
342 | -> FnvHashMap<DefId, Vec<ast::Attribute>> | |
343 | { | |
7453a54e | 344 | decoder::get_struct_field_attrs(&self.get_crate_data(cnum)) |
92a42be0 SL |
345 | } |
346 | ||
347 | fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId> | |
348 | { | |
349 | let cdata = self.get_crate_data(cnum); | |
350 | decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId { | |
351 | krate: cnum, | |
352 | index: index | |
353 | }) | |
354 | } | |
355 | ||
356 | fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)> | |
357 | { | |
358 | let cdata = self.get_crate_data(cnum); | |
7453a54e | 359 | decoder::get_native_libraries(&cdata) |
92a42be0 SL |
360 | } |
361 | ||
362 | fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId> | |
363 | { | |
364 | let cdata = self.get_crate_data(cnum); | |
7453a54e | 365 | decoder::get_reachable_ids(&cdata) |
92a42be0 SL |
366 | } |
367 | ||
54a0048b SL |
368 | /// Returns the `DefKey` for a given `DefId`. This indicates the |
369 | /// parent `DefId` as well as some idea of what kind of data the | |
370 | /// `DefId` refers to. | |
371 | fn def_key(&self, def: DefId) -> hir_map::DefKey { | |
372 | let cdata = self.get_crate_data(def.krate); | |
373 | decoder::def_key(&cdata, def.index) | |
374 | } | |
375 | ||
376 | fn relative_def_path(&self, def: DefId) -> hir_map::DefPath { | |
92a42be0 | 377 | let cdata = self.get_crate_data(def.krate); |
54a0048b | 378 | decoder::def_path(&cdata, def.index) |
92a42be0 SL |
379 | } |
380 | ||
7453a54e SL |
381 | fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { |
382 | let cdata = self.get_crate_data(def_id.krate); | |
383 | decoder::get_variant_kind(&cdata, def_id.index) | |
384 | } | |
385 | ||
386 | fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId> | |
387 | { | |
388 | let cdata = self.get_crate_data(struct_def_id.krate); | |
389 | decoder::get_struct_ctor_def_id(&cdata, struct_def_id.index) | |
390 | } | |
391 | ||
92a42be0 SL |
392 | fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId> |
393 | { | |
394 | let cdata = self.get_crate_data(did.krate); | |
7453a54e | 395 | decoder::get_tuple_struct_definition_if_ctor(&cdata, did.index) |
92a42be0 SL |
396 | } |
397 | ||
398 | fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> | |
399 | { | |
400 | let cdata = self.get_crate_data(def.krate); | |
7453a54e | 401 | decoder::get_struct_field_names(&self.intr, &cdata, def.index) |
92a42be0 SL |
402 | } |
403 | ||
404 | fn item_children(&self, def_id: DefId) -> Vec<ChildItem> | |
405 | { | |
406 | let mut result = vec![]; | |
407 | let crate_data = self.get_crate_data(def_id.krate); | |
408 | let get_crate_data = |cnum| self.get_crate_data(cnum); | |
409 | decoder::each_child_of_item( | |
7453a54e | 410 | self.intr.clone(), &crate_data, |
92a42be0 SL |
411 | def_id.index, get_crate_data, |
412 | |def, name, vis| result.push(ChildItem { | |
413 | def: def, | |
414 | name: name, | |
415 | vis: vis | |
416 | })); | |
417 | result | |
418 | } | |
419 | ||
420 | fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem> | |
421 | { | |
422 | let mut result = vec![]; | |
423 | let crate_data = self.get_crate_data(cnum); | |
424 | let get_crate_data = |cnum| self.get_crate_data(cnum); | |
425 | decoder::each_top_level_item_of_crate( | |
7453a54e | 426 | self.intr.clone(), &crate_data, get_crate_data, |
92a42be0 SL |
427 | |def, name, vis| result.push(ChildItem { |
428 | def: def, | |
429 | name: name, | |
430 | vis: vis | |
431 | })); | |
432 | result | |
433 | } | |
434 | ||
54a0048b | 435 | fn maybe_get_item_ast(&'tcx self, tcx: &TyCtxt<'tcx>, def: DefId) |
92a42be0 SL |
436 | -> FoundAst<'tcx> |
437 | { | |
438 | let cdata = self.get_crate_data(def.krate); | |
54a0048b | 439 | decoder::maybe_get_item_ast(&cdata, tcx, def.index) |
92a42be0 SL |
440 | } |
441 | ||
54a0048b | 442 | fn maybe_get_item_mir(&self, tcx: &TyCtxt<'tcx>, def: DefId) |
9cc50fc6 SL |
443 | -> Option<Mir<'tcx>> { |
444 | let cdata = self.get_crate_data(def.krate); | |
7453a54e SL |
445 | decoder::maybe_get_item_mir(&cdata, tcx, def.index) |
446 | } | |
447 | ||
448 | fn is_item_mir_available(&self, def: DefId) -> bool { | |
449 | let cdata = self.get_crate_data(def.krate); | |
450 | decoder::is_item_mir_available(&cdata, def.index) | |
9cc50fc6 SL |
451 | } |
452 | ||
92a42be0 SL |
453 | fn crates(&self) -> Vec<ast::CrateNum> |
454 | { | |
455 | let mut result = vec![]; | |
456 | self.iter_crate_data(|cnum, _| result.push(cnum)); | |
457 | result | |
458 | } | |
459 | ||
460 | fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)> | |
461 | { | |
462 | self.get_used_libraries().borrow().clone() | |
463 | } | |
464 | ||
465 | fn used_link_args(&self) -> Vec<String> | |
466 | { | |
467 | self.get_used_link_args().borrow().clone() | |
468 | } | |
469 | ||
470 | fn metadata_filename(&self) -> &str | |
471 | { | |
472 | loader::METADATA_FILENAME | |
473 | } | |
474 | ||
475 | fn metadata_section_name(&self, target: &Target) -> &str | |
476 | { | |
477 | loader::meta_section_name(target) | |
478 | } | |
54a0048b SL |
479 | fn encode_type(&self, |
480 | tcx: &TyCtxt<'tcx>, | |
481 | ty: Ty<'tcx>, | |
482 | def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String) | |
483 | -> Vec<u8> | |
92a42be0 | 484 | { |
54a0048b | 485 | encoder::encoded_ty(tcx, ty, def_id_to_string) |
92a42be0 SL |
486 | } |
487 | ||
488 | fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)> | |
489 | { | |
490 | self.do_get_used_crates(prefer) | |
491 | } | |
492 | ||
493 | fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource | |
494 | { | |
495 | self.opt_used_crate_source(cnum).unwrap() | |
496 | } | |
497 | ||
498 | fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum> | |
499 | { | |
500 | self.do_extern_mod_stmt_cnum(emod_id) | |
501 | } | |
502 | ||
503 | fn encode_metadata(&self, | |
54a0048b | 504 | tcx: &TyCtxt<'tcx>, |
92a42be0 SL |
505 | reexports: &def::ExportMap, |
506 | item_symbols: &RefCell<NodeMap<String>>, | |
507 | link_meta: &LinkMeta, | |
508 | reachable: &NodeSet, | |
7453a54e | 509 | mir_map: &MirMap<'tcx>, |
92a42be0 SL |
510 | krate: &hir::Crate) -> Vec<u8> |
511 | { | |
54a0048b | 512 | let ecx = encoder::EncodeContext { |
92a42be0 SL |
513 | diag: tcx.sess.diagnostic(), |
514 | tcx: tcx, | |
515 | reexports: reexports, | |
516 | item_symbols: item_symbols, | |
517 | link_meta: link_meta, | |
518 | cstore: self, | |
9cc50fc6 SL |
519 | reachable: reachable, |
520 | mir_map: mir_map, | |
54a0048b | 521 | type_abbrevs: RefCell::new(FnvHashMap()), |
92a42be0 | 522 | }; |
54a0048b | 523 | encoder::encode_metadata(ecx, krate) |
92a42be0 SL |
524 | |
525 | } | |
526 | ||
527 | fn metadata_encoding_version(&self) -> &[u8] | |
528 | { | |
529 | encoder::metadata_encoding_version | |
530 | } | |
54a0048b SL |
531 | |
532 | /// Returns a map from a sufficiently visible external item (i.e. an external item that is | |
533 | /// visible from at least one local module) to a sufficiently visible parent (considering | |
534 | /// modules that re-export the external item to be parents). | |
535 | fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> { | |
536 | let mut visible_parent_map = self.visible_parent_map.borrow_mut(); | |
537 | if !visible_parent_map.is_empty() { return visible_parent_map; } | |
538 | ||
539 | use rustc::middle::cstore::{CrateStore, ChildItem}; | |
540 | use std::collections::vec_deque::VecDeque; | |
541 | use std::collections::hash_map::Entry; | |
542 | for cnum in 1 .. self.next_crate_num() { | |
543 | let cdata = self.get_crate_data(cnum); | |
544 | ||
545 | match cdata.extern_crate.get() { | |
546 | // Ignore crates without a corresponding local `extern crate` item. | |
547 | Some(extern_crate) if !extern_crate.direct => continue, | |
548 | _ => {}, | |
549 | } | |
550 | ||
551 | let mut bfs_queue = &mut VecDeque::new(); | |
552 | let mut add_child = |bfs_queue: &mut VecDeque<_>, child: ChildItem, parent: DefId| { | |
553 | let child = match child.def { | |
554 | DefLike::DlDef(def) if child.vis == ty::Visibility::Public => def.def_id(), | |
555 | _ => return, | |
556 | }; | |
557 | ||
558 | match visible_parent_map.entry(child) { | |
559 | Entry::Occupied(mut entry) => { | |
560 | // If `child` is defined in crate `cnum`, ensure | |
561 | // that it is mapped to a parent in `cnum`. | |
562 | if child.krate == cnum && entry.get().krate != cnum { | |
563 | entry.insert(parent); | |
564 | } | |
565 | } | |
566 | Entry::Vacant(entry) => { | |
567 | entry.insert(parent); | |
568 | bfs_queue.push_back(child); | |
569 | } | |
570 | } | |
571 | }; | |
572 | ||
573 | let croot = DefId { krate: cnum, index: CRATE_DEF_INDEX }; | |
574 | for child in self.crate_top_level_items(cnum) { | |
575 | add_child(bfs_queue, child, croot); | |
576 | } | |
577 | while let Some(def) = bfs_queue.pop_front() { | |
578 | for child in self.item_children(def) { | |
579 | add_child(bfs_queue, child, def); | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
584 | visible_parent_map | |
585 | } | |
92a42be0 | 586 | } |