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.
11 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
12 // file at the top-level directory of this distribution and at
13 // http://rust-lang.org/COPYRIGHT.
15 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
16 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
17 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
18 // option. This file may not be copied, modified, or distributed
19 // except according to those terms.
21 // the rustc crate store interface. This also includes types that
22 // are *mostly* used as a part of that interface, but these should
23 // probably get a better home if someone can find one.
25 use hir::def::{self, Def};
26 use hir::def_id::{CrateNum, DefId, DefIndex};
27 use hir::map as hir_map;
28 use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
30 use middle::lang_items;
31 use middle::resolve_lifetime::ObjectLifetimeDefault;
32 use ty::{self, Ty, TyCtxt};
35 use session::search_paths::PathKind;
36 use util::nodemap::{NodeSet, DefIdMap};
38 use std::collections::BTreeMap;
39 use std::path::PathBuf;
43 use syntax::ext::base::SyntaxExtension;
44 use syntax::symbol::Symbol;
46 use rustc_back::target::Target;
48 use rustc_back::PanicStrategy;
50 pub use self::NativeLibraryKind::*;
52 // lonely orphan structs and enums looking for a better home
54 #[derive(Clone, Debug)]
56 pub crate_name: Symbol,
60 // Where a crate came from on the local filesystem. One of these three options
62 #[derive(PartialEq, Clone, Debug)]
63 pub struct CrateSource {
64 pub dylib: Option<(PathBuf, PathKind)>,
65 pub rlib: Option<(PathBuf, PathKind)>,
66 pub rmeta: Option<(PathBuf, PathKind)>,
69 #[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
71 /// A dependency that is only used for its macros, none of which are visible from other crates.
72 /// These are included in the metadata only as placeholders and are ignored when decoding.
74 /// A dependency that is only used for its macros.
76 /// A dependency that is always injected into the dependency list and so
77 /// doesn't need to be linked to an rlib, e.g. the injected allocator.
79 /// A dependency that is required by an rlib version of this crate.
80 /// Ordinary `extern crate`s result in `Explicit` dependencies.
85 pub fn macros_only(self) -> bool {
87 DepKind::UnexportedMacrosOnly | DepKind::MacrosOnly => true,
88 DepKind::Implicit | DepKind::Explicit => false,
93 #[derive(PartialEq, Clone, Debug)]
101 pub fn is_some(&self) -> bool {
102 if let LibSource::Some(_) = *self {
109 pub fn option(&self) -> Option<PathBuf> {
111 LibSource::Some(ref p) => Some(p.clone()),
112 LibSource::MetadataOnly | LibSource::None => None,
117 #[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
118 pub enum LinkagePreference {
123 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
124 pub enum NativeLibraryKind {
125 NativeStatic, // native static library (.a archive)
126 NativeStaticNobundle, // native static library, which doesn't get bundled into .rlibs
127 NativeFramework, // OSX-specific
128 NativeUnknown, // default way to specify a dynamic library
131 #[derive(Clone, Hash, RustcEncodable, RustcDecodable)]
132 pub struct NativeLibrary {
133 pub kind: NativeLibraryKind,
135 pub cfg: Option<ast::MetaItem>,
136 pub foreign_items: Vec<DefIndex>,
139 pub enum LoadedMacro {
140 MacroRules(ast::MacroDef),
141 ProcMacro(Rc<SyntaxExtension>),
144 #[derive(Copy, Clone, Debug)]
145 pub struct ExternCrate {
146 /// def_id of an `extern crate` in the current crate that caused
147 /// this crate to be loaded; note that there could be multiple
151 /// span of the extern crate that caused this to be loaded
154 /// If true, then this crate is the crate named by the extern
155 /// crate referenced above. If false, then this crate is a dep
159 /// Number of links to reach the extern crate `def_id`
160 /// declaration; used to select the extern crate with the shortest
165 /// A store of Rust crates, through with their metadata
167 pub trait CrateStore<'tcx> {
169 fn describe_def(&self, def: DefId) -> Option<Def>;
170 fn def_span(&self, sess: &Session, def: DefId) -> Span;
171 fn stability(&self, def: DefId) -> Option<attr::Stability>;
172 fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
173 fn visibility(&self, def: DefId) -> ty::Visibility;
174 fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind;
175 fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
176 -> ty::ClosureTy<'tcx>;
177 fn item_variances(&self, def: DefId) -> Vec<ty::Variance>;
178 fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
180 fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
181 fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
182 -> ty::GenericPredicates<'tcx>;
183 fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
184 -> ty::GenericPredicates<'tcx>;
185 fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
186 -> ty::Generics<'tcx>;
187 fn item_generics_own_param_counts(&self, def: DefId) -> (usize, usize);
188 fn item_generics_object_lifetime_defaults(&self, def: DefId)
189 -> Vec<ObjectLifetimeDefault>;
190 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
191 fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
192 fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef;
193 fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
194 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
197 fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
200 fn associated_item_def_ids(&self, def_id: DefId) -> Vec<DefId>;
201 fn impl_trait_ref<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
202 -> Option<ty::TraitRef<'tcx>>;
203 fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity;
204 fn custom_coerce_unsized_kind(&self, def: DefId)
205 -> Option<ty::adjustment::CustomCoerceUnsized>;
206 fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
208 // trait/impl-item info
209 fn trait_of_item(&self, def_id: DefId) -> Option<DefId>;
210 fn associated_item(&self, def: DefId) -> Option<ty::AssociatedItem>;
213 fn is_const_fn(&self, did: DefId) -> bool;
214 fn is_defaulted_trait(&self, did: DefId) -> bool;
215 fn is_default_impl(&self, impl_did: DefId) -> bool;
216 fn is_foreign_item(&self, did: DefId) -> bool;
217 fn is_dllimport_foreign_item(&self, def: DefId) -> bool;
218 fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool;
219 fn is_exported_symbol(&self, def_id: DefId) -> bool;
222 fn dylib_dependency_formats(&self, cnum: CrateNum)
223 -> Vec<(CrateNum, LinkagePreference)>;
224 fn dep_kind(&self, cnum: CrateNum) -> DepKind;
225 fn export_macros(&self, cnum: CrateNum);
226 fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
227 fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
228 fn is_staged_api(&self, cnum: CrateNum) -> bool;
229 fn is_allocator(&self, cnum: CrateNum) -> bool;
230 fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
231 fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
232 fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool;
233 fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy;
234 fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate>;
235 /// The name of the crate as it is referred to in source code of the current
237 fn crate_name(&self, cnum: CrateNum) -> Symbol;
238 /// The name of the crate as it is stored in the crate's metadata.
239 fn original_crate_name(&self, cnum: CrateNum) -> Symbol;
240 fn crate_hash(&self, cnum: CrateNum) -> Svh;
241 fn crate_disambiguator(&self, cnum: CrateNum) -> Symbol;
242 fn plugin_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>;
243 fn derive_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>;
244 fn native_libraries(&self, cnum: CrateNum) -> Vec<NativeLibrary>;
245 fn exported_symbols(&self, cnum: CrateNum) -> Vec<DefId>;
246 fn is_no_builtins(&self, cnum: CrateNum) -> bool;
249 fn retrace_path(&self,
251 path_data: &[DisambiguatedDefPathData])
253 fn def_key(&self, def: DefId) -> DefKey;
254 fn def_path(&self, def: DefId) -> hir_map::DefPath;
255 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
256 fn item_children(&self, did: DefId) -> Vec<def::Export>;
257 fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
260 fn maybe_get_item_body<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
261 -> Option<&'tcx hir::Body>;
262 fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body>;
263 fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;
265 fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
266 fn is_item_mir_available(&self, def: DefId) -> bool;
268 // This is basically a 1-based range of ints, which is a little
269 // silly - I may fix that.
270 fn crates(&self) -> Vec<CrateNum>;
271 fn used_libraries(&self) -> Vec<NativeLibrary>;
272 fn used_link_args(&self) -> Vec<String>;
275 fn metadata_filename(&self) -> &str;
276 fn metadata_section_name(&self, target: &Target) -> &str;
277 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
278 fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
279 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
280 fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
281 reexports: &def::ExportMap,
282 link_meta: &LinkMeta,
283 reachable: &NodeSet) -> Vec<u8>;
284 fn metadata_encoding_version(&self) -> &[u8];
287 // FIXME: find a better place for this?
288 pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
289 let mut err_count = 0;
291 let mut say = |s: &str| {
293 (_, None) => bug!("{}", s),
294 (Some(sp), Some(sess)) => sess.span_err(sp, s),
295 (None, Some(sess)) => sess.err(s),
300 say("crate name must not be empty");
303 if c.is_alphanumeric() { continue }
304 if c == '_' { continue }
305 say(&format!("invalid character `{}` in crate name: `{}`", c, s));
310 sess.unwrap().abort_if_errors();
314 /// A dummy crate store that does not support any non-local crates,
315 /// for test purposes.
316 pub struct DummyCrateStore;
317 #[allow(unused_variables)]
318 impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
320 fn describe_def(&self, def: DefId) -> Option<Def> { bug!("describe_def") }
321 fn def_span(&self, sess: &Session, def: DefId) -> Span { bug!("def_span") }
322 fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
323 fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
324 fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
325 fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
326 fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
327 -> ty::ClosureTy<'tcx> { bug!("closure_ty") }
328 fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
329 fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
330 -> Ty<'tcx> { bug!("item_type") }
331 fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
332 bug!("visible_parent_map")
334 fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
335 -> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
336 fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
337 -> ty::GenericPredicates<'tcx> { bug!("item_super_predicates") }
338 fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
339 -> ty::Generics<'tcx> { bug!("item_generics") }
340 fn item_generics_own_param_counts(&self, def: DefId) -> (usize, usize)
341 { bug!("item_generics_own_param_counts") }
342 fn item_generics_object_lifetime_defaults(&self, def: DefId)
343 -> Vec<ObjectLifetimeDefault>
344 { bug!("item_generics_object_lifetime_defaults") }
345 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
346 fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
347 { bug!("trait_def") }
348 fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef
350 fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
351 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
354 fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
357 fn associated_item_def_ids(&self, def_id: DefId) -> Vec<DefId>
358 { bug!("associated_items") }
359 fn impl_trait_ref<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
360 -> Option<ty::TraitRef<'tcx>> { bug!("impl_trait_ref") }
361 fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity { bug!("impl_polarity") }
362 fn custom_coerce_unsized_kind(&self, def: DefId)
363 -> Option<ty::adjustment::CustomCoerceUnsized>
364 { bug!("custom_coerce_unsized_kind") }
365 fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }
367 // trait/impl-item info
368 fn trait_of_item(&self, def_id: DefId) -> Option<DefId> { bug!("trait_of_item") }
369 fn associated_item(&self, def: DefId) -> Option<ty::AssociatedItem> { bug!("associated_item") }
372 fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
373 fn is_defaulted_trait(&self, did: DefId) -> bool { bug!("is_defaulted_trait") }
374 fn is_default_impl(&self, impl_did: DefId) -> bool { bug!("is_default_impl") }
375 fn is_foreign_item(&self, did: DefId) -> bool { bug!("is_foreign_item") }
376 fn is_dllimport_foreign_item(&self, id: DefId) -> bool { false }
377 fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool { false }
378 fn is_exported_symbol(&self, def_id: DefId) -> bool { false }
381 fn dylib_dependency_formats(&self, cnum: CrateNum)
382 -> Vec<(CrateNum, LinkagePreference)>
383 { bug!("dylib_dependency_formats") }
384 fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>
385 { bug!("lang_items") }
386 fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
387 { bug!("missing_lang_items") }
388 fn is_staged_api(&self, cnum: CrateNum) -> bool { bug!("is_staged_api") }
389 fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
390 fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") }
391 fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
392 fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
393 fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }
394 fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool { bug!("is_sanitizer_runtime") }
395 fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy {
396 bug!("panic_strategy")
398 fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate> { bug!("extern_crate") }
399 fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
400 fn original_crate_name(&self, cnum: CrateNum) -> Symbol {
401 bug!("original_crate_name")
403 fn crate_hash(&self, cnum: CrateNum) -> Svh { bug!("crate_hash") }
404 fn crate_disambiguator(&self, cnum: CrateNum)
405 -> Symbol { bug!("crate_disambiguator") }
406 fn plugin_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>
407 { bug!("plugin_registrar_fn") }
408 fn derive_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>
409 { bug!("derive_registrar_fn") }
410 fn native_libraries(&self, cnum: CrateNum) -> Vec<NativeLibrary>
411 { bug!("native_libraries") }
412 fn exported_symbols(&self, cnum: CrateNum) -> Vec<DefId> { bug!("exported_symbols") }
413 fn is_no_builtins(&self, cnum: CrateNum) -> bool { bug!("is_no_builtins") }
416 fn retrace_path(&self,
418 path_data: &[DisambiguatedDefPathData])
423 fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }
424 fn def_path(&self, def: DefId) -> hir_map::DefPath {
425 bug!("relative_def_path")
427 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
428 fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
429 fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
432 fn maybe_get_item_body<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
433 -> Option<&'tcx hir::Body> {
434 bug!("maybe_get_item_body")
436 fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
437 bug!("item_body_nested_bodies")
439 fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
440 bug!("const_is_rvalue_promotable_to_static")
443 fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
444 -> Mir<'tcx> { bug!("get_item_mir") }
445 fn is_item_mir_available(&self, def: DefId) -> bool {
446 bug!("is_item_mir_available")
449 // This is basically a 1-based range of ints, which is a little
450 // silly - I may fix that.
451 fn crates(&self) -> Vec<CrateNum> { vec![] }
452 fn used_libraries(&self) -> Vec<NativeLibrary> { vec![] }
453 fn used_link_args(&self) -> Vec<String> { vec![] }
456 fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
457 fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") }
458 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
460 fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
461 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
462 fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
463 reexports: &def::ExportMap,
464 link_meta: &LinkMeta,
465 reachable: &NodeSet) -> Vec<u8> { vec![] }
466 fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
469 pub trait CrateLoader {
470 fn process_item(&mut self, item: &ast::Item, defs: &Definitions);
471 fn postprocess(&mut self, krate: &ast::Crate);