Skip to main content

ridx_format/format/
generic_item.rs

1/*
2 * rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
3 * Copyright (c) 2026 Alexander Panasenko
4 *
5 * Contact: alex@prod.codes
6 * Author: https://prod.codes/about/
7 * Project: https://github.com/alex09x/rust-intelligence
8 * SPDX-License-Identifier: MIT OR Apache-2.0
9 */
10
11//! On-disk `#[repr(C)]` layout for structured generic parameter declarations (`GenericParamEntry`)
12//! and `where` / bound / supertrait / projection predicates (`WherePredEntry`) in `.ridx` v5.
13
14use super::ty_prim::{TyDefId, TyId};
15
16/// High-bit tag in `owner_key` distinguishing `ImplEntry` owners (`0x8000_0000 | impl_index`)
17/// from `DefEntry` owners (`0..0x7fff_ffff`).
18pub const GENERIC_OWNER_IMPL_BIT: u32 = 1 << 31;
19
20/// Owner of a `GenericParamEntry` or `WherePredEntry` row (`DefEntry` or `ImplEntry`).
21#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum GenericOwner {
23    /// Local `DefEntry` index (`0..2^31`).
24    Def(u32),
25    /// Shard `ImplEntry` index (`0..impls().len()`), including metadata-only sysroot impls.
26    Impl(u32),
27}
28
29impl GenericOwner {
30    #[inline]
31    pub const fn to_raw(self) -> u32 {
32        match self {
33            Self::Def(idx) => idx & !GENERIC_OWNER_IMPL_BIT,
34            Self::Impl(idx) => GENERIC_OWNER_IMPL_BIT | (idx & !GENERIC_OWNER_IMPL_BIT),
35        }
36    }
37
38    #[inline]
39    pub const fn from_raw(raw: u32) -> Self {
40        if (raw & GENERIC_OWNER_IMPL_BIT) != 0 {
41            Self::Impl(raw & !GENERIC_OWNER_IMPL_BIT)
42        } else {
43            Self::Def(raw)
44        }
45    }
46}
47
48/// Discriminator of a generic parameter (`1` byte).
49#[repr(u8)]
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
51pub enum GenericParamKind {
52    #[default]
53    Lifetime = 0,
54    Type = 1,
55    Const = 2,
56}
57
58impl GenericParamKind {
59    #[inline]
60    pub const fn from_u8(v: u8) -> Self {
61        match v {
62            0 => Self::Lifetime,
63            1 => Self::Type,
64            2 => Self::Const,
65            _ => Self::Type,
66        }
67    }
68}
69
70/// `GenericParamEntry.flags`: parameter declares a default (`type T = U` or `const N: usize = 4`).
71pub const GENERIC_PARAM_FLAG_HAS_DEFAULT: u8 = 1 << 0;
72/// `GenericParamEntry.flags`: synthetic `impl Trait` in argument position (APIT).
73pub const GENERIC_PARAM_FLAG_SYNTHETIC: u8 = 1 << 1;
74/// `GenericParamEntry.flags`: implicit `Self` parameter (`index == 0`) on a `Trait` or `TraitAlias`.
75pub const GENERIC_PARAM_FLAG_IS_SELF: u8 = 1 << 2;
76/// `GenericParamEntry.flags`: `Type` parameter has `Sized` bound (unset when `?Sized`).
77pub const GENERIC_PARAM_FLAG_SIZED: u8 = 1 << 3;
78/// `GenericParamEntry.flags`: compiler host/const effect parameter.
79pub const GENERIC_PARAM_FLAG_HOST_EFFECT: u8 = 1 << 4;
80
81/// Structured generic parameter declaration (`20` bytes, `4`-byte aligned).
82///
83/// Sorted on disk by `(owner_key, param_idx)` for `O(log N)` binary search per `DefEntry` or `ImplEntry`.
84#[repr(C)]
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub struct GenericParamEntry {
87    /// Encoded `GenericOwner` (`Def(def_idx)` or `Impl(impl_idx)`).
88    pub owner_key: u32,
89    /// Local `DefEntry` index of this parameter if present in `defs`, or `u32::MAX`.
90    pub def_index: u32,
91    /// Parameter name offset in `string_pool` (e.g. `"T"`, `"'a"`, `"N"`, `"Self"`).
92    pub name_offset: u32,
93    /// For `Type` with `HAS_DEFAULT`: default `TyId` (`u32::MAX` if no default).
94    /// For `Const`: value `TyId` of the const parameter (e.g. `usize`).
95    /// For `Lifetime`: `u32::MAX`.
96    pub ty_or_default: u32,
97    /// Exact `rustc` `GenericParamDef.index` matching `TyKindView::Param { index, .. }`.
98    pub param_idx: u16,
99    /// `GenericParamKind` (`0 = Lifetime, 1 = Type, 2 = Const`).
100    pub kind: u8,
101    /// `GENERIC_PARAM_FLAG_*` bitmask.
102    pub flags: u8,
103}
104
105impl GenericParamEntry {
106    #[inline]
107    pub const fn to_le(self) -> Self {
108        Self {
109            owner_key: self.owner_key.to_le(),
110            def_index: self.def_index.to_le(),
111            name_offset: self.name_offset.to_le(),
112            ty_or_default: self.ty_or_default.to_le(),
113            param_idx: self.param_idx.to_le(),
114            kind: self.kind,
115            flags: self.flags,
116        }
117    }
118
119    #[inline]
120    pub const fn from_le(self) -> Self {
121        Self {
122            owner_key: u32::from_le(self.owner_key),
123            def_index: u32::from_le(self.def_index),
124            name_offset: u32::from_le(self.name_offset),
125            ty_or_default: u32::from_le(self.ty_or_default),
126            param_idx: u16::from_le(self.param_idx),
127            kind: self.kind,
128            flags: self.flags,
129        }
130    }
131
132    #[inline]
133    pub const fn owner_key(&self) -> u32 {
134        u32::from_le(self.owner_key)
135    }
136
137    #[inline]
138    pub const fn param_idx(&self) -> u16 {
139        u16::from_le(self.param_idx)
140    }
141}
142
143/// Discriminator of a `WherePredEntry` clause (`1` byte).
144#[repr(u8)]
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
146pub enum WherePredKind {
147    #[default]
148    TraitBound = 0,
149    ProjectionEq = 1,
150    TypeOutlives = 2,
151    RegionOutlives = 3,
152}
153
154impl WherePredKind {
155    #[inline]
156    pub const fn from_u8(v: u8) -> Self {
157        match v {
158            0 => Self::TraitBound,
159            1 => Self::ProjectionEq,
160            2 => Self::TypeOutlives,
161            3 => Self::RegionOutlives,
162            _ => Self::TraitBound,
163        }
164    }
165}
166
167/// `WherePredEntry.flags`: higher-ranked binder `for<'a>` wraps this predicate.
168pub const WHERE_FLAG_HRTB: u8 = 1 << 0;
169/// `WherePredEntry.flags`: negative polarity (`T: !Trait`).
170pub const WHERE_FLAG_NEGATIVE: u8 = 1 << 1;
171/// `WherePredEntry.flags`: `const` trait bound (`T: ~const Trait` / `const Trait`).
172pub const WHERE_FLAG_CONST: u8 = 1 << 2;
173
174/// Structured `where` / inline bound / supertrait / assoc-item bound predicate (`28` bytes, `4`-byte aligned).
175///
176/// Sorted on disk by `owner_key` (`GenericOwner::to_raw()`), preserving declaration order per owner.
177#[repr(C)]
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
179pub struct WherePredEntry {
180    /// Encoded `GenericOwner` (`Def(def_idx)` or `Impl(impl_idx)`).
181    pub owner_key: u32,
182    /// Constrained `TyId` (`TraitBound`, `ProjectionEq` `self_ty`, `TypeOutlives`), or lhs region (`RegionOutlives`).
183    pub lhs_ty: u32,
184    /// Target trait `TyDefId` (`TraitBound` / `ProjectionEq`), or `u32::MAX` for outlives clauses.
185    pub trait_def: u32,
186    /// Start index in `ty_args` for `trait_def`'s generic args excluding `Self` (`0` if empty).
187    pub trait_args_start: u32,
188    /// For `ProjectionEq`: RHS `TyId` (`u32::MAX` if non-type const term).
189    /// For `TypeOutlives` / `RegionOutlives`: target lifetime (`LIFETIME_STATIC`, `LIFETIME_ELIDED`, or region index).
190    /// For `TraitBound`: `u32::MAX`.
191    pub rhs_or_region: u32,
192    /// Associated item name offset in `string_pool` for `ProjectionEq` (e.g. `"Item"`, `"Output"`), else `u32::MAX`.
193    pub assoc_name_offset: u32,
194    /// Number of `GenericArgWord`s starting at `trait_args_start`.
195    pub trait_args_len: u16,
196    /// `WherePredKind` (`0..=3`).
197    pub kind: u8,
198    /// `WHERE_FLAG_*` bitmask.
199    pub flags: u8,
200}
201
202impl WherePredEntry {
203    #[inline]
204    pub const fn to_le(self) -> Self {
205        Self {
206            owner_key: self.owner_key.to_le(),
207            lhs_ty: self.lhs_ty.to_le(),
208            trait_def: self.trait_def.to_le(),
209            trait_args_start: self.trait_args_start.to_le(),
210            rhs_or_region: self.rhs_or_region.to_le(),
211            assoc_name_offset: self.assoc_name_offset.to_le(),
212            trait_args_len: self.trait_args_len.to_le(),
213            kind: self.kind,
214            flags: self.flags,
215        }
216    }
217
218    #[inline]
219    pub const fn from_le(self) -> Self {
220        Self {
221            owner_key: u32::from_le(self.owner_key),
222            lhs_ty: u32::from_le(self.lhs_ty),
223            trait_def: u32::from_le(self.trait_def),
224            trait_args_start: u32::from_le(self.trait_args_start),
225            rhs_or_region: u32::from_le(self.rhs_or_region),
226            assoc_name_offset: u32::from_le(self.assoc_name_offset),
227            trait_args_len: u16::from_le(self.trait_args_len),
228            kind: self.kind,
229            flags: self.flags,
230        }
231    }
232
233    #[inline]
234    pub const fn owner_key(&self) -> u32 {
235        u32::from_le(self.owner_key)
236    }
237
238    #[inline]
239    pub const fn trait_def_opt(&self) -> Option<TyDefId> {
240        let d = u32::from_le(self.trait_def);
241        if d == u32::MAX {
242            None
243        } else {
244            Some(TyDefId(d))
245        }
246    }
247
248    #[inline]
249    pub const fn lhs_ty_id(&self) -> TyId {
250        TyId(u32::from_le(self.lhs_ty))
251    }
252}
253
254const _: () = {
255    assert!(std::mem::size_of::<GenericParamEntry>() == 20);
256    assert!(std::mem::align_of::<GenericParamEntry>() == 4);
257    assert!(std::mem::size_of::<WherePredEntry>() == 28);
258    assert!(std::mem::align_of::<WherePredEntry>() == 4);
259};