diff options
Diffstat (limited to 'crates/proc_macro_srv/src/lib.rs')
-rw-r--r-- | crates/proc_macro_srv/src/lib.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/crates/proc_macro_srv/src/lib.rs b/crates/proc_macro_srv/src/lib.rs new file mode 100644 index 000000000..7e4e4ad50 --- /dev/null +++ b/crates/proc_macro_srv/src/lib.rs | |||
@@ -0,0 +1,69 @@ | |||
1 | //! RA Proc Macro Server | ||
2 | //! | ||
3 | //! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code. | ||
4 | //! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander. | ||
5 | //! | ||
6 | //! But we adapt it to better fit RA needs: | ||
7 | //! | ||
8 | //! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with | ||
9 | //! RA than `proc-macro2` token stream. | ||
10 | //! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` | ||
11 | //! rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)… | ||
12 | |||
13 | #[allow(dead_code)] | ||
14 | #[doc(hidden)] | ||
15 | mod proc_macro; | ||
16 | |||
17 | #[doc(hidden)] | ||
18 | mod rustc_server; | ||
19 | |||
20 | mod dylib; | ||
21 | |||
22 | use proc_macro::bridge::client::TokenStream; | ||
23 | use proc_macro_api::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; | ||
24 | use std::{ | ||
25 | collections::{hash_map::Entry, HashMap}, | ||
26 | fs, | ||
27 | path::{Path, PathBuf}, | ||
28 | time::SystemTime, | ||
29 | }; | ||
30 | |||
31 | #[derive(Default)] | ||
32 | pub(crate) struct ProcMacroSrv { | ||
33 | expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>, | ||
34 | } | ||
35 | |||
36 | impl ProcMacroSrv { | ||
37 | pub fn expand(&mut self, task: &ExpansionTask) -> Result<ExpansionResult, String> { | ||
38 | let expander = self.expander(&task.lib)?; | ||
39 | match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { | ||
40 | Ok(expansion) => Ok(ExpansionResult { expansion }), | ||
41 | Err(msg) => { | ||
42 | Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult, String> { | ||
48 | let expander = self.expander(&task.lib)?; | ||
49 | Ok(ListMacrosResult { macros: expander.list_macros() }) | ||
50 | } | ||
51 | |||
52 | fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { | ||
53 | let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| { | ||
54 | format!("Failed to get file metadata for {}: {:?}", path.display(), err) | ||
55 | })?; | ||
56 | |||
57 | Ok(match self.expanders.entry((path.to_path_buf(), time)) { | ||
58 | Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| { | ||
59 | format!("Cannot create expander for {}: {:?}", path.display(), err) | ||
60 | })?), | ||
61 | Entry::Occupied(e) => e.into_mut(), | ||
62 | }) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub mod cli; | ||
67 | |||
68 | #[cfg(test)] | ||
69 | mod tests; | ||