1use std::marker::PhantomData;
2use std::num::NonZero;
3
4pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob, TargetModifiers};
5use decoder::{DecodeContext, Metadata};
6use def_path_hash_map::DefPathHashMapRef;
7use encoder::EncodeContext;
8pub use encoder::{EncodedMetadata, encode_metadata, rendered_const};
9use rustc_abi::{FieldIdx, ReprOptions, VariantIdx};
10use rustc_ast::expand::StrippedCfgItem;
11use rustc_data_structures::fx::FxHashMap;
12use rustc_data_structures::svh::Svh;
13use rustc_hir::PreciseCapturingArgKind;
14use rustc_hir::def::{CtorKind, DefKind, DocLinkResMap};
15use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIndex, DefPathHash, StableCrateId};
16use rustc_hir::definitions::DefKey;
17use rustc_hir::lang_items::LangItem;
18use rustc_index::IndexVec;
19use rustc_index::bit_set::DenseBitSet;
20use rustc_macros::{
21 Decodable, Encodable, MetadataDecodable, MetadataEncodable, TyDecodable, TyEncodable,
22};
23use rustc_middle::metadata::ModChild;
24use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
25use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
26use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
27use rustc_middle::middle::lib_features::FeatureStability;
28use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
29use rustc_middle::ty::fast_reject::SimplifiedType;
30use rustc_middle::ty::{
31 self, DeducedParamAttrs, ParameterizedOverTcx, Ty, TyCtxt, UnusedGenericParams,
32};
33use rustc_middle::util::Providers;
34use rustc_middle::{mir, trivially_parameterized_over_tcx};
35use rustc_serialize::opaque::FileEncoder;
36use rustc_session::config::{SymbolManglingVersion, TargetModifier};
37use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
38use rustc_span::edition::Edition;
39use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
40use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
41use rustc_target::spec::{PanicStrategy, TargetTuple};
42use table::TableBuilder;
43use {rustc_ast as ast, rustc_attr_data_structures as attrs, rustc_hir as hir};
44
45use crate::creader::CrateMetadataRef;
46
47mod decoder;
48mod def_path_hash_map;
49mod encoder;
50mod table;
51
52pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
53 format!("rustc {cfg_version}")
54}
55
56const METADATA_VERSION: u8 = 10;
60
61pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
67
68#[must_use]
84struct LazyValue<T> {
85 position: NonZero<usize>,
86 _marker: PhantomData<fn() -> T>,
87}
88
89impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
90 type Value<'tcx> = LazyValue<T::Value<'tcx>>;
91}
92
93impl<T> LazyValue<T> {
94 fn from_position(position: NonZero<usize>) -> LazyValue<T> {
95 LazyValue { position, _marker: PhantomData }
96 }
97}
98
99struct LazyArray<T> {
110 position: NonZero<usize>,
111 num_elems: usize,
112 _marker: PhantomData<fn() -> T>,
113}
114
115impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
116 type Value<'tcx> = LazyArray<T::Value<'tcx>>;
117}
118
119impl<T> Default for LazyArray<T> {
120 fn default() -> LazyArray<T> {
121 LazyArray::from_position_and_num_elems(NonZero::new(1).unwrap(), 0)
122 }
123}
124
125impl<T> LazyArray<T> {
126 fn from_position_and_num_elems(position: NonZero<usize>, num_elems: usize) -> LazyArray<T> {
127 LazyArray { position, num_elems, _marker: PhantomData }
128 }
129}
130
131struct LazyTable<I, T> {
137 position: NonZero<usize>,
138 width: usize,
141 len: usize,
143 _marker: PhantomData<fn(I) -> T>,
144}
145
146impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
147 type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
148}
149
150impl<I, T> LazyTable<I, T> {
151 fn from_position_and_encoded_size(
152 position: NonZero<usize>,
153 width: usize,
154 len: usize,
155 ) -> LazyTable<I, T> {
156 LazyTable { position, width, len, _marker: PhantomData }
157 }
158}
159
160impl<T> Copy for LazyValue<T> {}
161impl<T> Clone for LazyValue<T> {
162 fn clone(&self) -> Self {
163 *self
164 }
165}
166
167impl<T> Copy for LazyArray<T> {}
168impl<T> Clone for LazyArray<T> {
169 fn clone(&self) -> Self {
170 *self
171 }
172}
173
174impl<I, T> Copy for LazyTable<I, T> {}
175impl<I, T> Clone for LazyTable<I, T> {
176 fn clone(&self) -> Self {
177 *self
178 }
179}
180
181#[derive(Copy, Clone, PartialEq, Eq, Debug)]
183enum LazyState {
184 NoNode,
186
187 NodeStart(NonZero<usize>),
190
191 Previous(NonZero<usize>),
194}
195
196type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextKey>>>;
197type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
198type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
199
200#[derive(MetadataEncodable, MetadataDecodable)]
201pub(crate) struct ProcMacroData {
202 proc_macro_decls_static: DefIndex,
203 stability: Option<attrs::Stability>,
204 macros: LazyArray<DefIndex>,
205}
206
207#[derive(MetadataEncodable, MetadataDecodable)]
215pub(crate) struct CrateHeader {
216 pub(crate) triple: TargetTuple,
217 pub(crate) hash: Svh,
218 pub(crate) name: Symbol,
219 pub(crate) is_proc_macro_crate: bool,
224 pub(crate) is_stub: bool,
230}
231
232#[derive(MetadataEncodable, MetadataDecodable)]
250pub(crate) struct CrateRoot {
251 header: CrateHeader,
253
254 extra_filename: String,
255 stable_crate_id: StableCrateId,
256 required_panic_strategy: Option<PanicStrategy>,
257 panic_in_drop_strategy: PanicStrategy,
258 edition: Edition,
259 has_global_allocator: bool,
260 has_alloc_error_handler: bool,
261 has_panic_handler: bool,
262 has_default_lib_allocator: bool,
263
264 crate_deps: LazyArray<CrateDep>,
265 dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
266 lib_features: LazyArray<(Symbol, FeatureStability)>,
267 stability_implications: LazyArray<(Symbol, Symbol)>,
268 lang_items: LazyArray<(DefIndex, LangItem)>,
269 lang_items_missing: LazyArray<LangItem>,
270 stripped_cfg_items: LazyArray<StrippedCfgItem<DefIndex>>,
271 diagnostic_items: LazyArray<(Symbol, DefIndex)>,
272 native_libraries: LazyArray<NativeLib>,
273 foreign_modules: LazyArray<ForeignModule>,
274 traits: LazyArray<DefIndex>,
275 impls: LazyArray<TraitImpls>,
276 incoherent_impls: LazyArray<IncoherentImpls>,
277 interpret_alloc_index: LazyArray<u64>,
278 proc_macro_data: Option<ProcMacroData>,
279
280 tables: LazyTables,
281 debugger_visualizers: LazyArray<DebuggerVisualizerFile>,
282
283 exportable_items: LazyArray<DefIndex>,
284 stable_order_of_exportable_impls: LazyArray<(DefIndex, usize)>,
285 exported_non_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
286 exported_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
287
288 syntax_contexts: SyntaxContextTable,
289 expn_data: ExpnDataTable,
290 expn_hashes: ExpnHashTable,
291
292 def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
293
294 source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
295 target_modifiers: LazyArray<TargetModifier>,
296
297 compiler_builtins: bool,
298 needs_allocator: bool,
299 needs_panic_runtime: bool,
300 no_builtins: bool,
301 panic_runtime: bool,
302 profiler_runtime: bool,
303 symbol_mangling_version: SymbolManglingVersion,
304
305 specialization_enabled_in: bool,
306}
307
308#[derive(Copy, Clone)]
312pub(crate) struct RawDefId {
313 krate: u32,
314 index: u32,
315}
316
317impl From<DefId> for RawDefId {
318 fn from(val: DefId) -> Self {
319 RawDefId { krate: val.krate.as_u32(), index: val.index.as_u32() }
320 }
321}
322
323impl RawDefId {
324 fn decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId {
326 self.decode_from_cdata(meta.0)
327 }
328
329 fn decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId {
330 let krate = CrateNum::from_u32(self.krate);
331 let krate = cdata.map_encoded_cnum_to_current(krate);
332 DefId { krate, index: DefIndex::from_u32(self.index) }
333 }
334}
335
336#[derive(Encodable, Decodable)]
337pub(crate) struct CrateDep {
338 pub name: Symbol,
339 pub hash: Svh,
340 pub host_hash: Option<Svh>,
341 pub kind: CrateDepKind,
342 pub extra_filename: String,
343 pub is_private: bool,
344}
345
346#[derive(MetadataEncodable, MetadataDecodable)]
347pub(crate) struct TraitImpls {
348 trait_id: (u32, DefIndex),
349 impls: LazyArray<(DefIndex, Option<SimplifiedType>)>,
350}
351
352#[derive(MetadataEncodable, MetadataDecodable)]
353pub(crate) struct IncoherentImpls {
354 self_ty: SimplifiedType,
355 impls: LazyArray<DefIndex>,
356}
357
358macro_rules! define_tables {
360 (
361 - defaulted: $($name1:ident: Table<$IDX1:ty, $T1:ty>,)+
362 - optional: $($name2:ident: Table<$IDX2:ty, $T2:ty>,)+
363 ) => {
364 #[derive(MetadataEncodable, MetadataDecodable)]
365 pub(crate) struct LazyTables {
366 $($name1: LazyTable<$IDX1, $T1>,)+
367 $($name2: LazyTable<$IDX2, Option<$T2>>,)+
368 }
369
370 #[derive(Default)]
371 struct TableBuilders {
372 $($name1: TableBuilder<$IDX1, $T1>,)+
373 $($name2: TableBuilder<$IDX2, Option<$T2>>,)+
374 }
375
376 impl TableBuilders {
377 fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
378 LazyTables {
379 $($name1: self.$name1.encode(buf),)+
380 $($name2: self.$name2.encode(buf),)+
381 }
382 }
383 }
384 }
385}
386
387define_tables! {
388- defaulted:
389 intrinsic: Table<DefIndex, Option<LazyValue<ty::IntrinsicDef>>>,
390 is_macro_rules: Table<DefIndex, bool>,
391 type_alias_is_lazy: Table<DefIndex, bool>,
392 attr_flags: Table<DefIndex, AttrFlags>,
393 def_path_hashes: Table<DefIndex, u64>,
399 explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
400 explicit_item_self_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
401 inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
402 explicit_super_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
403 explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
404 explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
405 inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
406 associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
407 opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
408 module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
413 cross_crate_inlinable: Table<DefIndex, bool>,
414
415- optional:
416 attributes: Table<DefIndex, LazyArray<hir::Attribute>>,
417 module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
420 associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
421 def_kind: Table<DefIndex, DefKind>,
422 visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
423 safety: Table<DefIndex, hir::Safety>,
424 def_span: Table<DefIndex, LazyValue<Span>>,
425 def_ident_span: Table<DefIndex, LazyValue<Span>>,
426 lookup_stability: Table<DefIndex, LazyValue<attrs::Stability>>,
427 lookup_const_stability: Table<DefIndex, LazyValue<attrs::ConstStability>>,
428 lookup_default_body_stability: Table<DefIndex, LazyValue<attrs::DefaultBodyStability>>,
429 lookup_deprecation_entry: Table<DefIndex, LazyValue<attrs::Deprecation>>,
430 explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
431 generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
432 type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, Ty<'static>>>>,
433 variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
434 fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,
435 codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
436 impl_trait_header: Table<DefIndex, LazyValue<ty::ImplTraitHeader<'static>>>,
437 const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, rustc_middle::ty::Const<'static>>>>,
438 object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
439 optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
440 mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
441 closure_saved_names_of_captured_variables: Table<DefIndex, LazyValue<IndexVec<FieldIdx, Symbol>>>,
442 mir_coroutine_witnesses: Table<DefIndex, LazyValue<mir::CoroutineLayout<'static>>>,
443 promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
444 thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
445 impl_parent: Table<DefIndex, RawDefId>,
446 constness: Table<DefIndex, hir::Constness>,
447 const_conditions: Table<DefIndex, LazyValue<ty::ConstConditions<'static>>>,
448 defaultness: Table<DefIndex, hir::Defaultness>,
449 coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
451 mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
452 rendered_const: Table<DefIndex, LazyValue<String>>,
453 rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
454 asyncness: Table<DefIndex, ty::Asyncness>,
455 fn_arg_idents: Table<DefIndex, LazyArray<Option<Ident>>>,
456 coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
457 coroutine_for_closure: Table<DefIndex, RawDefId>,
458 adt_destructor: Table<DefIndex, LazyValue<ty::Destructor>>,
459 adt_async_destructor: Table<DefIndex, LazyValue<ty::AsyncDestructor>>,
460 coroutine_by_move_body_def_id: Table<DefIndex, RawDefId>,
461 eval_static_initializer: Table<DefIndex, LazyValue<mir::interpret::ConstAllocation<'static>>>,
462 trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
463 trait_item_def_id: Table<DefIndex, RawDefId>,
464 expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
465 default_fields: Table<DefIndex, LazyValue<DefId>>,
466 params_in_repr: Table<DefIndex, LazyValue<DenseBitSet<u32>>>,
467 repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
468 def_keys: Table<DefIndex, LazyValue<DefKey>>,
473 proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
474 variant_data: Table<DefIndex, LazyValue<VariantData>>,
475 assoc_container: Table<DefIndex, ty::AssocItemContainer>,
476 macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
477 proc_macro: Table<DefIndex, MacroKind>,
478 deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
479 trait_impl_trait_tys: Table<DefIndex, LazyValue<DefIdMap<ty::EarlyBinder<'static, Ty<'static>>>>>,
480 doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
481 doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
482 assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
483 opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
484 anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
485}
486
487#[derive(TyEncodable, TyDecodable)]
488struct VariantData {
489 idx: VariantIdx,
490 discr: ty::VariantDiscr,
491 ctor: Option<(CtorKind, DefIndex)>,
493 is_non_exhaustive: bool,
494}
495
496bitflags::bitflags! {
497 #[derive(Default)]
498 pub struct AttrFlags: u8 {
499 const IS_DOC_HIDDEN = 1 << 0;
500 }
501}
502
503#[derive(Encodable, Decodable, Copy, Clone)]
520struct SpanTag(u8);
521
522#[derive(Debug, Copy, Clone, PartialEq, Eq)]
523enum SpanKind {
524 Local = 0b00,
525 Foreign = 0b01,
526 Partial = 0b10,
527 Indirect = 0b11,
531}
532
533impl SpanTag {
534 fn new(kind: SpanKind, context: rustc_span::SyntaxContext, length: usize) -> SpanTag {
535 let mut data = 0u8;
536 data |= kind as u8;
537 if context.is_root() {
538 data |= 0b100;
539 }
540 let all_1s_len = (0xffu8 << 3) >> 3;
541 if length < all_1s_len as usize {
543 data |= (length as u8) << 3;
544 } else {
545 data |= all_1s_len << 3;
546 }
547
548 SpanTag(data)
549 }
550
551 fn indirect(relative: bool, length_bytes: u8) -> SpanTag {
552 let mut tag = SpanTag(SpanKind::Indirect as u8);
553 if relative {
554 tag.0 |= 0b100;
555 }
556 assert!(length_bytes <= 8);
557 tag.0 |= length_bytes << 3;
558 tag
559 }
560
561 fn kind(self) -> SpanKind {
562 let masked = self.0 & 0b11;
563 match masked {
564 0b00 => SpanKind::Local,
565 0b01 => SpanKind::Foreign,
566 0b10 => SpanKind::Partial,
567 0b11 => SpanKind::Indirect,
568 _ => unreachable!(),
569 }
570 }
571
572 fn is_relative_offset(self) -> bool {
573 debug_assert_eq!(self.kind(), SpanKind::Indirect);
574 self.0 & 0b100 != 0
575 }
576
577 fn context(self) -> Option<rustc_span::SyntaxContext> {
578 if self.0 & 0b100 != 0 { Some(rustc_span::SyntaxContext::root()) } else { None }
579 }
580
581 fn length(self) -> Option<rustc_span::BytePos> {
582 let all_1s_len = (0xffu8 << 3) >> 3;
583 let len = self.0 >> 3;
584 if len != all_1s_len { Some(rustc_span::BytePos(u32::from(len))) } else { None }
585 }
586}
587
588const SYMBOL_STR: u8 = 0;
590const SYMBOL_OFFSET: u8 = 1;
591const SYMBOL_PREDEFINED: u8 = 2;
592
593pub fn provide(providers: &mut Providers) {
594 encoder::provide(providers);
595 decoder::provide(providers);
596}
597
598trivially_parameterized_over_tcx! {
599 VariantData,
600 RawDefId,
601 TraitImpls,
602 IncoherentImpls,
603 CrateHeader,
604 CrateRoot,
605 CrateDep,
606 AttrFlags,
607}