]> git.lizzy.rs Git - rust.git/blob - crates/proc-macro-srv/src/abis/mod.rs
Support the 1.64 nightly proc macro 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_48;
28 mod abi_1_54;
29 mod abi_1_56;
30 mod abi_1_57;
31 mod abi_1_58;
32 mod abi_1_63;
33 mod abi_1_64;
34
35 use super::dylib::LoadProcMacroDylibError;
36 pub(crate) use abi_1_48::Abi as Abi_1_48;
37 pub(crate) use abi_1_54::Abi as Abi_1_54;
38 pub(crate) use abi_1_56::Abi as Abi_1_56;
39 pub(crate) use abi_1_57::Abi as Abi_1_57;
40 pub(crate) use abi_1_58::Abi as Abi_1_58;
41 pub(crate) use abi_1_63::Abi as Abi_1_63;
42 pub(crate) use abi_1_64::Abi as Abi_1_64;
43 use libloading::Library;
44 use proc_macro_api::{ProcMacroKind, RustCInfo};
45
46 pub struct PanicMessage {
47     message: Option<String>,
48 }
49
50 impl PanicMessage {
51     pub fn as_str(&self) -> Option<String> {
52         self.message.clone()
53     }
54 }
55
56 pub(crate) enum Abi {
57     Abi1_48(Abi_1_48),
58     Abi1_54(Abi_1_54),
59     Abi1_56(Abi_1_56),
60     Abi1_57(Abi_1_57),
61     Abi1_58(Abi_1_58),
62     Abi1_63(Abi_1_63),
63     Abi1_64(Abi_1_64),
64 }
65
66 impl Abi {
67     /// Load a new ABI.
68     ///
69     /// # Arguments
70     ///
71     /// *`lib` - The dynamic library containing the macro implementations
72     /// *`symbol_name` - The symbol name the macros can be found attributes
73     /// *`info` - RustCInfo about the compiler that was used to compile the
74     ///           macro crate. This is the information we use to figure out
75     ///           which ABI to return
76     pub fn from_lib(
77         lib: &Library,
78         symbol_name: String,
79         info: RustCInfo,
80     ) -> Result<Abi, LoadProcMacroDylibError> {
81         // FIXME: this should use exclusive ranges when they're stable
82         // https://github.com/rust-lang/rust/issues/37854
83         match (info.version.0, info.version.1) {
84             (1, 48..=53) => {
85                 let inner = unsafe { Abi_1_48::from_lib(lib, symbol_name) }?;
86                 Ok(Abi::Abi1_48(inner))
87             }
88             (1, 54..=55) => {
89                 let inner = unsafe { Abi_1_54::from_lib(lib, symbol_name) }?;
90                 Ok(Abi::Abi1_54(inner))
91             }
92             (1, 56) => {
93                 let inner = unsafe { Abi_1_56::from_lib(lib, symbol_name) }?;
94                 Ok(Abi::Abi1_56(inner))
95             }
96             (1, 57) => {
97                 let inner = unsafe { Abi_1_57::from_lib(lib, symbol_name) }?;
98                 Ok(Abi::Abi1_57(inner))
99             }
100             (1, 58..=62) => {
101                 let inner = unsafe { Abi_1_58::from_lib(lib, symbol_name) }?;
102                 Ok(Abi::Abi1_58(inner))
103             }
104             (1, 63) => {
105                 let inner = unsafe { Abi_1_63::from_lib(lib, symbol_name) }?;
106                 Ok(Abi::Abi1_63(inner))
107             }
108             (1, 64..) => {
109                 let inner = unsafe { Abi_1_64::from_lib(lib, symbol_name) }?;
110                 Ok(Abi::Abi1_64(inner))
111             }
112             _ => Err(LoadProcMacroDylibError::UnsupportedABI),
113         }
114     }
115
116     pub fn expand(
117         &self,
118         macro_name: &str,
119         macro_body: &tt::Subtree,
120         attributes: Option<&tt::Subtree>,
121     ) -> Result<tt::Subtree, PanicMessage> {
122         match self {
123             Self::Abi1_48(abi) => abi.expand(macro_name, macro_body, attributes),
124             Self::Abi1_54(abi) => abi.expand(macro_name, macro_body, attributes),
125             Self::Abi1_56(abi) => abi.expand(macro_name, macro_body, attributes),
126             Self::Abi1_57(abi) => abi.expand(macro_name, macro_body, attributes),
127             Self::Abi1_58(abi) => abi.expand(macro_name, macro_body, attributes),
128             Self::Abi1_63(abi) => abi.expand(macro_name, macro_body, attributes),
129             Self::Abi1_64(abi) => abi.expand(macro_name, macro_body, attributes),
130         }
131     }
132
133     pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
134         match self {
135             Self::Abi1_48(abi) => abi.list_macros(),
136             Self::Abi1_54(abi) => abi.list_macros(),
137             Self::Abi1_56(abi) => abi.list_macros(),
138             Self::Abi1_57(abi) => abi.list_macros(),
139             Self::Abi1_58(abi) => abi.list_macros(),
140             Self::Abi1_63(abi) => abi.list_macros(),
141             Self::Abi1_64(abi) => abi.list_macros(),
142         }
143     }
144 }