aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro_srv/src/lib.rs
diff options
context:
space:
mode:
authorPavan Kumar Sunkara <[email protected]>2020-08-13 01:57:26 +0100
committerPavan Kumar Sunkara <[email protected]>2020-08-13 02:18:19 +0100
commit349e6c62ada1fa45a8b80edb877b5e7c9d0c306d (patch)
treec08cb7cd48089c1c7316ff2570de02df76e9d9ef /crates/ra_proc_macro_srv/src/lib.rs
parentf277ec27ac024992e8b8834c68e16b31c983be1b (diff)
Rename ra_proc_macro_srv -> proc_macro_srv
Diffstat (limited to 'crates/ra_proc_macro_srv/src/lib.rs')
-rw-r--r--crates/ra_proc_macro_srv/src/lib.rs69
1 files changed, 0 insertions, 69 deletions
diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs
deleted file mode 100644
index 1fc2eef82..000000000
--- a/crates/ra_proc_macro_srv/src/lib.rs
+++ /dev/null
@@ -1,69 +0,0 @@
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 gerenal ABI compatibility is still an issue)
12
13#[allow(dead_code)]
14#[doc(hidden)]
15mod proc_macro;
16
17#[doc(hidden)]
18mod rustc_server;
19
20mod dylib;
21
22use proc_macro::bridge::client::TokenStream;
23use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
24use std::{
25 collections::{hash_map::Entry, HashMap},
26 fs,
27 path::{Path, PathBuf},
28 time::SystemTime,
29};
30
31#[derive(Default)]
32pub(crate) struct ProcMacroSrv {
33 expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>,
34}
35
36impl 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
66pub mod cli;
67
68#[cfg(test)]
69mod tests;