1use std::any::Any;
6use std::path::PathBuf;
7
8use rustc_abi::ExternAbi;
9use rustc_ast as ast;
10use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
11use rustc_hir::def_id::{
12 CrateNum, DefId, LOCAL_CRATE, LocalDefId, StableCrateId, StableCrateIdMap,
13};
14use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
15use rustc_macros::{Decodable, Encodable, HashStable_Generic};
16use rustc_span::{Span, Symbol};
17
18use crate::search_paths::PathKind;
19use crate::utils::NativeLibKind;
20
21#[derive(PartialEq, Clone, Debug, HashStable_Generic, Encodable, Decodable)]
26pub struct CrateSource {
27 pub dylib: Option<(PathBuf, PathKind)>,
28 pub rlib: Option<(PathBuf, PathKind)>,
29 pub rmeta: Option<(PathBuf, PathKind)>,
30 pub sdylib_interface: Option<(PathBuf, PathKind)>,
31}
32
33impl CrateSource {
34 #[inline]
35 pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
36 self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).map(|p| &p.0)
37 }
38}
39
40#[derive(Encodable, Decodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
41#[derive(HashStable_Generic)]
42pub enum CrateDepKind {
43 MacrosOnly,
45 Implicit,
48 Explicit,
51}
52
53impl CrateDepKind {
54 #[inline]
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_Generic)]
64pub enum LinkagePreference {
65 RequireDynamic,
66 RequireStatic,
67}
68
69#[derive(Debug, Encodable, Decodable, HashStable_Generic)]
70pub struct NativeLib {
71 pub kind: NativeLibKind,
72 pub name: Symbol,
73 pub filename: Option<Symbol>,
75 pub cfg: Option<ast::MetaItemInner>,
76 pub foreign_module: Option<DefId>,
77 pub verbatim: Option<bool>,
78 pub dll_imports: Vec<DllImport>,
79}
80
81impl NativeLib {
82 pub fn has_modifiers(&self) -> bool {
83 self.verbatim.is_some() || self.kind.has_modifiers()
84 }
85
86 pub fn wasm_import_module(&self) -> Option<Symbol> {
87 if self.kind == NativeLibKind::WasmImportModule { Some(self.name) } else { None }
88 }
89}
90
91#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq, Eq)]
94pub enum PeImportNameType {
95 Ordinal(u16),
98 Decorated,
101 NoPrefix,
104 Undecorated,
108}
109
110#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
111pub struct DllImport {
112 pub name: Symbol,
113 pub import_name_type: Option<PeImportNameType>,
114 pub calling_convention: DllCallingConvention,
119 pub span: Span,
121 pub is_fn: bool,
123}
124
125impl DllImport {
126 pub fn ordinal(&self) -> Option<u16> {
127 if let Some(PeImportNameType::Ordinal(ordinal)) = self.import_name_type {
128 Some(ordinal)
129 } else {
130 None
131 }
132 }
133
134 pub fn is_missing_decorations(&self) -> bool {
135 self.import_name_type == Some(PeImportNameType::Undecorated)
136 || self.import_name_type == Some(PeImportNameType::NoPrefix)
137 }
138}
139
140#[derive(Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
145pub enum DllCallingConvention {
146 C,
147 Stdcall(usize),
148 Fastcall(usize),
149 Vectorcall(usize),
150}
151
152#[derive(Clone, Encodable, Decodable, HashStable_Generic, Debug)]
153pub struct ForeignModule {
154 pub foreign_items: Vec<DefId>,
155 pub def_id: DefId,
156 pub abi: ExternAbi,
157}
158
159#[derive(Copy, Clone, Debug, HashStable_Generic)]
160pub struct ExternCrate {
161 pub src: ExternCrateSource,
162
163 pub span: Span,
165
166 pub path_len: usize,
169
170 pub dependency_of: CrateNum,
172}
173
174impl ExternCrate {
175 #[inline]
179 pub fn is_direct(&self) -> bool {
180 self.dependency_of == LOCAL_CRATE
181 }
182
183 #[inline]
184 pub fn rank(&self) -> impl PartialOrd {
185 (self.is_direct(), !self.path_len)
189 }
190}
191
192#[derive(Copy, Clone, Debug, HashStable_Generic)]
193pub enum ExternCrateSource {
194 Extern(
196 DefId,
200 ),
201 Path,
203}
204
205pub trait CrateStore: std::fmt::Debug {
215 fn as_any(&self) -> &dyn Any;
216 fn untracked_as_any(&mut self) -> &mut dyn Any;
217
218 fn def_key(&self, def: DefId) -> DefKey;
222 fn def_path(&self, def: DefId) -> DefPath;
223 fn def_path_hash(&self, def: DefId) -> DefPathHash;
224
225 fn crate_name(&self, cnum: CrateNum) -> Symbol;
228 fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
229}
230
231pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
232
233pub struct Untracked {
234 pub cstore: FreezeLock<Box<CrateStoreDyn>>,
235 pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
237 pub definitions: FreezeLock<Definitions>,
238 pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
240}