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.
17 use middle::cstore::{CrateStore, CrateSource, ChildItem, FoundAst};
18 use middle::cstore::{NativeLibraryKind, LinkMeta, LinkagePreference};
20 use middle::lang_items;
21 use middle::ty::{self, Ty};
22 use middle::def_id::{DefId, DefIndex};
24 use rustc::front::map as ast_map;
25 use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};
27 use std::cell::RefCell;
29 use std::path::PathBuf;
32 use rustc_back::svh::Svh;
33 use rustc_back::target::Target;
36 impl<'tcx> CrateStore<'tcx> for cstore::CStore {
37 fn stability(&self, def: DefId) -> Option<attr::Stability>
39 let cdata = self.get_crate_data(def.krate);
40 decoder::get_stability(&*cdata, def.index)
43 fn closure_kind(&self, _tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind
45 assert!(!def_id.is_local());
46 let cdata = self.get_crate_data(def_id.krate);
47 decoder::closure_kind(&*cdata, def_id.index)
50 fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx>
52 assert!(!def_id.is_local());
53 let cdata = self.get_crate_data(def_id.krate);
54 decoder::closure_ty(&*cdata, def_id.index, tcx)
57 fn item_variances(&self, def: DefId) -> ty::ItemVariances {
58 let cdata = self.get_crate_data(def.krate);
59 decoder::get_item_variances(&*cdata, def.index)
62 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> {
63 let cdata = self.get_crate_data(def.krate);
64 decoder::get_repr_attrs(&*cdata, def.index)
67 fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
68 -> ty::TypeScheme<'tcx>
70 let cdata = self.get_crate_data(def.krate);
71 decoder::get_type(&*cdata, def.index, tcx)
74 fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
75 -> ty::GenericPredicates<'tcx>
77 let cdata = self.get_crate_data(def.krate);
78 decoder::get_predicates(&*cdata, def.index, tcx)
81 fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
82 -> ty::GenericPredicates<'tcx>
84 let cdata = self.get_crate_data(def.krate);
85 decoder::get_super_predicates(&*cdata, def.index, tcx)
88 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>
90 let cdata = self.get_crate_data(def_id.krate);
91 decoder::get_item_attrs(&*cdata, def_id.index)
94 fn item_symbol(&self, def: DefId) -> String
96 let cdata = self.get_crate_data(def.krate);
97 decoder::get_symbol(&cdata, def.index)
100 fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx>
102 let cdata = self.get_crate_data(def.krate);
103 decoder::get_trait_def(&*cdata, def.index, tcx)
106 fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
108 let cdata = self.get_crate_data(def.krate);
109 decoder::get_adt_def(&self.intr, &*cdata, def.index, tcx)
112 fn method_arg_names(&self, did: DefId) -> Vec<String>
114 let cdata = self.get_crate_data(did.krate);
115 decoder::get_method_arg_names(&cdata, did.index)
118 fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem> {
119 let cdata = self.get_crate_data(def.krate);
120 let path = decoder::get_item_path(&*cdata, def.index);
122 cdata.with_local_path(|cpath| {
123 let mut r = Vec::with_capacity(cpath.len() + path.len());
130 fn item_name(&self, def: DefId) -> ast::Name {
131 let cdata = self.get_crate_data(def.krate);
132 decoder::get_item_name(&self.intr, &cdata, def.index)
136 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
138 let mut result = vec![];
139 let cdata = self.get_crate_data(def_id.krate);
140 decoder::each_inherent_implementation_for_type(&*cdata, def_id.index,
141 |iid| result.push(iid));
145 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>
147 let mut result = vec![];
148 self.iter_crate_data(|_, cdata| {
149 decoder::each_implementation_for_trait(cdata, def_id, &mut |iid| {
156 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
157 -> Vec<Rc<ty::Method<'tcx>>>
159 let cdata = self.get_crate_data(def.krate);
160 decoder::get_provided_trait_methods(self.intr.clone(), &*cdata, def.index, tcx)
163 fn trait_item_def_ids(&self, def: DefId)
164 -> Vec<ty::ImplOrTraitItemId>
166 let cdata = self.get_crate_data(def.krate);
167 decoder::get_trait_item_def_ids(&*cdata, def.index)
170 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>
172 let cdata = self.get_crate_data(impl_def_id.krate);
173 decoder::get_impl_items(&*cdata, impl_def_id.index)
176 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>
178 let cdata = self.get_crate_data(def.krate);
179 decoder::get_impl_polarity(&*cdata, def.index)
182 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
183 -> Option<ty::TraitRef<'tcx>>
185 let cdata = self.get_crate_data(def.krate);
186 decoder::get_impl_trait(&*cdata, def.index, tcx)
189 fn custom_coerce_unsized_kind(&self, def: DefId)
190 -> Option<ty::adjustment::CustomCoerceUnsized>
192 let cdata = self.get_crate_data(def.krate);
193 decoder::get_custom_coerce_unsized_kind(&*cdata, def.index)
197 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
198 -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
199 let cdata = self.get_crate_data(def.krate);
200 decoder::get_associated_consts(self.intr.clone(), &*cdata, def.index, tcx)
203 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> Option<DefId>
205 let cdata = self.get_crate_data(def_id.krate);
206 decoder::get_trait_of_item(&*cdata, def_id.index, tcx)
209 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
210 -> ty::ImplOrTraitItem<'tcx>
212 let cdata = self.get_crate_data(def.krate);
213 decoder::get_impl_or_trait_item(
220 fn is_const_fn(&self, did: DefId) -> bool
222 let cdata = self.get_crate_data(did.krate);
223 decoder::is_const_fn(&cdata, did.index)
226 fn is_defaulted_trait(&self, trait_def_id: DefId) -> bool
228 let cdata = self.get_crate_data(trait_def_id.krate);
229 decoder::is_defaulted_trait(&*cdata, trait_def_id.index)
232 fn is_impl(&self, did: DefId) -> bool
234 let cdata = self.get_crate_data(did.krate);
235 decoder::is_impl(&*cdata, did.index)
238 fn is_default_impl(&self, impl_did: DefId) -> bool {
239 let cdata = self.get_crate_data(impl_did.krate);
240 decoder::is_default_impl(&*cdata, impl_did.index)
243 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool
245 let cdata = self.get_crate_data(did.krate);
246 decoder::is_extern_fn(&*cdata, did.index, tcx)
249 fn is_static(&self, did: DefId) -> bool
251 let cdata = self.get_crate_data(did.krate);
252 decoder::is_static(&*cdata, did.index)
255 fn is_static_method(&self, def: DefId) -> bool
257 let cdata = self.get_crate_data(def.krate);
258 decoder::is_static_method(&*cdata, def.index)
261 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool
263 self.do_is_statically_included_foreign_item(id)
266 fn is_typedef(&self, did: DefId) -> bool {
267 let cdata = self.get_crate_data(did.krate);
268 decoder::is_typedef(&*cdata, did.index)
271 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
272 -> Vec<(ast::CrateNum, LinkagePreference)>
274 let cdata = self.get_crate_data(cnum);
275 decoder::get_dylib_dependency_formats(&cdata)
278 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>
280 let mut result = vec![];
281 let crate_data = self.get_crate_data(cnum);
282 decoder::each_lang_item(&*crate_data, |did, lid| {
283 result.push((did, lid)); true
288 fn missing_lang_items(&self, cnum: ast::CrateNum)
289 -> Vec<lang_items::LangItem>
291 let cdata = self.get_crate_data(cnum);
292 decoder::get_missing_lang_items(&*cdata)
295 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool
297 self.get_crate_data(cnum).staged_api
300 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool
302 self.get_crate_data(cnum).explicitly_linked.get()
305 fn is_allocator(&self, cnum: ast::CrateNum) -> bool
307 self.get_crate_data(cnum).is_allocator()
310 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>
312 decoder::get_crate_attributes(self.get_crate_data(cnum).data())
315 fn crate_name(&self, cnum: ast::CrateNum) -> String
317 self.get_crate_data(cnum).name.clone()
320 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh
322 let cdata = self.get_crate_data(cnum);
323 decoder::get_crate_hash(cdata.data())
326 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
327 -> FnvHashMap<DefId, Vec<ast::Attribute>>
329 decoder::get_struct_field_attrs(&*self.get_crate_data(cnum))
332 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
334 let cdata = self.get_crate_data(cnum);
335 decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId {
341 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>
343 let cdata = self.get_crate_data(cnum);
344 decoder::get_native_libraries(&*cdata)
347 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>
349 let cdata = self.get_crate_data(cnum);
350 decoder::get_reachable_ids(&*cdata)
353 fn def_path(&self, def: DefId) -> ast_map::DefPath
355 let cdata = self.get_crate_data(def.krate);
356 let path = decoder::def_path(&*cdata, def.index);
357 let local_path = cdata.local_def_path();
358 local_path.into_iter().chain(path).collect()
361 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
363 let cdata = self.get_crate_data(did.krate);
364 decoder::get_tuple_struct_definition_if_ctor(&*cdata, did.index)
367 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
369 let cdata = self.get_crate_data(def.krate);
370 decoder::get_struct_field_names(&self.intr, &*cdata, def.index)
373 fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
375 let mut result = vec![];
376 let crate_data = self.get_crate_data(def_id.krate);
377 let get_crate_data = |cnum| self.get_crate_data(cnum);
378 decoder::each_child_of_item(
379 self.intr.clone(), &*crate_data,
380 def_id.index, get_crate_data,
381 |def, name, vis| result.push(ChildItem {
389 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
391 let mut result = vec![];
392 let crate_data = self.get_crate_data(cnum);
393 let get_crate_data = |cnum| self.get_crate_data(cnum);
394 decoder::each_top_level_item_of_crate(
395 self.intr.clone(), &*crate_data, get_crate_data,
396 |def, name, vis| result.push(ChildItem {
404 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
407 let cdata = self.get_crate_data(def.krate);
408 let decode_inlined_item = Box::new(astencode::decode_inlined_item);
409 decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item)
412 fn crates(&self) -> Vec<ast::CrateNum>
414 let mut result = vec![];
415 self.iter_crate_data(|cnum, _| result.push(cnum));
419 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>
421 self.get_used_libraries().borrow().clone()
424 fn used_link_args(&self) -> Vec<String>
426 self.get_used_link_args().borrow().clone()
429 fn metadata_filename(&self) -> &str
431 loader::METADATA_FILENAME
434 fn metadata_section_name(&self, target: &Target) -> &str
436 loader::meta_section_name(target)
438 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
440 encoder::encoded_ty(tcx, ty)
443 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
445 self.do_get_used_crates(prefer)
448 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource
450 self.opt_used_crate_source(cnum).unwrap()
453 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>
455 self.do_extern_mod_stmt_cnum(emod_id)
458 fn encode_metadata(&self,
459 tcx: &ty::ctxt<'tcx>,
460 reexports: &def::ExportMap,
461 item_symbols: &RefCell<NodeMap<String>>,
462 link_meta: &LinkMeta,
464 krate: &hir::Crate) -> Vec<u8>
466 let encode_inlined_item: encoder::EncodeInlinedItem =
467 Box::new(|ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii));
469 let encode_params = encoder::EncodeParams {
470 diag: tcx.sess.diagnostic(),
472 reexports: reexports,
473 item_symbols: item_symbols,
474 link_meta: link_meta,
476 encode_inlined_item: encode_inlined_item,
479 encoder::encode_metadata(encode_params, krate)
483 fn metadata_encoding_version(&self) -> &[u8]
485 encoder::metadata_encoding_version