Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ra_proc_macro_srv #3800

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/ra_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ pub mod msg;

use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
use ra_tt::{SmolStr, Subtree};
use rpc::ProcMacroKind;
use std::{
path::{Path, PathBuf},
sync::Arc,
};

pub use rpc::{ExpansionResult, ExpansionTask};
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};

#[derive(Debug, Clone)]
pub struct ProcMacroProcessExpander {
Expand Down
24 changes: 24 additions & 0 deletions crates/ra_proc_macro_srv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
edition = "2018"
name = "ra_proc_macro_srv"
version = "0.1.0"
authors = ["rust-analyzer developers"]
publish = false

[lib]
doctest = false

[dependencies]
ra_tt = { path = "../ra_tt" }
ra_mbe = { path = "../ra_mbe" }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matklad We need mbe here for parsing string to ra_tt::Token which is used for lib_proc_macro bridge api.

ra_proc_macro = { path = "../ra_proc_macro" }

serde_derive = "1.0.104"
serde = "1.0.104"
serde_json = "1.0.48"
libloading = "0.5.2"
goblin = "0.2.0"

[dev-dependencies]
cargo_metadata = "0.9.1"
difference = "2.0.0"
21 changes: 21 additions & 0 deletions crates/ra_proc_macro_srv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! 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.
//!
//! But we change some several design for fitting RA needs:
//!
//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with
//! RA then proc-macro2 token stream.
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)

use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};

pub fn expand_task(_task: &ExpansionTask) -> Result<ExpansionResult, String> {
unimplemented!()
}

pub fn list_macros(_task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
unimplemented!()
}
55 changes: 55 additions & 0 deletions crates/ra_proc_macro_srv/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Driver for proc macro server

use ra_proc_macro::msg::{self, Message};
use ra_proc_macro_srv::{expand_task, list_macros};

use std::io;

fn read_request() -> Result<Option<msg::Request>, io::Error> {
let stdin = io::stdin();
let mut stdin = stdin.lock();
msg::Request::read(&mut stdin)
}

fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
let msg: msg::Response = match res {
Ok(res) => res,
Err(err) => msg::Response::Error(msg::ResponseError {
code: msg::ErrorCode::ExpansionError,
message: err,
}),
};

let stdout = io::stdout();
let mut stdout = stdout.lock();
msg.write(&mut stdout)
}
fn main() {
loop {
let req = match read_request() {
Err(err) => {
eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string());
continue;
}
Ok(None) => continue,
Ok(Some(req)) => req,
};

match req {
msg::Request::ListMacro(task) => {
if let Err(err) =
write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it)))
{
eprintln!("Write message error on list macro: {}", err);
}
}
msg::Request::ExpansionMacro(task) => {
if let Err(err) =
write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it)))
{
eprintln!("Write message error on expansion macro: {}", err);
}
}
}
}
}