1 use crate::index::Index;
2 use crate::index_builder::{FromId, IndexBuilder, Untracked};
3 use crate::isolated_encoder::IsolatedEncoder;
6 use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
7 EncodedMetadata, ForeignModule};
8 use rustc::hir::def::CtorKind;
9 use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
10 use rustc::hir::map::definitions::DefPathTable;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc::middle::dependency_format::Linkage;
13 use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
14 metadata_symbol_name};
15 use rustc::middle::lang_items;
16 use rustc::mir::{self, interpret};
17 use rustc::traits::specialization_graph;
18 use rustc::ty::{self, Ty, TyCtxt, ReprOptions, SymbolName};
19 use rustc::ty::codec::{self as ty_codec, TyEncoder};
20 use rustc::ty::layout::VariantIdx;
22 use rustc::session::config::{self, CrateType};
23 use rustc::util::nodemap::FxHashMap;
25 use rustc_data_structures::stable_hasher::StableHasher;
26 use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
30 use rustc_data_structures::sync::Lrc;
32 use syntax::ast::{self, CRATE_NODE_ID};
34 use syntax::source_map::Spanned;
35 use syntax::symbol::keywords;
36 use syntax_pos::{self, hygiene, FileName, SourceFile, Span};
37 use log::{debug, trace};
39 use rustc::hir::{self, PatKind};
40 use rustc::hir::itemlikevisit::ItemLikeVisitor;
41 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
42 use rustc::hir::intravisit;
44 pub struct EncodeContext<'a, 'tcx: 'a> {
45 opaque: opaque::Encoder,
46 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
48 lazy_state: LazyState,
49 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
50 predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
52 interpret_allocs: FxHashMap<interpret::AllocId, usize>,
53 interpret_allocs_inverse: Vec<interpret::AllocId>,
55 // This is used to speed up Span encoding.
56 source_file_cache: Lrc<SourceFile>,
59 macro_rules! encoder_methods {
60 ($($name:ident($ty:ty);)*) => {
61 $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
62 self.opaque.$name(value)
67 impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
68 type Error = <opaque::Encoder as Encoder>::Error;
70 fn emit_unit(&mut self) -> Result<(), Self::Error> {
97 impl<'a, 'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'a, 'tcx> {
98 fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
99 self.emit_lazy_distance(lazy.position, Lazy::<T>::min_size())
103 impl<'a, 'tcx, T> SpecializedEncoder<LazySeq<T>> for EncodeContext<'a, 'tcx> {
104 fn specialized_encode(&mut self, seq: &LazySeq<T>) -> Result<(), Self::Error> {
105 self.emit_usize(seq.len)?;
109 self.emit_lazy_distance(seq.position, LazySeq::<T>::min_size(seq.len))
113 impl<'a, 'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'a, 'tcx> {
115 fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
116 self.emit_u32(cnum.as_u32())
120 impl<'a, 'tcx> SpecializedEncoder<DefId> for EncodeContext<'a, 'tcx> {
122 fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> {
133 impl<'a, 'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'a, 'tcx> {
135 fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> {
136 self.emit_u32(def_index.as_raw_u32())
140 impl<'a, 'tcx> SpecializedEncoder<Span> for EncodeContext<'a, 'tcx> {
141 fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
143 return TAG_INVALID_SPAN.encode(self)
146 let span = span.data();
148 // The Span infrastructure should make sure that this invariant holds:
149 debug_assert!(span.lo <= span.hi);
151 if !self.source_file_cache.contains(span.lo) {
152 let source_map = self.tcx.sess.source_map();
153 let source_file_index = source_map.lookup_source_file_idx(span.lo);
154 self.source_file_cache = source_map.files()[source_file_index].clone();
157 if !self.source_file_cache.contains(span.hi) {
158 // Unfortunately, macro expansion still sometimes generates Spans
159 // that malformed in this way.
160 return TAG_INVALID_SPAN.encode(self)
163 TAG_VALID_SPAN.encode(self)?;
164 span.lo.encode(self)?;
166 // Encode length which is usually less than span.hi and profits more
167 // from the variable-length integer encoding that we use.
168 let len = span.hi - span.lo;
171 // Don't encode the expansion context.
175 impl<'a, 'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'a, 'tcx> {
177 fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {
178 self.specialized_encode(&def_id.to_def_id())
182 impl<'a, 'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'a, 'tcx> {
183 fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> {
184 ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands)
188 impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx> {
189 fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
190 use std::collections::hash_map::Entry;
191 let index = match self.interpret_allocs.entry(*alloc_id) {
192 Entry::Occupied(e) => *e.get(),
193 Entry::Vacant(e) => {
194 let idx = self.interpret_allocs_inverse.len();
195 self.interpret_allocs_inverse.push(*alloc_id);
205 impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'a, 'tcx> {
206 fn specialized_encode(&mut self,
207 predicates: &ty::GenericPredicates<'tcx>)
208 -> Result<(), Self::Error> {
209 ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
213 impl<'a, 'tcx> SpecializedEncoder<Fingerprint> for EncodeContext<'a, 'tcx> {
214 fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
215 f.encode_opaque(&mut self.opaque)
219 impl<'a, 'tcx, T: Encodable> SpecializedEncoder<mir::ClearCrossCrate<T>>
220 for EncodeContext<'a, 'tcx> {
221 fn specialized_encode(&mut self,
222 _: &mir::ClearCrossCrate<T>)
223 -> Result<(), Self::Error> {
228 impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> {
229 fn position(&self) -> usize {
230 self.opaque.position()
234 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
236 fn emit_node<F: FnOnce(&mut Self, usize) -> R, R>(&mut self, f: F) -> R {
237 assert_eq!(self.lazy_state, LazyState::NoNode);
238 let pos = self.position();
239 self.lazy_state = LazyState::NodeStart(pos);
240 let r = f(self, pos);
241 self.lazy_state = LazyState::NoNode;
245 fn emit_lazy_distance(&mut self,
248 -> Result<(), <Self as Encoder>::Error> {
249 let min_end = position + min_size;
250 let distance = match self.lazy_state {
251 LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
252 LazyState::NodeStart(start) => {
253 assert!(min_end <= start);
256 LazyState::Previous(last_min_end) => {
258 last_min_end <= position,
259 "make sure that the calls to `lazy*` \
260 are in the same order as the metadata fields",
262 position - last_min_end
265 self.lazy_state = LazyState::Previous(min_end);
266 self.emit_usize(distance)
269 pub fn lazy<T: Encodable>(&mut self, value: &T) -> Lazy<T> {
270 self.emit_node(|ecx, pos| {
271 value.encode(ecx).unwrap();
273 assert!(pos + Lazy::<T>::min_size() <= ecx.position());
274 Lazy::with_position(pos)
278 pub fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T>
279 where I: IntoIterator<Item = T>,
282 self.emit_node(|ecx, pos| {
283 let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
285 assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
286 LazySeq::with_position_and_length(pos, len)
290 pub fn lazy_seq_ref<'b, I, T>(&mut self, iter: I) -> LazySeq<T>
291 where I: IntoIterator<Item = &'b T>,
294 self.emit_node(|ecx, pos| {
295 let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
297 assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
298 LazySeq::with_position_and_length(pos, len)
302 // Encodes something that corresponds to a single DepNode::GlobalMetaData
303 // and registers the Fingerprint in the `metadata_hashes` map.
304 pub fn tracked<'x, DATA, R>(&'x mut self,
305 op: fn(&mut IsolatedEncoder<'x, 'a, 'tcx>, DATA) -> R,
308 op(&mut IsolatedEncoder::new(self), data)
311 fn encode_info_for_items(&mut self) -> Index {
312 let krate = self.tcx.hir().krate();
313 let mut index = IndexBuilder::new(self);
314 let vis = Spanned { span: syntax_pos::DUMMY_SP, node: hir::VisibilityKind::Public };
315 index.record(DefId::local(CRATE_DEF_INDEX),
316 IsolatedEncoder::encode_info_for_mod,
317 FromId(CRATE_NODE_ID, (&krate.module, &krate.attrs, &vis)));
318 let mut visitor = EncodeVisitor { index };
319 krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
320 for macro_def in &krate.exported_macros {
321 visitor.visit_macro_def(macro_def);
323 visitor.index.into_items()
326 fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
327 let definitions = self.tcx.hir().definitions();
328 self.lazy(definitions.def_path_table())
331 fn encode_source_map(&mut self) -> LazySeq<syntax_pos::SourceFile> {
332 let source_map = self.tcx.sess.source_map();
333 let all_source_files = source_map.files();
335 let (working_dir, _cwd_remapped) = self.tcx.sess.working_dir.clone();
337 let adapted = all_source_files.iter()
338 .filter(|source_file| {
339 // No need to re-export imported source_files, as any downstream
340 // crate will import them from their original source.
341 !source_file.is_imported()
344 match source_file.name {
345 // This path of this SourceFile has been modified by
346 // path-remapping, so we use it verbatim (and avoid
347 // cloning the whole map in the process).
348 _ if source_file.name_was_remapped => source_file.clone(),
350 // Otherwise expand all paths to absolute paths because
351 // any relative paths are potentially relative to a
353 FileName::Real(ref name) => {
354 let mut adapted = (**source_file).clone();
355 adapted.name = Path::new(&working_dir).join(name).into();
356 adapted.name_hash = {
357 let mut hasher: StableHasher<u128> = StableHasher::new();
358 adapted.name.hash(&mut hasher);
364 // expanded code, not from a file
365 _ => source_file.clone(),
368 .collect::<Vec<_>>();
370 self.lazy_seq_ref(adapted.iter().map(|rc| &**rc))
373 fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
374 let mut i = self.position();
376 let crate_deps = self.tracked(IsolatedEncoder::encode_crate_deps, ());
377 let dylib_dependency_formats = self.tracked(
378 IsolatedEncoder::encode_dylib_dependency_formats,
380 let dep_bytes = self.position() - i;
382 // Encode the lib features.
384 let lib_features = self.tracked(IsolatedEncoder::encode_lib_features, ());
385 let lib_feature_bytes = self.position() - i;
387 // Encode the language items.
389 let lang_items = self.tracked(IsolatedEncoder::encode_lang_items, ());
390 let lang_items_missing = self.tracked(
391 IsolatedEncoder::encode_lang_items_missing,
393 let lang_item_bytes = self.position() - i;
395 // Encode the native libraries used
397 let native_libraries = self.tracked(
398 IsolatedEncoder::encode_native_libraries,
400 let native_lib_bytes = self.position() - i;
402 let foreign_modules = self.tracked(
403 IsolatedEncoder::encode_foreign_modules,
408 let source_map = self.encode_source_map();
409 let source_map_bytes = self.position() - i;
411 // Encode DefPathTable
413 let def_path_table = self.encode_def_path_table();
414 let def_path_table_bytes = self.position() - i;
416 // Encode the def IDs of impls, for coherence checking.
418 let impls = self.tracked(IsolatedEncoder::encode_impls, ());
419 let impl_bytes = self.position() - i;
421 // Encode exported symbols info.
423 let exported_symbols = self.tcx.exported_symbols(LOCAL_CRATE);
424 let exported_symbols = self.tracked(
425 IsolatedEncoder::encode_exported_symbols,
427 let exported_symbols_bytes = self.position() - i;
433 let items = self.encode_info_for_items();
434 let item_bytes = self.position() - i;
436 // Encode the allocation index
437 let interpret_alloc_index = {
438 let mut interpret_alloc_index = Vec::new();
440 trace!("beginning to encode alloc ids");
442 let new_n = self.interpret_allocs_inverse.len();
443 // if we have found new ids, serialize those, too
448 trace!("encoding {} further alloc ids", new_n - n);
449 for idx in n..new_n {
450 let id = self.interpret_allocs_inverse[idx];
451 let pos = self.position() as u32;
452 interpret_alloc_index.push(pos);
453 interpret::specialized_encode_alloc_id(
461 self.lazy_seq(interpret_alloc_index)
466 let index = items.write_index(&mut self.opaque);
467 let index_bytes = self.position() - i;
469 let attrs = tcx.hir().krate_attrs();
470 let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
471 let has_default_lib_allocator = attr::contains_name(&attrs, "default_lib_allocator");
472 let has_global_allocator = *tcx.sess.has_global_allocator.get();
473 let has_panic_handler = *tcx.sess.has_panic_handler.try_get().unwrap_or(&false);
475 let root = self.lazy(&CrateRoot {
476 name: tcx.crate_name(LOCAL_CRATE),
477 extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
478 triple: tcx.sess.opts.target_triple.clone(),
479 hash: tcx.crate_hash(LOCAL_CRATE),
480 disambiguator: tcx.sess.local_crate_disambiguator(),
481 panic_strategy: tcx.sess.panic_strategy(),
482 edition: hygiene::default_edition(),
483 has_global_allocator: has_global_allocator,
484 has_panic_handler: has_panic_handler,
485 has_default_lib_allocator: has_default_lib_allocator,
486 plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index),
487 proc_macro_decls_static: if is_proc_macro {
488 let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap();
494 compiler_builtins: attr::contains_name(&attrs, "compiler_builtins"),
495 needs_allocator: attr::contains_name(&attrs, "needs_allocator"),
496 needs_panic_runtime: attr::contains_name(&attrs, "needs_panic_runtime"),
497 no_builtins: attr::contains_name(&attrs, "no_builtins"),
498 panic_runtime: attr::contains_name(&attrs, "panic_runtime"),
499 profiler_runtime: attr::contains_name(&attrs, "profiler_runtime"),
500 sanitizer_runtime: attr::contains_name(&attrs, "sanitizer_runtime"),
503 dylib_dependency_formats,
513 interpret_alloc_index,
517 let total_bytes = self.position();
519 if self.tcx.sess.meta_stats() {
520 let mut zero_bytes = 0;
521 for e in self.opaque.data.iter() {
527 println!("metadata stats:");
528 println!(" dep bytes: {}", dep_bytes);
529 println!(" lib feature bytes: {}", lib_feature_bytes);
530 println!(" lang item bytes: {}", lang_item_bytes);
531 println!(" native bytes: {}", native_lib_bytes);
532 println!(" source_map bytes: {}", source_map_bytes);
533 println!(" impl bytes: {}", impl_bytes);
534 println!(" exp. symbols bytes: {}", exported_symbols_bytes);
535 println!(" def-path table bytes: {}", def_path_table_bytes);
536 println!(" item bytes: {}", item_bytes);
537 println!(" index bytes: {}", index_bytes);
538 println!(" zero bytes: {}", zero_bytes);
539 println!(" total bytes: {}", total_bytes);
546 // These are methods for encoding various things. They are meant to be used with
547 // IndexBuilder::record() and EncodeContext::tracked(). They actually
548 // would not have to be methods of IsolatedEncoder (free standing functions
549 // taking IsolatedEncoder as first argument would be just fine) but by making
550 // them methods we don't have to repeat the lengthy `<'a, 'b: 'a, 'tcx: 'b>`
551 // clause again and again.
552 impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
553 fn encode_variances_of(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
554 debug!("IsolatedEncoder::encode_variances_of({:?})", def_id);
556 self.lazy_seq_from_slice(&tcx.variances_of(def_id))
559 fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
561 let ty = tcx.type_of(def_id);
562 debug!("IsolatedEncoder::encode_item_type({:?}) => {:?}", def_id, ty);
566 /// Encode data for the given variant of the given ADT. The
567 /// index of the variant is untracked: this is ok because we
568 /// will have to lookup the adt-def by its id, and that gives us
569 /// the right to access any information in the adt-def (including,
570 /// e.g., the length of the various vectors).
571 fn encode_enum_variant_info(&mut self,
572 (enum_did, Untracked(index)): (DefId, Untracked<VariantIdx>))
575 let def = tcx.adt_def(enum_did);
576 let variant = &def.variants[index];
577 let def_id = variant.did;
578 debug!("IsolatedEncoder::encode_enum_variant_info({:?})", def_id);
580 let data = VariantData {
581 ctor_kind: variant.ctor_kind,
582 discr: variant.discr,
584 ctor_sig: if variant.ctor_kind == CtorKind::Fn {
585 Some(self.lazy(&tcx.fn_sig(def_id)))
591 let enum_id = tcx.hir().as_local_node_id(enum_did).unwrap();
592 let enum_vis = &tcx.hir().expect_item(enum_id).vis;
595 kind: EntryKind::Variant(self.lazy(&data)),
596 visibility: self.lazy(&ty::Visibility::from_hir(enum_vis, enum_id, tcx)),
597 span: self.lazy(&tcx.def_span(def_id)),
598 attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
599 children: self.lazy_seq(variant.fields.iter().map(|f| {
600 assert!(f.did.is_local());
603 stability: self.encode_stability(def_id),
604 deprecation: self.encode_deprecation(def_id),
606 ty: Some(self.encode_item_type(def_id)),
607 inherent_impls: LazySeq::empty(),
608 variances: if variant.ctor_kind == CtorKind::Fn {
609 self.encode_variances_of(def_id)
613 generics: Some(self.encode_generics(def_id)),
614 predicates: Some(self.encode_predicates(def_id)),
615 predicates_defined_on: None,
617 mir: self.encode_optimized_mir(def_id),
621 fn encode_info_for_mod(&mut self,
622 FromId(id, (md, attrs, vis)): FromId<(&hir::Mod,
627 let def_id = tcx.hir().local_def_id(id);
628 debug!("IsolatedEncoder::encode_info_for_mod({:?})", def_id);
631 reexports: match tcx.module_exports(def_id) {
632 Some(ref exports) => self.lazy_seq_from_slice(exports.as_slice()),
633 _ => LazySeq::empty(),
638 kind: EntryKind::Mod(self.lazy(&data)),
639 visibility: self.lazy(&ty::Visibility::from_hir(vis, id, tcx)),
640 span: self.lazy(&tcx.def_span(def_id)),
641 attributes: self.encode_attributes(attrs),
642 children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
643 tcx.hir().local_def_id(item_id.id).index
645 stability: self.encode_stability(def_id),
646 deprecation: self.encode_deprecation(def_id),
649 inherent_impls: LazySeq::empty(),
650 variances: LazySeq::empty(),
653 predicates_defined_on: None,
659 /// Encode data for the given field of the given variant of the
660 /// given ADT. The indices of the variant/field are untracked:
661 /// this is ok because we will have to lookup the adt-def by its
662 /// id, and that gives us the right to access any information in
663 /// the adt-def (including, e.g., the length of the various
665 fn encode_field(&mut self,
666 (adt_def_id, Untracked((variant_index, field_index))): (DefId,
667 Untracked<(VariantIdx,
671 let variant = &tcx.adt_def(adt_def_id).variants[variant_index];
672 let field = &variant.fields[field_index];
674 let def_id = field.did;
675 debug!("IsolatedEncoder::encode_field({:?})", def_id);
677 let variant_id = tcx.hir().as_local_hir_id(variant.did).unwrap();
678 let variant_data = tcx.hir().expect_variant_data(variant_id);
681 kind: EntryKind::Field,
682 visibility: self.lazy(&field.vis),
683 span: self.lazy(&tcx.def_span(def_id)),
684 attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
685 children: LazySeq::empty(),
686 stability: self.encode_stability(def_id),
687 deprecation: self.encode_deprecation(def_id),
689 ty: Some(self.encode_item_type(def_id)),
690 inherent_impls: LazySeq::empty(),
691 variances: LazySeq::empty(),
692 generics: Some(self.encode_generics(def_id)),
693 predicates: Some(self.encode_predicates(def_id)),
694 predicates_defined_on: None,
700 fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<'tcx> {
701 debug!("IsolatedEncoder::encode_struct_ctor({:?})", def_id);
703 let adt_def = tcx.adt_def(adt_def_id);
704 let variant = adt_def.non_enum_variant();
706 let data = VariantData {
707 ctor_kind: variant.ctor_kind,
708 discr: variant.discr,
709 struct_ctor: Some(def_id.index),
710 ctor_sig: if variant.ctor_kind == CtorKind::Fn {
711 Some(self.lazy(&tcx.fn_sig(def_id)))
717 let struct_id = tcx.hir().as_local_node_id(adt_def_id).unwrap();
718 let struct_vis = &tcx.hir().expect_item(struct_id).vis;
719 let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
720 for field in &variant.fields {
721 if ctor_vis.is_at_least(field.vis, tcx) {
722 ctor_vis = field.vis;
726 // If the structure is marked as non_exhaustive then lower the visibility
727 // to within the crate.
728 if adt_def.non_enum_variant().is_field_list_non_exhaustive() &&
729 ctor_vis == ty::Visibility::Public
731 ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
734 let repr_options = get_repr_options(&tcx, adt_def_id);
737 kind: EntryKind::Struct(self.lazy(&data), repr_options),
738 visibility: self.lazy(&ctor_vis),
739 span: self.lazy(&tcx.def_span(def_id)),
740 attributes: LazySeq::empty(),
741 children: LazySeq::empty(),
742 stability: self.encode_stability(def_id),
743 deprecation: self.encode_deprecation(def_id),
745 ty: Some(self.encode_item_type(def_id)),
746 inherent_impls: LazySeq::empty(),
747 variances: if variant.ctor_kind == CtorKind::Fn {
748 self.encode_variances_of(def_id)
752 generics: Some(self.encode_generics(def_id)),
753 predicates: Some(self.encode_predicates(def_id)),
754 predicates_defined_on: None,
756 mir: self.encode_optimized_mir(def_id),
760 fn encode_generics(&mut self, def_id: DefId) -> Lazy<ty::Generics> {
761 debug!("IsolatedEncoder::encode_generics({:?})", def_id);
763 self.lazy(tcx.generics_of(def_id))
766 fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
767 debug!("IsolatedEncoder::encode_predicates({:?})", def_id);
769 self.lazy(&tcx.predicates_of(def_id))
772 fn encode_predicates_defined_on(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
773 debug!("IsolatedEncoder::encode_predicates_defined_on({:?})", def_id);
775 self.lazy(&tcx.predicates_defined_on(def_id))
778 fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
779 debug!("IsolatedEncoder::encode_info_for_trait_item({:?})", def_id);
782 let node_id = tcx.hir().as_local_node_id(def_id).unwrap();
783 let ast_item = tcx.hir().expect_trait_item(node_id);
784 let trait_item = tcx.associated_item(def_id);
786 let container = match trait_item.defaultness {
787 hir::Defaultness::Default { has_value: true } =>
788 AssociatedContainer::TraitWithDefault,
789 hir::Defaultness::Default { has_value: false } =>
790 AssociatedContainer::TraitRequired,
791 hir::Defaultness::Final =>
792 span_bug!(ast_item.span, "traits cannot have final items"),
795 let kind = match trait_item.kind {
796 ty::AssociatedKind::Const => {
798 if let hir::TraitItemKind::Const(_, Some(body)) = ast_item.node {
799 self.const_qualif(0, body)
801 ConstQualif { mir: 0, ast_promotable: false }
805 hir::print::to_string(self.tcx.hir(), |s| s.print_trait_item(ast_item));
806 let rendered_const = self.lazy(&RenderedConst(rendered));
808 EntryKind::AssociatedConst(container, const_qualif, rendered_const)
810 ty::AssociatedKind::Method => {
811 let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node {
812 let arg_names = match *m {
813 hir::TraitMethod::Required(ref names) => {
814 self.encode_fn_arg_names(names)
816 hir::TraitMethod::Provided(body) => {
817 self.encode_fn_arg_names_for_body(body)
821 constness: hir::Constness::NotConst,
823 sig: self.lazy(&tcx.fn_sig(def_id)),
828 EntryKind::Method(self.lazy(&MethodData {
831 has_self: trait_item.method_has_self_argument,
834 ty::AssociatedKind::Type => EntryKind::AssociatedType(container),
835 ty::AssociatedKind::Existential =>
836 span_bug!(ast_item.span, "existential type in trait"),
841 visibility: self.lazy(&trait_item.vis),
842 span: self.lazy(&ast_item.span),
843 attributes: self.encode_attributes(&ast_item.attrs),
844 children: LazySeq::empty(),
845 stability: self.encode_stability(def_id),
846 deprecation: self.encode_deprecation(def_id),
848 ty: match trait_item.kind {
849 ty::AssociatedKind::Const |
850 ty::AssociatedKind::Method => {
851 Some(self.encode_item_type(def_id))
853 ty::AssociatedKind::Type => {
854 if trait_item.defaultness.has_value() {
855 Some(self.encode_item_type(def_id))
860 ty::AssociatedKind::Existential => unreachable!(),
862 inherent_impls: LazySeq::empty(),
863 variances: if trait_item.kind == ty::AssociatedKind::Method {
864 self.encode_variances_of(def_id)
868 generics: Some(self.encode_generics(def_id)),
869 predicates: Some(self.encode_predicates(def_id)),
870 predicates_defined_on: None,
872 mir: self.encode_optimized_mir(def_id),
876 fn metadata_output_only(&self) -> bool {
877 // MIR optimisation can be skipped when we're just interested in the metadata.
878 !self.tcx.sess.opts.output_types.should_codegen()
881 fn const_qualif(&self, mir: u8, body_id: hir::BodyId) -> ConstQualif {
882 let body_owner_def_id = self.tcx.hir().body_owner_def_id(body_id);
883 let ast_promotable = self.tcx.const_is_rvalue_promotable_to_static(body_owner_def_id);
885 ConstQualif { mir, ast_promotable }
888 fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'tcx> {
889 debug!("IsolatedEncoder::encode_info_for_impl_item({:?})", def_id);
892 let node_id = self.tcx.hir().as_local_node_id(def_id).unwrap();
893 let ast_item = self.tcx.hir().expect_impl_item(node_id);
894 let impl_item = self.tcx.associated_item(def_id);
896 let container = match impl_item.defaultness {
897 hir::Defaultness::Default { has_value: true } => AssociatedContainer::ImplDefault,
898 hir::Defaultness::Final => AssociatedContainer::ImplFinal,
899 hir::Defaultness::Default { has_value: false } =>
900 span_bug!(ast_item.span, "impl items always have values (currently)"),
903 let kind = match impl_item.kind {
904 ty::AssociatedKind::Const => {
905 if let hir::ImplItemKind::Const(_, body_id) = ast_item.node {
906 let mir = self.tcx.at(ast_item.span).mir_const_qualif(def_id).0;
908 EntryKind::AssociatedConst(container,
909 self.const_qualif(mir, body_id),
910 self.encode_rendered_const_for_body(body_id))
915 ty::AssociatedKind::Method => {
916 let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
918 constness: sig.header.constness,
919 arg_names: self.encode_fn_arg_names_for_body(body),
920 sig: self.lazy(&tcx.fn_sig(def_id)),
925 EntryKind::Method(self.lazy(&MethodData {
928 has_self: impl_item.method_has_self_argument,
931 ty::AssociatedKind::Existential => EntryKind::AssociatedExistential(container),
932 ty::AssociatedKind::Type => EntryKind::AssociatedType(container)
936 match ast_item.node {
937 hir::ImplItemKind::Const(..) => true,
938 hir::ImplItemKind::Method(ref sig, _) => {
939 let generics = self.tcx.generics_of(def_id);
940 let needs_inline = (generics.requires_monomorphization(self.tcx) ||
941 tcx.codegen_fn_attrs(def_id).requests_inline()) &&
942 !self.metadata_output_only();
943 let is_const_fn = sig.header.constness == hir::Constness::Const;
944 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
945 needs_inline || is_const_fn || always_encode_mir
947 hir::ImplItemKind::Existential(..) |
948 hir::ImplItemKind::Type(..) => false,
953 visibility: self.lazy(&impl_item.vis),
954 span: self.lazy(&ast_item.span),
955 attributes: self.encode_attributes(&ast_item.attrs),
956 children: LazySeq::empty(),
957 stability: self.encode_stability(def_id),
958 deprecation: self.encode_deprecation(def_id),
960 ty: Some(self.encode_item_type(def_id)),
961 inherent_impls: LazySeq::empty(),
962 variances: if impl_item.kind == ty::AssociatedKind::Method {
963 self.encode_variances_of(def_id)
967 generics: Some(self.encode_generics(def_id)),
968 predicates: Some(self.encode_predicates(def_id)),
969 predicates_defined_on: None,
971 mir: if mir { self.encode_optimized_mir(def_id) } else { None },
975 fn encode_fn_arg_names_for_body(&mut self, body_id: hir::BodyId)
976 -> LazySeq<ast::Name> {
977 self.tcx.dep_graph.with_ignore(|| {
978 let body = self.tcx.hir().body(body_id);
979 self.lazy_seq(body.arguments.iter().map(|arg| {
981 PatKind::Binding(_, _, _, ident, _) => ident.name,
982 _ => keywords::Invalid.name(),
988 fn encode_fn_arg_names(&mut self, param_names: &[ast::Ident]) -> LazySeq<ast::Name> {
989 self.lazy_seq(param_names.iter().map(|ident| ident.name))
992 fn encode_optimized_mir(&mut self, def_id: DefId) -> Option<Lazy<mir::Mir<'tcx>>> {
993 debug!("EntryBuilder::encode_mir({:?})", def_id);
994 if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
995 let mir = self.tcx.optimized_mir(def_id);
996 Some(self.lazy(&mir))
1002 // Encodes the inherent implementations of a structure, enumeration, or trait.
1003 fn encode_inherent_implementations(&mut self, def_id: DefId) -> LazySeq<DefIndex> {
1004 debug!("IsolatedEncoder::encode_inherent_implementations({:?})", def_id);
1005 let implementations = self.tcx.inherent_impls(def_id);
1006 if implementations.is_empty() {
1009 self.lazy_seq(implementations.iter().map(|&def_id| {
1010 assert!(def_id.is_local());
1016 fn encode_stability(&mut self, def_id: DefId) -> Option<Lazy<attr::Stability>> {
1017 debug!("IsolatedEncoder::encode_stability({:?})", def_id);
1018 self.tcx.lookup_stability(def_id).map(|stab| self.lazy(stab))
1021 fn encode_deprecation(&mut self, def_id: DefId) -> Option<Lazy<attr::Deprecation>> {
1022 debug!("IsolatedEncoder::encode_deprecation({:?})", def_id);
1023 self.tcx.lookup_deprecation(def_id).map(|depr| self.lazy(&depr))
1026 fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
1027 let body = self.tcx.hir().body(body_id);
1028 let rendered = hir::print::to_string(self.tcx.hir(), |s| s.print_expr(&body.value));
1029 let rendered_const = &RenderedConst(rendered);
1030 self.lazy(rendered_const)
1033 fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) -> Entry<'tcx> {
1036 debug!("IsolatedEncoder::encode_info_for_item({:?})", def_id);
1038 let kind = match item.node {
1039 hir::ItemKind::Static(_, hir::MutMutable, _) => EntryKind::MutStatic,
1040 hir::ItemKind::Static(_, hir::MutImmutable, _) => EntryKind::ImmStatic,
1041 hir::ItemKind::Const(_, body_id) => {
1042 let mir = tcx.at(item.span).mir_const_qualif(def_id).0;
1044 self.const_qualif(mir, body_id),
1045 self.encode_rendered_const_for_body(body_id)
1048 hir::ItemKind::Fn(_, header, .., body) => {
1050 constness: header.constness,
1051 arg_names: self.encode_fn_arg_names_for_body(body),
1052 sig: self.lazy(&tcx.fn_sig(def_id)),
1055 EntryKind::Fn(self.lazy(&data))
1057 hir::ItemKind::Mod(ref m) => {
1058 return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
1060 hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod,
1061 hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
1062 hir::ItemKind::Ty(..) => EntryKind::Type,
1063 hir::ItemKind::Existential(..) => EntryKind::Existential,
1064 hir::ItemKind::Enum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
1065 hir::ItemKind::Struct(ref struct_def, _) => {
1066 let variant = tcx.adt_def(def_id).non_enum_variant();
1068 // Encode def_ids for each field and method
1069 // for methods, write all the stuff get_trait_method
1071 let struct_ctor = if !struct_def.is_struct() {
1072 Some(tcx.hir().local_def_id(struct_def.id()).index)
1077 let repr_options = get_repr_options(&tcx, def_id);
1079 EntryKind::Struct(self.lazy(&VariantData {
1080 ctor_kind: variant.ctor_kind,
1081 discr: variant.discr,
1086 hir::ItemKind::Union(..) => {
1087 let variant = tcx.adt_def(def_id).non_enum_variant();
1088 let repr_options = get_repr_options(&tcx, def_id);
1090 EntryKind::Union(self.lazy(&VariantData {
1091 ctor_kind: variant.ctor_kind,
1092 discr: variant.discr,
1097 hir::ItemKind::Impl(_, polarity, defaultness, ..) => {
1098 let trait_ref = tcx.impl_trait_ref(def_id);
1099 let parent = if let Some(trait_ref) = trait_ref {
1100 let trait_def = tcx.trait_def(trait_ref.def_id);
1101 trait_def.ancestors(tcx, def_id).nth(1).and_then(|node| {
1103 specialization_graph::Node::Impl(parent) => Some(parent),
1111 // if this is an impl of `CoerceUnsized`, create its
1112 // "unsized info", else just store None
1113 let coerce_unsized_info =
1114 trait_ref.and_then(|t| {
1115 if Some(t.def_id) == tcx.lang_items().coerce_unsized_trait() {
1116 Some(tcx.at(item.span).coerce_unsized_info(def_id))
1122 let data = ImplData {
1125 parent_impl: parent,
1126 coerce_unsized_info,
1127 trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)),
1130 EntryKind::Impl(self.lazy(&data))
1132 hir::ItemKind::Trait(..) => {
1133 let trait_def = tcx.trait_def(def_id);
1134 let data = TraitData {
1135 unsafety: trait_def.unsafety,
1136 paren_sugar: trait_def.paren_sugar,
1137 has_auto_impl: tcx.trait_is_auto(def_id),
1138 is_marker: trait_def.is_marker,
1139 super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
1142 EntryKind::Trait(self.lazy(&data))
1144 hir::ItemKind::TraitAlias(..) => {
1145 let data = TraitAliasData {
1146 super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
1149 EntryKind::TraitAlias(self.lazy(&data))
1151 hir::ItemKind::ExternCrate(_) |
1152 hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
1157 visibility: self.lazy(&ty::Visibility::from_hir(&item.vis, item.id, tcx)),
1158 span: self.lazy(&item.span),
1159 attributes: self.encode_attributes(&item.attrs),
1160 children: match item.node {
1161 hir::ItemKind::ForeignMod(ref fm) => {
1162 self.lazy_seq(fm.items
1164 .map(|foreign_item| tcx.hir().local_def_id(foreign_item.id).index))
1166 hir::ItemKind::Enum(..) => {
1167 let def = self.tcx.adt_def(def_id);
1168 self.lazy_seq(def.variants.iter().map(|v| {
1169 assert!(v.did.is_local());
1173 hir::ItemKind::Struct(..) |
1174 hir::ItemKind::Union(..) => {
1175 let def = self.tcx.adt_def(def_id);
1176 self.lazy_seq(def.non_enum_variant().fields.iter().map(|f| {
1177 assert!(f.did.is_local());
1181 hir::ItemKind::Impl(..) |
1182 hir::ItemKind::Trait(..) => {
1183 self.lazy_seq(tcx.associated_item_def_ids(def_id).iter().map(|&def_id| {
1184 assert!(def_id.is_local());
1188 _ => LazySeq::empty(),
1190 stability: self.encode_stability(def_id),
1191 deprecation: self.encode_deprecation(def_id),
1193 ty: match item.node {
1194 hir::ItemKind::Static(..) |
1195 hir::ItemKind::Const(..) |
1196 hir::ItemKind::Fn(..) |
1197 hir::ItemKind::Ty(..) |
1198 hir::ItemKind::Existential(..) |
1199 hir::ItemKind::Enum(..) |
1200 hir::ItemKind::Struct(..) |
1201 hir::ItemKind::Union(..) |
1202 hir::ItemKind::Impl(..) => Some(self.encode_item_type(def_id)),
1205 inherent_impls: self.encode_inherent_implementations(def_id),
1206 variances: match item.node {
1207 hir::ItemKind::Enum(..) |
1208 hir::ItemKind::Struct(..) |
1209 hir::ItemKind::Union(..) |
1210 hir::ItemKind::Fn(..) => self.encode_variances_of(def_id),
1211 _ => LazySeq::empty(),
1213 generics: match item.node {
1214 hir::ItemKind::Static(..) |
1215 hir::ItemKind::Const(..) |
1216 hir::ItemKind::Fn(..) |
1217 hir::ItemKind::Ty(..) |
1218 hir::ItemKind::Enum(..) |
1219 hir::ItemKind::Struct(..) |
1220 hir::ItemKind::Union(..) |
1221 hir::ItemKind::Impl(..) |
1222 hir::ItemKind::Existential(..) |
1223 hir::ItemKind::Trait(..) => Some(self.encode_generics(def_id)),
1224 hir::ItemKind::TraitAlias(..) => Some(self.encode_generics(def_id)),
1227 predicates: match item.node {
1228 hir::ItemKind::Static(..) |
1229 hir::ItemKind::Const(..) |
1230 hir::ItemKind::Fn(..) |
1231 hir::ItemKind::Ty(..) |
1232 hir::ItemKind::Enum(..) |
1233 hir::ItemKind::Struct(..) |
1234 hir::ItemKind::Union(..) |
1235 hir::ItemKind::Impl(..) |
1236 hir::ItemKind::Existential(..) |
1237 hir::ItemKind::Trait(..) |
1238 hir::ItemKind::TraitAlias(..) => Some(self.encode_predicates(def_id)),
1242 // The only time that `predicates_defined_on` is used (on
1243 // an external item) is for traits, during chalk lowering,
1244 // so only encode it in that case as an efficiency
1245 // hack. (No reason not to expand it in the future if
1247 predicates_defined_on: match item.node {
1248 hir::ItemKind::Trait(..) |
1249 hir::ItemKind::TraitAlias(..) => Some(self.encode_predicates_defined_on(def_id)),
1250 _ => None, // not *wrong* for other kinds of items, but not needed
1253 mir: match item.node {
1254 hir::ItemKind::Static(..) => {
1255 self.encode_optimized_mir(def_id)
1257 hir::ItemKind::Const(..) => self.encode_optimized_mir(def_id),
1258 hir::ItemKind::Fn(_, header, ..) => {
1259 let generics = tcx.generics_of(def_id);
1261 (generics.requires_monomorphization(tcx) ||
1262 tcx.codegen_fn_attrs(def_id).requests_inline()) &&
1263 !self.metadata_output_only();
1264 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
1266 || header.constness == hir::Constness::Const
1267 || always_encode_mir
1269 self.encode_optimized_mir(def_id)
1279 /// Serialize the text of exported macros
1280 fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
1281 use syntax::print::pprust;
1282 let def_id = self.tcx.hir().local_def_id_from_hir_id(macro_def.hir_id);
1284 kind: EntryKind::MacroDef(self.lazy(&MacroDef {
1285 body: pprust::tts_to_string(¯o_def.body.trees().collect::<Vec<_>>()),
1286 legacy: macro_def.legacy,
1288 visibility: self.lazy(&ty::Visibility::Public),
1289 span: self.lazy(¯o_def.span),
1290 attributes: self.encode_attributes(¯o_def.attrs),
1291 stability: self.encode_stability(def_id),
1292 deprecation: self.encode_deprecation(def_id),
1294 children: LazySeq::empty(),
1296 inherent_impls: LazySeq::empty(),
1297 variances: LazySeq::empty(),
1300 predicates_defined_on: None,
1305 fn encode_info_for_ty_param(&mut self,
1306 (def_id, Untracked(has_default)): (DefId, Untracked<bool>))
1308 debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id);
1311 kind: EntryKind::Type,
1312 visibility: self.lazy(&ty::Visibility::Public),
1313 span: self.lazy(&tcx.def_span(def_id)),
1314 attributes: LazySeq::empty(),
1315 children: LazySeq::empty(),
1319 ty: if has_default {
1320 Some(self.encode_item_type(def_id))
1324 inherent_impls: LazySeq::empty(),
1325 variances: LazySeq::empty(),
1328 predicates_defined_on: None,
1334 fn encode_info_for_const_param(&mut self, def_id: DefId) -> Entry<'tcx> {
1335 debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id);
1338 kind: EntryKind::Type,
1339 visibility: self.lazy(&ty::Visibility::Public),
1340 span: self.lazy(&tcx.def_span(def_id)),
1341 attributes: LazySeq::empty(),
1342 children: LazySeq::empty(),
1346 ty: Some(self.encode_item_type(def_id)),
1347 inherent_impls: LazySeq::empty(),
1348 variances: LazySeq::empty(),
1351 predicates_defined_on: None,
1357 fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
1358 debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id);
1361 let tables = self.tcx.typeck_tables_of(def_id);
1362 let node_id = self.tcx.hir().as_local_node_id(def_id).unwrap();
1363 let hir_id = self.tcx.hir().node_to_hir_id(node_id);
1364 let kind = match tables.node_type(hir_id).sty {
1365 ty::Generator(def_id, ..) => {
1366 let layout = self.tcx.generator_layout(def_id);
1367 let data = GeneratorData {
1368 layout: layout.clone(),
1370 EntryKind::Generator(self.lazy(&data))
1373 ty::Closure(def_id, substs) => {
1374 let sig = substs.closure_sig(def_id, self.tcx);
1375 let data = ClosureData { sig: self.lazy(&sig) };
1376 EntryKind::Closure(self.lazy(&data))
1379 _ => bug!("closure that is neither generator nor closure")
1384 visibility: self.lazy(&ty::Visibility::Public),
1385 span: self.lazy(&tcx.def_span(def_id)),
1386 attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
1387 children: LazySeq::empty(),
1391 ty: Some(self.encode_item_type(def_id)),
1392 inherent_impls: LazySeq::empty(),
1393 variances: LazySeq::empty(),
1394 generics: Some(self.encode_generics(def_id)),
1396 predicates_defined_on: None,
1398 mir: self.encode_optimized_mir(def_id),
1402 fn encode_info_for_anon_const(&mut self, def_id: DefId) -> Entry<'tcx> {
1403 debug!("IsolatedEncoder::encode_info_for_anon_const({:?})", def_id);
1405 let id = tcx.hir().as_local_node_id(def_id).unwrap();
1406 let body_id = tcx.hir().body_owned_by(id);
1407 let const_data = self.encode_rendered_const_for_body(body_id);
1408 let mir = tcx.mir_const_qualif(def_id).0;
1411 kind: EntryKind::Const(self.const_qualif(mir, body_id), const_data),
1412 visibility: self.lazy(&ty::Visibility::Public),
1413 span: self.lazy(&tcx.def_span(def_id)),
1414 attributes: LazySeq::empty(),
1415 children: LazySeq::empty(),
1419 ty: Some(self.encode_item_type(def_id)),
1420 inherent_impls: LazySeq::empty(),
1421 variances: LazySeq::empty(),
1422 generics: Some(self.encode_generics(def_id)),
1423 predicates: Some(self.encode_predicates(def_id)),
1424 predicates_defined_on: None,
1426 mir: self.encode_optimized_mir(def_id),
1430 fn encode_attributes(&mut self, attrs: &[ast::Attribute]) -> LazySeq<ast::Attribute> {
1431 // NOTE: This must use lazy_seq_from_slice(), not lazy_seq() because
1432 // we rely on the HashStable specialization for [Attribute]
1433 // to properly filter things out.
1434 self.lazy_seq_from_slice(attrs)
1437 fn encode_native_libraries(&mut self, _: ()) -> LazySeq<NativeLibrary> {
1438 let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
1439 self.lazy_seq(used_libraries.iter().cloned())
1442 fn encode_foreign_modules(&mut self, _: ()) -> LazySeq<ForeignModule> {
1443 let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
1444 self.lazy_seq(foreign_modules.iter().cloned())
1447 fn encode_crate_deps(&mut self, _: ()) -> LazySeq<CrateDep> {
1448 let crates = self.tcx.crates();
1450 let mut deps = crates
1453 let dep = CrateDep {
1454 name: self.tcx.original_crate_name(cnum),
1455 hash: self.tcx.crate_hash(cnum),
1456 kind: self.tcx.dep_kind(cnum),
1457 extra_filename: self.tcx.extra_filename(cnum),
1461 .collect::<Vec<_>>();
1463 deps.sort_by_key(|&(cnum, _)| cnum);
1466 // Sanity-check the crate numbers
1467 let mut expected_cnum = 1;
1468 for &(n, _) in &deps {
1469 assert_eq!(n, CrateNum::new(expected_cnum));
1474 // We're just going to write a list of crate 'name-hash-version's, with
1475 // the assumption that they are numbered 1 to n.
1476 // FIXME (#2166): This is not nearly enough to support correct versioning
1477 // but is enough to get transitive crate dependencies working.
1478 self.lazy_seq_ref(deps.iter().map(|&(_, ref dep)| dep))
1481 fn encode_lib_features(&mut self, _: ()) -> LazySeq<(ast::Name, Option<ast::Name>)> {
1483 let lib_features = tcx.lib_features();
1484 self.lazy_seq(lib_features.to_vec())
1487 fn encode_lang_items(&mut self, _: ()) -> LazySeq<(DefIndex, usize)> {
1489 let lang_items = tcx.lang_items();
1490 let lang_items = lang_items.items().iter();
1491 self.lazy_seq(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
1492 if let Some(def_id) = opt_def_id {
1493 if def_id.is_local() {
1494 return Some((def_id.index, i));
1501 fn encode_lang_items_missing(&mut self, _: ()) -> LazySeq<lang_items::LangItem> {
1503 self.lazy_seq_ref(&tcx.lang_items().missing)
1506 /// Encodes an index, mapping each trait to its (local) implementations.
1507 fn encode_impls(&mut self, _: ()) -> LazySeq<TraitImpls> {
1508 debug!("IsolatedEncoder::encode_impls()");
1510 let mut visitor = ImplVisitor {
1512 impls: FxHashMap::default(),
1514 tcx.hir().krate().visit_all_item_likes(&mut visitor);
1516 let mut all_impls: Vec<_> = visitor.impls.into_iter().collect();
1518 // Bring everything into deterministic order for hashing
1519 all_impls.sort_by_cached_key(|&(trait_def_id, _)| {
1520 tcx.def_path_hash(trait_def_id)
1523 let all_impls: Vec<_> = all_impls
1525 .map(|(trait_def_id, mut impls)| {
1526 // Bring everything into deterministic order for hashing
1527 impls.sort_by_cached_key(|&def_index| {
1528 tcx.hir().definitions().def_path_hash(def_index)
1532 trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
1533 impls: self.lazy_seq_from_slice(&impls[..]),
1538 self.lazy_seq_from_slice(&all_impls[..])
1541 // Encodes all symbols exported from this crate into the metadata.
1543 // This pass is seeded off the reachability list calculated in the
1544 // middle::reachable module but filters out items that either don't have a
1545 // symbol associated with them (they weren't translated) or if they're an FFI
1546 // definition (as that's not defined in this crate).
1547 fn encode_exported_symbols(&mut self,
1548 exported_symbols: &[(ExportedSymbol<'_>, SymbolExportLevel)])
1549 -> EncodedExportedSymbols {
1550 // The metadata symbol name is special. It should not show up in
1551 // downstream crates.
1552 let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx));
1554 let lazy_seq = self.lazy_seq(exported_symbols
1556 .filter(|&&(ref exported_symbol, _)| {
1557 match *exported_symbol {
1558 ExportedSymbol::NoDefId(symbol_name) => {
1559 symbol_name != metadata_symbol_name
1566 EncodedExportedSymbols {
1568 position: lazy_seq.position,
1572 fn encode_dylib_dependency_formats(&mut self, _: ()) -> LazySeq<Option<LinkagePreference>> {
1573 match self.tcx.sess.dependency_formats.borrow().get(&config::CrateType::Dylib) {
1575 self.lazy_seq(arr.iter().map(|slot| {
1577 Linkage::NotLinked |
1578 Linkage::IncludedFromDylib => None,
1580 Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
1581 Linkage::Static => Some(LinkagePreference::RequireStatic),
1585 None => LazySeq::empty(),
1589 fn encode_info_for_foreign_item(&mut self,
1590 (def_id, nitem): (DefId, &hir::ForeignItem))
1594 debug!("IsolatedEncoder::encode_info_for_foreign_item({:?})", def_id);
1596 let kind = match nitem.node {
1597 hir::ForeignItemKind::Fn(_, ref names, _) => {
1599 constness: hir::Constness::NotConst,
1600 arg_names: self.encode_fn_arg_names(names),
1601 sig: self.lazy(&tcx.fn_sig(def_id)),
1603 EntryKind::ForeignFn(self.lazy(&data))
1605 hir::ForeignItemKind::Static(_, true) => EntryKind::ForeignMutStatic,
1606 hir::ForeignItemKind::Static(_, false) => EntryKind::ForeignImmStatic,
1607 hir::ForeignItemKind::Type => EntryKind::ForeignType,
1612 visibility: self.lazy(&ty::Visibility::from_hir(&nitem.vis, nitem.id, tcx)),
1613 span: self.lazy(&nitem.span),
1614 attributes: self.encode_attributes(&nitem.attrs),
1615 children: LazySeq::empty(),
1616 stability: self.encode_stability(def_id),
1617 deprecation: self.encode_deprecation(def_id),
1619 ty: Some(self.encode_item_type(def_id)),
1620 inherent_impls: LazySeq::empty(),
1621 variances: match nitem.node {
1622 hir::ForeignItemKind::Fn(..) => self.encode_variances_of(def_id),
1623 _ => LazySeq::empty(),
1625 generics: Some(self.encode_generics(def_id)),
1626 predicates: Some(self.encode_predicates(def_id)),
1627 predicates_defined_on: None,
1634 struct EncodeVisitor<'a, 'b: 'a, 'tcx: 'b> {
1635 index: IndexBuilder<'a, 'b, 'tcx>,
1638 impl<'a, 'b, 'tcx> Visitor<'tcx> for EncodeVisitor<'a, 'b, 'tcx> {
1639 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
1640 NestedVisitorMap::OnlyBodies(&self.index.tcx.hir())
1642 fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
1643 intravisit::walk_expr(self, ex);
1644 self.index.encode_info_for_expr(ex);
1646 fn visit_item(&mut self, item: &'tcx hir::Item) {
1647 intravisit::walk_item(self, item);
1648 let def_id = self.index.tcx.hir().local_def_id(item.id);
1650 hir::ItemKind::ExternCrate(_) |
1651 hir::ItemKind::Use(..) => (), // ignore these
1652 _ => self.index.record(def_id, IsolatedEncoder::encode_info_for_item, (def_id, item)),
1654 self.index.encode_addl_info_for_item(item);
1656 fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem) {
1657 intravisit::walk_foreign_item(self, ni);
1658 let def_id = self.index.tcx.hir().local_def_id(ni.id);
1659 self.index.record(def_id,
1660 IsolatedEncoder::encode_info_for_foreign_item,
1663 fn visit_variant(&mut self,
1664 v: &'tcx hir::Variant,
1665 g: &'tcx hir::Generics,
1667 intravisit::walk_variant(self, v, g, id);
1669 if let Some(ref discr) = v.node.disr_expr {
1670 let def_id = self.index.tcx.hir().local_def_id(discr.id);
1671 self.index.record(def_id, IsolatedEncoder::encode_info_for_anon_const, def_id);
1674 fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
1675 intravisit::walk_generics(self, generics);
1676 self.index.encode_info_for_generics(generics);
1678 fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
1679 intravisit::walk_ty(self, ty);
1680 self.index.encode_info_for_ty(ty);
1682 fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) {
1683 let def_id = self.index.tcx.hir().local_def_id_from_hir_id(macro_def.hir_id);
1684 self.index.record(def_id, IsolatedEncoder::encode_info_for_macro_def, macro_def);
1688 impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
1689 fn encode_fields(&mut self, adt_def_id: DefId) {
1690 let def = self.tcx.adt_def(adt_def_id);
1691 for (variant_index, variant) in def.variants.iter_enumerated() {
1692 for (field_index, field) in variant.fields.iter().enumerate() {
1693 self.record(field.did,
1694 IsolatedEncoder::encode_field,
1695 (adt_def_id, Untracked((variant_index, field_index))));
1700 fn encode_info_for_generics(&mut self, generics: &hir::Generics) {
1701 for param in &generics.params {
1703 hir::GenericParamKind::Lifetime { .. } => {}
1704 hir::GenericParamKind::Type { ref default, .. } => {
1705 let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id);
1706 let has_default = Untracked(default.is_some());
1707 let encode_info = IsolatedEncoder::encode_info_for_ty_param;
1708 self.record(def_id, encode_info, (def_id, has_default));
1710 hir::GenericParamKind::Const { .. } => {
1711 let def_id = self.tcx.hir().local_def_id_from_hir_id(param.hir_id);
1712 let encode_info = IsolatedEncoder::encode_info_for_const_param;
1713 self.record(def_id, encode_info, def_id);
1719 fn encode_info_for_ty(&mut self, ty: &hir::Ty) {
1721 hir::TyKind::Array(_, ref length) => {
1722 let def_id = self.tcx.hir().local_def_id(length.id);
1723 self.record(def_id, IsolatedEncoder::encode_info_for_anon_const, def_id);
1729 fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
1731 hir::ExprKind::Closure(..) => {
1732 let def_id = self.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
1733 self.record(def_id, IsolatedEncoder::encode_info_for_closure, def_id);
1739 /// In some cases, along with the item itself, we also
1740 /// encode some sub-items. Usually we want some info from the item
1741 /// so it's easier to do that here then to wait until we would encounter
1742 /// normally in the visitor walk.
1743 fn encode_addl_info_for_item(&mut self, item: &hir::Item) {
1744 let def_id = self.tcx.hir().local_def_id(item.id);
1746 hir::ItemKind::Static(..) |
1747 hir::ItemKind::Const(..) |
1748 hir::ItemKind::Fn(..) |
1749 hir::ItemKind::Mod(..) |
1750 hir::ItemKind::ForeignMod(..) |
1751 hir::ItemKind::GlobalAsm(..) |
1752 hir::ItemKind::ExternCrate(..) |
1753 hir::ItemKind::Use(..) |
1754 hir::ItemKind::Ty(..) |
1755 hir::ItemKind::Existential(..) |
1756 hir::ItemKind::TraitAlias(..) => {
1757 // no sub-item recording needed in these cases
1759 hir::ItemKind::Enum(..) => {
1760 self.encode_fields(def_id);
1762 let def = self.tcx.adt_def(def_id);
1763 for (i, variant) in def.variants.iter_enumerated() {
1764 self.record(variant.did,
1765 IsolatedEncoder::encode_enum_variant_info,
1766 (def_id, Untracked(i)));
1769 hir::ItemKind::Struct(ref struct_def, _) => {
1770 self.encode_fields(def_id);
1772 // If the struct has a constructor, encode it.
1773 if !struct_def.is_struct() {
1774 let ctor_def_id = self.tcx.hir().local_def_id(struct_def.id());
1775 self.record(ctor_def_id,
1776 IsolatedEncoder::encode_struct_ctor,
1777 (def_id, ctor_def_id));
1780 hir::ItemKind::Union(..) => {
1781 self.encode_fields(def_id);
1783 hir::ItemKind::Impl(..) => {
1784 for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
1785 self.record(trait_item_def_id,
1786 IsolatedEncoder::encode_info_for_impl_item,
1790 hir::ItemKind::Trait(..) => {
1791 for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
1792 self.record(item_def_id,
1793 IsolatedEncoder::encode_info_for_trait_item,
1801 struct ImplVisitor<'a, 'tcx: 'a> {
1802 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1803 impls: FxHashMap<DefId, Vec<DefIndex>>,
1806 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'a, 'tcx> {
1807 fn visit_item(&mut self, item: &hir::Item) {
1808 if let hir::ItemKind::Impl(..) = item.node {
1809 let impl_id = self.tcx.hir().local_def_id(item.id);
1810 if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) {
1812 .entry(trait_ref.def_id)
1814 .push(impl_id.index);
1819 fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem) {}
1821 fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) {
1822 // handled in `visit_item` above
1826 // NOTE(eddyb) The following comment was preserved for posterity, even
1827 // though it's no longer relevant as EBML (which uses nested & tagged
1828 // "documents") was replaced with a scheme that can't go out of bounds.
1830 // And here we run into yet another obscure archive bug: in which metadata
1831 // loaded from archives may have trailing garbage bytes. Awhile back one of
1832 // our tests was failing sporadically on the macOS 64-bit builders (both nopt
1833 // and opt) by having ebml generate an out-of-bounds panic when looking at
1836 // Upon investigation it turned out that the metadata file inside of an rlib
1837 // (and ar archive) was being corrupted. Some compilations would generate a
1838 // metadata file which would end in a few extra bytes, while other
1839 // compilations would not have these extra bytes appended to the end. These
1840 // extra bytes were interpreted by ebml as an extra tag, so they ended up
1841 // being interpreted causing the out-of-bounds.
1843 // The root cause of why these extra bytes were appearing was never
1844 // discovered, and in the meantime the solution we're employing is to insert
1845 // the length of the metadata to the start of the metadata. Later on this
1846 // will allow us to slice the metadata to the precise length that we just
1847 // generated regardless of trailing bytes that end up in it.
1849 pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
1852 let mut encoder = opaque::Encoder::new(vec![]);
1853 encoder.emit_raw_bytes(METADATA_HEADER);
1855 // Will be filled with the root position after encoding everything.
1856 encoder.emit_raw_bytes(&[0, 0, 0, 0]);
1858 let (root, mut result) = {
1859 let mut ecx = EncodeContext {
1862 lazy_state: LazyState::NoNode,
1863 type_shorthands: Default::default(),
1864 predicate_shorthands: Default::default(),
1865 source_file_cache: tcx.sess.source_map().files()[0].clone(),
1866 interpret_allocs: Default::default(),
1867 interpret_allocs_inverse: Default::default(),
1870 // Encode the rustc version string in a predictable location.
1871 rustc_version().encode(&mut ecx).unwrap();
1873 // Encode all the entries and extra information in the crate,
1874 // culminating in the `CrateRoot` which points to all of it.
1875 let root = ecx.encode_crate_root();
1876 (root, ecx.opaque.into_inner())
1879 // Encode the root position.
1880 let header = METADATA_HEADER.len();
1881 let pos = root.position;
1882 result[header + 0] = (pos >> 24) as u8;
1883 result[header + 1] = (pos >> 16) as u8;
1884 result[header + 2] = (pos >> 8) as u8;
1885 result[header + 3] = (pos >> 0) as u8;
1887 EncodedMetadata { raw_data: result }
1890 pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions {
1891 let ty = tcx.type_of(did);
1893 ty::Adt(ref def, _) => return def.repr,
1894 _ => bug!("{} is not an ADT", ty),