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