1 //! the rustc crate store interface. This also includes types that
2 //! are *mostly* used as a part of that interface, but these should
3 //! probably get a better home if someone can find one.
5 use crate::search_paths::PathKind;
6 use crate::utils::NativeLibKind;
9 use rustc_data_structures::sync::{self, MetadataRef};
10 use rustc_hir::def_id::{CrateNum, DefId, StableCrateId, LOCAL_CRATE};
11 use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
12 use rustc_span::hygiene::{ExpnHash, ExpnId};
13 use rustc_span::symbol::Symbol;
15 use rustc_target::spec::Target;
18 use std::path::{Path, PathBuf};
20 // lonely orphan structs and enums looking for a better home
22 /// Where a crate came from on the local filesystem. One of these three options
24 #[derive(PartialEq, Clone, Debug, HashStable_Generic, Encodable, Decodable)]
25 pub struct CrateSource {
26 pub dylib: Option<(PathBuf, PathKind)>,
27 pub rlib: Option<(PathBuf, PathKind)>,
28 pub rmeta: Option<(PathBuf, PathKind)>,
33 pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
34 self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).map(|p| &p.0)
38 #[derive(Encodable, Decodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
39 #[derive(HashStable_Generic)]
40 pub enum CrateDepKind {
41 /// A dependency that is only used for its macros.
43 /// A dependency that is always injected into the dependency list and so
44 /// doesn't need to be linked to an rlib, e.g., the injected allocator.
46 /// A dependency that is required by an rlib version of this crate.
47 /// Ordinary `extern crate`s result in `Explicit` dependencies.
53 pub fn macros_only(self) -> bool {
55 CrateDepKind::MacrosOnly => true,
56 CrateDepKind::Implicit | CrateDepKind::Explicit => false,
61 #[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable_Generic)]
62 pub enum LinkagePreference {
67 #[derive(Debug, Encodable, Decodable, HashStable_Generic)]
68 pub struct NativeLib {
69 pub kind: NativeLibKind,
70 pub name: Option<Symbol>,
71 pub cfg: Option<ast::MetaItem>,
72 pub foreign_module: Option<DefId>,
73 pub wasm_import_module: Option<Symbol>,
74 pub verbatim: Option<bool>,
75 pub dll_imports: Vec<DllImport>,
79 pub fn has_modifiers(&self) -> bool {
80 self.verbatim.is_some() || self.kind.has_modifiers()
84 /// Different ways that the PE Format can decorate a symbol name.
85 /// From <https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-name-type>
86 #[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq, Eq)]
87 pub enum PeImportNameType {
89 /// Uses the ordinal (i.e., a number) rather than the name.
91 /// Same as IMPORT_NAME
92 /// Name is decorated with all prefixes and suffixes.
94 /// Same as IMPORT_NAME_NOPREFIX
95 /// Prefix (e.g., the leading `_` or `@`) is skipped, but suffix is kept.
97 /// Same as IMPORT_NAME_UNDECORATE
98 /// Prefix (e.g., the leading `_` or `@`) and suffix (the first `@` and all
99 /// trailing characters) are skipped.
103 #[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
104 pub struct DllImport {
106 pub import_name_type: Option<PeImportNameType>,
107 /// Calling convention for the function.
109 /// On x86_64, this is always `DllCallingConvention::C`; on i686, it can be any
110 /// of the values, and we use `DllCallingConvention::C` to represent `"cdecl"`.
111 pub calling_convention: DllCallingConvention,
112 /// Span of import's "extern" declaration; used for diagnostics.
114 /// Is this for a function (rather than a static variable).
119 pub fn ordinal(&self) -> Option<u16> {
120 if let Some(PeImportNameType::Ordinal(ordinal)) = self.import_name_type {
128 /// Calling convention for a function defined in an external library.
130 /// The usize value, where present, indicates the size of the function's argument list
132 #[derive(Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
133 pub enum DllCallingConvention {
140 #[derive(Clone, Encodable, Decodable, HashStable_Generic, Debug)]
141 pub struct ForeignModule {
142 pub foreign_items: Vec<DefId>,
146 #[derive(Copy, Clone, Debug, HashStable_Generic)]
147 pub struct ExternCrate {
148 pub src: ExternCrateSource,
150 /// span of the extern crate that caused this to be loaded
153 /// Number of links to reach the extern;
154 /// used to select the extern with the shortest path
157 /// Crate that depends on this crate
158 pub dependency_of: CrateNum,
162 /// If true, then this crate is the crate named by the extern
163 /// crate referenced above. If false, then this crate is a dep
166 pub fn is_direct(&self) -> bool {
167 self.dependency_of == LOCAL_CRATE
171 pub fn rank(&self) -> impl PartialOrd {
173 // - direct extern crate to indirect
174 // - shorter paths to longer
175 (self.is_direct(), !self.path_len)
179 #[derive(Copy, Clone, Debug, HashStable_Generic)]
180 pub enum ExternCrateSource {
181 /// Crate is loaded by `extern crate`.
183 /// def_id of the item in the current crate that caused
184 /// this crate to be loaded; note that there could be multiple
188 /// Crate is implicitly loaded by a path resolving through extern prelude.
192 /// The backend's way to give the crate store access to the metadata in a library.
193 /// Note that it returns the raw metadata bytes stored in the library file, whether
194 /// it is compressed, uncompressed, some weird mix, etc.
195 /// rmeta files are backend independent and not handled here.
197 /// At the time of this writing, there is only one backend and one way to store
198 /// metadata in library -- this trait just serves to decouple rustc_metadata from
199 /// the archive reader, which depends on LLVM.
200 pub trait MetadataLoader {
201 fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
202 fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
205 pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
207 /// A store of Rust crates, through which their metadata can be accessed.
209 /// Note that this trait should probably not be expanding today. All new
210 /// functionality should be driven through queries instead!
212 /// If you find a method on this trait named `{name}_untracked` it signifies
213 /// that it's *not* tracked for dependency information throughout compilation
214 /// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
216 pub trait CrateStore: std::fmt::Debug {
217 fn as_any(&self) -> &dyn Any;
219 // Foreign definitions.
220 // This information is safe to access, since it's hashed as part of the DefPathHash, which incr.
221 // comp. uses to identify a DefId.
222 fn def_key(&self, def: DefId) -> DefKey;
223 fn def_path(&self, def: DefId) -> DefPath;
224 fn def_path_hash(&self, def: DefId) -> DefPathHash;
226 // This information is safe to access, since it's hashed as part of the StableCrateId, which
227 // incr. comp. uses to identify a CrateNum.
228 fn crate_name(&self, cnum: CrateNum) -> Symbol;
229 fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
230 fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
232 /// Fetch a DefId from a DefPathHash for a foreign crate.
233 fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
234 fn expn_hash_to_expn_id(
242 /// Imports all `SourceFile`s from the given crate into the current session.
243 /// This normally happens automatically when we decode a `Span` from
244 /// that crate's metadata - however, the incr comp cache needs
245 /// to trigger this manually when decoding a foreign `Span`
246 fn import_source_files(&self, sess: &Session, cnum: CrateNum);
249 pub type CrateStoreDyn = dyn CrateStore + sync::Sync;