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, symbol::{InternedString}};
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")
519 Symbol::intern(self.raw_proc_macro(item_index).name())
523 crate fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
524 if !self.is_proc_macro(index) {
525 self.kind(index).def_kind()
528 macro_kind(self.raw_proc_macro(index))
533 crate fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
534 self.root.per_def.span.get(self, index).unwrap().decode((self, sess))
537 crate fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
538 let (name, kind, helper_attrs) = match *self.raw_proc_macro(id) {
539 ProcMacro::CustomDerive { trait_name, attributes, client } => {
541 attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
544 SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive { client })),
548 ProcMacro::Attr { name, client } => (
549 name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new()
551 ProcMacro::Bang { name, client } => (
552 name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new()
556 SyntaxExtension::new(
559 self.get_span(id, sess),
562 Symbol::intern(name),
563 &self.get_item_attrs(id, sess),
567 crate fn get_trait_def(&self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
568 match self.kind(item_id) {
569 EntryKind::Trait(data) => {
570 let data = data.decode((self, sess));
571 ty::TraitDef::new(self.local_def_id(item_id),
576 self.def_path_table.def_path_hash(item_id))
578 EntryKind::TraitAlias(_) => {
579 ty::TraitDef::new(self.local_def_id(item_id),
580 hir::Unsafety::Normal,
584 self.def_path_table.def_path_hash(item_id))
586 _ => bug!("def-index does not refer to trait or trait alias"),
593 kind: &EntryKind<'_>,
596 ) -> ty::VariantDef {
597 let data = match kind {
598 EntryKind::Variant(data) |
599 EntryKind::Struct(data, _) |
600 EntryKind::Union(data, _) => data.decode(self),
604 let adt_kind = match kind {
605 EntryKind::Variant(_) => ty::AdtKind::Enum,
606 EntryKind::Struct(..) => ty::AdtKind::Struct,
607 EntryKind::Union(..) => ty::AdtKind::Union,
611 let variant_did = if adt_kind == ty::AdtKind::Enum {
612 Some(self.local_def_id(index))
616 let ctor_did = data.ctor.map(|index| self.local_def_id(index));
620 Ident::with_dummy_span(self.item_name(index)),
624 self.root.per_def.children.get(self, index).unwrap_or(Lazy::empty())
625 .decode(self).map(|index| ty::FieldDef {
626 did: self.local_def_id(index),
627 ident: Ident::with_dummy_span(self.item_name(index)),
628 vis: self.get_visibility(index),
637 crate fn get_adt_def(&self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> &'tcx ty::AdtDef {
638 let kind = self.kind(item_id);
639 let did = self.local_def_id(item_id);
641 let (adt_kind, repr) = match kind {
642 EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
643 EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
644 EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
645 _ => bug!("get_adt_def called on a non-ADT {:?}", did),
648 let variants = if let ty::AdtKind::Enum = adt_kind {
649 self.root.per_def.children.get(self, item_id).unwrap_or(Lazy::empty())
652 self.get_variant(tcx, &self.kind(index), index, did)
656 std::iter::once(self.get_variant(tcx, &kind, item_id, did)).collect()
659 tcx.alloc_adt_def(did, adt_kind, variants, repr)
662 crate fn get_predicates(
666 ) -> ty::GenericPredicates<'tcx> {
667 self.root.per_def.predicates.get(self, item_id).unwrap().decode((self, tcx))
670 crate fn get_predicates_defined_on(
674 ) -> ty::GenericPredicates<'tcx> {
675 self.root.per_def.predicates_defined_on.get(self, item_id).unwrap().decode((self, tcx))
678 crate fn get_super_predicates(
682 ) -> ty::GenericPredicates<'tcx> {
683 let super_predicates = match self.kind(item_id) {
684 EntryKind::Trait(data) => data.decode(self).super_predicates,
685 EntryKind::TraitAlias(data) => data.decode(self).super_predicates,
686 _ => bug!("def-index does not refer to trait or trait alias"),
689 super_predicates.decode((self, tcx))
692 crate fn get_generics(&self, item_id: DefIndex, sess: &Session) -> ty::Generics {
693 self.root.per_def.generics.get(self, item_id).unwrap().decode((self, sess))
696 crate fn get_type(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
697 self.root.per_def.ty.get(self, id).unwrap().decode((self, tcx))
700 crate fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
701 match self.is_proc_macro(id) {
702 true => self.root.proc_macro_stability.clone(),
703 false => self.root.per_def.stability.get(self, id).map(|stab| stab.decode(self)),
707 crate fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
708 self.root.per_def.deprecation.get(self, id)
709 .filter(|_| !self.is_proc_macro(id))
710 .map(|depr| depr.decode(self))
713 crate fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
714 match self.is_proc_macro(id) {
715 true => ty::Visibility::Public,
716 false => self.root.per_def.visibility.get(self, id).unwrap().decode(self),
720 fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
721 match self.kind(id) {
722 EntryKind::Impl(data) => data.decode(self),
727 crate fn get_parent_impl(&self, id: DefIndex) -> Option<DefId> {
728 self.get_impl_data(id).parent_impl
731 crate fn get_impl_polarity(&self, id: DefIndex) -> ty::ImplPolarity {
732 self.get_impl_data(id).polarity
735 crate fn get_impl_defaultness(&self, id: DefIndex) -> hir::Defaultness {
736 self.get_impl_data(id).defaultness
739 crate fn get_coerce_unsized_info(
742 ) -> Option<ty::adjustment::CoerceUnsizedInfo> {
743 self.get_impl_data(id).coerce_unsized_info
746 crate fn get_impl_trait(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
747 self.get_impl_data(id).trait_ref.map(|tr| tr.decode((self, tcx)))
750 /// Iterates over all the stability attributes in the given crate.
751 crate fn get_lib_features(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(ast::Name, Option<ast::Name>)] {
752 // FIXME: For a proc macro crate, not sure whether we should return the "host"
753 // features or an empty Vec. Both don't cause ICEs.
754 tcx.arena.alloc_from_iter(self.root
759 /// Iterates over the language items in the given crate.
760 crate fn get_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
761 if self.is_proc_macro_crate() {
762 // Proc macro crates do not export any lang-items to the target.
765 tcx.arena.alloc_from_iter(self.root
768 .map(|(def_index, index)| (self.local_def_id(def_index), index)))
772 /// Iterates over the diagnostic items in the given crate.
773 crate fn get_diagnostic_items(
776 ) -> &'tcx FxHashMap<Symbol, DefId> {
777 tcx.arena.alloc(if self.is_proc_macro_crate() {
778 // Proc macro crates do not export any diagnostic-items to the target.
784 .map(|(name, def_index)| (name, self.local_def_id(def_index)))
789 /// Iterates over each child of the given item.
790 crate fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
791 where F: FnMut(def::Export<hir::HirId>)
793 if let Some(proc_macros_ids) = self.root.proc_macro_data.map(|d| d.decode(self)) {
794 /* If we are loading as a proc macro, we want to return the view of this crate
795 * as a proc macro crate.
797 if id == CRATE_DEF_INDEX {
798 for def_index in proc_macros_ids {
799 let raw_macro = self.raw_proc_macro(def_index);
801 DefKind::Macro(macro_kind(raw_macro)),
802 self.local_def_id(def_index),
804 let ident = Ident::from_str(raw_macro.name());
805 callback(def::Export {
808 vis: ty::Visibility::Public,
817 let kind = match self.maybe_kind(id) {
822 // Iterate over all children.
823 let macros_only = self.dep_kind.lock().macros_only();
824 let children = self.root.per_def.children.get(self, id).unwrap_or(Lazy::empty());
825 for child_index in children.decode((self, sess)) {
831 if let Some(child_kind) = self.maybe_kind(child_index) {
833 EntryKind::MacroDef(..) => {}
834 _ if macros_only => continue,
838 // Hand off the item to the callback.
840 // FIXME(eddyb) Don't encode these in children.
841 EntryKind::ForeignMod => {
843 self.root.per_def.children.get(self, child_index)
844 .unwrap_or(Lazy::empty());
845 for child_index in child_children.decode((self, sess)) {
846 if let Some(kind) = self.def_kind(child_index) {
847 callback(def::Export {
848 res: Res::Def(kind, self.local_def_id(child_index)),
849 ident: Ident::with_dummy_span(self.item_name(child_index)),
850 vis: self.get_visibility(child_index),
851 span: self.root.per_def.span.get(self, child_index).unwrap()
852 .decode((self, sess)),
858 EntryKind::Impl(_) => continue,
863 let def_key = self.def_key(child_index);
864 let span = self.get_span(child_index, sess);
865 if let (Some(kind), Some(name)) =
866 (self.def_kind(child_index), def_key.disambiguated_data.data.get_opt_name()) {
867 let ident = Ident::from_interned_str(name);
868 let vis = self.get_visibility(child_index);
869 let def_id = self.local_def_id(child_index);
870 let res = Res::Def(kind, def_id);
871 callback(def::Export { res, ident, vis, span });
872 // For non-re-export structs and variants add their constructors to children.
873 // Re-export lists automatically contain constructors when necessary.
876 if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
877 let ctor_kind = self.get_ctor_kind(child_index);
878 let ctor_res = Res::Def(
879 DefKind::Ctor(CtorOf::Struct, ctor_kind),
882 let vis = self.get_visibility(ctor_def_id.index);
883 callback(def::Export { res: ctor_res, vis, ident, span });
886 DefKind::Variant => {
887 // Braced variants, unlike structs, generate unusable names in
888 // value namespace, they are reserved for possible future use.
889 // It's ok to use the variant's id as a ctor id since an
890 // error will be reported on any use of such resolution anyway.
891 let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
892 let ctor_kind = self.get_ctor_kind(child_index);
893 let ctor_res = Res::Def(
894 DefKind::Ctor(CtorOf::Variant, ctor_kind),
897 let mut vis = self.get_visibility(ctor_def_id.index);
898 if ctor_def_id == def_id && vis == ty::Visibility::Public {
899 // For non-exhaustive variants lower the constructor visibility to
900 // within the crate. We only need this for fictive constructors,
901 // for other constructors correct visibilities
902 // were already encoded in metadata.
903 let attrs = self.get_item_attrs(def_id.index, sess);
904 if attr::contains_name(&attrs, sym::non_exhaustive) {
905 let crate_def_id = self.local_def_id(CRATE_DEF_INDEX);
906 vis = ty::Visibility::Restricted(crate_def_id);
909 callback(def::Export { res: ctor_res, ident, vis, span });
917 if let EntryKind::Mod(data) = kind {
918 for exp in data.decode((self, sess)).reexports.decode((self, sess)) {
920 Res::Def(DefKind::Macro(..), _) => {}
921 _ if macros_only => continue,
929 crate fn is_item_mir_available(&self, id: DefIndex) -> bool {
930 !self.is_proc_macro(id) &&
931 self.root.per_def.mir.get(self, id).is_some()
934 crate fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
935 self.root.per_def.mir.get(self, id)
936 .filter(|_| !self.is_proc_macro(id))
938 bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
943 crate fn get_promoted_mir(
947 ) -> IndexVec<Promoted, Body<'tcx>> {
948 self.root.per_def.promoted_mir.get(self, id)
949 .filter(|_| !self.is_proc_macro(id))
951 bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
956 crate fn mir_const_qualif(&self, id: DefIndex) -> u8 {
957 match self.kind(id) {
958 EntryKind::Const(qualif, _) |
959 EntryKind::AssocConst(AssocContainer::ImplDefault, qualif, _) |
960 EntryKind::AssocConst(AssocContainer::ImplFinal, qualif, _) => {
967 crate fn get_associated_item(&self, id: DefIndex) -> ty::AssocItem {
968 let def_key = self.def_key(id);
969 let parent = self.local_def_id(def_key.parent.unwrap());
970 let name = def_key.disambiguated_data.data.get_opt_name().unwrap();
972 let (kind, container, has_self) = match self.kind(id) {
973 EntryKind::AssocConst(container, _, _) => {
974 (ty::AssocKind::Const, container, false)
976 EntryKind::Method(data) => {
977 let data = data.decode(self);
978 (ty::AssocKind::Method, data.container, data.has_self)
980 EntryKind::AssocType(container) => {
981 (ty::AssocKind::Type, container, false)
983 EntryKind::AssocOpaqueTy(container) => {
984 (ty::AssocKind::OpaqueTy, container, false)
986 _ => bug!("cannot get associated-item of `{:?}`", def_key)
990 ident: Ident::from_interned_str(name),
992 vis: self.get_visibility(id),
993 defaultness: container.defaultness(),
994 def_id: self.local_def_id(id),
995 container: container.with_def_id(parent),
996 method_has_self_argument: has_self
1000 crate fn get_item_variances(&self, id: DefIndex) -> Vec<ty::Variance> {
1001 self.root.per_def.variances.get(self, id).unwrap_or(Lazy::empty())
1002 .decode(self).collect()
1005 crate fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
1006 match self.kind(node_id) {
1007 EntryKind::Struct(data, _) |
1008 EntryKind::Union(data, _) |
1009 EntryKind::Variant(data) => data.decode(self).ctor_kind,
1010 _ => CtorKind::Fictive,
1014 crate fn get_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
1015 match self.kind(node_id) {
1016 EntryKind::Struct(data, _) => {
1017 data.decode(self).ctor.map(|index| self.local_def_id(index))
1019 EntryKind::Variant(data) => {
1020 data.decode(self).ctor.map(|index| self.local_def_id(index))
1026 crate fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Lrc<[ast::Attribute]> {
1027 // The attributes for a tuple struct/variant are attached to the definition, not the ctor;
1028 // we assume that someone passing in a tuple struct ctor is actually wanting to
1029 // look at the definition
1030 let def_key = self.def_key(node_id);
1031 let item_id = if def_key.disambiguated_data.data == DefPathData::Ctor {
1032 def_key.parent.unwrap()
1037 Lrc::from(self.root.per_def.attributes.get(self, item_id).unwrap_or(Lazy::empty())
1038 .decode((self, sess))
1039 .collect::<Vec<_>>())
1042 crate fn get_struct_field_names(
1046 ) -> Vec<Spanned<ast::Name>> {
1047 self.root.per_def.children.get(self, id).unwrap_or(Lazy::empty())
1049 .map(|index| respan(self.get_span(index, sess), self.item_name(index)))
1053 // Translate a DefId from the current compilation environment to a DefId
1054 // for an external crate.
1055 fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
1056 for (local, &global) in self.cnum_map.iter_enumerated() {
1057 if global == did.krate {
1068 crate fn get_inherent_implementations_for_type(
1072 ) -> &'tcx [DefId] {
1073 tcx.arena.alloc_from_iter(
1074 self.root.per_def.inherent_impls.get(self, id).unwrap_or(Lazy::empty())
1076 .map(|index| self.local_def_id(index))
1080 crate fn get_implementations_for_trait(
1083 filter: Option<DefId>,
1084 ) -> &'tcx [DefId] {
1085 if self.is_proc_macro_crate() {
1086 // proc-macro crates export no trait impls.
1090 // Do a reverse lookup beforehand to avoid touching the crate_num
1091 // hash map in the loop below.
1092 let filter = match filter.map(|def_id| self.reverse_translate_def_id(def_id)) {
1093 Some(Some(def_id)) => Some((def_id.krate.as_u32(), def_id.index)),
1094 Some(None) => return &[],
1098 if let Some(filter) = filter {
1099 if let Some(impls) = self.trait_impls.get(&filter) {
1100 tcx.arena.alloc_from_iter(impls.decode(self).map(|idx| self.local_def_id(idx)))
1105 tcx.arena.alloc_from_iter(self.trait_impls.values().flat_map(|impls| {
1106 impls.decode(self).map(|idx| self.local_def_id(idx))
1111 crate fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
1112 let def_key = self.def_key(id);
1113 match def_key.disambiguated_data.data {
1114 DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
1115 // Not an associated item
1118 def_key.parent.and_then(|parent_index| {
1119 match self.kind(parent_index) {
1120 EntryKind::Trait(_) |
1121 EntryKind::TraitAlias(_) => Some(self.local_def_id(parent_index)),
1128 crate fn get_native_libraries(&self, sess: &Session) -> Vec<NativeLibrary> {
1129 if self.is_proc_macro_crate() {
1130 // Proc macro crates do not have any *target* native libraries.
1133 self.root.native_libraries.decode((self, sess)).collect()
1137 crate fn get_foreign_modules(&self, tcx: TyCtxt<'tcx>) -> &'tcx [ForeignModule] {
1138 if self.is_proc_macro_crate() {
1139 // Proc macro crates do not have any *target* foreign modules.
1142 tcx.arena.alloc_from_iter(self.root.foreign_modules.decode((self, tcx.sess)))
1146 crate fn get_dylib_dependency_formats(
1149 ) -> &'tcx [(CrateNum, LinkagePreference)] {
1150 tcx.arena.alloc_from_iter(self.root
1151 .dylib_dependency_formats
1154 .flat_map(|(i, link)| {
1155 let cnum = CrateNum::new(i + 1);
1156 link.map(|link| (self.cnum_map[cnum], link))
1160 crate fn get_missing_lang_items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] {
1161 if self.is_proc_macro_crate() {
1162 // Proc macro crates do not depend on any target weak lang-items.
1165 tcx.arena.alloc_from_iter(self.root
1171 crate fn get_fn_param_names(&self, id: DefIndex) -> Vec<ast::Name> {
1172 let param_names = match self.kind(id) {
1173 EntryKind::Fn(data) |
1174 EntryKind::ForeignFn(data) => data.decode(self).param_names,
1175 EntryKind::Method(data) => data.decode(self).fn_data.param_names,
1178 param_names.decode(self).collect()
1181 crate fn exported_symbols(
1184 ) -> Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)> {
1185 if self.is_proc_macro_crate() {
1186 // If this crate is a custom derive crate, then we're not even going to
1187 // link those in so we skip those crates.
1190 self.root.exported_symbols.decode((self, tcx)).collect()
1194 crate fn get_rendered_const(&self, id: DefIndex) -> String {
1195 match self.kind(id) {
1196 EntryKind::Const(_, data) |
1197 EntryKind::AssocConst(_, _, data) => data.decode(self).0,
1202 crate fn get_macro(&self, id: DefIndex) -> MacroDef {
1203 match self.kind(id) {
1204 EntryKind::MacroDef(macro_def) => macro_def.decode(self),
1209 crate fn is_const_fn_raw(&self, id: DefIndex) -> bool {
1210 let constness = match self.kind(id) {
1211 EntryKind::Method(data) => data.decode(self).fn_data.constness,
1212 EntryKind::Fn(data) => data.decode(self).constness,
1213 EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1214 _ => hir::Constness::NotConst,
1216 constness == hir::Constness::Const
1219 crate fn asyncness(&self, id: DefIndex) -> hir::IsAsync {
1220 match self.kind(id) {
1221 EntryKind::Fn(data) => data.decode(self).asyncness,
1222 EntryKind::Method(data) => data.decode(self).fn_data.asyncness,
1223 EntryKind::ForeignFn(data) => data.decode(self).asyncness,
1224 _ => bug!("asyncness: expected function kind"),
1228 crate fn is_foreign_item(&self, id: DefIndex) -> bool {
1229 match self.kind(id) {
1230 EntryKind::ForeignImmStatic |
1231 EntryKind::ForeignMutStatic |
1232 EntryKind::ForeignFn(_) => true,
1237 crate fn static_mutability(&self, id: DefIndex) -> Option<hir::Mutability> {
1238 match self.kind(id) {
1239 EntryKind::ImmStatic |
1240 EntryKind::ForeignImmStatic => Some(hir::MutImmutable),
1241 EntryKind::MutStatic |
1242 EntryKind::ForeignMutStatic => Some(hir::MutMutable),
1247 crate fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1248 let sig = match self.kind(id) {
1249 EntryKind::Fn(data) |
1250 EntryKind::ForeignFn(data) => data.decode(self).sig,
1251 EntryKind::Method(data) => data.decode(self).fn_data.sig,
1252 EntryKind::Variant(data) |
1253 EntryKind::Struct(data, _) => data.decode(self).ctor_sig.unwrap(),
1254 EntryKind::Closure(data) => data.decode(self).sig,
1257 sig.decode((self, tcx))
1261 crate fn def_key(&self, index: DefIndex) -> DefKey {
1262 let mut key = self.def_path_table.def_key(index);
1263 if self.is_proc_macro(index) {
1264 let name = self.raw_proc_macro(index).name();
1265 key.disambiguated_data.data = DefPathData::MacroNs(InternedString::intern(name));
1270 // Returns the path leading to the thing with this `id`.
1271 crate fn def_path(&self, id: DefIndex) -> DefPath {
1272 debug!("def_path(cnum={:?}, id={:?})", self.cnum, id);
1273 DefPath::make(self.cnum, id, |parent| self.def_key(parent))
1277 crate fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
1278 self.def_path_table.def_path_hash(index)
1281 /// Imports the source_map from an external crate into the source_map of the crate
1282 /// currently being compiled (the "local crate").
1284 /// The import algorithm works analogous to how AST items are inlined from an
1285 /// external crate's metadata:
1286 /// For every SourceFile in the external source_map an 'inline' copy is created in the
1287 /// local source_map. The correspondence relation between external and local
1288 /// SourceFiles is recorded in the `ImportedSourceFile` objects returned from this
1289 /// function. When an item from an external crate is later inlined into this
1290 /// crate, this correspondence information is used to translate the span
1291 /// information of the inlined item so that it refers the correct positions in
1292 /// the local source_map (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
1294 /// The import algorithm in the function below will reuse SourceFiles already
1295 /// existing in the local source_map. For example, even if the SourceFile of some
1296 /// source file of libstd gets imported many times, there will only ever be
1297 /// one SourceFile object for the corresponding file in the local source_map.
1299 /// Note that imported SourceFiles do not actually contain the source code of the
1300 /// file they represent, just information about length, line breaks, and
1301 /// multibyte characters. This information is enough to generate valid debuginfo
1302 /// for items inlined from other crates.
1304 /// Proc macro crates don't currently export spans, so this function does not have
1305 /// to work for them.
1306 fn imported_source_files(
1308 local_source_map: &source_map::SourceMap,
1309 ) -> ReadGuard<'a, Vec<cstore::ImportedSourceFile>> {
1311 let source_files = self.source_map_import_info.borrow();
1312 if !source_files.is_empty() {
1313 return source_files;
1317 // Lock the source_map_import_info to ensure this only happens once
1318 let mut source_map_import_info = self.source_map_import_info.borrow_mut();
1320 if !source_map_import_info.is_empty() {
1321 drop(source_map_import_info);
1322 return self.source_map_import_info.borrow();
1325 let external_source_map = self.root.source_map.decode(self);
1327 let imported_source_files = external_source_map.map(|source_file_to_import| {
1328 // We can't reuse an existing SourceFile, so allocate a new one
1329 // containing the information we need.
1330 let syntax_pos::SourceFile { name,
1336 mut multibyte_chars,
1337 mut non_narrow_chars,
1339 .. } = source_file_to_import;
1341 let source_length = (end_pos - start_pos).to_usize();
1343 // Translate line-start positions and multibyte character
1344 // position into frame of reference local to file.
1345 // `SourceMap::new_imported_source_file()` will then translate those
1346 // coordinates to their new global frame of reference when the
1347 // offset of the SourceFile is known.
1348 for pos in &mut lines {
1349 *pos = *pos - start_pos;
1351 for mbc in &mut multibyte_chars {
1352 mbc.pos = mbc.pos - start_pos;
1354 for swc in &mut non_narrow_chars {
1355 *swc = *swc - start_pos;
1358 let local_version = local_source_map.new_imported_source_file(name,
1367 debug!("CrateMetaData::imported_source_files alloc \
1368 source_file {:?} original (start_pos {:?} end_pos {:?}) \
1369 translated (start_pos {:?} end_pos {:?})",
1370 local_version.name, start_pos, end_pos,
1371 local_version.start_pos, local_version.end_pos);
1373 cstore::ImportedSourceFile {
1374 original_start_pos: start_pos,
1375 original_end_pos: end_pos,
1376 translated_source_file: local_version,
1380 *source_map_import_info = imported_source_files;
1381 drop(source_map_import_info);
1383 // This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
1384 self.source_map_import_info.borrow()
1387 /// Get the `DepNodeIndex` corresponding this crate. The result of this
1388 /// method is cached in the `dep_node_index` field.
1389 pub(super) fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1390 let mut dep_node_index = self.dep_node_index.load();
1392 if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1393 // We have not cached the DepNodeIndex for this upstream crate yet,
1394 // so use the dep-graph to find it out and cache it.
1395 // Note that multiple threads can enter this block concurrently.
1396 // That is fine because the DepNodeIndex remains constant
1397 // throughout the whole compilation session, and multiple stores
1398 // would always write the same value.
1400 let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1401 let dep_node = def_path_hash.to_dep_node(DepKind::CrateMetadata);
1403 dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1404 assert!(dep_node_index != DepNodeIndex::INVALID);
1405 self.dep_node_index.store(dep_node_index);
1412 // Cannot be implemented on 'ProcMacro', as libproc_macro
1413 // does not depend on libsyntax
1414 fn macro_kind(raw: &ProcMacro) -> MacroKind {
1416 ProcMacro::CustomDerive { .. } => MacroKind::Derive,
1417 ProcMacro::Attr { .. } => MacroKind::Attr,
1418 ProcMacro::Bang { .. } => MacroKind::Bang