Skip to main content

ridx_format/format/
ty.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)]` structures and discriminant tags for `TyArena` (`.ridx` format version 5).
12
13/// Discriminant tag for `TyEntry` (`Infer`-free by construction).
14#[repr(u8)]
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
16pub enum TyTag {
17    #[default]
18    Unit = 0,
19    Bool = 1,
20    Char = 2,
21    Int = 3,
22    Uint = 4,
23    Float = 5,
24    Str = 6,
25    Never = 7,
26    Adt = 8,
27    Ref = 9,
28    Ptr = 10,
29    Slice = 11,
30    Array = 12,
31    Tuple = 13,
32    FnPtr = 14,
33    FnDef = 15,
34    Param = 16,
35    Assoc = 17,
36    Dyn = 18,
37    ImplTrait = 19,
38    Unknown = 255,
39}
40
41impl TyTag {
42    #[inline]
43    pub const fn from_u8(v: u8) -> Self {
44        match v {
45            0 => Self::Unit,
46            1 => Self::Bool,
47            2 => Self::Char,
48            3 => Self::Int,
49            4 => Self::Uint,
50            5 => Self::Float,
51            6 => Self::Str,
52            7 => Self::Never,
53            8 => Self::Adt,
54            9 => Self::Ref,
55            10 => Self::Ptr,
56            11 => Self::Slice,
57            12 => Self::Array,
58            13 => Self::Tuple,
59            14 => Self::FnPtr,
60            15 => Self::FnDef,
61            16 => Self::Param,
62            17 => Self::Assoc,
63            18 => Self::Dyn,
64            19 => Self::ImplTrait,
65            _ => Self::Unknown,
66        }
67    }
68}
69
70/// Interned definition reference for ADTs, traits, and function items in `TyArena` (16 bytes).
71#[repr(C)]
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
73pub struct TyDefEntry {
74    pub path_hash: u64,
75    pub path_offset: u32,
76    /// Bit 31 == 0 => `local_def_idx`; Bit 31 == 1 => external `unit_key` offset in `string_pool` (masked by `0x7fff_ffff`).
77    pub loc_or_unit: u32,
78}
79
80impl TyDefEntry {
81    pub const EXTERN_BIT: u32 = 0x8000_0000;
82
83    #[inline]
84    pub const fn local(path_hash: u64, path_offset: u32, local_def_idx: u32) -> Self {
85        Self { path_hash, path_offset, loc_or_unit: local_def_idx & !Self::EXTERN_BIT }
86    }
87
88    #[inline]
89    pub const fn external(path_hash: u64, path_offset: u32, unit_offset: u32) -> Self {
90        Self { path_hash, path_offset, loc_or_unit: Self::EXTERN_BIT | (unit_offset & !Self::EXTERN_BIT) }
91    }
92
93    #[inline]
94    pub const fn to_le(self) -> Self {
95        Self { path_hash: self.path_hash.to_le(), path_offset: self.path_offset.to_le(), loc_or_unit: self.loc_or_unit.to_le() }
96    }
97
98    #[inline]
99    pub const fn from_le(self) -> Self {
100        Self { path_hash: u64::from_le(self.path_hash), path_offset: u32::from_le(self.path_offset), loc_or_unit: u32::from_le(self.loc_or_unit) }
101    }
102
103    #[inline]
104    pub const fn path_hash(&self) -> u64 {
105        u64::from_le(self.path_hash)
106    }
107
108    #[inline]
109    pub const fn path_offset(&self) -> u32 {
110        u32::from_le(self.path_offset)
111    }
112
113    #[inline]
114    pub const fn local_def_idx(&self) -> Option<u32> {
115        let v = u32::from_le(self.loc_or_unit);
116        if (v & Self::EXTERN_BIT) == 0 {
117            Some(v)
118        } else {
119            None
120        }
121    }
122
123    #[inline]
124    pub const fn extern_unit_offset(&self) -> Option<u32> {
125        let v = u32::from_le(self.loc_or_unit);
126        if (v & Self::EXTERN_BIT) != 0 {
127            Some(v & !Self::EXTERN_BIT)
128        } else {
129            None
130        }
131    }
132}
133
134/// Trait bound entry used by `Dyn`, `ImplTrait`, and generic predicates (16 bytes).
135#[repr(C)]
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
137pub struct TyBoundEntry {
138    pub trait_def: u32,
139    pub args_start: u32,
140    pub args_len: u16,
141    pub assoc_len: u16,
142    pub flags: u32,
143}
144
145impl TyBoundEntry {
146    #[inline]
147    pub const fn to_le(self) -> Self {
148        Self {
149            trait_def: self.trait_def.to_le(),
150            args_start: self.args_start.to_le(),
151            args_len: self.args_len.to_le(),
152            assoc_len: self.assoc_len.to_le(),
153            flags: self.flags.to_le(),
154        }
155    }
156
157    #[inline]
158    pub const fn from_le(self) -> Self {
159        Self {
160            trait_def: u32::from_le(self.trait_def),
161            args_start: u32::from_le(self.args_start),
162            args_len: u16::from_le(self.args_len),
163            assoc_len: u16::from_le(self.assoc_len),
164            flags: u32::from_le(self.flags),
165        }
166    }
167}
168
169/// Fixed-size 16-byte interned type node in `ty_entries`.
170#[repr(C)]
171#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
172pub struct TyEntry {
173    pub tag: u8,
174    pub flags: u8,
175    pub extra: u16,
176    pub a: u32,
177    pub b: u32,
178    pub c: u32,
179}
180
181impl TyEntry {
182    #[inline]
183    pub const fn to_le(self) -> Self {
184        Self { tag: self.tag, flags: self.flags, extra: self.extra.to_le(), a: self.a.to_le(), b: self.b.to_le(), c: self.c.to_le() }
185    }
186
187    #[inline]
188    pub const fn from_le(self) -> Self {
189        Self { tag: self.tag, flags: self.flags, extra: u16::from_le(self.extra), a: u32::from_le(self.a), b: u32::from_le(self.b), c: u32::from_le(self.c) }
190    }
191}
192
193/// Structured function or method signature (`16` bytes, align `4`), sorted ascending by `def_index`.
194#[repr(C)]
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
196pub struct FnSigEntry {
197    /// 0-based index into the `defs` section (`reader.definitions()[def_index as usize]`).
198    pub def_index: u32,
199    /// Start index of input parameter `TyId` words in `ty_args`.
200    pub inputs_start: u32,
201    /// Number of input parameters (including receiver when `FN_FLAG_HAS_SELF` is set).
202    pub inputs_len: u16,
203    /// Bitmask of `FN_FLAG_UNSAFE | FN_FLAG_VARIADIC | FN_FLAG_ASYNC | FN_FLAG_CONST | FN_FLAG_HAS_SELF`.
204    pub flags: u8,
205    /// ABI category (`FnAbiKind as u8`).
206    pub abi: u8,
207    /// Return type `TyId` in `ty_entries`.
208    pub output: u32,
209}
210
211impl FnSigEntry {
212    #[inline]
213    pub const fn to_le(self) -> Self {
214        Self {
215            def_index: self.def_index.to_le(),
216            inputs_start: self.inputs_start.to_le(),
217            inputs_len: self.inputs_len.to_le(),
218            flags: self.flags,
219            abi: self.abi,
220            output: self.output.to_le(),
221        }
222    }
223
224    #[inline]
225    pub const fn from_le(self) -> Self {
226        Self {
227            def_index: u32::from_le(self.def_index),
228            inputs_start: u32::from_le(self.inputs_start),
229            inputs_len: u16::from_le(self.inputs_len),
230            flags: self.flags,
231            abi: self.abi,
232            output: u32::from_le(self.output),
233        }
234    }
235
236    #[inline]
237    pub const fn def_index(&self) -> u32 {
238        u32::from_le(self.def_index)
239    }
240
241    #[inline]
242    pub const fn inputs_start(&self) -> u32 {
243        u32::from_le(self.inputs_start)
244    }
245
246    #[inline]
247    pub const fn inputs_len(&self) -> u16 {
248        u16::from_le(self.inputs_len)
249    }
250
251    #[inline]
252    pub const fn output_ty(&self) -> super::ty_prim::TyId {
253        super::ty_prim::TyId(u32::from_le(self.output))
254    }
255
256    #[inline]
257    pub const fn abi_kind(&self) -> super::ty_prim::FnAbiKind {
258        super::ty_prim::FnAbiKind::from_u8(self.abi)
259    }
260}