]> git.lizzy.rs Git - rust.git/blob - crates/proc_macro_srv/src/abis/mod.rs
Merge #9585
[rust.git] / crates / proc_macro_srv / src / abis / mod.rs
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).
6 //!
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
9 //! provider.
10 //!
11 //! # Adding a new ABI
12 //!
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
18 //!
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
24 //!
25
26 // pub(crate) so tests can use the TokenStream, more notes in test/utils.rs
27 pub(crate) mod abi_1_47;
28 mod abi_1_55;
29
30 use super::dylib::LoadProcMacroDylibError;
31 pub(crate) use abi_1_47::Abi as Abi_1_47;
32 pub(crate) use abi_1_55::Abi as Abi_1_55;
33 use libloading::Library;
34 use proc_macro_api::{ProcMacroKind, RustCInfo};
35
36 pub struct PanicMessage {
37     message: Option<String>,
38 }
39
40 impl PanicMessage {
41     pub fn as_str(&self) -> Option<String> {
42         self.message.clone()
43     }
44 }
45
46 pub(crate) enum Abi {
47     Abi1_47(Abi_1_47),
48     Abi1_55(Abi_1_55),
49 }
50
51 impl Abi {
52     /// Load a new ABI.
53     ///
54     /// # Arguments
55     ///
56     /// *`lib` - The dynamic library containing the macro implementations
57     /// *`symbol_name` - The symbol name the macros can be found attributes
58     /// *`info` - RustCInfo about the compiler that was used to compile the
59     ///           macro crate. This is the information we use to figure out
60     ///           which ABI to return
61     pub fn from_lib(
62         lib: &Library,
63         symbol_name: String,
64         info: RustCInfo,
65     ) -> Result<Abi, LoadProcMacroDylibError> {
66         if info.version.0 != 1 {
67             Err(LoadProcMacroDylibError::UnsupportedABI)
68         } else if info.version.1 < 47 {
69             Err(LoadProcMacroDylibError::UnsupportedABI)
70         } else if info.version.1 < 54 {
71             let inner = unsafe { Abi_1_47::from_lib(lib, symbol_name) }?;
72             Ok(Abi::Abi1_47(inner))
73         } else {
74             let inner = unsafe { Abi_1_55::from_lib(lib, symbol_name) }?;
75             Ok(Abi::Abi1_55(inner))
76         }
77     }
78
79     pub fn expand(
80         &self,
81         macro_name: &str,
82         macro_body: &tt::Subtree,
83         attributes: Option<&tt::Subtree>,
84     ) -> Result<tt::Subtree, PanicMessage> {
85         match self {
86             Self::Abi1_55(abi) => abi.expand(macro_name, macro_body, attributes),
87             Self::Abi1_47(abi) => abi.expand(macro_name, macro_body, attributes),
88         }
89     }
90
91     pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
92         match self {
93             Self::Abi1_47(abi) => abi.list_macros(),
94             Self::Abi1_55(abi) => abi.list_macros(),
95         }
96     }
97 }