]> git.lizzy.rs Git - rust.git/blob - crates/proc_macro_srv/src/abis/mod.rs
Merge #10776
[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 mod abi_1_58;
31
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};
39
40 pub struct PanicMessage {
41     message: Option<String>,
42 }
43
44 impl PanicMessage {
45     pub fn as_str(&self) -> Option<String> {
46         self.message.clone()
47     }
48 }
49
50 pub(crate) enum Abi {
51     Abi1_47(Abi_1_47),
52     Abi1_55(Abi_1_55),
53     Abi1_56(Abi_1_56),
54     Abi1_58(Abi_1_58),
55 }
56
57 impl Abi {
58     /// Load a new ABI.
59     ///
60     /// # Arguments
61     ///
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
67     pub fn from_lib(
68         lib: &Library,
69         symbol_name: String,
70         info: RustCInfo,
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))
85         } else {
86             let inner = unsafe { Abi_1_58::from_lib(lib, symbol_name) }?;
87             Ok(Abi::Abi1_58(inner))
88         }
89     }
90
91     pub fn expand(
92         &self,
93         macro_name: &str,
94         macro_body: &tt::Subtree,
95         attributes: Option<&tt::Subtree>,
96     ) -> Result<tt::Subtree, PanicMessage> {
97         match self {
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),
102         }
103     }
104
105     pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
106         match self {
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(),
111         }
112     }
113 }