1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use rustc::middle::cstore::{LinkMeta, LinkagePreference, NativeLibrary};
16 use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId};
17 use rustc::hir::map::definitions::DefPathTable;
18 use rustc::middle::dependency_format::Linkage;
19 use rustc::middle::lang_items;
21 use rustc::traits::specialization_graph;
22 use rustc::ty::{self, Ty, TyCtxt, ReprOptions};
24 use rustc::session::config::{self, CrateTypeProcMacro};
25 use rustc::util::nodemap::{FxHashMap, NodeSet};
27 use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
30 use std::io::prelude::*;
34 use syntax::ast::{self, CRATE_NODE_ID};
35 use syntax::codemap::Spanned;
37 use syntax::symbol::Symbol;
38 use syntax_pos::{self, DUMMY_SP};
40 use rustc::hir::{self, PatKind};
41 use rustc::hir::itemlikevisit::ItemLikeVisitor;
42 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
43 use rustc::hir::intravisit;
45 use super::index_builder::{FromId, IndexBuilder, Untracked};
47 pub struct EncodeContext<'a, 'tcx: 'a> {
48 opaque: opaque::Encoder<'a>,
49 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
50 link_meta: &'a LinkMeta,
51 cstore: &'a cstore::CStore,
52 exported_symbols: &'a NodeSet,
54 lazy_state: LazyState,
55 type_shorthands: FxHashMap<Ty<'tcx>, usize>,
56 predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
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<'a> as Encoder>::Error;
70 fn emit_nil(&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<Ty<'tcx>> for EncodeContext<'a, 'tcx> {
114 fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> {
115 self.encode_with_shorthand(ty, &ty.sty, |ecx| &mut ecx.type_shorthands)
119 impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'a, 'tcx> {
120 fn specialized_encode(&mut self,
121 predicates: &ty::GenericPredicates<'tcx>)
122 -> Result<(), Self::Error> {
123 predicates.parent.encode(self)?;
124 predicates.predicates.len().encode(self)?;
125 for predicate in &predicates.predicates {
126 self.encode_with_shorthand(predicate, predicate, |ecx| &mut ecx.predicate_shorthands)?
132 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
133 pub fn position(&self) -> usize {
134 self.opaque.position()
137 fn emit_node<F: FnOnce(&mut Self, usize) -> R, R>(&mut self, f: F) -> R {
138 assert_eq!(self.lazy_state, LazyState::NoNode);
139 let pos = self.position();
140 self.lazy_state = LazyState::NodeStart(pos);
141 let r = f(self, pos);
142 self.lazy_state = LazyState::NoNode;
146 fn emit_lazy_distance(&mut self,
149 -> Result<(), <Self as Encoder>::Error> {
150 let min_end = position + min_size;
151 let distance = match self.lazy_state {
152 LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
153 LazyState::NodeStart(start) => {
154 assert!(min_end <= start);
157 LazyState::Previous(last_min_end) => {
158 assert!(last_min_end <= position);
159 position - last_min_end
162 self.lazy_state = LazyState::Previous(min_end);
163 self.emit_usize(distance)
166 pub fn lazy<T: Encodable>(&mut self, value: &T) -> Lazy<T> {
167 self.emit_node(|ecx, pos| {
168 value.encode(ecx).unwrap();
170 assert!(pos + Lazy::<T>::min_size() <= ecx.position());
171 Lazy::with_position(pos)
175 fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T>
176 where I: IntoIterator<Item = T>,
179 self.emit_node(|ecx, pos| {
180 let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
182 assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
183 LazySeq::with_position_and_length(pos, len)
187 fn lazy_seq_ref<'b, I, T>(&mut self, iter: I) -> LazySeq<T>
188 where I: IntoIterator<Item = &'b T>,
191 self.emit_node(|ecx, pos| {
192 let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
194 assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
195 LazySeq::with_position_and_length(pos, len)
199 /// Encode the given value or a previously cached shorthand.
200 fn encode_with_shorthand<T, U, M>(&mut self,
204 -> Result<(), <Self as Encoder>::Error>
205 where M: for<'b> Fn(&'b mut Self) -> &'b mut FxHashMap<T, usize>,
206 T: Clone + Eq + Hash,
209 let existing_shorthand = map(self).get(value).cloned();
210 if let Some(shorthand) = existing_shorthand {
211 return self.emit_usize(shorthand);
214 let start = self.position();
215 variant.encode(self)?;
216 let len = self.position() - start;
218 // The shorthand encoding uses the same usize as the
219 // discriminant, with an offset so they can't conflict.
220 let discriminant = unsafe { intrinsics::discriminant_value(variant) };
221 assert!(discriminant < SHORTHAND_OFFSET as u64);
222 let shorthand = start + SHORTHAND_OFFSET;
224 // Get the number of bits that leb128 could fit
225 // in the same space as the fully encoded type.
226 let leb128_bits = len * 7;
228 // Check that the shorthand is a not longer than the
229 // full encoding itself, i.e. it's an obvious win.
230 if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
231 map(self).insert(value.clone(), shorthand);
237 fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
239 self.lazy_seq(tcx.item_variances(def_id).iter().cloned())
242 fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
244 self.lazy(&tcx.item_type(def_id))
247 /// Encode data for the given variant of the given ADT. The
248 /// index of the variant is untracked: this is ok because we
249 /// will have to lookup the adt-def by its id, and that gives us
250 /// the right to access any information in the adt-def (including,
251 /// e.g., the length of the various vectors).
252 fn encode_enum_variant_info(&mut self,
253 (enum_did, Untracked(index)): (DefId, Untracked<usize>))
256 let def = tcx.lookup_adt_def(enum_did);
257 let variant = &def.variants[index];
258 let def_id = variant.did;
260 let data = VariantData {
261 ctor_kind: variant.ctor_kind,
262 discr: variant.discr,
263 evaluated_discr: match variant.discr {
264 ty::VariantDiscr::Explicit(def_id) => {
265 ty::queries::monomorphic_const_eval::get(tcx, DUMMY_SP, def_id).ok()
267 ty::VariantDiscr::Relative(_) => None
272 let enum_id = tcx.hir.as_local_node_id(enum_did).unwrap();
273 let enum_vis = &tcx.hir.expect_item(enum_id).vis;
276 kind: EntryKind::Variant(self.lazy(&data)),
277 visibility: self.lazy(&ty::Visibility::from_hir(enum_vis, enum_id, tcx)),
278 span: self.lazy(&tcx.def_span(def_id)),
279 attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
280 children: self.lazy_seq(variant.fields.iter().map(|f| {
281 assert!(f.did.is_local());
284 stability: self.encode_stability(def_id),
285 deprecation: self.encode_deprecation(def_id),
287 ty: Some(self.encode_item_type(def_id)),
288 inherent_impls: LazySeq::empty(),
289 variances: LazySeq::empty(),
290 generics: Some(self.encode_generics(def_id)),
291 predicates: Some(self.encode_predicates(def_id)),
294 mir: self.encode_mir(def_id),
298 fn encode_info_for_mod(&mut self,
299 FromId(id, (md, attrs, vis)): FromId<(&hir::Mod,
304 let def_id = tcx.hir.local_def_id(id);
307 reexports: match tcx.export_map.get(&id) {
308 Some(exports) if *vis == hir::Public => self.lazy_seq_ref(exports),
309 _ => LazySeq::empty(),
314 kind: EntryKind::Mod(self.lazy(&data)),
315 visibility: self.lazy(&ty::Visibility::from_hir(vis, id, tcx)),
316 span: self.lazy(&md.inner),
317 attributes: self.encode_attributes(attrs),
318 children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
319 tcx.hir.local_def_id(item_id.id).index
321 stability: self.encode_stability(def_id),
322 deprecation: self.encode_deprecation(def_id),
325 inherent_impls: LazySeq::empty(),
326 variances: LazySeq::empty(),
336 impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
337 fn encode_fields(&mut self, adt_def_id: DefId) {
338 let def = self.tcx.lookup_adt_def(adt_def_id);
339 for (variant_index, variant) in def.variants.iter().enumerate() {
340 for (field_index, field) in variant.fields.iter().enumerate() {
341 self.record(field.did,
342 EncodeContext::encode_field,
343 (adt_def_id, Untracked((variant_index, field_index))));
349 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
350 /// Encode data for the given field of the given variant of the
351 /// given ADT. The indices of the variant/field are untracked:
352 /// this is ok because we will have to lookup the adt-def by its
353 /// id, and that gives us the right to access any information in
354 /// the adt-def (including, e.g., the length of the various
356 fn encode_field(&mut self,
357 (adt_def_id, Untracked((variant_index, field_index))): (DefId,
362 let variant = &tcx.lookup_adt_def(adt_def_id).variants[variant_index];
363 let field = &variant.fields[field_index];
365 let def_id = field.did;
366 let variant_id = tcx.hir.as_local_node_id(variant.did).unwrap();
367 let variant_data = tcx.hir.expect_variant_data(variant_id);
370 kind: EntryKind::Field,
371 visibility: self.lazy(&field.vis),
372 span: self.lazy(&tcx.def_span(def_id)),
373 attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
374 children: LazySeq::empty(),
375 stability: self.encode_stability(def_id),
376 deprecation: self.encode_deprecation(def_id),
378 ty: Some(self.encode_item_type(def_id)),
379 inherent_impls: LazySeq::empty(),
380 variances: LazySeq::empty(),
381 generics: Some(self.encode_generics(def_id)),
382 predicates: Some(self.encode_predicates(def_id)),
389 fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<'tcx> {
391 let variant = tcx.lookup_adt_def(adt_def_id).struct_variant();
393 let data = VariantData {
394 ctor_kind: variant.ctor_kind,
395 discr: variant.discr,
396 evaluated_discr: None,
397 struct_ctor: Some(def_id.index),
400 let struct_id = tcx.hir.as_local_node_id(adt_def_id).unwrap();
401 let struct_vis = &tcx.hir.expect_item(struct_id).vis;
402 let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
403 for field in &variant.fields {
404 if ctor_vis.is_at_least(field.vis, tcx) {
405 ctor_vis = field.vis;
409 let repr_options = get_repr_options(&tcx, adt_def_id);
412 kind: EntryKind::Struct(self.lazy(&data), repr_options),
413 visibility: self.lazy(&ctor_vis),
414 span: self.lazy(&tcx.def_span(def_id)),
415 attributes: LazySeq::empty(),
416 children: LazySeq::empty(),
417 stability: self.encode_stability(def_id),
418 deprecation: self.encode_deprecation(def_id),
420 ty: Some(self.encode_item_type(def_id)),
421 inherent_impls: LazySeq::empty(),
422 variances: LazySeq::empty(),
423 generics: Some(self.encode_generics(def_id)),
424 predicates: Some(self.encode_predicates(def_id)),
427 mir: self.encode_mir(def_id),
431 fn encode_generics(&mut self, def_id: DefId) -> Lazy<ty::Generics> {
433 self.lazy(tcx.item_generics(def_id))
436 fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
438 self.lazy(&tcx.item_predicates(def_id))
441 fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
444 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
445 let ast_item = tcx.hir.expect_trait_item(node_id);
446 let trait_item = tcx.associated_item(def_id);
448 let container = match trait_item.defaultness {
449 hir::Defaultness::Default { has_value: true } =>
450 AssociatedContainer::TraitWithDefault,
451 hir::Defaultness::Default { has_value: false } =>
452 AssociatedContainer::TraitRequired,
453 hir::Defaultness::Final =>
454 span_bug!(ast_item.span, "traits cannot have final items"),
457 let kind = match trait_item.kind {
458 ty::AssociatedKind::Const => {
459 EntryKind::AssociatedConst(container, 0)
461 ty::AssociatedKind::Method => {
462 let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node {
463 let arg_names = match *m {
464 hir::TraitMethod::Required(ref names) => {
465 self.encode_fn_arg_names(names)
467 hir::TraitMethod::Provided(body) => {
468 self.encode_fn_arg_names_for_body(body)
472 constness: hir::Constness::NotConst,
478 EntryKind::Method(self.lazy(&MethodData {
480 container: container,
481 has_self: trait_item.method_has_self_argument,
484 ty::AssociatedKind::Type => EntryKind::AssociatedType(container),
489 visibility: self.lazy(&trait_item.vis),
490 span: self.lazy(&ast_item.span),
491 attributes: self.encode_attributes(&ast_item.attrs),
492 children: LazySeq::empty(),
493 stability: self.encode_stability(def_id),
494 deprecation: self.encode_deprecation(def_id),
496 ty: match trait_item.kind {
497 ty::AssociatedKind::Const |
498 ty::AssociatedKind::Method => {
499 Some(self.encode_item_type(def_id))
501 ty::AssociatedKind::Type => {
502 if trait_item.defaultness.has_value() {
503 Some(self.encode_item_type(def_id))
509 inherent_impls: LazySeq::empty(),
510 variances: LazySeq::empty(),
511 generics: Some(self.encode_generics(def_id)),
512 predicates: Some(self.encode_predicates(def_id)),
514 ast: if let hir::TraitItemKind::Const(_, Some(body)) = ast_item.node {
515 Some(self.encode_body(body))
519 mir: self.encode_mir(def_id),
523 fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'tcx> {
524 let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
525 let ast_item = self.tcx.hir.expect_impl_item(node_id);
526 let impl_item = self.tcx.associated_item(def_id);
528 let container = match impl_item.defaultness {
529 hir::Defaultness::Default { has_value: true } => AssociatedContainer::ImplDefault,
530 hir::Defaultness::Final => AssociatedContainer::ImplFinal,
531 hir::Defaultness::Default { has_value: false } =>
532 span_bug!(ast_item.span, "impl items always have values (currently)"),
535 let kind = match impl_item.kind {
536 ty::AssociatedKind::Const => {
537 EntryKind::AssociatedConst(container,
538 ty::queries::mir_const_qualif::get(self.tcx, ast_item.span, def_id))
540 ty::AssociatedKind::Method => {
541 let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
543 constness: sig.constness,
544 arg_names: self.encode_fn_arg_names_for_body(body),
549 EntryKind::Method(self.lazy(&MethodData {
551 container: container,
552 has_self: impl_item.method_has_self_argument,
555 ty::AssociatedKind::Type => EntryKind::AssociatedType(container)
558 let (ast, mir) = if let hir::ImplItemKind::Const(_, body) = ast_item.node {
560 } else if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
561 let generics = self.tcx.item_generics(def_id);
562 let types = generics.parent_types as usize + generics.types.len();
563 let needs_inline = types > 0 || attr::requests_inline(&ast_item.attrs);
564 let is_const_fn = sig.constness == hir::Constness::Const;
565 let ast = if is_const_fn { Some(body) } else { None };
566 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
567 (ast, needs_inline || is_const_fn || always_encode_mir)
574 visibility: self.lazy(&impl_item.vis),
575 span: self.lazy(&ast_item.span),
576 attributes: self.encode_attributes(&ast_item.attrs),
577 children: LazySeq::empty(),
578 stability: self.encode_stability(def_id),
579 deprecation: self.encode_deprecation(def_id),
581 ty: Some(self.encode_item_type(def_id)),
582 inherent_impls: LazySeq::empty(),
583 variances: LazySeq::empty(),
584 generics: Some(self.encode_generics(def_id)),
585 predicates: Some(self.encode_predicates(def_id)),
587 ast: ast.map(|body| self.encode_body(body)),
588 mir: if mir { self.encode_mir(def_id) } else { None },
592 fn encode_fn_arg_names_for_body(&mut self, body_id: hir::BodyId)
593 -> LazySeq<ast::Name> {
594 let _ignore = self.tcx.dep_graph.in_ignore();
595 let body = self.tcx.hir.body(body_id);
596 self.lazy_seq(body.arguments.iter().map(|arg| {
598 PatKind::Binding(_, _, name, _) => name.node,
599 _ => Symbol::intern("")
604 fn encode_fn_arg_names(&mut self, names: &[Spanned<ast::Name>])
605 -> LazySeq<ast::Name> {
606 self.lazy_seq(names.iter().map(|name| name.node))
609 fn encode_mir(&mut self, def_id: DefId) -> Option<Lazy<mir::Mir<'tcx>>> {
610 self.tcx.maps.mir.borrow().get(&def_id).map(|mir| self.lazy(&*mir.borrow()))
613 // Encodes the inherent implementations of a structure, enumeration, or trait.
614 fn encode_inherent_implementations(&mut self, def_id: DefId) -> LazySeq<DefIndex> {
615 match self.tcx.maps.inherent_impls.borrow().get(&def_id) {
616 None => LazySeq::empty(),
617 Some(implementations) => {
618 self.lazy_seq(implementations.iter().map(|&def_id| {
619 assert!(def_id.is_local());
626 fn encode_stability(&mut self, def_id: DefId) -> Option<Lazy<attr::Stability>> {
627 self.tcx.lookup_stability(def_id).map(|stab| self.lazy(stab))
630 fn encode_deprecation(&mut self, def_id: DefId) -> Option<Lazy<attr::Deprecation>> {
631 self.tcx.lookup_deprecation(def_id).map(|depr| self.lazy(&depr))
634 fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) -> Entry<'tcx> {
637 debug!("encoding info for item at {}",
638 tcx.sess.codemap().span_to_string(item.span));
640 let kind = match item.node {
641 hir::ItemStatic(_, hir::MutMutable, _) => EntryKind::MutStatic,
642 hir::ItemStatic(_, hir::MutImmutable, _) => EntryKind::ImmStatic,
643 hir::ItemConst(..) => {
644 EntryKind::Const(ty::queries::mir_const_qualif::get(tcx, item.span, def_id))
646 hir::ItemFn(_, _, constness, .., body) => {
648 constness: constness,
649 arg_names: self.encode_fn_arg_names_for_body(body),
652 EntryKind::Fn(self.lazy(&data))
654 hir::ItemMod(ref m) => {
655 return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
657 hir::ItemForeignMod(_) => EntryKind::ForeignMod,
658 hir::ItemTy(..) => EntryKind::Type,
659 hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
660 hir::ItemStruct(ref struct_def, _) => {
661 let variant = tcx.lookup_adt_def(def_id).struct_variant();
663 // Encode def_ids for each field and method
664 // for methods, write all the stuff get_trait_method
666 let struct_ctor = if !struct_def.is_struct() {
667 Some(tcx.hir.local_def_id(struct_def.id()).index)
672 let repr_options = get_repr_options(&tcx, def_id);
674 EntryKind::Struct(self.lazy(&VariantData {
675 ctor_kind: variant.ctor_kind,
676 discr: variant.discr,
677 evaluated_discr: None,
678 struct_ctor: struct_ctor,
681 hir::ItemUnion(..) => {
682 let variant = tcx.lookup_adt_def(def_id).struct_variant();
683 let repr_options = get_repr_options(&tcx, def_id);
685 EntryKind::Union(self.lazy(&VariantData {
686 ctor_kind: variant.ctor_kind,
687 discr: variant.discr,
688 evaluated_discr: None,
692 hir::ItemDefaultImpl(..) => {
693 let data = ImplData {
694 polarity: hir::ImplPolarity::Positive,
696 coerce_unsized_info: None,
697 trait_ref: tcx.impl_trait_ref(def_id).map(|trait_ref| self.lazy(&trait_ref)),
700 EntryKind::DefaultImpl(self.lazy(&data))
702 hir::ItemImpl(_, polarity, ..) => {
703 let trait_ref = tcx.impl_trait_ref(def_id);
704 let parent = if let Some(trait_ref) = trait_ref {
705 let trait_def = tcx.lookup_trait_def(trait_ref.def_id);
706 trait_def.ancestors(def_id).skip(1).next().and_then(|node| {
708 specialization_graph::Node::Impl(parent) => Some(parent),
716 // if this is an impl of `CoerceUnsized`, create its
717 // "unsized info", else just store None
718 let coerce_unsized_info =
719 trait_ref.and_then(|t| {
720 if Some(t.def_id) == tcx.lang_items.coerce_unsized_trait() {
721 Some(ty::queries::coerce_unsized_info::get(tcx, item.span, def_id))
727 let data = ImplData {
730 coerce_unsized_info: coerce_unsized_info,
731 trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)),
734 EntryKind::Impl(self.lazy(&data))
736 hir::ItemTrait(..) => {
737 let trait_def = tcx.lookup_trait_def(def_id);
738 let data = TraitData {
739 unsafety: trait_def.unsafety,
740 paren_sugar: trait_def.paren_sugar,
741 has_default_impl: tcx.trait_has_default_impl(def_id),
742 super_predicates: self.lazy(&tcx.item_super_predicates(def_id)),
745 EntryKind::Trait(self.lazy(&data))
747 hir::ItemExternCrate(_) |
748 hir::ItemUse(..) => bug!("cannot encode info for item {:?}", item),
753 visibility: self.lazy(&ty::Visibility::from_hir(&item.vis, item.id, tcx)),
754 span: self.lazy(&item.span),
755 attributes: self.encode_attributes(&item.attrs),
756 children: match item.node {
757 hir::ItemForeignMod(ref fm) => {
758 self.lazy_seq(fm.items
760 .map(|foreign_item| tcx.hir.local_def_id(foreign_item.id).index))
762 hir::ItemEnum(..) => {
763 let def = self.tcx.lookup_adt_def(def_id);
764 self.lazy_seq(def.variants.iter().map(|v| {
765 assert!(v.did.is_local());
769 hir::ItemStruct(..) |
770 hir::ItemUnion(..) => {
771 let def = self.tcx.lookup_adt_def(def_id);
772 self.lazy_seq(def.struct_variant().fields.iter().map(|f| {
773 assert!(f.did.is_local());
778 hir::ItemTrait(..) => {
779 self.lazy_seq(tcx.associated_item_def_ids(def_id).iter().map(|&def_id| {
780 assert!(def_id.is_local());
784 _ => LazySeq::empty(),
786 stability: self.encode_stability(def_id),
787 deprecation: self.encode_deprecation(def_id),
789 ty: match item.node {
790 hir::ItemStatic(..) |
795 hir::ItemStruct(..) |
797 hir::ItemImpl(..) => Some(self.encode_item_type(def_id)),
800 inherent_impls: self.encode_inherent_implementations(def_id),
801 variances: match item.node {
803 hir::ItemStruct(..) |
805 hir::ItemTrait(..) => self.encode_item_variances(def_id),
806 _ => LazySeq::empty(),
808 generics: match item.node {
809 hir::ItemStatic(..) |
814 hir::ItemStruct(..) |
817 hir::ItemTrait(..) => Some(self.encode_generics(def_id)),
820 predicates: match item.node {
821 hir::ItemStatic(..) |
826 hir::ItemStruct(..) |
829 hir::ItemTrait(..) => Some(self.encode_predicates(def_id)),
833 ast: match item.node {
834 hir::ItemConst(_, body) |
835 hir::ItemFn(_, _, hir::Constness::Const, _, _, body) => {
836 Some(self.encode_body(body))
840 mir: match item.node {
841 hir::ItemStatic(..) if self.tcx.sess.opts.debugging_opts.always_encode_mir => {
842 self.encode_mir(def_id)
844 hir::ItemConst(..) => self.encode_mir(def_id),
845 hir::ItemFn(_, _, constness, _, ref generics, _) => {
846 let tps_len = generics.ty_params.len();
847 let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
848 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
849 if needs_inline || constness == hir::Constness::Const || always_encode_mir {
850 self.encode_mir(def_id)
860 /// Serialize the text of exported macros
861 fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
862 use syntax::print::pprust;
864 kind: EntryKind::MacroDef(self.lazy(&MacroDef {
865 body: pprust::tts_to_string(¯o_def.body.trees().collect::<Vec<_>>()),
867 visibility: self.lazy(&ty::Visibility::Public),
868 span: self.lazy(¯o_def.span),
870 attributes: self.encode_attributes(¯o_def.attrs),
871 children: LazySeq::empty(),
875 inherent_impls: LazySeq::empty(),
876 variances: LazySeq::empty(),
885 impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
886 /// In some cases, along with the item itself, we also
887 /// encode some sub-items. Usually we want some info from the item
888 /// so it's easier to do that here then to wait until we would encounter
889 /// normally in the visitor walk.
890 fn encode_addl_info_for_item(&mut self, item: &hir::Item) {
891 let def_id = self.tcx.hir.local_def_id(item.id);
893 hir::ItemStatic(..) |
897 hir::ItemForeignMod(..) |
898 hir::ItemExternCrate(..) |
900 hir::ItemDefaultImpl(..) |
902 // no sub-item recording needed in these cases
904 hir::ItemEnum(..) => {
905 self.encode_fields(def_id);
907 let def = self.tcx.lookup_adt_def(def_id);
908 for (i, variant) in def.variants.iter().enumerate() {
909 self.record(variant.did,
910 EncodeContext::encode_enum_variant_info,
911 (def_id, Untracked(i)));
914 hir::ItemStruct(ref struct_def, _) => {
915 self.encode_fields(def_id);
917 // If the struct has a constructor, encode it.
918 if !struct_def.is_struct() {
919 let ctor_def_id = self.tcx.hir.local_def_id(struct_def.id());
920 self.record(ctor_def_id,
921 EncodeContext::encode_struct_ctor,
922 (def_id, ctor_def_id));
925 hir::ItemUnion(..) => {
926 self.encode_fields(def_id);
928 hir::ItemImpl(..) => {
929 for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
930 self.record(trait_item_def_id,
931 EncodeContext::encode_info_for_impl_item,
935 hir::ItemTrait(..) => {
936 for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
937 self.record(item_def_id,
938 EncodeContext::encode_info_for_trait_item,
946 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
947 fn encode_info_for_foreign_item(&mut self,
948 (def_id, nitem): (DefId, &hir::ForeignItem))
952 debug!("writing foreign item {}", tcx.node_path_str(nitem.id));
954 let kind = match nitem.node {
955 hir::ForeignItemFn(_, ref names, _) => {
957 constness: hir::Constness::NotConst,
958 arg_names: self.encode_fn_arg_names(names),
960 EntryKind::ForeignFn(self.lazy(&data))
962 hir::ForeignItemStatic(_, true) => EntryKind::ForeignMutStatic,
963 hir::ForeignItemStatic(_, false) => EntryKind::ForeignImmStatic,
968 visibility: self.lazy(&ty::Visibility::from_hir(&nitem.vis, nitem.id, tcx)),
969 span: self.lazy(&nitem.span),
970 attributes: self.encode_attributes(&nitem.attrs),
971 children: LazySeq::empty(),
972 stability: self.encode_stability(def_id),
973 deprecation: self.encode_deprecation(def_id),
975 ty: Some(self.encode_item_type(def_id)),
976 inherent_impls: LazySeq::empty(),
977 variances: LazySeq::empty(),
978 generics: Some(self.encode_generics(def_id)),
979 predicates: Some(self.encode_predicates(def_id)),
987 struct EncodeVisitor<'a, 'b: 'a, 'tcx: 'b> {
988 index: IndexBuilder<'a, 'b, 'tcx>,
991 impl<'a, 'b, 'tcx> Visitor<'tcx> for EncodeVisitor<'a, 'b, 'tcx> {
992 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
993 NestedVisitorMap::OnlyBodies(&self.index.tcx.hir)
995 fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
996 intravisit::walk_expr(self, ex);
997 self.index.encode_info_for_expr(ex);
999 fn visit_item(&mut self, item: &'tcx hir::Item) {
1000 intravisit::walk_item(self, item);
1001 let def_id = self.index.tcx.hir.local_def_id(item.id);
1003 hir::ItemExternCrate(_) |
1004 hir::ItemUse(..) => (), // ignore these
1005 _ => self.index.record(def_id, EncodeContext::encode_info_for_item, (def_id, item)),
1007 self.index.encode_addl_info_for_item(item);
1009 fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem) {
1010 intravisit::walk_foreign_item(self, ni);
1011 let def_id = self.index.tcx.hir.local_def_id(ni.id);
1012 self.index.record(def_id,
1013 EncodeContext::encode_info_for_foreign_item,
1016 fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
1017 intravisit::walk_generics(self, generics);
1018 self.index.encode_info_for_generics(generics);
1020 fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
1021 intravisit::walk_ty(self, ty);
1022 self.index.encode_info_for_ty(ty);
1024 fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) {
1025 let def_id = self.index.tcx.hir.local_def_id(macro_def.id);
1026 self.index.record(def_id, EncodeContext::encode_info_for_macro_def, macro_def);
1030 impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
1031 fn encode_info_for_generics(&mut self, generics: &hir::Generics) {
1032 for ty_param in &generics.ty_params {
1033 let def_id = self.tcx.hir.local_def_id(ty_param.id);
1034 let has_default = Untracked(ty_param.default.is_some());
1035 self.record(def_id, EncodeContext::encode_info_for_ty_param, (def_id, has_default));
1039 fn encode_info_for_ty(&mut self, ty: &hir::Ty) {
1040 if let hir::TyImplTrait(_) = ty.node {
1041 let def_id = self.tcx.hir.local_def_id(ty.id);
1042 self.record(def_id, EncodeContext::encode_info_for_anon_ty, def_id);
1046 fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
1048 hir::ExprClosure(..) => {
1049 let def_id = self.tcx.hir.local_def_id(expr.id);
1050 self.record(def_id, EncodeContext::encode_info_for_closure, def_id);
1057 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1058 fn encode_info_for_ty_param(&mut self,
1059 (def_id, Untracked(has_default)): (DefId, Untracked<bool>))
1063 kind: EntryKind::Type,
1064 visibility: self.lazy(&ty::Visibility::Public),
1065 span: self.lazy(&tcx.def_span(def_id)),
1066 attributes: LazySeq::empty(),
1067 children: LazySeq::empty(),
1071 ty: if has_default {
1072 Some(self.encode_item_type(def_id))
1076 inherent_impls: LazySeq::empty(),
1077 variances: LazySeq::empty(),
1086 fn encode_info_for_anon_ty(&mut self, def_id: DefId) -> Entry<'tcx> {
1089 kind: EntryKind::Type,
1090 visibility: self.lazy(&ty::Visibility::Public),
1091 span: self.lazy(&tcx.def_span(def_id)),
1092 attributes: LazySeq::empty(),
1093 children: LazySeq::empty(),
1097 ty: Some(self.encode_item_type(def_id)),
1098 inherent_impls: LazySeq::empty(),
1099 variances: LazySeq::empty(),
1100 generics: Some(self.encode_generics(def_id)),
1101 predicates: Some(self.encode_predicates(def_id)),
1108 fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
1111 let data = ClosureData {
1112 kind: tcx.closure_kind(def_id),
1113 ty: self.lazy(&tcx.closure_type(def_id)),
1117 kind: EntryKind::Closure(self.lazy(&data)),
1118 visibility: self.lazy(&ty::Visibility::Public),
1119 span: self.lazy(&tcx.def_span(def_id)),
1120 attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
1121 children: LazySeq::empty(),
1125 ty: Some(self.encode_item_type(def_id)),
1126 inherent_impls: LazySeq::empty(),
1127 variances: LazySeq::empty(),
1128 generics: Some(self.encode_generics(def_id)),
1132 mir: self.encode_mir(def_id),
1136 fn encode_info_for_items(&mut self) -> Index {
1137 let krate = self.tcx.hir.krate();
1138 let mut index = IndexBuilder::new(self);
1139 index.record(DefId::local(CRATE_DEF_INDEX),
1140 EncodeContext::encode_info_for_mod,
1141 FromId(CRATE_NODE_ID, (&krate.module, &krate.attrs, &hir::Public)));
1142 let mut visitor = EncodeVisitor { index: index };
1143 krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
1144 for macro_def in &krate.exported_macros {
1145 visitor.visit_macro_def(macro_def);
1147 visitor.index.into_items()
1150 fn encode_attributes(&mut self, attrs: &[ast::Attribute]) -> LazySeq<ast::Attribute> {
1151 self.lazy_seq_ref(attrs)
1154 fn encode_crate_deps(&mut self) -> LazySeq<CrateDep> {
1155 fn get_ordered_deps(cstore: &cstore::CStore) -> Vec<(CrateNum, Rc<cstore::CrateMetadata>)> {
1156 // Pull the cnums and name,vers,hash out of cstore
1157 let mut deps = Vec::new();
1158 cstore.iter_crate_data(|cnum, val| {
1159 deps.push((cnum, val.clone()));
1163 deps.sort_by(|kv1, kv2| kv1.0.cmp(&kv2.0));
1165 // Sanity-check the crate numbers
1166 let mut expected_cnum = 1;
1167 for &(n, _) in &deps {
1168 assert_eq!(n, CrateNum::new(expected_cnum));
1175 // We're just going to write a list of crate 'name-hash-version's, with
1176 // the assumption that they are numbered 1 to n.
1177 // FIXME (#2166): This is not nearly enough to support correct versioning
1178 // but is enough to get transitive crate dependencies working.
1179 let deps = get_ordered_deps(self.cstore);
1180 self.lazy_seq(deps.iter().map(|&(_, ref dep)| {
1184 kind: dep.dep_kind.get(),
1189 fn encode_lang_items(&mut self) -> (LazySeq<(DefIndex, usize)>, LazySeq<lang_items::LangItem>) {
1191 let lang_items = tcx.lang_items.items().iter();
1192 (self.lazy_seq(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
1193 if let Some(def_id) = opt_def_id {
1194 if def_id.is_local() {
1195 return Some((def_id.index, i));
1200 self.lazy_seq_ref(&tcx.lang_items.missing))
1203 fn encode_native_libraries(&mut self) -> LazySeq<NativeLibrary> {
1204 let used_libraries = self.tcx.sess.cstore.used_libraries();
1205 self.lazy_seq(used_libraries)
1208 fn encode_codemap(&mut self) -> LazySeq<syntax_pos::FileMap> {
1209 let codemap = self.tcx.sess.codemap();
1210 let all_filemaps = codemap.files.borrow();
1211 self.lazy_seq_ref(all_filemaps.iter()
1213 // No need to re-export imported filemaps, as any downstream
1214 // crate will import them from their original source.
1215 !filemap.is_imported()
1217 .map(|filemap| &**filemap))
1220 fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
1221 let definitions = self.tcx.hir.definitions();
1222 self.lazy(definitions.def_path_table())
1226 struct ImplVisitor<'a, 'tcx: 'a> {
1227 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1228 impls: FxHashMap<DefId, Vec<DefIndex>>,
1231 impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'a, 'tcx> {
1232 fn visit_item(&mut self, item: &hir::Item) {
1233 if let hir::ItemImpl(..) = item.node {
1234 let impl_id = self.tcx.hir.local_def_id(item.id);
1235 if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) {
1237 .entry(trait_ref.def_id)
1239 .push(impl_id.index);
1244 fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem) {}
1246 fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) {
1247 // handled in `visit_item` above
1251 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1252 /// Encodes an index, mapping each trait to its (local) implementations.
1253 fn encode_impls(&mut self) -> LazySeq<TraitImpls> {
1254 let mut visitor = ImplVisitor {
1258 self.tcx.hir.krate().visit_all_item_likes(&mut visitor);
1260 let all_impls: Vec<_> = visitor.impls
1262 .map(|(trait_def_id, impls)| {
1264 trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
1265 impls: self.lazy_seq(impls),
1270 self.lazy_seq(all_impls)
1273 // Encodes all symbols exported from this crate into the metadata.
1275 // This pass is seeded off the reachability list calculated in the
1276 // middle::reachable module but filters out items that either don't have a
1277 // symbol associated with them (they weren't translated) or if they're an FFI
1278 // definition (as that's not defined in this crate).
1279 fn encode_exported_symbols(&mut self) -> LazySeq<DefIndex> {
1280 let exported_symbols = self.exported_symbols;
1282 self.lazy_seq(exported_symbols.iter().map(|&id| tcx.hir.local_def_id(id).index))
1285 fn encode_dylib_dependency_formats(&mut self) -> LazySeq<Option<LinkagePreference>> {
1286 match self.tcx.sess.dependency_formats.borrow().get(&config::CrateTypeDylib) {
1288 self.lazy_seq(arr.iter().map(|slot| {
1290 Linkage::NotLinked |
1291 Linkage::IncludedFromDylib => None,
1293 Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
1294 Linkage::Static => Some(LinkagePreference::RequireStatic),
1298 None => LazySeq::empty(),
1302 fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
1303 let mut i = self.position();
1304 let crate_deps = self.encode_crate_deps();
1305 let dylib_dependency_formats = self.encode_dylib_dependency_formats();
1306 let dep_bytes = self.position() - i;
1308 // Encode the language items.
1309 i = self.position();
1310 let (lang_items, lang_items_missing) = self.encode_lang_items();
1311 let lang_item_bytes = self.position() - i;
1313 // Encode the native libraries used
1314 i = self.position();
1315 let native_libraries = self.encode_native_libraries();
1316 let native_lib_bytes = self.position() - i;
1319 i = self.position();
1320 let codemap = self.encode_codemap();
1321 let codemap_bytes = self.position() - i;
1323 // Encode DefPathTable
1324 i = self.position();
1325 let def_path_table = self.encode_def_path_table();
1326 let def_path_table_bytes = self.position() - i;
1328 // Encode the def IDs of impls, for coherence checking.
1329 i = self.position();
1330 let impls = self.encode_impls();
1331 let impl_bytes = self.position() - i;
1333 // Encode exported symbols info.
1334 i = self.position();
1335 let exported_symbols = self.encode_exported_symbols();
1336 let exported_symbols_bytes = self.position() - i;
1338 // Encode and index the items.
1339 i = self.position();
1340 let items = self.encode_info_for_items();
1341 let item_bytes = self.position() - i;
1343 i = self.position();
1344 let index = items.write_index(&mut self.opaque.cursor);
1345 let index_bytes = self.position() - i;
1348 let link_meta = self.link_meta;
1349 let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
1350 let root = self.lazy(&CrateRoot {
1351 name: link_meta.crate_name,
1352 triple: tcx.sess.opts.target_triple.clone(),
1353 hash: link_meta.crate_hash,
1354 disambiguator: tcx.sess.local_crate_disambiguator(),
1355 panic_strategy: tcx.sess.panic_strategy(),
1356 plugin_registrar_fn: tcx.sess
1357 .plugin_registrar_fn
1359 .map(|id| tcx.hir.local_def_id(id).index),
1360 macro_derive_registrar: if is_proc_macro {
1361 let id = tcx.sess.derive_registrar_fn.get().unwrap();
1362 Some(tcx.hir.local_def_id(id).index)
1367 crate_deps: crate_deps,
1368 dylib_dependency_formats: dylib_dependency_formats,
1369 lang_items: lang_items,
1370 lang_items_missing: lang_items_missing,
1371 native_libraries: native_libraries,
1373 def_path_table: def_path_table,
1375 exported_symbols: exported_symbols,
1379 let total_bytes = self.position();
1381 if self.tcx.sess.meta_stats() {
1382 let mut zero_bytes = 0;
1383 for e in self.opaque.cursor.get_ref() {
1389 println!("metadata stats:");
1390 println!(" dep bytes: {}", dep_bytes);
1391 println!(" lang item bytes: {}", lang_item_bytes);
1392 println!(" native bytes: {}", native_lib_bytes);
1393 println!(" codemap bytes: {}", codemap_bytes);
1394 println!(" impl bytes: {}", impl_bytes);
1395 println!(" exp. symbols bytes: {}", exported_symbols_bytes);
1396 println!(" def-path table bytes: {}", def_path_table_bytes);
1397 println!(" item bytes: {}", item_bytes);
1398 println!(" index bytes: {}", index_bytes);
1399 println!(" zero bytes: {}", zero_bytes);
1400 println!(" total bytes: {}", total_bytes);
1407 // NOTE(eddyb) The following comment was preserved for posterity, even
1408 // though it's no longer relevant as EBML (which uses nested & tagged
1409 // "documents") was replaced with a scheme that can't go out of bounds.
1411 // And here we run into yet another obscure archive bug: in which metadata
1412 // loaded from archives may have trailing garbage bytes. Awhile back one of
1413 // our tests was failing sporadically on the macOS 64-bit builders (both nopt
1414 // and opt) by having ebml generate an out-of-bounds panic when looking at
1417 // Upon investigation it turned out that the metadata file inside of an rlib
1418 // (and ar archive) was being corrupted. Some compilations would generate a
1419 // metadata file which would end in a few extra bytes, while other
1420 // compilations would not have these extra bytes appended to the end. These
1421 // extra bytes were interpreted by ebml as an extra tag, so they ended up
1422 // being interpreted causing the out-of-bounds.
1424 // The root cause of why these extra bytes were appearing was never
1425 // discovered, and in the meantime the solution we're employing is to insert
1426 // the length of the metadata to the start of the metadata. Later on this
1427 // will allow us to slice the metadata to the precise length that we just
1428 // generated regardless of trailing bytes that end up in it.
1430 pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1431 cstore: &cstore::CStore,
1432 link_meta: &LinkMeta,
1433 exported_symbols: &NodeSet)
1435 let mut cursor = Cursor::new(vec![]);
1436 cursor.write_all(METADATA_HEADER).unwrap();
1438 // Will be filed with the root position after encoding everything.
1439 cursor.write_all(&[0, 0, 0, 0]).unwrap();
1442 let mut ecx = EncodeContext {
1443 opaque: opaque::Encoder::new(&mut cursor),
1445 link_meta: link_meta,
1447 exported_symbols: exported_symbols,
1448 lazy_state: LazyState::NoNode,
1449 type_shorthands: Default::default(),
1450 predicate_shorthands: Default::default(),
1453 // Encode the rustc version string in a predictable location.
1454 rustc_version().encode(&mut ecx).unwrap();
1456 // Encode all the entries and extra information in the crate,
1457 // culminating in the `CrateRoot` which points to all of it.
1458 ecx.encode_crate_root()
1460 let mut result = cursor.into_inner();
1462 // Encode the root position.
1463 let header = METADATA_HEADER.len();
1464 let pos = root.position;
1465 result[header + 0] = (pos >> 24) as u8;
1466 result[header + 1] = (pos >> 16) as u8;
1467 result[header + 2] = (pos >> 8) as u8;
1468 result[header + 3] = (pos >> 0) as u8;
1473 pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions {
1474 let ty = tcx.item_type(did);
1476 ty::TyAdt(ref def, _) => return def.repr,
1477 _ => bug!("{} is not an ADT", ty),