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