4 use rustc::hir::def::{self, CtorKind};
5 use rustc::hir::def_id::{DefIndex, DefId};
6 use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
7 use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignModule};
8 use rustc::middle::lang_items;
10 use rustc::session::CrateDisambiguator;
11 use rustc::session::config::SymbolManglingVersion;
12 use rustc::ty::{self, Ty, ReprOptions};
13 use rustc_target::spec::{PanicStrategy, TargetTriple};
14 use rustc_data_structures::svh::Svh;
16 use rustc_serialize as serialize;
17 use syntax::{ast, attr};
18 use syntax::edition::Edition;
19 use syntax::symbol::Symbol;
20 use syntax_pos::{self, Span};
22 use std::marker::PhantomData;
24 pub fn rustc_version() -> String {
26 option_env!("CFG_VERSION").unwrap_or("unknown version"))
29 /// Metadata encoding version.
30 /// N.B., increment this if you change the format of metadata such that
31 /// the rustc version can't be found to compare with `rustc_version()`.
32 pub const METADATA_VERSION: u8 = 4;
34 /// Metadata header which includes `METADATA_VERSION`.
35 /// To get older versions of rustc to ignore this metadata,
36 /// there are 4 zero bytes at the start, which are treated
37 /// as a length of 0 by old compilers.
39 /// This header is followed by the position of the `CrateRoot`,
40 /// which is encoded as a 32-bit big-endian unsigned integer,
41 /// and further followed by the rustc version string.
42 pub const METADATA_HEADER: &[u8; 12] =
43 &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
45 /// A value of type T referred to by its absolute position
46 /// in the metadata, and which can be decoded lazily.
48 /// Metadata is effective a tree, encoded in post-order,
49 /// and with the root's position written next to the header.
50 /// That means every single `Lazy` points to some previous
51 /// location in the metadata and is part of a larger node.
53 /// The first `Lazy` in a node is encoded as the backwards
54 /// distance from the position where the containing node
55 /// starts and where the `Lazy` points to, while the rest
56 /// use the forward distance from the previous `Lazy`.
57 /// Distances start at 1, as 0-byte nodes are invalid.
58 /// Also invalid are nodes being referred in a different
59 /// order than they were encoded in.
63 _marker: PhantomData<T>,
67 pub fn with_position(position: usize) -> Lazy<T> {
74 /// Returns the minimum encoded size of a value of type `T`.
75 // FIXME(eddyb) Give better estimates for certain types.
76 pub fn min_size() -> usize {
81 impl<T> Copy for Lazy<T> {}
82 impl<T> Clone for Lazy<T> {
83 fn clone(&self) -> Self {
88 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
89 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
91 /// A sequence of type T referred to by its absolute position
92 /// in the metadata and length, and which can be decoded lazily.
93 /// The sequence is a single node for the purposes of `Lazy`.
95 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
96 /// position, not at the position, which means that the length
97 /// doesn't need to be known before encoding all the elements.
99 /// If the length is 0, no position is encoded, but otherwise,
100 /// the encoding is that of `Lazy`, with the distinction that
101 /// the minimal distance the length of the sequence, i.e.
102 /// it's assumed there's no 0-byte element in the sequence.
104 pub struct LazySeq<T> {
107 _marker: PhantomData<T>,
111 pub fn empty() -> LazySeq<T> {
112 LazySeq::with_position_and_length(0, 0)
115 pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
119 _marker: PhantomData,
123 /// Returns the minimum encoded size of `length` values of type `T`.
124 pub fn min_size(length: usize) -> usize {
129 impl<T> Copy for LazySeq<T> {}
130 impl<T> Clone for LazySeq<T> {
131 fn clone(&self) -> Self {
136 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
137 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
139 /// Encoding / decoding state for `Lazy` and `LazySeq`.
140 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
142 /// Outside of a metadata node.
145 /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
146 /// The position is that of the node itself.
149 /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
150 /// The position is a conservative estimate of where that
151 /// previous `Lazy` / `LazySeq` would end (see their comments).
155 #[derive(RustcEncodable, RustcDecodable)]
156 pub struct CrateRoot<'tcx> {
158 pub triple: TargetTriple,
159 pub extra_filename: String,
161 pub disambiguator: CrateDisambiguator,
162 pub panic_strategy: PanicStrategy,
163 pub edition: Edition,
164 pub has_global_allocator: bool,
165 pub has_panic_handler: bool,
166 pub has_default_lib_allocator: bool,
167 pub plugin_registrar_fn: Option<DefIndex>,
168 pub proc_macro_decls_static: Option<DefIndex>,
169 pub proc_macro_stability: Option<attr::Stability>,
171 pub crate_deps: LazySeq<CrateDep>,
172 pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
173 pub lib_features: LazySeq<(Symbol, Option<Symbol>)>,
174 pub lang_items: LazySeq<(DefIndex, usize)>,
175 pub lang_items_missing: LazySeq<lang_items::LangItem>,
176 pub native_libraries: LazySeq<NativeLibrary>,
177 pub foreign_modules: LazySeq<ForeignModule>,
178 pub source_map: LazySeq<syntax_pos::SourceFile>,
179 pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
180 pub impls: LazySeq<TraitImpls>,
181 pub exported_symbols: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)>,
182 pub interpret_alloc_index: LazySeq<u32>,
184 pub entries_index: LazySeq<index::Index<'tcx>>,
186 pub compiler_builtins: bool,
187 pub needs_allocator: bool,
188 pub needs_panic_runtime: bool,
189 pub no_builtins: bool,
190 pub panic_runtime: bool,
191 pub profiler_runtime: bool,
192 pub sanitizer_runtime: bool,
193 pub symbol_mangling_version: SymbolManglingVersion,
196 #[derive(RustcEncodable, RustcDecodable)]
197 pub struct CrateDep {
201 pub extra_filename: String,
204 #[derive(RustcEncodable, RustcDecodable)]
205 pub struct TraitImpls {
206 pub trait_id: (u32, DefIndex),
207 pub impls: LazySeq<DefIndex>,
210 #[derive(RustcEncodable, RustcDecodable)]
211 pub struct Entry<'tcx> {
212 pub kind: EntryKind<'tcx>,
213 pub visibility: Lazy<ty::Visibility>,
214 pub span: Lazy<Span>,
215 pub attributes: LazySeq<ast::Attribute>,
216 pub children: LazySeq<DefIndex>,
217 pub stability: Option<Lazy<attr::Stability>>,
218 pub deprecation: Option<Lazy<attr::Deprecation>>,
220 pub ty: Option<Lazy<Ty<'tcx>>>,
221 pub inherent_impls: LazySeq<DefIndex>,
222 pub variances: LazySeq<ty::Variance>,
223 pub generics: Option<Lazy<ty::Generics>>,
224 pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
225 pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
227 pub mir: Option<Lazy<mir::Body<'tcx>>>,
230 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
231 pub enum EntryKind<'tcx> {
232 Const(ConstQualif, Lazy<RenderedConst>),
246 Variant(Lazy<VariantData<'tcx>>),
247 Struct(Lazy<VariantData<'tcx>>, ReprOptions),
248 Union(Lazy<VariantData<'tcx>>, ReprOptions),
249 Fn(Lazy<FnData<'tcx>>),
250 ForeignFn(Lazy<FnData<'tcx>>),
252 MacroDef(Lazy<MacroDef>),
253 Closure(Lazy<ClosureData<'tcx>>),
254 Generator(Lazy<GeneratorData<'tcx>>),
255 Trait(Lazy<TraitData<'tcx>>),
256 Impl(Lazy<ImplData<'tcx>>),
257 Method(Lazy<MethodData<'tcx>>),
258 AssocType(AssocContainer),
259 AssocExistential(AssocContainer),
260 AssocConst(AssocContainer, ConstQualif, Lazy<RenderedConst>),
261 TraitAlias(Lazy<TraitAliasData<'tcx>>),
264 /// Additional data for EntryKind::Const and EntryKind::AssocConst
265 #[derive(Clone, Copy, RustcEncodable, RustcDecodable)]
266 pub struct ConstQualif {
268 pub ast_promotable: bool,
271 /// Contains a constant which has been rendered to a String.
273 #[derive(RustcEncodable, RustcDecodable)]
274 pub struct RenderedConst(pub String);
276 #[derive(RustcEncodable, RustcDecodable)]
278 pub reexports: LazySeq<def::Export<hir::HirId>>,
281 #[derive(RustcEncodable, RustcDecodable)]
282 pub struct MacroDef {
287 #[derive(RustcEncodable, RustcDecodable)]
288 pub struct FnData<'tcx> {
289 pub constness: hir::Constness,
290 pub arg_names: LazySeq<ast::Name>,
291 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
294 #[derive(RustcEncodable, RustcDecodable)]
295 pub struct VariantData<'tcx> {
296 pub ctor_kind: CtorKind,
297 pub discr: ty::VariantDiscr,
298 /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
299 pub ctor: Option<DefIndex>,
300 /// If this is a tuple struct or variant
301 /// ctor, this is its "function" signature.
302 pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
305 #[derive(RustcEncodable, RustcDecodable)]
306 pub struct TraitData<'tcx> {
307 pub unsafety: hir::Unsafety,
308 pub paren_sugar: bool,
309 pub has_auto_impl: bool,
311 pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
314 #[derive(RustcEncodable, RustcDecodable)]
315 pub struct TraitAliasData<'tcx> {
316 pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
319 #[derive(RustcEncodable, RustcDecodable)]
320 pub struct ImplData<'tcx> {
321 pub polarity: hir::ImplPolarity,
322 pub defaultness: hir::Defaultness,
323 pub parent_impl: Option<DefId>,
325 /// This is `Some` only for impls of `CoerceUnsized`.
326 pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
327 pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
331 /// Describes whether the container of an associated item
332 /// is a trait or an impl and whether, in a trait, it has
333 /// a default, or an in impl, whether it's marked "default".
334 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
335 pub enum AssocContainer {
342 impl AssocContainer {
343 pub fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
345 AssocContainer::TraitRequired |
346 AssocContainer::TraitWithDefault => ty::TraitContainer(def_id),
348 AssocContainer::ImplDefault |
349 AssocContainer::ImplFinal => ty::ImplContainer(def_id),
353 pub fn defaultness(&self) -> hir::Defaultness {
355 AssocContainer::TraitRequired => hir::Defaultness::Default {
359 AssocContainer::TraitWithDefault |
360 AssocContainer::ImplDefault => hir::Defaultness::Default {
364 AssocContainer::ImplFinal => hir::Defaultness::Final,
369 #[derive(RustcEncodable, RustcDecodable)]
370 pub struct MethodData<'tcx> {
371 pub fn_data: FnData<'tcx>,
372 pub container: AssocContainer,
376 #[derive(RustcEncodable, RustcDecodable)]
377 pub struct ClosureData<'tcx> {
378 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
381 #[derive(RustcEncodable, RustcDecodable)]
382 pub struct GeneratorData<'tcx> {
383 pub layout: mir::GeneratorLayout<'tcx>,
386 // Tags used for encoding Spans:
387 pub const TAG_VALID_SPAN: u8 = 0;
388 pub const TAG_INVALID_SPAN: u8 = 1;