]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/schema.rs
Rollup merge of #41087 - estebank:tuple-float-index, r=arielb1
[rust.git] / src / librustc_metadata / schema.rs
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.
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 astencode;
12 use index;
13
14 use rustc::hir;
15 use rustc::hir::def::{self, CtorKind};
16 use rustc::hir::def_id::{DefIndex, DefId};
17 use rustc::middle::const_val::ConstVal;
18 use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary};
19 use rustc::middle::lang_items;
20 use rustc::mir;
21 use rustc::ty::{self, Ty, ReprOptions};
22 use rustc_back::PanicStrategy;
23
24 use rustc_serialize as serialize;
25 use syntax::{ast, attr};
26 use syntax::symbol::Symbol;
27 use syntax_pos::{self, Span};
28
29 use std::marker::PhantomData;
30
31 pub fn rustc_version() -> String {
32     format!("rustc {}",
33             option_env!("CFG_VERSION").unwrap_or("unknown version"))
34 }
35
36 /// Metadata encoding version.
37 /// NB: increment this if you change the format of metadata such that
38 /// the rustc version can't be found to compare with `rustc_version()`.
39 pub const METADATA_VERSION: u8 = 4;
40
41 /// Metadata header which includes `METADATA_VERSION`.
42 /// To get older versions of rustc to ignore this metadata,
43 /// there are 4 zero bytes at the start, which are treated
44 /// as a length of 0 by old compilers.
45 ///
46 /// This header is followed by the position of the `CrateRoot`,
47 /// which is encoded as a 32-bit big-endian unsigned integer,
48 /// and further followed by the rustc version string.
49 pub const METADATA_HEADER: &'static [u8; 12] =
50     &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
51
52 /// The shorthand encoding uses an enum's variant index `usize`
53 /// and is offset by this value so it never matches a real variant.
54 /// This offset is also chosen so that the first byte is never < 0x80.
55 pub const SHORTHAND_OFFSET: usize = 0x80;
56
57 /// A value of type T referred to by its absolute position
58 /// in the metadata, and which can be decoded lazily.
59 ///
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.
64 ///
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.
72 #[must_use]
73 pub struct Lazy<T> {
74     pub position: usize,
75     _marker: PhantomData<T>,
76 }
77
78 impl<T> Lazy<T> {
79     pub fn with_position(position: usize) -> Lazy<T> {
80         Lazy {
81             position: position,
82             _marker: PhantomData,
83         }
84     }
85
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 {
89         1
90     }
91 }
92
93 impl<T> Copy for Lazy<T> {}
94 impl<T> Clone for Lazy<T> {
95     fn clone(&self) -> Self {
96         *self
97     }
98 }
99
100 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
101 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
102
103 /// A sequence of type T referred to by its absolute position
104 /// in the metadata and length, and which can be decoded lazily.
105 /// The sequence is a single node for the purposes of `Lazy`.
106 ///
107 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
108 /// position, not at the position, which means that the length
109 /// doesn't need to be known before encoding all the elements.
110 ///
111 /// If the length is 0, no position is encoded, but otherwise,
112 /// the encoding is that of `Lazy`, with the distinction that
113 /// the minimal distance the length of the sequence, i.e.
114 /// it's assumed there's no 0-byte element in the sequence.
115 #[must_use]
116 pub struct LazySeq<T> {
117     pub len: usize,
118     pub position: usize,
119     _marker: PhantomData<T>,
120 }
121
122 impl<T> LazySeq<T> {
123     pub fn empty() -> LazySeq<T> {
124         LazySeq::with_position_and_length(0, 0)
125     }
126
127     pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
128         LazySeq {
129             len: len,
130             position: position,
131             _marker: PhantomData,
132         }
133     }
134
135     /// Returns the minimum encoded size of `length` values of type `T`.
136     pub fn min_size(length: usize) -> usize {
137         length
138     }
139 }
140
141 impl<T> Copy for LazySeq<T> {}
142 impl<T> Clone for LazySeq<T> {
143     fn clone(&self) -> Self {
144         *self
145     }
146 }
147
148 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
149 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
150
151 /// Encoding / decoding state for `Lazy` and `LazySeq`.
152 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
153 pub enum LazyState {
154     /// Outside of a metadata node.
155     NoNode,
156
157     /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
158     /// The position is that of the node itself.
159     NodeStart(usize),
160
161     /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
162     /// The position is a conservative estimate of where that
163     /// previous `Lazy` / `LazySeq` would end (see their comments).
164     Previous(usize),
165 }
166
167 #[derive(RustcEncodable, RustcDecodable)]
168 pub struct CrateRoot {
169     pub name: Symbol,
170     pub triple: String,
171     pub hash: hir::svh::Svh,
172     pub disambiguator: Symbol,
173     pub panic_strategy: PanicStrategy,
174     pub plugin_registrar_fn: Option<DefIndex>,
175     pub macro_derive_registrar: Option<DefIndex>,
176
177     pub crate_deps: LazySeq<CrateDep>,
178     pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
179     pub lang_items: LazySeq<(DefIndex, usize)>,
180     pub lang_items_missing: LazySeq<lang_items::LangItem>,
181     pub native_libraries: LazySeq<NativeLibrary>,
182     pub codemap: LazySeq<syntax_pos::FileMap>,
183     pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
184     pub impls: LazySeq<TraitImpls>,
185     pub exported_symbols: LazySeq<DefIndex>,
186     pub index: LazySeq<index::Index>,
187 }
188
189 #[derive(RustcEncodable, RustcDecodable)]
190 pub struct CrateDep {
191     pub name: ast::Name,
192     pub hash: hir::svh::Svh,
193     pub kind: DepKind,
194 }
195
196 #[derive(RustcEncodable, RustcDecodable)]
197 pub struct TraitImpls {
198     pub trait_id: (u32, DefIndex),
199     pub impls: LazySeq<DefIndex>,
200 }
201
202 #[derive(RustcEncodable, RustcDecodable)]
203 pub struct Entry<'tcx> {
204     pub kind: EntryKind<'tcx>,
205     pub visibility: Lazy<ty::Visibility>,
206     pub span: Lazy<Span>,
207     pub attributes: LazySeq<ast::Attribute>,
208     pub children: LazySeq<DefIndex>,
209     pub stability: Option<Lazy<attr::Stability>>,
210     pub deprecation: Option<Lazy<attr::Deprecation>>,
211
212     pub ty: Option<Lazy<Ty<'tcx>>>,
213     pub inherent_impls: LazySeq<DefIndex>,
214     pub variances: LazySeq<ty::Variance>,
215     pub generics: Option<Lazy<ty::Generics>>,
216     pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
217
218     pub ast: Option<Lazy<astencode::Ast<'tcx>>>,
219     pub mir: Option<Lazy<mir::Mir<'tcx>>>,
220 }
221
222 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
223 pub enum EntryKind<'tcx> {
224     Const(u8),
225     ImmStatic,
226     MutStatic,
227     ForeignImmStatic,
228     ForeignMutStatic,
229     ForeignMod,
230     Type,
231     Enum(ReprOptions),
232     Field,
233     Variant(Lazy<VariantData<'tcx>>),
234     Struct(Lazy<VariantData<'tcx>>, ReprOptions),
235     Union(Lazy<VariantData<'tcx>>, ReprOptions),
236     Fn(Lazy<FnData>),
237     ForeignFn(Lazy<FnData>),
238     Mod(Lazy<ModData>),
239     MacroDef(Lazy<MacroDef>),
240     Closure(Lazy<ClosureData<'tcx>>),
241     Trait(Lazy<TraitData<'tcx>>),
242     Impl(Lazy<ImplData<'tcx>>),
243     DefaultImpl(Lazy<ImplData<'tcx>>),
244     Method(Lazy<MethodData>),
245     AssociatedType(AssociatedContainer),
246     AssociatedConst(AssociatedContainer, u8),
247 }
248
249 #[derive(RustcEncodable, RustcDecodable)]
250 pub struct ModData {
251     pub reexports: LazySeq<def::Export>,
252 }
253
254 #[derive(RustcEncodable, RustcDecodable)]
255 pub struct MacroDef {
256     pub body: String,
257 }
258
259 #[derive(RustcEncodable, RustcDecodable)]
260 pub struct FnData {
261     pub constness: hir::Constness,
262     pub arg_names: LazySeq<ast::Name>,
263 }
264
265 #[derive(RustcEncodable, RustcDecodable)]
266 pub struct VariantData<'tcx> {
267     pub ctor_kind: CtorKind,
268     pub discr: ty::VariantDiscr,
269     pub evaluated_discr: Option<ConstVal<'tcx>>,
270
271     /// If this is a struct's only variant, this
272     /// is the index of the "struct ctor" item.
273     pub struct_ctor: Option<DefIndex>,
274 }
275
276 #[derive(RustcEncodable, RustcDecodable)]
277 pub struct TraitData<'tcx> {
278     pub unsafety: hir::Unsafety,
279     pub paren_sugar: bool,
280     pub has_default_impl: bool,
281     pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
282 }
283
284 #[derive(RustcEncodable, RustcDecodable)]
285 pub struct ImplData<'tcx> {
286     pub polarity: hir::ImplPolarity,
287     pub parent_impl: Option<DefId>,
288
289     /// This is `Some` only for impls of `CoerceUnsized`.
290     pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
291     pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
292 }
293
294 /// Describes whether the container of an associated item
295 /// is a trait or an impl and whether, in a trait, it has
296 /// a default, or an in impl, whether it's marked "default".
297 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
298 pub enum AssociatedContainer {
299     TraitRequired,
300     TraitWithDefault,
301     ImplDefault,
302     ImplFinal,
303 }
304
305 impl AssociatedContainer {
306     pub fn with_def_id(&self, def_id: DefId) -> ty::AssociatedItemContainer {
307         match *self {
308             AssociatedContainer::TraitRequired |
309             AssociatedContainer::TraitWithDefault => ty::TraitContainer(def_id),
310
311             AssociatedContainer::ImplDefault |
312             AssociatedContainer::ImplFinal => ty::ImplContainer(def_id),
313         }
314     }
315
316     pub fn defaultness(&self) -> hir::Defaultness {
317         match *self {
318             AssociatedContainer::TraitRequired => hir::Defaultness::Default {
319                 has_value: false,
320             },
321
322             AssociatedContainer::TraitWithDefault |
323             AssociatedContainer::ImplDefault => hir::Defaultness::Default {
324                 has_value: true,
325             },
326
327             AssociatedContainer::ImplFinal => hir::Defaultness::Final,
328         }
329     }
330 }
331
332 #[derive(RustcEncodable, RustcDecodable)]
333 pub struct MethodData {
334     pub fn_data: FnData,
335     pub container: AssociatedContainer,
336     pub has_self: bool,
337 }
338
339 #[derive(RustcEncodable, RustcDecodable)]
340 pub struct ClosureData<'tcx> {
341     pub kind: ty::ClosureKind,
342     pub ty: Lazy<ty::PolyFnSig<'tcx>>,
343 }