1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::search_paths::PathKind;
use crate::utils::NativeLibKind;
use crate::Session;
use rustc_ast as ast;
use rustc_data_structures::sync::{self, MetadataRef, RwLock};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::{ExpnHash, ExpnId};
use rustc_span::symbol::Symbol;
use rustc_span::Span;
use rustc_target::spec::Target;
use std::any::Any;
use std::path::{Path, PathBuf};
#[derive(PartialEq, Clone, Debug, HashStable_Generic, Encodable, Decodable)]
pub struct CrateSource {
pub dylib: Option<(PathBuf, PathKind)>,
pub rlib: Option<(PathBuf, PathKind)>,
pub rmeta: Option<(PathBuf, PathKind)>,
}
impl CrateSource {
#[inline]
pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).map(|p| &p.0)
}
}
#[derive(Encodable, Decodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[derive(HashStable_Generic)]
pub enum CrateDepKind {
MacrosOnly,
Implicit,
Explicit,
}
impl CrateDepKind {
#[inline]
pub fn macros_only(self) -> bool {
match self {
CrateDepKind::MacrosOnly => true,
CrateDepKind::Implicit | CrateDepKind::Explicit => false,
}
}
}
#[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable_Generic)]
pub enum LinkagePreference {
RequireDynamic,
RequireStatic,
}
#[derive(Debug, Encodable, Decodable, HashStable_Generic)]
pub struct NativeLib {
pub kind: NativeLibKind,
pub name: Option<Symbol>,
pub filename: Option<Symbol>,
pub cfg: Option<ast::MetaItem>,
pub foreign_module: Option<DefId>,
pub wasm_import_module: Option<Symbol>,
pub verbatim: Option<bool>,
pub dll_imports: Vec<DllImport>,
}
impl NativeLib {
pub fn has_modifiers(&self) -> bool {
self.verbatim.is_some() || self.kind.has_modifiers()
}
}
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq, Eq)]
pub enum PeImportNameType {
Ordinal(u16),
Decorated,
NoPrefix,
Undecorated,
}
#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
pub struct DllImport {
pub name: Symbol,
pub import_name_type: Option<PeImportNameType>,
pub calling_convention: DllCallingConvention,
pub span: Span,
pub is_fn: bool,
}
impl DllImport {
pub fn ordinal(&self) -> Option<u16> {
if let Some(PeImportNameType::Ordinal(ordinal)) = self.import_name_type {
Some(ordinal)
} else {
None
}
}
}
#[derive(Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
pub enum DllCallingConvention {
C,
Stdcall(usize),
Fastcall(usize),
Vectorcall(usize),
}
#[derive(Clone, Encodable, Decodable, HashStable_Generic, Debug)]
pub struct ForeignModule {
pub foreign_items: Vec<DefId>,
pub def_id: DefId,
}
#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct ExternCrate {
pub src: ExternCrateSource,
pub span: Span,
pub path_len: usize,
pub dependency_of: CrateNum,
}
impl ExternCrate {
#[inline]
pub fn is_direct(&self) -> bool {
self.dependency_of == LOCAL_CRATE
}
#[inline]
pub fn rank(&self) -> impl PartialOrd {
(self.is_direct(), !self.path_len)
}
}
#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum ExternCrateSource {
Extern(
DefId,
),
Path,
}
pub trait MetadataLoader {
fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
}
pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
pub trait CrateStore: std::fmt::Debug {
fn as_any(&self) -> &dyn Any;
fn untracked_as_any(&mut self) -> &mut dyn Any;
fn def_key(&self, def: DefId) -> DefKey;
fn def_path(&self, def: DefId) -> DefPath;
fn def_path_hash(&self, def: DefId) -> DefPathHash;
fn crate_name(&self, cnum: CrateNum) -> Symbol;
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
fn expn_hash_to_expn_id(
&self,
sess: &Session,
cnum: CrateNum,
index_guess: u32,
hash: ExpnHash,
) -> ExpnId;
fn import_source_files(&self, sess: &Session, cnum: CrateNum);
}
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
#[derive(Debug)]
pub struct Untracked {
pub cstore: Box<CrateStoreDyn>,
pub source_span: IndexVec<LocalDefId, Span>,
pub definitions: RwLock<Definitions>,
}