nandi/rustnimpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

macros.rs · 64 lines · 2.7 KBRust Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Shims for macros whose expansion cannot usefully be followed.
//!
//! The general rule in this project is that a macro whose expansion is not
//! known is rejected. `bitflags!` is an argued exception, and the argument is
//! this: expanding it does not help. `cargo rustc -Zunpretty=expanded` on a
//! one-flag user produces 869 lines that still call into
//! `bitflags::{Bits, Flag, Flags, iter, parser}` — which are themselves
//! defined by 26 further `macro_rules!` across five files. The chain does not
//! terminate in code we could lower.
//!
//! What the macro *means*, though, is small, documented and stable: a newtype
//! over an integer with named constants and set operations. So it is lowered
//! directly. That is not approximating a semantic we cannot represent — it is
//! implementing one we can, and `tests/cases/034-bitflags.rs` checks the
//! result against the real crate rather than against this comment.
//!
//! Verified against bitflags 2.13.2. Two behaviours are worth naming because
//! they are not what a reimplementation would guess: `!x` is complemented and
//! then masked to `all()`, and `from_bits` returns `None` for any bit outside
//! `all()`.

use syn::parse::{Parse, ParseStream};
use syn::{braced, Expr, Ident, Token, Type, Visibility};

/// One `pub struct Name: Repr { const A = ..; }` inside a `bitflags!`.
pub struct FlagsDef {
    pub name: Ident,
    pub repr: Type,
    pub flags: Vec<(Ident, Expr)>,
}

/// The whole macro body, which may declare more than one type.
pub struct BitflagsInput(pub Vec<FlagsDef>);

impl Parse for BitflagsInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut out = Vec::new();
        while !input.is_empty() {
            // Attributes on the struct (`#[derive(..)]`) carry no meaning we
            // need: the operations they derive are generated regardless.
            let _ = input.call(syn::Attribute::parse_outer)?;
            let _: Visibility = input.parse()?;
            input.parse::<Token![struct]>()?;
            let name: Ident = input.parse()?;
            input.parse::<Token![:]>()?;
            let repr: Type = input.parse()?;

            let body;
            braced!(body in input);
            let mut flags = Vec::new();
            while !body.is_empty() {
                let _ = body.call(syn::Attribute::parse_outer)?;
                body.parse::<Token![const]>()?;
                let fname: Ident = body.parse()?;
                body.parse::<Token![=]>()?;
                let value: Expr = body.parse()?;
                body.parse::<Token![;]>()?;
                flags.push((fname, value));
            }
            out.push(FlagsDef { name, repr, flags });
        }
        Ok(BitflagsInput(out))
    }
}