]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/rmeta/decoder.rs
Rollup merge of #74601 - GuillaumeGomez:cleanup-e0724, r=jyn514
[rust.git] / src / librustc_metadata / rmeta / decoder.rs
1 // Decoding metadata from a single crate's metadata
2
3 use crate::creader::CrateMetadataRef;
4 use crate::rmeta::table::{FixedSizeEncoding, Table};
5 use crate::rmeta::*;
6
7 use rustc_ast::ast;
8 use rustc_attr as attr;
9 use rustc_data_structures::captures::Captures;
10 use rustc_data_structures::fingerprint::Fingerprint;
11 use rustc_data_structures::fx::FxHashMap;
12 use rustc_data_structures::svh::Svh;
13 use rustc_data_structures::sync::{AtomicCell, Lock, LockGuard, Lrc, OnceCell};
14 use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
15 use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
16 use rustc_hir as hir;
17 use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
18 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
19 use rustc_hir::definitions::DefPathTable;
20 use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
21 use rustc_hir::lang_items;
22 use rustc_index::vec::{Idx, IndexVec};
23 use rustc_middle::dep_graph::{self, DepNode, DepNodeExt, DepNodeIndex};
24 use rustc_middle::hir::exports::Export;
25 use rustc_middle::middle::cstore::{CrateSource, ExternCrate};
26 use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLib};
27 use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
28 use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
29 use rustc_middle::mir::{self, interpret, Body, Promoted};
30 use rustc_middle::ty::codec::TyDecoder;
31 use rustc_middle::ty::{self, Ty, TyCtxt};
32 use rustc_middle::util::common::record_time;
33 use rustc_serialize::{opaque, Decodable, Decoder, SpecializedDecoder, UseSpecializedDecodable};
34 use rustc_session::Session;
35 use rustc_span::source_map::{respan, Spanned};
36 use rustc_span::symbol::{sym, Ident, Symbol};
37 use rustc_span::{self, hygiene::MacroKind, BytePos, Pos, Span, DUMMY_SP};
38
39 use log::debug;
40 use proc_macro::bridge::client::ProcMacro;
41 use std::io;
42 use std::mem;
43 use std::num::NonZeroUsize;
44 use std::path::Path;
45
46 pub use cstore_impl::{provide, provide_extern};
47
48 mod cstore_impl;
49
50 crate struct MetadataBlob(MetadataRef);
51
52 // A map from external crate numbers (as decoded from some crate file) to
53 // local crate numbers (as generated during this session). Each external
54 // crate may refer to types in other external crates, and each has their
55 // own crate numbers.
56 crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;
57
58 crate struct CrateMetadata {
59     /// The primary crate data - binary metadata blob.
60     blob: MetadataBlob,
61
62     // --- Some data pre-decoded from the metadata blob, usually for performance ---
63     /// Properties of the whole crate.
64     /// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
65     /// lifetime is only used behind `Lazy`, and therefore acts like an
66     /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
67     /// is being used to decode those values.
68     root: CrateRoot<'static>,
69     /// For each definition in this crate, we encode a key. When the
70     /// crate is loaded, we read all the keys and put them in this
71     /// hashmap, which gives the reverse mapping. This allows us to
72     /// quickly retrace a `DefPath`, which is needed for incremental
73     /// compilation support.
74     def_path_table: DefPathTable,
75     /// Trait impl data.
76     /// FIXME: Used only from queries and can use query cache,
77     /// so pre-decoding can probably be avoided.
78     trait_impls: FxHashMap<(u32, DefIndex), Lazy<[DefIndex]>>,
79     /// Proc macro descriptions for this crate, if it's a proc macro crate.
80     raw_proc_macros: Option<&'static [ProcMacro]>,
81     /// Source maps for code from the crate.
82     source_map_import_info: OnceCell<Vec<ImportedSourceFile>>,
83     /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
84     alloc_decoding_state: AllocDecodingState,
85     /// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
86     /// It is initialized on the first access in `get_crate_dep_node_index()`.
87     /// Do not access the value directly, as it might not have been initialized yet.
88     /// The field must always be initialized to `DepNodeIndex::INVALID`.
89     dep_node_index: AtomicCell<DepNodeIndex>,
90
91     // --- Other significant crate properties ---
92     /// ID of this crate, from the current compilation session's point of view.
93     cnum: CrateNum,
94     /// Maps crate IDs as they are were seen from this crate's compilation sessions into
95     /// IDs as they are seen from the current compilation session.
96     cnum_map: CrateNumMap,
97     /// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
98     dependencies: Lock<Vec<CrateNum>>,
99     /// How to link (or not link) this crate to the currently compiled crate.
100     dep_kind: Lock<DepKind>,
101     /// Filesystem location of this crate.
102     source: CrateSource,
103     /// Whether or not this crate should be consider a private dependency
104     /// for purposes of the 'exported_private_dependencies' lint
105     private_dep: bool,
106     /// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
107     host_hash: Option<Svh>,
108
109     // --- Data used only for improving diagnostics ---
110     /// Information about the `extern crate` item or path that caused this crate to be loaded.
111     /// If this is `None`, then the crate was injected (e.g., by the allocator).
112     extern_crate: Lock<Option<ExternCrate>>,
113 }
114
115 /// Holds information about a rustc_span::SourceFile imported from another crate.
116 /// See `imported_source_files()` for more information.
117 struct ImportedSourceFile {
118     /// This SourceFile's byte-offset within the source_map of its original crate
119     original_start_pos: rustc_span::BytePos,
120     /// The end of this SourceFile within the source_map of its original crate
121     original_end_pos: rustc_span::BytePos,
122     /// The imported SourceFile's representation within the local source_map
123     translated_source_file: Lrc<rustc_span::SourceFile>,
124 }
125
126 pub(super) struct DecodeContext<'a, 'tcx> {
127     opaque: opaque::Decoder<'a>,
128     cdata: Option<CrateMetadataRef<'a>>,
129     sess: Option<&'tcx Session>,
130     tcx: Option<TyCtxt<'tcx>>,
131
132     // Cache the last used source_file for translating spans as an optimization.
133     last_source_file_index: usize,
134
135     lazy_state: LazyState,
136
137     // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
138     alloc_decoding_session: Option<AllocDecodingSession<'a>>,
139 }
140
141 /// Abstract over the various ways one can create metadata decoders.
142 pub(super) trait Metadata<'a, 'tcx>: Copy {
143     fn raw_bytes(self) -> &'a [u8];
144     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
145         None
146     }
147     fn sess(self) -> Option<&'tcx Session> {
148         None
149     }
150     fn tcx(self) -> Option<TyCtxt<'tcx>> {
151         None
152     }
153
154     fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
155         let tcx = self.tcx();
156         DecodeContext {
157             opaque: opaque::Decoder::new(self.raw_bytes(), pos),
158             cdata: self.cdata(),
159             sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
160             tcx,
161             last_source_file_index: 0,
162             lazy_state: LazyState::NoNode,
163             alloc_decoding_session: self
164                 .cdata()
165                 .map(|cdata| cdata.cdata.alloc_decoding_state.new_decoding_session()),
166         }
167     }
168 }
169
170 impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
171     fn raw_bytes(self) -> &'a [u8] {
172         &self.0
173     }
174 }
175
176 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a MetadataBlob, &'tcx Session) {
177     fn raw_bytes(self) -> &'a [u8] {
178         let (blob, _) = self;
179         &blob.0
180     }
181
182     fn sess(self) -> Option<&'tcx Session> {
183         let (_, sess) = self;
184         Some(sess)
185     }
186 }
187
188 impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadataRef<'a> {
189     fn raw_bytes(self) -> &'a [u8] {
190         self.blob.raw_bytes()
191     }
192     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
193         Some(*self)
194     }
195 }
196
197 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadataRef<'a>, &'tcx Session) {
198     fn raw_bytes(self) -> &'a [u8] {
199         self.0.raw_bytes()
200     }
201     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
202         Some(*self.0)
203     }
204     fn sess(self) -> Option<&'tcx Session> {
205         Some(&self.1)
206     }
207 }
208
209 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadataRef<'a>, TyCtxt<'tcx>) {
210     fn raw_bytes(self) -> &'a [u8] {
211         self.0.raw_bytes()
212     }
213     fn cdata(self) -> Option<CrateMetadataRef<'a>> {
214         Some(*self.0)
215     }
216     fn tcx(self) -> Option<TyCtxt<'tcx>> {
217         Some(self.1)
218     }
219 }
220
221 impl<'a, 'tcx, T: Decodable> Lazy<T, ()> {
222     fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
223         let mut dcx = metadata.decoder(self.position.get());
224         dcx.lazy_state = LazyState::NodeStart(self.position);
225         T::decode(&mut dcx).unwrap()
226     }
227 }
228
229 impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T], usize> {
230     fn decode<M: Metadata<'a, 'tcx>>(
231         self,
232         metadata: M,
233     ) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
234         let mut dcx = metadata.decoder(self.position.get());
235         dcx.lazy_state = LazyState::NodeStart(self.position);
236         (0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
237     }
238 }
239
240 impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
241     fn tcx(&self) -> TyCtxt<'tcx> {
242         self.tcx.expect("missing TyCtxt in DecodeContext")
243     }
244
245     fn cdata(&self) -> CrateMetadataRef<'a> {
246         self.cdata.expect("missing CrateMetadata in DecodeContext")
247     }
248
249     fn read_lazy_with_meta<T: ?Sized + LazyMeta>(
250         &mut self,
251         meta: T::Meta,
252     ) -> Result<Lazy<T>, <Self as Decoder>::Error> {
253         let min_size = T::min_size(meta);
254         let distance = self.read_usize()?;
255         let position = match self.lazy_state {
256             LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
257             LazyState::NodeStart(start) => {
258                 let start = start.get();
259                 assert!(distance + min_size <= start);
260                 start - distance - min_size
261             }
262             LazyState::Previous(last_min_end) => last_min_end.get() + distance,
263         };
264         self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
265         Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
266     }
267 }
268
269 impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
270     #[inline]
271     fn tcx(&self) -> TyCtxt<'tcx> {
272         self.tcx.expect("missing TyCtxt in DecodeContext")
273     }
274
275     #[inline]
276     fn peek_byte(&self) -> u8 {
277         self.opaque.data[self.opaque.position()]
278     }
279
280     #[inline]
281     fn position(&self) -> usize {
282         self.opaque.position()
283     }
284
285     fn cached_ty_for_shorthand<F>(
286         &mut self,
287         shorthand: usize,
288         or_insert_with: F,
289     ) -> Result<Ty<'tcx>, Self::Error>
290     where
291         F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>,
292     {
293         let tcx = self.tcx();
294
295         let key = ty::CReaderCacheKey { cnum: self.cdata().cnum, pos: shorthand };
296
297         if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
298             return Ok(ty);
299         }
300
301         let ty = or_insert_with(self)?;
302         tcx.ty_rcache.borrow_mut().insert(key, ty);
303         Ok(ty)
304     }
305
306     fn cached_predicate_for_shorthand<F>(
307         &mut self,
308         shorthand: usize,
309         or_insert_with: F,
310     ) -> Result<ty::Predicate<'tcx>, Self::Error>
311     where
312         F: FnOnce(&mut Self) -> Result<ty::Predicate<'tcx>, Self::Error>,
313     {
314         let tcx = self.tcx();
315
316         let key = ty::CReaderCacheKey { cnum: self.cdata().cnum, pos: shorthand };
317
318         if let Some(&pred) = tcx.pred_rcache.borrow().get(&key) {
319             return Ok(pred);
320         }
321
322         let pred = or_insert_with(self)?;
323         tcx.pred_rcache.borrow_mut().insert(key, pred);
324         Ok(pred)
325     }
326
327     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
328     where
329         F: FnOnce(&mut Self) -> R,
330     {
331         let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
332         let old_opaque = mem::replace(&mut self.opaque, new_opaque);
333         let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
334         let r = f(self);
335         self.opaque = old_opaque;
336         self.lazy_state = old_state;
337         r
338     }
339
340     fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
341         if cnum == LOCAL_CRATE { self.cdata().cnum } else { self.cdata().cnum_map[cnum] }
342     }
343 }
344
345 impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T, ()>> for DecodeContext<'a, 'tcx> {
346     fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
347         self.read_lazy_with_meta(())
348     }
349 }
350
351 impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T], usize>> for DecodeContext<'a, 'tcx> {
352     fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
353         let len = self.read_usize()?;
354         if len == 0 { Ok(Lazy::empty()) } else { self.read_lazy_with_meta(len) }
355     }
356 }
357
358 impl<'a, 'tcx, I: Idx, T> SpecializedDecoder<Lazy<Table<I, T>, usize>> for DecodeContext<'a, 'tcx>
359 where
360     Option<T>: FixedSizeEncoding,
361 {
362     fn specialized_decode(&mut self) -> Result<Lazy<Table<I, T>>, Self::Error> {
363         let len = self.read_usize()?;
364         self.read_lazy_with_meta(len)
365     }
366 }
367
368 impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> {
369     #[inline]
370     fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {
371         let krate = CrateNum::decode(self)?;
372         let index = DefIndex::decode(self)?;
373
374         Ok(DefId { krate, index })
375     }
376 }
377
378 impl<'a, 'tcx> SpecializedDecoder<DefIndex> for DecodeContext<'a, 'tcx> {
379     #[inline]
380     fn specialized_decode(&mut self) -> Result<DefIndex, Self::Error> {
381         Ok(DefIndex::from_u32(self.read_u32()?))
382     }
383 }
384
385 impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for DecodeContext<'a, 'tcx> {
386     #[inline]
387     fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
388         Ok(DefId::decode(self)?.expect_local())
389     }
390 }
391
392 impl<'a, 'tcx> SpecializedDecoder<interpret::AllocId> for DecodeContext<'a, 'tcx> {
393     fn specialized_decode(&mut self) -> Result<interpret::AllocId, Self::Error> {
394         if let Some(alloc_decoding_session) = self.alloc_decoding_session {
395             alloc_decoding_session.decode_alloc_id(self)
396         } else {
397             bug!("Attempting to decode interpret::AllocId without CrateMetadata")
398         }
399     }
400 }
401
402 impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
403     fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
404         let tag = u8::decode(self)?;
405
406         if tag == TAG_INVALID_SPAN {
407             return Ok(DUMMY_SP);
408         }
409
410         debug_assert!(tag == TAG_VALID_SPAN_LOCAL || tag == TAG_VALID_SPAN_FOREIGN);
411
412         let lo = BytePos::decode(self)?;
413         let len = BytePos::decode(self)?;
414         let hi = lo + len;
415
416         let sess = if let Some(sess) = self.sess {
417             sess
418         } else {
419             bug!("Cannot decode Span without Session.")
420         };
421
422         // There are two possibilities here:
423         // 1. This is a 'local span', which is located inside a `SourceFile`
424         // that came from this crate. In this case, we use the source map data
425         // encoded in this crate. This branch should be taken nearly all of the time.
426         // 2. This is a 'foreign span', which is located inside a `SourceFile`
427         // that came from a *different* crate (some crate upstream of the one
428         // whose metadata we're looking at). For example, consider this dependency graph:
429         //
430         // A -> B -> C
431         //
432         // Suppose that we're currently compiling crate A, and start deserializing
433         // metadata from crate B. When we deserialize a Span from crate B's metadata,
434         // there are two posibilites:
435         //
436         // 1. The span references a file from crate B. This makes it a 'local' span,
437         // which means that we can use crate B's serialized source map information.
438         // 2. The span references a file from crate C. This makes it a 'foreign' span,
439         // which means we need to use Crate *C* (not crate B) to determine the source
440         // map information. We only record source map information for a file in the
441         // crate that 'owns' it, so deserializing a Span may require us to look at
442         // a transitive dependency.
443         //
444         // When we encode a foreign span, we adjust its 'lo' and 'high' values
445         // to be based on the *foreign* crate (e.g. crate C), not the crate
446         // we are writing metadata for (e.g. crate B). This allows us to
447         // treat the 'local' and 'foreign' cases almost identically during deserialization:
448         // we can call `imported_source_files` for the proper crate, and binary search
449         // through the returned slice using our span.
450         let imported_source_files = if tag == TAG_VALID_SPAN_LOCAL {
451             self.cdata().imported_source_files(sess)
452         } else {
453             // When we encode a proc-macro crate, all `Span`s should be encoded
454             // with `TAG_VALID_SPAN_LOCAL`
455             if self.cdata().root.is_proc_macro_crate() {
456                 // Decode `CrateNum` as u32 - using `CrateNum::decode` will ICE
457                 // since we don't have `cnum_map` populated.
458                 let cnum = u32::decode(self)?;
459                 panic!(
460                     "Decoding of crate {:?} tried to access proc-macro dep {:?}",
461                     self.cdata().root.name,
462                     cnum
463                 );
464             }
465             // tag is TAG_VALID_SPAN_FOREIGN, checked by `debug_assert` above
466             let cnum = CrateNum::decode(self)?;
467             debug!(
468                 "SpecializedDecoder<Span>::specialized_decode: loading source files from cnum {:?}",
469                 cnum
470             );
471
472             // Decoding 'foreign' spans should be rare enough that it's
473             // not worth it to maintain a per-CrateNum cache for `last_source_file_index`.
474             // We just set it to 0, to ensure that we don't try to access something out
475             // of bounds for our initial 'guess'
476             self.last_source_file_index = 0;
477
478             let foreign_data = self.cdata().cstore.get_crate_data(cnum);
479             foreign_data.imported_source_files(sess)
480         };
481
482         let source_file = {
483             // Optimize for the case that most spans within a translated item
484             // originate from the same source_file.
485             let last_source_file = &imported_source_files[self.last_source_file_index];
486
487             if lo >= last_source_file.original_start_pos && lo <= last_source_file.original_end_pos
488             {
489                 last_source_file
490             } else {
491                 let index = imported_source_files
492                     .binary_search_by_key(&lo, |source_file| source_file.original_start_pos)
493                     .unwrap_or_else(|index| index - 1);
494
495                 // Don't try to cache the index for foreign spans,
496                 // as this would require a map from CrateNums to indices
497                 if tag == TAG_VALID_SPAN_LOCAL {
498                     self.last_source_file_index = index;
499                 }
500                 &imported_source_files[index]
501             }
502         };
503
504         // Make sure our binary search above is correct.
505         debug_assert!(
506             lo >= source_file.original_start_pos && lo <= source_file.original_end_pos,
507             "Bad binary search: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
508             lo,
509             source_file.original_start_pos,
510             source_file.original_end_pos
511         );
512
513         // Make sure we correctly filtered out invalid spans during encoding
514         debug_assert!(
515             hi >= source_file.original_start_pos && hi <= source_file.original_end_pos,
516             "Bad binary search: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}",
517             hi,
518             source_file.original_start_pos,
519             source_file.original_end_pos
520         );
521
522         let lo =
523             (lo + source_file.translated_source_file.start_pos) - source_file.original_start_pos;
524         let hi =
525             (hi + source_file.translated_source_file.start_pos) - source_file.original_start_pos;
526
527         Ok(Span::with_root_ctxt(lo, hi))
528     }
529 }
530
531 impl<'a, 'tcx> SpecializedDecoder<Fingerprint> for DecodeContext<'a, 'tcx> {
532     fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
533         Fingerprint::decode_opaque(&mut self.opaque)
534     }
535 }
536
537 impl<'a, 'tcx, T> SpecializedDecoder<mir::ClearCrossCrate<T>> for DecodeContext<'a, 'tcx>
538 where
539     mir::ClearCrossCrate<T>: UseSpecializedDecodable,
540 {
541     #[inline]
542     fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
543         Ok(mir::ClearCrossCrate::Clear)
544     }
545 }
546
547 implement_ty_decoder!(DecodeContext<'a, 'tcx>);
548
549 impl MetadataBlob {
550     crate fn new(metadata_ref: MetadataRef) -> MetadataBlob {
551         MetadataBlob(metadata_ref)
552     }
553
554     crate fn is_compatible(&self) -> bool {
555         self.raw_bytes().starts_with(METADATA_HEADER)
556     }
557
558     crate fn get_rustc_version(&self) -> String {
559         Lazy::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap())
560             .decode(self)
561     }
562
563     crate fn get_root(&self) -> CrateRoot<'tcx> {
564         let slice = self.raw_bytes();
565         let offset = METADATA_HEADER.len();
566         let pos = (((slice[offset + 0] as u32) << 24)
567             | ((slice[offset + 1] as u32) << 16)
568             | ((slice[offset + 2] as u32) << 8)
569             | ((slice[offset + 3] as u32) << 0)) as usize;
570         Lazy::<CrateRoot<'tcx>>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
571     }
572
573     crate fn list_crate_metadata(&self, out: &mut dyn io::Write) -> io::Result<()> {
574         write!(out, "=External Dependencies=\n")?;
575         let root = self.get_root();
576         for (i, dep) in root.crate_deps.decode(self).enumerate() {
577             write!(out, "{} {}{}\n", i + 1, dep.name, dep.extra_filename)?;
578         }
579         write!(out, "\n")?;
580         Ok(())
581     }
582 }
583
584 impl EntryKind {
585     fn def_kind(&self) -> DefKind {
586         match *self {
587             EntryKind::AnonConst(..) => DefKind::AnonConst,
588             EntryKind::Const(..) => DefKind::Const,
589             EntryKind::AssocConst(..) => DefKind::AssocConst,
590             EntryKind::ImmStatic
591             | EntryKind::MutStatic
592             | EntryKind::ForeignImmStatic
593             | EntryKind::ForeignMutStatic => DefKind::Static,
594             EntryKind::Struct(_, _) => DefKind::Struct,
595             EntryKind::Union(_, _) => DefKind::Union,
596             EntryKind::Fn(_) | EntryKind::ForeignFn(_) => DefKind::Fn,
597             EntryKind::AssocFn(_) => DefKind::AssocFn,
598             EntryKind::Type => DefKind::TyAlias,
599             EntryKind::TypeParam => DefKind::TyParam,
600             EntryKind::ConstParam => DefKind::ConstParam,
601             EntryKind::OpaqueTy => DefKind::OpaqueTy,
602             EntryKind::AssocType(_) => DefKind::AssocTy,
603             EntryKind::Mod(_) => DefKind::Mod,
604             EntryKind::Variant(_) => DefKind::Variant,
605             EntryKind::Trait(_) => DefKind::Trait,
606             EntryKind::TraitAlias => DefKind::TraitAlias,
607             EntryKind::Enum(..) => DefKind::Enum,
608             EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
609             EntryKind::ForeignType => DefKind::ForeignTy,
610             EntryKind::Impl(_) => DefKind::Impl,
611             EntryKind::Closure => DefKind::Closure,
612             EntryKind::ForeignMod => DefKind::ForeignMod,
613             EntryKind::GlobalAsm => DefKind::GlobalAsm,
614             EntryKind::Field => DefKind::Field,
615             EntryKind::Generator(_) => DefKind::Generator,
616         }
617     }
618 }
619
620 impl CrateRoot<'_> {
621     crate fn is_proc_macro_crate(&self) -> bool {
622         self.proc_macro_data.is_some()
623     }
624
625     crate fn name(&self) -> Symbol {
626         self.name
627     }
628
629     crate fn disambiguator(&self) -> CrateDisambiguator {
630         self.disambiguator
631     }
632
633     crate fn hash(&self) -> Svh {
634         self.hash
635     }
636
637     crate fn triple(&self) -> &TargetTriple {
638         &self.triple
639     }
640
641     crate fn decode_crate_deps(
642         &self,
643         metadata: &'a MetadataBlob,
644     ) -> impl ExactSizeIterator<Item = CrateDep> + Captures<'a> {
645         self.crate_deps.decode(metadata)
646     }
647 }
648
649 impl<'a, 'tcx> CrateMetadataRef<'a> {
650     fn is_proc_macro(&self, id: DefIndex) -> bool {
651         self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
652     }
653
654     fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
655         self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
656     }
657
658     fn kind(&self, item_id: DefIndex) -> EntryKind {
659         assert!(!self.is_proc_macro(item_id));
660         self.maybe_kind(item_id).unwrap_or_else(|| {
661             bug!(
662                 "CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
663                 item_id,
664                 self.root.name,
665                 self.cnum,
666             )
667         })
668     }
669
670     fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
671         // DefIndex's in root.proc_macro_data have a one-to-one correspondence
672         // with items in 'raw_proc_macros'.
673         let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
674         &self.raw_proc_macros.unwrap()[pos]
675     }
676
677     fn item_ident(&self, item_index: DefIndex, sess: &Session) -> Ident {
678         if !self.is_proc_macro(item_index) {
679             let name = self
680                 .def_key(item_index)
681                 .disambiguated_data
682                 .data
683                 .get_opt_name()
684                 .expect("no name in item_ident");
685             let span = self
686                 .root
687                 .tables
688                 .ident_span
689                 .get(self, item_index)
690                 .map(|data| data.decode((self, sess)))
691                 .unwrap_or_else(|| panic!("Missing ident span for {:?} ({:?})", name, item_index));
692             Ident::new(name, span)
693         } else {
694             Ident::new(
695                 Symbol::intern(self.raw_proc_macro(item_index).name()),
696                 self.get_span(item_index, sess),
697             )
698         }
699     }
700
701     fn def_kind(&self, index: DefIndex) -> DefKind {
702         if !self.is_proc_macro(index) {
703             self.kind(index).def_kind()
704         } else {
705             DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
706         }
707     }
708
709     fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
710         self.root.tables.span.get(self, index).unwrap().decode((self, sess))
711     }
712
713     fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
714         let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
715             ProcMacro::CustomDerive { trait_name, attributes, client } => {
716                 let helper_attrs =
717                     attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
718                 (
719                     trait_name,
720                     SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
721                     helper_attrs,
722                 )
723             }
724             ProcMacro::Attr { name, client } => {
725                 (name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new())
726             }
727             ProcMacro::Bang { name, client } => {
728                 (name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new())
729             }
730         };
731
732         SyntaxExtension::new(
733             &sess.parse_sess,
734             kind,
735             self.get_span(id, sess),
736             helper_attrs,
737             self.root.edition,
738             Symbol::intern(name),
739             &self.get_item_attrs(id, sess),
740         )
741     }
742
743     fn get_trait_def(&self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
744         match self.kind(item_id) {
745             EntryKind::Trait(data) => {
746                 let data = data.decode((self, sess));
747                 ty::TraitDef::new(
748                     self.local_def_id(item_id),
749                     data.unsafety,
750                     data.paren_sugar,
751                     data.has_auto_impl,
752                     data.is_marker,
753                     data.specialization_kind,
754                     self.def_path_table.def_path_hash(item_id),
755                 )
756             }
757             EntryKind::TraitAlias => ty::TraitDef::new(
758                 self.local_def_id(item_id),
759                 hir::Unsafety::Normal,
760                 false,
761                 false,
762                 false,
763                 ty::trait_def::TraitSpecializationKind::None,
764                 self.def_path_table.def_path_hash(item_id),
765             ),
766             _ => bug!("def-index does not refer to trait or trait alias"),
767         }
768     }
769
770     fn get_variant(
771         &self,
772         tcx: TyCtxt<'tcx>,
773         kind: &EntryKind,
774         index: DefIndex,
775         parent_did: DefId,
776         sess: &Session,
777     ) -> ty::VariantDef {
778         let data = match kind {
779             EntryKind::Variant(data) | EntryKind::Struct(data, _) | EntryKind::Union(data, _) => {
780                 data.decode(self)
781             }
782             _ => bug!(),
783         };
784
785         let adt_kind = match kind {
786             EntryKind::Variant(_) => ty::AdtKind::Enum,
787             EntryKind::Struct(..) => ty::AdtKind::Struct,
788             EntryKind::Union(..) => ty::AdtKind::Union,
789             _ => bug!(),
790         };
791
792         let variant_did =
793             if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
794         let ctor_did = data.ctor.map(|index| self.local_def_id(index));
795
796         ty::VariantDef::new(
797             tcx,
798             self.item_ident(index, sess),
799             variant_did,
800             ctor_did,
801             data.discr,
802             self.root
803                 .tables
804                 .children
805                 .get(self, index)
806                 .unwrap_or(Lazy::empty())
807                 .decode(self)
808                 .map(|index| ty::FieldDef {
809                     did: self.local_def_id(index),
810                     ident: self.item_ident(index, sess),
811                     vis: self.get_visibility(index),
812                 })
813                 .collect(),
814             data.ctor_kind,
815             adt_kind,
816             parent_did,
817             false,
818         )
819     }
820
821     fn get_adt_def(&self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> &'tcx ty::AdtDef {
822         let kind = self.kind(item_id);
823         let did = self.local_def_id(item_id);
824
825         let (adt_kind, repr) = match kind {
826             EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
827             EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
828             EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
829             _ => bug!("get_adt_def called on a non-ADT {:?}", did),
830         };
831
832         let variants = if let ty::AdtKind::Enum = adt_kind {
833             self.root
834                 .tables
835                 .children
836                 .get(self, item_id)
837                 .unwrap_or(Lazy::empty())
838                 .decode(self)
839                 .map(|index| self.get_variant(tcx, &self.kind(index), index, did, tcx.sess))
840                 .collect()
841         } else {
842             std::iter::once(self.get_variant(tcx, &kind, item_id, did, tcx.sess)).collect()
843         };
844
845         tcx.alloc_adt_def(did, adt_kind, variants, repr)
846     }
847
848     fn get_explicit_predicates(
849         &self,
850         item_id: DefIndex,
851         tcx: TyCtxt<'tcx>,
852     ) -> ty::GenericPredicates<'tcx> {
853         self.root.tables.explicit_predicates.get(self, item_id).unwrap().decode((self, tcx))
854     }
855
856     fn get_inferred_outlives(
857         &self,
858         item_id: DefIndex,
859         tcx: TyCtxt<'tcx>,
860     ) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
861         self.root
862             .tables
863             .inferred_outlives
864             .get(self, item_id)
865             .map(|predicates| predicates.decode((self, tcx)))
866             .unwrap_or_default()
867     }
868
869     fn get_super_predicates(
870         &self,
871         item_id: DefIndex,
872         tcx: TyCtxt<'tcx>,
873     ) -> ty::GenericPredicates<'tcx> {
874         self.root.tables.super_predicates.get(self, item_id).unwrap().decode((self, tcx))
875     }
876
877     fn get_generics(&self, item_id: DefIndex, sess: &Session) -> ty::Generics {
878         self.root.tables.generics.get(self, item_id).unwrap().decode((self, sess))
879     }
880
881     fn get_type(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
882         self.root.tables.ty.get(self, id).unwrap().decode((self, tcx))
883     }
884
885     fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
886         match self.is_proc_macro(id) {
887             true => self.root.proc_macro_stability,
888             false => self.root.tables.stability.get(self, id).map(|stab| stab.decode(self)),
889         }
890     }
891
892     fn get_const_stability(&self, id: DefIndex) -> Option<attr::ConstStability> {
893         self.root.tables.const_stability.get(self, id).map(|stab| stab.decode(self))
894     }
895
896     fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
897         self.root
898             .tables
899             .deprecation
900             .get(self, id)
901             .filter(|_| !self.is_proc_macro(id))
902             .map(|depr| depr.decode(self))
903     }
904
905     fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
906         match self.is_proc_macro(id) {
907             true => ty::Visibility::Public,
908             false => self.root.tables.visibility.get(self, id).unwrap().decode(self),
909         }
910     }
911
912     fn get_impl_data(&self, id: DefIndex) -> ImplData {
913         match self.kind(id) {
914             EntryKind::Impl(data) => data.decode(self),
915             _ => bug!(),
916         }
917     }
918
919     fn get_parent_impl(&self, id: DefIndex) -> Option<DefId> {
920         self.get_impl_data(id).parent_impl
921     }
922
923     fn get_impl_polarity(&self, id: DefIndex) -> ty::ImplPolarity {
924         self.get_impl_data(id).polarity
925     }
926
927     fn get_impl_defaultness(&self, id: DefIndex) -> hir::Defaultness {
928         self.get_impl_data(id).defaultness
929     }
930
931     fn get_coerce_unsized_info(&self, id: DefIndex) -> Option<ty::adjustment::CoerceUnsizedInfo> {
932         self.get_impl_data(id).coerce_unsized_info
933     }
934
935     fn get_impl_trait(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
936         self.root.tables.impl_trait_ref.get(self, id).map(|tr| tr.decode((self, tcx)))
937     }
938
939     /// Iterates over all the stability attributes in the given crate.
940     fn get_lib_features(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option<Symbol>)] {
941         // FIXME: For a proc macro crate, not sure whether we should return the "host"
942         // features or an empty Vec. Both don't cause ICEs.
943         tcx.arena.alloc_from_iter(self.root.lib_features.decode(self))
944     }
945
946     /// Iterates over the language items in the given crate.
947     fn get_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
948         if self.root.is_proc_macro_crate() {
949             // Proc macro crates do not export any lang-items to the target.
950             &[]
951         } else {
952             tcx.arena.alloc_from_iter(
953                 self.root
954                     .lang_items
955                     .decode(self)
956                     .map(|(def_index, index)| (self.local_def_id(def_index), index)),
957             )
958         }
959     }
960
961     /// Iterates over the diagnostic items in the given crate.
962     fn get_diagnostic_items(&self) -> FxHashMap<Symbol, DefId> {
963         if self.root.is_proc_macro_crate() {
964             // Proc macro crates do not export any diagnostic-items to the target.
965             Default::default()
966         } else {
967             self.root
968                 .diagnostic_items
969                 .decode(self)
970                 .map(|(name, def_index)| (name, self.local_def_id(def_index)))
971                 .collect()
972         }
973     }
974
975     /// Iterates over each child of the given item.
976     fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
977     where
978         F: FnMut(Export<hir::HirId>),
979     {
980         if let Some(proc_macros_ids) = self.root.proc_macro_data.map(|d| d.decode(self)) {
981             /* If we are loading as a proc macro, we want to return the view of this crate
982              * as a proc macro crate.
983              */
984             if id == CRATE_DEF_INDEX {
985                 for def_index in proc_macros_ids {
986                     let raw_macro = self.raw_proc_macro(def_index);
987                     let res = Res::Def(
988                         DefKind::Macro(macro_kind(raw_macro)),
989                         self.local_def_id(def_index),
990                     );
991                     let ident = self.item_ident(def_index, sess);
992                     callback(Export {
993                         ident,
994                         res,
995                         vis: ty::Visibility::Public,
996                         span: self.get_span(def_index, sess),
997                     });
998                 }
999             }
1000             return;
1001         }
1002
1003         // Find the item.
1004         let kind = match self.maybe_kind(id) {
1005             None => return,
1006             Some(kind) => kind,
1007         };
1008
1009         // Iterate over all children.
1010         let macros_only = self.dep_kind.lock().macros_only();
1011         let children = self.root.tables.children.get(self, id).unwrap_or(Lazy::empty());
1012         for child_index in children.decode((self, sess)) {
1013             if macros_only {
1014                 continue;
1015             }
1016
1017             // Get the item.
1018             if let Some(child_kind) = self.maybe_kind(child_index) {
1019                 match child_kind {
1020                     EntryKind::MacroDef(..) => {}
1021                     _ if macros_only => continue,
1022                     _ => {}
1023                 }
1024
1025                 // Hand off the item to the callback.
1026                 match child_kind {
1027                     // FIXME(eddyb) Don't encode these in children.
1028                     EntryKind::ForeignMod => {
1029                         let child_children = self
1030                             .root
1031                             .tables
1032                             .children
1033                             .get(self, child_index)
1034                             .unwrap_or(Lazy::empty());
1035                         for child_index in child_children.decode((self, sess)) {
1036                             let kind = self.def_kind(child_index);
1037                             callback(Export {
1038                                 res: Res::Def(kind, self.local_def_id(child_index)),
1039                                 ident: self.item_ident(child_index, sess),
1040                                 vis: self.get_visibility(child_index),
1041                                 span: self
1042                                     .root
1043                                     .tables
1044                                     .span
1045                                     .get(self, child_index)
1046                                     .unwrap()
1047                                     .decode((self, sess)),
1048                             });
1049                         }
1050                         continue;
1051                     }
1052                     EntryKind::Impl(_) => continue,
1053
1054                     _ => {}
1055                 }
1056
1057                 let def_key = self.def_key(child_index);
1058                 let span = self.get_span(child_index, sess);
1059                 if def_key.disambiguated_data.data.get_opt_name().is_some() {
1060                     let kind = self.def_kind(child_index);
1061                     let ident = self.item_ident(child_index, sess);
1062                     let vis = self.get_visibility(child_index);
1063                     let def_id = self.local_def_id(child_index);
1064                     let res = Res::Def(kind, def_id);
1065                     callback(Export { res, ident, vis, span });
1066                     // For non-re-export structs and variants add their constructors to children.
1067                     // Re-export lists automatically contain constructors when necessary.
1068                     match kind {
1069                         DefKind::Struct => {
1070                             if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
1071                                 let ctor_kind = self.get_ctor_kind(child_index);
1072                                 let ctor_res =
1073                                     Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1074                                 let vis = self.get_visibility(ctor_def_id.index);
1075                                 callback(Export { res: ctor_res, vis, ident, span });
1076                             }
1077                         }
1078                         DefKind::Variant => {
1079                             // Braced variants, unlike structs, generate unusable names in
1080                             // value namespace, they are reserved for possible future use.
1081                             // It's ok to use the variant's id as a ctor id since an
1082                             // error will be reported on any use of such resolution anyway.
1083                             let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
1084                             let ctor_kind = self.get_ctor_kind(child_index);
1085                             let ctor_res =
1086                                 Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
1087                             let mut vis = self.get_visibility(ctor_def_id.index);
1088                             if ctor_def_id == def_id && vis == ty::Visibility::Public {
1089                                 // For non-exhaustive variants lower the constructor visibility to
1090                                 // within the crate. We only need this for fictive constructors,
1091                                 // for other constructors correct visibilities
1092                                 // were already encoded in metadata.
1093                                 let attrs = self.get_item_attrs(def_id.index, sess);
1094                                 if attr::contains_name(&attrs, sym::non_exhaustive) {
1095                                     let crate_def_id = self.local_def_id(CRATE_DEF_INDEX);
1096                                     vis = ty::Visibility::Restricted(crate_def_id);
1097                                 }
1098                             }
1099                             callback(Export { res: ctor_res, ident, vis, span });
1100                         }
1101                         _ => {}
1102                     }
1103                 }
1104             }
1105         }
1106
1107         if let EntryKind::Mod(data) = kind {
1108             for exp in data.decode((self, sess)).reexports.decode((self, sess)) {
1109                 match exp.res {
1110                     Res::Def(DefKind::Macro(..), _) => {}
1111                     _ if macros_only => continue,
1112                     _ => {}
1113                 }
1114                 callback(exp);
1115             }
1116         }
1117     }
1118
1119     fn is_item_mir_available(&self, id: DefIndex) -> bool {
1120         !self.is_proc_macro(id) && self.root.tables.mir.get(self, id).is_some()
1121     }
1122
1123     fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
1124         self.root
1125             .tables
1126             .mir
1127             .get(self, id)
1128             .filter(|_| !self.is_proc_macro(id))
1129             .unwrap_or_else(|| {
1130                 bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
1131             })
1132             .decode((self, tcx))
1133     }
1134
1135     fn get_unused_generic_params(&self, id: DefIndex) -> FiniteBitSet<u64> {
1136         self.root
1137             .tables
1138             .unused_generic_params
1139             .get(self, id)
1140             .filter(|_| !self.is_proc_macro(id))
1141             .map(|params| params.decode(self))
1142             .unwrap_or_default()
1143     }
1144
1145     fn get_promoted_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> IndexVec<Promoted, Body<'tcx>> {
1146         self.root
1147             .tables
1148             .promoted_mir
1149             .get(self, id)
1150             .filter(|_| !self.is_proc_macro(id))
1151             .unwrap_or_else(|| {
1152                 bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
1153             })
1154             .decode((self, tcx))
1155     }
1156
1157     fn mir_const_qualif(&self, id: DefIndex) -> mir::ConstQualifs {
1158         match self.kind(id) {
1159             EntryKind::AnonConst(qualif, _)
1160             | EntryKind::Const(qualif, _)
1161             | EntryKind::AssocConst(
1162                 AssocContainer::ImplDefault
1163                 | AssocContainer::ImplFinal
1164                 | AssocContainer::TraitWithDefault,
1165                 qualif,
1166                 _,
1167             ) => qualif,
1168             _ => bug!("mir_const_qualif: unexpected kind"),
1169         }
1170     }
1171
1172     fn get_associated_item(&self, id: DefIndex, sess: &Session) -> ty::AssocItem {
1173         let def_key = self.def_key(id);
1174         let parent = self.local_def_id(def_key.parent.unwrap());
1175         let ident = self.item_ident(id, sess);
1176
1177         let (kind, container, has_self) = match self.kind(id) {
1178             EntryKind::AssocConst(container, _, _) => (ty::AssocKind::Const, container, false),
1179             EntryKind::AssocFn(data) => {
1180                 let data = data.decode(self);
1181                 (ty::AssocKind::Fn, data.container, data.has_self)
1182             }
1183             EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
1184             _ => bug!("cannot get associated-item of `{:?}`", def_key),
1185         };
1186
1187         ty::AssocItem {
1188             ident,
1189             kind,
1190             vis: self.get_visibility(id),
1191             defaultness: container.defaultness(),
1192             def_id: self.local_def_id(id),
1193             container: container.with_def_id(parent),
1194             fn_has_self_parameter: has_self,
1195         }
1196     }
1197
1198     fn get_item_variances(&self, id: DefIndex) -> Vec<ty::Variance> {
1199         self.root.tables.variances.get(self, id).unwrap_or(Lazy::empty()).decode(self).collect()
1200     }
1201
1202     fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
1203         match self.kind(node_id) {
1204             EntryKind::Struct(data, _) | EntryKind::Union(data, _) | EntryKind::Variant(data) => {
1205                 data.decode(self).ctor_kind
1206             }
1207             _ => CtorKind::Fictive,
1208         }
1209     }
1210
1211     fn get_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
1212         match self.kind(node_id) {
1213             EntryKind::Struct(data, _) => {
1214                 data.decode(self).ctor.map(|index| self.local_def_id(index))
1215             }
1216             EntryKind::Variant(data) => {
1217                 data.decode(self).ctor.map(|index| self.local_def_id(index))
1218             }
1219             _ => None,
1220         }
1221     }
1222
1223     fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Vec<ast::Attribute> {
1224         // The attributes for a tuple struct/variant are attached to the definition, not the ctor;
1225         // we assume that someone passing in a tuple struct ctor is actually wanting to
1226         // look at the definition
1227         let def_key = self.def_key(node_id);
1228         let item_id = if def_key.disambiguated_data.data == DefPathData::Ctor {
1229             def_key.parent.unwrap()
1230         } else {
1231             node_id
1232         };
1233
1234         self.root
1235             .tables
1236             .attributes
1237             .get(self, item_id)
1238             .unwrap_or(Lazy::empty())
1239             .decode((self, sess))
1240             .collect::<Vec<_>>()
1241     }
1242
1243     fn get_struct_field_names(&self, id: DefIndex, sess: &Session) -> Vec<Spanned<Symbol>> {
1244         self.root
1245             .tables
1246             .children
1247             .get(self, id)
1248             .unwrap_or(Lazy::empty())
1249             .decode(self)
1250             .map(|index| respan(self.get_span(index, sess), self.item_ident(index, sess).name))
1251             .collect()
1252     }
1253
1254     fn get_inherent_implementations_for_type(
1255         &self,
1256         tcx: TyCtxt<'tcx>,
1257         id: DefIndex,
1258     ) -> &'tcx [DefId] {
1259         tcx.arena.alloc_from_iter(
1260             self.root
1261                 .tables
1262                 .inherent_impls
1263                 .get(self, id)
1264                 .unwrap_or(Lazy::empty())
1265                 .decode(self)
1266                 .map(|index| self.local_def_id(index)),
1267         )
1268     }
1269
1270     fn get_implementations_for_trait(
1271         &self,
1272         tcx: TyCtxt<'tcx>,
1273         filter: Option<DefId>,
1274     ) -> &'tcx [DefId] {
1275         if self.root.is_proc_macro_crate() {
1276             // proc-macro crates export no trait impls.
1277             return &[];
1278         }
1279
1280         // Do a reverse lookup beforehand to avoid touching the crate_num
1281         // hash map in the loop below.
1282         let filter = match filter.map(|def_id| self.reverse_translate_def_id(def_id)) {
1283             Some(Some(def_id)) => Some((def_id.krate.as_u32(), def_id.index)),
1284             Some(None) => return &[],
1285             None => None,
1286         };
1287
1288         if let Some(filter) = filter {
1289             if let Some(impls) = self.trait_impls.get(&filter) {
1290                 tcx.arena.alloc_from_iter(impls.decode(self).map(|idx| self.local_def_id(idx)))
1291             } else {
1292                 &[]
1293             }
1294         } else {
1295             tcx.arena.alloc_from_iter(
1296                 self.trait_impls
1297                     .values()
1298                     .flat_map(|impls| impls.decode(self).map(|idx| self.local_def_id(idx))),
1299             )
1300         }
1301     }
1302
1303     fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
1304         let def_key = self.def_key(id);
1305         match def_key.disambiguated_data.data {
1306             DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
1307             // Not an associated item
1308             _ => return None,
1309         }
1310         def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
1311             EntryKind::Trait(_) | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
1312             _ => None,
1313         })
1314     }
1315
1316     fn get_native_libraries(&self, sess: &Session) -> Vec<NativeLib> {
1317         if self.root.is_proc_macro_crate() {
1318             // Proc macro crates do not have any *target* native libraries.
1319             vec![]
1320         } else {
1321             self.root.native_libraries.decode((self, sess)).collect()
1322         }
1323     }
1324
1325     fn get_foreign_modules(&self, tcx: TyCtxt<'tcx>) -> &'tcx [ForeignModule] {
1326         if self.root.is_proc_macro_crate() {
1327             // Proc macro crates do not have any *target* foreign modules.
1328             &[]
1329         } else {
1330             tcx.arena.alloc_from_iter(self.root.foreign_modules.decode((self, tcx.sess)))
1331         }
1332     }
1333
1334     fn get_dylib_dependency_formats(
1335         &self,
1336         tcx: TyCtxt<'tcx>,
1337     ) -> &'tcx [(CrateNum, LinkagePreference)] {
1338         tcx.arena.alloc_from_iter(
1339             self.root.dylib_dependency_formats.decode(self).enumerate().flat_map(|(i, link)| {
1340                 let cnum = CrateNum::new(i + 1);
1341                 link.map(|link| (self.cnum_map[cnum], link))
1342             }),
1343         )
1344     }
1345
1346     fn get_missing_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] {
1347         if self.root.is_proc_macro_crate() {
1348             // Proc macro crates do not depend on any target weak lang-items.
1349             &[]
1350         } else {
1351             tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
1352         }
1353     }
1354
1355     fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Ident] {
1356         let param_names = match self.kind(id) {
1357             EntryKind::Fn(data) | EntryKind::ForeignFn(data) => data.decode(self).param_names,
1358             EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names,
1359             _ => Lazy::empty(),
1360         };
1361         tcx.arena.alloc_from_iter(param_names.decode((self, tcx)))
1362     }
1363
1364     fn exported_symbols(
1365         &self,
1366         tcx: TyCtxt<'tcx>,
1367     ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
1368         if self.root.is_proc_macro_crate() {
1369             // If this crate is a custom derive crate, then we're not even going to
1370             // link those in so we skip those crates.
1371             &[]
1372         } else {
1373             tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
1374         }
1375     }
1376
1377     fn get_rendered_const(&self, id: DefIndex) -> String {
1378         match self.kind(id) {
1379             EntryKind::AnonConst(_, data)
1380             | EntryKind::Const(_, data)
1381             | EntryKind::AssocConst(_, _, data) => data.decode(self).0,
1382             _ => bug!(),
1383         }
1384     }
1385
1386     fn get_macro(&self, id: DefIndex, sess: &Session) -> MacroDef {
1387         match self.kind(id) {
1388             EntryKind::MacroDef(macro_def) => macro_def.decode((self, sess)),
1389             _ => bug!(),
1390         }
1391     }
1392
1393     // This replicates some of the logic of the crate-local `is_const_fn_raw` query, because we
1394     // don't serialize constness for tuple variant and tuple struct constructors.
1395     fn is_const_fn_raw(&self, id: DefIndex) -> bool {
1396         let constness = match self.kind(id) {
1397             EntryKind::AssocFn(data) => data.decode(self).fn_data.constness,
1398             EntryKind::Fn(data) => data.decode(self).constness,
1399             EntryKind::ForeignFn(data) => data.decode(self).constness,
1400             EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1401             _ => hir::Constness::NotConst,
1402         };
1403         constness == hir::Constness::Const
1404     }
1405
1406     fn asyncness(&self, id: DefIndex) -> hir::IsAsync {
1407         match self.kind(id) {
1408             EntryKind::Fn(data) => data.decode(self).asyncness,
1409             EntryKind::AssocFn(data) => data.decode(self).fn_data.asyncness,
1410             EntryKind::ForeignFn(data) => data.decode(self).asyncness,
1411             _ => bug!("asyncness: expected function kind"),
1412         }
1413     }
1414
1415     fn is_foreign_item(&self, id: DefIndex) -> bool {
1416         match self.kind(id) {
1417             EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => {
1418                 true
1419             }
1420             _ => false,
1421         }
1422     }
1423
1424     fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
1425         match self.kind(id) {
1426             EntryKind::ImmStatic | EntryKind::ForeignImmStatic => Some(hir::Mutability::Not),
1427             EntryKind::MutStatic | EntryKind::ForeignMutStatic => Some(hir::Mutability::Mut),
1428             _ => None,
1429         }
1430     }
1431
1432     fn generator_kind(&self, id: DefIndex) -> Option<hir::GeneratorKind> {
1433         match self.kind(id) {
1434             EntryKind::Generator(data) => Some(data),
1435             _ => None,
1436         }
1437     }
1438
1439     fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1440         self.root.tables.fn_sig.get(self, id).unwrap().decode((self, tcx))
1441     }
1442
1443     #[inline]
1444     fn def_key(&self, index: DefIndex) -> DefKey {
1445         let mut key = self.def_path_table.def_key(index);
1446         if self.is_proc_macro(index) {
1447             let name = self.raw_proc_macro(index).name();
1448             key.disambiguated_data.data = DefPathData::MacroNs(Symbol::intern(name));
1449         }
1450         key
1451     }
1452
1453     // Returns the path leading to the thing with this `id`.
1454     fn def_path(&self, id: DefIndex) -> DefPath {
1455         debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1456         DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1457     }
1458
1459     /// Imports the source_map from an external crate into the source_map of the crate
1460     /// currently being compiled (the "local crate").
1461     ///
1462     /// The import algorithm works analogous to how AST items are inlined from an
1463     /// external crate's metadata:
1464     /// For every SourceFile in the external source_map an 'inline' copy is created in the
1465     /// local source_map. The correspondence relation between external and local
1466     /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1467     /// function. When an item from an external crate is later inlined into this
1468     /// crate, this correspondence information is used to translate the span
1469     /// information of the inlined item so that it refers the correct positions in
1470     /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1471     ///
1472     /// The import algorithm in the function below will reuse SourceFiles already
1473     /// existing in the local source_map. For example, even if the SourceFile of some
1474     /// source file of libstd gets imported many times, there will only ever be
1475     /// one SourceFile object for the corresponding file in the local source_map.
1476     ///
1477     /// Note that imported SourceFiles do not actually contain the source code of the
1478     /// file they represent, just information about length, line breaks, and
1479     /// multibyte characters. This information is enough to generate valid debuginfo
1480     /// for items inlined from other crates.
1481     ///
1482     /// Proc macro crates don't currently export spans, so this function does not have
1483     /// to work for them.
1484     fn imported_source_files(&self, sess: &Session) -> &'a [ImportedSourceFile] {
1485         // Translate the virtual `/rustc/$hash` prefix back to a real directory
1486         // that should hold actual sources, where possible.
1487         let virtual_rust_source_base_dir = option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR")
1488             .map(Path::new)
1489             .filter(|_| {
1490                 // Only spend time on further checks if we have what to translate *to*.
1491                 sess.real_rust_source_base_dir.is_some()
1492             })
1493             .filter(|virtual_dir| {
1494                 // Don't translate away `/rustc/$hash` if we're still remapping to it,
1495                 // since that means we're still building `std`/`rustc` that need it,
1496                 // and we don't want the real path to leak into codegen/debuginfo.
1497                 !sess.opts.remap_path_prefix.iter().any(|(_from, to)| to == virtual_dir)
1498             });
1499         let try_to_translate_virtual_to_real = |name: &mut rustc_span::FileName| {
1500             debug!(
1501                 "try_to_translate_virtual_to_real(name={:?}): \
1502                  virtual_rust_source_base_dir={:?}, real_rust_source_base_dir={:?}",
1503                 name, virtual_rust_source_base_dir, sess.real_rust_source_base_dir,
1504             );
1505
1506             if let Some(virtual_dir) = virtual_rust_source_base_dir {
1507                 if let Some(real_dir) = &sess.real_rust_source_base_dir {
1508                     if let rustc_span::FileName::Real(old_name) = name {
1509                         if let rustc_span::RealFileName::Named(one_path) = old_name {
1510                             if let Ok(rest) = one_path.strip_prefix(virtual_dir) {
1511                                 let virtual_name = one_path.clone();
1512                                 let new_path = real_dir.join(rest);
1513                                 debug!(
1514                                     "try_to_translate_virtual_to_real: `{}` -> `{}`",
1515                                     virtual_name.display(),
1516                                     new_path.display(),
1517                                 );
1518                                 let new_name = rustc_span::RealFileName::Devirtualized {
1519                                     local_path: new_path,
1520                                     virtual_name,
1521                                 };
1522                                 *old_name = new_name;
1523                             }
1524                         }
1525                     }
1526                 }
1527             }
1528         };
1529
1530         self.cdata.source_map_import_info.get_or_init(|| {
1531             let external_source_map = self.root.source_map.decode(self);
1532
1533             external_source_map
1534                 .map(|source_file_to_import| {
1535                     // We can't reuse an existing SourceFile, so allocate a new one
1536                     // containing the information we need.
1537                     let rustc_span::SourceFile {
1538                         mut name,
1539                         name_was_remapped,
1540                         src_hash,
1541                         start_pos,
1542                         end_pos,
1543                         mut lines,
1544                         mut multibyte_chars,
1545                         mut non_narrow_chars,
1546                         mut normalized_pos,
1547                         name_hash,
1548                         ..
1549                     } = source_file_to_import;
1550
1551                     // If this file's path has been remapped to `/rustc/$hash`,
1552                     // we might be able to reverse that (also see comments above,
1553                     // on `try_to_translate_virtual_to_real`).
1554                     // FIXME(eddyb) we could check `name_was_remapped` here,
1555                     // but in practice it seems to be always `false`.
1556                     try_to_translate_virtual_to_real(&mut name);
1557
1558                     let source_length = (end_pos - start_pos).to_usize();
1559
1560                     // Translate line-start positions and multibyte character
1561                     // position into frame of reference local to file.
1562                     // `SourceMap::new_imported_source_file()` will then translate those
1563                     // coordinates to their new global frame of reference when the
1564                     // offset of the SourceFile is known.
1565                     for pos in &mut lines {
1566                         *pos = *pos - start_pos;
1567                     }
1568                     for mbc in &mut multibyte_chars {
1569                         mbc.pos = mbc.pos - start_pos;
1570                     }
1571                     for swc in &mut non_narrow_chars {
1572                         *swc = *swc - start_pos;
1573                     }
1574                     for np in &mut normalized_pos {
1575                         np.pos = np.pos - start_pos;
1576                     }
1577
1578                     let local_version = sess.source_map().new_imported_source_file(
1579                         name,
1580                         name_was_remapped,
1581                         src_hash,
1582                         name_hash,
1583                         source_length,
1584                         self.cnum,
1585                         lines,
1586                         multibyte_chars,
1587                         non_narrow_chars,
1588                         normalized_pos,
1589                         start_pos,
1590                         end_pos,
1591                     );
1592                     debug!(
1593                         "CrateMetaData::imported_source_files alloc \
1594                          source_file {:?} original (start_pos {:?} end_pos {:?}) \
1595                          translated (start_pos {:?} end_pos {:?})",
1596                         local_version.name,
1597                         start_pos,
1598                         end_pos,
1599                         local_version.start_pos,
1600                         local_version.end_pos
1601                     );
1602
1603                     ImportedSourceFile {
1604                         original_start_pos: start_pos,
1605                         original_end_pos: end_pos,
1606                         translated_source_file: local_version,
1607                     }
1608                 })
1609                 .collect()
1610         })
1611     }
1612 }
1613
1614 impl CrateMetadata {
1615     crate fn new(
1616         sess: &Session,
1617         blob: MetadataBlob,
1618         root: CrateRoot<'static>,
1619         raw_proc_macros: Option<&'static [ProcMacro]>,
1620         cnum: CrateNum,
1621         cnum_map: CrateNumMap,
1622         dep_kind: DepKind,
1623         source: CrateSource,
1624         private_dep: bool,
1625         host_hash: Option<Svh>,
1626     ) -> CrateMetadata {
1627         let def_path_table = record_time(&sess.perf_stats.decode_def_path_tables_time, || {
1628             root.def_path_table.decode((&blob, sess))
1629         });
1630         let trait_impls = root
1631             .impls
1632             .decode((&blob, sess))
1633             .map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
1634             .collect();
1635         let alloc_decoding_state =
1636             AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1637         let dependencies = Lock::new(cnum_map.iter().cloned().collect());
1638         CrateMetadata {
1639             blob,
1640             root,
1641             def_path_table,
1642             trait_impls,
1643             raw_proc_macros,
1644             source_map_import_info: OnceCell::new(),
1645             alloc_decoding_state,
1646             dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
1647             cnum,
1648             cnum_map,
1649             dependencies,
1650             dep_kind: Lock::new(dep_kind),
1651             source,
1652             private_dep,
1653             host_hash,
1654             extern_crate: Lock::new(None),
1655         }
1656     }
1657
1658     crate fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
1659         self.dependencies.borrow()
1660     }
1661
1662     crate fn add_dependency(&self, cnum: CrateNum) {
1663         self.dependencies.borrow_mut().push(cnum);
1664     }
1665
1666     crate fn update_extern_crate(&self, new_extern_crate: ExternCrate) -> bool {
1667         let mut extern_crate = self.extern_crate.borrow_mut();
1668         let update = Some(new_extern_crate.rank()) > extern_crate.as_ref().map(ExternCrate::rank);
1669         if update {
1670             *extern_crate = Some(new_extern_crate);
1671         }
1672         update
1673     }
1674
1675     crate fn source(&self) -> &CrateSource {
1676         &self.source
1677     }
1678
1679     crate fn dep_kind(&self) -> DepKind {
1680         *self.dep_kind.lock()
1681     }
1682
1683     crate fn update_dep_kind(&self, f: impl FnOnce(DepKind) -> DepKind) {
1684         self.dep_kind.with_lock(|dep_kind| *dep_kind = f(*dep_kind))
1685     }
1686
1687     crate fn panic_strategy(&self) -> PanicStrategy {
1688         self.root.panic_strategy
1689     }
1690
1691     crate fn needs_panic_runtime(&self) -> bool {
1692         self.root.needs_panic_runtime
1693     }
1694
1695     crate fn is_panic_runtime(&self) -> bool {
1696         self.root.panic_runtime
1697     }
1698
1699     crate fn is_profiler_runtime(&self) -> bool {
1700         self.root.profiler_runtime
1701     }
1702
1703     crate fn needs_allocator(&self) -> bool {
1704         self.root.needs_allocator
1705     }
1706
1707     crate fn has_global_allocator(&self) -> bool {
1708         self.root.has_global_allocator
1709     }
1710
1711     crate fn has_default_lib_allocator(&self) -> bool {
1712         self.root.has_default_lib_allocator
1713     }
1714
1715     crate fn is_proc_macro_crate(&self) -> bool {
1716         self.root.is_proc_macro_crate()
1717     }
1718
1719     crate fn name(&self) -> Symbol {
1720         self.root.name
1721     }
1722
1723     crate fn disambiguator(&self) -> CrateDisambiguator {
1724         self.root.disambiguator
1725     }
1726
1727     crate fn hash(&self) -> Svh {
1728         self.root.hash
1729     }
1730
1731     fn local_def_id(&self, index: DefIndex) -> DefId {
1732         DefId { krate: self.cnum, index }
1733     }
1734
1735     // Translate a DefId from the current compilation environment to a DefId
1736     // for an external crate.
1737     fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
1738         for (local, &global) in self.cnum_map.iter_enumerated() {
1739             if global == did.krate {
1740                 return Some(DefId { krate: local, index: did.index });
1741             }
1742         }
1743
1744         None
1745     }
1746
1747     #[inline]
1748     fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
1749         self.def_path_table.def_path_hash(index)
1750     }
1751
1752     /// Get the `DepNodeIndex` corresponding this crate. The result of this
1753     /// method is cached in the `dep_node_index` field.
1754     fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1755         let mut dep_node_index = self.dep_node_index.load();
1756
1757         if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1758             // We have not cached the DepNodeIndex for this upstream crate yet,
1759             // so use the dep-graph to find it out and cache it.
1760             // Note that multiple threads can enter this block concurrently.
1761             // That is fine because the DepNodeIndex remains constant
1762             // throughout the whole compilation session, and multiple stores
1763             // would always write the same value.
1764
1765             let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1766             let dep_node =
1767                 DepNode::from_def_path_hash(def_path_hash, dep_graph::DepKind::CrateMetadata);
1768
1769             dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1770             assert!(dep_node_index != DepNodeIndex::INVALID);
1771             self.dep_node_index.store(dep_node_index);
1772         }
1773
1774         dep_node_index
1775     }
1776 }
1777
1778 // Cannot be implemented on 'ProcMacro', as libproc_macro
1779 // does not depend on librustc_ast
1780 fn macro_kind(raw: &ProcMacro) -> MacroKind {
1781     match raw {
1782         ProcMacro::CustomDerive { .. } => MacroKind::Derive,
1783         ProcMacro::Attr { .. } => MacroKind::Attr,
1784         ProcMacro::Bang { .. } => MacroKind::Bang,
1785     }
1786 }