1use std::iter::TrustedLen;
4use std::ops::{Deref, DerefMut};
5use std::path::{Path, PathBuf};
6use std::sync::{Arc, OnceLock};
7use std::{io, mem};
8
9pub(super) use cstore_impl::provide;
10use rustc_ast as ast;
11use rustc_data_structures::fingerprint::Fingerprint;
12use rustc_data_structures::fx::FxIndexMap;
13use rustc_data_structures::owned_slice::OwnedSlice;
14use rustc_data_structures::sync::Lock;
15use rustc_data_structures::unhash::UnhashMap;
16use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
17use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
18use rustc_hir::Safety;
19use rustc_hir::def::Res;
20use rustc_hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE};
21use rustc_hir::definitions::{DefPath, DefPathData};
22use rustc_hir::diagnostic_items::DiagnosticItems;
23use rustc_index::Idx;
24use rustc_middle::middle::lib_features::LibFeatures;
25use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
26use rustc_middle::ty::Visibility;
27use rustc_middle::ty::codec::TyDecoder;
28use rustc_middle::{bug, implement_ty_decoder};
29use rustc_proc_macro::bridge::client::ProcMacro;
30use rustc_serialize::opaque::MemDecoder;
31use rustc_serialize::{Decodable, Decoder};
32use rustc_session::config::TargetModifier;
33use rustc_session::cstore::{CrateSource, ExternCrate};
34use rustc_span::hygiene::HygieneDecodeContext;
35use rustc_span::{
36 BlobDecoder, BytePos, ByteSymbol, DUMMY_SP, Pos, SpanData, SpanDecoder, Symbol, SyntaxContext,
37 kw,
38};
39use tracing::debug;
40
41use crate::creader::CStore;
42use crate::rmeta::table::IsDefault;
43use crate::rmeta::*;
44
45mod cstore_impl;
46
47pub(crate) struct MetadataBlob(OwnedSlice);
51
52impl std::ops::Deref for MetadataBlob {
53 type Target = [u8];
54
55 #[inline]
56 fn deref(&self) -> &[u8] {
57 &self.0[..]
58 }
59}
60
61impl MetadataBlob {
62 pub(crate) fn new(slice: OwnedSlice) -> Result<Self, ()> {
64 if MemDecoder::new(&slice, 0).is_ok() { Ok(Self(slice)) } else { Err(()) }
65 }
66
67 pub(crate) fn bytes(&self) -> &OwnedSlice {
70 &self.0
71 }
72}
73
74pub(crate) type CrateNumMap = IndexVec<CrateNum, CrateNum>;
79
80pub(crate) type TargetModifiers = Vec<TargetModifier>;
82
83pub(crate) struct CrateMetadata {
84 blob: MetadataBlob,
86
87 root: CrateRoot,
90 trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
94 incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<DefIndex>>,
99 raw_proc_macros: Option<&'static [ProcMacro]>,
101 source_map_import_info: Lock<Vec<Option<ImportedSourceFile>>>,
103 def_path_hash_map: DefPathHashMapRef<'static>,
105 expn_hash_map: OnceLock<UnhashMap<ExpnHash, ExpnIndex>>,
107 alloc_decoding_state: AllocDecodingState,
109 def_key_cache: Lock<FxHashMap<DefIndex, DefKey>>,
111
112 cnum: CrateNum,
115 cnum_map: CrateNumMap,
118 dependencies: Vec<CrateNum>,
120 dep_kind: CrateDepKind,
122 source: Arc<CrateSource>,
124 private_dep: bool,
128 host_hash: Option<Svh>,
130 used: bool,
132
133 hygiene_context: HygieneDecodeContext,
139
140 extern_crate: Option<ExternCrate>,
144}
145
146#[derive(Clone)]
149struct ImportedSourceFile {
150 original_start_pos: rustc_span::BytePos,
152 original_end_pos: rustc_span::BytePos,
154 translated_source_file: Arc<rustc_span::SourceFile>,
156}
157
158pub(super) struct BlobDecodeContext<'a> {
162 opaque: MemDecoder<'a>,
163 blob: &'a MetadataBlob,
164 lazy_state: LazyState,
165}
166
167pub(super) trait LazyDecoder: BlobDecoder {
173 fn set_lazy_state(&mut self, state: LazyState);
174 fn get_lazy_state(&self) -> LazyState;
175
176 fn read_lazy<T>(&mut self) -> LazyValue<T> {
177 self.read_lazy_offset_then(|pos| LazyValue::from_position(pos))
178 }
179
180 fn read_lazy_array<T>(&mut self, len: usize) -> LazyArray<T> {
181 self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len))
182 }
183
184 fn read_lazy_table<I, T>(&mut self, width: usize, len: usize) -> LazyTable<I, T> {
185 self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, width, len))
186 }
187
188 #[inline]
189 fn read_lazy_offset_then<T>(&mut self, f: impl Fn(NonZero<usize>) -> T) -> T {
190 let distance = self.read_usize();
191 let position = match self.get_lazy_state() {
192 LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
193 LazyState::NodeStart(start) => {
194 let start = start.get();
195 assert!(distance <= start);
196 start - distance
197 }
198 LazyState::Previous(last_pos) => last_pos.get() + distance,
199 };
200 let position = NonZero::new(position).unwrap();
201 self.set_lazy_state(LazyState::Previous(position));
202 f(position)
203 }
204}
205
206impl<'a> LazyDecoder for BlobDecodeContext<'a> {
207 fn set_lazy_state(&mut self, state: LazyState) {
208 self.lazy_state = state;
209 }
210
211 fn get_lazy_state(&self) -> LazyState {
212 self.lazy_state
213 }
214}
215
216pub(super) struct MetadataDecodeContext<'a, 'tcx> {
220 blob_decoder: BlobDecodeContext<'a>,
221 cdata: CrateMetadataRef<'a>,
222 tcx: TyCtxt<'tcx>,
223
224 alloc_decoding_session: AllocDecodingSession<'a>,
226}
227
228impl<'a, 'tcx> LazyDecoder for MetadataDecodeContext<'a, 'tcx> {
229 fn set_lazy_state(&mut self, state: LazyState) {
230 self.lazy_state = state;
231 }
232
233 fn get_lazy_state(&self) -> LazyState {
234 self.lazy_state
235 }
236}
237
238impl<'a, 'tcx> DerefMut for MetadataDecodeContext<'a, 'tcx> {
239 fn deref_mut(&mut self) -> &mut Self::Target {
240 &mut self.blob_decoder
241 }
242}
243
244impl<'a, 'tcx> Deref for MetadataDecodeContext<'a, 'tcx> {
245 type Target = BlobDecodeContext<'a>;
246
247 fn deref(&self) -> &Self::Target {
248 &self.blob_decoder
249 }
250}
251
252pub(super) trait Metadata<'a>: Copy {
253 type Context: BlobDecoder + LazyDecoder;
254
255 fn blob(self) -> &'a MetadataBlob;
256 fn decoder(self, pos: usize) -> Self::Context;
257}
258
259impl<'a> Metadata<'a> for &'a MetadataBlob {
260 type Context = BlobDecodeContext<'a>;
261
262 fn blob(self) -> &'a MetadataBlob {
263 self
264 }
265
266 fn decoder(self, pos: usize) -> Self::Context {
267 BlobDecodeContext {
268 opaque: MemDecoder::new(self, pos).unwrap(),
276 lazy_state: LazyState::NoNode,
277 blob: self.blob(),
278 }
279 }
280}
281
282impl<'a, 'tcx> Metadata<'a> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
283 type Context = MetadataDecodeContext<'a, 'tcx>;
284
285 fn blob(self) -> &'a MetadataBlob {
286 &self.0.cdata.blob
287 }
288
289 fn decoder(self, pos: usize) -> MetadataDecodeContext<'a, 'tcx> {
290 MetadataDecodeContext {
291 blob_decoder: self.blob().decoder(pos),
292 cdata: self.0,
293 tcx: self.1,
294 alloc_decoding_session: self.0.cdata.alloc_decoding_state.new_decoding_session(),
295 }
296 }
297}
298
299impl<T: ParameterizedOverTcx> LazyValue<T> {
300 #[inline]
301 fn decode<'a, 'tcx, M: Metadata<'a>>(self, metadata: M) -> T::Value<'tcx>
302 where
303 T::Value<'tcx>: Decodable<M::Context>,
304 {
305 let mut dcx = metadata.decoder(self.position.get());
306 dcx.set_lazy_state(LazyState::NodeStart(self.position));
307 T::Value::decode(&mut dcx)
308 }
309}
310
311struct DecodeIterator<T, D> {
312 elem_counter: std::ops::Range<usize>,
313 dcx: D,
314 _phantom: PhantomData<fn() -> T>,
315}
316
317impl<D: Decoder, T: Decodable<D>> Iterator for DecodeIterator<T, D> {
318 type Item = T;
319
320 #[inline(always)]
321 fn next(&mut self) -> Option<Self::Item> {
322 self.elem_counter.next().map(|_| T::decode(&mut self.dcx))
323 }
324
325 #[inline(always)]
326 fn size_hint(&self) -> (usize, Option<usize>) {
327 self.elem_counter.size_hint()
328 }
329}
330
331impl<D: Decoder, T: Decodable<D>> ExactSizeIterator for DecodeIterator<T, D> {
332 fn len(&self) -> usize {
333 self.elem_counter.len()
334 }
335}
336
337unsafe impl<D: Decoder, T: Decodable<D>> TrustedLen for DecodeIterator<T, D> {}
338
339impl<T: ParameterizedOverTcx> LazyArray<T> {
340 #[inline]
341 fn decode<'a, 'tcx, M: Metadata<'a>>(
342 self,
343 metadata: M,
344 ) -> DecodeIterator<T::Value<'tcx>, M::Context>
345 where
346 T::Value<'tcx>: Decodable<M::Context>,
347 {
348 let mut dcx = metadata.decoder(self.position.get());
349 dcx.set_lazy_state(LazyState::NodeStart(self.position));
350 DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData }
351 }
352}
353
354impl<'a, 'tcx> MetadataDecodeContext<'a, 'tcx> {
355 #[inline]
356 fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
357 self.cdata.map_encoded_cnum_to_current(cnum)
358 }
359}
360
361impl<'a> BlobDecodeContext<'a> {
362 #[inline]
363 pub(crate) fn blob(&self) -> &'a MetadataBlob {
364 self.blob
365 }
366
367 fn decode_symbol_or_byte_symbol<S>(
368 &mut self,
369 new_from_index: impl Fn(u32) -> S,
370 read_and_intern_str_or_byte_str_this: impl Fn(&mut Self) -> S,
371 read_and_intern_str_or_byte_str_opaque: impl Fn(&mut MemDecoder<'a>) -> S,
372 ) -> S {
373 let tag = self.read_u8();
374
375 match tag {
376 SYMBOL_STR => read_and_intern_str_or_byte_str_this(self),
377 SYMBOL_OFFSET => {
378 let pos = self.read_usize();
380
381 self.opaque.with_position(pos, |d| read_and_intern_str_or_byte_str_opaque(d))
383 }
384 SYMBOL_PREDEFINED => new_from_index(self.read_u32()),
385 _ => unreachable!(),
386 }
387 }
388}
389
390impl<'a, 'tcx> TyDecoder<'tcx> for MetadataDecodeContext<'a, 'tcx> {
391 const CLEAR_CROSS_CRATE: bool = true;
392
393 #[inline]
394 fn interner(&self) -> TyCtxt<'tcx> {
395 self.tcx
396 }
397
398 fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
399 where
400 F: FnOnce(&mut Self) -> Ty<'tcx>,
401 {
402 let tcx = self.tcx;
403
404 let key = ty::CReaderCacheKey { cnum: Some(self.cdata.cnum), pos: shorthand };
405
406 if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
407 return ty;
408 }
409
410 let ty = or_insert_with(self);
411 tcx.ty_rcache.borrow_mut().insert(key, ty);
412 ty
413 }
414
415 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
416 where
417 F: FnOnce(&mut Self) -> R,
418 {
419 let new_opaque = self.blob_decoder.opaque.split_at(pos);
420 let old_opaque = mem::replace(&mut self.blob_decoder.opaque, new_opaque);
421 let old_state = mem::replace(&mut self.blob_decoder.lazy_state, LazyState::NoNode);
422 let r = f(self);
423 self.blob_decoder.opaque = old_opaque;
424 self.blob_decoder.lazy_state = old_state;
425 r
426 }
427
428 fn decode_alloc_id(&mut self) -> rustc_middle::mir::interpret::AllocId {
429 let ads = self.alloc_decoding_session;
430 ads.decode_alloc_id(self)
431 }
432}
433
434impl<'a, 'tcx> Decodable<MetadataDecodeContext<'a, 'tcx>> for ExpnIndex {
435 #[inline]
436 fn decode(d: &mut MetadataDecodeContext<'a, 'tcx>) -> ExpnIndex {
437 ExpnIndex::from_u32(d.read_u32())
438 }
439}
440
441impl<'a, 'tcx> SpanDecoder for MetadataDecodeContext<'a, 'tcx> {
442 fn decode_attr_id(&mut self) -> rustc_span::AttrId {
443 self.tcx.sess.psess.attr_id_generator.mk_attr_id()
444 }
445
446 fn decode_crate_num(&mut self) -> CrateNum {
447 let cnum = CrateNum::from_u32(self.read_u32());
448 self.map_encoded_cnum_to_current(cnum)
449 }
450
451 fn decode_def_id(&mut self) -> DefId {
452 DefId { krate: Decodable::decode(self), index: Decodable::decode(self) }
453 }
454
455 fn decode_syntax_context(&mut self) -> SyntaxContext {
456 let cdata = self.cdata;
457 let tcx = self.tcx;
458
459 let cname = cdata.root.name();
460 rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
461 debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
462 cdata
463 .root
464 .syntax_contexts
465 .get((cdata, tcx), id)
466 .unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
467 .decode((cdata, tcx))
468 })
469 }
470
471 fn decode_expn_id(&mut self) -> ExpnId {
472 let local_cdata = self.cdata;
473
474 let tcx = self.tcx;
475 let cnum = CrateNum::decode(self);
476 let index = u32::decode(self);
477
478 let expn_id = rustc_span::hygiene::decode_expn_id(cnum, index, |expn_id| {
479 let ExpnId { krate: cnum, local_id: index } = expn_id;
480 debug_assert_ne!(cnum, LOCAL_CRATE);
483 let crate_data = if cnum == local_cdata.cnum {
484 local_cdata
485 } else {
486 local_cdata.cstore.get_crate_data(cnum)
487 };
488 let expn_data = crate_data
489 .root
490 .expn_data
491 .get((crate_data, tcx), index)
492 .unwrap()
493 .decode((crate_data, tcx));
494 let expn_hash = crate_data
495 .root
496 .expn_hashes
497 .get((crate_data, tcx), index)
498 .unwrap()
499 .decode((crate_data, tcx));
500 (expn_data, expn_hash)
501 });
502 expn_id
503 }
504
505 fn decode_span(&mut self) -> Span {
506 let start = self.position();
507 let tag = SpanTag(self.peek_byte());
508 let data = if tag.kind() == SpanKind::Indirect {
509 self.read_u8();
511 let bytes_needed = tag.length().unwrap().0 as usize;
513 let mut total = [0u8; usize::BITS as usize / 8];
514 total[..bytes_needed].copy_from_slice(self.read_raw_bytes(bytes_needed));
515 let offset_or_position = usize::from_le_bytes(total);
516 let position = if tag.is_relative_offset() {
517 start - offset_or_position
518 } else {
519 offset_or_position
520 };
521 self.with_position(position, SpanData::decode)
522 } else {
523 SpanData::decode(self)
524 };
525 data.span()
526 }
527}
528
529impl<'a, 'tcx> BlobDecoder for MetadataDecodeContext<'a, 'tcx> {
530 fn decode_def_index(&mut self) -> DefIndex {
531 self.blob_decoder.decode_def_index()
532 }
533 fn decode_symbol(&mut self) -> Symbol {
534 self.blob_decoder.decode_symbol()
535 }
536
537 fn decode_byte_symbol(&mut self) -> ByteSymbol {
538 self.blob_decoder.decode_byte_symbol()
539 }
540}
541
542impl<'a> BlobDecoder for BlobDecodeContext<'a> {
543 fn decode_def_index(&mut self) -> DefIndex {
544 DefIndex::from_u32(self.read_u32())
545 }
546 fn decode_symbol(&mut self) -> Symbol {
547 self.decode_symbol_or_byte_symbol(
548 Symbol::new,
549 |this| Symbol::intern(this.read_str()),
550 |opaque| Symbol::intern(opaque.read_str()),
551 )
552 }
553
554 fn decode_byte_symbol(&mut self) -> ByteSymbol {
555 self.decode_symbol_or_byte_symbol(
556 ByteSymbol::new,
557 |this| ByteSymbol::intern(this.read_byte_str()),
558 |opaque| ByteSymbol::intern(opaque.read_byte_str()),
559 )
560 }
561}
562
563impl<'a, 'tcx> Decodable<MetadataDecodeContext<'a, 'tcx>> for SpanData {
564 fn decode(decoder: &mut MetadataDecodeContext<'a, 'tcx>) -> SpanData {
565 let tag = SpanTag::decode(decoder);
566 let ctxt = tag.context().unwrap_or_else(|| SyntaxContext::decode(decoder));
567
568 if tag.kind() == SpanKind::Partial {
569 return DUMMY_SP.with_ctxt(ctxt).data();
570 }
571
572 debug_assert!(tag.kind() == SpanKind::Local || tag.kind() == SpanKind::Foreign);
573
574 let lo = BytePos::decode(decoder);
575 let len = tag.length().unwrap_or_else(|| BytePos::decode(decoder));
576 let hi = lo + len;
577
578 let tcx = decoder.tcx;
579
580 let metadata_index = u32::decode(decoder);
582
583 let source_file = if tag.kind() == SpanKind::Local {
612 decoder.cdata.imported_source_file(tcx, metadata_index)
613 } else {
614 if decoder.cdata.root.is_proc_macro_crate() {
617 let cnum = u32::decode(decoder);
620 panic!(
621 "Decoding of crate {:?} tried to access proc-macro dep {:?}",
622 decoder.cdata.root.header.name, cnum
623 );
624 }
625 let cnum = CrateNum::decode(decoder);
627 debug!(
628 "SpecializedDecoder<Span>::specialized_decode: loading source files from cnum {:?}",
629 cnum
630 );
631
632 let foreign_data = decoder.cdata.cstore.get_crate_data(cnum);
633 foreign_data.imported_source_file(tcx, metadata_index)
634 };
635
636 debug_assert!(
638 lo + source_file.original_start_pos <= source_file.original_end_pos,
639 "Malformed encoded span: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
640 lo,
641 source_file.original_start_pos,
642 source_file.original_end_pos
643 );
644
645 debug_assert!(
647 hi + source_file.original_start_pos <= source_file.original_end_pos,
648 "Malformed encoded span: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
649 hi,
650 source_file.original_start_pos,
651 source_file.original_end_pos
652 );
653
654 let lo = lo + source_file.translated_source_file.start_pos;
655 let hi = hi + source_file.translated_source_file.start_pos;
656
657 SpanData { lo, hi, ctxt, parent: None }
659 }
660}
661
662impl<'a, 'tcx> Decodable<MetadataDecodeContext<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
663 fn decode(d: &mut MetadataDecodeContext<'a, 'tcx>) -> Self {
664 ty::codec::RefDecodable::decode(d)
665 }
666}
667
668impl<D: LazyDecoder, T> Decodable<D> for LazyValue<T> {
669 fn decode(decoder: &mut D) -> Self {
670 decoder.read_lazy()
671 }
672}
673
674impl<D: LazyDecoder, T> Decodable<D> for LazyArray<T> {
675 #[inline]
676 fn decode(decoder: &mut D) -> Self {
677 let len = decoder.read_usize();
678 if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
679 }
680}
681
682impl<I: Idx, D: LazyDecoder, T> Decodable<D> for LazyTable<I, T> {
683 fn decode(decoder: &mut D) -> Self {
684 let width = decoder.read_usize();
685 let len = decoder.read_usize();
686 decoder.read_lazy_table(width, len)
687 }
688}
689
690mod meta {
691 use super::*;
692 implement_ty_decoder!(MetadataDecodeContext<'a, 'tcx>);
693}
694mod blob {
695 use super::*;
696 implement_ty_decoder!(BlobDecodeContext<'a>);
697}
698
699impl MetadataBlob {
700 pub(crate) fn check_compatibility(
701 &self,
702 cfg_version: &'static str,
703 ) -> Result<(), Option<String>> {
704 if !self.starts_with(METADATA_HEADER) {
705 if self.starts_with(b"rust") {
706 return Err(Some("<unknown rustc version>".to_owned()));
707 }
708 return Err(None);
709 }
710
711 let found_version =
712 LazyValue::<String>::from_position(NonZero::new(METADATA_HEADER.len() + 8).unwrap())
713 .decode(self);
714 if rustc_version(cfg_version) != found_version {
715 return Err(Some(found_version));
716 }
717
718 Ok(())
719 }
720
721 fn root_pos(&self) -> NonZero<usize> {
722 let offset = METADATA_HEADER.len();
723 let pos_bytes = self[offset..][..8].try_into().unwrap();
724 let pos = u64::from_le_bytes(pos_bytes);
725 NonZero::new(pos as usize).unwrap()
726 }
727
728 pub(crate) fn get_header(&self) -> CrateHeader {
729 let pos = self.root_pos();
730 LazyValue::<CrateHeader>::from_position(pos).decode(self)
731 }
732
733 pub(crate) fn get_root(&self) -> CrateRoot {
734 let pos = self.root_pos();
735 LazyValue::<CrateRoot>::from_position(pos).decode(self)
736 }
737
738 pub(crate) fn list_crate_metadata(
739 &self,
740 out: &mut dyn io::Write,
741 ls_kinds: &[String],
742 ) -> io::Result<()> {
743 let root = self.get_root();
744
745 let all_ls_kinds = vec![
746 "root".to_owned(),
747 "lang_items".to_owned(),
748 "features".to_owned(),
749 "items".to_owned(),
750 ];
751 let ls_kinds = if ls_kinds.contains(&"all".to_owned()) { &all_ls_kinds } else { ls_kinds };
752
753 for kind in ls_kinds {
754 match &**kind {
755 "root" => {
756 writeln!(out, "Crate info:")?;
757 writeln!(out, "name {}{}", root.name(), root.extra_filename)?;
758 writeln!(
759 out,
760 "hash {} stable_crate_id {:?}",
761 root.hash(),
762 root.stable_crate_id
763 )?;
764 writeln!(out, "proc_macro {:?}", root.proc_macro_data.is_some())?;
765 writeln!(out, "triple {}", root.header.triple.tuple())?;
766 writeln!(out, "edition {}", root.edition)?;
767 writeln!(out, "symbol_mangling_version {:?}", root.symbol_mangling_version)?;
768 writeln!(
769 out,
770 "required_panic_strategy {:?} panic_in_drop_strategy {:?}",
771 root.required_panic_strategy, root.panic_in_drop_strategy
772 )?;
773 writeln!(
774 out,
775 "has_global_allocator {} has_alloc_error_handler {} has_panic_handler {} has_default_lib_allocator {}",
776 root.has_global_allocator,
777 root.has_alloc_error_handler,
778 root.has_panic_handler,
779 root.has_default_lib_allocator
780 )?;
781 writeln!(
782 out,
783 "compiler_builtins {} needs_allocator {} needs_panic_runtime {} no_builtins {} panic_runtime {} profiler_runtime {}",
784 root.compiler_builtins,
785 root.needs_allocator,
786 root.needs_panic_runtime,
787 root.no_builtins,
788 root.panic_runtime,
789 root.profiler_runtime
790 )?;
791
792 writeln!(out, "=External Dependencies=")?;
793 let dylib_dependency_formats =
794 root.dylib_dependency_formats.decode(self).collect::<Vec<_>>();
795 for (i, dep) in root.crate_deps.decode(self).enumerate() {
796 let CrateDep { name, extra_filename, hash, host_hash, kind, is_private } =
797 dep;
798 let number = i + 1;
799
800 writeln!(
801 out,
802 "{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?} {privacy}{linkage}",
803 privacy = if is_private { "private" } else { "public" },
804 linkage = if dylib_dependency_formats.is_empty() {
805 String::new()
806 } else {
807 format!(" linkage {:?}", dylib_dependency_formats[i])
808 }
809 )?;
810 }
811 write!(out, "\n")?;
812 }
813
814 "lang_items" => {
815 writeln!(out, "=Lang items=")?;
816 for (id, lang_item) in root.lang_items.decode(self) {
817 writeln!(
818 out,
819 "{} = crate{}",
820 lang_item.name(),
821 DefPath::make(LOCAL_CRATE, id, |parent| root
822 .tables
823 .def_keys
824 .get(self, parent)
825 .unwrap()
826 .decode(self))
827 .to_string_no_crate_verbose()
828 )?;
829 }
830 for lang_item in root.lang_items_missing.decode(self) {
831 writeln!(out, "{} = <missing>", lang_item.name())?;
832 }
833 write!(out, "\n")?;
834 }
835
836 "features" => {
837 writeln!(out, "=Lib features=")?;
838 for (feature, since) in root.lib_features.decode(self) {
839 writeln!(
840 out,
841 "{}{}",
842 feature,
843 if let FeatureStability::AcceptedSince(since) = since {
844 format!(" since {since}")
845 } else {
846 String::new()
847 }
848 )?;
849 }
850 write!(out, "\n")?;
851 }
852
853 "items" => {
854 writeln!(out, "=Items=")?;
855
856 fn print_item(
857 blob: &MetadataBlob,
858 out: &mut dyn io::Write,
859 item: DefIndex,
860 indent: usize,
861 ) -> io::Result<()> {
862 let root = blob.get_root();
863
864 let def_kind = root.tables.def_kind.get(blob, item).unwrap();
865 let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob);
866 #[allow(rustc::symbol_intern_string_literal)]
867 let def_name = if item == CRATE_DEF_INDEX {
868 kw::Crate
869 } else {
870 def_key
871 .disambiguated_data
872 .data
873 .get_opt_name()
874 .unwrap_or_else(|| Symbol::intern("???"))
875 };
876 let visibility =
877 root.tables.visibility.get(blob, item).unwrap().decode(blob).map_id(
878 |index| {
879 format!(
880 "crate{}",
881 DefPath::make(LOCAL_CRATE, index, |parent| root
882 .tables
883 .def_keys
884 .get(blob, parent)
885 .unwrap()
886 .decode(blob))
887 .to_string_no_crate_verbose()
888 )
889 },
890 );
891 write!(
892 out,
893 "{nil: <indent$}{:?} {:?} {} {{",
894 visibility,
895 def_kind,
896 def_name,
897 nil = "",
898 )?;
899
900 if let Some(children) =
901 root.tables.module_children_non_reexports.get(blob, item)
902 {
903 write!(out, "\n")?;
904 for child in children.decode(blob) {
905 print_item(blob, out, child, indent + 4)?;
906 }
907 writeln!(out, "{nil: <indent$}}}", nil = "")?;
908 } else {
909 writeln!(out, "}}")?;
910 }
911
912 Ok(())
913 }
914
915 print_item(self, out, CRATE_DEF_INDEX, 0)?;
916
917 write!(out, "\n")?;
918 }
919
920 _ => {
921 writeln!(
922 out,
923 "unknown -Zls kind. allowed values are: all, root, lang_items, features, items"
924 )?;
925 }
926 }
927 }
928
929 Ok(())
930 }
931}
932
933impl CrateRoot {
934 pub(crate) fn is_proc_macro_crate(&self) -> bool {
935 self.proc_macro_data.is_some()
936 }
937
938 pub(crate) fn name(&self) -> Symbol {
939 self.header.name
940 }
941
942 pub(crate) fn hash(&self) -> Svh {
943 self.header.hash
944 }
945
946 pub(crate) fn stable_crate_id(&self) -> StableCrateId {
947 self.stable_crate_id
948 }
949
950 pub(crate) fn decode_crate_deps<'a>(
951 &self,
952 metadata: &'a MetadataBlob,
953 ) -> impl ExactSizeIterator<Item = CrateDep> {
954 self.crate_deps.decode(metadata)
955 }
956
957 pub(crate) fn decode_target_modifiers<'a>(
958 &self,
959 metadata: &'a MetadataBlob,
960 ) -> impl ExactSizeIterator<Item = TargetModifier> {
961 self.target_modifiers.decode(metadata)
962 }
963}
964
965impl<'a> CrateMetadataRef<'a> {
966 fn missing(self, descr: &str, id: DefIndex) -> ! {
967 bug!("missing `{descr}` for {:?}", self.local_def_id(id))
968 }
969
970 fn raw_proc_macro(self, tcx: TyCtxt<'_>, id: DefIndex) -> &'a ProcMacro {
971 let pos = self
974 .root
975 .proc_macro_data
976 .as_ref()
977 .unwrap()
978 .macros
979 .decode((self, tcx))
980 .position(|i| i == id)
981 .unwrap();
982 &self.raw_proc_macros.unwrap()[pos]
983 }
984
985 fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
986 let def_key = self.def_key(item_index);
987 def_key.disambiguated_data.data.get_opt_name().or_else(|| {
988 if def_key.disambiguated_data.data == DefPathData::Ctor {
989 let parent_index = def_key.parent.expect("no parent for a constructor");
990 self.def_key(parent_index).disambiguated_data.data.get_opt_name()
991 } else {
992 None
993 }
994 })
995 }
996
997 fn item_name(self, item_index: DefIndex) -> Symbol {
998 self.opt_item_name(item_index).expect("no encoded ident for item")
999 }
1000
1001 fn opt_item_ident(self, tcx: TyCtxt<'_>, item_index: DefIndex) -> Option<Ident> {
1002 let name = self.opt_item_name(item_index)?;
1003 let span = self
1004 .root
1005 .tables
1006 .def_ident_span
1007 .get((self, tcx), item_index)
1008 .unwrap_or_else(|| self.missing("def_ident_span", item_index))
1009 .decode((self, tcx));
1010 Some(Ident::new(name, span))
1011 }
1012
1013 fn item_ident(self, tcx: TyCtxt<'_>, item_index: DefIndex) -> Ident {
1014 self.opt_item_ident(tcx, item_index).expect("no encoded ident for item")
1015 }
1016
1017 #[inline]
1018 pub(super) fn map_encoded_cnum_to_current(self, cnum: CrateNum) -> CrateNum {
1019 if cnum == LOCAL_CRATE { self.cnum } else { self.cnum_map[cnum] }
1020 }
1021
1022 fn def_kind(self, tcx: TyCtxt<'_>, item_id: DefIndex) -> DefKind {
1023 self.root
1024 .tables
1025 .def_kind
1026 .get((self, tcx), item_id)
1027 .unwrap_or_else(|| self.missing("def_kind", item_id))
1028 }
1029
1030 fn get_span(self, tcx: TyCtxt<'_>, index: DefIndex) -> Span {
1031 self.root
1032 .tables
1033 .def_span
1034 .get((self, tcx), index)
1035 .unwrap_or_else(|| self.missing("def_span", index))
1036 .decode((self, tcx))
1037 }
1038
1039 fn load_proc_macro<'tcx>(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> SyntaxExtension {
1040 let (name, kind, helper_attrs) = match *self.raw_proc_macro(tcx, id) {
1041 ProcMacro::CustomDerive { trait_name, attributes, client } => {
1042 let helper_attrs =
1043 attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
1044 (
1045 trait_name,
1046 SyntaxExtensionKind::Derive(Arc::new(DeriveProcMacro { client })),
1047 helper_attrs,
1048 )
1049 }
1050 ProcMacro::Attr { name, client } => {
1051 (name, SyntaxExtensionKind::Attr(Arc::new(AttrProcMacro { client })), Vec::new())
1052 }
1053 ProcMacro::Bang { name, client } => {
1054 (name, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })), Vec::new())
1055 }
1056 };
1057
1058 let sess = tcx.sess;
1059 let attrs: Vec<_> = self.get_item_attrs(tcx, id).collect();
1060 SyntaxExtension::new(
1061 sess,
1062 kind,
1063 self.get_span(tcx, id),
1064 helper_attrs,
1065 self.root.edition,
1066 Symbol::intern(name),
1067 &attrs,
1068 false,
1069 )
1070 }
1071
1072 fn get_variant(
1073 self,
1074 tcx: TyCtxt<'_>,
1075 kind: DefKind,
1076 index: DefIndex,
1077 parent_did: DefId,
1078 ) -> (VariantIdx, ty::VariantDef) {
1079 let adt_kind = match kind {
1080 DefKind::Variant => ty::AdtKind::Enum,
1081 DefKind::Struct => ty::AdtKind::Struct,
1082 DefKind::Union => ty::AdtKind::Union,
1083 _ => bug!(),
1084 };
1085
1086 let data =
1087 self.root.tables.variant_data.get((self, tcx), index).unwrap().decode((self, tcx));
1088
1089 let variant_did =
1090 if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
1091 let ctor = data.ctor.map(|(kind, index)| (kind, self.local_def_id(index)));
1092
1093 (
1094 data.idx,
1095 ty::VariantDef::new(
1096 self.item_name(index),
1097 variant_did,
1098 ctor,
1099 data.discr,
1100 self.get_associated_item_or_field_def_ids(tcx, index)
1101 .map(|did| ty::FieldDef {
1102 did,
1103 name: self.item_name(did.index),
1104 vis: self.get_visibility(tcx, did.index),
1105 safety: self.get_safety(tcx, did.index),
1106 value: self.get_default_field(tcx, did.index),
1107 })
1108 .collect(),
1109 parent_did,
1110 None,
1111 data.is_non_exhaustive,
1112 ),
1113 )
1114 }
1115
1116 fn get_adt_def<'tcx>(self, tcx: TyCtxt<'tcx>, item_id: DefIndex) -> ty::AdtDef<'tcx> {
1117 let kind = self.def_kind(tcx, item_id);
1118 let did = self.local_def_id(item_id);
1119
1120 let adt_kind = match kind {
1121 DefKind::Enum => ty::AdtKind::Enum,
1122 DefKind::Struct => ty::AdtKind::Struct,
1123 DefKind::Union => ty::AdtKind::Union,
1124 _ => bug!("get_adt_def called on a non-ADT {:?}", did),
1125 };
1126 let repr =
1127 self.root.tables.repr_options.get((self, tcx), item_id).unwrap().decode((self, tcx));
1128
1129 let mut variants: Vec<_> = if let ty::AdtKind::Enum = adt_kind {
1130 self.root
1131 .tables
1132 .module_children_non_reexports
1133 .get((self, tcx), item_id)
1134 .expect("variants are not encoded for an enum")
1135 .decode((self, tcx))
1136 .filter_map(|index| {
1137 let kind = self.def_kind(tcx, index);
1138 match kind {
1139 DefKind::Ctor(..) => None,
1140 _ => Some(self.get_variant(tcx, kind, index, did)),
1141 }
1142 })
1143 .collect()
1144 } else {
1145 std::iter::once(self.get_variant(tcx, kind, item_id, did)).collect()
1146 };
1147
1148 variants.sort_by_key(|(idx, _)| *idx);
1149
1150 tcx.mk_adt_def(
1151 did,
1152 adt_kind,
1153 variants.into_iter().map(|(_, variant)| variant).collect(),
1154 repr,
1155 )
1156 }
1157
1158 fn get_visibility(self, tcx: TyCtxt<'_>, id: DefIndex) -> Visibility<DefId> {
1159 self.root
1160 .tables
1161 .visibility
1162 .get((self, tcx), id)
1163 .unwrap_or_else(|| self.missing("visibility", id))
1164 .decode((self, tcx))
1165 .map_id(|index| self.local_def_id(index))
1166 }
1167
1168 fn get_safety(self, tcx: TyCtxt<'_>, id: DefIndex) -> Safety {
1169 self.root.tables.safety.get((self, tcx), id)
1170 }
1171
1172 fn get_default_field(self, tcx: TyCtxt<'_>, id: DefIndex) -> Option<DefId> {
1173 self.root.tables.default_fields.get((self, tcx), id).map(|d| d.decode((self, tcx)))
1174 }
1175
1176 fn get_expn_that_defined(self, tcx: TyCtxt<'_>, id: DefIndex) -> ExpnId {
1177 self.root
1178 .tables
1179 .expn_that_defined
1180 .get((self, tcx), id)
1181 .unwrap_or_else(|| self.missing("expn_that_defined", id))
1182 .decode((self, tcx))
1183 }
1184
1185 fn get_debugger_visualizers(self, tcx: TyCtxt<'_>) -> Vec<DebuggerVisualizerFile> {
1186 self.root.debugger_visualizers.decode((self, tcx)).collect::<Vec<_>>()
1187 }
1188
1189 fn get_lib_features(self, tcx: TyCtxt<'_>) -> LibFeatures {
1191 LibFeatures {
1192 stability: self
1193 .root
1194 .lib_features
1195 .decode((self, tcx))
1196 .map(|(sym, stab)| (sym, (stab, DUMMY_SP)))
1197 .collect(),
1198 }
1199 }
1200
1201 fn get_stability_implications<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Symbol)] {
1205 tcx.arena.alloc_from_iter(self.root.stability_implications.decode((self, tcx)))
1206 }
1207
1208 fn get_lang_items<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] {
1210 tcx.arena.alloc_from_iter(
1211 self.root
1212 .lang_items
1213 .decode((self, tcx))
1214 .map(move |(def_index, index)| (self.local_def_id(def_index), index)),
1215 )
1216 }
1217
1218 fn get_stripped_cfg_items<'tcx>(
1219 self,
1220 tcx: TyCtxt<'tcx>,
1221 cnum: CrateNum,
1222 ) -> &'tcx [StrippedCfgItem] {
1223 let item_names = self
1224 .root
1225 .stripped_cfg_items
1226 .decode((self, tcx))
1227 .map(|item| item.map_mod_id(|index| DefId { krate: cnum, index }));
1228 tcx.arena.alloc_from_iter(item_names)
1229 }
1230
1231 fn get_diagnostic_items(self, tcx: TyCtxt<'_>) -> DiagnosticItems {
1233 let mut id_to_name = DefIdMap::default();
1234 let name_to_id = self
1235 .root
1236 .diagnostic_items
1237 .decode((self, tcx))
1238 .map(|(name, def_index)| {
1239 let id = self.local_def_id(def_index);
1240 id_to_name.insert(id, name);
1241 (name, id)
1242 })
1243 .collect();
1244 DiagnosticItems { id_to_name, name_to_id }
1245 }
1246
1247 fn get_mod_child(self, tcx: TyCtxt<'_>, id: DefIndex) -> ModChild {
1248 let ident = self.item_ident(tcx, id);
1249 let res = Res::Def(self.def_kind(tcx, id), self.local_def_id(id));
1250 let vis = self.get_visibility(tcx, id);
1251
1252 ModChild { ident, res, vis, reexport_chain: Default::default() }
1253 }
1254
1255 fn get_module_children(self, tcx: TyCtxt<'_>, id: DefIndex) -> impl Iterator<Item = ModChild> {
1260 gen move {
1261 if let Some(data) = &self.root.proc_macro_data {
1262 if id == CRATE_DEF_INDEX {
1265 for child_index in data.macros.decode((self, tcx)) {
1266 yield self.get_mod_child(tcx, child_index);
1267 }
1268 }
1269 } else {
1270 let non_reexports =
1272 self.root.tables.module_children_non_reexports.get((self, tcx), id);
1273 for child_index in non_reexports.unwrap().decode((self, tcx)) {
1274 yield self.get_mod_child(tcx, child_index);
1275 }
1276
1277 let reexports = self.root.tables.module_children_reexports.get((self, tcx), id);
1278 if !reexports.is_default() {
1279 for reexport in reexports.decode((self, tcx)) {
1280 yield reexport;
1281 }
1282 }
1283 }
1284 }
1285 }
1286
1287 fn get_ambig_module_children(
1288 self,
1289 tcx: TyCtxt<'_>,
1290 id: DefIndex,
1291 ) -> impl Iterator<Item = AmbigModChild> {
1292 gen move {
1293 let children = self.root.tables.ambig_module_children.get((self, tcx), id);
1294 if !children.is_default() {
1295 for child in children.decode((self, tcx)) {
1296 yield child;
1297 }
1298 }
1299 }
1300 }
1301
1302 fn is_ctfe_mir_available(self, tcx: TyCtxt<'_>, id: DefIndex) -> bool {
1303 self.root.tables.mir_for_ctfe.get((self, tcx), id).is_some()
1304 }
1305
1306 fn is_item_mir_available(self, tcx: TyCtxt<'_>, id: DefIndex) -> bool {
1307 self.root.tables.optimized_mir.get((self, tcx), id).is_some()
1308 }
1309
1310 fn get_fn_has_self_parameter(self, tcx: TyCtxt<'_>, id: DefIndex) -> bool {
1311 self.root
1312 .tables
1313 .fn_arg_idents
1314 .get((self, tcx), id)
1315 .expect("argument names not encoded for a function")
1316 .decode((self, tcx))
1317 .nth(0)
1318 .is_some_and(|ident| matches!(ident, Some(Ident { name: kw::SelfLower, .. })))
1319 }
1320
1321 fn get_associated_item_or_field_def_ids(
1322 self,
1323 tcx: TyCtxt<'_>,
1324 id: DefIndex,
1325 ) -> impl Iterator<Item = DefId> {
1326 self.root
1327 .tables
1328 .associated_item_or_field_def_ids
1329 .get((self, tcx), id)
1330 .unwrap_or_else(|| self.missing("associated_item_or_field_def_ids", id))
1331 .decode((self, tcx))
1332 .map(move |child_index| self.local_def_id(child_index))
1333 }
1334
1335 fn get_associated_item(self, tcx: TyCtxt<'_>, id: DefIndex) -> ty::AssocItem {
1336 let kind = match self.def_kind(tcx, id) {
1337 DefKind::AssocConst => ty::AssocKind::Const { name: self.item_name(id) },
1338 DefKind::AssocFn => ty::AssocKind::Fn {
1339 name: self.item_name(id),
1340 has_self: self.get_fn_has_self_parameter(tcx, id),
1341 },
1342 DefKind::AssocTy => {
1343 let data = if let Some(rpitit_info) =
1344 self.root.tables.opt_rpitit_info.get((self, tcx), id)
1345 {
1346 ty::AssocTypeData::Rpitit(rpitit_info.decode((self, tcx)))
1347 } else {
1348 ty::AssocTypeData::Normal(self.item_name(id))
1349 };
1350 ty::AssocKind::Type { data }
1351 }
1352 _ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
1353 };
1354 let container =
1355 self.root.tables.assoc_container.get((self, tcx), id).unwrap().decode((self, tcx));
1356
1357 ty::AssocItem { kind, def_id: self.local_def_id(id), container }
1358 }
1359
1360 fn get_ctor(self, tcx: TyCtxt<'_>, node_id: DefIndex) -> Option<(CtorKind, DefId)> {
1361 match self.def_kind(tcx, node_id) {
1362 DefKind::Struct | DefKind::Variant => {
1363 let vdata = self
1364 .root
1365 .tables
1366 .variant_data
1367 .get((self, tcx), node_id)
1368 .unwrap()
1369 .decode((self, tcx));
1370 vdata.ctor.map(|(kind, index)| (kind, self.local_def_id(index)))
1371 }
1372 _ => None,
1373 }
1374 }
1375
1376 fn get_item_attrs(self, tcx: TyCtxt<'_>, id: DefIndex) -> impl Iterator<Item = hir::Attribute> {
1377 self.root
1378 .tables
1379 .attributes
1380 .get((self, tcx), id)
1381 .unwrap_or_else(|| {
1382 let def_key = self.def_key(id);
1386 assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor);
1387 let parent_id = def_key.parent.expect("no parent for a constructor");
1388 self.root
1389 .tables
1390 .attributes
1391 .get((self, tcx), parent_id)
1392 .expect("no encoded attributes for a structure or variant")
1393 })
1394 .decode((self, tcx))
1395 }
1396
1397 fn get_inherent_implementations_for_type<'tcx>(
1398 self,
1399 tcx: TyCtxt<'tcx>,
1400 id: DefIndex,
1401 ) -> &'tcx [DefId] {
1402 tcx.arena.alloc_from_iter(
1403 self.root
1404 .tables
1405 .inherent_impls
1406 .get((self, tcx), id)
1407 .decode((self, tcx))
1408 .map(|index| self.local_def_id(index)),
1409 )
1410 }
1411
1412 fn get_traits(self, tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> {
1414 self.root.traits.decode((self, tcx)).map(move |index| self.local_def_id(index))
1415 }
1416
1417 fn get_trait_impls(self, tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> {
1419 self.cdata.trait_impls.values().flat_map(move |impls| {
1420 impls.decode((self, tcx)).map(move |(impl_index, _)| self.local_def_id(impl_index))
1421 })
1422 }
1423
1424 fn get_incoherent_impls<'tcx>(self, tcx: TyCtxt<'tcx>, simp: SimplifiedType) -> &'tcx [DefId] {
1425 if let Some(impls) = self.cdata.incoherent_impls.get(&simp) {
1426 tcx.arena.alloc_from_iter(impls.decode((self, tcx)).map(|idx| self.local_def_id(idx)))
1427 } else {
1428 &[]
1429 }
1430 }
1431
1432 fn get_implementations_of_trait<'tcx>(
1433 self,
1434 tcx: TyCtxt<'tcx>,
1435 trait_def_id: DefId,
1436 ) -> &'tcx [(DefId, Option<SimplifiedType>)] {
1437 if self.trait_impls.is_empty() {
1438 return &[];
1439 }
1440
1441 let key = match self.reverse_translate_def_id(trait_def_id) {
1444 Some(def_id) => (def_id.krate.as_u32(), def_id.index),
1445 None => return &[],
1446 };
1447
1448 if let Some(impls) = self.trait_impls.get(&key) {
1449 tcx.arena.alloc_from_iter(
1450 impls
1451 .decode((self, tcx))
1452 .map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
1453 )
1454 } else {
1455 &[]
1456 }
1457 }
1458
1459 fn get_native_libraries(self, tcx: TyCtxt<'_>) -> impl Iterator<Item = NativeLib> {
1460 self.root.native_libraries.decode((self, tcx))
1461 }
1462
1463 fn get_proc_macro_quoted_span(self, tcx: TyCtxt<'_>, index: usize) -> Span {
1464 self.root
1465 .tables
1466 .proc_macro_quoted_spans
1467 .get((self, tcx), index)
1468 .unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}"))
1469 .decode((self, tcx))
1470 }
1471
1472 fn get_foreign_modules(self, tcx: TyCtxt<'_>) -> impl Iterator<Item = ForeignModule> {
1473 self.root.foreign_modules.decode((self, tcx))
1474 }
1475
1476 fn get_dylib_dependency_formats<'tcx>(
1477 self,
1478 tcx: TyCtxt<'tcx>,
1479 ) -> &'tcx [(CrateNum, LinkagePreference)] {
1480 tcx.arena.alloc_from_iter(
1481 self.root.dylib_dependency_formats.decode((self, tcx)).enumerate().flat_map(
1482 |(i, link)| {
1483 let cnum = CrateNum::new(i + 1); link.map(|link| (self.cnum_map[cnum], link))
1485 },
1486 ),
1487 )
1488 }
1489
1490 fn get_missing_lang_items<'tcx>(self, tcx: TyCtxt<'tcx>) -> &'tcx [LangItem] {
1491 tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode((self, tcx)))
1492 }
1493
1494 fn get_exportable_items(self, tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> {
1495 self.root.exportable_items.decode((self, tcx)).map(move |index| self.local_def_id(index))
1496 }
1497
1498 fn get_stable_order_of_exportable_impls(
1499 self,
1500 tcx: TyCtxt<'_>,
1501 ) -> impl Iterator<Item = (DefId, usize)> {
1502 self.root
1503 .stable_order_of_exportable_impls
1504 .decode((self, tcx))
1505 .map(move |v| (self.local_def_id(v.0), v.1))
1506 }
1507
1508 fn exported_non_generic_symbols<'tcx>(
1509 self,
1510 tcx: TyCtxt<'tcx>,
1511 ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
1512 tcx.arena.alloc_from_iter(self.root.exported_non_generic_symbols.decode((self, tcx)))
1513 }
1514
1515 fn exported_generic_symbols<'tcx>(
1516 self,
1517 tcx: TyCtxt<'tcx>,
1518 ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
1519 tcx.arena.alloc_from_iter(self.root.exported_generic_symbols.decode((self, tcx)))
1520 }
1521
1522 fn get_macro(self, tcx: TyCtxt<'_>, id: DefIndex) -> ast::MacroDef {
1523 match self.def_kind(tcx, id) {
1524 DefKind::Macro(_) => {
1525 let macro_rules = self.root.tables.is_macro_rules.get((self, tcx), id);
1526 let body = self
1527 .root
1528 .tables
1529 .macro_definition
1530 .get((self, tcx), id)
1531 .unwrap()
1532 .decode((self, tcx));
1533 ast::MacroDef { macro_rules, body: Box::new(body) }
1534 }
1535 _ => bug!(),
1536 }
1537 }
1538
1539 #[inline]
1540 fn def_key(self, index: DefIndex) -> DefKey {
1541 *self.def_key_cache.lock().entry(index).or_insert_with(|| {
1542 self.root.tables.def_keys.get(&self.blob, index).unwrap().decode(&self.blob)
1543 })
1544 }
1545
1546 fn def_path(self, id: DefIndex) -> DefPath {
1548 debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1549 DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1550 }
1551
1552 #[inline]
1553 fn def_path_hash(self, index: DefIndex) -> DefPathHash {
1554 let fingerprint = Fingerprint::new(
1558 self.root.stable_crate_id.as_u64(),
1559 self.root.tables.def_path_hashes.get(&self.blob, index),
1560 );
1561 DefPathHash::new(self.root.stable_crate_id, fingerprint.split().1)
1562 }
1563
1564 #[inline]
1565 fn def_path_hash_to_def_index(self, hash: DefPathHash) -> Option<DefIndex> {
1566 self.def_path_hash_map.def_path_hash_to_def_index(&hash)
1567 }
1568
1569 fn expn_hash_to_expn_id(self, tcx: TyCtxt<'_>, index_guess: u32, hash: ExpnHash) -> ExpnId {
1570 let index_guess = ExpnIndex::from_u32(index_guess);
1571 let old_hash = self
1572 .root
1573 .expn_hashes
1574 .get((self, tcx), index_guess)
1575 .map(|lazy| lazy.decode((self, tcx)));
1576
1577 let index = if old_hash == Some(hash) {
1578 index_guess
1582 } else {
1583 let map = self.cdata.expn_hash_map.get_or_init(|| {
1587 let end_id = self.root.expn_hashes.size() as u32;
1588 let mut map =
1589 UnhashMap::with_capacity_and_hasher(end_id as usize, Default::default());
1590 for i in 0..end_id {
1591 let i = ExpnIndex::from_u32(i);
1592 if let Some(hash) = self.root.expn_hashes.get((self, tcx), i) {
1593 map.insert(hash.decode((self, tcx)), i);
1594 }
1595 }
1596 map
1597 });
1598 map[&hash]
1599 };
1600
1601 let data = self.root.expn_data.get((self, tcx), index).unwrap().decode((self, tcx));
1602 rustc_span::hygiene::register_expn_id(self.cnum, index, data, hash)
1603 }
1604
1605 fn imported_source_file(self, tcx: TyCtxt<'_>, source_file_index: u32) -> ImportedSourceFile {
1631 fn filter<'a>(
1632 tcx: TyCtxt<'_>,
1633 real_source_base_dir: &Option<PathBuf>,
1634 path: Option<&'a Path>,
1635 ) -> Option<&'a Path> {
1636 path.filter(|_| {
1637 real_source_base_dir.is_some()
1639 && tcx.sess.opts.unstable_opts.translate_remapped_path_to_local_path
1641 })
1642 .filter(|virtual_dir| {
1643 !tcx.sess.opts.remap_path_prefix.iter().any(|(_from, to)| to == virtual_dir)
1647 })
1648 }
1649
1650 let try_to_translate_virtual_to_real =
1651 |virtual_source_base_dir: Option<&str>,
1652 real_source_base_dir: &Option<PathBuf>,
1653 name: &mut rustc_span::FileName| {
1654 let virtual_source_base_dir = [
1655 filter(tcx, real_source_base_dir, virtual_source_base_dir.map(Path::new)),
1656 filter(
1657 tcx,
1658 real_source_base_dir,
1659 tcx.sess.opts.unstable_opts.simulate_remapped_rust_src_base.as_deref(),
1660 ),
1661 ];
1662
1663 debug!(
1664 "try_to_translate_virtual_to_real(name={:?}): \
1665 virtual_source_base_dir={:?}, real_source_base_dir={:?}",
1666 name, virtual_source_base_dir, real_source_base_dir,
1667 );
1668
1669 for virtual_dir in virtual_source_base_dir.iter().flatten() {
1670 if let Some(real_dir) = &real_source_base_dir
1671 && let rustc_span::FileName::Real(old_name) = name
1672 && let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1673 old_name
1674 && let Ok(rest) = virtual_name.strip_prefix(virtual_dir)
1675 {
1676 let new_path = real_dir.join(rest);
1677
1678 debug!(
1679 "try_to_translate_virtual_to_real: `{}` -> `{}`",
1680 virtual_name.display(),
1681 new_path.display(),
1682 );
1683
1684 let (user_remapped, applied) =
1690 tcx.sess.source_map().path_mapping().map_prefix(&new_path);
1691 let new_name = if applied {
1692 rustc_span::RealFileName::Remapped {
1693 local_path: Some(new_path.clone()),
1694 virtual_name: user_remapped.to_path_buf(),
1695 }
1696 } else {
1697 rustc_span::RealFileName::LocalPath(new_path)
1698 };
1699 *old_name = new_name;
1700 }
1701 }
1702 };
1703
1704 let try_to_translate_real_to_virtual =
1705 |virtual_source_base_dir: Option<&str>,
1706 real_source_base_dir: &Option<PathBuf>,
1707 subdir: &str,
1708 name: &mut rustc_span::FileName| {
1709 if let Some(virtual_dir) =
1710 &tcx.sess.opts.unstable_opts.simulate_remapped_rust_src_base
1711 && let Some(real_dir) = real_source_base_dir
1712 && let rustc_span::FileName::Real(old_name) = name
1713 {
1714 let relative_path = match old_name {
1715 rustc_span::RealFileName::LocalPath(local) => {
1716 local.strip_prefix(real_dir).ok()
1717 }
1718 rustc_span::RealFileName::Remapped { virtual_name, .. } => {
1719 virtual_source_base_dir
1720 .and_then(|virtual_dir| virtual_name.strip_prefix(virtual_dir).ok())
1721 }
1722 };
1723 debug!(
1724 ?relative_path,
1725 ?virtual_dir,
1726 ?subdir,
1727 "simulate_remapped_rust_src_base"
1728 );
1729 if let Some(rest) = relative_path.and_then(|p| p.strip_prefix(subdir).ok()) {
1730 *old_name = rustc_span::RealFileName::Remapped {
1731 local_path: None,
1732 virtual_name: virtual_dir.join(subdir).join(rest),
1733 };
1734 }
1735 }
1736 };
1737
1738 let mut import_info = self.cdata.source_map_import_info.lock();
1739 for _ in import_info.len()..=(source_file_index as usize) {
1740 import_info.push(None);
1741 }
1742 import_info[source_file_index as usize]
1743 .get_or_insert_with(|| {
1744 let source_file_to_import = self
1745 .root
1746 .source_map
1747 .get((self, tcx), source_file_index)
1748 .expect("missing source file")
1749 .decode((self, tcx));
1750
1751 let original_end_pos = source_file_to_import.end_position();
1754 let rustc_span::SourceFile {
1755 mut name,
1756 src_hash,
1757 checksum_hash,
1758 start_pos: original_start_pos,
1759 normalized_source_len,
1760 unnormalized_source_len,
1761 lines,
1762 multibyte_chars,
1763 normalized_pos,
1764 stable_id,
1765 ..
1766 } = source_file_to_import;
1767
1768 try_to_translate_real_to_virtual(
1776 option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR"),
1777 &tcx.sess.opts.real_rust_source_base_dir,
1778 "library",
1779 &mut name,
1780 );
1781
1782 try_to_translate_real_to_virtual(
1787 option_env!("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR"),
1788 &tcx.sess.opts.real_rustc_dev_source_base_dir,
1789 "compiler",
1790 &mut name,
1791 );
1792
1793 try_to_translate_virtual_to_real(
1799 option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR"),
1800 &tcx.sess.opts.real_rust_source_base_dir,
1801 &mut name,
1802 );
1803
1804 try_to_translate_virtual_to_real(
1810 option_env!("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR"),
1811 &tcx.sess.opts.real_rustc_dev_source_base_dir,
1812 &mut name,
1813 );
1814
1815 let local_version = tcx.sess.source_map().new_imported_source_file(
1816 name,
1817 src_hash,
1818 checksum_hash,
1819 stable_id,
1820 normalized_source_len.to_u32(),
1821 unnormalized_source_len,
1822 self.cnum,
1823 lines,
1824 multibyte_chars,
1825 normalized_pos,
1826 source_file_index,
1827 );
1828 debug!(
1829 "CrateMetaData::imported_source_files alloc \
1830 source_file {:?} original (start_pos {:?} source_len {:?}) \
1831 translated (start_pos {:?} source_len {:?})",
1832 local_version.name,
1833 original_start_pos,
1834 normalized_source_len,
1835 local_version.start_pos,
1836 local_version.normalized_source_len
1837 );
1838
1839 ImportedSourceFile {
1840 original_start_pos,
1841 original_end_pos,
1842 translated_source_file: local_version,
1843 }
1844 })
1845 .clone()
1846 }
1847
1848 fn get_attr_flags(self, tcx: TyCtxt<'_>, index: DefIndex) -> AttrFlags {
1849 self.root.tables.attr_flags.get((self, tcx), index)
1850 }
1851
1852 fn get_intrinsic(self, tcx: TyCtxt<'_>, index: DefIndex) -> Option<ty::IntrinsicDef> {
1853 self.root.tables.intrinsic.get((self, tcx), index).map(|d| d.decode((self, tcx)))
1854 }
1855
1856 fn get_doc_link_resolutions(self, tcx: TyCtxt<'_>, index: DefIndex) -> DocLinkResMap {
1857 self.root
1858 .tables
1859 .doc_link_resolutions
1860 .get((self, tcx), index)
1861 .expect("no resolutions for a doc link")
1862 .decode((self, tcx))
1863 }
1864
1865 fn get_doc_link_traits_in_scope(
1866 self,
1867 tcx: TyCtxt<'_>,
1868 index: DefIndex,
1869 ) -> impl Iterator<Item = DefId> {
1870 self.root
1871 .tables
1872 .doc_link_traits_in_scope
1873 .get((self, tcx), index)
1874 .expect("no traits in scope for a doc link")
1875 .decode((self, tcx))
1876 }
1877}
1878
1879impl CrateMetadata {
1880 pub(crate) fn new(
1881 tcx: TyCtxt<'_>,
1882 cstore: &CStore,
1883 blob: MetadataBlob,
1884 root: CrateRoot,
1885 raw_proc_macros: Option<&'static [ProcMacro]>,
1886 cnum: CrateNum,
1887 cnum_map: CrateNumMap,
1888 dep_kind: CrateDepKind,
1889 source: CrateSource,
1890 private_dep: bool,
1891 host_hash: Option<Svh>,
1892 ) -> CrateMetadata {
1893 let trait_impls = root
1894 .impls
1895 .decode(&blob)
1896 .map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
1897 .collect();
1898 let alloc_decoding_state =
1899 AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1900 let dependencies = cnum_map.iter().copied().collect();
1901
1902 let def_path_hash_map = root.def_path_hash_map.decode(&blob);
1905
1906 let mut cdata = CrateMetadata {
1907 blob,
1908 root,
1909 trait_impls,
1910 incoherent_impls: Default::default(),
1911 raw_proc_macros,
1912 source_map_import_info: Lock::new(Vec::new()),
1913 def_path_hash_map,
1914 expn_hash_map: Default::default(),
1915 alloc_decoding_state,
1916 cnum,
1917 cnum_map,
1918 dependencies,
1919 dep_kind,
1920 source: Arc::new(source),
1921 private_dep,
1922 host_hash,
1923 used: false,
1924 extern_crate: None,
1925 hygiene_context: Default::default(),
1926 def_key_cache: Default::default(),
1927 };
1928
1929 let cref = CrateMetadataRef { cdata: &cdata, cstore };
1931 cdata.incoherent_impls = cdata
1932 .root
1933 .incoherent_impls
1934 .decode((cref, tcx))
1935 .map(|incoherent_impls| {
1936 (incoherent_impls.self_ty.decode((cref, tcx)), incoherent_impls.impls)
1937 })
1938 .collect();
1939
1940 cdata
1941 }
1942
1943 pub(crate) fn dependencies(&self) -> impl Iterator<Item = CrateNum> {
1944 self.dependencies.iter().copied()
1945 }
1946
1947 pub(crate) fn target_modifiers(&self) -> TargetModifiers {
1948 self.root.decode_target_modifiers(&self.blob).collect()
1949 }
1950
1951 pub(crate) fn update_extern_crate_diagnostics(
1953 &mut self,
1954 new_extern_crate: ExternCrate,
1955 ) -> bool {
1956 let update =
1957 self.extern_crate.as_ref().is_none_or(|old| old.rank() < new_extern_crate.rank());
1958 if update {
1959 self.extern_crate = Some(new_extern_crate);
1960 }
1961 update
1962 }
1963
1964 pub(crate) fn source(&self) -> &CrateSource {
1965 &*self.source
1966 }
1967
1968 pub(crate) fn dep_kind(&self) -> CrateDepKind {
1969 self.dep_kind
1970 }
1971
1972 pub(crate) fn set_dep_kind(&mut self, dep_kind: CrateDepKind) {
1973 self.dep_kind = dep_kind;
1974 }
1975
1976 pub(crate) fn update_and_private_dep(&mut self, private_dep: bool) {
1977 self.private_dep &= private_dep;
1978 }
1979
1980 pub(crate) fn used(&self) -> bool {
1981 self.used
1982 }
1983
1984 pub(crate) fn required_panic_strategy(&self) -> Option<PanicStrategy> {
1985 self.root.required_panic_strategy
1986 }
1987
1988 pub(crate) fn needs_panic_runtime(&self) -> bool {
1989 self.root.needs_panic_runtime
1990 }
1991
1992 pub(crate) fn is_private_dep(&self) -> bool {
1993 self.private_dep
1994 }
1995
1996 pub(crate) fn is_panic_runtime(&self) -> bool {
1997 self.root.panic_runtime
1998 }
1999
2000 pub(crate) fn is_profiler_runtime(&self) -> bool {
2001 self.root.profiler_runtime
2002 }
2003
2004 pub(crate) fn is_compiler_builtins(&self) -> bool {
2005 self.root.compiler_builtins
2006 }
2007
2008 pub(crate) fn needs_allocator(&self) -> bool {
2009 self.root.needs_allocator
2010 }
2011
2012 pub(crate) fn has_global_allocator(&self) -> bool {
2013 self.root.has_global_allocator
2014 }
2015
2016 pub(crate) fn has_alloc_error_handler(&self) -> bool {
2017 self.root.has_alloc_error_handler
2018 }
2019
2020 pub(crate) fn has_default_lib_allocator(&self) -> bool {
2021 self.root.has_default_lib_allocator
2022 }
2023
2024 pub(crate) fn is_proc_macro_crate(&self) -> bool {
2025 self.root.is_proc_macro_crate()
2026 }
2027
2028 pub(crate) fn proc_macros_for_crate(
2029 &self,
2030 tcx: TyCtxt<'_>,
2031 krate: CrateNum,
2032 cstore: &CStore,
2033 ) -> impl Iterator<Item = DefId> {
2034 gen move {
2035 for def_id in self.root.proc_macro_data.as_ref().into_iter().flat_map(move |data| {
2036 data.macros
2037 .decode((CrateMetadataRef { cdata: self, cstore }, tcx))
2038 .map(move |index| DefId { index, krate })
2039 }) {
2040 yield def_id;
2041 }
2042 }
2043 }
2044
2045 pub(crate) fn name(&self) -> Symbol {
2046 self.root.header.name
2047 }
2048
2049 pub(crate) fn hash(&self) -> Svh {
2050 self.root.header.hash
2051 }
2052
2053 pub(crate) fn has_async_drops(&self) -> bool {
2054 self.root.tables.adt_async_destructor.len > 0
2055 }
2056
2057 fn num_def_ids(&self) -> usize {
2058 self.root.tables.def_keys.size()
2059 }
2060
2061 fn local_def_id(&self, index: DefIndex) -> DefId {
2062 DefId { krate: self.cnum, index }
2063 }
2064
2065 fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
2068 for (local, &global) in self.cnum_map.iter_enumerated() {
2069 if global == did.krate {
2070 return Some(DefId { krate: local, index: did.index });
2071 }
2072 }
2073
2074 None
2075 }
2076}