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;
31 use super::dylib::LoadProcMacroDylibError;
32 pub(crate) use abi_1_47::Abi as Abi_1_47;
33 pub(crate) use abi_1_55::Abi as Abi_1_55;
34 pub(crate) use abi_1_56::Abi as Abi_1_56;
35 use libloading::Library;
36 use proc_macro_api::{ProcMacroKind, RustCInfo};
38 pub struct PanicMessage {
39 message: Option<String>,
43 pub fn as_str(&self) -> Option<String> {
59 /// *`lib` - The dynamic library containing the macro implementations
60 /// *`symbol_name` - The symbol name the macros can be found attributes
61 /// *`info` - RustCInfo about the compiler that was used to compile the
62 /// macro crate. This is the information we use to figure out
63 /// which ABI to return
68 ) -> Result<Abi, LoadProcMacroDylibError> {
69 if info.version.0 != 1 {
70 Err(LoadProcMacroDylibError::UnsupportedABI)
71 } else if info.version.1 < 47 {
72 Err(LoadProcMacroDylibError::UnsupportedABI)
73 } else if info.version.1 < 54 {
74 let inner = unsafe { Abi_1_47::from_lib(lib, symbol_name) }?;
75 Ok(Abi::Abi1_47(inner))
76 } else if info.version.1 < 56 {
77 let inner = unsafe { Abi_1_55::from_lib(lib, symbol_name) }?;
78 Ok(Abi::Abi1_55(inner))
80 let inner = unsafe { Abi_1_56::from_lib(lib, symbol_name) }?;
81 Ok(Abi::Abi1_56(inner))
88 macro_body: &tt::Subtree,
89 attributes: Option<&tt::Subtree>,
90 ) -> Result<tt::Subtree, PanicMessage> {
92 Self::Abi1_55(abi) => abi.expand(macro_name, macro_body, attributes),
93 Self::Abi1_47(abi) => abi.expand(macro_name, macro_body, attributes),
94 Self::Abi1_56(abi) => abi.expand(macro_name, macro_body, attributes),
98 pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
100 Self::Abi1_47(abi) => abi.list_macros(),
101 Self::Abi1_55(abi) => abi.list_macros(),
102 Self::Abi1_56(abi) => abi.list_macros(),