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

116 lines
3.5 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;
2020-03-27 04:55:58 +00:00
use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
2020-03-26 16:41:44 +00:00
use ra_tt::{SmolStr, Subtree};
2020-03-18 12:56:46 +00:00
use std::{
2020-04-16 20:08:01 +00:00
ffi::OsStr,
2020-03-18 12:56:46 +00:00
path::{Path, PathBuf},
sync::Arc,
};
2020-04-01 05:11:26 +00:00
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
2020-03-26 20:26:34 +00:00
#[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-27 04:55:58 +00:00
#[derive(Debug)]
2020-03-26 20:26:34 +00:00
enum ProcMacroClientKind {
2020-03-27 04:55:58 +00:00
Process { process: Arc<ProcMacroProcessSrv>, thread: ProcMacroProcessThread },
2020-03-18 12:56:46 +00:00
Dummy,
}
2020-03-27 04:55:58 +00:00
#[derive(Debug)]
2020-03-26 20:26:34 +00:00
pub struct ProcMacroClient {
kind: ProcMacroClientKind,
}
2020-03-18 12:56:46 +00:00
impl ProcMacroClient {
2020-04-16 20:08:01 +00:00
pub fn extern_process<I, S>(
process_path: &Path,
2020-04-16 20:08:01 +00:00
args: I,
) -> Result<ProcMacroClient, std::io::Error>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
2020-03-27 04:55:58 +00:00
Ok(ProcMacroClient {
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
})
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![],
2020-03-27 04:55:58 +00:00
ProcMacroClientKind::Process { process, .. } => {
2020-03-26 20:26:34 +00:00
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
}
}