aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/nameres/proc_macro.rs
blob: 3f095d623f950bf19cb8c2f5000fbbe691c35c8e (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Nameres-specific procedural macro data and helpers.

use hir_expand::name::{AsName, Name};
use tt::{Leaf, TokenTree};

use crate::attr::Attrs;

#[derive(Debug, PartialEq, Eq)]
pub(super) struct ProcMacroDef {
    pub(super) name: Name,
    pub(super) kind: ProcMacroKind,
}

#[derive(Debug, PartialEq, Eq)]
pub(super) enum ProcMacroKind {
    CustomDerive { helpers: Box<[Name]> },
    FnLike,
    Attr,
}

impl ProcMacroKind {
    pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
        match self {
            ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
            ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
            ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
        }
    }
}

impl Attrs {
    #[rustfmt::skip]
    pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
        if self.by_key("proc_macro").exists() {
            Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
        } else if self.by_key("proc_macro_attribute").exists() {
            Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr })
        } else if self.by_key("proc_macro_derive").exists() {
            let derive = self.by_key("proc_macro_derive").tt_values().next().unwrap();

            match &*derive.token_trees {
                // `#[proc_macro_derive(Trait)]`
                [TokenTree::Leaf(Leaf::Ident(trait_name))] => Some(ProcMacroDef {
                    name: trait_name.as_name(),
                    kind: ProcMacroKind::CustomDerive { helpers: Box::new([]) },
                }),

                // `#[proc_macro_derive(Trait, attibutes(helper1, helper2, ...))]`
                [
                    TokenTree::Leaf(Leaf::Ident(trait_name)),
                    TokenTree::Leaf(Leaf::Punct(comma)),
                    TokenTree::Leaf(Leaf::Ident(attributes)),
                    TokenTree::Subtree(helpers)
                ] if comma.char == ',' && attributes.text == "attributes" =>
                {
                    let helpers = helpers.token_trees.iter()
                        .filter(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Punct(comma)) if comma.char == ','))
                        .map(|tt| {
                            match tt {
                                TokenTree::Leaf(Leaf::Ident(helper)) => Some(helper.as_name()),
                                _ => None
                            }
                        })
                        .collect::<Option<Box<[_]>>>()?;

                    Some(ProcMacroDef {
                        name: trait_name.as_name(),
                        kind: ProcMacroKind::CustomDerive { helpers },
                    })
                }

                _ => {
                    log::trace!("malformed `#[proc_macro_derive]`: {}", derive);
                    None
                }
            }
        } else {
            None
        }
    }
}