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