]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / compiler / rustc_metadata / src / rmeta / decoder / cstore_impl.rs
CommitLineData
60c5eb7d 1use crate::creader::{CStore, LoadedMacro};
dfeec247 2use crate::foreign_modules;
9fa01778 3use crate::native_libs;
9ffffee4
FG
4use crate::rmeta::table::IsDefault;
5use crate::rmeta::AttrFlags;
92a42be0 6
3dfed10e 7use rustc_ast as ast;
923072b8 8use rustc_attr::Deprecation;
5099ac24 9use rustc_hir::def::{CtorKind, DefKind, Res};
04454e1e 10use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
ba9703b0 11use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
923072b8 12use rustc_middle::arena::ArenaAllocatable;
5099ac24 13use rustc_middle::metadata::ModChild;
ba9703b0 14use rustc_middle::middle::exported_symbols::ExportedSymbol;
923072b8 15use rustc_middle::middle::stability::DeprecationEntry;
353b0b11 16use rustc_middle::query::LocalCrate;
5099ac24 17use rustc_middle::ty::fast_reject::SimplifiedType;
3c0e092e 18use rustc_middle::ty::query::{ExternProviders, Providers};
353b0b11
FG
19use rustc_middle::ty::{self, TyCtxt};
20use rustc_session::cstore::CrateStore;
136023e0
XL
21use rustc_session::{Session, StableCrateId};
22use rustc_span::hygiene::{ExpnHash, ExpnId};
a2a8927a 23use rustc_span::symbol::{kw, Symbol};
353b0b11 24use rustc_span::Span;
92a42be0 25
dfeec247 26use rustc_data_structures::sync::Lrc;
8bb4bdeb 27use std::any::Any;
92a42be0 28
923072b8
FG
29use super::{Decodable, DecodeContext, DecodeIterator};
30
31trait ProcessQueryValue<'tcx, T> {
32 fn process_decoded(self, _tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> T;
33}
34
35impl<T> ProcessQueryValue<'_, Option<T>> for Option<T> {
36 #[inline(always)]
37 fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<T> {
38 self
39 }
40}
41
42impl<T> ProcessQueryValue<'_, T> for Option<T> {
43 #[inline(always)]
44 fn process_decoded(self, _tcx: TyCtxt<'_>, err: impl Fn() -> !) -> T {
45 if let Some(value) = self { value } else { err() }
46 }
47}
48
49impl<'tcx, T: ArenaAllocatable<'tcx>> ProcessQueryValue<'tcx, &'tcx T> for Option<T> {
50 #[inline(always)]
51 fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx T {
52 if let Some(value) = self { tcx.arena.alloc(value) } else { err() }
53 }
54}
55
56impl<T, E> ProcessQueryValue<'_, Result<Option<T>, E>> for Option<T> {
57 #[inline(always)]
58 fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Result<Option<T>, E> {
59 Ok(self)
60 }
61}
62
63impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'tcx, &'tcx [T]>
64 for Option<DecodeIterator<'a, 'tcx, T>>
65{
66 #[inline(always)]
67 fn process_decoded(self, tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> &'tcx [T] {
68 if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { &[] }
69 }
70}
71
72impl ProcessQueryValue<'_, Option<DeprecationEntry>> for Option<Deprecation> {
73 #[inline(always)]
74 fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<DeprecationEntry> {
75 self.map(DeprecationEntry::external)
76 }
77}
78
5e7ed085 79macro_rules! provide_one {
f2b60f7d 80 ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table }) => {
5e7ed085 81 provide_one! {
f2b60f7d 82 $tcx, $def_id, $other, $cdata, $name => {
923072b8
FG
83 $cdata
84 .root
85 .tables
86 .$name
87 .get($cdata, $def_id.index)
88 .map(|lazy| lazy.decode(($cdata, $tcx)))
89 .process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
90 }
91 }
92 };
9ffffee4
FG
93 ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_defaulted_array }) => {
94 provide_one! {
95 $tcx, $def_id, $other, $cdata, $name => {
96 let lazy = $cdata.root.tables.$name.get($cdata, $def_id.index);
97 if lazy.is_default() { &[] } else { $tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx))) }
98 }
99 }
100 };
f2b60f7d 101 ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_direct }) => {
923072b8 102 provide_one! {
f2b60f7d 103 $tcx, $def_id, $other, $cdata, $name => {
923072b8
FG
104 // We don't decode `table_direct`, since it's not a Lazy, but an actual value
105 $cdata
106 .root
107 .tables
108 .$name
109 .get($cdata, $def_id.index)
110 .process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
5e7ed085
FG
111 }
112 }
113 };
f2b60f7d
FG
114 ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => $compute:block) => {
115 fn $name<'tcx>(
116 $tcx: TyCtxt<'tcx>,
117 def_id_arg: ty::query::query_keys::$name<'tcx>,
9ffffee4 118 ) -> ty::query::query_provided::$name<'tcx> {
5e7ed085
FG
119 let _prof_timer =
120 $tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name)));
121
122 #[allow(unused_variables)]
123 let ($def_id, $other) = def_id_arg.into_args();
124 assert!(!$def_id.is_local());
125
126 // External query providers call `crate_hash` in order to register a dependency
127 // on the crate metadata. The exception is `crate_hash` itself, which obviously
128 // doesn't need to do this (and can't, as it would cause a query cycle).
129 use rustc_middle::dep_graph::DepKind;
130 if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() {
131 $tcx.ensure().crate_hash($def_id.krate);
132 }
133
9ffffee4
FG
134 let cdata = rustc_data_structures::sync::MappedReadGuard::map(CStore::from_tcx($tcx), |c| {
135 c.get_crate_data($def_id.krate).cdata
136 });
137 let $cdata = crate::creader::CrateMetadataRef {
138 cdata: &cdata,
139 cstore: &CStore::from_tcx($tcx),
140 };
5e7ed085
FG
141
142 $compute
143 }
144 };
145}
146
8bb4bdeb 147macro_rules! provide {
f2b60f7d 148 ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
5e7ed085 149 $($name:ident => { $($compute:tt)* })*) => {
3c0e092e 150 pub fn provide_extern(providers: &mut ExternProviders) {
5e7ed085 151 $(provide_one! {
f2b60f7d 152 $tcx, $def_id, $other, $cdata, $name => { $($compute)* }
8bb4bdeb
XL
153 })*
154
3c0e092e 155 *providers = ExternProviders {
8bb4bdeb
XL
156 $($name,)*
157 ..*providers
158 };
159 }
9e0c209e 160 }
8bb4bdeb 161}
9e0c209e 162
ea8adc8c
XL
163// small trait to work around different signature queries all being defined via
164// the macro above.
165trait IntoArgs {
5e7ed085
FG
166 type Other;
167 fn into_args(self) -> (DefId, Self::Other);
ea8adc8c
XL
168}
169
170impl IntoArgs for DefId {
5e7ed085
FG
171 type Other = ();
172 fn into_args(self) -> (DefId, ()) {
173 (self, ())
dfeec247 174 }
ea8adc8c
XL
175}
176
177impl IntoArgs for CrateNum {
5e7ed085
FG
178 type Other = ();
179 fn into_args(self) -> (DefId, ()) {
180 (self.as_def_id(), ())
dfeec247 181 }
ea8adc8c
XL
182}
183
184impl IntoArgs for (CrateNum, DefId) {
5e7ed085 185 type Other = DefId;
dfeec247
XL
186 fn into_args(self) -> (DefId, DefId) {
187 (self.0.as_def_id(), self.1)
188 }
ea8adc8c
XL
189}
190
a2a8927a 191impl<'tcx> IntoArgs for ty::InstanceDef<'tcx> {
5e7ed085
FG
192 type Other = ();
193 fn into_args(self) -> (DefId, ()) {
194 (self.def_id(), ())
195 }
196}
197
198impl IntoArgs for (CrateNum, SimplifiedType) {
199 type Other = SimplifiedType;
200 fn into_args(self) -> (DefId, SimplifiedType) {
201 (self.0.as_def_id(), self.1)
c295e0f8
XL
202 }
203}
204
f2b60f7d 205provide! { tcx, def_id, other, cdata,
9ffffee4 206 explicit_item_bounds => { table_defaulted_array }
5e7ed085
FG
207 explicit_predicates_of => { table }
208 generics_of => { table }
9ffffee4 209 inferred_outlives_of => { table_defaulted_array }
5e7ed085
FG
210 super_predicates_of => { table }
211 type_of => { table }
212 variances_of => { table }
213 fn_sig => { table }
04454e1e 214 codegen_fn_attrs => { table }
5e7ed085
FG
215 impl_trait_ref => { table }
216 const_param_default => { table }
f2b60f7d 217 object_lifetime_default => { table }
5e7ed085
FG
218 thir_abstract_const => { table }
219 optimized_mir => { table }
220 mir_for_ctfe => { table }
9ffffee4 221 mir_generator_witnesses => { table }
5e7ed085
FG
222 promoted_mir => { table }
223 def_span => { table }
224 def_ident_span => { table }
225 lookup_stability => { table }
226 lookup_const_stability => { table }
f2b60f7d 227 lookup_default_body_stability => { table }
5e7ed085 228 lookup_deprecation_entry => { table }
2b03887a 229 params_in_repr => { table }
353b0b11 230 unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) }
923072b8 231 opt_def_kind => { table_direct }
5e7ed085 232 impl_parent => { table }
923072b8
FG
233 impl_polarity => { table_direct }
234 impl_defaultness => { table_direct }
235 constness => { table_direct }
5e7ed085
FG
236 coerce_unsized_info => { table }
237 mir_const_qualif => { table }
238 rendered_const => { table }
923072b8 239 asyncness => { table_direct }
5e7ed085
FG
240 fn_arg_names => { table }
241 generator_kind => { table }
242 trait_def => { table }
2b03887a 243 deduced_param_attrs => { table }
487cf647
FG
244 is_type_alias_impl_trait => {
245 debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy);
9ffffee4 246 cdata.root.tables.is_type_alias_impl_trait.get(cdata, def_id.index)
487cf647 247 }
9c376795 248 collect_return_position_impl_trait_in_trait_tys => {
2b03887a
FG
249 Ok(cdata
250 .root
251 .tables
252 .trait_impl_trait_tys
253 .get(cdata, def_id.index)
254 .map(|lazy| lazy.decode((cdata, tcx)))
9c376795 255 .process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
353b0b11
FG
256 }
257 implied_predicates_of => {
258 cdata
259 .root
260 .tables
261 .implied_predicates_of
262 .get(cdata, def_id.index)
263 .map(|lazy| lazy.decode((cdata, tcx)))
264 .unwrap_or_else(|| {
265 debug_assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
266 tcx.super_predicates_of(def_id)
267 })
268 }
5e7ed085 269
353b0b11 270 associated_types_for_impl_traits_in_associated_fn => { table_defaulted_array }
9ffffee4 271
f2b60f7d 272 visibility => { cdata.get_visibility(def_id.index) }
8bb4bdeb
XL
273 adt_def => { cdata.get_adt_def(def_id.index, tcx) }
274 adt_destructor => {
275 let _ = cdata;
29967ef6 276 tcx.calculate_dtor(def_id, |_,_| Ok(()))
92a42be0 277 }
04454e1e
FG
278 associated_item_def_ids => {
279 tcx.arena.alloc_from_iter(cdata.get_associated_item_def_ids(def_id.index, tcx.sess))
280 }
f2b60f7d 281 associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
dc9dc135 282 inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
cc61c64b 283 is_foreign_item => { cdata.is_foreign_item(def_id.index) }
a2a8927a 284 item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
7cac9316 285 is_mir_available => { cdata.is_item_mir_available(def_id.index) }
5869c6ff 286 is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
041b39d2 287
dc9dc135 288 dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
17df50a5 289 is_private_dep => { cdata.private_dep }
94b46f34
XL
290 is_panic_runtime => { cdata.root.panic_runtime }
291 is_compiler_builtins => { cdata.root.compiler_builtins }
292 has_global_allocator => { cdata.root.has_global_allocator }
487cf647 293 has_alloc_error_handler => { cdata.root.has_alloc_error_handler }
b7449926 294 has_panic_handler => { cdata.root.has_panic_handler }
94b46f34 295 is_profiler_runtime => { cdata.root.profiler_runtime }
064997fb 296 required_panic_strategy => { cdata.root.required_panic_strategy }
c295e0f8 297 panic_in_drop_strategy => { cdata.root.panic_in_drop_strategy }
0531ce1d 298 extern_crate => {
dc9dc135
XL
299 let r = *cdata.extern_crate.lock();
300 r.map(|c| &*tcx.arena.alloc(c))
0531ce1d 301 }
94b46f34 302 is_no_builtins => { cdata.root.no_builtins }
dc9dc135 303 symbol_mangling_version => { cdata.root.symbol_mangling_version }
0531ce1d
XL
304 reachable_non_generics => {
305 let reachable_non_generics = tcx
306 .exported_symbols(cdata.cnum)
307 .iter()
04454e1e 308 .filter_map(|&(exported_symbol, export_info)| {
0531ce1d 309 if let ExportedSymbol::NonGeneric(def_id) = exported_symbol {
04454e1e 310 Some((def_id, export_info))
0531ce1d
XL
311 } else {
312 None
313 }
314 })
315 .collect();
316
f9f354fc 317 reachable_non_generics
0531ce1d 318 }
5099ac24
FG
319 native_libraries => { cdata.get_native_libraries(tcx.sess).collect() }
320 foreign_modules => { cdata.get_foreign_modules(tcx.sess).map(|m| (m.def_id, m)).collect() }
94b46f34 321 crate_hash => { cdata.root.hash }
60c5eb7d 322 crate_host_hash => { cdata.host_hash }
17df50a5 323 crate_name => { cdata.root.name }
ea8adc8c 324
83c7162d
XL
325 extra_filename => { cdata.root.extra_filename.clone() }
326
a2a8927a 327 traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
9ffffee4 328 trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
a2a8927a 329 implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
5e7ed085 330 crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
ea8adc8c 331
0531ce1d
XL
332 dep_kind => {
333 let r = *cdata.dep_kind.lock();
334 r
335 }
5099ac24 336 module_children => {
487cf647 337 tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess))
ea8adc8c 338 }
dc9dc135 339 defined_lib_features => { cdata.get_lib_features(tcx) }
064997fb
FG
340 stability_implications => {
341 cdata.get_stability_implications(tcx).iter().copied().collect()
342 }
923072b8 343 is_intrinsic => { cdata.get_is_intrinsic(def_id.index) }
5e7ed085 344 defined_lang_items => { cdata.get_lang_items(tcx) }
f9f354fc 345 diagnostic_items => { cdata.get_diagnostic_items() }
dc9dc135 346 missing_lang_items => { cdata.get_missing_lang_items(tcx) }
ea8adc8c 347
ea8adc8c 348 missing_extern_crate_item => {
29967ef6 349 let r = matches!(*cdata.extern_crate.borrow(), Some(extern_crate) if !extern_crate.is_direct());
0531ce1d 350 r
ea8adc8c
XL
351 }
352
5099ac24 353 used_crate_source => { Lrc::clone(&cdata.source) }
04454e1e 354 debugger_visualizers => { cdata.get_debugger_visualizers() }
ea8adc8c 355
e74abb32
XL
356 exported_symbols => {
357 let syms = cdata.exported_symbols(tcx);
358
359 // FIXME rust-lang/rust#64319, rust-lang/rust#64872: We want
360 // to block export of generics from dylibs, but we must fix
361 // rust-lang/rust#65890 before we can do that robustly.
362
ba9703b0 363 syms
e74abb32 364 }
f035d41b
XL
365
366 crate_extern_paths => { cdata.source().paths().cloned().collect() }
29967ef6 367 expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
04454e1e 368 generator_diagnostic_data => { cdata.get_generator_diagnostic_data(tcx, def_id.index) }
9ffffee4
FG
369 is_doc_hidden => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
370 doc_link_resolutions => { tcx.arena.alloc(cdata.get_doc_link_resolutions(def_id.index)) }
371 doc_link_traits_in_scope => {
372 tcx.arena.alloc_from_iter(cdata.get_doc_link_traits_in_scope(def_id.index))
373 }
041b39d2
XL
374}
375
a2a8927a 376pub(in crate::rmeta) fn provide(providers: &mut Providers) {
ea8adc8c
XL
377 // FIXME(#44234) - almost all of these queries have no sub-queries and
378 // therefore no actual inputs, they're just reading tables calculated in
379 // resolve! Does this work? Unsure! That's what the issue is about
041b39d2 380 *providers = Providers {
136023e0 381 allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),
487cf647 382 alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),
353b0b11 383 is_private_dep: |_tcx, LocalCrate| false,
f2b60f7d 384 native_library: |tcx, id| {
ea8adc8c
XL
385 tcx.native_libraries(id.krate)
386 .iter()
387 .filter(|lib| native_libs::relevant_lib(&tcx.sess, lib))
0531ce1d 388 .find(|lib| {
5e7ed085
FG
389 let Some(fm_id) = lib.foreign_module else {
390 return false;
0531ce1d 391 };
29967ef6
XL
392 let map = tcx.foreign_modules(id.krate);
393 map.get(&fm_id)
0531ce1d
XL
394 .expect("failed to find foreign module")
395 .foreign_items
396 .contains(&id)
397 })
ea8adc8c 398 },
353b0b11
FG
399 native_libraries: |tcx, LocalCrate| native_libs::collect(tcx),
400 foreign_modules: |tcx, LocalCrate| {
5099ac24 401 foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect()
ea8adc8c 402 },
ea8adc8c 403
0731742a 404 // Returns a map from a sufficiently visible external item (i.e., an
ea8adc8c
XL
405 // external item that is visible from at least one local module) to a
406 // sufficiently visible parent (considering modules that re-export the
407 // external item to be parents).
17df50a5 408 visible_parent_map: |tcx, ()| {
ea8adc8c 409 use std::collections::hash_map::Entry;
dfeec247 410 use std::collections::vec_deque::VecDeque;
ea8adc8c 411
a1dfa0c6 412 let mut visible_parent_map: DefIdMap<DefId> = Default::default();
064997fb
FG
413 // This is a secondary visible_parent_map, storing the DefId of
414 // parents that re-export the child as `_` or module parents
415 // which are `#[doc(hidden)]`. Since we prefer paths that don't
416 // do this, merge this map at the end, only if we're missing
417 // keys from the former.
418 // This is a rudimentary check that does not catch all cases,
419 // just the easiest.
9c376795 420 let mut fallback_map: Vec<(DefId, DefId)> = Default::default();
ea8adc8c 421
ff7c6d11
XL
422 // Issue 46112: We want the map to prefer the shortest
423 // paths when reporting the path to an item. Therefore we
424 // build up the map via a breadth-first search (BFS),
425 // which naturally yields minimal-length paths.
426 //
427 // Note that it needs to be a BFS over the whole forest of
428 // crates, not just each individual crate; otherwise you
429 // only get paths that are locally minimal with respect to
430 // whatever crate we happened to encounter first in this
431 // traversal, but not globally minimal across all crates.
432 let bfs_queue = &mut VecDeque::new();
433
c295e0f8 434 for &cnum in tcx.crates(()) {
ea8adc8c
XL
435 // Ignore crates without a corresponding local `extern crate` item.
436 if tcx.missing_extern_crate_item(cnum) {
dfeec247 437 continue;
ea8adc8c
XL
438 }
439
04454e1e 440 bfs_queue.push_back(cnum.as_def_id());
ff7c6d11
XL
441 }
442
5099ac24
FG
443 let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| {
444 if !child.vis.is_public() {
c295e0f8
XL
445 return;
446 }
ea8adc8c 447
5099ac24
FG
448 if let Some(def_id) = child.res.opt_def_id() {
449 if child.ident.name == kw::Underscore {
9c376795 450 fallback_map.push((def_id, parent));
a2a8927a
XL
451 return;
452 }
453
9ffffee4 454 if tcx.is_doc_hidden(parent) {
9c376795 455 fallback_map.push((def_id, parent));
064997fb
FG
456 return;
457 }
458
5099ac24 459 match visible_parent_map.entry(def_id) {
c295e0f8
XL
460 Entry::Occupied(mut entry) => {
461 // If `child` is defined in crate `cnum`, ensure
462 // that it is mapped to a parent in `cnum`.
5099ac24 463 if def_id.is_local() && entry.get().is_local() {
c295e0f8 464 entry.insert(parent);
dc9dc135 465 }
ea8adc8c 466 }
c295e0f8
XL
467 Entry::Vacant(entry) => {
468 entry.insert(parent);
5099ac24
FG
469 if matches!(
470 child.res,
471 Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, _)
472 ) {
473 bfs_queue.push_back(def_id);
474 }
c295e0f8 475 }
ea8adc8c
XL
476 }
477 }
c295e0f8
XL
478 };
479
480 while let Some(def) = bfs_queue.pop_front() {
5099ac24 481 for child in tcx.module_children(def).iter() {
c295e0f8
XL
482 add_child(bfs_queue, child, def);
483 }
ea8adc8c
XL
484 }
485
064997fb
FG
486 // Fill in any missing entries with the less preferable path.
487 // If this path re-exports the child as `_`, we still use this
488 // path in a diagnostic that suggests importing `::*`.
9c376795 489
a2a8927a
XL
490 for (child, parent) in fallback_map {
491 visible_parent_map.entry(child).or_insert(parent);
492 }
493
f9f354fc 494 visible_parent_map
ea8adc8c
XL
495 },
496
17df50a5 497 dependency_formats: |tcx, ()| Lrc::new(crate::dependency_format::calculate(tcx)),
353b0b11
FG
498 has_global_allocator: |tcx, LocalCrate| CStore::from_tcx(tcx).has_global_allocator(),
499 has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
17df50a5
XL
500 postorder_cnums: |tcx, ()| {
501 tcx.arena
502 .alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
60c5eb7d 503 },
353b0b11
FG
504 crates: |tcx, ()| {
505 // The list of loaded crates is now frozen in query cache,
506 // so make sure cstore is not mutably accessed from here on.
507 tcx.untracked().cstore.leak();
508 tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
509 },
041b39d2
XL
510 ..*providers
511 };
8bb4bdeb 512}
92a42be0 513
60c5eb7d 514impl CStore {
487cf647
FG
515 pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
516 self.get_crate_data(def.krate).get_ctor(def.index)
17df50a5
XL
517 }
518
487cf647
FG
519 pub fn module_children_untracked<'a>(
520 &'a self,
521 def_id: DefId,
522 sess: &'a Session,
523 ) -> impl Iterator<Item = ModChild> + 'a {
524 self.get_crate_data(def_id.krate).get_module_children(def_id.index, sess)
92a42be0
SL
525 }
526
b7449926 527 pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {
e74abb32
XL
528 let _prof_timer = sess.prof.generic_activity("metadata_load_macro");
529
476ff2be 530 let data = self.get_crate_data(id.krate);
60c5eb7d 531 if data.root.is_proc_macro_crate() {
136023e0 532 return LoadedMacro::ProcMacro(data.load_proc_macro(id.index, sess));
476ff2be
SL
533 }
534
ba9703b0 535 let span = data.get_span(id.index, sess);
476ff2be 536
dfeec247
XL
537 LoadedMacro::MacroDef(
538 ast::Item {
a2a8927a 539 ident: data.item_ident(id.index, sess),
dfeec247 540 id: ast::DUMMY_NODE_ID,
ba9703b0 541 span,
a2a8927a 542 attrs: data.get_item_attrs(id.index, sess).collect(),
ba9703b0 543 kind: ast::ItemKind::MacroDef(data.get_macro(id.index, sess)),
1b1a35ee
XL
544 vis: ast::Visibility {
545 span: span.shrink_to_lo(),
546 kind: ast::VisibilityKind::Inherited,
547 tokens: None,
548 },
dfeec247
XL
549 tokens: None,
550 },
551 data.root.edition,
552 )
476ff2be
SL
553 }
554
353b0b11 555 pub fn def_span_untracked(&self, def_id: DefId, sess: &Session) -> Span {
dfeec247
XL
556 self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
557 }
558
353b0b11 559 pub fn def_kind_untracked(&self, def: DefId) -> DefKind {
136023e0
XL
560 self.get_crate_data(def.krate).def_kind(def.index)
561 }
562
353b0b11
FG
563 pub fn expn_that_defined_untracked(&self, def_id: DefId, sess: &Session) -> ExpnId {
564 self.get_crate_data(def_id.krate).get_expn_that_defined(def_id.index, sess)
3dfed10e 565 }
fc512014 566
17df50a5
XL
567 /// Only public-facing way to traverse all the definitions in a non-local crate.
568 /// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
569 /// See <https://github.com/rust-lang/rust/pull/85889> for context.
570 pub fn num_def_ids_untracked(&self, cnum: CrateNum) -> usize {
fc512014
XL
571 self.get_crate_data(cnum).num_def_ids()
572 }
6a06907d 573
17df50a5
XL
574 pub fn get_proc_macro_quoted_span_untracked(
575 &self,
576 cnum: CrateNum,
577 id: usize,
578 sess: &Session,
579 ) -> Span {
580 self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
581 }
b7449926
XL
582}
583
60c5eb7d
XL
584impl CrateStore for CStore {
585 fn as_any(&self) -> &dyn Any {
586 self
b7449926 587 }
9c376795
FG
588 fn untracked_as_any(&mut self) -> &mut dyn Any {
589 self
590 }
b7449926 591
136023e0 592 fn crate_name(&self, cnum: CrateNum) -> Symbol {
e74abb32 593 self.get_crate_data(cnum).root.name
b7449926
XL
594 }
595
136023e0
XL
596 fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId {
597 self.get_crate_data(cnum).root.stable_crate_id
b7449926
XL
598 }
599
c295e0f8 600 fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum {
353b0b11
FG
601 *self
602 .stable_crate_ids
603 .get(&stable_crate_id)
604 .unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
c295e0f8
XL
605 }
606
b7449926
XL
607 /// Returns the `DefKey` for a given `DefId`. This indicates the
608 /// parent `DefId` as well as some idea of what kind of data the
609 /// `DefId` refers to.
610 fn def_key(&self, def: DefId) -> DefKey {
b7449926
XL
611 self.get_crate_data(def.krate).def_key(def.index)
612 }
613
614 fn def_path(&self, def: DefId) -> DefPath {
b7449926
XL
615 self.get_crate_data(def.krate).def_path(def.index)
616 }
617
618 fn def_path_hash(&self, def: DefId) -> DefPathHash {
619 self.get_crate_data(def.krate).def_path_hash(def.index)
620 }
621
c295e0f8
XL
622 fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
623 let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
624 DefId { krate: cnum, index: def_index }
625 }
626
627 fn expn_hash_to_expn_id(
fc512014 628 &self,
c295e0f8 629 sess: &Session,
fc512014
XL
630 cnum: CrateNum,
631 index_guess: u32,
c295e0f8
XL
632 hash: ExpnHash,
633 ) -> ExpnId {
634 self.get_crate_data(cnum).expn_hash_to_expn_id(sess, index_guess, hash)
92a42be0 635 }
a2a8927a
XL
636
637 fn import_source_files(&self, sess: &Session, cnum: CrateNum) {
f2b60f7d
FG
638 let cdata = self.get_crate_data(cnum);
639 for file_index in 0..cdata.root.source_map.size() {
640 cdata.imported_source_file(file_index as u32, sess);
641 }
a2a8927a 642 }
92a42be0 643}