Skip to main content

ridx_format/format/
macro_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//! Binary layout for declarative macro definitions (`macro_rules!` and Rust 2.0 `macro`).
12
13/// Macro was defined with `macro_rules!` (`true`) vs declarative `macro` 2.0 (`false`).
14pub const MACRO_FLAG_MACRO_RULES: u8 = 1 << 0;
15/// Macro carries `#[macro_export]`.
16pub const MACRO_FLAG_EXPORTED: u8 = 1 << 1;
17/// Macro carries `#[macro_export(local_inner_macros)]`.
18pub const MACRO_FLAG_LOCAL_INNER: u8 = 1 << 2;
19/// Macro carries `#[rustc_builtin_macro]` (compiler built-in expansion stub in `core`/`std`).
20pub const MACRO_FLAG_BUILTIN: u8 = 1 << 3;
21
22/// Outer delimiter of the macro definition body (`macro_rules! foo { ... }` / `( ... )` / `[ ... ]`).
23#[repr(u8)]
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub enum MacroDelim {
26    #[default]
27    Brace = 0,
28    Paren = 1,
29    Bracket = 2,
30}
31
32impl MacroDelim {
33    #[inline]
34    pub const fn from_u8(v: u8) -> Self {
35        match v {
36            1 => Self::Paren,
37            2 => Self::Bracket,
38            _ => Self::Brace,
39        }
40    }
41}
42
43/// Hygiene edition of the crate that defined the declarative macro.
44#[repr(u8)]
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
46pub enum MacroEdition {
47    Edition2015 = 0,
48    Edition2018 = 1,
49    #[default]
50    Edition2021 = 2,
51    Edition2024 = 3,
52}
53
54impl MacroEdition {
55    #[inline]
56    pub const fn from_u8(v: u8) -> Self {
57        match v {
58            0 => Self::Edition2015,
59            1 => Self::Edition2018,
60            2 => Self::Edition2021,
61            _ => Self::Edition2024,
62        }
63    }
64}
65
66/// Fixed-size 12-byte declarative macro table entry (`align 4`, zero wasted alignment gaps).
67///
68/// Entries in the `macro_defs` section are sorted strictly by `def_index` for `O(log N)` lookup.
69#[repr(C)]
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
71pub struct MacroDefEntry {
72    pub def_index: u32,
73    pub body_offset: u32,
74    pub flags: u8,
75    pub delim: u8,
76    pub edition: u8,
77    pub _pad: u8,
78}
79
80impl MacroDefEntry {
81    #[inline]
82    pub const fn new(def_index: u32, body_offset: u32, flags: u8, delim: MacroDelim, edition: MacroEdition) -> Self {
83        Self { def_index, body_offset, flags, delim: delim as u8, edition: edition as u8, _pad: 0 }
84    }
85
86    #[inline]
87    pub const fn def_index(self) -> u32 {
88        u32::from_le(self.def_index)
89    }
90
91    #[inline]
92    pub const fn body_offset(self) -> u32 {
93        u32::from_le(self.body_offset)
94    }
95
96    #[inline]
97    pub const fn is_macro_rules(self) -> bool {
98        (self.flags & MACRO_FLAG_MACRO_RULES) != 0
99    }
100
101    #[inline]
102    pub const fn is_exported(self) -> bool {
103        (self.flags & MACRO_FLAG_EXPORTED) != 0
104    }
105
106    #[inline]
107    pub const fn has_local_inner_macros(self) -> bool {
108        (self.flags & MACRO_FLAG_LOCAL_INNER) != 0
109    }
110
111    #[inline]
112    pub const fn is_builtin(self) -> bool {
113        (self.flags & MACRO_FLAG_BUILTIN) != 0
114    }
115
116    #[inline]
117    pub const fn delim_kind(self) -> MacroDelim {
118        MacroDelim::from_u8(self.delim)
119    }
120
121    #[inline]
122    pub const fn edition_kind(self) -> MacroEdition {
123        MacroEdition::from_u8(self.edition)
124    }
125
126    #[inline]
127    pub const fn to_le(self) -> Self {
128        Self { def_index: self.def_index.to_le(), body_offset: self.body_offset.to_le(), flags: self.flags, delim: self.delim, edition: self.edition, _pad: 0 }
129    }
130
131    #[inline]
132    pub const fn from_le(self) -> Self {
133        Self { def_index: u32::from_le(self.def_index), body_offset: u32::from_le(self.body_offset), flags: self.flags, delim: self.delim, edition: self.edition, _pad: 0 }
134    }
135}