rustc_metadata/rmeta/
decoder.rs

1// Decoding metadata from a single crate's metadata
2
3use 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
48/// A reference to the raw binary version of crate metadata.
49/// This struct applies [`MemDecoder`]'s validation when constructed
50/// so that later constructions are guaranteed to succeed.
51pub(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    /// Runs the [`MemDecoder`] validation and if it passes, constructs a new [`MetadataBlob`].
64    pub(crate) fn new(slice: OwnedSlice) -> Result<Self, ()> {
65        if MemDecoder::new(&slice, 0).is_ok() { Ok(Self(slice)) } else { Err(()) }
66    }
67
68    /// Since this has passed the validation of [`MetadataBlob::new`], this returns bytes which are
69    /// known to pass the [`MemDecoder`] validation.
70    pub(crate) fn bytes(&self) -> &OwnedSlice {
71        &self.0
72    }
73}
74
75/// A map from external crate numbers (as decoded from some crate file) to
76/// local crate numbers (as generated during this session). Each external
77/// crate may refer to types in other external crates, and each has their
78/// own crate numbers.
79pub(crate) type CrateNumMap = IndexVec<CrateNum, CrateNum>;
80
81/// Target modifiers - abi or exploit mitigations flags
82pub(crate) type TargetModifiers = Vec<TargetModifier>;
83
84pub(crate) struct CrateMetadata {
85    /// The primary crate data - binary metadata blob.
86    blob: MetadataBlob,
87
88    // --- Some data pre-decoded from the metadata blob, usually for performance ---
89    /// Data about the top-level items in a crate, as well as various crate-level metadata.
90    root: CrateRoot,
91    /// Trait impl data.
92    /// FIXME: Used only from queries and can use query cache,
93    /// so pre-decoding can probably be avoided.
94    trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
95    /// Inherent impls which do not follow the normal coherence rules.
96    ///
97    /// These can be introduced using either `#![rustc_coherence_is_core]`
98    /// or `#[rustc_allow_incoherent_impl]`.
99    incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<DefIndex>>,
100    /// Proc macro descriptions for this crate, if it's a proc macro crate.
101    raw_proc_macros: Option<&'static [ProcMacro]>,
102    /// Source maps for code from the crate.
103    source_map_import_info: Lock<Vec<Option<ImportedSourceFile>>>,
104    /// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`.
105    def_path_hash_map: DefPathHashMapRef<'static>,
106    /// Likewise for ExpnHash.
107    expn_hash_map: OnceLock<UnhashMap<ExpnHash, ExpnIndex>>,
108    /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
109    alloc_decoding_state: AllocDecodingState,
110    /// Caches decoded `DefKey`s.
111    def_key_cache: Lock<FxHashMap<DefIndex, DefKey>>,
112
113    // --- Other significant crate properties ---
114    /// ID of this crate, from the current compilation session's point of view.
115    cnum: CrateNum,
116    /// Maps crate IDs as they are were seen from this crate's compilation sessions into
117    /// IDs as they are seen from the current compilation session.
118    cnum_map: CrateNumMap,
119    /// How to link (or not link) this crate to the currently compiled crate.
120    dep_kind: CrateDepKind,
121    /// Filesystem location of this crate.
122    source: Arc<CrateSource>,
123    /// Whether or not this crate should be consider a private dependency.
124    /// Used by the 'exported_private_dependencies' lint, and for determining
125    /// whether to emit suggestions that reference this crate.
126    private_dep: bool,
127    /// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
128    host_hash: Option<Svh>,
129    /// The crate was used non-speculatively.
130    used: bool,
131
132    /// Additional data used for decoding `HygieneData` (e.g. `SyntaxContext`
133    /// and `ExpnId`).
134    /// Note that we store a `HygieneDecodeContext` for each `CrateMetadata`. This is
135    /// because `SyntaxContext` ids are not globally unique, so we need
136    /// to track which ids we've decoded on a per-crate basis.
137    hygiene_context: HygieneDecodeContext,
138
139    // --- Data used only for improving diagnostics ---
140    /// Information about the `extern crate` item or path that caused this crate to be loaded.
141    /// If this is `None`, then the crate was injected (e.g., by the allocator).
142    extern_crate: Option<ExternCrate>,
143}
144
145/// Holds information about a rustc_span::SourceFile imported from another crate.
146/// See `imported_source_file()` for more information.
147#[derive(Clone)]
148struct ImportedSourceFile {
149    /// This SourceFile's byte-offset within the source_map of its original crate
150    original_start_pos: rustc_span::BytePos,
151    /// The end of this SourceFile within the source_map of its original crate
152    original_end_pos: rustc_span::BytePos,
153    /// The imported SourceFile's representation within the local source_map
154    translated_source_file: Arc<rustc_span::SourceFile>,
155}
156
157/// Decode context used when we just have a blob of metadata from which we have to decode a header
158/// and [`CrateRoot`]. After that, [`MetadataDecodeContext`] can be used.
159/// Most notably, [`BlobDecodeContext]` doesn't implement [`SpanDecoder`]
160pub(super) struct BlobDecodeContext<'a> {
161    opaque: MemDecoder<'a>,
162    blob: &'a MetadataBlob,
163    lazy_state: LazyState,
164}
165
166/// This trait abstracts over decoders that can decode lazy values using [`LazyState`]:
167///
168/// - [`LazyValue`]
169/// - [`LazyArray`]
170/// - [`LazyTable`]
171pub(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
215/// This is the decode context used when crate metadata was already read.
216/// Decoding of some types, like `Span` require some information to already been read.
217/// Can be constructed from a [`TyCtxt`] and [`CrateMetadataRef`] (see the [`Metadata`] trait)
218pub(super) struct MetadataDecodeContext<'a, 'tcx> {
219    blob_decoder: BlobDecodeContext<'a>,
220    cdata: CrateMetadataRef<'a>,
221    tcx: TyCtxt<'tcx>,
222
223    // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
224    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            // FIXME: This unwrap should never panic because we check that it won't when creating
268            // `MetadataBlob`. Ideally we'd just have a `MetadataDecoder` and hand out subslices of
269            // it as we do elsewhere in the compiler using `MetadataDecoder::split_at`. But we own
270            // the data for the decoder so holding onto the `MemDecoder` too would make us a
271            // self-referential struct which is downright goofy because `MetadataBlob` is already
272            // self-referential. Probably `MemDecoder` should contain an `OwnedSlice`, but that
273            // demands a significant refactoring due to our crate graph.
274            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                // read str offset
378                let pos = self.read_usize();
379
380                // move to str offset and read
381                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            // Lookup local `ExpnData`s in our own crate data. Foreign `ExpnData`s
480            // are stored in the owning crate, to avoid duplication.
481            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            // Skip past the tag we just peek'd.
509            self.read_u8();
510            // indirect tag lengths are safe to access, since they're (0, 8)
511            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        // Index of the file in the corresponding crate's list of encoded files.
580        let metadata_index = u32::decode(decoder);
581
582        // There are two possibilities here:
583        // 1. This is a 'local span', which is located inside a `SourceFile`
584        // that came from this crate. In this case, we use the source map data
585        // encoded in this crate. This branch should be taken nearly all of the time.
586        // 2. This is a 'foreign span', which is located inside a `SourceFile`
587        // that came from a *different* crate (some crate upstream of the one
588        // whose metadata we're looking at). For example, consider this dependency graph:
589        //
590        // A -> B -> C
591        //
592        // Suppose that we're currently compiling crate A, and start deserializing
593        // metadata from crate B. When we deserialize a Span from crate B's metadata,
594        // there are two possibilities:
595        //
596        // 1. The span references a file from crate B. This makes it a 'local' span,
597        // which means that we can use crate B's serialized source map information.
598        // 2. The span references a file from crate C. This makes it a 'foreign' span,
599        // which means we need to use Crate *C* (not crate B) to determine the source
600        // map information. We only record source map information for a file in the
601        // crate that 'owns' it, so deserializing a Span may require us to look at
602        // a transitive dependency.
603        //
604        // When we encode a foreign span, we adjust its 'lo' and 'high' values
605        // to be based on the *foreign* crate (e.g. crate C), not the crate
606        // we are writing metadata for (e.g. crate B). This allows us to
607        // treat the 'local' and 'foreign' cases almost identically during deserialization:
608        // we can call `imported_source_file` for the proper crate, and binary search
609        // through the returned slice using our span.
610        let source_file = if tag.kind() == SpanKind::Local {
611            decoder.cdata.imported_source_file(tcx, metadata_index)
612        } else {
613            // When we encode a proc-macro crate, all `Span`s should be encoded
614            // with `TAG_VALID_SPAN_LOCAL`
615            if decoder.cdata.root.is_proc_macro_crate() {
616                // Decode `CrateNum` as u32 - using `CrateNum::decode` will ICE
617                // since we don't have `cnum_map` populated.
618                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            // tag is TAG_VALID_SPAN_FOREIGN, checked by `debug_assert` above
625            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        // Make sure our span is well-formed.
636        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        // Make sure we correctly filtered out invalid spans during encoding.
645        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        // Do not try to decode parent for foreign spans (it wasn't encoded in the first place).
657        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        // DefIndex's in root.proc_macro_data have a one-to-one correspondence
971        // with items in 'raw_proc_macros'.
972        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    /// Iterates over all the stability attributes in the given crate.
1189    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    /// Iterates over the stability implications in the given crate (when a `#[unstable]` attribute
1201    /// has an `implied_by` meta item, then the mapping from the implied feature to the actual
1202    /// feature is a stability implication).
1203    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    /// Iterates over the lang items in the given crate.
1208    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    /// Iterates over the diagnostic items in the given crate.
1231    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    /// Iterates over all named children of the given module,
1255    /// including both proper items and reexports.
1256    /// Module here is understood in name resolution sense - it can be a `mod` item,
1257    /// or a crate root, or an enum, or a trait.
1258    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 we are loading as a proc macro, we want to return
1262                // the view of this crate as a proc macro crate.
1263                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                // Iterate over all children.
1270                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                // Structure and variant constructors don't have any attributes encoded for them,
1382                // but we assume that someone passing a constructor ID actually wants to look at
1383                // the attributes on the corresponding struct or variant.
1384                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    /// Decodes all traits in the crate (for rustdoc and rustc diagnostics).
1412    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    /// Decodes all trait impls in the crate (for rustdoc).
1417    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        // Do a reverse lookup beforehand to avoid touching the crate_num
1441        // hash map in the loop below.
1442        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); // We skipped LOCAL_CRATE when encoding
1483                    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    // Returns the path leading to the thing with this `id`.
1553    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        // This is a hack to workaround the fact that we can't easily encode/decode a Hash64
1561        // into the FixedSizeEncoding, as Hash64 lacks a Default impl. A future refactor to
1562        // relax the Default restriction will likely fix this.
1563        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            // Fast path: the expn and its index is unchanged from the
1585            // previous compilation session. There is no need to decode anything
1586            // else.
1587            index_guess
1588        } else {
1589            // Slow path: We need to find out the new `DefIndex` of the provided
1590            // `DefPathHash`, if its still exists. This requires decoding every `DefPathHash`
1591            // stored in this crate.
1592            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    /// Imports the source_map from an external crate into the source_map of the crate
1612    /// currently being compiled (the "local crate").
1613    ///
1614    /// The import algorithm works analogous to how AST items are inlined from an
1615    /// external crate's metadata:
1616    /// For every SourceFile in the external source_map an 'inline' copy is created in the
1617    /// local source_map. The correspondence relation between external and local
1618    /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1619    /// function. When an item from an external crate is later inlined into this
1620    /// crate, this correspondence information is used to translate the span
1621    /// information of the inlined item so that it refers the correct positions in
1622    /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1623    ///
1624    /// The import algorithm in the function below will reuse SourceFiles already
1625    /// existing in the local source_map. For example, even if the SourceFile of some
1626    /// source file of libstd gets imported many times, there will only ever be
1627    /// one SourceFile object for the corresponding file in the local source_map.
1628    ///
1629    /// Note that imported SourceFiles do not actually contain the source code of the
1630    /// file they represent, just information about length, line breaks, and
1631    /// multibyte characters. This information is enough to generate valid debuginfo
1632    /// for items inlined from other crates.
1633    ///
1634    /// Proc macro crates don't currently export spans, so this function does not have
1635    /// to work for them.
1636    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                // Only spend time on further checks if we have what to translate *to*.
1644                real_source_base_dir.is_some()
1645                // Some tests need the translation to be always skipped.
1646                && tcx.sess.opts.unstable_opts.translate_remapped_path_to_local_path
1647            })
1648            .filter(|virtual_dir| {
1649                // Don't translate away `/rustc/$hash` if we're still remapping to it,
1650                // since that means we're still building `std`/`rustc` that need it,
1651                // and we don't want the real path to leak into codegen/debuginfo.
1652                !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                        // Check if the translated real path is affected by any user-requested
1691                        // remaps via --remap-path-prefix. Apply them if so.
1692                        // Note that this is a special case for imported rust-src paths specified by
1693                        // https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths.
1694                        // Other imported paths are not currently remapped (see #66251).
1695                        *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                // We can't reuse an existing SourceFile, so allocate a new one
1750                // containing the information we need.
1751                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                // If this file is under $sysroot/lib/rustlib/src/
1767                // and the user wish to simulate remapping with -Z simulate-remapped-rust-src-base,
1768                // then we change `name` to a similar state as if the rust was bootstrapped
1769                // with `remap-debuginfo = true`.
1770                // This is useful for testing so that tests about the effects of
1771                // `try_to_translate_virtual_to_real` don't have to worry about how the
1772                // compiler is bootstrapped.
1773                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                // If this file is under $sysroot/lib/rustlib/rustc-src/
1781                // and the user wish to simulate remapping with -Z simulate-remapped-rust-src-base,
1782                // then we change `name` to a similar state as if the rust was bootstrapped
1783                // with `remap-debuginfo = true`.
1784                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                // If this file's path has been remapped to `/rustc/$hash`,
1792                // we might be able to reverse that.
1793                //
1794                // NOTE: if you update this, you might need to also update bootstrap's code for generating
1795                // the `rust-src` component in `Src::run` in `src/bootstrap/dist.rs`.
1796                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                // If this file's path has been remapped to `/rustc-dev/$hash`,
1803                // we might be able to reverse that.
1804                //
1805                // NOTE: if you update this, you might need to also update bootstrap's code for generating
1806                // the `rustc-dev` component in `Src::run` in `src/bootstrap/dist.rs`.
1807                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        // Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
1900        // that does not copy any data. It just does some data verification.
1901        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        // Need `CrateMetadataRef` to decode `DefId`s in simplified types.
1926        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    /// Keep `new_extern_crate` if it looks better in diagnostics
1948    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    // Translate a DefId from the current compilation environment to a DefId
2062    // for an external crate.
2063    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}