]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/encoder.rs
Auto merge of #40867 - alexcrichton:rollup, r=alexcrichton
[rust.git] / src / librustc_metadata / encoder.rs
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.
4 //
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.
10
11 use cstore;
12 use index::Index;
13 use schema::*;
14
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;
20 use rustc::mir;
21 use rustc::traits::specialization_graph;
22 use rustc::ty::{self, Ty, TyCtxt, ReprOptions};
23
24 use rustc::session::config::{self, CrateTypeProcMacro};
25 use rustc::util::nodemap::{FxHashMap, NodeSet};
26
27 use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
28 use std::hash::Hash;
29 use std::intrinsics;
30 use std::io::prelude::*;
31 use std::io::Cursor;
32 use std::rc::Rc;
33 use std::u32;
34 use syntax::ast::{self, CRATE_NODE_ID};
35 use syntax::codemap::Spanned;
36 use syntax::attr;
37 use syntax::symbol::Symbol;
38 use syntax_pos::{self, DUMMY_SP};
39
40 use rustc::hir::{self, PatKind};
41 use rustc::hir::itemlikevisit::ItemLikeVisitor;
42 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
43 use rustc::hir::intravisit;
44
45 use super::index_builder::{FromId, IndexBuilder, Untracked};
46
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,
53
54     lazy_state: LazyState,
55     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
56     predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
57 }
58
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)
63         })*
64     }
65 }
66
67 impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
68     type Error = <opaque::Encoder<'a> as Encoder>::Error;
69
70     fn emit_nil(&mut self) -> Result<(), Self::Error> {
71         Ok(())
72     }
73
74     encoder_methods! {
75         emit_usize(usize);
76         emit_u128(u128);
77         emit_u64(u64);
78         emit_u32(u32);
79         emit_u16(u16);
80         emit_u8(u8);
81
82         emit_isize(isize);
83         emit_i128(i128);
84         emit_i64(i64);
85         emit_i32(i32);
86         emit_i16(i16);
87         emit_i8(i8);
88
89         emit_bool(bool);
90         emit_f64(f64);
91         emit_f32(f32);
92         emit_char(char);
93         emit_str(&str);
94     }
95 }
96
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())
100     }
101 }
102
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)?;
106         if seq.len == 0 {
107             return Ok(());
108         }
109         self.emit_lazy_distance(seq.position, LazySeq::<T>::min_size(seq.len))
110     }
111 }
112
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)
116     }
117 }
118
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)?
127         }
128         Ok(())
129     }
130 }
131
132 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
133     pub fn position(&self) -> usize {
134         self.opaque.position()
135     }
136
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;
143         r
144     }
145
146     fn emit_lazy_distance(&mut self,
147                           position: usize,
148                           min_size: usize)
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);
155                 start - min_end
156             }
157             LazyState::Previous(last_min_end) => {
158                 assert!(last_min_end <= position);
159                 position - last_min_end
160             }
161         };
162         self.lazy_state = LazyState::Previous(min_end);
163         self.emit_usize(distance)
164     }
165
166     pub fn lazy<T: Encodable>(&mut self, value: &T) -> Lazy<T> {
167         self.emit_node(|ecx, pos| {
168             value.encode(ecx).unwrap();
169
170             assert!(pos + Lazy::<T>::min_size() <= ecx.position());
171             Lazy::with_position(pos)
172         })
173     }
174
175     fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T>
176         where I: IntoIterator<Item = T>,
177               T: Encodable
178     {
179         self.emit_node(|ecx, pos| {
180             let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
181
182             assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
183             LazySeq::with_position_and_length(pos, len)
184         })
185     }
186
187     fn lazy_seq_ref<'b, I, T>(&mut self, iter: I) -> LazySeq<T>
188         where I: IntoIterator<Item = &'b T>,
189               T: 'b + Encodable
190     {
191         self.emit_node(|ecx, pos| {
192             let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
193
194             assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
195             LazySeq::with_position_and_length(pos, len)
196         })
197     }
198
199     /// Encode the given value or a previously cached shorthand.
200     fn encode_with_shorthand<T, U, M>(&mut self,
201                                       value: &T,
202                                       variant: &U,
203                                       map: M)
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,
207               U: Encodable
208     {
209         let existing_shorthand = map(self).get(value).cloned();
210         if let Some(shorthand) = existing_shorthand {
211             return self.emit_usize(shorthand);
212         }
213
214         let start = self.position();
215         variant.encode(self)?;
216         let len = self.position() - start;
217
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;
223
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;
227
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);
232         }
233
234         Ok(())
235     }
236
237     fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
238         let tcx = self.tcx;
239         self.lazy_seq(tcx.item_variances(def_id).iter().cloned())
240     }
241
242     fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
243         let tcx = self.tcx;
244         self.lazy(&tcx.item_type(def_id))
245     }
246
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>))
254                                 -> Entry<'tcx> {
255         let tcx = self.tcx;
256         let def = tcx.lookup_adt_def(enum_did);
257         let variant = &def.variants[index];
258         let def_id = variant.did;
259
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()
266                 }
267                 ty::VariantDiscr::Relative(_) => None
268             },
269             struct_ctor: None,
270         };
271
272         let enum_id = tcx.hir.as_local_node_id(enum_did).unwrap();
273         let enum_vis = &tcx.hir.expect_item(enum_id).vis;
274
275         Entry {
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());
282                 f.did.index
283             })),
284             stability: self.encode_stability(def_id),
285             deprecation: self.encode_deprecation(def_id),
286
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)),
292
293             ast: None,
294             mir: self.encode_mir(def_id),
295         }
296     }
297
298     fn encode_info_for_mod(&mut self,
299                            FromId(id, (md, attrs, vis)): FromId<(&hir::Mod,
300                                                                  &[ast::Attribute],
301                                                                  &hir::Visibility)>)
302                            -> Entry<'tcx> {
303         let tcx = self.tcx;
304         let def_id = tcx.hir.local_def_id(id);
305
306         let data = ModData {
307             reexports: match tcx.export_map.get(&id) {
308                 Some(exports) if *vis == hir::Public => self.lazy_seq_ref(exports),
309                 _ => LazySeq::empty(),
310             },
311         };
312
313         Entry {
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
320             })),
321             stability: self.encode_stability(def_id),
322             deprecation: self.encode_deprecation(def_id),
323
324             ty: None,
325             inherent_impls: LazySeq::empty(),
326             variances: LazySeq::empty(),
327             generics: None,
328             predicates: None,
329
330             ast: None,
331             mir: None
332         }
333     }
334 }
335
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))));
344             }
345         }
346     }
347 }
348
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
355     /// vectors).
356     fn encode_field(&mut self,
357                     (adt_def_id, Untracked((variant_index, field_index))): (DefId,
358                                                                             Untracked<(usize,
359                                                                                        usize)>))
360                     -> Entry<'tcx> {
361         let tcx = self.tcx;
362         let variant = &tcx.lookup_adt_def(adt_def_id).variants[variant_index];
363         let field = &variant.fields[field_index];
364
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);
368
369         Entry {
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),
377
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)),
383
384             ast: None,
385             mir: None,
386         }
387     }
388
389     fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<'tcx> {
390         let tcx = self.tcx;
391         let variant = tcx.lookup_adt_def(adt_def_id).struct_variant();
392
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),
398         };
399
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;
406             }
407         }
408
409         let repr_options = get_repr_options(&tcx, adt_def_id);
410
411         Entry {
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),
419
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)),
425
426             ast: None,
427             mir: self.encode_mir(def_id),
428         }
429     }
430
431     fn encode_generics(&mut self, def_id: DefId) -> Lazy<ty::Generics> {
432         let tcx = self.tcx;
433         self.lazy(tcx.item_generics(def_id))
434     }
435
436     fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
437         let tcx = self.tcx;
438         self.lazy(&tcx.item_predicates(def_id))
439     }
440
441     fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
442         let tcx = self.tcx;
443
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);
447
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"),
455         };
456
457         let kind = match trait_item.kind {
458             ty::AssociatedKind::Const => {
459                 EntryKind::AssociatedConst(container, 0)
460             }
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)
466                         }
467                         hir::TraitMethod::Provided(body) => {
468                             self.encode_fn_arg_names_for_body(body)
469                         }
470                     };
471                     FnData {
472                         constness: hir::Constness::NotConst,
473                         arg_names: arg_names
474                     }
475                 } else {
476                     bug!()
477                 };
478                 EntryKind::Method(self.lazy(&MethodData {
479                     fn_data: fn_data,
480                     container: container,
481                     has_self: trait_item.method_has_self_argument,
482                 }))
483             }
484             ty::AssociatedKind::Type => EntryKind::AssociatedType(container),
485         };
486
487         Entry {
488             kind: kind,
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),
495
496             ty: match trait_item.kind {
497                 ty::AssociatedKind::Const |
498                 ty::AssociatedKind::Method => {
499                     Some(self.encode_item_type(def_id))
500                 }
501                 ty::AssociatedKind::Type => {
502                     if trait_item.defaultness.has_value() {
503                         Some(self.encode_item_type(def_id))
504                     } else {
505                         None
506                     }
507                 }
508             },
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)),
513
514             ast: if let hir::TraitItemKind::Const(_, Some(body)) = ast_item.node {
515                 Some(self.encode_body(body))
516             } else {
517                 None
518             },
519             mir: self.encode_mir(def_id),
520         }
521     }
522
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);
527
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)"),
533         };
534
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))
539             }
540             ty::AssociatedKind::Method => {
541                 let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
542                     FnData {
543                         constness: sig.constness,
544                         arg_names: self.encode_fn_arg_names_for_body(body),
545                     }
546                 } else {
547                     bug!()
548                 };
549                 EntryKind::Method(self.lazy(&MethodData {
550                     fn_data: fn_data,
551                     container: container,
552                     has_self: impl_item.method_has_self_argument,
553                 }))
554             }
555             ty::AssociatedKind::Type => EntryKind::AssociatedType(container)
556         };
557
558         let (ast, mir) = if let hir::ImplItemKind::Const(_, body) = ast_item.node {
559             (Some(body), true)
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)
568         } else {
569             (None, false)
570         };
571
572         Entry {
573             kind: kind,
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),
580
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)),
586
587             ast: ast.map(|body| self.encode_body(body)),
588             mir: if mir { self.encode_mir(def_id) } else { None },
589         }
590     }
591
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| {
597             match arg.pat.node {
598                 PatKind::Binding(_, _, name, _) => name.node,
599                 _ => Symbol::intern("")
600             }
601         }))
602     }
603
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))
607     }
608
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()))
611     }
612
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());
620                     def_id.index
621                 }))
622             }
623         }
624     }
625
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))
628     }
629
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))
632     }
633
634     fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) -> Entry<'tcx> {
635         let tcx = self.tcx;
636
637         debug!("encoding info for item at {}",
638                tcx.sess.codemap().span_to_string(item.span));
639
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))
645             }
646             hir::ItemFn(_, _, constness, .., body) => {
647                 let data = FnData {
648                     constness: constness,
649                     arg_names: self.encode_fn_arg_names_for_body(body),
650                 };
651
652                 EntryKind::Fn(self.lazy(&data))
653             }
654             hir::ItemMod(ref m) => {
655                 return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
656             }
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();
662
663                 // Encode def_ids for each field and method
664                 // for methods, write all the stuff get_trait_method
665                 // needs to know
666                 let struct_ctor = if !struct_def.is_struct() {
667                     Some(tcx.hir.local_def_id(struct_def.id()).index)
668                 } else {
669                     None
670                 };
671
672                 let repr_options = get_repr_options(&tcx, def_id);
673
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,
679                 }), repr_options)
680             }
681             hir::ItemUnion(..) => {
682                 let variant = tcx.lookup_adt_def(def_id).struct_variant();
683                 let repr_options = get_repr_options(&tcx, def_id);
684
685                 EntryKind::Union(self.lazy(&VariantData {
686                     ctor_kind: variant.ctor_kind,
687                     discr: variant.discr,
688                     evaluated_discr: None,
689                     struct_ctor: None,
690                 }), repr_options)
691             }
692             hir::ItemDefaultImpl(..) => {
693                 let data = ImplData {
694                     polarity: hir::ImplPolarity::Positive,
695                     parent_impl: None,
696                     coerce_unsized_info: None,
697                     trait_ref: tcx.impl_trait_ref(def_id).map(|trait_ref| self.lazy(&trait_ref)),
698                 };
699
700                 EntryKind::DefaultImpl(self.lazy(&data))
701             }
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| {
707                         match node {
708                             specialization_graph::Node::Impl(parent) => Some(parent),
709                             _ => None,
710                         }
711                     })
712                 } else {
713                     None
714                 };
715
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))
722                         } else {
723                             None
724                         }
725                     });
726
727                 let data = ImplData {
728                     polarity: polarity,
729                     parent_impl: parent,
730                     coerce_unsized_info: coerce_unsized_info,
731                     trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)),
732                 };
733
734                 EntryKind::Impl(self.lazy(&data))
735             }
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)),
743                 };
744
745                 EntryKind::Trait(self.lazy(&data))
746             }
747             hir::ItemExternCrate(_) |
748             hir::ItemUse(..) => bug!("cannot encode info for item {:?}", item),
749         };
750
751         Entry {
752             kind: kind,
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
759                         .iter()
760                         .map(|foreign_item| tcx.hir.local_def_id(foreign_item.id).index))
761                 }
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());
766                         v.did.index
767                     }))
768                 }
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());
774                         f.did.index
775                     }))
776                 }
777                 hir::ItemImpl(..) |
778                 hir::ItemTrait(..) => {
779                     self.lazy_seq(tcx.associated_item_def_ids(def_id).iter().map(|&def_id| {
780                         assert!(def_id.is_local());
781                         def_id.index
782                     }))
783                 }
784                 _ => LazySeq::empty(),
785             },
786             stability: self.encode_stability(def_id),
787             deprecation: self.encode_deprecation(def_id),
788
789             ty: match item.node {
790                 hir::ItemStatic(..) |
791                 hir::ItemConst(..) |
792                 hir::ItemFn(..) |
793                 hir::ItemTy(..) |
794                 hir::ItemEnum(..) |
795                 hir::ItemStruct(..) |
796                 hir::ItemUnion(..) |
797                 hir::ItemImpl(..) => Some(self.encode_item_type(def_id)),
798                 _ => None,
799             },
800             inherent_impls: self.encode_inherent_implementations(def_id),
801             variances: match item.node {
802                 hir::ItemEnum(..) |
803                 hir::ItemStruct(..) |
804                 hir::ItemUnion(..) |
805                 hir::ItemTrait(..) => self.encode_item_variances(def_id),
806                 _ => LazySeq::empty(),
807             },
808             generics: match item.node {
809                 hir::ItemStatic(..) |
810                 hir::ItemConst(..) |
811                 hir::ItemFn(..) |
812                 hir::ItemTy(..) |
813                 hir::ItemEnum(..) |
814                 hir::ItemStruct(..) |
815                 hir::ItemUnion(..) |
816                 hir::ItemImpl(..) |
817                 hir::ItemTrait(..) => Some(self.encode_generics(def_id)),
818                 _ => None,
819             },
820             predicates: match item.node {
821                 hir::ItemStatic(..) |
822                 hir::ItemConst(..) |
823                 hir::ItemFn(..) |
824                 hir::ItemTy(..) |
825                 hir::ItemEnum(..) |
826                 hir::ItemStruct(..) |
827                 hir::ItemUnion(..) |
828                 hir::ItemImpl(..) |
829                 hir::ItemTrait(..) => Some(self.encode_predicates(def_id)),
830                 _ => None,
831             },
832
833             ast: match item.node {
834                 hir::ItemConst(_, body) |
835                 hir::ItemFn(_, _, hir::Constness::Const, _, _, body) => {
836                     Some(self.encode_body(body))
837                 }
838                 _ => None,
839             },
840             mir: match item.node {
841                 hir::ItemStatic(..) if self.tcx.sess.opts.debugging_opts.always_encode_mir => {
842                     self.encode_mir(def_id)
843                 }
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)
851                     } else {
852                         None
853                     }
854                 }
855                 _ => None,
856             },
857         }
858     }
859
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;
863         Entry {
864             kind: EntryKind::MacroDef(self.lazy(&MacroDef {
865                 body: pprust::tts_to_string(&macro_def.body.trees().collect::<Vec<_>>()),
866             })),
867             visibility: self.lazy(&ty::Visibility::Public),
868             span: self.lazy(&macro_def.span),
869
870             attributes: self.encode_attributes(&macro_def.attrs),
871             children: LazySeq::empty(),
872             stability: None,
873             deprecation: None,
874             ty: None,
875             inherent_impls: LazySeq::empty(),
876             variances: LazySeq::empty(),
877             generics: None,
878             predicates: None,
879             ast: None,
880             mir: None,
881         }
882     }
883 }
884
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);
892         match item.node {
893             hir::ItemStatic(..) |
894             hir::ItemConst(..) |
895             hir::ItemFn(..) |
896             hir::ItemMod(..) |
897             hir::ItemForeignMod(..) |
898             hir::ItemExternCrate(..) |
899             hir::ItemUse(..) |
900             hir::ItemDefaultImpl(..) |
901             hir::ItemTy(..) => {
902                 // no sub-item recording needed in these cases
903             }
904             hir::ItemEnum(..) => {
905                 self.encode_fields(def_id);
906
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)));
912                 }
913             }
914             hir::ItemStruct(ref struct_def, _) => {
915                 self.encode_fields(def_id);
916
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));
923                 }
924             }
925             hir::ItemUnion(..) => {
926                 self.encode_fields(def_id);
927             }
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,
932                                 trait_item_def_id);
933                 }
934             }
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,
939                                 item_def_id);
940                 }
941             }
942         }
943     }
944 }
945
946 impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
947     fn encode_info_for_foreign_item(&mut self,
948                                     (def_id, nitem): (DefId, &hir::ForeignItem))
949                                     -> Entry<'tcx> {
950         let tcx = self.tcx;
951
952         debug!("writing foreign item {}", tcx.node_path_str(nitem.id));
953
954         let kind = match nitem.node {
955             hir::ForeignItemFn(_, ref names, _) => {
956                 let data = FnData {
957                     constness: hir::Constness::NotConst,
958                     arg_names: self.encode_fn_arg_names(names),
959                 };
960                 EntryKind::ForeignFn(self.lazy(&data))
961             }
962             hir::ForeignItemStatic(_, true) => EntryKind::ForeignMutStatic,
963             hir::ForeignItemStatic(_, false) => EntryKind::ForeignImmStatic,
964         };
965
966         Entry {
967             kind: kind,
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),
974
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)),
980
981             ast: None,
982             mir: None,
983         }
984     }
985 }
986
987 struct EncodeVisitor<'a, 'b: 'a, 'tcx: 'b> {
988     index: IndexBuilder<'a, 'b, 'tcx>,
989 }
990
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)
994     }
995     fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
996         intravisit::walk_expr(self, ex);
997         self.index.encode_info_for_expr(ex);
998     }
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);
1002         match item.node {
1003             hir::ItemExternCrate(_) |
1004             hir::ItemUse(..) => (), // ignore these
1005             _ => self.index.record(def_id, EncodeContext::encode_info_for_item, (def_id, item)),
1006         }
1007         self.index.encode_addl_info_for_item(item);
1008     }
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,
1014                           (def_id, ni));
1015     }
1016     fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
1017         intravisit::walk_generics(self, generics);
1018         self.index.encode_info_for_generics(generics);
1019     }
1020     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
1021         intravisit::walk_ty(self, ty);
1022         self.index.encode_info_for_ty(ty);
1023     }
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);
1027     }
1028 }
1029
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));
1036         }
1037     }
1038
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);
1043         }
1044     }
1045
1046     fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
1047         match expr.node {
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);
1051             }
1052             _ => {}
1053         }
1054     }
1055 }
1056
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>))
1060                                 -> Entry<'tcx> {
1061         let tcx = self.tcx;
1062         Entry {
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(),
1068             stability: None,
1069             deprecation: None,
1070
1071             ty: if has_default {
1072                 Some(self.encode_item_type(def_id))
1073             } else {
1074                 None
1075             },
1076             inherent_impls: LazySeq::empty(),
1077             variances: LazySeq::empty(),
1078             generics: None,
1079             predicates: None,
1080
1081             ast: None,
1082             mir: None,
1083         }
1084     }
1085
1086     fn encode_info_for_anon_ty(&mut self, def_id: DefId) -> Entry<'tcx> {
1087         let tcx = self.tcx;
1088         Entry {
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(),
1094             stability: None,
1095             deprecation: None,
1096
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)),
1102
1103             ast: None,
1104             mir: None,
1105         }
1106     }
1107
1108     fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
1109         let tcx = self.tcx;
1110
1111         let data = ClosureData {
1112             kind: tcx.closure_kind(def_id),
1113             ty: self.lazy(&tcx.closure_type(def_id)),
1114         };
1115
1116         Entry {
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(),
1122             stability: None,
1123             deprecation: None,
1124
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)),
1129             predicates: None,
1130
1131             ast: None,
1132             mir: self.encode_mir(def_id),
1133         }
1134     }
1135
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);
1146         }
1147         visitor.index.into_items()
1148     }
1149
1150     fn encode_attributes(&mut self, attrs: &[ast::Attribute]) -> LazySeq<ast::Attribute> {
1151         self.lazy_seq_ref(attrs)
1152     }
1153
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()));
1160             });
1161
1162             // Sort by cnum
1163             deps.sort_by(|kv1, kv2| kv1.0.cmp(&kv2.0));
1164
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));
1169                 expected_cnum += 1;
1170             }
1171
1172             deps
1173         }
1174
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)| {
1181             CrateDep {
1182                 name: dep.name(),
1183                 hash: dep.hash(),
1184                 kind: dep.dep_kind.get(),
1185             }
1186         }))
1187     }
1188
1189     fn encode_lang_items(&mut self) -> (LazySeq<(DefIndex, usize)>, LazySeq<lang_items::LangItem>) {
1190         let tcx = self.tcx;
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));
1196                 }
1197             }
1198             None
1199         })),
1200          self.lazy_seq_ref(&tcx.lang_items.missing))
1201     }
1202
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)
1206     }
1207
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()
1212             .filter(|filemap| {
1213                 // No need to re-export imported filemaps, as any downstream
1214                 // crate will import them from their original source.
1215                 !filemap.is_imported()
1216             })
1217             .map(|filemap| &**filemap))
1218     }
1219
1220     fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
1221         let definitions = self.tcx.hir.definitions();
1222         self.lazy(definitions.def_path_table())
1223     }
1224 }
1225
1226 struct ImplVisitor<'a, 'tcx: 'a> {
1227     tcx: TyCtxt<'a, 'tcx, 'tcx>,
1228     impls: FxHashMap<DefId, Vec<DefIndex>>,
1229 }
1230
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) {
1236                 self.impls
1237                     .entry(trait_ref.def_id)
1238                     .or_insert(vec![])
1239                     .push(impl_id.index);
1240             }
1241         }
1242     }
1243
1244     fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem) {}
1245
1246     fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) {
1247         // handled in `visit_item` above
1248     }
1249 }
1250
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 {
1255             tcx: self.tcx,
1256             impls: FxHashMap(),
1257         };
1258         self.tcx.hir.krate().visit_all_item_likes(&mut visitor);
1259
1260         let all_impls: Vec<_> = visitor.impls
1261             .into_iter()
1262             .map(|(trait_def_id, impls)| {
1263                 TraitImpls {
1264                     trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
1265                     impls: self.lazy_seq(impls),
1266                 }
1267             })
1268             .collect();
1269
1270         self.lazy_seq(all_impls)
1271     }
1272
1273     // Encodes all symbols exported from this crate into the metadata.
1274     //
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;
1281         let tcx = self.tcx;
1282         self.lazy_seq(exported_symbols.iter().map(|&id| tcx.hir.local_def_id(id).index))
1283     }
1284
1285     fn encode_dylib_dependency_formats(&mut self) -> LazySeq<Option<LinkagePreference>> {
1286         match self.tcx.sess.dependency_formats.borrow().get(&config::CrateTypeDylib) {
1287             Some(arr) => {
1288                 self.lazy_seq(arr.iter().map(|slot| {
1289                     match *slot {
1290                         Linkage::NotLinked |
1291                         Linkage::IncludedFromDylib => None,
1292
1293                         Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
1294                         Linkage::Static => Some(LinkagePreference::RequireStatic),
1295                     }
1296                 }))
1297             }
1298             None => LazySeq::empty(),
1299         }
1300     }
1301
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;
1307
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;
1312
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;
1317
1318         // Encode codemap
1319         i = self.position();
1320         let codemap = self.encode_codemap();
1321         let codemap_bytes = self.position() - i;
1322
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;
1327
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;
1332
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;
1337
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;
1342
1343         i = self.position();
1344         let index = items.write_index(&mut self.opaque.cursor);
1345         let index_bytes = self.position() - i;
1346
1347         let tcx = self.tcx;
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
1358                 .get()
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)
1363             } else {
1364                 None
1365             },
1366
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,
1372             codemap: codemap,
1373             def_path_table: def_path_table,
1374             impls: impls,
1375             exported_symbols: exported_symbols,
1376             index: index,
1377         });
1378
1379         let total_bytes = self.position();
1380
1381         if self.tcx.sess.meta_stats() {
1382             let mut zero_bytes = 0;
1383             for e in self.opaque.cursor.get_ref() {
1384                 if *e == 0 {
1385                     zero_bytes += 1;
1386                 }
1387             }
1388
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);
1401         }
1402
1403         root
1404     }
1405 }
1406
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.
1410 //
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
1415 // metadata.
1416 //
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.
1423 //
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.
1429
1430 pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1431                                  cstore: &cstore::CStore,
1432                                  link_meta: &LinkMeta,
1433                                  exported_symbols: &NodeSet)
1434                                  -> Vec<u8> {
1435     let mut cursor = Cursor::new(vec![]);
1436     cursor.write_all(METADATA_HEADER).unwrap();
1437
1438     // Will be filed with the root position after encoding everything.
1439     cursor.write_all(&[0, 0, 0, 0]).unwrap();
1440
1441     let root = {
1442         let mut ecx = EncodeContext {
1443             opaque: opaque::Encoder::new(&mut cursor),
1444             tcx: tcx,
1445             link_meta: link_meta,
1446             cstore: cstore,
1447             exported_symbols: exported_symbols,
1448             lazy_state: LazyState::NoNode,
1449             type_shorthands: Default::default(),
1450             predicate_shorthands: Default::default(),
1451         };
1452
1453         // Encode the rustc version string in a predictable location.
1454         rustc_version().encode(&mut ecx).unwrap();
1455
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()
1459     };
1460     let mut result = cursor.into_inner();
1461
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;
1469
1470     result
1471 }
1472
1473 pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions {
1474     let ty = tcx.item_type(did);
1475     match ty.sty {
1476         ty::TyAdt(ref def, _) => return def.repr,
1477         _ => bug!("{} is not an ADT", ty),
1478     }
1479 }