1#[repr(C)]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub struct Span {
15 pub file_id: u32,
16 pub start_line: u32,
17 pub start_col: u32,
18 pub end_line: u32,
19 pub end_col: u32,
20}
21
22impl Span {
23 #[inline]
24 pub fn new(file_id: u32, start_line: u32, start_col: u32, end_line: u32, end_col: u32) -> Self {
25 Self { file_id, start_line, start_col, end_line, end_col }
26 }
27
28 #[inline]
29 pub fn to_le(self) -> Self {
30 Self {
31 file_id: self.file_id.to_le(),
32 start_line: self.start_line.to_le(),
33 start_col: self.start_col.to_le(),
34 end_line: self.end_line.to_le(),
35 end_col: self.end_col.to_le(),
36 }
37 }
38
39 #[inline]
40 pub fn from_le(self) -> Self {
41 Self {
42 file_id: u32::from_le(self.file_id),
43 start_line: u32::from_le(self.start_line),
44 start_col: u32::from_le(self.start_col),
45 end_line: u32::from_le(self.end_line),
46 end_col: u32::from_le(self.end_col),
47 }
48 }
49
50 #[inline]
51 pub fn file_id(&self) -> u32 {
52 u32::from_le(self.file_id)
53 }
54 #[inline]
55 pub fn start_line(&self) -> u32 {
56 u32::from_le(self.start_line)
57 }
58 #[inline]
59 pub fn start_col(&self) -> u32 {
60 u32::from_le(self.start_col)
61 }
62 #[inline]
63 pub fn end_line(&self) -> u32 {
64 u32::from_le(self.end_line)
65 }
66 #[inline]
67 pub fn end_col(&self) -> u32 {
68 u32::from_le(self.end_col)
69 }
70
71 #[inline]
73 pub fn contains(&self, file_id: u32, line: u32, col: u32) -> bool {
74 let s = self.from_le();
75 if s.file_id != file_id {
76 return false;
77 }
78 if line < s.start_line || line > s.end_line {
79 return false;
80 }
81 if line == s.start_line && col < s.start_col {
82 return false;
83 }
84 if line == s.end_line && col >= s.end_col {
85 return false;
86 }
87 true
88 }
89
90 #[inline]
92 pub fn approximate_size(&self) -> u64 {
93 let s = self.from_le();
94 let lines = (s.end_line.saturating_sub(s.start_line)) as u64;
95 if lines == 0 {
96 (s.end_col.saturating_sub(s.start_col)) as u64
97 } else {
98 (lines * 200 + (s.end_col as u64)).saturating_sub((s.start_col as u64).min(199))
99 }
100 }
101}
102
103#[repr(u8)]
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
106pub enum DefKind {
107 Mod = 0,
108 Struct = 1,
109 Union = 2,
110 Enum = 3,
111 Variant = 4,
112 Trait = 5,
113 TyAlias = 6,
114 ForeignTy = 7,
115 TraitAlias = 8,
116 AssocTy = 9,
117 TyParam = 10,
118 Fn = 11,
119 Const = 12,
120 ConstParam = 13,
121 Static = 14,
122 StructCtor = 15,
123 VariantCtor = 16,
124 AssocFn = 17,
125 AssocConst = 18,
126 Macro = 19,
127 ExternCrate = 20,
128 Use = 21,
129 ForeignMod = 22,
130 AnonConst = 23,
131 InlineConst = 24,
132 OpaqueTy = 25,
133 Field = 26,
134 LifetimeParam = 27,
135 GlobalAsm = 28,
136 Impl = 29,
137 Closure = 30,
138 Local = 31,
139 #[default]
140 Other = 255,
141}
142
143impl DefKind {
144 pub fn as_str(&self) -> &'static str {
145 match self {
146 DefKind::Mod => "mod",
147 DefKind::Struct => "struct",
148 DefKind::Union => "union",
149 DefKind::Enum => "enum",
150 DefKind::Variant => "variant",
151 DefKind::Trait => "trait",
152 DefKind::TyAlias => "type",
153 DefKind::ForeignTy => "type",
154 DefKind::TraitAlias => "trait",
155 DefKind::AssocTy => "type",
156 DefKind::TyParam => "type",
157 DefKind::Fn => "fn",
158 DefKind::Const => "const",
159 DefKind::ConstParam => "const",
160 DefKind::Static => "static",
161 DefKind::StructCtor => "struct",
162 DefKind::VariantCtor => "variant",
163 DefKind::AssocFn => "fn",
164 DefKind::AssocConst => "const",
165 DefKind::Macro => "macro",
166 DefKind::ExternCrate => "extern crate",
167 DefKind::Use => "use",
168 DefKind::ForeignMod => "mod",
169 DefKind::AnonConst => "const",
170 DefKind::InlineConst => "const",
171 DefKind::OpaqueTy => "type",
172 DefKind::Field => "field",
173 DefKind::LifetimeParam => "lifetime",
174 DefKind::GlobalAsm => "asm",
175 DefKind::Impl => "impl",
176 DefKind::Closure => "closure",
177 DefKind::Local => "let",
178 DefKind::Other => "other",
179 }
180 }
181}
182
183impl From<u8> for DefKind {
184 fn from(val: u8) -> Self {
185 match val {
186 0 => DefKind::Mod,
187 1 => DefKind::Struct,
188 2 => DefKind::Union,
189 3 => DefKind::Enum,
190 4 => DefKind::Variant,
191 5 => DefKind::Trait,
192 6 => DefKind::TyAlias,
193 7 => DefKind::ForeignTy,
194 8 => DefKind::TraitAlias,
195 9 => DefKind::AssocTy,
196 10 => DefKind::TyParam,
197 11 => DefKind::Fn,
198 12 => DefKind::Const,
199 13 => DefKind::ConstParam,
200 14 => DefKind::Static,
201 15 => DefKind::StructCtor,
202 16 => DefKind::VariantCtor,
203 17 => DefKind::AssocFn,
204 18 => DefKind::AssocConst,
205 19 => DefKind::Macro,
206 20 => DefKind::ExternCrate,
207 21 => DefKind::Use,
208 22 => DefKind::ForeignMod,
209 23 => DefKind::AnonConst,
210 24 => DefKind::InlineConst,
211 25 => DefKind::OpaqueTy,
212 26 => DefKind::Field,
213 27 => DefKind::LifetimeParam,
214 28 => DefKind::GlobalAsm,
215 29 => DefKind::Impl,
216 30 => DefKind::Closure,
217 31 => DefKind::Local,
218 _ => DefKind::Other,
219 }
220 }
221}
222
223pub const FLAG_MACRO_EXPANSION: u8 = 1 << 0;
225pub const FLAG_REEXPORT: u8 = 1 << 1;
226
227#[repr(C)]
229#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
230pub struct DefEntry {
231 pub def_id: u32,
233 pub kind: u8,
235 pub visibility: u8,
237 pub flags: u8,
239 pub _pad: u8,
240 pub name_offset: u32,
242 pub path_offset: u32,
244 pub sig_offset: u32,
246 pub span: Span,
248}
249
250impl DefEntry {
251 #[inline]
252 pub fn to_le(self) -> Self {
253 Self {
254 def_id: self.def_id.to_le(),
255 kind: self.kind,
256 visibility: self.visibility,
257 flags: self.flags,
258 _pad: self._pad,
259 name_offset: self.name_offset.to_le(),
260 path_offset: self.path_offset.to_le(),
261 sig_offset: self.sig_offset.to_le(),
262 span: self.span.to_le(),
263 }
264 }
265
266 #[inline]
267 pub fn from_le(self) -> Self {
268 Self {
269 def_id: u32::from_le(self.def_id),
270 kind: self.kind,
271 visibility: self.visibility,
272 flags: self.flags,
273 _pad: self._pad,
274 name_offset: u32::from_le(self.name_offset),
275 path_offset: u32::from_le(self.path_offset),
276 sig_offset: u32::from_le(self.sig_offset),
277 span: self.span.from_le(),
278 }
279 }
280
281 #[inline]
282 pub fn def_id(&self) -> u32 {
283 u32::from_le(self.def_id)
284 }
285 #[inline]
286 pub fn name_offset(&self) -> u32 {
287 u32::from_le(self.name_offset)
288 }
289 #[inline]
290 pub fn path_offset(&self) -> u32 {
291 u32::from_le(self.path_offset)
292 }
293 #[inline]
294 pub fn sig_offset(&self) -> u32 {
295 u32::from_le(self.sig_offset)
296 }
297 #[inline]
298 pub fn span(&self) -> Span {
299 self.span.from_le()
300 }
301 #[inline]
302 pub fn is_macro_expansion(&self) -> bool {
303 (self.flags & FLAG_MACRO_EXPANSION) != 0
304 }
305 #[inline]
306 pub fn is_reexport(&self) -> bool {
307 (self.flags & FLAG_REEXPORT) != 0
308 }
309 #[inline]
310 pub const fn item_vis(&self) -> super::ItemVis {
311 super::ItemVis::from_u8(self.visibility)
312 }
313}