1 use crate::creader::CrateMetadataRef;
3 use def_path_hash_map::DefPathHashMapRef;
4 use table::TableBuilder;
7 use rustc_attr as attr;
8 use rustc_data_structures::svh::Svh;
9 use rustc_data_structures::sync::MetadataRef;
11 use rustc_hir::def::{CtorKind, DefKind};
12 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId};
13 use rustc_hir::definitions::DefKey;
14 use rustc_hir::lang_items;
15 use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
16 use rustc_middle::metadata::ModChild;
17 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
18 use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
19 use rustc_middle::mir;
20 use rustc_middle::thir;
21 use rustc_middle::ty::fast_reject::SimplifiedType;
22 use rustc_middle::ty::query::Providers;
23 use rustc_middle::ty::{self, ReprOptions, Ty};
24 use rustc_middle::ty::{GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt};
25 use rustc_serialize::opaque::Encoder;
26 use rustc_session::config::SymbolManglingVersion;
27 use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
28 use rustc_span::edition::Edition;
29 use rustc_span::hygiene::{ExpnIndex, MacroKind};
30 use rustc_span::symbol::{Ident, Symbol};
31 use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
32 use rustc_target::spec::{PanicStrategy, TargetTriple};
34 use std::marker::PhantomData;
35 use std::num::NonZeroUsize;
37 pub use decoder::provide_extern;
38 use decoder::DecodeContext;
39 pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob};
40 use encoder::EncodeContext;
41 pub use encoder::{encode_metadata, EncodedMetadata};
42 use rustc_span::hygiene::SyntaxContextData;
45 mod def_path_hash_map;
49 pub(crate) fn rustc_version() -> String {
50 format!("rustc {}", option_env!("CFG_VERSION").unwrap_or("unknown version"))
53 /// Metadata encoding version.
54 /// N.B., increment this if you change the format of metadata such that
55 /// the rustc version can't be found to compare with `rustc_version()`.
56 const METADATA_VERSION: u8 = 6;
58 /// Metadata header which includes `METADATA_VERSION`.
60 /// This header is followed by the position of the `CrateRoot`,
61 /// which is encoded as a 32-bit big-endian unsigned integer,
62 /// and further followed by the rustc version string.
63 pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
65 /// A value of type T referred to by its absolute position
66 /// in the metadata, and which can be decoded lazily.
68 /// Metadata is effective a tree, encoded in post-order,
69 /// and with the root's position written next to the header.
70 /// That means every single `Lazy` points to some previous
71 /// location in the metadata and is part of a larger node.
73 /// The first `Lazy` in a node is encoded as the backwards
74 /// distance from the position where the containing node
75 /// starts and where the `Lazy` points to, while the rest
76 /// use the forward distance from the previous `Lazy`.
77 /// Distances start at 1, as 0-byte nodes are invalid.
78 /// Also invalid are nodes being referred in a different
79 /// order than they were encoded in.
82 position: NonZeroUsize,
83 _marker: PhantomData<fn() -> T>,
86 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
87 type Value<'tcx> = LazyValue<T::Value<'tcx>>;
90 impl<T> LazyValue<T> {
91 fn from_position(position: NonZeroUsize) -> LazyValue<T> {
92 LazyValue { position, _marker: PhantomData }
96 /// A list of lazily-decoded values.
98 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
99 /// position, not at the position, which means that the length
100 /// doesn't need to be known before encoding all the elements.
102 /// If the length is 0, no position is encoded, but otherwise,
103 /// the encoding is that of `Lazy`, with the distinction that
104 /// the minimal distance the length of the sequence, i.e.
105 /// it's assumed there's no 0-byte element in the sequence.
106 struct LazyArray<T> {
107 position: NonZeroUsize,
109 _marker: PhantomData<fn() -> T>,
112 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
113 type Value<'tcx> = LazyArray<T::Value<'tcx>>;
116 impl<T> LazyArray<T> {
117 fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
118 LazyArray { position, num_elems, _marker: PhantomData }
121 fn empty() -> LazyArray<T> {
122 LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
126 /// A list of lazily-decoded values, with the added capability of random access.
128 /// Random-access table (i.e. offering constant-time `get`/`set`), similar to
129 /// `LazyArray<T>`, but without requiring encoding or decoding all the values
130 /// eagerly and in-order.
131 struct LazyTable<I, T> {
132 position: NonZeroUsize,
134 _marker: PhantomData<fn(I) -> T>,
137 impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
138 type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
141 impl<I, T> LazyTable<I, T> {
142 fn from_position_and_encoded_size(
143 position: NonZeroUsize,
145 ) -> LazyTable<I, T> {
146 LazyTable { position, encoded_size, _marker: PhantomData }
150 impl<T> Copy for LazyValue<T> {}
151 impl<T> Clone for LazyValue<T> {
152 fn clone(&self) -> Self {
157 impl<T> Copy for LazyArray<T> {}
158 impl<T> Clone for LazyArray<T> {
159 fn clone(&self) -> Self {
164 impl<I, T> Copy for LazyTable<I, T> {}
165 impl<I, T> Clone for LazyTable<I, T> {
166 fn clone(&self) -> Self {
171 /// Encoding / decoding state for `Lazy`.
172 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
174 /// Outside of a metadata node.
177 /// Inside a metadata node, and before any `Lazy`.
178 /// The position is that of the node itself.
179 NodeStart(NonZeroUsize),
181 /// Inside a metadata node, with a previous `Lazy`.
182 /// The position is where that previous `Lazy` would start.
183 Previous(NonZeroUsize),
186 type SyntaxContextTable = LazyTable<u32, LazyValue<SyntaxContextData>>;
187 type ExpnDataTable = LazyTable<ExpnIndex, LazyValue<ExpnData>>;
188 type ExpnHashTable = LazyTable<ExpnIndex, LazyValue<ExpnHash>>;
190 #[derive(MetadataEncodable, MetadataDecodable)]
191 pub(crate) struct ProcMacroData {
192 proc_macro_decls_static: DefIndex,
193 stability: Option<attr::Stability>,
194 macros: LazyArray<DefIndex>,
197 /// Serialized metadata for a crate.
198 /// When compiling a proc-macro crate, we encode many of
199 /// the `LazyArray<T>` fields as `Lazy::empty()`. This serves two purposes:
201 /// 1. We avoid performing unnecessary work. Proc-macro crates can only
202 /// export proc-macros functions, which are compiled into a shared library.
203 /// As a result, a large amount of the information we normally store
204 /// (e.g. optimized MIR) is unneeded by downstream crates.
205 /// 2. We avoid serializing invalid `CrateNum`s. When we deserialize
206 /// a proc-macro crate, we don't load any of its dependencies (since we
207 /// just need to invoke a native function from the shared library).
208 /// This means that any foreign `CrateNum`s that we serialize cannot be
209 /// deserialized, since we will not know how to map them into the current
210 /// compilation session. If we were to serialize a proc-macro crate like
211 /// a normal crate, much of what we serialized would be unusable in addition
213 #[derive(MetadataEncodable, MetadataDecodable)]
214 pub(crate) struct CrateRoot {
216 triple: TargetTriple,
217 extra_filename: String,
219 stable_crate_id: StableCrateId,
220 panic_strategy: PanicStrategy,
221 panic_in_drop_strategy: PanicStrategy,
223 has_global_allocator: bool,
224 has_panic_handler: bool,
225 has_default_lib_allocator: bool,
227 crate_deps: LazyArray<CrateDep>,
228 dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
229 lib_features: LazyArray<(Symbol, Option<Symbol>)>,
230 lang_items: LazyArray<(DefIndex, usize)>,
231 lang_items_missing: LazyArray<lang_items::LangItem>,
232 diagnostic_items: LazyArray<(Symbol, DefIndex)>,
233 native_libraries: LazyArray<NativeLib>,
234 foreign_modules: LazyArray<ForeignModule>,
235 traits: LazyArray<DefIndex>,
236 impls: LazyArray<TraitImpls>,
237 incoherent_impls: LazyArray<IncoherentImpls>,
238 interpret_alloc_index: LazyArray<u32>,
239 proc_macro_data: Option<ProcMacroData>,
242 debugger_visualizers: LazyArray<rustc_span::DebuggerVisualizerFile>,
244 exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
246 syntax_contexts: SyntaxContextTable,
247 expn_data: ExpnDataTable,
248 expn_hashes: ExpnHashTable,
250 def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
252 source_map: LazyArray<rustc_span::SourceFile>,
254 compiler_builtins: bool,
255 needs_allocator: bool,
256 needs_panic_runtime: bool,
259 profiler_runtime: bool,
260 symbol_mangling_version: SymbolManglingVersion,
263 /// On-disk representation of `DefId`.
264 /// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk
265 /// representation and the compilation session.
266 #[derive(Copy, Clone)]
267 pub(crate) struct RawDefId {
272 impl Into<RawDefId> for DefId {
273 fn into(self) -> RawDefId {
274 RawDefId { krate: self.krate.as_u32(), index: self.index.as_u32() }
279 /// This exists so that `provide_one!` is happy
280 fn decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId {
281 self.decode_from_cdata(meta.0)
284 fn decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId {
285 let krate = CrateNum::from_u32(self.krate);
286 let krate = cdata.map_encoded_cnum_to_current(krate);
287 DefId { krate, index: DefIndex::from_u32(self.index) }
291 #[derive(Encodable, Decodable)]
292 pub(crate) struct CrateDep {
295 pub host_hash: Option<Svh>,
296 pub kind: CrateDepKind,
297 pub extra_filename: String,
300 #[derive(MetadataEncodable, MetadataDecodable)]
301 pub(crate) struct TraitImpls {
302 trait_id: (u32, DefIndex),
303 impls: LazyArray<(DefIndex, Option<SimplifiedType>)>,
306 #[derive(MetadataEncodable, MetadataDecodable)]
307 pub(crate) struct IncoherentImpls {
308 self_ty: SimplifiedType,
309 impls: LazyArray<DefIndex>,
312 /// Define `LazyTables` and `TableBuilders` at the same time.
313 macro_rules! define_tables {
314 ($($name:ident: Table<$IDX:ty, $T:ty>),+ $(,)?) => {
315 #[derive(MetadataEncodable, MetadataDecodable)]
316 pub(crate) struct LazyTables {
317 $($name: LazyTable<$IDX, $T>),+
321 struct TableBuilders {
322 $($name: TableBuilder<$IDX, $T>),+
326 fn encode(&self, buf: &mut Encoder) -> LazyTables {
328 $($name: self.$name.encode(buf)),+
336 kind: Table<DefIndex, LazyValue<EntryKind>>,
337 attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
338 children: Table<DefIndex, LazyArray<DefIndex>>,
340 opt_def_kind: Table<DefIndex, DefKind>,
341 visibility: Table<DefIndex, LazyValue<ty::Visibility>>,
342 def_span: Table<DefIndex, LazyValue<Span>>,
343 def_ident_span: Table<DefIndex, LazyValue<Span>>,
344 lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,
345 lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
346 lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
347 // As an optimization, a missing entry indicates an empty `&[]`.
348 explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
349 explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
350 generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
351 // As an optimization, a missing entry indicates an empty `&[]`.
352 inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
353 super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
354 type_of: Table<DefIndex, LazyValue<Ty<'static>>>,
355 variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
356 fn_sig: Table<DefIndex, LazyValue<ty::PolyFnSig<'static>>>,
357 codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
358 impl_trait_ref: Table<DefIndex, LazyValue<ty::TraitRef<'static>>>,
359 const_param_default: Table<DefIndex, LazyValue<rustc_middle::ty::Const<'static>>>,
360 optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
361 mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
362 promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
363 // FIXME(compiler-errors): Why isn't this a LazyArray?
364 thir_abstract_const: Table<DefIndex, LazyValue<&'static [thir::abstract_const::Node<'static>]>>,
365 impl_parent: Table<DefIndex, RawDefId>,
366 impl_polarity: Table<DefIndex, ty::ImplPolarity>,
367 impl_constness: Table<DefIndex, hir::Constness>,
368 is_intrinsic: Table<DefIndex, ()>,
369 impl_defaultness: Table<DefIndex, hir::Defaultness>,
370 // FIXME(eddyb) perhaps compute this on the fly if cheap enough?
371 coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
372 mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
373 rendered_const: Table<DefIndex, LazyValue<String>>,
374 asyncness: Table<DefIndex, hir::IsAsync>,
375 fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
376 generator_kind: Table<DefIndex, LazyValue<hir::GeneratorKind>>,
377 trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
379 trait_item_def_id: Table<DefIndex, RawDefId>,
380 inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
381 expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
382 unused_generic_params: Table<DefIndex, LazyValue<FiniteBitSet<u32>>>,
383 repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
384 // `def_keys` and `def_path_hashes` represent a lazy version of a
385 // `DefPathTable`. This allows us to avoid deserializing an entire
386 // `DefPathTable` up front, since we may only ever use a few
387 // definitions from any given crate.
388 def_keys: Table<DefIndex, LazyValue<DefKey>>,
389 def_path_hashes: Table<DefIndex, DefPathHash>,
390 proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
391 generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
392 may_have_doc_links: Table<DefIndex, ()>,
395 #[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
410 Variant(LazyValue<VariantData>),
411 Struct(LazyValue<VariantData>),
412 Union(LazyValue<VariantData>),
415 Mod(LazyArray<ModChild>),
416 MacroDef(LazyValue<ast::MacArgs>, /*macro_rules*/ bool),
417 ProcMacro(MacroKind),
422 AssocFn(LazyValue<AssocFnData>),
423 AssocType(AssocContainer),
424 AssocConst(AssocContainer),
428 #[derive(TyEncodable, TyDecodable)]
431 discr: ty::VariantDiscr,
432 /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
433 ctor: Option<DefIndex>,
434 is_non_exhaustive: bool,
437 /// Describes whether the container of an associated item
438 /// is a trait or an impl and whether, in a trait, it has
439 /// a default, or an in impl, whether it's marked "default".
440 #[derive(Copy, Clone, TyEncodable, TyDecodable)]
441 enum AssocContainer {
448 impl AssocContainer {
449 fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
451 AssocContainer::TraitRequired | AssocContainer::TraitWithDefault => {
452 ty::TraitContainer(def_id)
455 AssocContainer::ImplDefault | AssocContainer::ImplFinal => ty::ImplContainer(def_id),
459 fn defaultness(&self) -> hir::Defaultness {
461 AssocContainer::TraitRequired => hir::Defaultness::Default { has_value: false },
463 AssocContainer::TraitWithDefault | AssocContainer::ImplDefault => {
464 hir::Defaultness::Default { has_value: true }
467 AssocContainer::ImplFinal => hir::Defaultness::Final,
472 #[derive(MetadataEncodable, MetadataDecodable)]
474 container: AssocContainer,
478 #[derive(TyEncodable, TyDecodable)]
479 struct GeneratorData<'tcx> {
480 layout: mir::GeneratorLayout<'tcx>,
483 // Tags used for encoding Spans:
484 const TAG_VALID_SPAN_LOCAL: u8 = 0;
485 const TAG_VALID_SPAN_FOREIGN: u8 = 1;
486 const TAG_PARTIAL_SPAN: u8 = 2;
488 pub fn provide(providers: &mut Providers) {
489 encoder::provide(providers);
490 decoder::provide(providers);
493 trivially_parameterized_over_tcx! {