rustc_metadata/rmeta/
mod.rs

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
56/// Metadata encoding version.
57/// N.B., increment this if you change the format of metadata such that
58/// the rustc version can't be found to compare with `rustc_version()`.
59const METADATA_VERSION: u8 = 10;
60
61/// Metadata header which includes `METADATA_VERSION`.
62///
63/// This header is followed by the length of the compressed data, then
64/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian
65/// unsigned integer, and further followed by the rustc version string.
66pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
67
68/// A value of type T referred to by its absolute position
69/// in the metadata, and which can be decoded lazily.
70///
71/// Metadata is effective a tree, encoded in post-order,
72/// and with the root's position written next to the header.
73/// That means every single `LazyValue` points to some previous
74/// location in the metadata and is part of a larger node.
75///
76/// The first `LazyValue` in a node is encoded as the backwards
77/// distance from the position where the containing node
78/// starts and where the `LazyValue` points to, while the rest
79/// use the forward distance from the previous `LazyValue`.
80/// Distances start at 1, as 0-byte nodes are invalid.
81/// Also invalid are nodes being referred in a different
82/// order than they were encoded in.
83#[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
99/// A list of lazily-decoded values.
100///
101/// Unlike `LazyValue<Vec<T>>`, the length is encoded next to the
102/// position, not at the position, which means that the length
103/// doesn't need to be known before encoding all the elements.
104///
105/// If the length is 0, no position is encoded, but otherwise,
106/// the encoding is that of `LazyArray`, with the distinction that
107/// the minimal distance the length of the sequence, i.e.
108/// it's assumed there's no 0-byte element in the sequence.
109struct 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
131/// A list of lazily-decoded values, with the added capability of random access.
132///
133/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
134/// `LazyArray<T>`, but without requiring encoding or decoding all the values
135/// eagerly and in-order.
136struct LazyTable<I, T> {
137    position: NonZero<usize>,
138    /// The encoded size of the elements of a table is selected at runtime to drop
139    /// trailing zeroes. This is the number of bytes used for each table element.
140    width: usize,
141    /// How many elements are in the table.
142    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/// Encoding / decoding state for `Lazy`s (`LazyValue`, `LazyArray`, and `LazyTable`).
182#[derive(Copy, Clone, PartialEq, Eq, Debug)]
183enum LazyState {
184    /// Outside of a metadata node.
185    NoNode,
186
187    /// Inside a metadata node, and before any `Lazy`s.
188    /// The position is that of the node itself.
189    NodeStart(NonZero<usize>),
190
191    /// Inside a metadata node, with a previous `Lazy`s.
192    /// The position is where that previous `Lazy` would start.
193    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/// Serialized crate metadata.
208///
209/// This contains just enough information to determine if we should load the `CrateRoot` or not.
210/// Prefer [`CrateRoot`] whenever possible to avoid ICEs when using `omit-git-hash` locally.
211/// See #76720 for more details.
212///
213/// If you do modify this struct, also bump the [`METADATA_VERSION`] constant.
214#[derive(MetadataEncodable, MetadataDecodable)]
215pub(crate) struct CrateHeader {
216    pub(crate) triple: TargetTuple,
217    pub(crate) hash: Svh,
218    pub(crate) name: Symbol,
219    /// Whether this is the header for a proc-macro crate.
220    ///
221    /// This is separate from [`ProcMacroData`] to avoid having to update [`METADATA_VERSION`] every
222    /// time ProcMacroData changes.
223    pub(crate) is_proc_macro_crate: bool,
224    /// Whether this crate metadata section is just a stub.
225    /// Stubs do not contain the full metadata (it will be typically stored
226    /// in a separate rmeta file).
227    ///
228    /// This is used inside rlibs and dylibs when using `-Zembed-metadata=no`.
229    pub(crate) is_stub: bool,
230}
231
232/// Serialized `.rmeta` data for a crate.
233///
234/// When compiling a proc-macro crate, we encode many of
235/// the `LazyArray<T>` fields as `Lazy::empty()`. This serves two purposes:
236///
237/// 1. We avoid performing unnecessary work. Proc-macro crates can only
238/// export proc-macros functions, which are compiled into a shared library.
239/// As a result, a large amount of the information we normally store
240/// (e.g. optimized MIR) is unneeded by downstream crates.
241/// 2. We avoid serializing invalid `CrateNum`s. When we deserialize
242/// a proc-macro crate, we don't load any of its dependencies (since we
243/// just need to invoke a native function from the shared library).
244/// This means that any foreign `CrateNum`s that we serialize cannot be
245/// deserialized, since we will not know how to map them into the current
246/// compilation session. If we were to serialize a proc-macro crate like
247/// a normal crate, much of what we serialized would be unusable in addition
248/// to being unused.
249#[derive(MetadataEncodable, MetadataDecodable)]
250pub(crate) struct CrateRoot {
251    /// A header used to detect if this is the right crate to load.
252    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_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
286
287    syntax_contexts: SyntaxContextTable,
288    expn_data: ExpnDataTable,
289    expn_hashes: ExpnHashTable,
290
291    def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
292
293    source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
294    target_modifiers: LazyArray<TargetModifier>,
295
296    compiler_builtins: bool,
297    needs_allocator: bool,
298    needs_panic_runtime: bool,
299    no_builtins: bool,
300    panic_runtime: bool,
301    profiler_runtime: bool,
302    symbol_mangling_version: SymbolManglingVersion,
303
304    specialization_enabled_in: bool,
305}
306
307/// On-disk representation of `DefId`.
308/// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk
309/// representation and the compilation session.
310#[derive(Copy, Clone)]
311pub(crate) struct RawDefId {
312    krate: u32,
313    index: u32,
314}
315
316impl From<DefId> for RawDefId {
317    fn from(val: DefId) -> Self {
318        RawDefId { krate: val.krate.as_u32(), index: val.index.as_u32() }
319    }
320}
321
322impl RawDefId {
323    /// This exists so that `provide_one!` is happy
324    fn decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId {
325        self.decode_from_cdata(meta.0)
326    }
327
328    fn decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId {
329        let krate = CrateNum::from_u32(self.krate);
330        let krate = cdata.map_encoded_cnum_to_current(krate);
331        DefId { krate, index: DefIndex::from_u32(self.index) }
332    }
333}
334
335#[derive(Encodable, Decodable)]
336pub(crate) struct CrateDep {
337    pub name: Symbol,
338    pub hash: Svh,
339    pub host_hash: Option<Svh>,
340    pub kind: CrateDepKind,
341    pub extra_filename: String,
342    pub is_private: bool,
343}
344
345#[derive(MetadataEncodable, MetadataDecodable)]
346pub(crate) struct TraitImpls {
347    trait_id: (u32, DefIndex),
348    impls: LazyArray<(DefIndex, Option<SimplifiedType>)>,
349}
350
351#[derive(MetadataEncodable, MetadataDecodable)]
352pub(crate) struct IncoherentImpls {
353    self_ty: SimplifiedType,
354    impls: LazyArray<DefIndex>,
355}
356
357/// Define `LazyTables` and `TableBuilders` at the same time.
358macro_rules! define_tables {
359    (
360        - defaulted: $($name1:ident: Table<$IDX1:ty, $T1:ty>,)+
361        - optional: $($name2:ident: Table<$IDX2:ty, $T2:ty>,)+
362    ) => {
363        #[derive(MetadataEncodable, MetadataDecodable)]
364        pub(crate) struct LazyTables {
365            $($name1: LazyTable<$IDX1, $T1>,)+
366            $($name2: LazyTable<$IDX2, Option<$T2>>,)+
367        }
368
369        #[derive(Default)]
370        struct TableBuilders {
371            $($name1: TableBuilder<$IDX1, $T1>,)+
372            $($name2: TableBuilder<$IDX2, Option<$T2>>,)+
373        }
374
375        impl TableBuilders {
376            fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
377                LazyTables {
378                    $($name1: self.$name1.encode(buf),)+
379                    $($name2: self.$name2.encode(buf),)+
380                }
381            }
382        }
383    }
384}
385
386define_tables! {
387- defaulted:
388    intrinsic: Table<DefIndex, Option<LazyValue<ty::IntrinsicDef>>>,
389    is_macro_rules: Table<DefIndex, bool>,
390    type_alias_is_lazy: Table<DefIndex, bool>,
391    attr_flags: Table<DefIndex, AttrFlags>,
392    // The u64 is the crate-local part of the DefPathHash. All hashes in this crate have the same
393    // StableCrateId, so we omit encoding those into the table.
394    //
395    // Note also that this table is fully populated (no gaps) as every DefIndex should have a
396    // corresponding DefPathHash.
397    def_path_hashes: Table<DefIndex, u64>,
398    explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
399    explicit_item_self_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
400    inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
401    explicit_super_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
402    explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
403    explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
404    inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
405    associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
406    opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
407    // Reexported names are not associated with individual `DefId`s,
408    // e.g. a glob import can introduce a lot of names, all with the same `DefId`.
409    // That's why the encoded list needs to contain `ModChild` structures describing all the names
410    // individually instead of `DefId`s.
411    module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
412    cross_crate_inlinable: Table<DefIndex, bool>,
413
414- optional:
415    attributes: Table<DefIndex, LazyArray<hir::Attribute>>,
416    // For non-reexported names in a module every name is associated with a separate `DefId`,
417    // so we can take their names, visibilities etc from other encoded tables.
418    module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
419    associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
420    def_kind: Table<DefIndex, DefKind>,
421    visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
422    safety: Table<DefIndex, hir::Safety>,
423    def_span: Table<DefIndex, LazyValue<Span>>,
424    def_ident_span: Table<DefIndex, LazyValue<Span>>,
425    lookup_stability: Table<DefIndex, LazyValue<attrs::Stability>>,
426    lookup_const_stability: Table<DefIndex, LazyValue<attrs::ConstStability>>,
427    lookup_default_body_stability: Table<DefIndex, LazyValue<attrs::DefaultBodyStability>>,
428    lookup_deprecation_entry: Table<DefIndex, LazyValue<attrs::Deprecation>>,
429    explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
430    generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
431    type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, Ty<'static>>>>,
432    variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
433    fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,
434    codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
435    impl_trait_header: Table<DefIndex, LazyValue<ty::ImplTraitHeader<'static>>>,
436    const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, rustc_middle::ty::Const<'static>>>>,
437    object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
438    optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
439    mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
440    closure_saved_names_of_captured_variables: Table<DefIndex, LazyValue<IndexVec<FieldIdx, Symbol>>>,
441    mir_coroutine_witnesses: Table<DefIndex, LazyValue<mir::CoroutineLayout<'static>>>,
442    promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
443    thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
444    impl_parent: Table<DefIndex, RawDefId>,
445    constness: Table<DefIndex, hir::Constness>,
446    const_conditions: Table<DefIndex, LazyValue<ty::ConstConditions<'static>>>,
447    defaultness: Table<DefIndex, hir::Defaultness>,
448    // FIXME(eddyb) perhaps compute this on the fly if cheap enough?
449    coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
450    mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
451    rendered_const: Table<DefIndex, LazyValue<String>>,
452    rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
453    asyncness: Table<DefIndex, ty::Asyncness>,
454    fn_arg_idents: Table<DefIndex, LazyArray<Option<Ident>>>,
455    coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
456    coroutine_for_closure: Table<DefIndex, RawDefId>,
457    adt_destructor: Table<DefIndex, LazyValue<ty::Destructor>>,
458    adt_async_destructor: Table<DefIndex, LazyValue<ty::AsyncDestructor>>,
459    coroutine_by_move_body_def_id: Table<DefIndex, RawDefId>,
460    eval_static_initializer: Table<DefIndex, LazyValue<mir::interpret::ConstAllocation<'static>>>,
461    trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
462    trait_item_def_id: Table<DefIndex, RawDefId>,
463    expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
464    default_fields: Table<DefIndex, LazyValue<DefId>>,
465    params_in_repr: Table<DefIndex, LazyValue<DenseBitSet<u32>>>,
466    repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
467    // `def_keys` and `def_path_hashes` represent a lazy version of a
468    // `DefPathTable`. This allows us to avoid deserializing an entire
469    // `DefPathTable` up front, since we may only ever use a few
470    // definitions from any given crate.
471    def_keys: Table<DefIndex, LazyValue<DefKey>>,
472    proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
473    variant_data: Table<DefIndex, LazyValue<VariantData>>,
474    assoc_container: Table<DefIndex, ty::AssocItemContainer>,
475    macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
476    proc_macro: Table<DefIndex, MacroKind>,
477    deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
478    trait_impl_trait_tys: Table<DefIndex, LazyValue<DefIdMap<ty::EarlyBinder<'static, Ty<'static>>>>>,
479    doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
480    doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
481    assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
482    opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
483    anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
484}
485
486#[derive(TyEncodable, TyDecodable)]
487struct VariantData {
488    idx: VariantIdx,
489    discr: ty::VariantDiscr,
490    /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
491    ctor: Option<(CtorKind, DefIndex)>,
492    is_non_exhaustive: bool,
493}
494
495bitflags::bitflags! {
496    #[derive(Default)]
497    pub struct AttrFlags: u8 {
498        const IS_DOC_HIDDEN = 1 << 0;
499    }
500}
501
502/// A span tag byte encodes a bunch of data, so that we can cut out a few extra bytes from span
503/// encodings (which are very common, for example, libcore has ~650,000 unique spans and over 1.1
504/// million references to prior-written spans).
505///
506/// The byte format is split into several parts:
507///
508/// [ a a a a a c d d ]
509///
510/// `a` bits represent the span length. We have 5 bits, so we can store lengths up to 30 inline, with
511/// an all-1s pattern representing that the length is stored separately.
512///
513/// `c` represents whether the span context is zero (and then it is not stored as a separate varint)
514/// for direct span encodings, and whether the offset is absolute or relative otherwise (zero for
515/// absolute).
516///
517/// d bits represent the kind of span we are storing (local, foreign, partial, indirect).
518#[derive(Encodable, Decodable, Copy, Clone)]
519struct SpanTag(u8);
520
521#[derive(Debug, Copy, Clone, PartialEq, Eq)]
522enum SpanKind {
523    Local = 0b00,
524    Foreign = 0b01,
525    Partial = 0b10,
526    // Indicates the actual span contents are elsewhere.
527    // If this is the kind, then the span context bit represents whether it is a relative or
528    // absolute offset.
529    Indirect = 0b11,
530}
531
532impl SpanTag {
533    fn new(kind: SpanKind, context: rustc_span::SyntaxContext, length: usize) -> SpanTag {
534        let mut data = 0u8;
535        data |= kind as u8;
536        if context.is_root() {
537            data |= 0b100;
538        }
539        let all_1s_len = (0xffu8 << 3) >> 3;
540        // strictly less than - all 1s pattern is a sentinel for storage being out of band.
541        if length < all_1s_len as usize {
542            data |= (length as u8) << 3;
543        } else {
544            data |= all_1s_len << 3;
545        }
546
547        SpanTag(data)
548    }
549
550    fn indirect(relative: bool, length_bytes: u8) -> SpanTag {
551        let mut tag = SpanTag(SpanKind::Indirect as u8);
552        if relative {
553            tag.0 |= 0b100;
554        }
555        assert!(length_bytes <= 8);
556        tag.0 |= length_bytes << 3;
557        tag
558    }
559
560    fn kind(self) -> SpanKind {
561        let masked = self.0 & 0b11;
562        match masked {
563            0b00 => SpanKind::Local,
564            0b01 => SpanKind::Foreign,
565            0b10 => SpanKind::Partial,
566            0b11 => SpanKind::Indirect,
567            _ => unreachable!(),
568        }
569    }
570
571    fn is_relative_offset(self) -> bool {
572        debug_assert_eq!(self.kind(), SpanKind::Indirect);
573        self.0 & 0b100 != 0
574    }
575
576    fn context(self) -> Option<rustc_span::SyntaxContext> {
577        if self.0 & 0b100 != 0 { Some(rustc_span::SyntaxContext::root()) } else { None }
578    }
579
580    fn length(self) -> Option<rustc_span::BytePos> {
581        let all_1s_len = (0xffu8 << 3) >> 3;
582        let len = self.0 >> 3;
583        if len != all_1s_len { Some(rustc_span::BytePos(u32::from(len))) } else { None }
584    }
585}
586
587// Tags for encoding Symbol's
588const SYMBOL_STR: u8 = 0;
589const SYMBOL_OFFSET: u8 = 1;
590const SYMBOL_PREDEFINED: u8 = 2;
591
592pub fn provide(providers: &mut Providers) {
593    encoder::provide(providers);
594    decoder::provide(providers);
595}
596
597trivially_parameterized_over_tcx! {
598    VariantData,
599    RawDefId,
600    TraitImpls,
601    IncoherentImpls,
602    CrateHeader,
603    CrateRoot,
604    CrateDep,
605    AttrFlags,
606}