2021-03-18 13:56:37 -05:00
|
|
|
//! Nameres-specific procedural macro data and helpers.
|
|
|
|
|
|
|
|
use hir_expand::name::{AsName, Name};
|
|
|
|
|
|
|
|
use crate::attr::Attrs;
|
2023-01-31 04:49:49 -06:00
|
|
|
use crate::tt::{Leaf, TokenTree};
|
2021-03-18 13:56:37 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-03-08 18:13:38 -06:00
|
|
|
pub struct ProcMacroDef {
|
|
|
|
pub name: Name,
|
|
|
|
pub kind: ProcMacroKind,
|
2021-03-18 13:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-03-08 18:13:38 -06:00
|
|
|
pub enum ProcMacroKind {
|
2021-03-18 13:56:37 -05:00
|
|
|
CustomDerive { helpers: Box<[Name]> },
|
|
|
|
FnLike,
|
|
|
|
Attr,
|
|
|
|
}
|
|
|
|
|
2021-05-29 13:32:57 -05:00
|
|
|
impl ProcMacroKind {
|
2023-12-18 05:09:54 -06:00
|
|
|
pub(super) fn to_basedb_kind(&self) -> hir_expand::proc_macro::ProcMacroKind {
|
2021-05-29 13:32:57 -05:00
|
|
|
match self {
|
2023-12-18 05:09:54 -06:00
|
|
|
ProcMacroKind::CustomDerive { .. } => {
|
|
|
|
hir_expand::proc_macro::ProcMacroKind::CustomDerive
|
|
|
|
}
|
|
|
|
ProcMacroKind::FnLike => hir_expand::proc_macro::ProcMacroKind::FuncLike,
|
|
|
|
ProcMacroKind::Attr => hir_expand::proc_macro::ProcMacroKind::Attr,
|
2021-05-29 13:32:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 13:56:37 -05:00
|
|
|
impl Attrs {
|
|
|
|
#[rustfmt::skip]
|
2022-03-08 18:13:38 -06:00
|
|
|
pub fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
|
2022-02-11 15:06:03 -06:00
|
|
|
if self.is_proc_macro() {
|
2021-03-18 13:56:37 -05:00
|
|
|
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
|
2022-02-11 15:06:03 -06:00
|
|
|
} else if self.is_proc_macro_attribute() {
|
2021-03-18 13:56:37 -05:00
|
|
|
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr })
|
|
|
|
} else if self.by_key("proc_macro_derive").exists() {
|
2022-02-11 15:06:03 -06:00
|
|
|
let derive = self.by_key("proc_macro_derive").tt_values().next()?;
|
2022-11-07 06:24:17 -06:00
|
|
|
let def = parse_macro_name_and_helper_attrs(&derive.token_trees)
|
|
|
|
.map(|(name, helpers)| ProcMacroDef { name, kind: ProcMacroKind::CustomDerive { helpers } });
|
2021-03-18 13:56:37 -05:00
|
|
|
|
2022-11-07 06:24:17 -06:00
|
|
|
if def.is_none() {
|
|
|
|
tracing::trace!("malformed `#[proc_macro_derive]`: {}", derive);
|
2021-03-18 13:56:37 -05:00
|
|
|
}
|
2022-11-07 06:24:17 -06:00
|
|
|
|
|
|
|
def
|
2021-03-18 13:56:37 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 06:24:17 -06:00
|
|
|
|
|
|
|
// This fn is intended for `#[proc_macro_derive(..)]` and `#[rustc_builtin_macro(..)]`, which have
|
2023-04-13 17:35:00 -05:00
|
|
|
// the same structure.
|
2022-11-07 06:24:17 -06:00
|
|
|
#[rustfmt::skip]
|
|
|
|
pub(crate) fn parse_macro_name_and_helper_attrs(tt: &[TokenTree]) -> Option<(Name, Box<[Name]>)> {
|
|
|
|
match tt {
|
|
|
|
// `#[proc_macro_derive(Trait)]`
|
|
|
|
// `#[rustc_builtin_macro(Trait)]`
|
|
|
|
[TokenTree::Leaf(Leaf::Ident(trait_name))] => Some((trait_name.as_name(), Box::new([]))),
|
|
|
|
|
|
|
|
// `#[proc_macro_derive(Trait, attributes(helper1, helper2, ...))]`
|
|
|
|
// `#[rustc_builtin_macro(Trait, attributes(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((trait_name.as_name(), helpers))
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|