]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/schema.rs
Rollup merge of #61409 - varkor:condition-trait-param-ice, r=oli-obk
[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};
6 use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
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::session::config::SymbolManglingVersion;
12 use rustc::ty::{self, Ty, ReprOptions};
13 use rustc_target::spec::{PanicStrategy, TargetTriple};
14 use rustc_data_structures::svh::Svh;
15
16 use rustc_serialize as serialize;
17 use syntax::{ast, attr};
18 use syntax::edition::Edition;
19 use syntax::symbol::Symbol;
20 use syntax_pos::{self, Span};
21
22 use std::marker::PhantomData;
23
24 pub fn rustc_version() -> String {
25     format!("rustc {}",
26             option_env!("CFG_VERSION").unwrap_or("unknown version"))
27 }
28
29 /// Metadata encoding version.
30 /// N.B., increment this if you change the format of metadata such that
31 /// the rustc version can't be found to compare with `rustc_version()`.
32 pub const METADATA_VERSION: u8 = 4;
33
34 /// Metadata header which includes `METADATA_VERSION`.
35 /// To get older versions of rustc to ignore this metadata,
36 /// there are 4 zero bytes at the start, which are treated
37 /// as a length of 0 by old compilers.
38 ///
39 /// This header is followed by the position of the `CrateRoot`,
40 /// which is encoded as a 32-bit big-endian unsigned integer,
41 /// and further followed by the rustc version string.
42 pub const METADATA_HEADER: &[u8; 12] =
43     &[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
44
45 /// A value of type T referred to by its absolute position
46 /// in the metadata, and which can be decoded lazily.
47 ///
48 /// Metadata is effective a tree, encoded in post-order,
49 /// and with the root's position written next to the header.
50 /// That means every single `Lazy` points to some previous
51 /// location in the metadata and is part of a larger node.
52 ///
53 /// The first `Lazy` in a node is encoded as the backwards
54 /// distance from the position where the containing node
55 /// starts and where the `Lazy` points to, while the rest
56 /// use the forward distance from the previous `Lazy`.
57 /// Distances start at 1, as 0-byte nodes are invalid.
58 /// Also invalid are nodes being referred in a different
59 /// order than they were encoded in.
60 #[must_use]
61 pub struct Lazy<T> {
62     pub position: usize,
63     _marker: PhantomData<T>,
64 }
65
66 impl<T> Lazy<T> {
67     pub fn with_position(position: usize) -> Lazy<T> {
68         Lazy {
69             position,
70             _marker: PhantomData,
71         }
72     }
73
74     /// Returns the minimum encoded size of a value of type `T`.
75     // FIXME(eddyb) Give better estimates for certain types.
76     pub fn min_size() -> usize {
77         1
78     }
79 }
80
81 impl<T> Copy for Lazy<T> {}
82 impl<T> Clone for Lazy<T> {
83     fn clone(&self) -> Self {
84         *self
85     }
86 }
87
88 impl<T> serialize::UseSpecializedEncodable for Lazy<T> {}
89 impl<T> serialize::UseSpecializedDecodable for Lazy<T> {}
90
91 /// A sequence of type T referred to by its absolute position
92 /// in the metadata and length, and which can be decoded lazily.
93 /// The sequence is a single node for the purposes of `Lazy`.
94 ///
95 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
96 /// position, not at the position, which means that the length
97 /// doesn't need to be known before encoding all the elements.
98 ///
99 /// If the length is 0, no position is encoded, but otherwise,
100 /// the encoding is that of `Lazy`, with the distinction that
101 /// the minimal distance the length of the sequence, i.e.
102 /// it's assumed there's no 0-byte element in the sequence.
103 #[must_use]
104 pub struct LazySeq<T> {
105     pub len: usize,
106     pub position: usize,
107     _marker: PhantomData<T>,
108 }
109
110 impl<T> LazySeq<T> {
111     pub fn empty() -> LazySeq<T> {
112         LazySeq::with_position_and_length(0, 0)
113     }
114
115     pub fn with_position_and_length(position: usize, len: usize) -> LazySeq<T> {
116         LazySeq {
117             len,
118             position,
119             _marker: PhantomData,
120         }
121     }
122
123     /// Returns the minimum encoded size of `length` values of type `T`.
124     pub fn min_size(length: usize) -> usize {
125         length
126     }
127 }
128
129 impl<T> Copy for LazySeq<T> {}
130 impl<T> Clone for LazySeq<T> {
131     fn clone(&self) -> Self {
132         *self
133     }
134 }
135
136 impl<T> serialize::UseSpecializedEncodable for LazySeq<T> {}
137 impl<T> serialize::UseSpecializedDecodable for LazySeq<T> {}
138
139 /// Encoding / decoding state for `Lazy` and `LazySeq`.
140 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
141 pub enum LazyState {
142     /// Outside of a metadata node.
143     NoNode,
144
145     /// Inside a metadata node, and before any `Lazy` or `LazySeq`.
146     /// The position is that of the node itself.
147     NodeStart(usize),
148
149     /// Inside a metadata node, with a previous `Lazy` or `LazySeq`.
150     /// The position is a conservative estimate of where that
151     /// previous `Lazy` / `LazySeq` would end (see their comments).
152     Previous(usize),
153 }
154
155 #[derive(RustcEncodable, RustcDecodable)]
156 pub struct CrateRoot<'tcx> {
157     pub name: Symbol,
158     pub triple: TargetTriple,
159     pub extra_filename: String,
160     pub hash: Svh,
161     pub disambiguator: CrateDisambiguator,
162     pub panic_strategy: PanicStrategy,
163     pub edition: Edition,
164     pub has_global_allocator: bool,
165     pub has_panic_handler: bool,
166     pub has_default_lib_allocator: bool,
167     pub plugin_registrar_fn: Option<DefIndex>,
168     pub proc_macro_decls_static: Option<DefIndex>,
169     pub proc_macro_stability: Option<attr::Stability>,
170
171     pub crate_deps: LazySeq<CrateDep>,
172     pub dylib_dependency_formats: LazySeq<Option<LinkagePreference>>,
173     pub lib_features: LazySeq<(Symbol, Option<Symbol>)>,
174     pub lang_items: LazySeq<(DefIndex, usize)>,
175     pub lang_items_missing: LazySeq<lang_items::LangItem>,
176     pub native_libraries: LazySeq<NativeLibrary>,
177     pub foreign_modules: LazySeq<ForeignModule>,
178     pub source_map: LazySeq<syntax_pos::SourceFile>,
179     pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
180     pub impls: LazySeq<TraitImpls>,
181     pub exported_symbols: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)>,
182     pub interpret_alloc_index: LazySeq<u32>,
183
184     pub entries_index: LazySeq<index::Index<'tcx>>,
185
186     pub compiler_builtins: bool,
187     pub needs_allocator: bool,
188     pub needs_panic_runtime: bool,
189     pub no_builtins: bool,
190     pub panic_runtime: bool,
191     pub profiler_runtime: bool,
192     pub sanitizer_runtime: bool,
193     pub symbol_mangling_version: SymbolManglingVersion,
194 }
195
196 #[derive(RustcEncodable, RustcDecodable)]
197 pub struct CrateDep {
198     pub name: ast::Name,
199     pub hash: Svh,
200     pub kind: DepKind,
201     pub extra_filename: String,
202 }
203
204 #[derive(RustcEncodable, RustcDecodable)]
205 pub struct TraitImpls {
206     pub trait_id: (u32, DefIndex),
207     pub impls: LazySeq<DefIndex>,
208 }
209
210 #[derive(RustcEncodable, RustcDecodable)]
211 pub struct Entry<'tcx> {
212     pub kind: EntryKind<'tcx>,
213     pub visibility: Lazy<ty::Visibility>,
214     pub span: Lazy<Span>,
215     pub attributes: LazySeq<ast::Attribute>,
216     pub children: LazySeq<DefIndex>,
217     pub stability: Option<Lazy<attr::Stability>>,
218     pub deprecation: Option<Lazy<attr::Deprecation>>,
219
220     pub ty: Option<Lazy<Ty<'tcx>>>,
221     pub inherent_impls: LazySeq<DefIndex>,
222     pub variances: LazySeq<ty::Variance>,
223     pub generics: Option<Lazy<ty::Generics>>,
224     pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
225     pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
226
227     pub mir: Option<Lazy<mir::Body<'tcx>>>,
228 }
229
230 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
231 pub enum EntryKind<'tcx> {
232     Const(ConstQualif, Lazy<RenderedConst>),
233     ImmStatic,
234     MutStatic,
235     ForeignImmStatic,
236     ForeignMutStatic,
237     ForeignMod,
238     ForeignType,
239     GlobalAsm,
240     Type,
241     TypeParam,
242     ConstParam,
243     Existential,
244     Enum(ReprOptions),
245     Field,
246     Variant(Lazy<VariantData<'tcx>>),
247     Struct(Lazy<VariantData<'tcx>>, ReprOptions),
248     Union(Lazy<VariantData<'tcx>>, ReprOptions),
249     Fn(Lazy<FnData<'tcx>>),
250     ForeignFn(Lazy<FnData<'tcx>>),
251     Mod(Lazy<ModData>),
252     MacroDef(Lazy<MacroDef>),
253     Closure(Lazy<ClosureData<'tcx>>),
254     Generator(Lazy<GeneratorData<'tcx>>),
255     Trait(Lazy<TraitData<'tcx>>),
256     Impl(Lazy<ImplData<'tcx>>),
257     Method(Lazy<MethodData<'tcx>>),
258     AssocType(AssocContainer),
259     AssocExistential(AssocContainer),
260     AssocConst(AssocContainer, ConstQualif, Lazy<RenderedConst>),
261     TraitAlias(Lazy<TraitAliasData<'tcx>>),
262 }
263
264 /// Additional data for EntryKind::Const and EntryKind::AssocConst
265 #[derive(Clone, Copy, RustcEncodable, RustcDecodable)]
266 pub struct ConstQualif {
267     pub mir: u8,
268     pub ast_promotable: bool,
269 }
270
271 /// Contains a constant which has been rendered to a String.
272 /// Used by rustdoc.
273 #[derive(RustcEncodable, RustcDecodable)]
274 pub struct RenderedConst(pub String);
275
276 #[derive(RustcEncodable, RustcDecodable)]
277 pub struct ModData {
278     pub reexports: LazySeq<def::Export<hir::HirId>>,
279 }
280
281 #[derive(RustcEncodable, RustcDecodable)]
282 pub struct MacroDef {
283     pub body: String,
284     pub legacy: bool,
285 }
286
287 #[derive(RustcEncodable, RustcDecodable)]
288 pub struct FnData<'tcx> {
289     pub constness: hir::Constness,
290     pub arg_names: LazySeq<ast::Name>,
291     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
292 }
293
294 #[derive(RustcEncodable, RustcDecodable)]
295 pub struct VariantData<'tcx> {
296     pub ctor_kind: CtorKind,
297     pub discr: ty::VariantDiscr,
298     /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
299     pub ctor: Option<DefIndex>,
300     /// If this is a tuple struct or variant
301     /// ctor, this is its "function" signature.
302     pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
303 }
304
305 #[derive(RustcEncodable, RustcDecodable)]
306 pub struct TraitData<'tcx> {
307     pub unsafety: hir::Unsafety,
308     pub paren_sugar: bool,
309     pub has_auto_impl: bool,
310     pub is_marker: bool,
311     pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
312 }
313
314 #[derive(RustcEncodable, RustcDecodable)]
315 pub struct TraitAliasData<'tcx> {
316     pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
317 }
318
319 #[derive(RustcEncodable, RustcDecodable)]
320 pub struct ImplData<'tcx> {
321     pub polarity: hir::ImplPolarity,
322     pub defaultness: hir::Defaultness,
323     pub parent_impl: Option<DefId>,
324
325     /// This is `Some` only for impls of `CoerceUnsized`.
326     pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
327     pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
328 }
329
330
331 /// Describes whether the container of an associated item
332 /// is a trait or an impl and whether, in a trait, it has
333 /// a default, or an in impl, whether it's marked "default".
334 #[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
335 pub enum AssocContainer {
336     TraitRequired,
337     TraitWithDefault,
338     ImplDefault,
339     ImplFinal,
340 }
341
342 impl AssocContainer {
343     pub fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
344         match *self {
345             AssocContainer::TraitRequired |
346             AssocContainer::TraitWithDefault => ty::TraitContainer(def_id),
347
348             AssocContainer::ImplDefault |
349             AssocContainer::ImplFinal => ty::ImplContainer(def_id),
350         }
351     }
352
353     pub fn defaultness(&self) -> hir::Defaultness {
354         match *self {
355             AssocContainer::TraitRequired => hir::Defaultness::Default {
356                 has_value: false,
357             },
358
359             AssocContainer::TraitWithDefault |
360             AssocContainer::ImplDefault => hir::Defaultness::Default {
361                 has_value: true,
362             },
363
364             AssocContainer::ImplFinal => hir::Defaultness::Final,
365         }
366     }
367 }
368
369 #[derive(RustcEncodable, RustcDecodable)]
370 pub struct MethodData<'tcx> {
371     pub fn_data: FnData<'tcx>,
372     pub container: AssocContainer,
373     pub has_self: bool,
374 }
375
376 #[derive(RustcEncodable, RustcDecodable)]
377 pub struct ClosureData<'tcx> {
378     pub sig: Lazy<ty::PolyFnSig<'tcx>>,
379 }
380
381 #[derive(RustcEncodable, RustcDecodable)]
382 pub struct GeneratorData<'tcx> {
383     pub layout: mir::GeneratorLayout<'tcx>,
384 }
385
386 // Tags used for encoding Spans:
387 pub const TAG_VALID_SPAN: u8 = 0;
388 pub const TAG_INVALID_SPAN: u8 = 1;