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