]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/schema.rs
fc3af6cf2e7320a248b9cb15211142bddf5f797f
[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 index;
12
13 use rustc::hir;
14 use rustc::hir::def::{self, CtorKind};
15 use rustc::hir::def_id::{DefIndex, DefId, CrateNum};
16 use rustc::ich::StableHashingContext;
17 use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignModule};
18 use rustc::middle::lang_items;
19 use rustc::mir;
20 use rustc::session::CrateDisambiguator;
21 use rustc::ty::{self, Ty, ReprOptions};
22 use rustc_target::spec::{PanicStrategy, TargetTriple};
23 use rustc_data_structures::svh::Svh;
24
25 use rustc_serialize as serialize;
26 use syntax::{ast, attr};
27 use syntax::edition::Edition;
28 use syntax::symbol::Symbol;
29 use syntax_pos::{self, Span};
30
31 use std::marker::PhantomData;
32 use std::mem;
33
34 use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
35                                            StableHasherResult};
36
37 pub fn rustc_version() -> String {
38     format!("rustc {}",
39             option_env!("CFG_VERSION").unwrap_or("unknown version"))
40 }
41
42 /// Metadata encoding version.
43 /// NB: increment this if you change the format of metadata such that
44 /// the rustc version can't be found to compare with `rustc_version()`.
45 pub const METADATA_VERSION: u8 = 4;
46
47 /// Metadata header which includes `METADATA_VERSION`.
48 /// To get older versions of rustc to ignore this metadata,
49 /// there are 4 zero bytes at the start, which are treated
50 /// as a length of 0 by old compilers.
51 ///
52 /// This header is followed by the position of the `CrateRoot`,
53 /// which is encoded as a 32-bit big-endian unsigned integer,
54 /// and further followed by the rustc version string.
55 pub const METADATA_HEADER: &[u8; 12] =
56     &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
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,
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 impl<CTX, T> HashStable<CTX> for Lazy<T> {
105     fn hash_stable<W: StableHasherResult>(&self,
106                                           _: &mut CTX,
107                                           _: &mut StableHasher<W>) {
108         // There's nothing to do. Whatever got encoded within this Lazy<>
109         // wrapper has already been hashed.
110     }
111 }
112
113 /// A sequence of type T referred to by its absolute position
114 /// in the metadata and length, and which can be decoded lazily.
115 /// The sequence is a single node for the purposes of `Lazy`.
116 ///
117 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
118 /// position, not at the position, which means that the length
119 /// doesn't need to be known before encoding all the elements.
120 ///
121 /// If the length is 0, no position is encoded, but otherwise,
122 /// the encoding is that of `Lazy`, with the distinction that
123 /// the minimal distance the length of the sequence, i.e.
124 /// it's assumed there's no 0-byte element in the sequence.
125 #[must_use]
126 pub struct LazySeq<T> {
127     pub len: usize,
128     pub position: usize,
129     _marker: PhantomData<T>,
130 }
131
132 impl<T> LazySeq<T> {
133     pub fn empty() -> LazySeq<T> {
134         LazySeq::with_position_and_length(0, 0)
135     }
136
137     pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
138         LazySeq {
139             len,
140             position,
141             _marker: PhantomData,
142         }
143     }
144
145     /// Returns the minimum encoded size of `length` values of type `T`.
146     pub fn min_size(length: usize) -> usize {
147         length
148     }
149 }
150
151 impl<T> Copy for LazySeq<T> {}
152 impl<T> Clone for LazySeq<T> {
153     fn clone(&self) -> Self {
154         *self
155     }
156 }
157
158 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
159 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
160
161 impl<CTX, T> HashStable<CTX> for LazySeq<T> {
162     fn hash_stable<W: StableHasherResult>(&self,
163                                           _: &mut CTX,
164                                           _: &mut StableHasher<W>) {
165         // There's nothing to do. Whatever got encoded within this Lazy<>
166         // wrapper has already been hashed.
167     }
168 }
169
170 /// Encoding / decoding state for `Lazy` and `LazySeq`.
171 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
172 pub enum LazyState {
173     /// Outside of a metadata node.
174     NoNode,
175
176     /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
177     /// The position is that of the node itself.
178     NodeStart(usize),
179
180     /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
181     /// The position is a conservative estimate of where that
182     /// previous `Lazy` / `LazySeq` would end (see their comments).
183     Previous(usize),
184 }
185
186 #[derive(RustcEncodable, RustcDecodable)]
187 pub struct CrateRoot {
188     pub name: Symbol,
189     pub triple: TargetTriple,
190     pub extra_filename: String,
191     pub hash: Svh,
192     pub disambiguator: CrateDisambiguator,
193     pub panic_strategy: PanicStrategy,
194     pub edition: Edition,
195     pub has_global_allocator: bool,
196     pub has_panic_handler: bool,
197     pub has_default_lib_allocator: bool,
198     pub plugin_registrar_fn: Option<DefIndex>,
199     pub proc_macro_decls_static: Option<DefIndex>,
200
201     pub crate_deps: LazySeq<CrateDep>,
202     pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
203     pub lib_features: LazySeq<(Symbol, Option<Symbol>)>,
204     pub lang_items: LazySeq<(DefIndex, usize)>,
205     pub lang_items_missing: LazySeq<lang_items::LangItem>,
206     pub native_libraries: LazySeq<NativeLibrary>,
207     pub foreign_modules: LazySeq<ForeignModule>,
208     pub source_map: LazySeq<syntax_pos::SourceFile>,
209     pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
210     pub impls: LazySeq<TraitImpls>,
211     pub exported_symbols: EncodedExportedSymbols,
212     pub interpret_alloc_index: LazySeq<u32>,
213
214     pub index: LazySeq<index::Index>,
215
216     pub compiler_builtins: bool,
217     pub needs_allocator: bool,
218     pub needs_panic_runtime: bool,
219     pub no_builtins: bool,
220     pub panic_runtime: bool,
221     pub profiler_runtime: bool,
222     pub sanitizer_runtime: bool,
223 }
224
225 #[derive(RustcEncodable, RustcDecodable)]
226 pub struct CrateDep {
227     pub name: ast::Name,
228     pub hash: Svh,
229     pub kind: DepKind,
230     pub extra_filename: String,
231 }
232
233 impl_stable_hash_for!(struct CrateDep {
234     name,
235     hash,
236     kind,
237     extra_filename
238 });
239
240 #[derive(RustcEncodable, RustcDecodable)]
241 pub struct TraitImpls {
242     pub trait_id: (u32, DefIndex),
243     pub impls: LazySeq<DefIndex>,
244 }
245
246 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TraitImpls {
247     fn hash_stable<W: StableHasherResult>(&self,
248                                           hcx: &mut StableHashingContext<'a>,
249                                           hasher: &mut StableHasher<W>) {
250         let TraitImpls {
251             trait_id: (krate, def_index),
252             ref impls,
253         } = *self;
254
255         DefId {
256             krate: CrateNum::from_u32(krate),
257             index: def_index
258         }.hash_stable(hcx, hasher);
259         impls.hash_stable(hcx, hasher);
260     }
261 }
262
263 #[derive(RustcEncodable, RustcDecodable)]
264 pub struct Entry<'tcx> {
265     pub kind: EntryKind<'tcx>,
266     pub visibility: Lazy<ty::Visibility>,
267     pub span: Lazy<Span>,
268     pub attributes: LazySeq<ast::Attribute>,
269     pub children: LazySeq<DefIndex>,
270     pub stability: Option<Lazy<attr::Stability>>,
271     pub deprecation: Option<Lazy<attr::Deprecation>>,
272
273     pub ty: Option<Lazy<Ty<'tcx>>>,
274     pub inherent_impls: LazySeq<DefIndex>,
275     pub variances: LazySeq<ty::Variance>,
276     pub generics: Option<Lazy<ty::Generics>>,
277     pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
278     pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
279
280     pub mir: Option<Lazy<mir::Mir<'tcx>>>,
281 }
282
283 impl_stable_hash_for!(struct Entry<'tcx> {
284     kind,
285     visibility,
286     span,
287     attributes,
288     children,
289     stability,
290     deprecation,
291     ty,
292     inherent_impls,
293     variances,
294     generics,
295     predicates,
296     predicates_defined_on,
297     mir
298 });
299
300 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
301 pub enum EntryKind<'tcx> {
302     Const(ConstQualif, Lazy<RenderedConst>),
303     ImmStatic,
304     MutStatic,
305     ForeignImmStatic,
306     ForeignMutStatic,
307     ForeignMod,
308     ForeignType,
309     GlobalAsm,
310     Type,
311     Existential,
312     Enum(ReprOptions),
313     Field,
314     Variant(Lazy<VariantData<'tcx>>),
315     Struct(Lazy<VariantData<'tcx>>, ReprOptions),
316     Union(Lazy<VariantData<'tcx>>, ReprOptions),
317     Fn(Lazy<FnData<'tcx>>),
318     ForeignFn(Lazy<FnData<'tcx>>),
319     Mod(Lazy<ModData>),
320     MacroDef(Lazy<MacroDef>),
321     Closure(Lazy<ClosureData<'tcx>>),
322     Generator(Lazy<GeneratorData<'tcx>>),
323     Trait(Lazy<TraitData<'tcx>>),
324     Impl(Lazy<ImplData<'tcx>>),
325     Method(Lazy<MethodData<'tcx>>),
326     AssociatedType(AssociatedContainer),
327     AssociatedExistential(AssociatedContainer),
328     AssociatedConst(AssociatedContainer, ConstQualif, Lazy<RenderedConst>),
329 }
330
331 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for EntryKind<'gcx> {
332     fn hash_stable<W: StableHasherResult>(&self,
333                                           hcx: &mut StableHashingContext<'a>,
334                                           hasher: &mut StableHasher<W>) {
335         mem::discriminant(self).hash_stable(hcx, hasher);
336         match *self {
337             EntryKind::ImmStatic        |
338             EntryKind::MutStatic        |
339             EntryKind::ForeignImmStatic |
340             EntryKind::ForeignMutStatic |
341             EntryKind::ForeignMod       |
342             EntryKind::GlobalAsm        |
343             EntryKind::ForeignType      |
344             EntryKind::Field |
345             EntryKind::Existential |
346             EntryKind::Type => {
347                 // Nothing else to hash here.
348             }
349             EntryKind::Const(qualif, ref const_data) => {
350                 qualif.hash_stable(hcx, hasher);
351                 const_data.hash_stable(hcx, hasher);
352             }
353             EntryKind::Enum(ref repr_options) => {
354                 repr_options.hash_stable(hcx, hasher);
355             }
356             EntryKind::Variant(ref variant_data) => {
357                 variant_data.hash_stable(hcx, hasher);
358             }
359             EntryKind::Struct(ref variant_data, ref repr_options) |
360             EntryKind::Union(ref variant_data, ref repr_options)  => {
361                 variant_data.hash_stable(hcx, hasher);
362                 repr_options.hash_stable(hcx, hasher);
363             }
364             EntryKind::Fn(ref fn_data) |
365             EntryKind::ForeignFn(ref fn_data) => {
366                 fn_data.hash_stable(hcx, hasher);
367             }
368             EntryKind::Mod(ref mod_data) => {
369                 mod_data.hash_stable(hcx, hasher);
370             }
371             EntryKind::MacroDef(ref macro_def) => {
372                 macro_def.hash_stable(hcx, hasher);
373             }
374             EntryKind::Generator(data) => {
375                 data.hash_stable(hcx, hasher);
376             }
377             EntryKind::Closure(closure_data) => {
378                 closure_data.hash_stable(hcx, hasher);
379             }
380             EntryKind::Trait(ref trait_data) => {
381                 trait_data.hash_stable(hcx, hasher);
382             }
383             EntryKind::Impl(ref impl_data) => {
384                 impl_data.hash_stable(hcx, hasher);
385             }
386             EntryKind::Method(ref method_data) => {
387                 method_data.hash_stable(hcx, hasher);
388             }
389             EntryKind::AssociatedExistential(associated_container) |
390             EntryKind::AssociatedType(associated_container) => {
391                 associated_container.hash_stable(hcx, hasher);
392             }
393             EntryKind::AssociatedConst(associated_container, qualif, ref const_data) => {
394                 associated_container.hash_stable(hcx, hasher);
395                 qualif.hash_stable(hcx, hasher);
396                 const_data.hash_stable(hcx, hasher);
397             }
398         }
399     }
400 }
401
402 /// Additional data for EntryKind::Const and EntryKind::AssociatedConst
403 #[derive(Clone, Copy, RustcEncodable, RustcDecodable)]
404 pub struct ConstQualif {
405     pub mir: u8,
406     pub ast_promotable: bool,
407 }
408
409 impl_stable_hash_for!(struct ConstQualif { mir, ast_promotable });
410
411 /// Contains a constant which has been rendered to a String.
412 /// Used by rustdoc.
413 #[derive(RustcEncodable, RustcDecodable)]
414 pub struct RenderedConst(pub String);
415
416 impl<'a> HashStable<StableHashingContext<'a>> for RenderedConst {
417     #[inline]
418     fn hash_stable<W: StableHasherResult>(&self,
419                                           hcx: &mut StableHashingContext<'a>,
420                                           hasher: &mut StableHasher<W>) {
421         self.0.hash_stable(hcx, hasher);
422     }
423 }
424
425 #[derive(RustcEncodable, RustcDecodable)]
426 pub struct ModData {
427     pub reexports: LazySeq<def::Export>,
428 }
429
430 impl_stable_hash_for!(struct ModData { reexports });
431
432 #[derive(RustcEncodable, RustcDecodable)]
433 pub struct MacroDef {
434     pub body: String,
435     pub legacy: bool,
436 }
437
438 impl_stable_hash_for!(struct MacroDef { body, legacy });
439
440 #[derive(RustcEncodable, RustcDecodable)]
441 pub struct FnData<'tcx> {
442     pub constness: hir::Constness,
443     pub arg_names: LazySeq<ast::Name>,
444     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
445 }
446
447 impl_stable_hash_for!(struct FnData<'tcx> { constness, arg_names, sig });
448
449 #[derive(RustcEncodable, RustcDecodable)]
450 pub struct VariantData<'tcx> {
451     pub ctor_kind: CtorKind,
452     pub discr: ty::VariantDiscr,
453
454     /// If this is a struct's only variant, this
455     /// is the index of the "struct ctor" item.
456     pub struct_ctor: Option<DefIndex>,
457
458     /// If this is a tuple struct or variant
459     /// ctor, this is its "function" signature.
460     pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
461 }
462
463 impl_stable_hash_for!(struct VariantData<'tcx> {
464     ctor_kind,
465     discr,
466     struct_ctor,
467     ctor_sig
468 });
469
470 #[derive(RustcEncodable, RustcDecodable)]
471 pub struct TraitData<'tcx> {
472     pub unsafety: hir::Unsafety,
473     pub paren_sugar: bool,
474     pub has_auto_impl: bool,
475     pub is_marker: bool,
476     pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
477 }
478
479 impl_stable_hash_for!(struct TraitData<'tcx> {
480     unsafety,
481     paren_sugar,
482     has_auto_impl,
483     is_marker,
484     super_predicates
485 });
486
487 #[derive(RustcEncodable, RustcDecodable)]
488 pub struct ImplData<'tcx> {
489     pub polarity: hir::ImplPolarity,
490     pub defaultness: hir::Defaultness,
491     pub parent_impl: Option<DefId>,
492
493     /// This is `Some` only for impls of `CoerceUnsized`.
494     pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
495     pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
496 }
497
498 impl_stable_hash_for!(struct ImplData<'tcx> {
499     polarity,
500     defaultness,
501     parent_impl,
502     coerce_unsized_info,
503     trait_ref
504 });
505
506
507 /// Describes whether the container of an associated item
508 /// is a trait or an impl and whether, in a trait, it has
509 /// a default, or an in impl, whether it's marked "default".
510 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
511 pub enum AssociatedContainer {
512     TraitRequired,
513     TraitWithDefault,
514     ImplDefault,
515     ImplFinal,
516 }
517
518 impl_stable_hash_for!(enum ::schema::AssociatedContainer {
519     TraitRequired,
520     TraitWithDefault,
521     ImplDefault,
522     ImplFinal
523 });
524
525 impl AssociatedContainer {
526     pub fn with_def_id(&self, def_id: DefId) -> ty::AssociatedItemContainer {
527         match *self {
528             AssociatedContainer::TraitRequired |
529             AssociatedContainer::TraitWithDefault => ty::TraitContainer(def_id),
530
531             AssociatedContainer::ImplDefault |
532             AssociatedContainer::ImplFinal => ty::ImplContainer(def_id),
533         }
534     }
535
536     pub fn defaultness(&self) -> hir::Defaultness {
537         match *self {
538             AssociatedContainer::TraitRequired => hir::Defaultness::Default {
539                 has_value: false,
540             },
541
542             AssociatedContainer::TraitWithDefault |
543             AssociatedContainer::ImplDefault => hir::Defaultness::Default {
544                 has_value: true,
545             },
546
547             AssociatedContainer::ImplFinal => hir::Defaultness::Final,
548         }
549     }
550 }
551
552 #[derive(RustcEncodable, RustcDecodable)]
553 pub struct MethodData<'tcx> {
554     pub fn_data: FnData<'tcx>,
555     pub container: AssociatedContainer,
556     pub has_self: bool,
557 }
558 impl_stable_hash_for!(struct MethodData<'tcx> { fn_data, container, has_self });
559
560 #[derive(RustcEncodable, RustcDecodable)]
561 pub struct ClosureData<'tcx> {
562     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
563 }
564 impl_stable_hash_for!(struct ClosureData<'tcx> { sig });
565
566 #[derive(RustcEncodable, RustcDecodable)]
567 pub struct GeneratorData<'tcx> {
568     pub layout: mir::GeneratorLayout<'tcx>,
569 }
570 impl_stable_hash_for!(struct GeneratorData<'tcx> { layout });
571
572 // Tags used for encoding Spans:
573 pub const TAG_VALID_SPAN: u8 = 0;
574 pub const TAG_INVALID_SPAN: u8 = 1;
575
576 #[derive(RustcEncodable, RustcDecodable)]
577 pub struct EncodedExportedSymbols {
578     pub position: usize,
579     pub len: usize,
580 }