]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/schema.rs
dad0d26d2715d172ae92150765170b20039b86d4
[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, CrateNum};
17 use rustc::ich::StableHashingContext;
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 use std::mem;
31
32 use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
33                                            StableHasherResult};
34
35 pub fn rustc_version() -> String {
36     format!("rustc {}",
37             option_env!("CFG_VERSION").unwrap_or("unknown version"))
38 }
39
40 /// Metadata encoding version.
41 /// NB: increment this if you change the format of metadata such that
42 /// the rustc version can't be found to compare with `rustc_version()`.
43 pub const METADATA_VERSION: u8 = 4;
44
45 /// Metadata header which includes `METADATA_VERSION`.
46 /// To get older versions of rustc to ignore this metadata,
47 /// there are 4 zero bytes at the start, which are treated
48 /// as a length of 0 by old compilers.
49 ///
50 /// This header is followed by the position of the `CrateRoot`,
51 /// which is encoded as a 32-bit big-endian unsigned integer,
52 /// and further followed by the rustc version string.
53 pub const METADATA_HEADER: &'static [u8; 12] =
54     &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
55
56 /// The shorthand encoding uses an enum's variant index `usize`
57 /// and is offset by this value so it never matches a real variant.
58 /// This offset is also chosen so that the first byte is never < 0x80.
59 pub const SHORTHAND_OFFSET: usize = 0x80;
60
61 /// A value of type T referred to by its absolute position
62 /// in the metadata, and which can be decoded lazily.
63 ///
64 /// Metadata is effective a tree, encoded in post-order,
65 /// and with the root's position written next to the header.
66 /// That means every single `Lazy` points to some previous
67 /// location in the metadata and is part of a larger node.
68 ///
69 /// The first `Lazy` in a node is encoded as the backwards
70 /// distance from the position where the containing node
71 /// starts and where the `Lazy` points to, while the rest
72 /// use the forward distance from the previous `Lazy`.
73 /// Distances start at 1, as 0-byte nodes are invalid.
74 /// Also invalid are nodes being referred in a different
75 /// order than they were encoded in.
76 #[must_use]
77 pub struct Lazy<T> {
78     pub position: usize,
79     _marker: PhantomData<T>,
80 }
81
82 impl<T> Lazy<T> {
83     pub fn with_position(position: usize) -> Lazy<T> {
84         Lazy {
85             position,
86             _marker: PhantomData,
87         }
88     }
89
90     /// Returns the minimum encoded size of a value of type `T`.
91     // FIXME(eddyb) Give better estimates for certain types.
92     pub fn min_size() -> usize {
93         1
94     }
95 }
96
97 impl<T> Copy for Lazy<T> {}
98 impl<T> Clone for Lazy<T> {
99     fn clone(&self) -> Self {
100         *self
101     }
102 }
103
104 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
105 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
106
107 impl<CTX, T> HashStable<CTX> for Lazy<T> {
108     fn hash_stable<W: StableHasherResult>(&self,
109                                           _: &mut CTX,
110                                           _: &mut StableHasher<W>) {
111         // There's nothing to do. Whatever got encoded within this Lazy<>
112         // wrapper has already been hashed.
113     }
114 }
115
116 /// A sequence of type T referred to by its absolute position
117 /// in the metadata and length, and which can be decoded lazily.
118 /// The sequence is a single node for the purposes of `Lazy`.
119 ///
120 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
121 /// position, not at the position, which means that the length
122 /// doesn't need to be known before encoding all the elements.
123 ///
124 /// If the length is 0, no position is encoded, but otherwise,
125 /// the encoding is that of `Lazy`, with the distinction that
126 /// the minimal distance the length of the sequence, i.e.
127 /// it's assumed there's no 0-byte element in the sequence.
128 #[must_use]
129 pub struct LazySeq<T> {
130     pub len: usize,
131     pub position: usize,
132     _marker: PhantomData<T>,
133 }
134
135 impl<T> LazySeq<T> {
136     pub fn empty() -> LazySeq<T> {
137         LazySeq::with_position_and_length(0, 0)
138     }
139
140     pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
141         LazySeq {
142             len,
143             position,
144             _marker: PhantomData,
145         }
146     }
147
148     /// Returns the minimum encoded size of `length` values of type `T`.
149     pub fn min_size(length: usize) -> usize {
150         length
151     }
152 }
153
154 impl<T> Copy for LazySeq<T> {}
155 impl<T> Clone for LazySeq<T> {
156     fn clone(&self) -> Self {
157         *self
158     }
159 }
160
161 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
162 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
163
164 impl<CTX, T> HashStable<CTX> for LazySeq<T> {
165     fn hash_stable<W: StableHasherResult>(&self,
166                                           _: &mut CTX,
167                                           _: &mut StableHasher<W>) {
168         // There's nothing to do. Whatever got encoded within this Lazy<>
169         // wrapper has already been hashed.
170     }
171 }
172
173 /// Encoding / decoding state for `Lazy` and `LazySeq`.
174 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
175 pub enum LazyState {
176     /// Outside of a metadata node.
177     NoNode,
178
179     /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
180     /// The position is that of the node itself.
181     NodeStart(usize),
182
183     /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
184     /// The position is a conservative estimate of where that
185     /// previous `Lazy` / `LazySeq` would end (see their comments).
186     Previous(usize),
187 }
188
189 #[derive(RustcEncodable, RustcDecodable)]
190 pub struct CrateRoot {
191     pub name: Symbol,
192     pub triple: String,
193     pub hash: hir::svh::Svh,
194     pub disambiguator: Symbol,
195     pub panic_strategy: PanicStrategy,
196     pub has_global_allocator: bool,
197     pub has_default_lib_allocator: bool,
198     pub plugin_registrar_fn: Option<DefIndex>,
199     pub macro_derive_registrar: Option<DefIndex>,
200
201     pub crate_deps: LazySeq<CrateDep>,
202     pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
203     pub lang_items: LazySeq<(DefIndex, usize)>,
204     pub lang_items_missing: LazySeq<lang_items::LangItem>,
205     pub native_libraries: LazySeq<NativeLibrary>,
206     pub codemap: LazySeq<syntax_pos::FileMap>,
207     pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
208     pub impls: LazySeq<TraitImpls>,
209     pub exported_symbols: LazySeq<DefIndex>,
210     pub index: LazySeq<index::Index>,
211 }
212
213 #[derive(RustcEncodable, RustcDecodable)]
214 pub struct CrateDep {
215     pub name: ast::Name,
216     pub hash: hir::svh::Svh,
217     pub kind: DepKind,
218 }
219
220 impl_stable_hash_for!(struct CrateDep {
221     name,
222     hash,
223     kind
224 });
225
226 #[derive(RustcEncodable, RustcDecodable)]
227 pub struct TraitImpls {
228     pub trait_id: (u32, DefIndex),
229     pub impls: LazySeq<DefIndex>,
230 }
231
232 impl<'gcx> HashStable<StableHashingContext<'gcx>> for TraitImpls {
233     fn hash_stable<W: StableHasherResult>(&self,
234                                           hcx: &mut StableHashingContext<'gcx>,
235                                           hasher: &mut StableHasher<W>) {
236         let TraitImpls {
237             trait_id: (krate, def_index),
238             ref impls,
239         } = *self;
240
241         DefId {
242             krate: CrateNum::from_u32(krate),
243             index: def_index
244         }.hash_stable(hcx, hasher);
245         impls.hash_stable(hcx, hasher);
246     }
247 }
248
249 #[derive(RustcEncodable, RustcDecodable)]
250 pub struct Entry<'tcx> {
251     pub kind: EntryKind<'tcx>,
252     pub visibility: Lazy<ty::Visibility>,
253     pub span: Lazy<Span>,
254     pub attributes: LazySeq<ast::Attribute>,
255     pub children: LazySeq<DefIndex>,
256     pub stability: Option<Lazy<attr::Stability>>,
257     pub deprecation: Option<Lazy<attr::Deprecation>>,
258
259     pub ty: Option<Lazy<Ty<'tcx>>>,
260     pub inherent_impls: LazySeq<DefIndex>,
261     pub variances: LazySeq<ty::Variance>,
262     pub generics: Option<Lazy<ty::Generics>>,
263     pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
264
265     pub ast: Option<Lazy<astencode::Ast<'tcx>>>,
266     pub mir: Option<Lazy<mir::Mir<'tcx>>>,
267 }
268
269 impl_stable_hash_for!(struct Entry<'tcx> {
270     kind,
271     visibility,
272     span,
273     attributes,
274     children,
275     stability,
276     deprecation,
277     ty,
278     inherent_impls,
279     variances,
280     generics,
281     predicates,
282     ast,
283     mir
284 });
285
286 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
287 pub enum EntryKind<'tcx> {
288     Const(u8),
289     ImmStatic,
290     MutStatic,
291     ForeignImmStatic,
292     ForeignMutStatic,
293     ForeignMod,
294     GlobalAsm,
295     Type,
296     Enum(ReprOptions),
297     Field,
298     Variant(Lazy<VariantData<'tcx>>),
299     Struct(Lazy<VariantData<'tcx>>, ReprOptions),
300     Union(Lazy<VariantData<'tcx>>, ReprOptions),
301     Fn(Lazy<FnData<'tcx>>),
302     ForeignFn(Lazy<FnData<'tcx>>),
303     Mod(Lazy<ModData>),
304     MacroDef(Lazy<MacroDef>),
305     Closure(Lazy<ClosureData<'tcx>>),
306     Generator(Lazy<GeneratorData<'tcx>>),
307     Trait(Lazy<TraitData<'tcx>>),
308     Impl(Lazy<ImplData<'tcx>>),
309     DefaultImpl(Lazy<ImplData<'tcx>>),
310     Method(Lazy<MethodData<'tcx>>),
311     AssociatedType(AssociatedContainer),
312     AssociatedConst(AssociatedContainer, u8),
313 }
314
315 impl<'gcx> HashStable<StableHashingContext<'gcx>> for EntryKind<'gcx> {
316     fn hash_stable<W: StableHasherResult>(&self,
317                                           hcx: &mut StableHashingContext<'gcx>,
318                                           hasher: &mut StableHasher<W>) {
319         mem::discriminant(self).hash_stable(hcx, hasher);
320         match *self {
321             EntryKind::ImmStatic        |
322             EntryKind::MutStatic        |
323             EntryKind::ForeignImmStatic |
324             EntryKind::ForeignMutStatic |
325             EntryKind::ForeignMod       |
326             EntryKind::GlobalAsm        |
327             EntryKind::Field |
328             EntryKind::Type => {
329                 // Nothing else to hash here.
330             }
331             EntryKind::Const(qualif) => {
332                 qualif.hash_stable(hcx, hasher);
333             }
334             EntryKind::Enum(ref repr_options) => {
335                 repr_options.hash_stable(hcx, hasher);
336             }
337             EntryKind::Variant(ref variant_data) => {
338                 variant_data.hash_stable(hcx, hasher);
339             }
340             EntryKind::Struct(ref variant_data, ref repr_options) |
341             EntryKind::Union(ref variant_data, ref repr_options)  => {
342                 variant_data.hash_stable(hcx, hasher);
343                 repr_options.hash_stable(hcx, hasher);
344             }
345             EntryKind::Fn(ref fn_data) |
346             EntryKind::ForeignFn(ref fn_data) => {
347                 fn_data.hash_stable(hcx, hasher);
348             }
349             EntryKind::Mod(ref mod_data) => {
350                 mod_data.hash_stable(hcx, hasher);
351             }
352             EntryKind::MacroDef(ref macro_def) => {
353                 macro_def.hash_stable(hcx, hasher);
354             }
355             EntryKind::Generator(data) => {
356                 data.hash_stable(hcx, hasher);
357             }
358             EntryKind::Closure(closure_data) => {
359                 closure_data.hash_stable(hcx, hasher);
360             }
361             EntryKind::Trait(ref trait_data) => {
362                 trait_data.hash_stable(hcx, hasher);
363             }
364             EntryKind::DefaultImpl(ref impl_data) |
365             EntryKind::Impl(ref impl_data) => {
366                 impl_data.hash_stable(hcx, hasher);
367             }
368             EntryKind::Method(ref method_data) => {
369                 method_data.hash_stable(hcx, hasher);
370             }
371             EntryKind::AssociatedType(associated_container) => {
372                 associated_container.hash_stable(hcx, hasher);
373             }
374             EntryKind::AssociatedConst(associated_container, qualif) => {
375                 associated_container.hash_stable(hcx, hasher);
376                 qualif.hash_stable(hcx, hasher);
377             }
378         }
379     }
380 }
381
382 #[derive(RustcEncodable, RustcDecodable)]
383 pub struct ModData {
384     pub reexports: LazySeq<def::Export>,
385 }
386
387 impl_stable_hash_for!(struct ModData { reexports });
388
389 #[derive(RustcEncodable, RustcDecodable)]
390 pub struct MacroDef {
391     pub body: String,
392     pub legacy: bool,
393 }
394
395 impl_stable_hash_for!(struct MacroDef { body, legacy });
396
397 #[derive(RustcEncodable, RustcDecodable)]
398 pub struct FnData<'tcx> {
399     pub constness: hir::Constness,
400     pub arg_names: LazySeq<ast::Name>,
401     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
402 }
403
404 impl_stable_hash_for!(struct FnData<'tcx> { constness, arg_names, sig });
405
406 #[derive(RustcEncodable, RustcDecodable)]
407 pub struct VariantData<'tcx> {
408     pub ctor_kind: CtorKind,
409     pub discr: ty::VariantDiscr,
410
411     /// If this is a struct's only variant, this
412     /// is the index of the "struct ctor" item.
413     pub struct_ctor: Option<DefIndex>,
414
415     /// If this is a tuple struct or variant
416     /// ctor, this is its "function" signature.
417     pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
418 }
419
420 impl_stable_hash_for!(struct VariantData<'tcx> {
421     ctor_kind,
422     discr,
423     struct_ctor,
424     ctor_sig
425 });
426
427 #[derive(RustcEncodable, RustcDecodable)]
428 pub struct TraitData<'tcx> {
429     pub unsafety: hir::Unsafety,
430     pub paren_sugar: bool,
431     pub has_default_impl: bool,
432     pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
433 }
434
435 impl_stable_hash_for!(struct TraitData<'tcx> {
436     unsafety,
437     paren_sugar,
438     has_default_impl,
439     super_predicates
440 });
441
442 #[derive(RustcEncodable, RustcDecodable)]
443 pub struct ImplData<'tcx> {
444     pub polarity: hir::ImplPolarity,
445     pub defaultness: hir::Defaultness,
446     pub parent_impl: Option<DefId>,
447
448     /// This is `Some` only for impls of `CoerceUnsized`.
449     pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
450     pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
451 }
452
453 impl_stable_hash_for!(struct ImplData<'tcx> {
454     polarity,
455     defaultness,
456     parent_impl,
457     coerce_unsized_info,
458     trait_ref
459 });
460
461
462 /// Describes whether the container of an associated item
463 /// is a trait or an impl and whether, in a trait, it has
464 /// a default, or an in impl, whether it's marked "default".
465 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
466 pub enum AssociatedContainer {
467     TraitRequired,
468     TraitWithDefault,
469     ImplDefault,
470     ImplFinal,
471 }
472
473 impl_stable_hash_for!(enum ::schema::AssociatedContainer {
474     TraitRequired,
475     TraitWithDefault,
476     ImplDefault,
477     ImplFinal
478 });
479
480 impl AssociatedContainer {
481     pub fn with_def_id(&self, def_id: DefId) -> ty::AssociatedItemContainer {
482         match *self {
483             AssociatedContainer::TraitRequired |
484             AssociatedContainer::TraitWithDefault => ty::TraitContainer(def_id),
485
486             AssociatedContainer::ImplDefault |
487             AssociatedContainer::ImplFinal => ty::ImplContainer(def_id),
488         }
489     }
490
491     pub fn defaultness(&self) -> hir::Defaultness {
492         match *self {
493             AssociatedContainer::TraitRequired => hir::Defaultness::Default {
494                 has_value: false,
495             },
496
497             AssociatedContainer::TraitWithDefault |
498             AssociatedContainer::ImplDefault => hir::Defaultness::Default {
499                 has_value: true,
500             },
501
502             AssociatedContainer::ImplFinal => hir::Defaultness::Final,
503         }
504     }
505 }
506
507 #[derive(RustcEncodable, RustcDecodable)]
508 pub struct MethodData<'tcx> {
509     pub fn_data: FnData<'tcx>,
510     pub container: AssociatedContainer,
511     pub has_self: bool,
512 }
513 impl_stable_hash_for!(struct MethodData<'tcx> { fn_data, container, has_self });
514
515 #[derive(RustcEncodable, RustcDecodable)]
516 pub struct ClosureData<'tcx> {
517     pub kind: ty::ClosureKind,
518     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
519 }
520 impl_stable_hash_for!(struct ClosureData<'tcx> { kind, sig });
521
522 #[derive(RustcEncodable, RustcDecodable)]
523 pub struct GeneratorData<'tcx> {
524     pub sig: ty::PolyGenSig<'tcx>,
525     pub layout: mir::GeneratorLayout<'tcx>,
526 }
527 impl_stable_hash_for!(struct GeneratorData<'tcx> { sig, layout });