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