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::ty::{self, Ty, ReprOptions};
12 use rustc_target::spec::{PanicStrategy, TargetTriple};
13 use rustc_data_structures::svh::Svh;
15 use rustc_serialize as serialize;
16 use syntax::{ast, attr};
17 use syntax::edition::Edition;
18 use syntax::symbol::Symbol;
19 use syntax_pos::{self, Span};
21 use std::marker::PhantomData;
23 pub fn rustc_version() -> String {
25 option_env!("CFG_VERSION").unwrap_or("unknown version"))
28 /// Metadata encoding version.
29 /// N.B., increment this if you change the format of metadata such that
30 /// the rustc version can't be found to compare with `rustc_version()`.
31 pub const METADATA_VERSION: u8 = 4;
33 /// Metadata header which includes `METADATA_VERSION`.
34 /// To get older versions of rustc to ignore this metadata,
35 /// there are 4 zero bytes at the start, which are treated
36 /// as a length of 0 by old compilers.
38 /// This header is followed by the position of the `CrateRoot`,
39 /// which is encoded as a 32-bit big-endian unsigned integer,
40 /// and further followed by the rustc version string.
41 pub const METADATA_HEADER: &[u8; 12] =
42 &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
44 /// A value of type T referred to by its absolute position
45 /// in the metadata, and which can be decoded lazily.
47 /// Metadata is effective a tree, encoded in post-order,
48 /// and with the root's position written next to the header.
49 /// That means every single `Lazy` points to some previous
50 /// location in the metadata and is part of a larger node.
52 /// The first `Lazy` in a node is encoded as the backwards
53 /// distance from the position where the containing node
54 /// starts and where the `Lazy` points to, while the rest
55 /// use the forward distance from the previous `Lazy`.
56 /// Distances start at 1, as 0-byte nodes are invalid.
57 /// Also invalid are nodes being referred in a different
58 /// order than they were encoded in.
62 _marker: PhantomData<T>,
66 pub fn with_position(position: usize) -> Lazy<T> {
73 /// Returns the minimum encoded size of a value of type `T`.
74 // FIXME(eddyb) Give better estimates for certain types.
75 pub fn min_size() -> usize {
80 impl<T> Copy for Lazy<T> {}
81 impl<T> Clone for Lazy<T> {
82 fn clone(&self) -> Self {
87 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
88 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
90 /// A sequence of type T referred to by its absolute position
91 /// in the metadata and length, and which can be decoded lazily.
92 /// The sequence is a single node for the purposes of `Lazy`.
94 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
95 /// position, not at the position, which means that the length
96 /// doesn't need to be known before encoding all the elements.
98 /// If the length is 0, no position is encoded, but otherwise,
99 /// the encoding is that of `Lazy`, with the distinction that
100 /// the minimal distance the length of the sequence, i.e.
101 /// it's assumed there's no 0-byte element in the sequence.
103 pub struct LazySeq<T> {
106 _marker: PhantomData<T>,
110 pub fn empty() -> LazySeq<T> {
111 LazySeq::with_position_and_length(0, 0)
114 pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
118 _marker: PhantomData,
122 /// Returns the minimum encoded size of `length` values of type `T`.
123 pub fn min_size(length: usize) -> usize {
128 impl<T> Copy for LazySeq<T> {}
129 impl<T> Clone for LazySeq<T> {
130 fn clone(&self) -> Self {
135 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
136 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
138 /// Encoding / decoding state for `Lazy` and `LazySeq`.
139 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
141 /// Outside of a metadata node.
144 /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
145 /// The position is that of the node itself.
148 /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
149 /// The position is a conservative estimate of where that
150 /// previous `Lazy` / `LazySeq` would end (see their comments).
154 #[derive(RustcEncodable, RustcDecodable)]
155 pub struct CrateRoot<'tcx> {
157 pub triple: TargetTriple,
158 pub extra_filename: String,
160 pub disambiguator: CrateDisambiguator,
161 pub panic_strategy: PanicStrategy,
162 pub edition: Edition,
163 pub has_global_allocator: bool,
164 pub has_panic_handler: bool,
165 pub has_default_lib_allocator: bool,
166 pub plugin_registrar_fn: Option<DefIndex>,
167 pub proc_macro_decls_static: Option<DefIndex>,
168 pub proc_macro_stability: Option<attr::Stability>,
170 pub crate_deps: LazySeq<CrateDep>,
171 pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
172 pub lib_features: LazySeq<(Symbol, Option<Symbol>)>,
173 pub lang_items: LazySeq<(DefIndex, usize)>,
174 pub lang_items_missing: LazySeq<lang_items::LangItem>,
175 pub native_libraries: LazySeq<NativeLibrary>,
176 pub foreign_modules: LazySeq<ForeignModule>,
177 pub source_map: LazySeq<syntax_pos::SourceFile>,
178 pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
179 pub impls: LazySeq<TraitImpls>,
180 pub exported_symbols: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)>,
181 pub interpret_alloc_index: LazySeq<u32>,
183 pub entries_index: LazySeq<index::Index<'tcx>>,
185 pub compiler_builtins: bool,
186 pub needs_allocator: bool,
187 pub needs_panic_runtime: bool,
188 pub no_builtins: bool,
189 pub panic_runtime: bool,
190 pub profiler_runtime: bool,
191 pub sanitizer_runtime: bool,
194 #[derive(RustcEncodable, RustcDecodable)]
195 pub struct CrateDep {
199 pub extra_filename: String,
202 #[derive(RustcEncodable, RustcDecodable)]
203 pub struct TraitImpls {
204 pub trait_id: (u32, DefIndex),
205 pub impls: LazySeq<DefIndex>,
208 #[derive(RustcEncodable, RustcDecodable)]
209 pub struct Entry<'tcx> {
210 pub kind: EntryKind<'tcx>,
211 pub visibility: Lazy<ty::Visibility>,
212 pub span: Lazy<Span>,
213 pub attributes: LazySeq<ast::Attribute>,
214 pub children: LazySeq<DefIndex>,
215 pub stability: Option<Lazy<attr::Stability>>,
216 pub deprecation: Option<Lazy<attr::Deprecation>>,
218 pub ty: Option<Lazy<Ty<'tcx>>>,
219 pub inherent_impls: LazySeq<DefIndex>,
220 pub variances: LazySeq<ty::Variance>,
221 pub generics: Option<Lazy<ty::Generics>>,
222 pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
223 pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
225 pub mir: Option<Lazy<mir::Body<'tcx>>>,
228 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
229 pub enum EntryKind<'tcx> {
230 Const(ConstQualif, Lazy<RenderedConst>),
244 Variant(Lazy<VariantData<'tcx>>),
245 Struct(Lazy<VariantData<'tcx>>, ReprOptions),
246 Union(Lazy<VariantData<'tcx>>, ReprOptions),
247 Fn(Lazy<FnData<'tcx>>),
248 ForeignFn(Lazy<FnData<'tcx>>),
250 MacroDef(Lazy<MacroDef>),
251 Closure(Lazy<ClosureData<'tcx>>),
252 Generator(Lazy<GeneratorData<'tcx>>),
253 Trait(Lazy<TraitData<'tcx>>),
254 Impl(Lazy<ImplData<'tcx>>),
255 Method(Lazy<MethodData<'tcx>>),
256 AssocType(AssocContainer),
257 AssocExistential(AssocContainer),
258 AssocConst(AssocContainer, ConstQualif, Lazy<RenderedConst>),
259 TraitAlias(Lazy<TraitAliasData<'tcx>>),
262 /// Additional data for EntryKind::Const and EntryKind::AssocConst
263 #[derive(Clone, Copy, RustcEncodable, RustcDecodable)]
264 pub struct ConstQualif {
266 pub ast_promotable: bool,
269 /// Contains a constant which has been rendered to a String.
271 #[derive(RustcEncodable, RustcDecodable)]
272 pub struct RenderedConst(pub String);
274 #[derive(RustcEncodable, RustcDecodable)]
276 pub reexports: LazySeq<def::Export<hir::HirId>>,
279 #[derive(RustcEncodable, RustcDecodable)]
280 pub struct MacroDef {
285 #[derive(RustcEncodable, RustcDecodable)]
286 pub struct FnData<'tcx> {
287 pub constness: hir::Constness,
288 pub arg_names: LazySeq<ast::Name>,
289 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
292 #[derive(RustcEncodable, RustcDecodable)]
293 pub struct VariantData<'tcx> {
294 pub ctor_kind: CtorKind,
295 pub discr: ty::VariantDiscr,
296 /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
297 pub ctor: Option<DefIndex>,
298 /// If this is a tuple struct or variant
299 /// ctor, this is its "function" signature.
300 pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
303 #[derive(RustcEncodable, RustcDecodable)]
304 pub struct TraitData<'tcx> {
305 pub unsafety: hir::Unsafety,
306 pub paren_sugar: bool,
307 pub has_auto_impl: bool,
309 pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
312 #[derive(RustcEncodable, RustcDecodable)]
313 pub struct TraitAliasData<'tcx> {
314 pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
317 #[derive(RustcEncodable, RustcDecodable)]
318 pub struct ImplData<'tcx> {
319 pub polarity: hir::ImplPolarity,
320 pub defaultness: hir::Defaultness,
321 pub parent_impl: Option<DefId>,
323 /// This is `Some` only for impls of `CoerceUnsized`.
324 pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
325 pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
329 /// Describes whether the container of an associated item
330 /// is a trait or an impl and whether, in a trait, it has
331 /// a default, or an in impl, whether it's marked "default".
332 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
333 pub enum AssocContainer {
340 impl AssocContainer {
341 pub fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
343 AssocContainer::TraitRequired |
344 AssocContainer::TraitWithDefault => ty::TraitContainer(def_id),
346 AssocContainer::ImplDefault |
347 AssocContainer::ImplFinal => ty::ImplContainer(def_id),
351 pub fn defaultness(&self) -> hir::Defaultness {
353 AssocContainer::TraitRequired => hir::Defaultness::Default {
357 AssocContainer::TraitWithDefault |
358 AssocContainer::ImplDefault => hir::Defaultness::Default {
362 AssocContainer::ImplFinal => hir::Defaultness::Final,
367 #[derive(RustcEncodable, RustcDecodable)]
368 pub struct MethodData<'tcx> {
369 pub fn_data: FnData<'tcx>,
370 pub container: AssocContainer,
374 #[derive(RustcEncodable, RustcDecodable)]
375 pub struct ClosureData<'tcx> {
376 pub sig: Lazy<ty::PolyFnSig<'tcx>>,
379 #[derive(RustcEncodable, RustcDecodable)]
380 pub struct GeneratorData<'tcx> {
381 pub layout: mir::GeneratorLayout<'tcx>,
384 // Tags used for encoding Spans:
385 pub const TAG_VALID_SPAN: u8 = 0;
386 pub const TAG_INVALID_SPAN: u8 = 1;