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

107 lines
3.3 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
2020-03-26 20:26:34 +00:00
mod rpc;
mod process;
pub mod msg;
use process::ProcMacroProcessSrv;
2020-03-26 16:41:44 +00:00
use ra_tt::{SmolStr, Subtree};
2020-03-26 20:26:34 +00:00
use rpc::ProcMacroKind;
2020-03-18 12:56:46 +00:00
use std::{
path::{Path, PathBuf},
sync::Arc,
};
2020-03-26 20:26:34 +00:00
pub use rpc::{ExpansionResult, ExpansionTask};
#[derive(Debug, Clone)]
2020-03-18 12:56:46 +00:00
pub struct ProcMacroProcessExpander {
2020-03-26 02:49:23 +00:00
process: Arc<ProcMacroProcessSrv>,
2020-03-26 20:26:34 +00:00
dylib_path: PathBuf,
2020-03-26 16:41:44 +00:00
name: SmolStr,
2020-03-18 12:56:46 +00:00
}
2020-03-26 20:26:34 +00:00
impl Eq for ProcMacroProcessExpander {}
impl PartialEq for ProcMacroProcessExpander {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.dylib_path == other.dylib_path
&& Arc::ptr_eq(&self.process, &other.process)
}
}
2020-03-26 16:41:44 +00:00
impl ra_tt::TokenExpander for ProcMacroProcessExpander {
fn expand(
2020-03-18 12:56:46 +00:00
&self,
2020-03-26 20:26:34 +00:00
subtree: &Subtree,
2020-03-26 16:41:44 +00:00
_attr: Option<&Subtree>,
) -> Result<Subtree, ra_tt::ExpansionError> {
2020-03-26 20:26:34 +00:00
self.process.custom_derive(&self.dylib_path, subtree, &self.name)
2020-03-18 12:56:46 +00:00
}
}
2020-03-26 20:26:34 +00:00
#[derive(Debug, Clone)]
enum ProcMacroClientKind {
2020-03-26 02:49:23 +00:00
Process { process: Arc<ProcMacroProcessSrv> },
2020-03-18 12:56:46 +00:00
Dummy,
}
2020-03-26 20:26:34 +00:00
#[derive(Debug, Clone)]
pub struct ProcMacroClient {
kind: ProcMacroClientKind,
}
2020-03-18 12:56:46 +00:00
impl ProcMacroClient {
2020-03-26 20:26:34 +00:00
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
let process = ProcMacroProcessSrv::run(process_path)?;
Ok(ProcMacroClient { kind: ProcMacroClientKind::Process { process: Arc::new(process) } })
2020-03-18 12:56:46 +00:00
}
pub fn dummy() -> ProcMacroClient {
2020-03-26 20:26:34 +00:00
ProcMacroClient { kind: ProcMacroClientKind::Dummy }
2020-03-18 12:56:46 +00:00
}
2020-03-26 16:41:44 +00:00
pub fn by_dylib_path(
&self,
2020-03-26 20:26:34 +00:00
dylib_path: &Path,
2020-03-26 16:41:44 +00:00
) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
2020-03-26 20:26:34 +00:00
match &self.kind {
ProcMacroClientKind::Dummy => vec![],
ProcMacroClientKind::Process { process } => {
let macros = match process.find_proc_macros(dylib_path) {
Err(err) => {
eprintln!("Fail to find proc macro. Error: {:#?}", err);
return vec![];
}
Ok(macros) => macros,
};
macros
.into_iter()
.filter_map(|(name, kind)| {
// FIXME: Support custom derive only for now.
match kind {
ProcMacroKind::CustomDerive => {
let name = SmolStr::new(&name);
let expander: Arc<dyn ra_tt::TokenExpander> =
Arc::new(ProcMacroProcessExpander {
process: process.clone(),
name: name.clone(),
dylib_path: dylib_path.into(),
});
Some((name, expander))
}
_ => None,
}
})
.collect()
}
}
2020-03-18 09:47:59 +00:00
}
}