1 // Decoding metadata from a single crate's metadata
3 use crate::cstore::{self, CrateMetadata, MetadataBlob};
5 use crate::table::{FixedSizeEncoding, PerDefTable};
7 use rustc_index::vec::IndexVec;
8 use rustc_data_structures::sync::{Lrc, ReadGuard};
9 use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
11 use rustc::middle::cstore::{LinkagePreference, NativeLibrary, ForeignModule};
12 use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
13 use rustc::hir::def::{self, Res, DefKind, CtorOf, CtorKind};
14 use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
15 use rustc_data_structures::fingerprint::Fingerprint;
16 use rustc_data_structures::fx::FxHashMap;
17 use rustc::dep_graph::{DepNodeIndex, DepKind};
18 use rustc::middle::lang_items;
19 use rustc::mir::{self, interpret};
20 use rustc::mir::interpret::AllocDecodingSession;
21 use rustc::session::Session;
22 use rustc::ty::{self, Ty, TyCtxt};
23 use rustc::ty::codec::TyDecoder;
24 use rustc::mir::{Body, Promoted};
25 use rustc::util::captures::Captures;
29 use std::num::NonZeroUsize;
32 use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
34 use syntax::ast::{self, Ident};
35 use syntax::source_map::{self, respan, Spanned};
36 use syntax::symbol::{Symbol, sym};
37 use syntax_expand::base::{MacroKind, SyntaxExtensionKind, SyntaxExtension};
38 use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};
40 use proc_macro::bridge::client::ProcMacro;
41 use syntax_expand::proc_macro::{AttrProcMacro, ProcMacroDerive, BangProcMacro};
43 crate struct DecodeContext<'a, 'tcx> {
44 opaque: opaque::Decoder<'a>,
45 cdata: Option<&'a CrateMetadata>,
46 sess: Option<&'tcx Session>,
47 tcx: Option<TyCtxt<'tcx>>,
49 // Cache the last used source_file for translating spans as an optimization.
50 last_source_file_index: usize,
52 lazy_state: LazyState,
54 // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
55 alloc_decoding_session: Option<AllocDecodingSession<'a>>,
58 /// Abstract over the various ways one can create metadata decoders.
59 crate trait Metadata<'a, 'tcx>: Copy {
60 fn raw_bytes(self) -> &'a [u8];
61 fn cdata(self) -> Option<&'a CrateMetadata> { None }
62 fn sess(self) -> Option<&'tcx Session> { None }
63 fn tcx(self) -> Option<TyCtxt<'tcx>> { None }
65 fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
68 opaque: opaque::Decoder::new(self.raw_bytes(), pos),
70 sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
72 last_source_file_index: 0,
73 lazy_state: LazyState::NoNode,
74 alloc_decoding_session: self.cdata().map(|cdata| {
75 cdata.alloc_decoding_state.new_decoding_session()
81 impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
82 fn raw_bytes(self) -> &'a [u8] {
88 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a MetadataBlob, &'tcx Session) {
89 fn raw_bytes(self) -> &'a [u8] {
94 fn sess(self) -> Option<&'tcx Session> {
101 impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadata {
102 fn raw_bytes(self) -> &'a [u8] {
103 self.blob.raw_bytes()
105 fn cdata(self) -> Option<&'a CrateMetadata> {
110 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, &'tcx Session) {
111 fn raw_bytes(self) -> &'a [u8] {
114 fn cdata(self) -> Option<&'a CrateMetadata> {
117 fn sess(self) -> Option<&'tcx Session> {
122 impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
123 fn raw_bytes(self) -> &'a [u8] {
126 fn cdata(self) -> Option<&'a CrateMetadata> {
129 fn tcx(self) -> Option<TyCtxt<'tcx>> {
134 impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
135 crate fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
136 let mut dcx = metadata.decoder(self.position.get());
137 dcx.lazy_state = LazyState::NodeStart(self.position);
138 T::decode(&mut dcx).unwrap()
142 impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
143 crate fn decode<M: Metadata<'a, 'tcx>>(
146 ) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
147 let mut dcx = metadata.decoder(self.position.get());
148 dcx.lazy_state = LazyState::NodeStart(self.position);
149 (0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
153 impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
154 fn tcx(&self) -> TyCtxt<'tcx> {
155 self.tcx.expect("missing TyCtxt in DecodeContext")
158 fn cdata(&self) -> &'a CrateMetadata {
159 self.cdata.expect("missing CrateMetadata in DecodeContext")
162 fn read_lazy_with_meta<T: ?Sized + LazyMeta>(
165 ) -> Result<Lazy<T>, <Self as Decoder>::Error> {
166 let min_size = T::min_size(meta);
167 let distance = self.read_usize()?;
168 let position = match self.lazy_state {
169 LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
170 LazyState::NodeStart(start) => {
171 let start = start.get();
172 assert!(distance + min_size <= start);
173 start - distance - min_size
175 LazyState::Previous(last_min_end) => last_min_end.get() + distance,
177 self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
178 Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
182 impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
184 fn tcx(&self) -> TyCtxt<'tcx> {
185 self.tcx.expect("missing TyCtxt in DecodeContext")
189 fn peek_byte(&self) -> u8 {
190 self.opaque.data[self.opaque.position()]
194 fn position(&self) -> usize {
195 self.opaque.position()
198 fn cached_ty_for_shorthand<F>(&mut self,
201 -> Result<Ty<'tcx>, Self::Error>
202 where F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>
204 let tcx = self.tcx();
206 let key = ty::CReaderCacheKey {
207 cnum: self.cdata().cnum,
211 if let Some(&ty) = tcx.rcache.borrow().get(&key) {
215 let ty = or_insert_with(self)?;
216 tcx.rcache.borrow_mut().insert(key, ty);
220 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
221 where F: FnOnce(&mut Self) -> R
223 let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
224 let old_opaque = mem::replace(&mut self.opaque, new_opaque);
225 let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
227 self.opaque = old_opaque;
228 self.lazy_state = old_state;
232 fn map_encoded_cnum_to_current(&self, cnum: CrateNum) -> CrateNum {
233 if cnum == LOCAL_CRATE {
236 self.cdata().cnum_map[cnum]
241 impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
242 fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
243 self.read_lazy_with_meta(())
247 impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
248 fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
249 let len = self.read_usize()?;
253 self.read_lazy_with_meta(len)
258 impl<'a, 'tcx, T> SpecializedDecoder<Lazy<PerDefTable<T>>> for DecodeContext<'a, 'tcx>
259 where Option<T>: FixedSizeEncoding,
261 fn specialized_decode(&mut self) -> Result<Lazy<PerDefTable<T>>, Self::Error> {
262 let len = self.read_usize()?;
263 self.read_lazy_with_meta(len)
267 impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> {
269 fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {
270 let krate = CrateNum::decode(self)?;
271 let index = DefIndex::decode(self)?;
280 impl<'a, 'tcx> SpecializedDecoder<DefIndex> for DecodeContext<'a, 'tcx> {
282 fn specialized_decode(&mut self) -> Result<DefIndex, Self::Error> {
283 Ok(DefIndex::from_u32(self.read_u32()?))
287 impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for DecodeContext<'a, 'tcx> {
289 fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
290 self.specialized_decode().map(|i| LocalDefId::from_def_id(i))
294 impl<'a, 'tcx> SpecializedDecoder<interpret::AllocId> for DecodeContext<'a, 'tcx> {
295 fn specialized_decode(&mut self) -> Result<interpret::AllocId, Self::Error> {
296 if let Some(alloc_decoding_session) = self.alloc_decoding_session {
297 alloc_decoding_session.decode_alloc_id(self)
299 bug!("Attempting to decode interpret::AllocId without CrateMetadata")
304 impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
305 fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
306 let tag = u8::decode(self)?;
308 if tag == TAG_INVALID_SPAN {
312 debug_assert_eq!(tag, TAG_VALID_SPAN);
314 let lo = BytePos::decode(self)?;
315 let len = BytePos::decode(self)?;
318 let sess = if let Some(sess) = self.sess {
321 bug!("Cannot decode Span without Session.")
324 let imported_source_files = self.cdata().imported_source_files(&sess.source_map());
326 // Optimize for the case that most spans within a translated item
327 // originate from the same source_file.
328 let last_source_file = &imported_source_files[self.last_source_file_index];
330 if lo >= last_source_file.original_start_pos &&
331 lo <= last_source_file.original_end_pos {
335 let mut b = imported_source_files.len();
339 if imported_source_files[m].original_start_pos > lo {
346 self.last_source_file_index = a;
347 &imported_source_files[a]
351 // Make sure our binary search above is correct.
352 debug_assert!(lo >= source_file.original_start_pos &&
353 lo <= source_file.original_end_pos);
355 // Make sure we correctly filtered out invalid spans during encoding
356 debug_assert!(hi >= source_file.original_start_pos &&
357 hi <= source_file.original_end_pos);
359 let lo = (lo + source_file.translated_source_file.start_pos)
360 - source_file.original_start_pos;
361 let hi = (hi + source_file.translated_source_file.start_pos)
362 - source_file.original_start_pos;
364 Ok(Span::with_root_ctxt(lo, hi))
368 impl SpecializedDecoder<Ident> for DecodeContext<'_, '_> {
369 fn specialized_decode(&mut self) -> Result<Ident, Self::Error> {
370 // FIXME(jseyfried): intercrate hygiene
372 Ok(Ident::with_dummy_span(Symbol::decode(self)?))
376 impl<'a, 'tcx> SpecializedDecoder<Fingerprint> for DecodeContext<'a, 'tcx> {
377 fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
378 Fingerprint::decode_opaque(&mut self.opaque)
382 impl<'a, 'tcx, T: Decodable> SpecializedDecoder<mir::ClearCrossCrate<T>>
383 for DecodeContext<'a, 'tcx> {
385 fn specialized_decode(&mut self) -> Result<mir::ClearCrossCrate<T>, Self::Error> {
386 Ok(mir::ClearCrossCrate::Clear)
390 implement_ty_decoder!( DecodeContext<'a, 'tcx> );
392 impl<'tcx> MetadataBlob {
393 crate fn is_compatible(&self) -> bool {
394 self.raw_bytes().starts_with(METADATA_HEADER)
397 crate fn get_rustc_version(&self) -> String {
398 Lazy::<String>::from_position(
399 NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap(),
403 crate fn get_root(&self) -> CrateRoot<'tcx> {
404 let slice = self.raw_bytes();
405 let offset = METADATA_HEADER.len();
406 let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) |
407 ((slice[offset + 2] as u32) << 8) |
408 ((slice[offset + 3] as u32) << 0)) as usize;
409 Lazy::<CrateRoot<'tcx>>::from_position(
410 NonZeroUsize::new(pos).unwrap(),
414 crate fn list_crate_metadata(&self,
415 out: &mut dyn io::Write) -> io::Result<()> {
416 write!(out, "=External Dependencies=\n")?;
417 let root = self.get_root();
418 for (i, dep) in root.crate_deps
421 write!(out, "{} {}{}\n", i + 1, dep.name, dep.extra_filename)?;
428 impl<'tcx> EntryKind<'tcx> {
429 fn def_kind(&self) -> Option<DefKind> {
431 EntryKind::Const(..) => DefKind::Const,
432 EntryKind::AssocConst(..) => DefKind::AssocConst,
433 EntryKind::ImmStatic |
434 EntryKind::MutStatic |
435 EntryKind::ForeignImmStatic |
436 EntryKind::ForeignMutStatic => DefKind::Static,
437 EntryKind::Struct(_, _) => DefKind::Struct,
438 EntryKind::Union(_, _) => DefKind::Union,
440 EntryKind::ForeignFn(_) => DefKind::Fn,
441 EntryKind::Method(_) => DefKind::Method,
442 EntryKind::Type => DefKind::TyAlias,
443 EntryKind::TypeParam => DefKind::TyParam,
444 EntryKind::ConstParam => DefKind::ConstParam,
445 EntryKind::OpaqueTy => DefKind::OpaqueTy,
446 EntryKind::AssocType(_) => DefKind::AssocTy,
447 EntryKind::AssocOpaqueTy(_) => DefKind::AssocOpaqueTy,
448 EntryKind::Mod(_) => DefKind::Mod,
449 EntryKind::Variant(_) => DefKind::Variant,
450 EntryKind::Trait(_) => DefKind::Trait,
451 EntryKind::TraitAlias => DefKind::TraitAlias,
452 EntryKind::Enum(..) => DefKind::Enum,
453 EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
454 EntryKind::ForeignType => DefKind::ForeignTy,
456 EntryKind::ForeignMod |
457 EntryKind::GlobalAsm |
460 EntryKind::Generator(_) |
461 EntryKind::Closure => return None,
466 impl<'a, 'tcx> CrateMetadata {
467 crate fn is_proc_macro_crate(&self) -> bool {
468 self.root.proc_macro_decls_static.is_some()
471 fn is_proc_macro(&self, id: DefIndex) -> bool {
472 self.is_proc_macro_crate() &&
473 self.root.proc_macro_data.unwrap().decode(self).find(|x| *x == id).is_some()
476 fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind<'tcx>> {
477 self.root.per_def.kind.get(self, item_id).map(|k| k.decode(self))
480 fn kind(&self, item_id: DefIndex) -> EntryKind<'tcx> {
481 assert!(!self.is_proc_macro(item_id));
482 self.maybe_kind(item_id).unwrap_or_else(|| {
484 "CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
492 fn local_def_id(&self, index: DefIndex) -> DefId {
499 fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
500 // DefIndex's in root.proc_macro_data have a one-to-one correspondence
501 // with items in 'raw_proc_macros'.
502 // NOTE: If you update the order of macros in 'proc_macro_data' for any reason,
503 // you must also update src/libsyntax_ext/proc_macro_harness.rs
504 // Failing to do so will result in incorrect data being associated
505 // with proc macros when deserialized.
506 let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
507 &self.raw_proc_macros.unwrap()[pos]
510 crate fn item_name(&self, item_index: DefIndex) -> Symbol {
511 if !self.is_proc_macro(item_index) {
512 self.def_key(item_index)
516 .expect("no name in item_name")
518 Symbol::intern(self.raw_proc_macro(item_index).name())
522 crate fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
523 if !self.is_proc_macro(index) {
524 self.kind(index).def_kind()
527 macro_kind(self.raw_proc_macro(index))
532 crate fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
533 self.root.per_def.span.get(self, index).unwrap().decode((self, sess))
536 crate fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
537 let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
538 ProcMacro::CustomDerive { trait_name, attributes, client } => {
540 attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
543 SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
547 ProcMacro::Attr { name, client } => (
548 name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new()
550 ProcMacro::Bang { name, client } => (
551 name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new()
555 SyntaxExtension::new(
558 self.get_span(id, sess),
561 Symbol::intern(name),
562 &self.get_item_attrs(id, sess),
566 crate fn get_trait_def(&self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
567 match self.kind(item_id) {
568 EntryKind::Trait(data) => {
569 let data = data.decode((self, sess));
570 ty::TraitDef::new(self.local_def_id(item_id),
575 self.def_path_table.def_path_hash(item_id))
577 EntryKind::TraitAlias => {
578 ty::TraitDef::new(self.local_def_id(item_id),
579 hir::Unsafety::Normal,
583 self.def_path_table.def_path_hash(item_id))
585 _ => bug!("def-index does not refer to trait or trait alias"),
592 kind: &EntryKind<'_>,
595 ) -> ty::VariantDef {
596 let data = match kind {
597 EntryKind::Variant(data) |
598 EntryKind::Struct(data, _) |
599 EntryKind::Union(data, _) => data.decode(self),
603 let adt_kind = match kind {
604 EntryKind::Variant(_) => ty::AdtKind::Enum,
605 EntryKind::Struct(..) => ty::AdtKind::Struct,
606 EntryKind::Union(..) => ty::AdtKind::Union,
610 let variant_did = if adt_kind == ty::AdtKind::Enum {
611 Some(self.local_def_id(index))
615 let ctor_did = data.ctor.map(|index| self.local_def_id(index));
619 Ident::with_dummy_span(self.item_name(index)),
623 self.root.per_def.children.get(self, index).unwrap_or(Lazy::empty())
624 .decode(self).map(|index| ty::FieldDef {
625 did: self.local_def_id(index),
626 ident: Ident::with_dummy_span(self.item_name(index)),
627 vis: self.get_visibility(index),
636 crate fn get_adt_def(&self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> &'tcx ty::AdtDef {
637 let kind = self.kind(item_id);
638 let did = self.local_def_id(item_id);
640 let (adt_kind, repr) = match kind {
641 EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
642 EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
643 EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
644 _ => bug!("get_adt_def called on a non-ADT {:?}", did),
647 let variants = if let ty::AdtKind::Enum = adt_kind {
648 self.root.per_def.children.get(self, item_id).unwrap_or(Lazy::empty())
651 self.get_variant(tcx, &self.kind(index), index, did)
655 std::iter::once(self.get_variant(tcx, &kind, item_id, did)).collect()
658 tcx.alloc_adt_def(did, adt_kind, variants, repr)
661 crate fn get_predicates(
665 ) -> ty::GenericPredicates<'tcx> {
666 self.root.per_def.predicates.get(self, item_id).unwrap().decode((self, tcx))
669 crate fn get_predicates_defined_on(
673 ) -> ty::GenericPredicates<'tcx> {
674 self.root.per_def.predicates_defined_on.get(self, item_id).unwrap().decode((self, tcx))
677 crate fn get_super_predicates(
681 ) -> ty::GenericPredicates<'tcx> {
682 self.root.per_def.super_predicates.get(self, item_id).unwrap().decode((self, tcx))
685 crate fn get_generics(&self, item_id: DefIndex, sess: &Session) -> ty::Generics {
686 self.root.per_def.generics.get(self, item_id).unwrap().decode((self, sess))
689 crate fn get_type(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
690 self.root.per_def.ty.get(self, id).unwrap().decode((self, tcx))
693 crate fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
694 match self.is_proc_macro(id) {
695 true => self.root.proc_macro_stability.clone(),
696 false => self.root.per_def.stability.get(self, id).map(|stab| stab.decode(self)),
700 crate fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
701 self.root.per_def.deprecation.get(self, id)
702 .filter(|_| !self.is_proc_macro(id))
703 .map(|depr| depr.decode(self))
706 crate fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
707 match self.is_proc_macro(id) {
708 true => ty::Visibility::Public,
709 false => self.root.per_def.visibility.get(self, id).unwrap().decode(self),
713 fn get_impl_data(&self, id: DefIndex) -> ImplData {
714 match self.kind(id) {
715 EntryKind::Impl(data) => data.decode(self),
720 crate fn get_parent_impl(&self, id: DefIndex) -> Option<DefId> {
721 self.get_impl_data(id).parent_impl
724 crate fn get_impl_polarity(&self, id: DefIndex) -> ty::ImplPolarity {
725 self.get_impl_data(id).polarity
728 crate fn get_impl_defaultness(&self, id: DefIndex) -> hir::Defaultness {
729 self.get_impl_data(id).defaultness
732 crate fn get_coerce_unsized_info(
735 ) -> Option<ty::adjustment::CoerceUnsizedInfo> {
736 self.get_impl_data(id).coerce_unsized_info
739 crate fn get_impl_trait(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
740 self.root.per_def.impl_trait_ref.get(self, id).map(|tr| tr.decode((self, tcx)))
743 /// Iterates over all the stability attributes in the given crate.
744 crate fn get_lib_features(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(ast::Name, Option<ast::Name>)] {
745 // FIXME: For a proc macro crate, not sure whether we should return the "host"
746 // features or an empty Vec. Both don't cause ICEs.
747 tcx.arena.alloc_from_iter(self.root
752 /// Iterates over the language items in the given crate.
753 crate fn get_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
754 if self.is_proc_macro_crate() {
755 // Proc macro crates do not export any lang-items to the target.
758 tcx.arena.alloc_from_iter(self.root
761 .map(|(def_index, index)| (self.local_def_id(def_index), index)))
765 /// Iterates over the diagnostic items in the given crate.
766 crate fn get_diagnostic_items(
769 ) -> &'tcx FxHashMap<Symbol, DefId> {
770 tcx.arena.alloc(if self.is_proc_macro_crate() {
771 // Proc macro crates do not export any diagnostic-items to the target.
777 .map(|(name, def_index)| (name, self.local_def_id(def_index)))
782 /// Iterates over each child of the given item.
783 crate fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
784 where F: FnMut(def::Export<hir::HirId>)
786 if let Some(proc_macros_ids) = self.root.proc_macro_data.map(|d| d.decode(self)) {
787 /* If we are loading as a proc macro, we want to return the view of this crate
788 * as a proc macro crate.
790 if id == CRATE_DEF_INDEX {
791 for def_index in proc_macros_ids {
792 let raw_macro = self.raw_proc_macro(def_index);
794 DefKind::Macro(macro_kind(raw_macro)),
795 self.local_def_id(def_index),
797 let ident = Ident::from_str(raw_macro.name());
798 callback(def::Export {
801 vis: ty::Visibility::Public,
810 let kind = match self.maybe_kind(id) {
815 // Iterate over all children.
816 let macros_only = self.dep_kind.lock().macros_only();
817 let children = self.root.per_def.children.get(self, id).unwrap_or(Lazy::empty());
818 for child_index in children.decode((self, sess)) {
824 if let Some(child_kind) = self.maybe_kind(child_index) {
826 EntryKind::MacroDef(..) => {}
827 _ if macros_only => continue,
831 // Hand off the item to the callback.
833 // FIXME(eddyb) Don't encode these in children.
834 EntryKind::ForeignMod => {
836 self.root.per_def.children.get(self, child_index)
837 .unwrap_or(Lazy::empty());
838 for child_index in child_children.decode((self, sess)) {
839 if let Some(kind) = self.def_kind(child_index) {
840 callback(def::Export {
841 res: Res::Def(kind, self.local_def_id(child_index)),
842 ident: Ident::with_dummy_span(self.item_name(child_index)),
843 vis: self.get_visibility(child_index),
844 span: self.root.per_def.span.get(self, child_index).unwrap()
845 .decode((self, sess)),
851 EntryKind::Impl(_) => continue,
856 let def_key = self.def_key(child_index);
857 let span = self.get_span(child_index, sess);
858 if let (Some(kind), Some(name)) =
859 (self.def_kind(child_index), def_key.disambiguated_data.data.get_opt_name()) {
860 let ident = Ident::with_dummy_span(name);
861 let vis = self.get_visibility(child_index);
862 let def_id = self.local_def_id(child_index);
863 let res = Res::Def(kind, def_id);
864 callback(def::Export { res, ident, vis, span });
865 // For non-re-export structs and variants add their constructors to children.
866 // Re-export lists automatically contain constructors when necessary.
869 if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
870 let ctor_kind = self.get_ctor_kind(child_index);
871 let ctor_res = Res::Def(
872 DefKind::Ctor(CtorOf::Struct, ctor_kind),
875 let vis = self.get_visibility(ctor_def_id.index);
876 callback(def::Export { res: ctor_res, vis, ident, span });
879 DefKind::Variant => {
880 // Braced variants, unlike structs, generate unusable names in
881 // value namespace, they are reserved for possible future use.
882 // It's ok to use the variant's id as a ctor id since an
883 // error will be reported on any use of such resolution anyway.
884 let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
885 let ctor_kind = self.get_ctor_kind(child_index);
886 let ctor_res = Res::Def(
887 DefKind::Ctor(CtorOf::Variant, ctor_kind),
890 let mut vis = self.get_visibility(ctor_def_id.index);
891 if ctor_def_id == def_id && vis == ty::Visibility::Public {
892 // For non-exhaustive variants lower the constructor visibility to
893 // within the crate. We only need this for fictive constructors,
894 // for other constructors correct visibilities
895 // were already encoded in metadata.
896 let attrs = self.get_item_attrs(def_id.index, sess);
897 if attr::contains_name(&attrs, sym::non_exhaustive) {
898 let crate_def_id = self.local_def_id(CRATE_DEF_INDEX);
899 vis = ty::Visibility::Restricted(crate_def_id);
902 callback(def::Export { res: ctor_res, ident, vis, span });
910 if let EntryKind::Mod(data) = kind {
911 for exp in data.decode((self, sess)).reexports.decode((self, sess)) {
913 Res::Def(DefKind::Macro(..), _) => {}
914 _ if macros_only => continue,
922 crate fn is_item_mir_available(&self, id: DefIndex) -> bool {
923 !self.is_proc_macro(id) &&
924 self.root.per_def.mir.get(self, id).is_some()
927 crate fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
928 self.root.per_def.mir.get(self, id)
929 .filter(|_| !self.is_proc_macro(id))
931 bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
936 crate fn get_promoted_mir(
940 ) -> IndexVec<Promoted, Body<'tcx>> {
941 self.root.per_def.promoted_mir.get(self, id)
942 .filter(|_| !self.is_proc_macro(id))
944 bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
949 crate fn mir_const_qualif(&self, id: DefIndex) -> u8 {
950 match self.kind(id) {
951 EntryKind::Const(qualif, _) |
952 EntryKind::AssocConst(AssocContainer::ImplDefault, qualif, _) |
953 EntryKind::AssocConst(AssocContainer::ImplFinal, qualif, _) => {
960 crate fn get_associated_item(&self, id: DefIndex) -> ty::AssocItem {
961 let def_key = self.def_key(id);
962 let parent = self.local_def_id(def_key.parent.unwrap());
963 let name = def_key.disambiguated_data.data.get_opt_name().unwrap();
965 let (kind, container, has_self) = match self.kind(id) {
966 EntryKind::AssocConst(container, _, _) => {
967 (ty::AssocKind::Const, container, false)
969 EntryKind::Method(data) => {
970 let data = data.decode(self);
971 (ty::AssocKind::Method, data.container, data.has_self)
973 EntryKind::AssocType(container) => {
974 (ty::AssocKind::Type, container, false)
976 EntryKind::AssocOpaqueTy(container) => {
977 (ty::AssocKind::OpaqueTy, container, false)
979 _ => bug!("cannot get associated-item of `{:?}`", def_key)
983 ident: Ident::with_dummy_span(name),
985 vis: self.get_visibility(id),
986 defaultness: container.defaultness(),
987 def_id: self.local_def_id(id),
988 container: container.with_def_id(parent),
989 method_has_self_argument: has_self
993 crate fn get_item_variances(&self, id: DefIndex) -> Vec<ty::Variance> {
994 self.root.per_def.variances.get(self, id).unwrap_or(Lazy::empty())
995 .decode(self).collect()
998 crate fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
999 match self.kind(node_id) {
1000 EntryKind::Struct(data, _) |
1001 EntryKind::Union(data, _) |
1002 EntryKind::Variant(data) => data.decode(self).ctor_kind,
1003 _ => CtorKind::Fictive,
1007 crate fn get_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
1008 match self.kind(node_id) {
1009 EntryKind::Struct(data, _) => {
1010 data.decode(self).ctor.map(|index| self.local_def_id(index))
1012 EntryKind::Variant(data) => {
1013 data.decode(self).ctor.map(|index| self.local_def_id(index))
1019 crate fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Lrc<[ast::Attribute]> {
1020 // The attributes for a tuple struct/variant are attached to the definition, not the ctor;
1021 // we assume that someone passing in a tuple struct ctor is actually wanting to
1022 // look at the definition
1023 let def_key = self.def_key(node_id);
1024 let item_id = if def_key.disambiguated_data.data == DefPathData::Ctor {
1025 def_key.parent.unwrap()
1030 Lrc::from(self.root.per_def.attributes.get(self, item_id).unwrap_or(Lazy::empty())
1031 .decode((self, sess))
1032 .collect::<Vec<_>>())
1035 crate fn get_struct_field_names(
1039 ) -> Vec<Spanned<ast::Name>> {
1040 self.root.per_def.children.get(self, id).unwrap_or(Lazy::empty())
1042 .map(|index| respan(self.get_span(index, sess), self.item_name(index)))
1046 // Translate a DefId from the current compilation environment to a DefId
1047 // for an external crate.
1048 fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
1049 for (local, &global) in self.cnum_map.iter_enumerated() {
1050 if global == did.krate {
1061 crate fn get_inherent_implementations_for_type(
1065 ) -> &'tcx [DefId] {
1066 tcx.arena.alloc_from_iter(
1067 self.root.per_def.inherent_impls.get(self, id).unwrap_or(Lazy::empty())
1069 .map(|index| self.local_def_id(index))
1073 crate fn get_implementations_for_trait(
1076 filter: Option<DefId>,
1077 ) -> &'tcx [DefId] {
1078 if self.is_proc_macro_crate() {
1079 // proc-macro crates export no trait impls.
1083 // Do a reverse lookup beforehand to avoid touching the crate_num
1084 // hash map in the loop below.
1085 let filter = match filter.map(|def_id| self.reverse_translate_def_id(def_id)) {
1086 Some(Some(def_id)) => Some((def_id.krate.as_u32(), def_id.index)),
1087 Some(None) => return &[],
1091 if let Some(filter) = filter {
1092 if let Some(impls) = self.trait_impls.get(&filter) {
1093 tcx.arena.alloc_from_iter(impls.decode(self).map(|idx| self.local_def_id(idx)))
1098 tcx.arena.alloc_from_iter(self.trait_impls.values().flat_map(|impls| {
1099 impls.decode(self).map(|idx| self.local_def_id(idx))
1104 crate fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
1105 let def_key = self.def_key(id);
1106 match def_key.disambiguated_data.data {
1107 DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
1108 // Not an associated item
1111 def_key.parent.and_then(|parent_index| {
1112 match self.kind(parent_index) {
1113 EntryKind::Trait(_) |
1114 EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
1121 crate fn get_native_libraries(&self, sess: &Session) -> Vec<NativeLibrary> {
1122 if self.is_proc_macro_crate() {
1123 // Proc macro crates do not have any *target* native libraries.
1126 self.root.native_libraries.decode((self, sess)).collect()
1130 crate fn get_foreign_modules(&self, tcx: TyCtxt<'tcx>) -> &'tcx [ForeignModule] {
1131 if self.is_proc_macro_crate() {
1132 // Proc macro crates do not have any *target* foreign modules.
1135 tcx.arena.alloc_from_iter(self.root.foreign_modules.decode((self, tcx.sess)))
1139 crate fn get_dylib_dependency_formats(
1142 ) -> &'tcx [(CrateNum, LinkagePreference)] {
1143 tcx.arena.alloc_from_iter(self.root
1144 .dylib_dependency_formats
1147 .flat_map(|(i, link)| {
1148 let cnum = CrateNum::new(i + 1);
1149 link.map(|link| (self.cnum_map[cnum], link))
1153 crate fn get_missing_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] {
1154 if self.is_proc_macro_crate() {
1155 // Proc macro crates do not depend on any target weak lang-items.
1158 tcx.arena.alloc_from_iter(self.root
1164 crate fn get_fn_param_names(&self, id: DefIndex) -> Vec<ast::Name> {
1165 let param_names = match self.kind(id) {
1166 EntryKind::Fn(data) |
1167 EntryKind::ForeignFn(data) => data.decode(self).param_names,
1168 EntryKind::Method(data) => data.decode(self).fn_data.param_names,
1171 param_names.decode(self).collect()
1174 crate fn exported_symbols(
1177 ) -> Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)> {
1178 if self.is_proc_macro_crate() {
1179 // If this crate is a custom derive crate, then we're not even going to
1180 // link those in so we skip those crates.
1183 self.root.exported_symbols.decode((self, tcx)).collect()
1187 crate fn get_rendered_const(&self, id: DefIndex) -> String {
1188 match self.kind(id) {
1189 EntryKind::Const(_, data) |
1190 EntryKind::AssocConst(_, _, data) => data.decode(self).0,
1195 crate fn get_macro(&self, id: DefIndex) -> MacroDef {
1196 match self.kind(id) {
1197 EntryKind::MacroDef(macro_def) => macro_def.decode(self),
1202 crate fn is_const_fn_raw(&self, id: DefIndex) -> bool {
1203 let constness = match self.kind(id) {
1204 EntryKind::Method(data) => data.decode(self).fn_data.constness,
1205 EntryKind::Fn(data) => data.decode(self).constness,
1206 EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1207 _ => hir::Constness::NotConst,
1209 constness == hir::Constness::Const
1212 crate fn asyncness(&self, id: DefIndex) -> hir::IsAsync {
1213 match self.kind(id) {
1214 EntryKind::Fn(data) => data.decode(self).asyncness,
1215 EntryKind::Method(data) => data.decode(self).fn_data.asyncness,
1216 EntryKind::ForeignFn(data) => data.decode(self).asyncness,
1217 _ => bug!("asyncness: expected function kind"),
1221 crate fn is_foreign_item(&self, id: DefIndex) -> bool {
1222 match self.kind(id) {
1223 EntryKind::ForeignImmStatic |
1224 EntryKind::ForeignMutStatic |
1225 EntryKind::ForeignFn(_) => true,
1230 crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
1231 match self.kind(id) {
1232 EntryKind::ImmStatic |
1233 EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
1234 EntryKind::MutStatic |
1235 EntryKind::ForeignMutStatic => Some(hir::MutMutable),
1240 crate fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1241 self.root.per_def.fn_sig.get(self, id).unwrap().decode((self, tcx))
1245 crate fn def_key(&self, index: DefIndex) -> DefKey {
1246 let mut key = self.def_path_table.def_key(index);
1247 if self.is_proc_macro(index) {
1248 let name = self.raw_proc_macro(index).name();
1249 key.disambiguated_data.data = DefPathData::MacroNs(Symbol::intern(name));
1254 // Returns the path leading to the thing with this `id`.
1255 crate fn def_path(&self, id: DefIndex) -> DefPath {
1256 debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1257 DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1261 crate fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
1262 self.def_path_table.def_path_hash(index)
1265 /// Imports the source_map from an external crate into the source_map of the crate
1266 /// currently being compiled (the "local crate").
1268 /// The import algorithm works analogous to how AST items are inlined from an
1269 /// external crate's metadata:
1270 /// For every SourceFile in the external source_map an 'inline' copy is created in the
1271 /// local source_map. The correspondence relation between external and local
1272 /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1273 /// function. When an item from an external crate is later inlined into this
1274 /// crate, this correspondence information is used to translate the span
1275 /// information of the inlined item so that it refers the correct positions in
1276 /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1278 /// The import algorithm in the function below will reuse SourceFiles already
1279 /// existing in the local source_map. For example, even if the SourceFile of some
1280 /// source file of libstd gets imported many times, there will only ever be
1281 /// one SourceFile object for the corresponding file in the local source_map.
1283 /// Note that imported SourceFiles do not actually contain the source code of the
1284 /// file they represent, just information about length, line breaks, and
1285 /// multibyte characters. This information is enough to generate valid debuginfo
1286 /// for items inlined from other crates.
1288 /// Proc macro crates don't currently export spans, so this function does not have
1289 /// to work for them.
1290 fn imported_source_files(
1292 local_source_map: &source_map::SourceMap,
1293 ) -> ReadGuard<'a, Vec<cstore::ImportedSourceFile>> {
1295 let source_files = self.source_map_import_info.borrow();
1296 if !source_files.is_empty() {
1297 return source_files;
1301 // Lock the source_map_import_info to ensure this only happens once
1302 let mut source_map_import_info = self.source_map_import_info.borrow_mut();
1304 if !source_map_import_info.is_empty() {
1305 drop(source_map_import_info);
1306 return self.source_map_import_info.borrow();
1309 let external_source_map = self.root.source_map.decode(self);
1311 let imported_source_files = external_source_map.map(|source_file_to_import| {
1312 // We can't reuse an existing SourceFile, so allocate a new one
1313 // containing the information we need.
1314 let syntax_pos::SourceFile { name,
1320 mut multibyte_chars,
1321 mut non_narrow_chars,
1323 .. } = source_file_to_import;
1325 let source_length = (end_pos - start_pos).to_usize();
1327 // Translate line-start positions and multibyte character
1328 // position into frame of reference local to file.
1329 // `SourceMap::new_imported_source_file()` will then translate those
1330 // coordinates to their new global frame of reference when the
1331 // offset of the SourceFile is known.
1332 for pos in &mut lines {
1333 *pos = *pos - start_pos;
1335 for mbc in &mut multibyte_chars {
1336 mbc.pos = mbc.pos - start_pos;
1338 for swc in &mut non_narrow_chars {
1339 *swc = *swc - start_pos;
1342 let local_version = local_source_map.new_imported_source_file(name,
1351 debug!("CrateMetaData::imported_source_files alloc \
1352 source_file {:?} original (start_pos {:?} end_pos {:?}) \
1353 translated (start_pos {:?} end_pos {:?})",
1354 local_version.name, start_pos, end_pos,
1355 local_version.start_pos, local_version.end_pos);
1357 cstore::ImportedSourceFile {
1358 original_start_pos: start_pos,
1359 original_end_pos: end_pos,
1360 translated_source_file: local_version,
1364 *source_map_import_info = imported_source_files;
1365 drop(source_map_import_info);
1367 // This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
1368 self.source_map_import_info.borrow()
1371 /// Get the `DepNodeIndex` corresponding this crate. The result of this
1372 /// method is cached in the `dep_node_index` field.
1373 pub(super) fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1374 let mut dep_node_index = self.dep_node_index.load();
1376 if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1377 // We have not cached the DepNodeIndex for this upstream crate yet,
1378 // so use the dep-graph to find it out and cache it.
1379 // Note that multiple threads can enter this block concurrently.
1380 // That is fine because the DepNodeIndex remains constant
1381 // throughout the whole compilation session, and multiple stores
1382 // would always write the same value.
1384 let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1385 let dep_node = def_path_hash.to_dep_node(DepKind::CrateMetadata);
1387 dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1388 assert!(dep_node_index != DepNodeIndex::INVALID);
1389 self.dep_node_index.store(dep_node_index);
1396 // Cannot be implemented on 'ProcMacro', as libproc_macro
1397 // does not depend on libsyntax
1398 fn macro_kind(raw: &ProcMacro) -> MacroKind {
1400 ProcMacro::CustomDerive { .. } => MacroKind::Derive,
1401 ProcMacro::Attr { .. } => MacroKind::Attr,
1402 ProcMacro::Bang { .. } => MacroKind::Bang