2020-04-01 00:11:26 -05:00
|
|
|
//! RA Proc Macro Server
|
|
|
|
//!
|
|
|
|
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
|
|
|
|
//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander.
|
|
|
|
//!
|
2020-04-20 13:26:10 -05:00
|
|
|
//! But we adapt it to better fit RA needs:
|
2020-04-01 00:11:26 -05:00
|
|
|
//!
|
2020-08-12 09:46:20 -05:00
|
|
|
//! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with
|
2020-04-20 13:26:10 -05:00
|
|
|
//! RA than `proc-macro2` token stream.
|
2020-04-01 00:11:26 -05:00
|
|
|
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
|
2020-08-13 05:07:28 -05:00
|
|
|
//! rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)…
|
2020-11-02 06:13:32 -06:00
|
|
|
#![allow(unreachable_pub)]
|
2020-04-01 00:11:26 -05:00
|
|
|
|
2020-04-04 03:04:01 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[doc(hidden)]
|
|
|
|
mod proc_macro;
|
|
|
|
|
2020-04-04 03:07:22 -05:00
|
|
|
#[doc(hidden)]
|
|
|
|
mod rustc_server;
|
|
|
|
|
2020-04-04 03:09:36 -05:00
|
|
|
mod dylib;
|
|
|
|
|
2020-04-04 03:28:32 -05:00
|
|
|
use proc_macro::bridge::client::TokenStream;
|
2020-08-13 05:07:28 -05:00
|
|
|
use proc_macro_api::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
|
2020-04-23 21:23:01 -05:00
|
|
|
use std::{
|
|
|
|
collections::{hash_map::Entry, HashMap},
|
2020-04-26 04:58:56 -05:00
|
|
|
fs,
|
2020-04-23 21:23:01 -05:00
|
|
|
path::{Path, PathBuf},
|
2020-04-24 23:30:28 -05:00
|
|
|
time::SystemTime,
|
2020-04-23 21:23:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct ProcMacroSrv {
|
2020-04-24 23:30:28 -05:00
|
|
|
expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>,
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-04 03:10:45 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
impl ProcMacroSrv {
|
|
|
|
pub fn expand(&mut self, task: &ExpansionTask) -> Result<ExpansionResult, String> {
|
|
|
|
let expander = self.expander(&task.lib)?;
|
|
|
|
match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
|
|
|
|
Ok(expansion) => Ok(ExpansionResult { expansion }),
|
|
|
|
Err(msg) => {
|
2020-12-08 12:43:58 -06:00
|
|
|
let msg = msg.as_str().unwrap_or("<unknown error>");
|
|
|
|
Err(format!("proc-macro panicked: {}", msg))
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-04 03:10:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
|
|
|
|
let expander = self.expander(&task.lib)?;
|
|
|
|
Ok(ListMacrosResult { macros: expander.list_macros() })
|
|
|
|
}
|
2020-04-20 13:26:10 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
|
2020-04-26 04:58:56 -05:00
|
|
|
let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| {
|
|
|
|
format!("Failed to get file metadata for {}: {:?}", path.display(), err)
|
|
|
|
})?;
|
2020-04-24 23:30:28 -05:00
|
|
|
|
|
|
|
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
|
2020-04-23 21:23:01 -05:00
|
|
|
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {
|
|
|
|
format!("Cannot create expander for {}: {:?}", path.display(), err)
|
|
|
|
})?),
|
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
|
|
|
})
|
|
|
|
}
|
2020-04-01 00:11:26 -05:00
|
|
|
}
|
2020-04-09 12:43:47 -05:00
|
|
|
|
2020-04-16 08:13:57 -05:00
|
|
|
pub mod cli;
|
|
|
|
|
2020-04-09 12:43:47 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|