1use std::any::Any;
6use std::path::PathBuf;
7
8use rustc_abi::ExternAbi;
9use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
10use rustc_hir::attrs::{CfgEntry, NativeLibKind, PeImportNameType};
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::{BlobDecodable, Decodable, Encodable, HashStable_Generic};
16use rustc_span::{Span, Symbol};
17
18#[derive(PartialEq, Clone, Debug, HashStable_Generic, Encodable, Decodable)]
23pub struct CrateSource {
24 pub dylib: Option<PathBuf>,
25 pub rlib: Option<PathBuf>,
26 pub rmeta: Option<PathBuf>,
27 pub sdylib_interface: Option<PathBuf>,
28}
29
30impl CrateSource {
31 #[inline]
32 pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
33 self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter())
34 }
35}
36
37#[derive(Encodable, BlobDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
38#[derive(HashStable_Generic)]
39pub enum CrateDepKind {
40 MacrosOnly,
42 Conditional,
45 Unconditional,
49}
50
51impl CrateDepKind {
52 #[inline]
53 pub fn macros_only(self) -> bool {
54 match self {
55 CrateDepKind::MacrosOnly => true,
56 CrateDepKind::Conditional | CrateDepKind::Unconditional => false,
57 }
58 }
59}
60
61#[derive(Copy, Debug, PartialEq, Clone, Encodable, BlobDecodable, HashStable_Generic)]
62pub enum LinkagePreference {
63 RequireDynamic,
64 RequireStatic,
65}
66
67#[derive(Debug, Encodable, Decodable, HashStable_Generic)]
68pub struct NativeLib {
69 pub kind: NativeLibKind,
70 pub name: Symbol,
71 pub filename: Option<Symbol>,
73 pub cfg: Option<CfgEntry>,
74 pub foreign_module: Option<DefId>,
75 pub verbatim: Option<bool>,
76 pub dll_imports: Vec<DllImport>,
77}
78
79impl NativeLib {
80 pub fn has_modifiers(&self) -> bool {
81 self.verbatim.is_some() || self.kind.has_modifiers()
82 }
83
84 pub fn wasm_import_module(&self) -> Option<Symbol> {
85 if self.kind == NativeLibKind::WasmImportModule { Some(self.name) } else { None }
86 }
87}
88
89#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
90pub struct DllImport {
91 pub name: Symbol,
92 pub import_name_type: Option<PeImportNameType>,
93 pub calling_convention: DllCallingConvention,
98 pub span: Span,
100 pub is_fn: bool,
102}
103
104impl DllImport {
105 pub fn ordinal(&self) -> Option<u16> {
106 if let Some(PeImportNameType::Ordinal(ordinal)) = self.import_name_type {
107 Some(ordinal)
108 } else {
109 None
110 }
111 }
112
113 pub fn is_missing_decorations(&self) -> bool {
114 self.import_name_type == Some(PeImportNameType::Undecorated)
115 || self.import_name_type == Some(PeImportNameType::NoPrefix)
116 }
117}
118
119#[derive(Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
124pub enum DllCallingConvention {
125 C,
126 Stdcall(usize),
127 Fastcall(usize),
128 Vectorcall(usize),
129}
130
131#[derive(Clone, Encodable, Decodable, HashStable_Generic, Debug)]
132pub struct ForeignModule {
133 pub foreign_items: Vec<DefId>,
134 pub def_id: DefId,
135 pub abi: ExternAbi,
136}
137
138#[derive(Copy, Clone, Debug, HashStable_Generic)]
139pub struct ExternCrate {
140 pub src: ExternCrateSource,
141
142 pub span: Span,
144
145 pub path_len: usize,
148
149 pub dependency_of: CrateNum,
151}
152
153impl ExternCrate {
154 #[inline]
158 pub fn is_direct(&self) -> bool {
159 self.dependency_of == LOCAL_CRATE
160 }
161
162 #[inline]
163 pub fn rank(&self) -> impl PartialOrd {
164 (self.is_direct(), !self.path_len)
168 }
169}
170
171#[derive(Copy, Clone, Debug, HashStable_Generic)]
172pub enum ExternCrateSource {
173 Extern(
175 DefId,
179 ),
180 Path,
182}
183
184pub trait CrateStore: std::fmt::Debug {
194 fn as_any(&self) -> &dyn Any;
195 fn untracked_as_any(&mut self) -> &mut dyn Any;
196
197 fn def_key(&self, def: DefId) -> DefKey;
201 fn def_path(&self, def: DefId) -> DefPath;
202 fn def_path_hash(&self, def: DefId) -> DefPathHash;
203
204 fn crate_name(&self, cnum: CrateNum) -> Symbol;
207 fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
208}
209
210pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
211
212pub struct Untracked {
213 pub cstore: FreezeLock<Box<CrateStoreDyn>>,
214 pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
216 pub definitions: FreezeLock<Definitions>,
217 pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
219}