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.
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.
12 use front::map as ast_map;
14 use metadata::decoder;
15 use metadata::encoder;
17 use middle::astencode;
19 use middle::lang_items;
20 use middle::ty::{self, Ty};
21 use middle::def_id::{DefId, DefIndex};
22 use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
25 use std::cell::RefCell;
27 use std::path::PathBuf;
30 use rustc_back::target::Target;
33 pub use metadata::common::LinkMeta;
34 pub use metadata::creader::validate_crate_name;
35 pub use metadata::cstore::CrateSource;
36 pub use metadata::cstore::LinkagePreference;
37 pub use metadata::cstore::NativeLibraryKind;
38 pub use metadata::decoder::DecodeInlinedItem;
39 pub use metadata::decoder::DefLike;
40 pub use metadata::inline::InlinedItem;
42 pub use self::DefLike::{DlDef, DlField, DlImpl};
43 pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
45 pub struct ChildItem {
48 pub vis: hir::Visibility
51 pub enum FoundAst<'ast> {
52 Found(&'ast InlinedItem),
53 FoundParent(DefId, &'ast InlinedItem),
57 pub trait CrateStore<'tcx> : Any {
59 fn stability(&self, def: DefId) -> Option<attr::Stability>;
60 fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
62 fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
63 -> ty::ClosureTy<'tcx>;
64 fn item_variances(&self, def: DefId) -> ty::ItemVariances;
65 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
66 fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
67 -> ty::TypeScheme<'tcx>;
68 fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem>;
69 fn item_name(&self, def: DefId) -> ast::Name;
70 fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
71 -> ty::GenericPredicates<'tcx>;
72 fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
73 -> ty::GenericPredicates<'tcx>;
74 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
75 fn item_symbol(&self, def: DefId) -> String;
76 fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
77 fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
78 fn method_arg_names(&self, did: DefId) -> Vec<String>;
79 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
82 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
83 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
84 -> Vec<Rc<ty::Method<'tcx>>>;
85 fn trait_item_def_ids(&self, def: DefId)
86 -> Vec<ty::ImplOrTraitItemId>;
89 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
90 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
91 -> Option<ty::TraitRef<'tcx>>;
92 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
93 fn custom_coerce_unsized_kind(&self, def: DefId)
94 -> Option<ty::adjustment::CustomCoerceUnsized>;
95 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
96 -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
98 // trait/impl-item info
99 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
101 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
102 -> ty::ImplOrTraitItem<'tcx>;
105 fn is_const_fn(&self, did: DefId) -> bool;
106 fn is_defaulted_trait(&self, did: DefId) -> bool;
107 fn is_impl(&self, did: DefId) -> bool;
108 fn is_default_impl(&self, impl_did: DefId) -> bool;
109 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool;
110 fn is_static(&self, did: DefId) -> bool;
111 fn is_static_method(&self, did: DefId) -> bool;
112 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
113 fn is_typedef(&self, did: DefId) -> bool;
116 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
117 -> Vec<(ast::CrateNum, LinkagePreference)>;
118 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
119 fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
120 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
121 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
122 fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
123 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>;
124 fn crate_name(&self, cnum: ast::CrateNum) -> String;
125 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
126 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
127 -> FnvHashMap<DefId, Vec<ast::Attribute>>;
128 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
129 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
130 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
133 fn def_path(&self, def: DefId) -> ast_map::DefPath;
134 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
135 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
136 fn item_children(&self, did: DefId) -> Vec<ChildItem>;
137 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
140 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
142 // This is basically a 1-based range of ints, which is a little
143 // silly - I may fix that.
144 fn crates(&self) -> Vec<ast::CrateNum>;
145 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
146 fn used_link_args(&self) -> Vec<String>;
149 fn metadata_filename(&self) -> &str;
150 fn metadata_section_name(&self, target: &Target) -> &str;
151 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
152 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
153 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
154 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
155 fn encode_metadata(&self,
156 tcx: &ty::ctxt<'tcx>,
157 reexports: &def::ExportMap,
158 item_symbols: &RefCell<NodeMap<String>>,
159 link_meta: &LinkMeta,
161 krate: &hir::Crate) -> Vec<u8>;
162 fn metadata_encoding_version(&self) -> &[u8];
165 impl<'tcx> CrateStore<'tcx> for cstore::CStore {
166 fn stability(&self, def: DefId) -> Option<attr::Stability>
168 let cdata = self.get_crate_data(def.krate);
169 decoder::get_stability(&*cdata, def.index)
172 fn closure_kind(&self, _tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind
174 assert!(!def_id.is_local());
175 let cdata = self.get_crate_data(def_id.krate);
176 decoder::closure_kind(&*cdata, def_id.index)
179 fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx>
181 assert!(!def_id.is_local());
182 let cdata = self.get_crate_data(def_id.krate);
183 decoder::closure_ty(&*cdata, def_id.index, tcx)
186 fn item_variances(&self, def: DefId) -> ty::ItemVariances {
187 let cdata = self.get_crate_data(def.krate);
188 decoder::get_item_variances(&*cdata, def.index)
191 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> {
192 let cdata = self.get_crate_data(def.krate);
193 decoder::get_repr_attrs(&*cdata, def.index)
196 fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
197 -> ty::TypeScheme<'tcx>
199 let cdata = self.get_crate_data(def.krate);
200 decoder::get_type(&*cdata, def.index, tcx)
203 fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
204 -> ty::GenericPredicates<'tcx>
206 let cdata = self.get_crate_data(def.krate);
207 decoder::get_predicates(&*cdata, def.index, tcx)
210 fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
211 -> ty::GenericPredicates<'tcx>
213 let cdata = self.get_crate_data(def.krate);
214 decoder::get_super_predicates(&*cdata, def.index, tcx)
217 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>
219 let cdata = self.get_crate_data(def_id.krate);
220 decoder::get_item_attrs(&*cdata, def_id.index)
223 fn item_symbol(&self, def: DefId) -> String
225 let cdata = self.get_crate_data(def.krate);
226 decoder::get_symbol(&cdata, def.index)
229 fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx>
231 let cdata = self.get_crate_data(def.krate);
232 decoder::get_trait_def(&*cdata, def.index, tcx)
235 fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
237 let cdata = self.get_crate_data(def.krate);
238 decoder::get_adt_def(&self.intr, &*cdata, def.index, tcx)
241 fn method_arg_names(&self, did: DefId) -> Vec<String>
243 let cdata = self.get_crate_data(did.krate);
244 decoder::get_method_arg_names(&cdata, did.index)
247 fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem> {
248 let cdata = self.get_crate_data(def.krate);
249 let path = decoder::get_item_path(&*cdata, def.index);
251 cdata.with_local_path(|cpath| {
252 let mut r = Vec::with_capacity(cpath.len() + path.len());
259 fn item_name(&self, def: DefId) -> ast::Name {
260 let cdata = self.get_crate_data(def.krate);
261 decoder::get_item_name(&self.intr, &cdata, def.index)
265 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
267 let mut result = vec![];
268 let cdata = self.get_crate_data(def_id.krate);
269 decoder::each_inherent_implementation_for_type(&*cdata, def_id.index,
270 |iid| result.push(iid));
274 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>
276 let mut result = vec![];
277 self.iter_crate_data(|_, cdata| {
278 decoder::each_implementation_for_trait(cdata, def_id, &mut |iid| {
285 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
286 -> Vec<Rc<ty::Method<'tcx>>>
288 let cdata = self.get_crate_data(def.krate);
289 decoder::get_provided_trait_methods(self.intr.clone(), &*cdata, def.index, tcx)
292 fn trait_item_def_ids(&self, def: DefId)
293 -> Vec<ty::ImplOrTraitItemId>
295 let cdata = self.get_crate_data(def.krate);
296 decoder::get_trait_item_def_ids(&*cdata, def.index)
299 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>
301 let cdata = self.get_crate_data(impl_def_id.krate);
302 decoder::get_impl_items(&*cdata, impl_def_id.index)
305 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>
307 let cdata = self.get_crate_data(def.krate);
308 decoder::get_impl_polarity(&*cdata, def.index)
311 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
312 -> Option<ty::TraitRef<'tcx>>
314 let cdata = self.get_crate_data(def.krate);
315 decoder::get_impl_trait(&*cdata, def.index, tcx)
318 fn custom_coerce_unsized_kind(&self, def: DefId)
319 -> Option<ty::adjustment::CustomCoerceUnsized>
321 let cdata = self.get_crate_data(def.krate);
322 decoder::get_custom_coerce_unsized_kind(&*cdata, def.index)
326 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
327 -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
328 let cdata = self.get_crate_data(def.krate);
329 decoder::get_associated_consts(self.intr.clone(), &*cdata, def.index, tcx)
332 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> Option<DefId>
334 let cdata = self.get_crate_data(def_id.krate);
335 decoder::get_trait_of_item(&*cdata, def_id.index, tcx)
338 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
339 -> ty::ImplOrTraitItem<'tcx>
341 let cdata = self.get_crate_data(def.krate);
342 decoder::get_impl_or_trait_item(
349 fn is_const_fn(&self, did: DefId) -> bool
351 let cdata = self.get_crate_data(did.krate);
352 decoder::is_const_fn(&cdata, did.index)
355 fn is_defaulted_trait(&self, trait_def_id: DefId) -> bool
357 let cdata = self.get_crate_data(trait_def_id.krate);
358 decoder::is_defaulted_trait(&*cdata, trait_def_id.index)
361 fn is_impl(&self, did: DefId) -> bool
363 let cdata = self.get_crate_data(did.krate);
364 decoder::is_impl(&*cdata, did.index)
367 fn is_default_impl(&self, impl_did: DefId) -> bool {
368 let cdata = self.get_crate_data(impl_did.krate);
369 decoder::is_default_impl(&*cdata, impl_did.index)
372 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool
374 let cdata = self.get_crate_data(did.krate);
375 decoder::is_extern_fn(&*cdata, did.index, tcx)
378 fn is_static(&self, did: DefId) -> bool
380 let cdata = self.get_crate_data(did.krate);
381 decoder::is_static(&*cdata, did.index)
384 fn is_static_method(&self, def: DefId) -> bool
386 let cdata = self.get_crate_data(def.krate);
387 decoder::is_static_method(&*cdata, def.index)
390 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool
392 self.do_is_statically_included_foreign_item(id)
395 fn is_typedef(&self, did: DefId) -> bool {
396 let cdata = self.get_crate_data(did.krate);
397 decoder::is_typedef(&*cdata, did.index)
400 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
401 -> Vec<(ast::CrateNum, LinkagePreference)>
403 let cdata = self.get_crate_data(cnum);
404 decoder::get_dylib_dependency_formats(&cdata)
407 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>
409 let mut result = vec![];
410 let crate_data = self.get_crate_data(cnum);
411 decoder::each_lang_item(&*crate_data, |did, lid| {
412 result.push((did, lid)); true
417 fn missing_lang_items(&self, cnum: ast::CrateNum)
418 -> Vec<lang_items::LangItem>
420 let cdata = self.get_crate_data(cnum);
421 decoder::get_missing_lang_items(&*cdata)
424 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool
426 self.get_crate_data(cnum).staged_api
429 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool
431 self.get_crate_data(cnum).explicitly_linked.get()
434 fn is_allocator(&self, cnum: ast::CrateNum) -> bool
436 self.get_crate_data(cnum).is_allocator()
439 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>
441 decoder::get_crate_attributes(self.get_crate_data(cnum).data())
444 fn crate_name(&self, cnum: ast::CrateNum) -> String
446 self.get_crate_data(cnum).name.clone()
449 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh
451 let cdata = self.get_crate_data(cnum);
452 decoder::get_crate_hash(cdata.data())
455 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
456 -> FnvHashMap<DefId, Vec<ast::Attribute>>
458 decoder::get_struct_field_attrs(&*self.get_crate_data(cnum))
461 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
463 let cdata = self.get_crate_data(cnum);
464 decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId {
470 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>
472 let cdata = self.get_crate_data(cnum);
473 decoder::get_native_libraries(&*cdata)
476 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>
478 let cdata = self.get_crate_data(cnum);
479 decoder::get_reachable_ids(&*cdata)
482 fn def_path(&self, def: DefId) -> ast_map::DefPath
484 let cdata = self.get_crate_data(def.krate);
485 let path = decoder::def_path(&*cdata, def.index);
486 let local_path = cdata.local_def_path();
487 local_path.into_iter().chain(path).collect()
490 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
492 let cdata = self.get_crate_data(did.krate);
493 decoder::get_tuple_struct_definition_if_ctor(&*cdata, did.index)
496 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
498 let cdata = self.get_crate_data(def.krate);
499 decoder::get_struct_field_names(&self.intr, &*cdata, def.index)
502 fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
504 let mut result = vec![];
505 let crate_data = self.get_crate_data(def_id.krate);
506 let get_crate_data = |cnum| self.get_crate_data(cnum);
507 decoder::each_child_of_item(
508 self.intr.clone(), &*crate_data,
509 def_id.index, get_crate_data,
510 |def, name, vis| result.push(ChildItem {
518 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
520 let mut result = vec![];
521 let crate_data = self.get_crate_data(cnum);
522 let get_crate_data = |cnum| self.get_crate_data(cnum);
523 decoder::each_top_level_item_of_crate(
524 self.intr.clone(), &*crate_data, get_crate_data,
525 |def, name, vis| result.push(ChildItem {
533 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
536 let cdata = self.get_crate_data(def.krate);
537 let decode_inlined_item = Box::new(astencode::decode_inlined_item);
538 decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item)
541 fn crates(&self) -> Vec<ast::CrateNum>
543 let mut result = vec![];
544 self.iter_crate_data(|cnum, _| result.push(cnum));
548 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>
550 self.get_used_libraries().borrow().clone()
553 fn used_link_args(&self) -> Vec<String>
555 self.get_used_link_args().borrow().clone()
558 fn metadata_filename(&self) -> &str
560 loader::METADATA_FILENAME
563 fn metadata_section_name(&self, target: &Target) -> &str
565 loader::meta_section_name(target)
567 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
569 encoder::encoded_ty(tcx, ty)
572 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
574 self.do_get_used_crates(prefer)
577 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource
579 self.do_get_used_crate_source(cnum).unwrap()
582 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>
584 self.find_extern_mod_stmt_cnum(emod_id)
587 fn encode_metadata(&self,
588 tcx: &ty::ctxt<'tcx>,
589 reexports: &def::ExportMap,
590 item_symbols: &RefCell<NodeMap<String>>,
591 link_meta: &LinkMeta,
593 krate: &hir::Crate) -> Vec<u8>
595 let encode_inlined_item: encoder::EncodeInlinedItem =
596 Box::new(|ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii));
598 let encode_params = encoder::EncodeParams {
599 diag: tcx.sess.diagnostic(),
601 reexports: reexports,
602 item_symbols: item_symbols,
603 link_meta: link_meta,
605 encode_inlined_item: encode_inlined_item,
608 encoder::encode_metadata(encode_params, krate)
612 fn metadata_encoding_version(&self) -> &[u8]
614 encoder::metadata_encoding_version