1 //! Macro ABI for version 1.55 of rustc
11 use libloading::Library;
12 use proc_macro_api::ProcMacroKind;
14 use super::PanicMessage;
16 pub(crate) struct Abi {
17 exported_macros: Vec<proc_macro::bridge::client::ProcMacro>,
20 impl From<proc_macro::bridge::PanicMessage> for PanicMessage {
21 fn from(p: proc_macro::bridge::PanicMessage) -> Self {
22 Self { message: p.as_str().map(|s| s.to_string()) }
27 pub unsafe fn from_lib(lib: &Library, symbol_name: String) -> Result<Abi, libloading::Error> {
28 let macros: libloading::Symbol<&&[proc_macro::bridge::client::ProcMacro]> =
29 lib.get(symbol_name.as_bytes())?;
30 Ok(Self { exported_macros: macros.to_vec() })
36 macro_body: &tt::Subtree,
37 attributes: Option<&tt::Subtree>,
38 ) -> Result<tt::Subtree, PanicMessage> {
39 let parsed_body = rustc_server::TokenStream::with_subtree(macro_body.clone());
41 let parsed_attributes = attributes.map_or(rustc_server::TokenStream::new(), |attr| {
42 rustc_server::TokenStream::with_subtree(attr.clone())
45 for proc_macro in &self.exported_macros {
47 proc_macro::bridge::client::ProcMacro::CustomDerive {
48 trait_name, client, ..
49 } if *trait_name == macro_name => {
51 &proc_macro::bridge::server::SameThread,
52 rustc_server::Rustc::default(),
56 return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
58 proc_macro::bridge::client::ProcMacro::Bang { name, client }
59 if *name == macro_name =>
62 &proc_macro::bridge::server::SameThread,
63 rustc_server::Rustc::default(),
67 return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
69 proc_macro::bridge::client::ProcMacro::Attr { name, client }
70 if *name == macro_name =>
73 &proc_macro::bridge::server::SameThread,
74 rustc_server::Rustc::default(),
79 return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
85 Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into())
88 pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
91 .map(|proc_macro| match proc_macro {
92 proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
93 (trait_name.to_string(), ProcMacroKind::CustomDerive)
95 proc_macro::bridge::client::ProcMacro::Bang { name, .. } => {
96 (name.to_string(), ProcMacroKind::FuncLike)
98 proc_macro::bridge::client::ProcMacro::Attr { name, .. } => {
99 (name.to_string(), ProcMacroKind::Attr)