rust-analyzer/crates/ra_proc_macro/src/lib.rs

84 lines
2.2 KiB
Rust
Raw Normal View History

2020-03-18 12:56:46 +00:00
//! 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
2020-03-26 02:49:23 +00:00
//! is used to provide basic infrastructure for communication between two
//! processes: Client (RA itself), Server (the external program)
2020-03-18 12:56:46 +00:00
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<Subtree, ExpandError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcMacroProcessExpander {
2020-03-26 02:49:23 +00:00
process: Arc<ProcMacroProcessSrv>,
2020-03-18 12:56:46 +00:00
}
impl ProcMacroExpander for ProcMacroProcessExpander {
fn custom_derive(
&self,
_subtree: &Subtree,
_derive_name: &str,
) -> Result<Subtree, ExpandError> {
// FIXME: do nothing for now
Ok(Subtree::default())
}
}
#[derive(Debug, Clone)]
pub struct ProcMacro {
2020-03-26 02:49:23 +00:00
expander: Arc<dyn ProcMacroExpander>,
2020-03-18 12:56:46 +00:00
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<Subtree, ExpandError> {
self.expander.custom_derive(subtree, &self.name)
}
}
2020-03-26 02:49:23 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcMacroProcessSrv {
path: PathBuf,
}
2020-03-18 12:56:46 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProcMacroClient {
2020-03-26 02:49:23 +00:00
Process { process: Arc<ProcMacroProcessSrv> },
2020-03-18 12:56:46 +00:00
Dummy,
}
impl ProcMacroClient {
pub fn extern_process(process_path: &Path) -> ProcMacroClient {
2020-03-26 02:49:23 +00:00
let process = ProcMacroProcessSrv { path: process_path.into() };
ProcMacroClient::Process { process: Arc::new(process) }
2020-03-18 12:56:46 +00:00
}
pub fn dummy() -> ProcMacroClient {
ProcMacroClient::Dummy
}
pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> {
// FIXME: return empty for now
vec![]
2020-03-18 09:47:59 +00:00
}
}