Skip to main content

ridx_format/format/
ty_prim.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//! Scalar identifiers, primitive widths, calling conventions, and generic argument words for `TyArena`.
12
13/// 32-bit interned type identifier within a shard's `TyArena`.
14#[repr(transparent)]
15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
16pub struct TyId(pub u32);
17
18impl TyId {
19    pub const NONE: Self = Self(u32::MAX);
20
21    #[inline]
22    pub const fn is_none(self) -> bool {
23        self.0 == u32::MAX
24    }
25
26    #[inline]
27    pub const fn is_some(self) -> bool {
28        self.0 != u32::MAX
29    }
30
31    #[inline]
32    pub const fn index(self) -> usize {
33        self.0 as usize
34    }
35}
36
37/// 32-bit interned type-level definition reference within a shard's `ty_defs` table.
38#[repr(transparent)]
39#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
40pub struct TyDefId(pub u32);
41
42impl TyDefId {
43    pub const NONE: Self = Self(u32::MAX);
44
45    #[inline]
46    pub const fn is_none(self) -> bool {
47        self.0 == u32::MAX
48    }
49
50    #[inline]
51    pub const fn is_some(self) -> bool {
52        self.0 != u32::MAX
53    }
54
55    #[inline]
56    pub const fn index(self) -> usize {
57        self.0 as usize
58    }
59}
60
61/// Reference or raw-pointer mutability.
62#[repr(u8)]
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
64pub enum TyMutability {
65    #[default]
66    Shared = 0,
67    Mut = 1,
68}
69
70impl TyMutability {
71    #[inline]
72    pub const fn from_u8(v: u8) -> Self {
73        if v == 1 {
74            Self::Mut
75        } else {
76            Self::Shared
77        }
78    }
79}
80
81/// Signed integer width.
82#[repr(u8)]
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
84pub enum IntTyWidth {
85    #[default]
86    Isize = 0,
87    I8 = 1,
88    I16 = 2,
89    I32 = 3,
90    I64 = 4,
91    I128 = 5,
92}
93
94impl IntTyWidth {
95    #[inline]
96    pub const fn from_u8(v: u8) -> Self {
97        match v {
98            1 => Self::I8,
99            2 => Self::I16,
100            3 => Self::I32,
101            4 => Self::I64,
102            5 => Self::I128,
103            _ => Self::Isize,
104        }
105    }
106
107    pub const fn as_str(self) -> &'static str {
108        match self {
109            Self::Isize => "isize",
110            Self::I8 => "i8",
111            Self::I16 => "i16",
112            Self::I32 => "i32",
113            Self::I64 => "i64",
114            Self::I128 => "i128",
115        }
116    }
117}
118
119/// Unsigned integer width.
120#[repr(u8)]
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
122pub enum UintTyWidth {
123    #[default]
124    Usize = 0,
125    U8 = 1,
126    U16 = 2,
127    U32 = 3,
128    U64 = 4,
129    U128 = 5,
130}
131
132impl UintTyWidth {
133    #[inline]
134    pub const fn from_u8(v: u8) -> Self {
135        match v {
136            1 => Self::U8,
137            2 => Self::U16,
138            3 => Self::U32,
139            4 => Self::U64,
140            5 => Self::U128,
141            _ => Self::Usize,
142        }
143    }
144
145    pub const fn as_str(self) -> &'static str {
146        match self {
147            Self::Usize => "usize",
148            Self::U8 => "u8",
149            Self::U16 => "u16",
150            Self::U32 => "u32",
151            Self::U64 => "u64",
152            Self::U128 => "u128",
153        }
154    }
155}
156
157/// Floating-point width.
158#[repr(u8)]
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
160pub enum FloatTyWidth {
161    F16 = 0,
162    #[default]
163    F32 = 1,
164    F64 = 2,
165    F128 = 3,
166}
167
168impl FloatTyWidth {
169    #[inline]
170    pub const fn from_u8(v: u8) -> Self {
171        match v {
172            0 => Self::F16,
173            1 => Self::F32,
174            3 => Self::F128,
175            _ => Self::F64,
176        }
177    }
178
179    pub const fn as_str(self) -> &'static str {
180        match self {
181            Self::F16 => "f16",
182            Self::F32 => "f32",
183            Self::F64 => "f64",
184            Self::F128 => "f128",
185        }
186    }
187}
188
189/// Function pointer ABI category.
190#[repr(u8)]
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
192pub enum FnAbiKind {
193    #[default]
194    Rust = 0,
195    C = 1,
196    System = 2,
197    RustCall = 3,
198    Other = 4,
199}
200
201impl FnAbiKind {
202    #[inline]
203    pub const fn from_u8(v: u8) -> Self {
204        Self::from_u16(v as u16)
205    }
206
207    #[inline]
208    pub const fn from_u16(v: u16) -> Self {
209        match v {
210            0 => Self::Rust,
211            1 => Self::C,
212            2 => Self::System,
213            3 => Self::RustCall,
214            _ => Self::Other,
215        }
216    }
217}
218
219pub const FN_FLAG_UNSAFE: u8 = 1 << 0;
220pub const FN_FLAG_VARIADIC: u8 = 1 << 1;
221pub const FN_FLAG_ASYNC: u8 = 1 << 2;
222pub const FN_FLAG_CONST: u8 = 1 << 3;
223pub const FN_FLAG_HAS_SELF: u8 = 1 << 4;
224pub const BOUND_FLAG_MAYBE: u32 = 1 << 0;
225
226/// Sentinel lifetime values encoded in `Ref` / `Dyn` / `GenericArgWord::from_lifetime`.
227pub const LIFETIME_ELIDED: u32 = 0;
228pub const LIFETIME_STATIC: u32 = 0x3fff_ffff;
229
230/// Const generic / array length representation encoded inside a single `u32` word.
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232pub enum ConstArg {
233    /// Inline non-negative integer constant (`0..=0x1fff_ffff`), e.g. `[u8; 32]`.
234    Value(u32),
235    /// Generic const parameter index (`const N: usize` -> `Param(idx)`).
236    Param(u32),
237    /// Interned `string_pool` offset for general constant expressions/literals.
238    ExprOffset(u32),
239}
240
241/// Packed 32-bit generic argument word stored in `ty_args`.
242#[repr(transparent)]
243#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
244pub struct GenericArgWord(pub u32);
245
246impl GenericArgWord {
247    pub const TAG_MASK: u32 = 0xc000_0000;
248    pub const PAYLOAD_MASK: u32 = 0x3fff_ffff;
249    pub const TAG_TYPE: u32 = 0x0000_0000;
250    pub const TAG_CONST_PARAM: u32 = 0x4000_0000;
251    pub const TAG_CONST_VAL: u32 = 0x8000_0000;
252    pub const TAG_LIFETIME: u32 = 0xc000_0000;
253    const CONST_EXPR_BIT: u32 = 0x2000_0000;
254    const CONST_SUB_MASK: u32 = 0x1fff_ffff;
255
256    #[inline]
257    pub const fn from_type(ty: TyId) -> Self {
258        debug_assert!(ty.0 <= Self::PAYLOAD_MASK);
259        Self(Self::TAG_TYPE | (ty.0 & Self::PAYLOAD_MASK))
260    }
261
262    #[inline]
263    pub const fn from_const(c: ConstArg) -> Self {
264        match c {
265            ConstArg::Param(idx) => Self(Self::TAG_CONST_PARAM | (idx & Self::PAYLOAD_MASK)),
266            ConstArg::Value(v) => Self(Self::TAG_CONST_VAL | (v & Self::CONST_SUB_MASK)),
267            ConstArg::ExprOffset(off) => Self(Self::TAG_CONST_VAL | Self::CONST_EXPR_BIT | (off & Self::CONST_SUB_MASK)),
268        }
269    }
270
271    #[inline]
272    pub const fn from_lifetime(lifetime_code: u32) -> Self {
273        Self(Self::TAG_LIFETIME | (lifetime_code & Self::PAYLOAD_MASK))
274    }
275
276    #[inline]
277    pub const fn decode_const(raw: u32) -> ConstArg {
278        let w = Self(raw);
279        match w.0 & Self::TAG_MASK {
280            Self::TAG_CONST_PARAM => ConstArg::Param(w.0 & Self::PAYLOAD_MASK),
281            _ => {
282                if (w.0 & Self::CONST_EXPR_BIT) != 0 {
283                    ConstArg::ExprOffset(w.0 & Self::CONST_SUB_MASK)
284                } else {
285                    ConstArg::Value(w.0 & Self::CONST_SUB_MASK)
286                }
287            }
288        }
289    }
290
291    #[inline]
292    pub const fn tag_bits(self) -> u32 {
293        self.0 & Self::TAG_MASK
294    }
295
296    #[inline]
297    pub const fn payload(self) -> u32 {
298        self.0 & Self::PAYLOAD_MASK
299    }
300}