1 // Copyright 2012-2016 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.
14 use rustc::hir::def::{self, CtorKind};
15 use rustc::hir::def_id::{DefIndex, DefId, CrateNum};
16 use rustc::ich::StableHashingContext;
17 use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignModule};
18 use rustc::middle::lang_items;
20 use rustc::session::CrateDisambiguator;
21 use rustc::ty::{self, Ty, ReprOptions};
22 use rustc_target::spec::{PanicStrategy, TargetTriple};
24 use rustc_serialize as serialize;
25 use syntax::{ast, attr};
26 use syntax::edition::Edition;
27 use syntax::symbol::Symbol;
28 use syntax_pos::{self, Span};
30 use std::marker::PhantomData;
33 use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
36 pub fn rustc_version() -> String {
38 option_env!("CFG_VERSION").unwrap_or("unknown version"))
41 /// Metadata encoding version.
42 /// NB: increment this if you change the format of metadata such that
43 /// the rustc version can't be found to compare with `rustc_version()`.
44 pub const METADATA_VERSION: u8 = 4;
46 /// Metadata header which includes `METADATA_VERSION`.
47 /// To get older versions of rustc to ignore this metadata,
48 /// there are 4 zero bytes at the start, which are treated
49 /// as a length of 0 by old compilers.
51 /// This header is followed by the position of the `CrateRoot`,
52 /// which is encoded as a 32-bit big-endian unsigned integer,
53 /// and further followed by the rustc version string.
54 pub const METADATA_HEADER: &'static [u8; 12] =
55 &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
57 /// A value of type T referred to by its absolute position
58 /// in the metadata, and which can be decoded lazily.
60 /// Metadata is effective a tree, encoded in post-order,
61 /// and with the root's position written next to the header.
62 /// That means every single `Lazy` points to some previous
63 /// location in the metadata and is part of a larger node.
65 /// The first `Lazy` in a node is encoded as the backwards
66 /// distance from the position where the containing node
67 /// starts and where the `Lazy` points to, while the rest
68 /// use the forward distance from the previous `Lazy`.
69 /// Distances start at 1, as 0-byte nodes are invalid.
70 /// Also invalid are nodes being referred in a different
71 /// order than they were encoded in.
75 _marker: PhantomData<T>,
79 pub fn with_position(position: usize) -> Lazy<T> {
86 /// Returns the minimum encoded size of a value of type `T`.
87 // FIXME(eddyb) Give better estimates for certain types.
88 pub fn min_size() -> usize {
93 impl<T> Copy for Lazy<T> {}
94 impl<T> Clone for Lazy<T> {
95 fn clone(&self) -> Self {
100 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
101 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
103 impl<CTX, T> HashStable<CTX> for Lazy<T> {
104 fn hash_stable<W: StableHasherResult>(&self,
106 _: &mut StableHasher<W>) {
107 // There's nothing to do. Whatever got encoded within this Lazy<>
108 // wrapper has already been hashed.
112 /// A sequence of type T referred to by its absolute position
113 /// in the metadata and length, and which can be decoded lazily.
114 /// The sequence is a single node for the purposes of `Lazy`.
116 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
117 /// position, not at the position, which means that the length
118 /// doesn't need to be known before encoding all the elements.
120 /// If the length is 0, no position is encoded, but otherwise,
121 /// the encoding is that of `Lazy`, with the distinction that
122 /// the minimal distance the length of the sequence, i.e.
123 /// it's assumed there's no 0-byte element in the sequence.
125 pub struct LazySeq<T> {
128 _marker: PhantomData<T>,
132 pub fn empty() -> LazySeq<T> {
133 LazySeq::with_position_and_length(0, 0)
136 pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
140 _marker: PhantomData,
144 /// Returns the minimum encoded size of `length` values of type `T`.
145 pub fn min_size(length: usize) -> usize {
150 impl<T> Copy for LazySeq<T> {}
151 impl<T> Clone for LazySeq<T> {
152 fn clone(&self) -> Self {
157 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
158 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
160 impl<CTX, T> HashStable<CTX> for LazySeq<T> {
161 fn hash_stable<W: StableHasherResult>(&self,
163 _: &mut StableHasher<W>) {
164 // There's nothing to do. Whatever got encoded within this Lazy<>
165 // wrapper has already been hashed.
169 /// Encoding / decoding state for `Lazy` and `LazySeq`.
170 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
172 /// Outside of a metadata node.
175 /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
176 /// The position is that of the node itself.
179 /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
180 /// The position is a conservative estimate of where that
181 /// previous `Lazy` / `LazySeq` would end (see their comments).
185 #[derive(RustcEncodable, RustcDecodable)]
186 pub struct CrateRoot {
188 pub triple: TargetTriple,
189 pub extra_filename: String,
190 pub hash: hir::svh::Svh,
191 pub disambiguator: CrateDisambiguator,
192 pub panic_strategy: PanicStrategy,
193 pub edition: Edition,
194 pub has_global_allocator: bool,
195 pub has_default_lib_allocator: bool,
196 pub plugin_registrar_fn: Option<DefIndex>,
197 pub macro_derive_registrar: Option<DefIndex>,
199 pub crate_deps: LazySeq<CrateDep>,
200 pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
201 pub lang_items: LazySeq<(DefIndex, usize)>,
202 pub lang_items_missing: LazySeq<lang_items::LangItem>,
203 pub native_libraries: LazySeq<NativeLibrary>,
204 pub foreign_modules: LazySeq<ForeignModule>,
205 pub codemap: LazySeq<syntax_pos::FileMap>,
206 pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
207 pub impls: LazySeq<TraitImpls>,
208 pub exported_symbols: EncodedExportedSymbols,
209 pub wasm_custom_sections: LazySeq<DefIndex>,
210 pub interpret_alloc_index: LazySeq<u32>,
212 pub index: LazySeq<index::Index>,
214 pub compiler_builtins: bool,
215 pub needs_allocator: bool,
216 pub needs_panic_runtime: bool,
217 pub no_builtins: bool,
218 pub panic_runtime: bool,
219 pub profiler_runtime: bool,
220 pub sanitizer_runtime: bool,
223 #[derive(RustcEncodable, RustcDecodable)]
224 pub struct CrateDep {
226 pub hash: hir::svh::Svh,
228 pub extra_filename: String,
231 impl_stable_hash_for!(struct CrateDep {
238 #[derive(RustcEncodable, RustcDecodable)]
239 pub struct TraitImpls {
240 pub trait_id: (u32, DefIndex),
241 pub impls: LazySeq<DefIndex>,
244 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TraitImpls {
245 fn hash_stable<W: StableHasherResult>(&self,
246 hcx: &mut StableHashingContext<'a>,
247 hasher: &mut StableHasher<W>) {
249 trait_id: (krate, def_index),
254 krate: CrateNum::from_u32(krate),
256 }.hash_stable(hcx, hasher);
257 impls.hash_stable(hcx, hasher);
261 #[derive(RustcEncodable, RustcDecodable)]
262 pub struct Entry<'tcx> {
263 pub kind: EntryKind<'tcx>,
264 pub visibility: Lazy<ty::Visibility>,
265 pub span: Lazy<Span>,
266 pub attributes: LazySeq<ast::Attribute>,
267 pub children: LazySeq<DefIndex>,
268 pub stability: Option<Lazy<attr::Stability>>,
269 pub deprecation: Option<Lazy<attr::Deprecation>>,
271 pub ty: Option<Lazy<Ty<'tcx>>>,
272 pub inherent_impls: LazySeq<DefIndex>,
273 pub variances: LazySeq<ty::Variance>,
274 pub generics: Option<Lazy<ty::Generics>>,
275 pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
277 pub mir: Option<Lazy<mir::Mir<'tcx>>>,
280 impl_stable_hash_for!(struct Entry<'tcx> {
296 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
297 pub enum EntryKind<'tcx> {
298 Const(ConstQualif, Lazy<RenderedConst>),
310 Variant(Lazy<VariantData<'tcx>>),
311 Struct(Lazy<VariantData<'tcx>>, ReprOptions),
312 Union(Lazy<VariantData<'tcx>>, ReprOptions),
313 Fn(Lazy<FnData<'tcx>>),
314 ForeignFn(Lazy<FnData<'tcx>>),
316 MacroDef(Lazy<MacroDef>),
317 Closure(Lazy<ClosureData<'tcx>>),
318 Generator(Lazy<GeneratorData<'tcx>>),
319 Trait(Lazy<TraitData<'tcx>>),
320 Impl(Lazy<ImplData<'tcx>>),
321 Method(Lazy<MethodData<'tcx>>),
322 AssociatedType(AssociatedContainer),
323 AssociatedConst(AssociatedContainer, ConstQualif, Lazy<RenderedConst>),
326 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for EntryKind<'gcx> {
327 fn hash_stable<W: StableHasherResult>(&self,
328 hcx: &mut StableHashingContext<'a>,
329 hasher: &mut StableHasher<W>) {
330 mem::discriminant(self).hash_stable(hcx, hasher);
332 EntryKind::ImmStatic |
333 EntryKind::MutStatic |
334 EntryKind::ForeignImmStatic |
335 EntryKind::ForeignMutStatic |
336 EntryKind::ForeignMod |
337 EntryKind::GlobalAsm |
338 EntryKind::ForeignType |
340 EntryKind::Existential |
342 // Nothing else to hash here.
344 EntryKind::Const(qualif, ref const_data) => {
345 qualif.hash_stable(hcx, hasher);
346 const_data.hash_stable(hcx, hasher);
348 EntryKind::Enum(ref repr_options) => {
349 repr_options.hash_stable(hcx, hasher);
351 EntryKind::Variant(ref variant_data) => {
352 variant_data.hash_stable(hcx, hasher);
354 EntryKind::Struct(ref variant_data, ref repr_options) |
355 EntryKind::Union(ref variant_data, ref repr_options) => {
356 variant_data.hash_stable(hcx, hasher);
357 repr_options.hash_stable(hcx, hasher);
359 EntryKind::Fn(ref fn_data) |
360 EntryKind::ForeignFn(ref fn_data) => {
361 fn_data.hash_stable(hcx, hasher);
363 EntryKind::Mod(ref mod_data) => {
364 mod_data.hash_stable(hcx, hasher);
366 EntryKind::MacroDef(ref macro_def) => {
367 macro_def.hash_stable(hcx, hasher);
369 EntryKind::Generator(data) => {
370 data.hash_stable(hcx, hasher);
372 EntryKind::Closure(closure_data) => {
373 closure_data.hash_stable(hcx, hasher);
375 EntryKind::Trait(ref trait_data) => {
376 trait_data.hash_stable(hcx, hasher);
378 EntryKind::Impl(ref impl_data) => {
379 impl_data.hash_stable(hcx, hasher);
381 EntryKind::Method(ref method_data) => {
382 method_data.hash_stable(hcx, hasher);
384 EntryKind::AssociatedType(associated_container) => {
385 associated_container.hash_stable(hcx, hasher);
387 EntryKind::AssociatedConst(associated_container, qualif, ref const_data) => {
388 associated_container.hash_stable(hcx, hasher);
389 qualif.hash_stable(hcx, hasher);
390 const_data.hash_stable(hcx, hasher);
396 /// Additional data for EntryKind::Const and EntryKind::AssociatedConst
397 #[derive(Clone, Copy, RustcEncodable, RustcDecodable)]
398 pub struct ConstQualif {
400 pub ast_promotable: bool,
403 impl_stable_hash_for!(struct ConstQualif { mir, ast_promotable });
405 /// Contains a constant which has been rendered to a String.
407 #[derive(RustcEncodable, RustcDecodable)]
408 pub struct RenderedConst(pub String);
410 impl<'a> HashStable<StableHashingContext<'a>> for RenderedConst {
412 fn hash_stable<W: StableHasherResult>(&self,
413 hcx: &mut StableHashingContext<'a>,
414 hasher: &mut StableHasher<W>) {
415 self.0.hash_stable(hcx, hasher);
419 #[derive(RustcEncodable, RustcDecodable)]
421 pub reexports: LazySeq<def::Export>,
424 impl_stable_hash_for!(struct ModData { reexports });
426 #[derive(RustcEncodable, RustcDecodable)]
427 pub struct MacroDef {
432 impl_stable_hash_for!(struct MacroDef { body, legacy });
434 #[derive(RustcEncodable, RustcDecodable)]
435 pub struct FnData<'tcx> {
436 pub constness: hir::Constness,
437 pub arg_names: LazySeq<ast::Name>,
438 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
441 impl_stable_hash_for!(struct FnData<'tcx> { constness, arg_names, sig });
443 #[derive(RustcEncodable, RustcDecodable)]
444 pub struct VariantData<'tcx> {
445 pub ctor_kind: CtorKind,
446 pub discr: ty::VariantDiscr,
448 /// If this is a struct's only variant, this
449 /// is the index of the "struct ctor" item.
450 pub struct_ctor: Option<DefIndex>,
452 /// If this is a tuple struct or variant
453 /// ctor, this is its "function" signature.
454 pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
457 impl_stable_hash_for!(struct VariantData<'tcx> {
464 #[derive(RustcEncodable, RustcDecodable)]
465 pub struct TraitData<'tcx> {
466 pub unsafety: hir::Unsafety,
467 pub paren_sugar: bool,
468 pub has_auto_impl: bool,
469 pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
472 impl_stable_hash_for!(struct TraitData<'tcx> {
479 #[derive(RustcEncodable, RustcDecodable)]
480 pub struct ImplData<'tcx> {
481 pub polarity: hir::ImplPolarity,
482 pub defaultness: hir::Defaultness,
483 pub parent_impl: Option<DefId>,
485 /// This is `Some` only for impls of `CoerceUnsized`.
486 pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
487 pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
490 impl_stable_hash_for!(struct ImplData<'tcx> {
499 /// Describes whether the container of an associated item
500 /// is a trait or an impl and whether, in a trait, it has
501 /// a default, or an in impl, whether it's marked "default".
502 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
503 pub enum AssociatedContainer {
510 impl_stable_hash_for!(enum ::schema::AssociatedContainer {
517 impl AssociatedContainer {
518 pub fn with_def_id(&self, def_id: DefId) -> ty::AssociatedItemContainer {
520 AssociatedContainer::TraitRequired |
521 AssociatedContainer::TraitWithDefault => ty::TraitContainer(def_id),
523 AssociatedContainer::ImplDefault |
524 AssociatedContainer::ImplFinal => ty::ImplContainer(def_id),
528 pub fn defaultness(&self) -> hir::Defaultness {
530 AssociatedContainer::TraitRequired => hir::Defaultness::Default {
534 AssociatedContainer::TraitWithDefault |
535 AssociatedContainer::ImplDefault => hir::Defaultness::Default {
539 AssociatedContainer::ImplFinal => hir::Defaultness::Final,
544 #[derive(RustcEncodable, RustcDecodable)]
545 pub struct MethodData<'tcx> {
546 pub fn_data: FnData<'tcx>,
547 pub container: AssociatedContainer,
550 impl_stable_hash_for!(struct MethodData<'tcx> { fn_data, container, has_self });
552 #[derive(RustcEncodable, RustcDecodable)]
553 pub struct ClosureData<'tcx> {
554 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
556 impl_stable_hash_for!(struct ClosureData<'tcx> { sig });
558 #[derive(RustcEncodable, RustcDecodable)]
559 pub struct GeneratorData<'tcx> {
560 pub layout: mir::GeneratorLayout<'tcx>,
562 impl_stable_hash_for!(struct GeneratorData<'tcx> { layout });
564 // Tags used for encoding Spans:
565 pub const TAG_VALID_SPAN: u8 = 0;
566 pub const TAG_INVALID_SPAN: u8 = 1;
568 #[derive(RustcEncodable, RustcDecodable)]
569 pub struct EncodedExportedSymbols {