]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/cstore.rs
Auto merge of #86968 - inquisitivecrystal:missing-docs-v2, r=oli-obk
[rust.git] / compiler / rustc_middle / src / middle / cstore.rs
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.
4
5 use crate::ty::TyCtxt;
6
7 use rustc_ast as ast;
8 use rustc_data_structures::svh::Svh;
9 use rustc_data_structures::sync::{self, MetadataRef};
10 use rustc_hir::def::DefKind;
11 use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
12 use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
13 use rustc_macros::HashStable;
14 use rustc_session::search_paths::PathKind;
15 use rustc_session::utils::NativeLibKind;
16 use rustc_session::StableCrateId;
17 use rustc_span::symbol::Symbol;
18 use rustc_span::Span;
19 use rustc_target::spec::Target;
20
21 use std::any::Any;
22 use std::path::{Path, PathBuf};
23
24 // lonely orphan structs and enums looking for a better home
25
26 /// Where a crate came from on the local filesystem. One of these three options
27 /// must be non-None.
28 #[derive(PartialEq, Clone, Debug, HashStable, Encodable, Decodable)]
29 pub struct CrateSource {
30     pub dylib: Option<(PathBuf, PathKind)>,
31     pub rlib: Option<(PathBuf, PathKind)>,
32     pub rmeta: Option<(PathBuf, PathKind)>,
33 }
34
35 impl CrateSource {
36     pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
37         self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).map(|p| &p.0)
38     }
39 }
40
41 #[derive(Encodable, Decodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
42 #[derive(HashStable)]
43 pub enum CrateDepKind {
44     /// A dependency that is only used for its macros.
45     MacrosOnly,
46     /// A dependency that is always injected into the dependency list and so
47     /// doesn't need to be linked to an rlib, e.g., the injected allocator.
48     Implicit,
49     /// A dependency that is required by an rlib version of this crate.
50     /// Ordinary `extern crate`s result in `Explicit` dependencies.
51     Explicit,
52 }
53
54 impl CrateDepKind {
55     pub fn macros_only(self) -> bool {
56         match self {
57             CrateDepKind::MacrosOnly => true,
58             CrateDepKind::Implicit | CrateDepKind::Explicit => false,
59         }
60     }
61 }
62
63 #[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)]
64 pub enum LinkagePreference {
65     RequireDynamic,
66     RequireStatic,
67 }
68
69 #[derive(Clone, Debug, Encodable, Decodable, HashStable)]
70 pub struct NativeLib {
71     pub kind: NativeLibKind,
72     pub name: Option<Symbol>,
73     pub cfg: Option<ast::MetaItem>,
74     pub foreign_module: Option<DefId>,
75     pub wasm_import_module: Option<Symbol>,
76     pub verbatim: Option<bool>,
77     pub dll_imports: Vec<DllImport>,
78 }
79
80 #[derive(Clone, Debug, PartialEq, Eq, Encodable, Decodable, Hash, HashStable)]
81 pub struct DllImport {
82     pub name: Symbol,
83     pub ordinal: Option<u16>,
84     /// Calling convention for the function.
85     ///
86     /// On x86_64, this is always `DllCallingConvention::C`; on i686, it can be any
87     /// of the values, and we use `DllCallingConvention::C` to represent `"cdecl"`.
88     pub calling_convention: DllCallingConvention,
89     /// Span of import's "extern" declaration; used for diagnostics.
90     pub span: Span,
91 }
92
93 /// Calling convention for a function defined in an external library.
94 ///
95 /// The usize value, where present, indicates the size of the function's argument list
96 /// in bytes.
97 #[derive(Copy, Clone, Debug, PartialEq, Eq, Encodable, Decodable, Hash, HashStable)]
98 pub enum DllCallingConvention {
99     C,
100     Stdcall(usize),
101     Fastcall(usize),
102     Vectorcall(usize),
103 }
104
105 #[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
106 pub struct ForeignModule {
107     pub foreign_items: Vec<DefId>,
108     pub def_id: DefId,
109 }
110
111 #[derive(Copy, Clone, Debug, HashStable)]
112 pub struct ExternCrate {
113     pub src: ExternCrateSource,
114
115     /// span of the extern crate that caused this to be loaded
116     pub span: Span,
117
118     /// Number of links to reach the extern;
119     /// used to select the extern with the shortest path
120     pub path_len: usize,
121
122     /// Crate that depends on this crate
123     pub dependency_of: CrateNum,
124 }
125
126 impl ExternCrate {
127     /// If true, then this crate is the crate named by the extern
128     /// crate referenced above. If false, then this crate is a dep
129     /// of the crate.
130     pub fn is_direct(&self) -> bool {
131         self.dependency_of == LOCAL_CRATE
132     }
133
134     pub fn rank(&self) -> impl PartialOrd {
135         // Prefer:
136         // - direct extern crate to indirect
137         // - shorter paths to longer
138         (self.is_direct(), !self.path_len)
139     }
140 }
141
142 #[derive(Copy, Clone, Debug, HashStable)]
143 pub enum ExternCrateSource {
144     /// Crate is loaded by `extern crate`.
145     Extern(
146         /// def_id of the item in the current crate that caused
147         /// this crate to be loaded; note that there could be multiple
148         /// such ids
149         DefId,
150     ),
151     /// Crate is implicitly loaded by a path resolving through extern prelude.
152     Path,
153 }
154
155 #[derive(Encodable, Decodable)]
156 pub struct EncodedMetadata {
157     pub raw_data: Vec<u8>,
158 }
159
160 impl EncodedMetadata {
161     pub fn new() -> EncodedMetadata {
162         EncodedMetadata { raw_data: Vec::new() }
163     }
164 }
165
166 /// The backend's way to give the crate store access to the metadata in a library.
167 /// Note that it returns the raw metadata bytes stored in the library file, whether
168 /// it is compressed, uncompressed, some weird mix, etc.
169 /// rmeta files are backend independent and not handled here.
170 ///
171 /// At the time of this writing, there is only one backend and one way to store
172 /// metadata in library -- this trait just serves to decouple rustc_metadata from
173 /// the archive reader, which depends on LLVM.
174 pub trait MetadataLoader {
175     fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
176     fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
177 }
178
179 pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
180
181 /// A store of Rust crates, through which their metadata can be accessed.
182 ///
183 /// Note that this trait should probably not be expanding today. All new
184 /// functionality should be driven through queries instead!
185 ///
186 /// If you find a method on this trait named `{name}_untracked` it signifies
187 /// that it's *not* tracked for dependency information throughout compilation
188 /// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
189 /// during resolve)
190 pub trait CrateStore {
191     fn as_any(&self) -> &dyn Any;
192
193     // resolve
194     fn def_key(&self, def: DefId) -> DefKey;
195     fn def_kind(&self, def: DefId) -> DefKind;
196     fn def_path(&self, def: DefId) -> DefPath;
197     fn def_path_hash(&self, def: DefId) -> DefPathHash;
198     fn def_path_hash_to_def_id(
199         &self,
200         cnum: CrateNum,
201         index_guess: u32,
202         hash: DefPathHash,
203     ) -> Option<DefId>;
204
205     // "queries" used in resolve that aren't tracked for incremental compilation
206     fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
207     fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId;
208     fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
209
210     // This is basically a 1-based range of ints, which is a little
211     // silly - I may fix that.
212     fn crates_untracked(&self) -> Vec<CrateNum>;
213
214     // utility functions
215     fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
216 }
217
218 pub type CrateStoreDyn = dyn CrateStore + sync::Sync;