// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use astencode; use index; use rustc::hir; use rustc::hir::def::{self, CtorKind}; use rustc::hir::def_id::{DefIndex, DefId, CrateNum}; use rustc::ich::StableHashingContext; use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary}; use rustc::middle::lang_items; use rustc::mir; use rustc::ty::{self, Ty, ReprOptions}; use rustc_back::PanicStrategy; use rustc_serialize as serialize; use syntax::{ast, attr}; use syntax::symbol::Symbol; use syntax_pos::{self, Span}; use std::marker::PhantomData; use std::mem; use rustc_data_structures::stable_hasher::{StableHasher, HashStable, StableHasherResult}; use rustc::dep_graph::{DepGraph, DepNode}; pub fn rustc_version() -> String { format!("rustc {}", option_env!("CFG_VERSION").unwrap_or("unknown version")) } /// Metadata encoding version. /// NB: increment this if you change the format of metadata such that /// the rustc version can't be found to compare with `rustc_version()`. pub const METADATA_VERSION: u8 = 4; /// Metadata header which includes `METADATA_VERSION`. /// To get older versions of rustc to ignore this metadata, /// there are 4 zero bytes at the start, which are treated /// as a length of 0 by old compilers. /// /// This header is followed by the position of the `CrateRoot`, /// which is encoded as a 32-bit big-endian unsigned integer, /// and further followed by the rustc version string. pub const METADATA_HEADER: &'static [u8; 12] = &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION]; /// The shorthand encoding uses an enum's variant index `usize` /// and is offset by this value so it never matches a real variant. /// This offset is also chosen so that the first byte is never < 0x80. pub const SHORTHAND_OFFSET: usize = 0x80; /// A value of type T referred to by its absolute position /// in the metadata, and which can be decoded lazily. /// /// Metadata is effective a tree, encoded in post-order, /// and with the root's position written next to the header. /// That means every single `Lazy` points to some previous /// location in the metadata and is part of a larger node. /// /// The first `Lazy` in a node is encoded as the backwards /// distance from the position where the containing node /// starts and where the `Lazy` points to, while the rest /// use the forward distance from the previous `Lazy`. /// Distances start at 1, as 0-byte nodes are invalid. /// Also invalid are nodes being referred in a different /// order than they were encoded in. #[must_use] pub struct Lazy { pub position: usize, _marker: PhantomData, } impl Lazy { pub fn with_position(position: usize) -> Lazy { Lazy { position: position, _marker: PhantomData, } } /// Returns the minimum encoded size of a value of type `T`. // FIXME(eddyb) Give better estimates for certain types. pub fn min_size() -> usize { 1 } } impl Copy for Lazy {} impl Clone for Lazy { fn clone(&self) -> Self { *self } } impl serialize::UseSpecializedEncodable for Lazy {} impl serialize::UseSpecializedDecodable for Lazy {} impl HashStable for Lazy { fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) { // There's nothing to do. Whatever got encoded within this Lazy<> // wrapper has already been hashed. } } /// A sequence of type T referred to by its absolute position /// in the metadata and length, and which can be decoded lazily. /// The sequence is a single node for the purposes of `Lazy`. /// /// Unlike `Lazy>`, the length is encoded next to the /// position, not at the position, which means that the length /// doesn't need to be known before encoding all the elements. /// /// If the length is 0, no position is encoded, but otherwise, /// the encoding is that of `Lazy`, with the distinction that /// the minimal distance the length of the sequence, i.e. /// it's assumed there's no 0-byte element in the sequence. #[must_use] pub struct LazySeq { pub len: usize, pub position: usize, _marker: PhantomData, } impl LazySeq { pub fn empty() -> LazySeq { LazySeq::with_position_and_length(0, 0) } pub fn with_position_and_length(position: usize, len: usize) -> LazySeq { LazySeq { len: len, position: position, _marker: PhantomData, } } /// Returns the minimum encoded size of `length` values of type `T`. pub fn min_size(length: usize) -> usize { length } } impl Copy for LazySeq {} impl Clone for LazySeq { fn clone(&self) -> Self { *self } } impl serialize::UseSpecializedEncodable for LazySeq {} impl serialize::UseSpecializedDecodable for LazySeq {} impl HashStable for LazySeq { fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) { // There's nothing to do. Whatever got encoded within this Lazy<> // wrapper has already been hashed. } } /// Encoding / decoding state for `Lazy` and `LazySeq`. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum LazyState { /// Outside of a metadata node. NoNode, /// Inside a metadata node, and before any `Lazy` or `LazySeq`. /// The position is that of the node itself. NodeStart(usize), /// Inside a metadata node, with a previous `Lazy` or `LazySeq`. /// The position is a conservative estimate of where that /// previous `Lazy` / `LazySeq` would end (see their comments). Previous(usize), } /// A `Tracked` wraps a value so that one can only access it when specifying /// the `DepNode` for that value. This makes it harder to forget registering /// reads. #[derive(RustcEncodable, RustcDecodable)] pub struct Tracked { state: T, } impl Tracked { pub fn new(state: T) -> Tracked { Tracked { state: state, } } pub fn get(&self, dep_graph: &DepGraph, dep_node: DepNode) -> &T { dep_graph.read(dep_node); &self.state } pub fn get_untracked(&self) -> &T { &self.state } pub fn map(&self, f: F) -> Tracked where F: FnOnce(&T) -> R { Tracked { state: f(&self.state), } } } impl<'a, 'tcx, T> HashStable> for Tracked where T: HashStable> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a, 'tcx>, hasher: &mut StableHasher) { let Tracked { ref state } = *self; state.hash_stable(hcx, hasher); } } #[derive(RustcEncodable, RustcDecodable)] pub struct CrateRoot { pub name: Symbol, pub triple: String, pub hash: hir::svh::Svh, pub disambiguator: Symbol, pub panic_strategy: Tracked, pub plugin_registrar_fn: Option, pub macro_derive_registrar: Option, pub crate_deps: Tracked>, pub dylib_dependency_formats: Tracked>>, pub lang_items: Tracked>, pub lang_items_missing: Tracked>, pub native_libraries: Tracked>, pub codemap: LazySeq, pub def_path_table: Lazy, pub impls: Tracked>, pub exported_symbols: Tracked>, pub index: LazySeq, } #[derive(RustcEncodable, RustcDecodable)] pub struct CrateDep { pub name: ast::Name, pub hash: hir::svh::Svh, pub kind: DepKind, } impl_stable_hash_for!(struct CrateDep { name, hash, kind }); #[derive(RustcEncodable, RustcDecodable)] pub struct TraitImpls { pub trait_id: (u32, DefIndex), pub impls: LazySeq, } impl<'a, 'tcx> HashStable> for TraitImpls { fn hash_stable(&self, hcx: &mut StableHashingContext<'a, 'tcx>, hasher: &mut StableHasher) { let TraitImpls { trait_id: (krate, def_index), ref impls, } = *self; DefId { krate: CrateNum::from_u32(krate), index: def_index }.hash_stable(hcx, hasher); impls.hash_stable(hcx, hasher); } } #[derive(RustcEncodable, RustcDecodable)] pub struct Entry<'tcx> { pub kind: EntryKind<'tcx>, pub visibility: Lazy, pub span: Lazy, pub attributes: LazySeq, pub children: LazySeq, pub stability: Option>, pub deprecation: Option>, pub ty: Option>>, pub inherent_impls: LazySeq, pub variances: LazySeq, pub generics: Option>, pub predicates: Option>>, pub ast: Option>>, pub mir: Option>>, } impl_stable_hash_for!(struct Entry<'tcx> { kind, visibility, span, attributes, children, stability, deprecation, ty, inherent_impls, variances, generics, predicates, ast, mir }); #[derive(Copy, Clone, RustcEncodable, RustcDecodable)] pub enum EntryKind<'tcx> { Const(u8), ImmStatic, MutStatic, ForeignImmStatic, ForeignMutStatic, ForeignMod, GlobalAsm, Type, Enum(ReprOptions), Field, Variant(Lazy), Struct(Lazy, ReprOptions), Union(Lazy, ReprOptions), Fn(Lazy), ForeignFn(Lazy), Mod(Lazy), MacroDef(Lazy), Closure(Lazy>), Trait(Lazy>), Impl(Lazy>), DefaultImpl(Lazy>), Method(Lazy), AssociatedType(AssociatedContainer), AssociatedConst(AssociatedContainer, u8), } impl<'a, 'tcx> HashStable> for EntryKind<'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a, 'tcx>, hasher: &mut StableHasher) { mem::discriminant(self).hash_stable(hcx, hasher); match *self { EntryKind::ImmStatic | EntryKind::MutStatic | EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignMod | EntryKind::GlobalAsm | EntryKind::Field | EntryKind::Type => { // Nothing else to hash here. } EntryKind::Const(qualif) => { qualif.hash_stable(hcx, hasher); } EntryKind::Enum(ref repr_options) => { repr_options.hash_stable(hcx, hasher); } EntryKind::Variant(ref variant_data) => { variant_data.hash_stable(hcx, hasher); } EntryKind::Struct(ref variant_data, ref repr_options) | EntryKind::Union(ref variant_data, ref repr_options) => { variant_data.hash_stable(hcx, hasher); repr_options.hash_stable(hcx, hasher); } EntryKind::Fn(ref fn_data) | EntryKind::ForeignFn(ref fn_data) => { fn_data.hash_stable(hcx, hasher); } EntryKind::Mod(ref mod_data) => { mod_data.hash_stable(hcx, hasher); } EntryKind::MacroDef(ref macro_def) => { macro_def.hash_stable(hcx, hasher); } EntryKind::Closure(closure_data) => { closure_data.hash_stable(hcx, hasher); } EntryKind::Trait(ref trait_data) => { trait_data.hash_stable(hcx, hasher); } EntryKind::DefaultImpl(ref impl_data) | EntryKind::Impl(ref impl_data) => { impl_data.hash_stable(hcx, hasher); } EntryKind::Method(ref method_data) => { method_data.hash_stable(hcx, hasher); } EntryKind::AssociatedType(associated_container) => { associated_container.hash_stable(hcx, hasher); } EntryKind::AssociatedConst(associated_container, qualif) => { associated_container.hash_stable(hcx, hasher); qualif.hash_stable(hcx, hasher); } } } } #[derive(RustcEncodable, RustcDecodable)] pub struct ModData { pub reexports: LazySeq, } impl_stable_hash_for!(struct ModData { reexports }); #[derive(RustcEncodable, RustcDecodable)] pub struct MacroDef { pub body: String, pub legacy: bool, } impl_stable_hash_for!(struct MacroDef { body, legacy }); #[derive(RustcEncodable, RustcDecodable)] pub struct FnData { pub constness: hir::Constness, pub arg_names: LazySeq, } impl_stable_hash_for!(struct FnData { constness, arg_names }); #[derive(RustcEncodable, RustcDecodable)] pub struct VariantData { pub ctor_kind: CtorKind, pub discr: ty::VariantDiscr, /// If this is a struct's only variant, this /// is the index of the "struct ctor" item. pub struct_ctor: Option, } impl_stable_hash_for!(struct VariantData { ctor_kind, discr, struct_ctor }); #[derive(RustcEncodable, RustcDecodable)] pub struct TraitData<'tcx> { pub unsafety: hir::Unsafety, pub paren_sugar: bool, pub has_default_impl: bool, pub super_predicates: Lazy>, } impl_stable_hash_for!(struct TraitData<'tcx> { unsafety, paren_sugar, has_default_impl, super_predicates }); #[derive(RustcEncodable, RustcDecodable)] pub struct ImplData<'tcx> { pub polarity: hir::ImplPolarity, pub defaultness: hir::Defaultness, pub parent_impl: Option, /// This is `Some` only for impls of `CoerceUnsized`. pub coerce_unsized_info: Option, pub trait_ref: Option>>, } impl_stable_hash_for!(struct ImplData<'tcx> { polarity, defaultness, parent_impl, coerce_unsized_info, trait_ref }); /// Describes whether the container of an associated item /// is a trait or an impl and whether, in a trait, it has /// a default, or an in impl, whether it's marked "default". #[derive(Copy, Clone, RustcEncodable, RustcDecodable)] pub enum AssociatedContainer { TraitRequired, TraitWithDefault, ImplDefault, ImplFinal, } impl_stable_hash_for!(enum ::schema::AssociatedContainer { TraitRequired, TraitWithDefault, ImplDefault, ImplFinal }); impl AssociatedContainer { pub fn with_def_id(&self, def_id: DefId) -> ty::AssociatedItemContainer { match *self { AssociatedContainer::TraitRequired | AssociatedContainer::TraitWithDefault => ty::TraitContainer(def_id), AssociatedContainer::ImplDefault | AssociatedContainer::ImplFinal => ty::ImplContainer(def_id), } } pub fn defaultness(&self) -> hir::Defaultness { match *self { AssociatedContainer::TraitRequired => hir::Defaultness::Default { has_value: false, }, AssociatedContainer::TraitWithDefault | AssociatedContainer::ImplDefault => hir::Defaultness::Default { has_value: true, }, AssociatedContainer::ImplFinal => hir::Defaultness::Final, } } } #[derive(RustcEncodable, RustcDecodable)] pub struct MethodData { pub fn_data: FnData, pub container: AssociatedContainer, pub has_self: bool, } impl_stable_hash_for!(struct MethodData { fn_data, container, has_self }); #[derive(RustcEncodable, RustcDecodable)] pub struct ClosureData<'tcx> { pub kind: ty::ClosureKind, pub ty: Lazy>, } impl_stable_hash_for!(struct ClosureData<'tcx> { kind, ty });