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.
26 use dep_graph::DepNode;
27 use hir::def_id::{CrateNum, DefId, DefIndex};
28 use hir::map as hir_map;
29 use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
32 use middle::lang_items;
33 use ty::{self, TyCtxt};
35 use session::search_paths::PathKind;
36 use util::nodemap::{NodeSet, DefIdMap};
39 use std::path::PathBuf;
42 use syntax::ext::base::SyntaxExtension;
43 use syntax::symbol::Symbol;
45 use rustc_back::target::Target;
47 use rustc_back::PanicStrategy;
49 pub use self::NativeLibraryKind::*;
51 // lonely orphan structs and enums looking for a better home
53 #[derive(Clone, Debug)]
58 // Where a crate came from on the local filesystem. One of these three options
60 #[derive(PartialEq, Clone, Debug)]
61 pub struct CrateSource {
62 pub dylib: Option<(PathBuf, PathKind)>,
63 pub rlib: Option<(PathBuf, PathKind)>,
64 pub rmeta: Option<(PathBuf, PathKind)>,
67 #[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
69 /// A dependency that is only used for its macros, none of which are visible from other crates.
70 /// These are included in the metadata only as placeholders and are ignored when decoding.
72 /// A dependency that is only used for its macros.
74 /// A dependency that is always injected into the dependency list and so
75 /// doesn't need to be linked to an rlib, e.g. the injected allocator.
77 /// A dependency that is required by an rlib version of this crate.
78 /// Ordinary `extern crate`s result in `Explicit` dependencies.
83 pub fn macros_only(self) -> bool {
85 DepKind::UnexportedMacrosOnly | DepKind::MacrosOnly => true,
86 DepKind::Implicit | DepKind::Explicit => false,
91 #[derive(PartialEq, Clone, Debug)]
99 pub fn is_some(&self) -> bool {
100 if let LibSource::Some(_) = *self {
107 pub fn option(&self) -> Option<PathBuf> {
109 LibSource::Some(ref p) => Some(p.clone()),
110 LibSource::MetadataOnly | LibSource::None => None,
115 #[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
116 pub enum LinkagePreference {
121 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
122 pub enum NativeLibraryKind {
123 NativeStatic, // native static library (.a archive)
124 NativeStaticNobundle, // native static library, which doesn't get bundled into .rlibs
125 NativeFramework, // macOS-specific
126 NativeUnknown, // default way to specify a dynamic library
129 #[derive(Clone, Hash, RustcEncodable, RustcDecodable)]
130 pub struct NativeLibrary {
131 pub kind: NativeLibraryKind,
133 pub cfg: Option<ast::MetaItem>,
134 pub foreign_items: Vec<DefIndex>,
137 pub enum LoadedMacro {
139 ProcMacro(Rc<SyntaxExtension>),
142 #[derive(Copy, Clone, Debug)]
143 pub struct ExternCrate {
144 /// def_id of an `extern crate` in the current crate that caused
145 /// this crate to be loaded; note that there could be multiple
149 /// span of the extern crate that caused this to be loaded
152 /// If true, then this crate is the crate named by the extern
153 /// crate referenced above. If false, then this crate is a dep
157 /// Number of links to reach the extern crate `def_id`
158 /// declaration; used to select the extern crate with the shortest
163 pub struct EncodedMetadata {
164 pub raw_data: Vec<u8>,
165 pub hashes: EncodedMetadataHashes,
168 impl EncodedMetadata {
169 pub fn new() -> EncodedMetadata {
171 raw_data: Vec::new(),
172 hashes: EncodedMetadataHashes::new(),
177 /// The hash for some metadata that (when saving) will be exported
178 /// from this crate, or which (when importing) was exported by an
180 #[derive(Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
181 pub struct EncodedMetadataHash {
182 pub def_index: DefIndex,
183 pub hash: ich::Fingerprint,
186 /// The hash for some metadata that (when saving) will be exported
187 /// from this crate, or which (when importing) was exported by an
189 #[derive(Debug, RustcEncodable, RustcDecodable, Clone)]
190 pub struct EncodedMetadataHashes {
191 pub entry_hashes: Vec<EncodedMetadataHash>,
192 pub global_hashes: Vec<(DepNode<()>, ich::Fingerprint)>,
195 impl EncodedMetadataHashes {
196 pub fn new() -> EncodedMetadataHashes {
197 EncodedMetadataHashes {
198 entry_hashes: Vec::new(),
199 global_hashes: Vec::new(),
204 /// A store of Rust crates, through with their metadata
206 pub trait CrateStore {
207 fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
210 fn visibility(&self, def: DefId) -> ty::Visibility;
211 fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
212 fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
213 fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]>;
214 fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
217 fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
220 fn impl_defaultness(&self, def: DefId) -> hir::Defaultness;
221 fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
223 // trait/impl-item info
224 fn trait_of_item(&self, def_id: DefId) -> Option<DefId>;
225 fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
228 fn is_const_fn(&self, did: DefId) -> bool;
229 fn is_default_impl(&self, impl_did: DefId) -> bool;
230 fn is_foreign_item(&self, did: DefId) -> bool;
231 fn is_dllimport_foreign_item(&self, def: DefId) -> bool;
232 fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool;
233 fn is_exported_symbol(&self, def_id: DefId) -> bool;
236 fn dylib_dependency_formats(&self, cnum: CrateNum)
237 -> Vec<(CrateNum, LinkagePreference)>;
238 fn dep_kind(&self, cnum: CrateNum) -> DepKind;
239 fn export_macros(&self, cnum: CrateNum);
240 fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
241 fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
242 fn is_allocator(&self, cnum: CrateNum) -> bool;
243 fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
244 fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
245 fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool;
246 fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy;
247 fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate>;
248 /// The name of the crate as it is referred to in source code of the current
250 fn crate_name(&self, cnum: CrateNum) -> Symbol;
251 /// The name of the crate as it is stored in the crate's metadata.
252 fn original_crate_name(&self, cnum: CrateNum) -> Symbol;
253 fn crate_hash(&self, cnum: CrateNum) -> Svh;
254 fn crate_disambiguator(&self, cnum: CrateNum) -> Symbol;
255 fn plugin_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>;
256 fn derive_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>;
257 fn native_libraries(&self, cnum: CrateNum) -> Vec<NativeLibrary>;
258 fn exported_symbols(&self, cnum: CrateNum) -> Vec<DefId>;
259 fn is_no_builtins(&self, cnum: CrateNum) -> bool;
262 fn retrace_path(&self,
264 path_data: &[DisambiguatedDefPathData])
266 fn def_key(&self, def: DefId) -> DefKey;
267 fn def_path(&self, def: DefId) -> hir_map::DefPath;
268 fn def_path_hash(&self, def: DefId) -> u64;
269 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
270 fn item_children(&self, did: DefId) -> Vec<def::Export>;
271 fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
274 fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
277 // This is basically a 1-based range of ints, which is a little
278 // silly - I may fix that.
279 fn crates(&self) -> Vec<CrateNum>;
280 fn used_libraries(&self) -> Vec<NativeLibrary>;
281 fn used_link_args(&self) -> Vec<String>;
284 fn metadata_filename(&self) -> &str;
285 fn metadata_section_name(&self, target: &Target) -> &str;
286 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
287 fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
288 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
289 fn encode_metadata<'a, 'tcx>(&self,
290 tcx: TyCtxt<'a, 'tcx, 'tcx>,
291 link_meta: &LinkMeta,
294 fn metadata_encoding_version(&self) -> &[u8];
297 // FIXME: find a better place for this?
298 pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
299 let mut err_count = 0;
301 let mut say = |s: &str| {
303 (_, None) => bug!("{}", s),
304 (Some(sp), Some(sess)) => sess.span_err(sp, s),
305 (None, Some(sess)) => sess.err(s),
310 say("crate name must not be empty");
313 if c.is_alphanumeric() { continue }
314 if c == '_' { continue }
315 say(&format!("invalid character `{}` in crate name: `{}`", c, s));
320 sess.unwrap().abort_if_errors();
324 /// A dummy crate store that does not support any non-local crates,
325 /// for test purposes.
326 pub struct DummyCrateStore;
328 #[allow(unused_variables)]
329 impl CrateStore for DummyCrateStore {
330 fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>
331 { bug!("crate_data_as_rc_any") }
333 fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
334 fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
335 bug!("visible_parent_map")
337 fn item_generics_cloned(&self, def: DefId) -> ty::Generics
338 { bug!("item_generics_cloned") }
339 fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]> { bug!("item_attrs") }
340 fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
343 fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
346 fn impl_defaultness(&self, def: DefId) -> hir::Defaultness { bug!("impl_defaultness") }
347 fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }
349 // trait/impl-item info
350 fn trait_of_item(&self, def_id: DefId) -> Option<DefId> { bug!("trait_of_item") }
351 fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
352 { bug!("associated_item_cloned") }
355 fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
356 fn is_default_impl(&self, impl_did: DefId) -> bool { bug!("is_default_impl") }
357 fn is_foreign_item(&self, did: DefId) -> bool { bug!("is_foreign_item") }
358 fn is_dllimport_foreign_item(&self, id: DefId) -> bool { false }
359 fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool { false }
360 fn is_exported_symbol(&self, def_id: DefId) -> bool { false }
363 fn dylib_dependency_formats(&self, cnum: CrateNum)
364 -> Vec<(CrateNum, LinkagePreference)>
365 { bug!("dylib_dependency_formats") }
366 fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>
367 { bug!("lang_items") }
368 fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
369 { bug!("missing_lang_items") }
370 fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
371 fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") }
372 fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
373 fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
374 fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }
375 fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool { bug!("is_sanitizer_runtime") }
376 fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy {
377 bug!("panic_strategy")
379 fn extern_crate(&self, cnum: CrateNum) -> Option<ExternCrate> { bug!("extern_crate") }
380 fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
381 fn original_crate_name(&self, cnum: CrateNum) -> Symbol {
382 bug!("original_crate_name")
384 fn crate_hash(&self, cnum: CrateNum) -> Svh { bug!("crate_hash") }
385 fn crate_disambiguator(&self, cnum: CrateNum)
386 -> Symbol { bug!("crate_disambiguator") }
387 fn plugin_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>
388 { bug!("plugin_registrar_fn") }
389 fn derive_registrar_fn(&self, cnum: CrateNum) -> Option<DefId>
390 { bug!("derive_registrar_fn") }
391 fn native_libraries(&self, cnum: CrateNum) -> Vec<NativeLibrary>
392 { bug!("native_libraries") }
393 fn exported_symbols(&self, cnum: CrateNum) -> Vec<DefId> { bug!("exported_symbols") }
394 fn is_no_builtins(&self, cnum: CrateNum) -> bool { bug!("is_no_builtins") }
397 fn retrace_path(&self,
399 path_data: &[DisambiguatedDefPathData])
404 fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }
405 fn def_path(&self, def: DefId) -> hir_map::DefPath {
406 bug!("relative_def_path")
408 fn def_path_hash(&self, def: DefId) -> u64 {
411 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
412 fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
413 fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
416 fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
421 // This is basically a 1-based range of ints, which is a little
422 // silly - I may fix that.
423 fn crates(&self) -> Vec<CrateNum> { vec![] }
424 fn used_libraries(&self) -> Vec<NativeLibrary> { vec![] }
425 fn used_link_args(&self) -> Vec<String> { vec![] }
428 fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
429 fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") }
430 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
432 fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
433 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
434 fn encode_metadata<'a, 'tcx>(&self,
435 tcx: TyCtxt<'a, 'tcx, 'tcx>,
436 link_meta: &LinkMeta,
439 bug!("encode_metadata")
441 fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
444 pub trait CrateLoader {
445 fn process_item(&mut self, item: &ast::Item, defs: &Definitions);
446 fn postprocess(&mut self, krate: &ast::Crate);