1 //! Procedural macros are implemented by compiling the macro providing crate
2 //! to a dynamic library with a particular ABI which the compiler uses to expand
3 //! macros. Unfortunately this ABI is not specified and can change from version
4 //! to version of the compiler. To support this we copy the ABI from the rust
5 //! compiler into submodules of this module (e.g proc_macro_srv::abis::abi_1_47).
7 //! All of these ABIs are subsumed in the `Abi` enum, which exposes a simple
8 //! interface the rest of rust analyzer can use to talk to the macro
11 //! # Adding a new ABI
13 //! To add a new ABI you'll need to copy the source of the target proc_macro
14 //! crate from the source tree of the Rust compiler into this directory tree.
15 //! Then you'll need to modify it
16 //! - Remove any feature! or other things which won't compile on stable
17 //! - change any absolute imports to relative imports within the ABI tree
19 //! Then you'll need to add a branch to the `Abi` enum and an implementation of
20 //! `Abi::expand`, `Abi::list_macros` and `Abi::from_lib` for the new ABI. See
21 //! `proc_macro_srv/src/abis/abi_1_47/mod.rs` for an example. Finally you'll
22 //! need to update the conditionals in `Abi::from_lib` to return your new ABI
23 //! for the relevant versions of the rust compiler
26 // pub(crate) so tests can use the TokenStream, more notes in test/utils.rs
27 pub(crate) mod abi_1_47;
32 use super::dylib::LoadProcMacroDylibError;
33 pub(crate) use abi_1_47::Abi as Abi_1_47;
34 pub(crate) use abi_1_55::Abi as Abi_1_55;
35 pub(crate) use abi_1_56::Abi as Abi_1_56;
36 pub(crate) use abi_1_58::Abi as Abi_1_58;
37 use libloading::Library;
38 use proc_macro_api::{ProcMacroKind, RustCInfo};
40 pub struct PanicMessage {
41 message: Option<String>,
45 pub fn as_str(&self) -> Option<String> {
62 /// *`lib` - The dynamic library containing the macro implementations
63 /// *`symbol_name` - The symbol name the macros can be found attributes
64 /// *`info` - RustCInfo about the compiler that was used to compile the
65 /// macro crate. This is the information we use to figure out
66 /// which ABI to return
71 ) -> Result<Abi, LoadProcMacroDylibError> {
72 if info.version.0 != 1 {
73 Err(LoadProcMacroDylibError::UnsupportedABI)
74 } else if info.version.1 < 47 {
75 Err(LoadProcMacroDylibError::UnsupportedABI)
76 } else if info.version.1 < 54 {
77 let inner = unsafe { Abi_1_47::from_lib(lib, symbol_name) }?;
78 Ok(Abi::Abi1_47(inner))
79 } else if info.version.1 < 56 {
80 let inner = unsafe { Abi_1_55::from_lib(lib, symbol_name) }?;
81 Ok(Abi::Abi1_55(inner))
82 } else if info.version.1 < 57 {
83 let inner = unsafe { Abi_1_56::from_lib(lib, symbol_name) }?;
84 Ok(Abi::Abi1_56(inner))
86 let inner = unsafe { Abi_1_58::from_lib(lib, symbol_name) }?;
87 Ok(Abi::Abi1_58(inner))
94 macro_body: &tt::Subtree,
95 attributes: Option<&tt::Subtree>,
96 ) -> Result<tt::Subtree, PanicMessage> {
98 Self::Abi1_55(abi) => abi.expand(macro_name, macro_body, attributes),
99 Self::Abi1_47(abi) => abi.expand(macro_name, macro_body, attributes),
100 Self::Abi1_56(abi) => abi.expand(macro_name, macro_body, attributes),
101 Self::Abi1_58(abi) => abi.expand(macro_name, macro_body, attributes),
105 pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
107 Self::Abi1_47(abi) => abi.list_macros(),
108 Self::Abi1_55(abi) => abi.list_macros(),
109 Self::Abi1_56(abi) => abi.list_macros(),
110 Self::Abi1_58(abi) => abi.list_macros(),