From d0b6ed4441469acfb6bc6555d78abf12637b6cf4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 18 Mar 2020 20:56:46 +0800 Subject: Add ProcMacroClient --- crates/ra_proc_macro/src/lib.rs | 81 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) (limited to 'crates/ra_proc_macro') 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 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); +//! Client-side Proc-Macro crate +//! +//! We separate proc-macro expanding logic to an extern program to allow +//! different implementations (e.g. wasm or dylib loading). And this crate +//! is used for provide basic infra-structure for commnicate between two +//! process: Client (RA itself), Server (the external program) + +use ra_mbe::ExpandError; +use ra_tt::Subtree; +use std::{ + path::{Path, PathBuf}, + sync::Arc, +}; + +trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe { + fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result; +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProcMacroProcessExpander { + process_path: PathBuf, +} + +impl ProcMacroExpander for ProcMacroProcessExpander { + fn custom_derive( + &self, + _subtree: &Subtree, + _derive_name: &str, + ) -> Result { + // FIXME: do nothing for now + Ok(Subtree::default()) + } +} + +#[derive(Debug, Clone)] +pub struct ProcMacro { + expander: Arc>, + name: String, +} + +impl Eq for ProcMacro {} +impl PartialEq for ProcMacro { + fn eq(&self, other: &ProcMacro) -> bool { + self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander) + } +} + +impl ProcMacro { + pub fn name(&self) -> String { + self.name.clone() + } + + pub fn custom_derive(&self, subtree: &Subtree) -> Result { + self.expander.custom_derive(subtree, &self.name) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ProcMacroClient { + Process { expander: Arc }, + Dummy, +} + +impl ProcMacroClient { + pub fn extern_process(process_path: &Path) -> ProcMacroClient { + let expander = ProcMacroProcessExpander { process_path: process_path.into() }; + ProcMacroClient::Process { expander: Arc::new(expander) } + } + + pub fn dummy() -> ProcMacroClient { + ProcMacroClient::Dummy + } + + pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec { + // FIXME: return empty for now + vec![] } } -- cgit v1.2.3