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