]> git.lizzy.rs Git - rust.git/blob - crates/proc_macro_srv/src/abis/mod.rs
Copy the proc_macro crate for the 1.56 ABI
[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 mod abi_1_56;
30
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};
37
38 pub struct PanicMessage {
39     message: Option<String>,
40 }
41
42 impl PanicMessage {
43     pub fn as_str(&self) -> Option<String> {
44         self.message.clone()
45     }
46 }
47
48 pub(crate) enum Abi {
49     Abi1_47(Abi_1_47),
50     Abi1_55(Abi_1_55),
51     Abi1_56(Abi_1_56),
52 }
53
54 impl Abi {
55     /// Load a new ABI.
56     ///
57     /// # Arguments
58     ///
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
64     pub fn from_lib(
65         lib: &Library,
66         symbol_name: String,
67         info: RustCInfo,
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))
79         } else {
80             let inner = unsafe { Abi_1_56::from_lib(lib, symbol_name) }?;
81             Ok(Abi::Abi1_56(inner))
82         }
83     }
84
85     pub fn expand(
86         &self,
87         macro_name: &str,
88         macro_body: &tt::Subtree,
89         attributes: Option<&tt::Subtree>,
90     ) -> Result<tt::Subtree, PanicMessage> {
91         match self {
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),
95         }
96     }
97
98     pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
99         match self {
100             Self::Abi1_47(abi) => abi.list_macros(),
101             Self::Abi1_55(abi) => abi.list_macros(),
102             Self::Abi1_56(abi) => abi.list_macros(),
103         }
104     }
105 }