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::{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 inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
81 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
82 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
83 -> Vec<Rc<ty::Method<'tcx>>>;
84 fn trait_item_def_ids(&self, def: DefId)
85 -> Vec<ty::ImplOrTraitItemId>;
88 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
89 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
90 -> Option<ty::TraitRef<'tcx>>;
91 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
92 fn custom_coerce_unsized_kind(&self, def: DefId)
93 -> Option<ty::adjustment::CustomCoerceUnsized>;
94 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
95 -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
97 // trait/impl-item info
98 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
100 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
101 -> ty::ImplOrTraitItem<'tcx>;
104 fn is_const_fn(&self, did: DefId) -> bool;
105 fn is_defaulted_trait(&self, did: DefId) -> bool;
106 fn is_impl(&self, did: DefId) -> bool;
107 fn is_static_method(&self, did: DefId) -> bool;
108 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool;
109 fn is_static(&self, did: DefId) -> bool;
110 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
113 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
114 -> Vec<(ast::CrateNum, LinkagePreference)>;
115 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
116 fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
117 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
118 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
119 fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
120 fn crate_name(&self, cnum: ast::CrateNum) -> String;
121 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
122 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
123 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
124 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
127 fn def_path(&self, def: DefId) -> ast_map::DefPath;
128 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
129 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
130 fn item_children(&self, did: DefId) -> Vec<ChildItem>;
131 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
134 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
136 // This is basically a 1-based range of ints, which is a little
137 // silly - I may fix that.
138 fn crates(&self) -> Vec<ast::CrateNum>;
139 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
140 fn used_link_args(&self) -> Vec<String>;
143 fn metadata_filename(&self) -> &str;
144 fn metadata_section_name(&self, target: &Target) -> &str;
145 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
146 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
147 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
148 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
149 fn encode_metadata(&self,
150 tcx: &ty::ctxt<'tcx>,
151 reexports: &def::ExportMap,
152 item_symbols: &RefCell<NodeMap<String>>,
153 link_meta: &LinkMeta,
155 krate: &hir::Crate) -> Vec<u8>;
156 fn metadata_encoding_version(&self) -> &[u8];
159 impl<'tcx> CrateStore<'tcx> for cstore::CStore {
160 fn stability(&self, def: DefId) -> Option<attr::Stability>
162 let cdata = self.get_crate_data(def.krate);
163 decoder::get_stability(&*cdata, def.index)
166 fn closure_kind(&self, _tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind
168 assert!(!def_id.is_local());
169 let cdata = self.get_crate_data(def_id.krate);
170 decoder::closure_kind(&*cdata, def_id.index)
173 fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx>
175 assert!(!def_id.is_local());
176 let cdata = self.get_crate_data(def_id.krate);
177 decoder::closure_ty(&*cdata, def_id.index, tcx)
180 fn item_variances(&self, def: DefId) -> ty::ItemVariances {
181 let cdata = self.get_crate_data(def.krate);
182 decoder::get_item_variances(&*cdata, def.index)
185 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> {
186 let cdata = self.get_crate_data(def.krate);
187 decoder::get_repr_attrs(&*cdata, def.index)
190 fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
191 -> ty::TypeScheme<'tcx>
193 let cdata = self.get_crate_data(def.krate);
194 decoder::get_type(&*cdata, def.index, tcx)
197 fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
198 -> ty::GenericPredicates<'tcx>
200 let cdata = self.get_crate_data(def.krate);
201 decoder::get_predicates(&*cdata, def.index, tcx)
204 fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
205 -> ty::GenericPredicates<'tcx>
207 let cdata = self.get_crate_data(def.krate);
208 decoder::get_super_predicates(&*cdata, def.index, tcx)
211 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>
213 let cdata = self.get_crate_data(def_id.krate);
214 decoder::get_item_attrs(&*cdata, def_id.index)
217 fn item_symbol(&self, def: DefId) -> String
219 let cdata = self.get_crate_data(def.krate);
220 decoder::get_symbol(&cdata, def.index)
223 fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx>
225 let cdata = self.get_crate_data(def.krate);
226 decoder::get_trait_def(&*cdata, def.index, tcx)
229 fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
231 let cdata = self.get_crate_data(def.krate);
232 decoder::get_adt_def(&self.intr, &*cdata, def.index, tcx)
235 fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem> {
236 let cdata = self.get_crate_data(def.krate);
237 let path = decoder::get_item_path(&*cdata, def.index);
239 cdata.with_local_path(|cpath| {
240 let mut r = Vec::with_capacity(cpath.len() + path.len());
247 fn item_name(&self, def: DefId) -> ast::Name {
248 let cdata = self.get_crate_data(def.krate);
249 decoder::get_item_name(&self.intr, &cdata, def.index)
253 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
255 let mut result = vec![];
256 let cdata = self.get_crate_data(def_id.krate);
257 decoder::each_inherent_implementation_for_type(&*cdata, def_id.index,
258 |iid| result.push(iid));
262 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>
264 let mut result = vec![];
265 self.iter_crate_data(|_, cdata| {
266 decoder::each_implementation_for_trait(cdata, def_id, &mut |iid| {
273 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
274 -> Vec<Rc<ty::Method<'tcx>>>
276 let cdata = self.get_crate_data(def.krate);
277 decoder::get_provided_trait_methods(self.intr.clone(), &*cdata, def.index, tcx)
280 fn trait_item_def_ids(&self, def: DefId)
281 -> Vec<ty::ImplOrTraitItemId>
283 let cdata = self.get_crate_data(def.krate);
284 decoder::get_trait_item_def_ids(&*cdata, def.index)
287 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>
289 let cdata = self.get_crate_data(impl_def_id.krate);
290 decoder::get_impl_items(&*cdata, impl_def_id.index)
293 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>
295 let cdata = self.get_crate_data(def.krate);
296 decoder::get_impl_polarity(&*cdata, def.index)
299 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
300 -> Option<ty::TraitRef<'tcx>>
302 let cdata = self.get_crate_data(def.krate);
303 decoder::get_impl_trait(&*cdata, def.index, tcx)
306 fn custom_coerce_unsized_kind(&self, def: DefId)
307 -> Option<ty::adjustment::CustomCoerceUnsized>
309 let cdata = self.get_crate_data(def.krate);
310 decoder::get_custom_coerce_unsized_kind(&*cdata, def.index)
314 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
315 -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
316 let cdata = self.get_crate_data(def.krate);
317 decoder::get_associated_consts(self.intr.clone(), &*cdata, def.index, tcx)
320 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> Option<DefId>
322 let cdata = self.get_crate_data(def_id.krate);
323 decoder::get_trait_of_item(&*cdata, def_id.index, tcx)
326 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
327 -> ty::ImplOrTraitItem<'tcx>
329 let cdata = self.get_crate_data(def.krate);
330 decoder::get_impl_or_trait_item(
337 fn is_const_fn(&self, did: DefId) -> bool
339 let cdata = self.get_crate_data(did.krate);
340 decoder::is_const_fn(&cdata, did.index)
343 fn is_defaulted_trait(&self, trait_def_id: DefId) -> bool
345 let cdata = self.get_crate_data(trait_def_id.krate);
346 decoder::is_defaulted_trait(&*cdata, trait_def_id.index)
349 fn is_impl(&self, did: DefId) -> bool
351 let cdata = self.get_crate_data(did.krate);
352 decoder::is_impl(&*cdata, did.index)
355 fn is_static_method(&self, def: DefId) -> bool
357 let cdata = self.get_crate_data(def.krate);
358 decoder::is_static_method(&*cdata, def.index)
361 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool
363 let cdata = self.get_crate_data(did.krate);
364 decoder::is_extern_fn(&*cdata, did.index, tcx)
367 fn is_static(&self, did: DefId) -> bool
369 let cdata = self.get_crate_data(did.krate);
370 decoder::is_static(&*cdata, did.index)
373 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool
375 self.do_is_statically_included_foreign_item(id)
378 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
379 -> Vec<(ast::CrateNum, LinkagePreference)>
381 let cdata = self.get_crate_data(cnum);
382 decoder::get_dylib_dependency_formats(&cdata)
385 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>
387 let mut result = vec![];
388 let crate_data = self.get_crate_data(cnum);
389 decoder::each_lang_item(&*crate_data, |did, lid| {
390 result.push((did, lid)); true
395 fn missing_lang_items(&self, cnum: ast::CrateNum)
396 -> Vec<lang_items::LangItem>
398 let cdata = self.get_crate_data(cnum);
399 decoder::get_missing_lang_items(&*cdata)
402 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool
404 self.get_crate_data(cnum).staged_api
407 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool
409 self.get_crate_data(cnum).explicitly_linked.get()
412 fn is_allocator(&self, cnum: ast::CrateNum) -> bool
414 self.get_crate_data(cnum).is_allocator()
417 fn crate_name(&self, cnum: ast::CrateNum) -> String
419 self.get_crate_data(cnum).name.clone()
422 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh
424 let cdata = self.get_crate_data(cnum);
425 decoder::get_crate_hash(cdata.data())
428 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
430 let cdata = self.get_crate_data(cnum);
431 decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId {
437 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>
439 let cdata = self.get_crate_data(cnum);
440 decoder::get_native_libraries(&*cdata)
443 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>
445 let cdata = self.get_crate_data(cnum);
446 decoder::get_reachable_ids(&*cdata)
449 fn def_path(&self, def: DefId) -> ast_map::DefPath
451 let cdata = self.get_crate_data(def.krate);
452 let path = decoder::def_path(&*cdata, def.index);
453 let local_path = cdata.local_def_path();
454 local_path.into_iter().chain(path).collect()
457 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
459 let cdata = self.get_crate_data(did.krate);
460 decoder::get_tuple_struct_definition_if_ctor(&*cdata, did.index)
463 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
465 let cdata = self.get_crate_data(def.krate);
466 decoder::get_struct_field_names(&self.intr, &*cdata, def.index)
469 fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
471 let mut result = vec![];
472 let crate_data = self.get_crate_data(def_id.krate);
473 let get_crate_data = |cnum| self.get_crate_data(cnum);
474 decoder::each_child_of_item(
475 self.intr.clone(), &*crate_data,
476 def_id.index, get_crate_data,
477 |def, name, vis| result.push(ChildItem {
485 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
487 let mut result = vec![];
488 let crate_data = self.get_crate_data(cnum);
489 let get_crate_data = |cnum| self.get_crate_data(cnum);
490 decoder::each_top_level_item_of_crate(
491 self.intr.clone(), &*crate_data, get_crate_data,
492 |def, name, vis| result.push(ChildItem {
500 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
503 let cdata = self.get_crate_data(def.krate);
504 let decode_inlined_item = Box::new(astencode::decode_inlined_item);
505 decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item)
508 fn crates(&self) -> Vec<ast::CrateNum>
510 let mut result = vec![];
511 self.iter_crate_data(|cnum, _| result.push(cnum));
515 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>
517 self.get_used_libraries().borrow().clone()
520 fn used_link_args(&self) -> Vec<String>
522 self.get_used_link_args().borrow().clone()
525 fn metadata_filename(&self) -> &str
527 loader::METADATA_FILENAME
530 fn metadata_section_name(&self, target: &Target) -> &str
532 loader::meta_section_name(target)
534 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
536 encoder::encoded_ty(tcx, ty)
539 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
541 self.do_get_used_crates(prefer)
544 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource
546 self.do_get_used_crate_source(cnum).unwrap()
549 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>
551 self.find_extern_mod_stmt_cnum(emod_id)
554 fn encode_metadata(&self,
555 tcx: &ty::ctxt<'tcx>,
556 reexports: &def::ExportMap,
557 item_symbols: &RefCell<NodeMap<String>>,
558 link_meta: &LinkMeta,
560 krate: &hir::Crate) -> Vec<u8>
562 let encode_inlined_item: encoder::EncodeInlinedItem =
563 Box::new(|ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii));
565 let encode_params = encoder::EncodeParams {
566 diag: tcx.sess.diagnostic(),
568 reexports: reexports,
569 item_symbols: item_symbols,
570 link_meta: link_meta,
572 encode_inlined_item: encode_inlined_item,
575 encoder::encode_metadata(encode_params, krate)
579 fn metadata_encoding_version(&self) -> &[u8]
581 encoder::metadata_encoding_version