1 use crate::creader::CrateMetadataRef;
2 use decoder::Metadata;
3 use def_path_hash_map::DefPathHashMapRef;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
6 use table::TableBuilder;
7
8 use rustc_ast as ast;
9 use rustc_ast::expand::StrippedCfgItem;
10 use rustc_attr as attr;
11 use rustc_data_structures::svh::Svh;
12 use rustc_hir as hir;
13 use rustc_hir::def::{CtorKind, DefKind, DocLinkResMap};
14 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId};
15 use rustc_hir::definitions::DefKey;
16 use rustc_hir::lang_items::LangItem;
17 use rustc_index::bit_set::BitSet;
18 use rustc_index::IndexVec;
19 use rustc_middle::metadata::ModChild;
20 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
21 use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
22 use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
23 use rustc_middle::mir;
24 use rustc_middle::query::Providers;
25 use rustc_middle::ty::fast_reject::SimplifiedType;
26 use rustc_middle::ty::{self, ReprOptions, Ty, UnusedGenericParams};
27 use rustc_middle::ty::{DeducedParamAttrs, GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt};
28 use rustc_serialize::opaque::FileEncoder;
29 use rustc_session::config::SymbolManglingVersion;
30 use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
31 use rustc_span::edition::Edition;
32 use rustc_span::hygiene::{ExpnIndex, MacroKind};
33 use rustc_span::symbol::{Ident, Symbol};
34 use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
35 use rustc_target::abi::{FieldIdx, VariantIdx};
36 use rustc_target::spec::{PanicStrategy, TargetTriple};
37
38 use std::marker::PhantomData;
39 use std::num::NonZeroUsize;
40
41 pub use decoder::provide_extern;
42 use decoder::DecodeContext;
43 pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob};
44 use encoder::EncodeContext;
45 pub use encoder::{encode_metadata, EncodedMetadata};
46 use rustc_span::hygiene::SyntaxContextData;
47
48 mod decoder;
49 mod def_path_hash_map;
50 mod encoder;
51 mod table;
52
rustc_version(cfg_version: &'static str) -> String53 pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
54 format!("rustc {}", cfg_version)
55 }
56
57 /// Metadata encoding version.
58 /// N.B., increment this if you change the format of metadata such that
59 /// the rustc version can't be found to compare with `rustc_version()`.
60 const METADATA_VERSION: u8 = 8;
61
62 /// Metadata header which includes `METADATA_VERSION`.
63 ///
64 /// This header is followed by the length of the compressed data, then
65 /// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian
66 /// unsigned integer, and further followed by the rustc version string.
67 pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
68
69 /// A value of type T referred to by its absolute position
70 /// in the metadata, and which can be decoded lazily.
71 ///
72 /// Metadata is effective a tree, encoded in post-order,
73 /// and with the root's position written next to the header.
74 /// That means every single `LazyValue` points to some previous
75 /// location in the metadata and is part of a larger node.
76 ///
77 /// The first `LazyValue` in a node is encoded as the backwards
78 /// distance from the position where the containing node
79 /// starts and where the `LazyValue` points to, while the rest
80 /// use the forward distance from the previous `LazyValue`.
81 /// Distances start at 1, as 0-byte nodes are invalid.
82 /// Also invalid are nodes being referred in a different
83 /// order than they were encoded in.
84 #[must_use]
85 struct LazyValue<T> {
86 position: NonZeroUsize,
87 _marker: PhantomData<fn() -> T>,
88 }
89
90 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
91 type Value<'tcx> = LazyValue<T::Value<'tcx>>;
92 }
93
94 impl<T> LazyValue<T> {
from_position(position: NonZeroUsize) -> LazyValue<T>95 fn from_position(position: NonZeroUsize) -> LazyValue<T> {
96 LazyValue { position, _marker: PhantomData }
97 }
98 }
99
100 /// A list of lazily-decoded values.
101 ///
102 /// Unlike `LazyValue<Vec<T>>`, the length is encoded next to the
103 /// position, not at the position, which means that the length
104 /// doesn't need to be known before encoding all the elements.
105 ///
106 /// If the length is 0, no position is encoded, but otherwise,
107 /// the encoding is that of `LazyArray`, with the distinction that
108 /// the minimal distance the length of the sequence, i.e.
109 /// it's assumed there's no 0-byte element in the sequence.
110 struct LazyArray<T> {
111 position: NonZeroUsize,
112 num_elems: usize,
113 _marker: PhantomData<fn() -> T>,
114 }
115
116 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
117 type Value<'tcx> = LazyArray<T::Value<'tcx>>;
118 }
119
120 impl<T> Default for LazyArray<T> {
default() -> LazyArray<T>121 fn default() -> LazyArray<T> {
122 LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
123 }
124 }
125
126 impl<T> LazyArray<T> {
from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T>127 fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
128 LazyArray { position, num_elems, _marker: PhantomData }
129 }
130 }
131
132 /// A list of lazily-decoded values, with the added capability of random access.
133 ///
134 /// Random-access table (i.e. offering constant-time `get`/`set`), similar to
135 /// `LazyArray<T>`, but without requiring encoding or decoding all the values
136 /// eagerly and in-order.
137 struct LazyTable<I, T> {
138 position: NonZeroUsize,
139 encoded_size: usize,
140 _marker: PhantomData<fn(I) -> T>,
141 }
142
143 impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
144 type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
145 }
146
147 impl<I, T> LazyTable<I, T> {
from_position_and_encoded_size( position: NonZeroUsize, encoded_size: usize, ) -> LazyTable<I, T>148 fn from_position_and_encoded_size(
149 position: NonZeroUsize,
150 encoded_size: usize,
151 ) -> LazyTable<I, T> {
152 LazyTable { position, encoded_size, _marker: PhantomData }
153 }
154 }
155
156 impl<T> Copy for LazyValue<T> {}
157 impl<T> Clone for LazyValue<T> {
clone(&self) -> Self158 fn clone(&self) -> Self {
159 *self
160 }
161 }
162
163 impl<T> Copy for LazyArray<T> {}
164 impl<T> Clone for LazyArray<T> {
clone(&self) -> Self165 fn clone(&self) -> Self {
166 *self
167 }
168 }
169
170 impl<I, T> Copy for LazyTable<I, T> {}
171 impl<I, T> Clone for LazyTable<I, T> {
clone(&self) -> Self172 fn clone(&self) -> Self {
173 *self
174 }
175 }
176
177 /// Encoding / decoding state for `Lazy`s (`LazyValue`, `LazyArray`, and `LazyTable`).
178 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
179 enum LazyState {
180 /// Outside of a metadata node.
181 NoNode,
182
183 /// Inside a metadata node, and before any `Lazy`s.
184 /// The position is that of the node itself.
185 NodeStart(NonZeroUsize),
186
187 /// Inside a metadata node, with a previous `Lazy`s.
188 /// The position is where that previous `Lazy` would start.
189 Previous(NonZeroUsize),
190 }
191
192 type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextData>>>;
193 type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
194 type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
195
196 #[derive(MetadataEncodable, MetadataDecodable)]
197 pub(crate) struct ProcMacroData {
198 proc_macro_decls_static: DefIndex,
199 stability: Option<attr::Stability>,
200 macros: LazyArray<DefIndex>,
201 }
202
203 /// Serialized crate metadata.
204 ///
205 /// This contains just enough information to determine if we should load the `CrateRoot` or not.
206 /// Prefer [`CrateRoot`] whenever possible to avoid ICEs when using `omit-git-hash` locally.
207 /// See #76720 for more details.
208 ///
209 /// If you do modify this struct, also bump the [`METADATA_VERSION`] constant.
210 #[derive(MetadataEncodable, MetadataDecodable)]
211 pub(crate) struct CrateHeader {
212 pub(crate) triple: TargetTriple,
213 pub(crate) hash: Svh,
214 pub(crate) name: Symbol,
215 /// Whether this is the header for a proc-macro crate.
216 ///
217 /// This is separate from [`ProcMacroData`] to avoid having to update [`METADATA_VERSION`] every
218 /// time ProcMacroData changes.
219 pub(crate) is_proc_macro_crate: bool,
220 }
221
222 /// Serialized `.rmeta` data for a crate.
223 ///
224 /// When compiling a proc-macro crate, we encode many of
225 /// the `LazyArray<T>` fields as `Lazy::empty()`. This serves two purposes:
226 ///
227 /// 1. We avoid performing unnecessary work. Proc-macro crates can only
228 /// export proc-macros functions, which are compiled into a shared library.
229 /// As a result, a large amount of the information we normally store
230 /// (e.g. optimized MIR) is unneeded by downstream crates.
231 /// 2. We avoid serializing invalid `CrateNum`s. When we deserialize
232 /// a proc-macro crate, we don't load any of its dependencies (since we
233 /// just need to invoke a native function from the shared library).
234 /// This means that any foreign `CrateNum`s that we serialize cannot be
235 /// deserialized, since we will not know how to map them into the current
236 /// compilation session. If we were to serialize a proc-macro crate like
237 /// a normal crate, much of what we serialized would be unusable in addition
238 /// to being unused.
239 #[derive(MetadataEncodable, MetadataDecodable)]
240 pub(crate) struct CrateRoot {
241 /// A header used to detect if this is the right crate to load.
242 header: CrateHeader,
243
244 extra_filename: String,
245 stable_crate_id: StableCrateId,
246 required_panic_strategy: Option<PanicStrategy>,
247 panic_in_drop_strategy: PanicStrategy,
248 edition: Edition,
249 has_global_allocator: bool,
250 has_alloc_error_handler: bool,
251 has_panic_handler: bool,
252 has_default_lib_allocator: bool,
253
254 crate_deps: LazyArray<CrateDep>,
255 dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
256 lib_features: LazyArray<(Symbol, Option<Symbol>)>,
257 stability_implications: LazyArray<(Symbol, Symbol)>,
258 lang_items: LazyArray<(DefIndex, LangItem)>,
259 lang_items_missing: LazyArray<LangItem>,
260 stripped_cfg_items: LazyArray<StrippedCfgItem<DefIndex>>,
261 diagnostic_items: LazyArray<(Symbol, DefIndex)>,
262 native_libraries: LazyArray<NativeLib>,
263 foreign_modules: LazyArray<ForeignModule>,
264 traits: LazyArray<DefIndex>,
265 impls: LazyArray<TraitImpls>,
266 incoherent_impls: LazyArray<IncoherentImpls>,
267 interpret_alloc_index: LazyArray<u32>,
268 proc_macro_data: Option<ProcMacroData>,
269
270 tables: LazyTables,
271 debugger_visualizers: LazyArray<DebuggerVisualizerFile>,
272
273 exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
274
275 syntax_contexts: SyntaxContextTable,
276 expn_data: ExpnDataTable,
277 expn_hashes: ExpnHashTable,
278
279 def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
280
281 source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
282
283 compiler_builtins: bool,
284 needs_allocator: bool,
285 needs_panic_runtime: bool,
286 no_builtins: bool,
287 panic_runtime: bool,
288 profiler_runtime: bool,
289 symbol_mangling_version: SymbolManglingVersion,
290 }
291
292 /// On-disk representation of `DefId`.
293 /// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk
294 /// representation and the compilation session.
295 #[derive(Copy, Clone)]
296 pub(crate) struct RawDefId {
297 krate: u32,
298 index: u32,
299 }
300
301 impl Into<RawDefId> for DefId {
into(self) -> RawDefId302 fn into(self) -> RawDefId {
303 RawDefId { krate: self.krate.as_u32(), index: self.index.as_u32() }
304 }
305 }
306
307 impl RawDefId {
308 /// This exists so that `provide_one!` is happy
decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId309 fn decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId {
310 self.decode_from_cdata(meta.0)
311 }
312
decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId313 fn decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId {
314 let krate = CrateNum::from_u32(self.krate);
315 let krate = cdata.map_encoded_cnum_to_current(krate);
316 DefId { krate, index: DefIndex::from_u32(self.index) }
317 }
318 }
319
320 #[derive(Encodable, Decodable)]
321 pub(crate) struct CrateDep {
322 pub name: Symbol,
323 pub hash: Svh,
324 pub host_hash: Option<Svh>,
325 pub kind: CrateDepKind,
326 pub extra_filename: String,
327 pub is_private: bool,
328 }
329
330 #[derive(MetadataEncodable, MetadataDecodable)]
331 pub(crate) struct TraitImpls {
332 trait_id: (u32, DefIndex),
333 impls: LazyArray<(DefIndex, Option<SimplifiedType>)>,
334 }
335
336 #[derive(MetadataEncodable, MetadataDecodable)]
337 pub(crate) struct IncoherentImpls {
338 self_ty: SimplifiedType,
339 impls: LazyArray<DefIndex>,
340 }
341
342 /// Define `LazyTables` and `TableBuilders` at the same time.
343 macro_rules! define_tables {
344 (
345 - defaulted: $($name1:ident: Table<$IDX1:ty, $T1:ty>,)+
346 - optional: $($name2:ident: Table<$IDX2:ty, $T2:ty>,)+
347 ) => {
348 #[derive(MetadataEncodable, MetadataDecodable)]
349 pub(crate) struct LazyTables {
350 $($name1: LazyTable<$IDX1, $T1>,)+
351 $($name2: LazyTable<$IDX2, Option<$T2>>,)+
352 }
353
354 #[derive(Default)]
355 struct TableBuilders {
356 $($name1: TableBuilder<$IDX1, $T1>,)+
357 $($name2: TableBuilder<$IDX2, Option<$T2>>,)+
358 }
359
360 impl TableBuilders {
361 fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
362 LazyTables {
363 $($name1: self.$name1.encode(buf),)+
364 $($name2: self.$name2.encode(buf),)+
365 }
366 }
367 }
368 }
369 }
370
371 define_tables! {
372 - defaulted:
373 is_intrinsic: Table<DefIndex, bool>,
374 is_macro_rules: Table<DefIndex, bool>,
375 is_type_alias_impl_trait: Table<DefIndex, bool>,
376 attr_flags: Table<DefIndex, AttrFlags>,
377 def_path_hashes: Table<DefIndex, DefPathHash>,
378 explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
379 inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
380 inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
381 associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
382 opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
383 unused_generic_params: Table<DefIndex, UnusedGenericParams>,
384 // Reexported names are not associated with individual `DefId`s,
385 // e.g. a glob import can introduce a lot of names, all with the same `DefId`.
386 // That's why the encoded list needs to contain `ModChild` structures describing all the names
387 // individually instead of `DefId`s.
388 module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
389
390 - optional:
391 attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
392 // For non-reexported names in a module every name is associated with a separate `DefId`,
393 // so we can take their names, visibilities etc from other encoded tables.
394 module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
395 associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
396 opt_def_kind: Table<DefIndex, DefKind>,
397 visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
398 def_span: Table<DefIndex, LazyValue<Span>>,
399 def_ident_span: Table<DefIndex, LazyValue<Span>>,
400 lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,
401 lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
402 lookup_default_body_stability: Table<DefIndex, LazyValue<attr::DefaultBodyStability>>,
403 lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
404 explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
405 generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
406 super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
407 // As an optimization, we only store this for trait aliases,
408 // since it's identical to super_predicates_of for traits.
409 implied_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
410 type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<Ty<'static>>>>,
411 variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
412 fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::PolyFnSig<'static>>>>,
413 codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
414 impl_trait_ref: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::TraitRef<'static>>>>,
415 const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<rustc_middle::ty::Const<'static>>>>,
416 object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
417 optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
418 mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
419 closure_saved_names_of_captured_variables: Table<DefIndex, LazyValue<IndexVec<FieldIdx, Symbol>>>,
420 mir_generator_witnesses: Table<DefIndex, LazyValue<mir::GeneratorLayout<'static>>>,
421 promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
422 thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::Const<'static>>>>,
423 impl_parent: Table<DefIndex, RawDefId>,
424 impl_polarity: Table<DefIndex, ty::ImplPolarity>,
425 constness: Table<DefIndex, hir::Constness>,
426 defaultness: Table<DefIndex, hir::Defaultness>,
427 // FIXME(eddyb) perhaps compute this on the fly if cheap enough?
428 coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
429 mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
430 rendered_const: Table<DefIndex, LazyValue<String>>,
431 asyncness: Table<DefIndex, hir::IsAsync>,
432 fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
433 generator_kind: Table<DefIndex, LazyValue<hir::GeneratorKind>>,
434 trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
435 trait_item_def_id: Table<DefIndex, RawDefId>,
436 expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
437 params_in_repr: Table<DefIndex, LazyValue<BitSet<u32>>>,
438 repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
439 // `def_keys` and `def_path_hashes` represent a lazy version of a
440 // `DefPathTable`. This allows us to avoid deserializing an entire
441 // `DefPathTable` up front, since we may only ever use a few
442 // definitions from any given crate.
443 def_keys: Table<DefIndex, LazyValue<DefKey>>,
444 proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
445 generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
446 variant_data: Table<DefIndex, LazyValue<VariantData>>,
447 assoc_container: Table<DefIndex, ty::AssocItemContainer>,
448 macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
449 proc_macro: Table<DefIndex, MacroKind>,
450 deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
451 trait_impl_trait_tys: Table<DefIndex, LazyValue<FxHashMap<DefId, ty::EarlyBinder<Ty<'static>>>>>,
452 doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
453 doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
454 }
455
456 #[derive(TyEncodable, TyDecodable)]
457 struct VariantData {
458 idx: VariantIdx,
459 discr: ty::VariantDiscr,
460 /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
461 ctor: Option<(CtorKind, DefIndex)>,
462 is_non_exhaustive: bool,
463 }
464
465 bitflags::bitflags! {
466 #[derive(Default)]
467 pub struct AttrFlags: u8 {
468 const IS_DOC_HIDDEN = 1 << 0;
469 }
470 }
471
472 // Tags used for encoding Spans:
473 const TAG_VALID_SPAN_LOCAL: u8 = 0;
474 const TAG_VALID_SPAN_FOREIGN: u8 = 1;
475 const TAG_PARTIAL_SPAN: u8 = 2;
476
477 // Tags for encoding Symbol's
478 const SYMBOL_STR: u8 = 0;
479 const SYMBOL_OFFSET: u8 = 1;
480 const SYMBOL_PREINTERNED: u8 = 2;
481
provide(providers: &mut Providers)482 pub fn provide(providers: &mut Providers) {
483 encoder::provide(providers);
484 decoder::provide(providers);
485 }
486
487 trivially_parameterized_over_tcx! {
488 VariantData,
489 RawDefId,
490 TraitImpls,
491 IncoherentImpls,
492 CrateHeader,
493 CrateRoot,
494 CrateDep,
495 AttrFlags,
496 }
497