]>
Commit | Line | Data |
---|---|---|
5869c6ff | 1 | use crate::dep_graph::{DepNode, WorkProduct, WorkProductId}; |
dfeec247 | 2 | use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt}; |
74b04a01 | 3 | use rustc_attr::InlineAttr; |
ff7c6d11 | 4 | use rustc_data_structures::base_n; |
ba9703b0 | 5 | use rustc_data_structures::fingerprint::Fingerprint; |
dfeec247 | 6 | use rustc_data_structures::fx::FxHashMap; |
e74abb32 | 7 | use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; |
cdc7bbd5 | 8 | use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; |
a2a8927a | 9 | use rustc_hir::ItemId; |
5e7ed085 FG |
10 | use rustc_index::vec::Idx; |
11 | use rustc_query_system::ich::StableHashingContext; | |
ba9703b0 | 12 | use rustc_session::config::OptLevel; |
dfeec247 XL |
13 | use rustc_span::source_map::Span; |
14 | use rustc_span::symbol::Symbol; | |
b7449926 | 15 | use std::fmt; |
ff7c6d11 | 16 | use std::hash::Hash; |
ea8adc8c | 17 | |
dc9dc135 | 18 | /// Describes how a monomorphization will be instantiated in object files. |
e74abb32 | 19 | #[derive(PartialEq)] |
dc9dc135 XL |
20 | pub enum InstantiationMode { |
21 | /// There will be exactly one instance of the given MonoItem. It will have | |
22 | /// external linkage so that it can be linked to from other codegen units. | |
23 | GloballyShared { | |
24 | /// In some compilation scenarios we may decide to take functions that | |
25 | /// are typically `LocalCopy` and instead move them to `GloballyShared` | |
26 | /// to avoid codegenning them a bunch of times. In this situation, | |
27 | /// however, our local copy may conflict with other crates also | |
28 | /// inlining the same function. | |
29 | /// | |
30 | /// This flag indicates that this situation is occurring, and informs | |
31 | /// symbol name calculation that some extra mangling is needed to | |
32 | /// avoid conflicts. Note that this may eventually go away entirely if | |
33 | /// ThinLTO enables us to *always* have a globally shared instance of a | |
34 | /// function within one crate's compilation. | |
35 | may_conflict: bool, | |
36 | }, | |
37 | ||
38 | /// Each codegen unit containing a reference to the given MonoItem will | |
39 | /// have its own private copy of the function (with internal linkage). | |
40 | LocalCopy, | |
41 | } | |
42 | ||
5e7ed085 | 43 | #[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, HashStable)] |
ff7c6d11 | 44 | pub enum MonoItem<'tcx> { |
ea8adc8c | 45 | Fn(Instance<'tcx>), |
0531ce1d | 46 | Static(DefId), |
6a06907d | 47 | GlobalAsm(ItemId), |
ea8adc8c XL |
48 | } |
49 | ||
2c00a5a8 | 50 | impl<'tcx> MonoItem<'tcx> { |
c295e0f8 XL |
51 | /// Returns `true` if the mono item is user-defined (i.e. not compiler-generated, like shims). |
52 | pub fn is_user_defined(&self) -> bool { | |
53 | match *self { | |
54 | MonoItem::Fn(instance) => matches!(instance.def, InstanceDef::Item(..)), | |
55 | MonoItem::Static(..) | MonoItem::GlobalAsm(..) => true, | |
56 | } | |
57 | } | |
58 | ||
dc9dc135 | 59 | pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize { |
2c00a5a8 XL |
60 | match *self { |
61 | MonoItem::Fn(instance) => { | |
62 | // Estimate the size of a function based on how many statements | |
63 | // it contains. | |
64 | tcx.instance_def_size_estimate(instance.def) | |
dfeec247 | 65 | } |
2c00a5a8 XL |
66 | // Conservatively estimate the size of a static declaration |
67 | // or assembly to be 1. | |
dfeec247 | 68 | MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1, |
2c00a5a8 XL |
69 | } |
70 | } | |
dc9dc135 XL |
71 | |
72 | pub fn is_generic_fn(&self) -> bool { | |
73 | match *self { | |
dfeec247 XL |
74 | MonoItem::Fn(ref instance) => instance.substs.non_erasable_generics().next().is_some(), |
75 | MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false, | |
dc9dc135 XL |
76 | } |
77 | } | |
78 | ||
3dfed10e | 79 | pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName<'tcx> { |
dc9dc135 XL |
80 | match *self { |
81 | MonoItem::Fn(instance) => tcx.symbol_name(instance), | |
dfeec247 | 82 | MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)), |
6a06907d XL |
83 | MonoItem::GlobalAsm(item_id) => { |
84 | SymbolName::new(tcx, &format!("global_asm_{:?}", item_id.def_id)) | |
dc9dc135 XL |
85 | } |
86 | } | |
87 | } | |
88 | ||
89 | pub fn instantiation_mode(&self, tcx: TyCtxt<'tcx>) -> InstantiationMode { | |
dfeec247 XL |
90 | let generate_cgu_internal_copies = tcx |
91 | .sess | |
92 | .opts | |
93 | .debugging_opts | |
94 | .inline_in_all_cgus | |
95 | .unwrap_or_else(|| tcx.sess.opts.optimize != OptLevel::No) | |
1b1a35ee | 96 | && !tcx.sess.link_dead_code(); |
dc9dc135 XL |
97 | |
98 | match *self { | |
99 | MonoItem::Fn(ref instance) => { | |
17df50a5 | 100 | let entry_def_id = tcx.entry_fn(()).map(|(id, _)| id); |
f035d41b XL |
101 | // If this function isn't inlined or otherwise has an extern |
102 | // indicator, then we'll be creating a globally shared version. | |
103 | if tcx.codegen_fn_attrs(instance.def_id()).contains_extern_indicator() | |
dfeec247 | 104 | || !instance.def.generates_cgu_internal_copy(tcx) |
cdc7bbd5 | 105 | || Some(instance.def_id()) == entry_def_id |
dc9dc135 | 106 | { |
dfeec247 | 107 | return InstantiationMode::GloballyShared { may_conflict: false }; |
dc9dc135 XL |
108 | } |
109 | ||
110 | // At this point we don't have explicit linkage and we're an | |
111 | // inlined function. If we're inlining into all CGUs then we'll | |
f035d41b | 112 | // be creating a local copy per CGU. |
dfeec247 XL |
113 | if generate_cgu_internal_copies { |
114 | return InstantiationMode::LocalCopy; | |
dc9dc135 XL |
115 | } |
116 | ||
117 | // Finally, if this is `#[inline(always)]` we're sure to respect | |
118 | // that with an inline copy per CGU, but otherwise we'll be | |
119 | // creating one copy of this `#[inline]` function which may | |
120 | // conflict with upstream crates as it could be an exported | |
121 | // symbol. | |
122 | match tcx.codegen_fn_attrs(instance.def_id()).inline { | |
123 | InlineAttr::Always => InstantiationMode::LocalCopy, | |
dfeec247 | 124 | _ => InstantiationMode::GloballyShared { may_conflict: true }, |
dc9dc135 XL |
125 | } |
126 | } | |
dfeec247 | 127 | MonoItem::Static(..) | MonoItem::GlobalAsm(..) => { |
dc9dc135 XL |
128 | InstantiationMode::GloballyShared { may_conflict: false } |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | pub fn explicit_linkage(&self, tcx: TyCtxt<'tcx>) -> Option<Linkage> { | |
134 | let def_id = match *self { | |
135 | MonoItem::Fn(ref instance) => instance.def_id(), | |
136 | MonoItem::Static(def_id) => def_id, | |
137 | MonoItem::GlobalAsm(..) => return None, | |
138 | }; | |
139 | ||
140 | let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id); | |
141 | codegen_fn_attrs.linkage | |
142 | } | |
143 | ||
144 | /// Returns `true` if this instance is instantiable - whether it has no unsatisfied | |
145 | /// predicates. | |
146 | /// | |
147 | /// In order to codegen an item, all of its predicates must hold, because | |
148 | /// otherwise the item does not make sense. Type-checking ensures that | |
149 | /// the predicates of every item that is *used by* a valid item *do* | |
150 | /// hold, so we can rely on that. | |
151 | /// | |
152 | /// However, we codegen collector roots (reachable items) and functions | |
153 | /// in vtables when they are seen, even if they are not used, and so they | |
154 | /// might not be instantiable. For example, a programmer can define this | |
155 | /// public function: | |
156 | /// | |
157 | /// pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone { | |
158 | /// <&mut () as Clone>::clone(&s); | |
159 | /// } | |
160 | /// | |
161 | /// That function can't be codegened, because the method `<&mut () as Clone>::clone` | |
162 | /// does not exist. Luckily for us, that function can't ever be used, | |
163 | /// because that would require for `&'a mut (): Clone` to hold, so we | |
164 | /// can just not emit any code, or even a linker reference for it. | |
165 | /// | |
166 | /// Similarly, if a vtable method has such a signature, and therefore can't | |
167 | /// be used, we can just not emit it and have a placeholder (a null pointer, | |
168 | /// which will never be accessed) in its place. | |
169 | pub fn is_instantiable(&self, tcx: TyCtxt<'tcx>) -> bool { | |
170 | debug!("is_instantiable({:?})", self); | |
171 | let (def_id, substs) = match *self { | |
172 | MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs), | |
173 | MonoItem::Static(def_id) => (def_id, InternalSubsts::empty()), | |
174 | // global asm never has predicates | |
dfeec247 | 175 | MonoItem::GlobalAsm(..) => return true, |
dc9dc135 XL |
176 | }; |
177 | ||
3dfed10e | 178 | !tcx.subst_and_check_impossible_predicates((def_id, &substs)) |
dc9dc135 XL |
179 | } |
180 | ||
dc9dc135 XL |
181 | pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> { |
182 | match *self { | |
5099ac24 FG |
183 | MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(), |
184 | MonoItem::Static(def_id) => def_id.as_local(), | |
185 | MonoItem::GlobalAsm(item_id) => Some(item_id.def_id), | |
dfeec247 | 186 | } |
5099ac24 | 187 | .map(|def_id| tcx.def_span(def_id)) |
dc9dc135 | 188 | } |
cdc7bbd5 XL |
189 | |
190 | // Only used by rustc_codegen_cranelift | |
191 | pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode { | |
192 | crate::dep_graph::make_compile_mono_item(tcx, self) | |
193 | } | |
17df50a5 XL |
194 | |
195 | /// Returns the item's `CrateNum` | |
196 | pub fn krate(&self) -> CrateNum { | |
197 | match self { | |
198 | MonoItem::Fn(ref instance) => instance.def_id().krate, | |
199 | MonoItem::Static(def_id) => def_id.krate, | |
200 | MonoItem::GlobalAsm(..) => LOCAL_CRATE, | |
201 | } | |
202 | } | |
2c00a5a8 XL |
203 | } |
204 | ||
1b1a35ee XL |
205 | impl<'tcx> fmt::Display for MonoItem<'tcx> { |
206 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
207 | match *self { | |
208 | MonoItem::Fn(instance) => write!(f, "fn {}", instance), | |
209 | MonoItem::Static(def_id) => { | |
210 | write!(f, "static {}", Instance::new(def_id, InternalSubsts::empty())) | |
211 | } | |
212 | MonoItem::GlobalAsm(..) => write!(f, "global_asm"), | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
5869c6ff | 217 | #[derive(Debug)] |
ea8adc8c XL |
218 | pub struct CodegenUnit<'tcx> { |
219 | /// A name for this CGU. Incremental compilation requires that | |
9fa01778 | 220 | /// name be unique amongst **all** crates. Therefore, it should |
ea8adc8c XL |
221 | /// contain something unique to this crate (e.g., a module path) |
222 | /// as well as the crate name and disambiguator. | |
e74abb32 | 223 | name: Symbol, |
ff7c6d11 | 224 | items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>, |
2c00a5a8 | 225 | size_estimate: Option<usize>, |
17df50a5 | 226 | primary: bool, |
5099ac24 FG |
227 | /// True if this is CGU is used to hold code coverage information for dead code, |
228 | /// false otherwise. | |
229 | is_code_coverage_dead_code_cgu: bool, | |
ea8adc8c XL |
230 | } |
231 | ||
f9f354fc XL |
232 | /// Specifies the linkage type for a `MonoItem`. |
233 | /// | |
29967ef6 | 234 | /// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants. |
3dfed10e | 235 | #[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)] |
ea8adc8c XL |
236 | pub enum Linkage { |
237 | External, | |
238 | AvailableExternally, | |
239 | LinkOnceAny, | |
240 | LinkOnceODR, | |
241 | WeakAny, | |
242 | WeakODR, | |
243 | Appending, | |
244 | Internal, | |
245 | Private, | |
246 | ExternalWeak, | |
247 | Common, | |
248 | } | |
249 | ||
60c5eb7d | 250 | #[derive(Copy, Clone, PartialEq, Debug, HashStable)] |
ea8adc8c XL |
251 | pub enum Visibility { |
252 | Default, | |
253 | Hidden, | |
254 | Protected, | |
255 | } | |
256 | ||
ea8adc8c | 257 | impl<'tcx> CodegenUnit<'tcx> { |
17df50a5 | 258 | #[inline] |
e74abb32 | 259 | pub fn new(name: Symbol) -> CodegenUnit<'tcx> { |
5099ac24 FG |
260 | CodegenUnit { |
261 | name, | |
262 | items: Default::default(), | |
263 | size_estimate: None, | |
264 | primary: false, | |
265 | is_code_coverage_dead_code_cgu: false, | |
266 | } | |
ea8adc8c XL |
267 | } |
268 | ||
e74abb32 XL |
269 | pub fn name(&self) -> Symbol { |
270 | self.name | |
ea8adc8c XL |
271 | } |
272 | ||
e74abb32 | 273 | pub fn set_name(&mut self, name: Symbol) { |
ea8adc8c XL |
274 | self.name = name; |
275 | } | |
276 | ||
17df50a5 XL |
277 | pub fn is_primary(&self) -> bool { |
278 | self.primary | |
279 | } | |
280 | ||
281 | pub fn make_primary(&mut self) { | |
282 | self.primary = true; | |
283 | } | |
284 | ||
ff7c6d11 | 285 | pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { |
ea8adc8c XL |
286 | &self.items |
287 | } | |
288 | ||
dfeec247 | 289 | pub fn items_mut(&mut self) -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { |
ea8adc8c XL |
290 | &mut self.items |
291 | } | |
ff7c6d11 | 292 | |
5099ac24 FG |
293 | pub fn is_code_coverage_dead_code_cgu(&self) -> bool { |
294 | self.is_code_coverage_dead_code_cgu | |
295 | } | |
296 | ||
297 | /// Marks this CGU as the one used to contain code coverage information for dead code. | |
298 | pub fn make_code_coverage_dead_code_cgu(&mut self) { | |
299 | self.is_code_coverage_dead_code_cgu = true; | |
300 | } | |
301 | ||
ff7c6d11 XL |
302 | pub fn mangle_name(human_readable_name: &str) -> String { |
303 | // We generate a 80 bit hash from the name. This should be enough to | |
304 | // avoid collisions and is still reasonably short for filenames. | |
305 | let mut hasher = StableHasher::new(); | |
306 | human_readable_name.hash(&mut hasher); | |
307 | let hash: u128 = hasher.finish(); | |
308 | let hash = hash & ((1u128 << 80) - 1); | |
309 | base_n::encode(hash, base_n::CASE_INSENSITIVE) | |
310 | } | |
2c00a5a8 | 311 | |
dc9dc135 | 312 | pub fn estimate_size(&mut self, tcx: TyCtxt<'tcx>) { |
2c00a5a8 XL |
313 | // Estimate the size of a codegen unit as (approximately) the number of MIR |
314 | // statements it corresponds to. | |
315 | self.size_estimate = Some(self.items.keys().map(|mi| mi.size_estimate(tcx)).sum()); | |
316 | } | |
317 | ||
17df50a5 | 318 | #[inline] |
2c00a5a8 XL |
319 | pub fn size_estimate(&self) -> usize { |
320 | // Should only be called if `estimate_size` has previously been called. | |
321 | self.size_estimate.expect("estimate_size must be called before getting a size_estimate") | |
322 | } | |
323 | ||
324 | pub fn modify_size_estimate(&mut self, delta: usize) { | |
325 | assert!(self.size_estimate.is_some()); | |
326 | if let Some(size_estimate) = self.size_estimate { | |
327 | self.size_estimate = Some(size_estimate + delta); | |
328 | } | |
329 | } | |
dc9dc135 XL |
330 | |
331 | pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool { | |
332 | self.items().contains_key(item) | |
333 | } | |
334 | ||
335 | pub fn work_product_id(&self) -> WorkProductId { | |
a2a8927a | 336 | WorkProductId::from_cgu_name(self.name().as_str()) |
dc9dc135 XL |
337 | } |
338 | ||
923072b8 | 339 | pub fn previous_work_product(&self, tcx: TyCtxt<'_>) -> WorkProduct { |
dc9dc135 XL |
340 | let work_product_id = self.work_product_id(); |
341 | tcx.dep_graph | |
dfeec247 XL |
342 | .previous_work_product(&work_product_id) |
343 | .unwrap_or_else(|| panic!("Could not find work-product for CGU `{}`", self.name())) | |
dc9dc135 XL |
344 | } |
345 | ||
346 | pub fn items_in_deterministic_order( | |
347 | &self, | |
348 | tcx: TyCtxt<'tcx>, | |
349 | ) -> Vec<(MonoItem<'tcx>, (Linkage, Visibility))> { | |
350 | // The codegen tests rely on items being process in the same order as | |
351 | // they appear in the file, so for local items, we sort by node_id first | |
352 | #[derive(PartialEq, Eq, PartialOrd, Ord)] | |
a2a8927a | 353 | pub struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>); |
dc9dc135 | 354 | |
3dfed10e | 355 | fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> { |
dfeec247 XL |
356 | ItemSortKey( |
357 | match item { | |
358 | MonoItem::Fn(ref instance) => { | |
359 | match instance.def { | |
360 | // We only want to take HirIds of user-defined | |
361 | // instances into account. The others don't matter for | |
362 | // the codegen tests and can even make item order | |
363 | // unstable. | |
5e7ed085 | 364 | InstanceDef::Item(def) => def.did.as_local().map(Idx::index), |
dfeec247 XL |
365 | InstanceDef::VtableShim(..) |
366 | | InstanceDef::ReifyShim(..) | |
367 | | InstanceDef::Intrinsic(..) | |
368 | | InstanceDef::FnPtrShim(..) | |
369 | | InstanceDef::Virtual(..) | |
370 | | InstanceDef::ClosureOnceShim { .. } | |
371 | | InstanceDef::DropGlue(..) | |
372 | | InstanceDef::CloneShim(..) => None, | |
dc9dc135 XL |
373 | } |
374 | } | |
5e7ed085 FG |
375 | MonoItem::Static(def_id) => def_id.as_local().map(Idx::index), |
376 | MonoItem::GlobalAsm(item_id) => Some(item_id.def_id.index()), | |
dfeec247 XL |
377 | }, |
378 | item.symbol_name(tcx), | |
379 | ) | |
dc9dc135 XL |
380 | } |
381 | ||
382 | let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect(); | |
383 | items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); | |
384 | items | |
385 | } | |
386 | ||
387 | pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode { | |
5869c6ff | 388 | crate::dep_graph::make_compile_codegen_unit(tcx, self.name()) |
dc9dc135 | 389 | } |
ea8adc8c XL |
390 | } |
391 | ||
0531ce1d | 392 | impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for CodegenUnit<'tcx> { |
e74abb32 | 393 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
ea8adc8c XL |
394 | let CodegenUnit { |
395 | ref items, | |
396 | name, | |
2c00a5a8 XL |
397 | // The size estimate is not relevant to the hash |
398 | size_estimate: _, | |
17df50a5 | 399 | primary: _, |
5099ac24 | 400 | is_code_coverage_dead_code_cgu, |
ea8adc8c XL |
401 | } = *self; |
402 | ||
403 | name.hash_stable(hcx, hasher); | |
5099ac24 | 404 | is_code_coverage_dead_code_cgu.hash_stable(hcx, hasher); |
ea8adc8c | 405 | |
dfeec247 XL |
406 | let mut items: Vec<(Fingerprint, _)> = items |
407 | .iter() | |
408 | .map(|(mono_item, &attrs)| { | |
409 | let mut hasher = StableHasher::new(); | |
410 | mono_item.hash_stable(hcx, &mut hasher); | |
411 | let mono_item_fingerprint = hasher.finish(); | |
412 | (mono_item_fingerprint, attrs) | |
413 | }) | |
414 | .collect(); | |
ea8adc8c XL |
415 | |
416 | items.sort_unstable_by_key(|i| i.0); | |
417 | items.hash_stable(hcx, hasher); | |
418 | } | |
419 | } | |
420 | ||
dc9dc135 XL |
421 | pub struct CodegenUnitNameBuilder<'tcx> { |
422 | tcx: TyCtxt<'tcx>, | |
b7449926 XL |
423 | cache: FxHashMap<CrateNum, String>, |
424 | } | |
425 | ||
a2a8927a | 426 | impl<'tcx> CodegenUnitNameBuilder<'tcx> { |
dc9dc135 | 427 | pub fn new(tcx: TyCtxt<'tcx>) -> Self { |
dfeec247 | 428 | CodegenUnitNameBuilder { tcx, cache: Default::default() } |
b7449926 XL |
429 | } |
430 | ||
431 | /// CGU names should fulfill the following requirements: | |
432 | /// - They should be able to act as a file name on any kind of file system | |
433 | /// - They should not collide with other CGU names, even for different versions | |
434 | /// of the same crate. | |
435 | /// | |
436 | /// Consequently, we don't use special characters except for '.' and '-' and we | |
437 | /// prefix each name with the crate-name and crate-disambiguator. | |
438 | /// | |
439 | /// This function will build CGU names of the form: | |
440 | /// | |
04454e1e | 441 | /// ```text |
b7449926 XL |
442 | /// <crate-name>.<crate-disambiguator>[-in-<local-crate-id>](-<component>)*[.<special-suffix>] |
443 | /// <local-crate-id> = <local-crate-name>.<local-crate-disambiguator> | |
444 | /// ``` | |
445 | /// | |
446 | /// The '.' before `<special-suffix>` makes sure that names with a special | |
447 | /// suffix can never collide with a name built out of regular Rust | |
0731742a | 448 | /// identifiers (e.g., module paths). |
dfeec247 XL |
449 | pub fn build_cgu_name<I, C, S>( |
450 | &mut self, | |
451 | cnum: CrateNum, | |
452 | components: I, | |
453 | special_suffix: Option<S>, | |
454 | ) -> Symbol | |
455 | where | |
456 | I: IntoIterator<Item = C>, | |
457 | C: fmt::Display, | |
458 | S: fmt::Display, | |
b7449926 | 459 | { |
dfeec247 | 460 | let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix); |
b7449926 XL |
461 | |
462 | if self.tcx.sess.opts.debugging_opts.human_readable_cgu_names { | |
463 | cgu_name | |
464 | } else { | |
a2a8927a | 465 | Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str())) |
b7449926 XL |
466 | } |
467 | } | |
468 | ||
469 | /// Same as `CodegenUnit::build_cgu_name()` but will never mangle the | |
470 | /// resulting name. | |
dfeec247 XL |
471 | pub fn build_cgu_name_no_mangle<I, C, S>( |
472 | &mut self, | |
473 | cnum: CrateNum, | |
474 | components: I, | |
475 | special_suffix: Option<S>, | |
476 | ) -> Symbol | |
477 | where | |
478 | I: IntoIterator<Item = C>, | |
479 | C: fmt::Display, | |
480 | S: fmt::Display, | |
b7449926 XL |
481 | { |
482 | use std::fmt::Write; | |
483 | ||
484 | let mut cgu_name = String::with_capacity(64); | |
485 | ||
486 | // Start out with the crate name and disambiguator | |
487 | let tcx = self.tcx; | |
488 | let crate_prefix = self.cache.entry(cnum).or_insert_with(|| { | |
489 | // Whenever the cnum is not LOCAL_CRATE we also mix in the | |
490 | // local crate's ID. Otherwise there can be collisions between CGUs | |
491 | // instantiating stuff for upstream crates. | |
492 | let local_crate_id = if cnum != LOCAL_CRATE { | |
136023e0 XL |
493 | let local_stable_crate_id = tcx.sess.local_stable_crate_id(); |
494 | format!( | |
495 | "-in-{}.{:08x}", | |
496 | tcx.crate_name(LOCAL_CRATE), | |
497 | local_stable_crate_id.to_u64() as u32, | |
498 | ) | |
b7449926 XL |
499 | } else { |
500 | String::new() | |
501 | }; | |
502 | ||
136023e0 XL |
503 | let stable_crate_id = tcx.sess.local_stable_crate_id(); |
504 | format!( | |
505 | "{}.{:08x}{}", | |
506 | tcx.crate_name(cnum), | |
507 | stable_crate_id.to_u64() as u32, | |
508 | local_crate_id, | |
509 | ) | |
b7449926 XL |
510 | }); |
511 | ||
512 | write!(cgu_name, "{}", crate_prefix).unwrap(); | |
513 | ||
514 | // Add the components | |
515 | for component in components { | |
516 | write!(cgu_name, "-{}", component).unwrap(); | |
517 | } | |
518 | ||
519 | if let Some(special_suffix) = special_suffix { | |
520 | // We add a dot in here so it cannot clash with anything in a regular | |
521 | // Rust identifier | |
522 | write!(cgu_name, ".{}", special_suffix).unwrap(); | |
523 | } | |
524 | ||
a2a8927a | 525 | Symbol::intern(&cgu_name) |
b7449926 XL |
526 | } |
527 | } |