diff options
author | Edwin Cheng <[email protected]> | 2020-03-18 12:56:46 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-03-25 19:29:45 +0000 |
commit | d0b6ed4441469acfb6bc6555d78abf12637b6cf4 (patch) | |
tree | a4b39b33590c472a68fd76ceae96732e9cb54ebb /crates/ra_proc_macro | |
parent | a617f24eae6c02f087759312e9aa08507fbecdf0 (diff) |
Add ProcMacroClient
Diffstat (limited to 'crates/ra_proc_macro')
-rw-r--r-- | crates/ra_proc_macro/src/lib.rs | 81 |
1 files changed, 76 insertions, 5 deletions
diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs index 31e1bb209..a7a84249f 100644 --- a/crates/ra_proc_macro/src/lib.rs +++ b/crates/ra_proc_macro/src/lib.rs | |||
@@ -1,7 +1,78 @@ | |||
1 | #[cfg(test)] | 1 | //! Client-side Proc-Macro crate |
2 | mod tests { | 2 | //! |
3 | #[test] | 3 | //! We separate proc-macro expanding logic to an extern program to allow |
4 | fn it_works() { | 4 | //! different implementations (e.g. wasm or dylib loading). And this crate |
5 | assert_eq!(2 + 2, 4); | 5 | //! is used for provide basic infra-structure for commnicate between two |
6 | //! process: Client (RA itself), Server (the external program) | ||
7 | |||
8 | use ra_mbe::ExpandError; | ||
9 | use ra_tt::Subtree; | ||
10 | use std::{ | ||
11 | path::{Path, PathBuf}, | ||
12 | sync::Arc, | ||
13 | }; | ||
14 | |||
15 | trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe { | ||
16 | fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result<Subtree, ExpandError>; | ||
17 | } | ||
18 | |||
19 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
20 | pub struct ProcMacroProcessExpander { | ||
21 | process_path: PathBuf, | ||
22 | } | ||
23 | |||
24 | impl ProcMacroExpander for ProcMacroProcessExpander { | ||
25 | fn custom_derive( | ||
26 | &self, | ||
27 | _subtree: &Subtree, | ||
28 | _derive_name: &str, | ||
29 | ) -> Result<Subtree, ExpandError> { | ||
30 | // FIXME: do nothing for now | ||
31 | Ok(Subtree::default()) | ||
32 | } | ||
33 | } | ||
34 | |||
35 | #[derive(Debug, Clone)] | ||
36 | pub struct ProcMacro { | ||
37 | expander: Arc<Box<dyn ProcMacroExpander>>, | ||
38 | name: String, | ||
39 | } | ||
40 | |||
41 | impl Eq for ProcMacro {} | ||
42 | impl PartialEq for ProcMacro { | ||
43 | fn eq(&self, other: &ProcMacro) -> bool { | ||
44 | self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl ProcMacro { | ||
49 | pub fn name(&self) -> String { | ||
50 | self.name.clone() | ||
51 | } | ||
52 | |||
53 | pub fn custom_derive(&self, subtree: &Subtree) -> Result<Subtree, ExpandError> { | ||
54 | self.expander.custom_derive(subtree, &self.name) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
59 | pub enum ProcMacroClient { | ||
60 | Process { expander: Arc<ProcMacroProcessExpander> }, | ||
61 | Dummy, | ||
62 | } | ||
63 | |||
64 | impl ProcMacroClient { | ||
65 | pub fn extern_process(process_path: &Path) -> ProcMacroClient { | ||
66 | let expander = ProcMacroProcessExpander { process_path: process_path.into() }; | ||
67 | ProcMacroClient::Process { expander: Arc::new(expander) } | ||
68 | } | ||
69 | |||
70 | pub fn dummy() -> ProcMacroClient { | ||
71 | ProcMacroClient::Dummy | ||
72 | } | ||
73 | |||
74 | pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> { | ||
75 | // FIXME: return empty for now | ||
76 | vec![] | ||
6 | } | 77 | } |
7 | } | 78 | } |