1 use crate::creader::CrateMetadataRef;
3 use def_path_hash_map::DefPathHashMapRef;
4 use table::{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::GeneratorDiagnosticData;
24 use rustc_middle::ty::{self, ReprOptions, Ty};
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 /// Additional metadata for a `Lazy<T>` where `T` may not be `Sized`,
66 /// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
68 type Meta: Copy + 'static;
71 impl<T> LazyMeta for T {
75 impl<T> LazyMeta for [T] {
79 /// A value of type T referred to by its absolute position
80 /// in the metadata, and which can be decoded lazily.
82 /// Metadata is effective a tree, encoded in post-order,
83 /// and with the root's position written next to the header.
84 /// That means every single `Lazy` points to some previous
85 /// location in the metadata and is part of a larger node.
87 /// The first `Lazy` in a node is encoded as the backwards
88 /// distance from the position where the containing node
89 /// starts and where the `Lazy` points to, while the rest
90 /// use the forward distance from the previous `Lazy`.
91 /// Distances start at 1, as 0-byte nodes are invalid.
92 /// Also invalid are nodes being referred in a different
93 /// order than they were encoded in.
95 /// # Sequences (`Lazy<[T]>`)
97 /// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
98 /// position, not at the position, which means that the length
99 /// doesn't need to be known before encoding all the elements.
101 /// If the length is 0, no position is encoded, but otherwise,
102 /// the encoding is that of `Lazy`, with the distinction that
103 /// the minimal distance the length of the sequence, i.e.
104 /// it's assumed there's no 0-byte element in the sequence.
106 // FIXME(#59875) the `Meta` parameter only exists to dodge
107 // invariance wrt `T` (coming from the `meta: T::Meta` field).
108 struct Lazy<T, Meta = <T as LazyMeta>::Meta>
110 T: ?Sized + LazyMeta<Meta = Meta>,
111 Meta: 'static + Copy,
113 position: NonZeroUsize,
115 _marker: PhantomData<T>,
118 impl<T: ?Sized + LazyMeta> Lazy<T> {
119 fn from_position_and_meta(position: NonZeroUsize, meta: T::Meta) -> Lazy<T> {
120 Lazy { position, meta, _marker: PhantomData }
125 fn from_position(position: NonZeroUsize) -> Lazy<T> {
126 Lazy::from_position_and_meta(position, ())
131 fn empty() -> Lazy<[T]> {
132 Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
136 impl<T: ?Sized + LazyMeta> Copy for Lazy<T> {}
137 impl<T: ?Sized + LazyMeta> Clone for Lazy<T> {
138 fn clone(&self) -> Self {
143 /// Encoding / decoding state for `Lazy`.
144 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
146 /// Outside of a metadata node.
149 /// Inside a metadata node, and before any `Lazy`.
150 /// The position is that of the node itself.
151 NodeStart(NonZeroUsize),
153 /// Inside a metadata node, with a previous `Lazy`.
154 /// The position is where that previous `Lazy` would start.
155 Previous(NonZeroUsize),
158 // FIXME(#59875) `Lazy!(T)` replaces `Lazy<T>`, passing the `Meta` parameter
159 // manually, instead of relying on the default, to get the correct variance.
160 // Only needed when `T` itself contains a parameter (e.g. `'tcx`).
162 (Table<$I:ty, $T:ty>) => {Lazy<Table<$I, $T>, usize>};
163 ([$T:ty]) => {Lazy<[$T], usize>};
164 ($T:ty) => {Lazy<$T, ()>};
167 type SyntaxContextTable = Lazy<Table<u32, Lazy<SyntaxContextData>>>;
168 type ExpnDataTable = Lazy<Table<ExpnIndex, Lazy<ExpnData>>>;
169 type ExpnHashTable = Lazy<Table<ExpnIndex, Lazy<ExpnHash>>>;
171 #[derive(MetadataEncodable, MetadataDecodable)]
172 pub(crate) struct ProcMacroData {
173 proc_macro_decls_static: DefIndex,
174 stability: Option<attr::Stability>,
175 macros: Lazy<[DefIndex]>,
178 /// Serialized metadata for a crate.
179 /// When compiling a proc-macro crate, we encode many of
180 /// the `Lazy<[T]>` fields as `Lazy::empty()`. This serves two purposes:
182 /// 1. We avoid performing unnecessary work. Proc-macro crates can only
183 /// export proc-macros functions, which are compiled into a shared library.
184 /// As a result, a large amount of the information we normally store
185 /// (e.g. optimized MIR) is unneeded by downstream crates.
186 /// 2. We avoid serializing invalid `CrateNum`s. When we deserialize
187 /// a proc-macro crate, we don't load any of its dependencies (since we
188 /// just need to invoke a native function from the shared library).
189 /// This means that any foreign `CrateNum`s that we serialize cannot be
190 /// deserialized, since we will not know how to map them into the current
191 /// compilation session. If we were to serialize a proc-macro crate like
192 /// a normal crate, much of what we serialized would be unusable in addition
194 #[derive(MetadataEncodable, MetadataDecodable)]
195 pub(crate) struct CrateRoot<'tcx> {
197 triple: TargetTriple,
198 extra_filename: String,
200 stable_crate_id: StableCrateId,
201 panic_strategy: PanicStrategy,
202 panic_in_drop_strategy: PanicStrategy,
204 has_global_allocator: bool,
205 has_panic_handler: bool,
206 has_default_lib_allocator: bool,
208 crate_deps: Lazy<[CrateDep]>,
209 dylib_dependency_formats: Lazy<[Option<LinkagePreference>]>,
210 lib_features: Lazy<[(Symbol, Option<Symbol>)]>,
211 lang_items: Lazy<[(DefIndex, usize)]>,
212 lang_items_missing: Lazy<[lang_items::LangItem]>,
213 diagnostic_items: Lazy<[(Symbol, DefIndex)]>,
214 native_libraries: Lazy<[NativeLib]>,
215 foreign_modules: Lazy<[ForeignModule]>,
216 traits: Lazy<[DefIndex]>,
217 impls: Lazy<[TraitImpls]>,
218 incoherent_impls: Lazy<[IncoherentImpls]>,
219 interpret_alloc_index: Lazy<[u32]>,
220 proc_macro_data: Option<ProcMacroData>,
222 tables: LazyTables<'tcx>,
223 debugger_visualizers: Lazy<[rustc_span::DebuggerVisualizerFile]>,
225 exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportInfo)]),
227 syntax_contexts: SyntaxContextTable,
228 expn_data: ExpnDataTable,
229 expn_hashes: ExpnHashTable,
231 def_path_hash_map: Lazy<DefPathHashMapRef<'tcx>>,
233 source_map: Lazy<[rustc_span::SourceFile]>,
235 compiler_builtins: bool,
236 needs_allocator: bool,
237 needs_panic_runtime: bool,
240 profiler_runtime: bool,
241 symbol_mangling_version: SymbolManglingVersion,
244 /// On-disk representation of `DefId`.
245 /// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk
246 /// representation and the compilation session.
247 #[derive(Copy, Clone)]
248 pub(crate) struct RawDefId {
253 impl Into<RawDefId> for DefId {
254 fn into(self) -> RawDefId {
255 RawDefId { krate: self.krate.as_u32(), index: self.index.as_u32() }
260 fn decode(self, cdata: CrateMetadataRef<'_>) -> DefId {
261 let krate = CrateNum::from_u32(self.krate);
262 let krate = cdata.map_encoded_cnum_to_current(krate);
263 DefId { krate, index: DefIndex::from_u32(self.index) }
267 #[derive(Encodable, Decodable)]
268 pub(crate) struct CrateDep {
271 pub host_hash: Option<Svh>,
272 pub kind: CrateDepKind,
273 pub extra_filename: String,
276 #[derive(MetadataEncodable, MetadataDecodable)]
277 pub(crate) struct TraitImpls {
278 trait_id: (u32, DefIndex),
279 impls: Lazy<[(DefIndex, Option<SimplifiedType>)]>,
282 #[derive(MetadataEncodable, MetadataDecodable)]
283 pub(crate) struct IncoherentImpls {
284 self_ty: SimplifiedType,
285 impls: Lazy<[DefIndex]>,
288 /// Define `LazyTables` and `TableBuilders` at the same time.
289 macro_rules! define_tables {
290 ($($name:ident: Table<$IDX:ty, $T:ty>),+ $(,)?) => {
291 #[derive(MetadataEncodable, MetadataDecodable)]
292 pub(crate) struct LazyTables<'tcx> {
293 $($name: Lazy!(Table<$IDX, $T>)),+
297 struct TableBuilders<'tcx> {
298 $($name: TableBuilder<$IDX, $T>),+
301 impl<'tcx> TableBuilders<'tcx> {
302 fn encode(&self, buf: &mut Encoder) -> LazyTables<'tcx> {
304 $($name: self.$name.encode(buf)),+
312 kind: Table<DefIndex, Lazy<EntryKind>>,
313 attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
314 children: Table<DefIndex, Lazy<[DefIndex]>>,
316 opt_def_kind: Table<DefIndex, DefKind>,
317 visibility: Table<DefIndex, Lazy<ty::Visibility>>,
318 def_span: Table<DefIndex, Lazy<Span>>,
319 def_ident_span: Table<DefIndex, Lazy<Span>>,
320 lookup_stability: Table<DefIndex, Lazy<attr::Stability>>,
321 lookup_const_stability: Table<DefIndex, Lazy<attr::ConstStability>>,
322 lookup_deprecation_entry: Table<DefIndex, Lazy<attr::Deprecation>>,
323 // As an optimization, a missing entry indicates an empty `&[]`.
324 explicit_item_bounds: Table<DefIndex, Lazy!([(ty::Predicate<'tcx>, Span)])>,
325 explicit_predicates_of: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
326 generics_of: Table<DefIndex, Lazy<ty::Generics>>,
327 // As an optimization, a missing entry indicates an empty `&[]`.
328 inferred_outlives_of: Table<DefIndex, Lazy!([(ty::Predicate<'tcx>, Span)])>,
329 super_predicates_of: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
330 type_of: Table<DefIndex, Lazy!(Ty<'tcx>)>,
331 variances_of: Table<DefIndex, Lazy<[ty::Variance]>>,
332 fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
333 codegen_fn_attrs: Table<DefIndex, Lazy!(CodegenFnAttrs)>,
334 impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
335 const_param_default: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
336 optimized_mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
337 mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
338 promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
339 thir_abstract_const: Table<DefIndex, Lazy!(&'tcx [thir::abstract_const::Node<'tcx>])>,
340 impl_parent: Table<DefIndex, RawDefId>,
341 impl_polarity: Table<DefIndex, ty::ImplPolarity>,
342 impl_constness: Table<DefIndex, hir::Constness>,
343 is_intrinsic: Table<DefIndex, ()>,
344 impl_defaultness: Table<DefIndex, hir::Defaultness>,
345 // FIXME(eddyb) perhaps compute this on the fly if cheap enough?
346 coerce_unsized_info: Table<DefIndex, Lazy!(ty::adjustment::CoerceUnsizedInfo)>,
347 mir_const_qualif: Table<DefIndex, Lazy!(mir::ConstQualifs)>,
348 rendered_const: Table<DefIndex, Lazy!(String)>,
349 asyncness: Table<DefIndex, hir::IsAsync>,
350 fn_arg_names: Table<DefIndex, Lazy!([Ident])>,
351 generator_kind: Table<DefIndex, Lazy!(hir::GeneratorKind)>,
352 trait_def: Table<DefIndex, Lazy!(ty::TraitDef)>,
354 trait_item_def_id: Table<DefIndex, RawDefId>,
355 inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
356 expn_that_defined: Table<DefIndex, Lazy<ExpnId>>,
357 unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
358 repr_options: Table<DefIndex, Lazy<ReprOptions>>,
359 // `def_keys` and `def_path_hashes` represent a lazy version of a
360 // `DefPathTable`. This allows us to avoid deserializing an entire
361 // `DefPathTable` up front, since we may only ever use a few
362 // definitions from any given crate.
363 def_keys: Table<DefIndex, Lazy<DefKey>>,
364 def_path_hashes: Table<DefIndex, DefPathHash>,
365 proc_macro_quoted_spans: Table<usize, Lazy<Span>>,
366 generator_diagnostic_data: Table<DefIndex, Lazy<GeneratorDiagnosticData<'tcx>>>,
367 may_have_doc_links: Table<DefIndex, ()>,
370 #[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
385 Variant(Lazy<VariantData>),
386 Struct(Lazy<VariantData>),
387 Union(Lazy<VariantData>),
390 Mod(Lazy<[ModChild]>),
391 MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
392 ProcMacro(MacroKind),
397 AssocFn(Lazy<AssocFnData>),
398 AssocType(AssocContainer),
399 AssocConst(AssocContainer),
403 #[derive(TyEncodable, TyDecodable)]
406 discr: ty::VariantDiscr,
407 /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
408 ctor: Option<DefIndex>,
409 is_non_exhaustive: bool,
412 /// Describes whether the container of an associated item
413 /// is a trait or an impl and whether, in a trait, it has
414 /// a default, or an in impl, whether it's marked "default".
415 #[derive(Copy, Clone, TyEncodable, TyDecodable)]
416 enum AssocContainer {
423 impl AssocContainer {
424 fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
426 AssocContainer::TraitRequired | AssocContainer::TraitWithDefault => {
427 ty::TraitContainer(def_id)
430 AssocContainer::ImplDefault | AssocContainer::ImplFinal => ty::ImplContainer(def_id),
434 fn defaultness(&self) -> hir::Defaultness {
436 AssocContainer::TraitRequired => hir::Defaultness::Default { has_value: false },
438 AssocContainer::TraitWithDefault | AssocContainer::ImplDefault => {
439 hir::Defaultness::Default { has_value: true }
442 AssocContainer::ImplFinal => hir::Defaultness::Final,
447 #[derive(MetadataEncodable, MetadataDecodable)]
449 container: AssocContainer,
453 #[derive(TyEncodable, TyDecodable)]
454 struct GeneratorData<'tcx> {
455 layout: mir::GeneratorLayout<'tcx>,
458 // Tags used for encoding Spans:
459 const TAG_VALID_SPAN_LOCAL: u8 = 0;
460 const TAG_VALID_SPAN_FOREIGN: u8 = 1;
461 const TAG_PARTIAL_SPAN: u8 = 2;
463 pub fn provide(providers: &mut Providers) {
464 encoder::provide(providers);
465 decoder::provide(providers);