1use super::ty_prim::{TyDefId, TyId};
15
16pub const GENERIC_OWNER_IMPL_BIT: u32 = 1 << 31;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum GenericOwner {
23 Def(u32),
25 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#[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
70pub const GENERIC_PARAM_FLAG_HAS_DEFAULT: u8 = 1 << 0;
72pub const GENERIC_PARAM_FLAG_SYNTHETIC: u8 = 1 << 1;
74pub const GENERIC_PARAM_FLAG_IS_SELF: u8 = 1 << 2;
76pub const GENERIC_PARAM_FLAG_SIZED: u8 = 1 << 3;
78pub const GENERIC_PARAM_FLAG_HOST_EFFECT: u8 = 1 << 4;
80
81#[repr(C)]
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub struct GenericParamEntry {
87 pub owner_key: u32,
89 pub def_index: u32,
91 pub name_offset: u32,
93 pub ty_or_default: u32,
97 pub param_idx: u16,
99 pub kind: u8,
101 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#[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
167pub const WHERE_FLAG_HRTB: u8 = 1 << 0;
169pub const WHERE_FLAG_NEGATIVE: u8 = 1 << 1;
171pub const WHERE_FLAG_CONST: u8 = 1 << 2;
173
174#[repr(C)]
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
179pub struct WherePredEntry {
180 pub owner_key: u32,
182 pub lhs_ty: u32,
184 pub trait_def: u32,
186 pub trait_args_start: u32,
188 pub rhs_or_region: u32,
192 pub assoc_name_offset: u32,
194 pub trait_args_len: u16,
196 pub kind: u8,
198 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};