Skip to main content

ridx_format/format/
field_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 ADT/variant fields (`FieldEntry`) and
12//! definition value/alias/constructor types (`DefTyEntry`) in `.ridx` format version 5.
13
14use super::TyId;
15
16/// `FieldEntry.flags`: positional tuple field (`.0`, `.1`, ...).
17pub const FIELD_FLAG_TUPLE: u8 = 1 << 0;
18/// `FieldEntry.flags`: `unsafe` field (`FieldDef.safety.is_unsafe()`).
19pub const FIELD_FLAG_UNSAFE: u8 = 1 << 1;
20
21/// `DefTyEntry.flags`: `#[non_exhaustive]` on `Enum` variant list or `Struct`/`Variant` field list.
22pub const DEF_TY_FLAG_NON_EXHAUSTIVE: u8 = 1 << 0;
23/// `DefTyEntry.flags`: `static mut` (`Mutability::Mut`).
24pub const DEF_TY_FLAG_MUT_STATIC: u8 = 1 << 1;
25/// `DefTyEntry.flags`: `unsafe` extern static (`Safety::Unsafe`).
26pub const DEF_TY_FLAG_UNSAFE_STATIC: u8 = 1 << 2;
27/// `DefTyEntry.flags`: `#[fundamental]` ADT (`Box<T>`, `Pin<P>`).
28pub const DEF_TY_FLAG_FUNDAMENTAL: u8 = 1 << 3;
29/// `DefTyEntry.flags`: trait `AssocTy` declaration carrying a default type (`type Item = DefaultTy;`).
30pub const DEF_TY_FLAG_DEFAULT_ASSOC: u8 = 1 << 4;
31
32/// Visibility level matching `DefEntry.visibility` (`0 = Public, 1 = Crate, 2 = Restricted, 3 = Private`).
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
34#[repr(u8)]
35pub enum ItemVis {
36    /// `pub`: visible everywhere including downstream crates.
37    #[default]
38    Public = 0,
39    /// `pub(crate)`: visible throughout the defining crate.
40    Crate = 1,
41    /// `pub(super)` / `pub(in ancestor)`: visible in an ancestor module within the defining crate.
42    Restricted = 2,
43    /// Private to the enclosing module.
44    Private = 3,
45}
46
47impl ItemVis {
48    #[inline]
49    pub const fn from_u8(v: u8) -> Self {
50        match v {
51            0 => Self::Public,
52            1 => Self::Crate,
53            2 => Self::Restricted,
54            _ => Self::Private,
55        }
56    }
57
58    #[inline]
59    pub const fn is_public(self) -> bool {
60        matches!(self, Self::Public)
61    }
62
63    #[inline]
64    pub const fn is_crate_visible(self) -> bool {
65        matches!(self, Self::Public | Self::Crate)
66    }
67}
68
69/// Syntactic/constructor shape of a `Struct`, `Union`, or `Enum` `Variant`.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
71#[repr(u8)]
72pub enum CtorShape {
73    /// Not a value/record constructor (`TyAlias`, `Const`, `Static`, `AssocConst`, `Enum`).
74    #[default]
75    None = 0,
76    /// Braced record (`struct Point { x: i32 }`, `Enum::Struct { a: u8 }`, `union Raw { ... }`).
77    Record = 1,
78    /// Tuple constructor (`struct Token(u64);`, `Option::Some(T)`); also indexed in `fn_sigs`.
79    Tuple = 2,
80    /// Unit constructor (`struct Marker;`, `Option::None`).
81    Unit = 3,
82}
83
84impl CtorShape {
85    #[inline]
86    pub const fn from_u8(v: u8) -> Self {
87        match v {
88            1 => Self::Record,
89            2 => Self::Tuple,
90            3 => Self::Unit,
91            _ => Self::None,
92        }
93    }
94}
95
96/// Structured field row for a `Struct`, `Union`, or `Enum` `Variant` (`20` bytes, `4`-byte aligned).
97///
98/// Sorted on disk by `(owner_def_index, field_idx)` so all fields of an ADT or enum variant
99/// form a contiguous slice discoverable via `O(log N)` binary search.
100#[repr(C)]
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
102pub struct FieldEntry {
103    /// Local `DefEntry` index of the owning `Struct`, `Union`, or `Variant`.
104    pub owner_def_index: u32,
105    /// Local `DefEntry` index of this `Field` (`u32::MAX` when no `DefEntry` was emitted).
106    pub def_index: u32,
107    /// Field identifier offset in `string_pool` (`"x"`, `"0"`, `"1"`, ...).
108    pub name_offset: u32,
109    /// Field `TyId` in `ty_entries` (in the owning ADT's generic parameter namespace).
110    pub ty: u32,
111    /// `0`-based declaration position inside the struct/union/variant.
112    pub field_idx: u16,
113    /// `ItemVis` (`0 = Public, 1 = Crate, 2 = Restricted, 3 = Private`).
114    pub vis: u8,
115    /// `FIELD_FLAG_*` bitmask (`FIELD_FLAG_TUPLE`, `FIELD_FLAG_UNSAFE`).
116    pub flags: u8,
117}
118
119impl FieldEntry {
120    #[inline]
121    pub const fn to_le(self) -> Self {
122        Self {
123            owner_def_index: self.owner_def_index.to_le(),
124            def_index: self.def_index.to_le(),
125            name_offset: self.name_offset.to_le(),
126            ty: self.ty.to_le(),
127            field_idx: self.field_idx.to_le(),
128            vis: self.vis,
129            flags: self.flags,
130        }
131    }
132
133    #[inline]
134    pub const fn from_le(self) -> Self {
135        Self {
136            owner_def_index: u32::from_le(self.owner_def_index),
137            def_index: u32::from_le(self.def_index),
138            name_offset: u32::from_le(self.name_offset),
139            ty: u32::from_le(self.ty),
140            field_idx: u16::from_le(self.field_idx),
141            vis: self.vis,
142            flags: self.flags,
143        }
144    }
145
146    #[inline]
147    pub const fn owner_def_index(&self) -> u32 {
148        u32::from_le(self.owner_def_index)
149    }
150
151    #[inline]
152    pub const fn def_index_opt(&self) -> Option<u32> {
153        let idx = u32::from_le(self.def_index);
154        if idx == u32::MAX {
155            None
156        } else {
157            Some(idx)
158        }
159    }
160
161    #[inline]
162    pub const fn ty_id(&self) -> TyId {
163        TyId(u32::from_le(self.ty))
164    }
165
166    #[inline]
167    pub const fn field_idx(&self) -> u16 {
168        u16::from_le(self.field_idx)
169    }
170
171    #[inline]
172    pub const fn visibility(&self) -> ItemVis {
173        ItemVis::from_u8(self.vis)
174    }
175
176    #[inline]
177    pub const fn is_tuple_field(&self) -> bool {
178        (self.flags & FIELD_FLAG_TUPLE) != 0
179    }
180
181    #[inline]
182    pub const fn is_unsafe(&self) -> bool {
183        (self.flags & FIELD_FLAG_UNSAFE) != 0
184    }
185}
186
187/// Structured type/constructor descriptor keyed by `def_index` (`16` bytes, `4`-byte aligned).
188///
189/// Covers `TyAlias` (RHS target `TyId`), `Const`/`Static`/`AssocConst` (value `TyId`),
190/// `Struct`/`Union`/`Enum` (identity ADT `TyId` + `CtorShape`), and `Variant` (owning `Enum`
191/// identity `TyId`, `parent_def_index`, `variant_idx`, and `CtorShape`).
192/// Sorted on disk by `def_index` for `O(log N)` binary search.
193#[repr(C)]
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
195pub struct DefTyEntry {
196    /// Local `DefEntry` index of this definition (sorted primary key).
197    pub def_index: u32,
198    /// `TyId` in `ty_entries` (RHS for `TyAlias`, value type for constants/statics, ADT type for ADTs/variants).
199    pub ty: u32,
200    /// Owning `Enum` `DefEntry` index for `Variant` (`u32::MAX` for non-variants).
201    pub parent_def_index: u32,
202    /// `0`-based `VariantIdx` (`0..N` for `Variant`, `0` for `Struct`/`Union`, `u16::MAX` otherwise).
203    pub variant_idx: u16,
204    /// `CtorShape` (`None = 0`, `Record = 1`, `Tuple = 2`, `Unit = 3`).
205    pub ctor_shape: u8,
206    /// `DEF_TY_FLAG_*` bitmask.
207    pub flags: u8,
208}
209
210impl DefTyEntry {
211    #[inline]
212    pub const fn to_le(self) -> Self {
213        Self {
214            def_index: self.def_index.to_le(),
215            ty: self.ty.to_le(),
216            parent_def_index: self.parent_def_index.to_le(),
217            variant_idx: self.variant_idx.to_le(),
218            ctor_shape: self.ctor_shape,
219            flags: self.flags,
220        }
221    }
222
223    #[inline]
224    pub const fn from_le(self) -> Self {
225        Self {
226            def_index: u32::from_le(self.def_index),
227            ty: u32::from_le(self.ty),
228            parent_def_index: u32::from_le(self.parent_def_index),
229            variant_idx: u16::from_le(self.variant_idx),
230            ctor_shape: self.ctor_shape,
231            flags: self.flags,
232        }
233    }
234
235    #[inline]
236    pub const fn def_index(&self) -> u32 {
237        u32::from_le(self.def_index)
238    }
239
240    #[inline]
241    pub const fn ty_id(&self) -> TyId {
242        TyId(u32::from_le(self.ty))
243    }
244
245    #[inline]
246    pub const fn parent_def_index_opt(&self) -> Option<u32> {
247        let p = u32::from_le(self.parent_def_index);
248        if p == u32::MAX {
249            None
250        } else {
251            Some(p)
252        }
253    }
254
255    #[inline]
256    pub const fn variant_idx_opt(&self) -> Option<u16> {
257        let v = u16::from_le(self.variant_idx);
258        if v == u16::MAX {
259            None
260        } else {
261            Some(v)
262        }
263    }
264
265    #[inline]
266    pub const fn ctor_shape(&self) -> CtorShape {
267        CtorShape::from_u8(self.ctor_shape)
268    }
269}