aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro/src')
-rw-r--r--crates/ra_proc_macro/src/lib.rs17
-rw-r--r--crates/ra_proc_macro/src/msg.rs3
-rw-r--r--crates/ra_proc_macro/src/process.rs38
-rw-r--r--crates/ra_proc_macro/src/rpc.rs13
4 files changed, 35 insertions, 36 deletions
diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs
index 004943b9e..15db57eb2 100644
--- a/crates/ra_proc_macro/src/lib.rs
+++ b/crates/ra_proc_macro/src/lib.rs
@@ -9,8 +9,6 @@ mod rpc;
9mod process; 9mod process;
10pub mod msg; 10pub mod msg;
11 11
12use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
13use ra_tt::{SmolStr, Subtree};
14use std::{ 12use std::{
15 ffi::OsStr, 13 ffi::OsStr,
16 io, 14 io,
@@ -18,6 +16,10 @@ use std::{
18 sync::Arc, 16 sync::Arc,
19}; 17};
20 18
19use tt::{SmolStr, Subtree};
20
21use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
22
21pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind}; 23pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
22 24
23#[derive(Debug, Clone)] 25#[derive(Debug, Clone)]
@@ -36,12 +38,12 @@ impl PartialEq for ProcMacroProcessExpander {
36 } 38 }
37} 39}
38 40
39impl ra_tt::TokenExpander for ProcMacroProcessExpander { 41impl tt::TokenExpander for ProcMacroProcessExpander {
40 fn expand( 42 fn expand(
41 &self, 43 &self,
42 subtree: &Subtree, 44 subtree: &Subtree,
43 _attr: Option<&Subtree>, 45 _attr: Option<&Subtree>,
44 ) -> Result<Subtree, ra_tt::ExpansionError> { 46 ) -> Result<Subtree, tt::ExpansionError> {
45 self.process.custom_derive(&self.dylib_path, subtree, &self.name) 47 self.process.custom_derive(&self.dylib_path, subtree, &self.name)
46 } 48 }
47} 49}
@@ -72,10 +74,7 @@ impl ProcMacroClient {
72 ProcMacroClient { kind: ProcMacroClientKind::Dummy } 74 ProcMacroClient { kind: ProcMacroClientKind::Dummy }
73 } 75 }
74 76
75 pub fn by_dylib_path( 77 pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<(SmolStr, Arc<dyn tt::TokenExpander>)> {
76 &self,
77 dylib_path: &Path,
78 ) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
79 match &self.kind { 78 match &self.kind {
80 ProcMacroClientKind::Dummy => vec![], 79 ProcMacroClientKind::Dummy => vec![],
81 ProcMacroClientKind::Process { process, .. } => { 80 ProcMacroClientKind::Process { process, .. } => {
@@ -94,7 +93,7 @@ impl ProcMacroClient {
94 match kind { 93 match kind {
95 ProcMacroKind::CustomDerive => { 94 ProcMacroKind::CustomDerive => {
96 let name = SmolStr::new(&name); 95 let name = SmolStr::new(&name);
97 let expander: Arc<dyn ra_tt::TokenExpander> = 96 let expander: Arc<dyn tt::TokenExpander> =
98 Arc::new(ProcMacroProcessExpander { 97 Arc::new(ProcMacroProcessExpander {
99 process: process.clone(), 98 process: process.clone(),
100 name: name.clone(), 99 name: name.clone(),
diff --git a/crates/ra_proc_macro/src/msg.rs b/crates/ra_proc_macro/src/msg.rs
index 95d9b8804..f84ebdbc5 100644
--- a/crates/ra_proc_macro/src/msg.rs
+++ b/crates/ra_proc_macro/src/msg.rs
@@ -5,11 +5,12 @@ use std::{
5 io::{self, BufRead, Write}, 5 io::{self, BufRead, Write},
6}; 6};
7 7
8use serde::{de::DeserializeOwned, Deserialize, Serialize};
9
8use crate::{ 10use crate::{
9 rpc::{ListMacrosResult, ListMacrosTask}, 11 rpc::{ListMacrosResult, ListMacrosTask},
10 ExpansionResult, ExpansionTask, 12 ExpansionResult, ExpansionTask,
11}; 13};
12use serde::{de::DeserializeOwned, Deserialize, Serialize};
13 14
14#[derive(Debug, Serialize, Deserialize, Clone)] 15#[derive(Debug, Serialize, Deserialize, Clone)]
15pub enum Request { 16pub enum Request {
diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs
index 37dd3f496..51ffcaa78 100644
--- a/crates/ra_proc_macro/src/process.rs
+++ b/crates/ra_proc_macro/src/process.rs
@@ -1,21 +1,22 @@
1//! Handle process life-time and message passing for proc-macro client 1//! Handle process life-time and message passing for proc-macro client
2 2
3use crossbeam_channel::{bounded, Receiver, Sender};
4use ra_tt::Subtree;
5
6use crate::msg::{ErrorCode, Message, Request, Response, ResponseError};
7use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
8
9use io::{BufRead, BufReader};
10use std::{ 3use std::{
11 convert::{TryFrom, TryInto}, 4 convert::{TryFrom, TryInto},
12 ffi::{OsStr, OsString}, 5 ffi::{OsStr, OsString},
13 io::{self, Write}, 6 io::{self, BufRead, BufReader, Write},
14 path::{Path, PathBuf}, 7 path::{Path, PathBuf},
15 process::{Child, Command, Stdio}, 8 process::{Child, Command, Stdio},
16 sync::{Arc, Weak}, 9 sync::{Arc, Weak},
17}; 10};
18 11
12use crossbeam_channel::{bounded, Receiver, Sender};
13use tt::Subtree;
14
15use crate::{
16 msg::{ErrorCode, Message, Request, Response, ResponseError},
17 rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind},
18};
19
19#[derive(Debug, Default)] 20#[derive(Debug, Default)]
20pub(crate) struct ProcMacroProcessSrv { 21pub(crate) struct ProcMacroProcessSrv {
21 inner: Option<Weak<Sender<Task>>>, 22 inner: Option<Weak<Sender<Task>>>,
@@ -50,7 +51,7 @@ impl ProcMacroProcessSrv {
50 pub fn find_proc_macros( 51 pub fn find_proc_macros(
51 &self, 52 &self,
52 dylib_path: &Path, 53 dylib_path: &Path,
53 ) -> Result<Vec<(String, ProcMacroKind)>, ra_tt::ExpansionError> { 54 ) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
54 let task = ListMacrosTask { lib: dylib_path.to_path_buf() }; 55 let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
55 56
56 let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?; 57 let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?;
@@ -62,7 +63,7 @@ impl ProcMacroProcessSrv {
62 dylib_path: &Path, 63 dylib_path: &Path,
63 subtree: &Subtree, 64 subtree: &Subtree,
64 derive_name: &str, 65 derive_name: &str,
65 ) -> Result<Subtree, ra_tt::ExpansionError> { 66 ) -> Result<Subtree, tt::ExpansionError> {
66 let task = ExpansionTask { 67 let task = ExpansionTask {
67 macro_body: subtree.clone(), 68 macro_body: subtree.clone(),
68 macro_name: derive_name.to_string(), 69 macro_name: derive_name.to_string(),
@@ -74,38 +75,35 @@ impl ProcMacroProcessSrv {
74 Ok(result.expansion) 75 Ok(result.expansion)
75 } 76 }
76 77
77 pub fn send_task<R>(&self, req: Request) -> Result<R, ra_tt::ExpansionError> 78 pub fn send_task<R>(&self, req: Request) -> Result<R, tt::ExpansionError>
78 where 79 where
79 R: TryFrom<Response, Error = &'static str>, 80 R: TryFrom<Response, Error = &'static str>,
80 { 81 {
81 let sender = match &self.inner { 82 let sender = match &self.inner {
82 None => return Err(ra_tt::ExpansionError::Unknown("No sender is found.".to_string())), 83 None => return Err(tt::ExpansionError::Unknown("No sender is found.".to_string())),
83 Some(it) => it, 84 Some(it) => it,
84 }; 85 };
85 86
86 let (result_tx, result_rx) = bounded(0); 87 let (result_tx, result_rx) = bounded(0);
87 let sender = match sender.upgrade() { 88 let sender = match sender.upgrade() {
88 None => { 89 None => {
89 return Err(ra_tt::ExpansionError::Unknown("Proc macro process is closed.".into())) 90 return Err(tt::ExpansionError::Unknown("Proc macro process is closed.".into()))
90 } 91 }
91 Some(it) => it, 92 Some(it) => it,
92 }; 93 };
93 sender.send(Task { req, result_tx }).unwrap(); 94 sender.send(Task { req, result_tx }).unwrap();
94 let res = result_rx 95 let res = result_rx
95 .recv() 96 .recv()
96 .map_err(|_| ra_tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?; 97 .map_err(|_| tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?;
97 98
98 match res { 99 match res {
99 Some(Response::Error(err)) => { 100 Some(Response::Error(err)) => {
100 return Err(ra_tt::ExpansionError::ExpansionError(err.message)); 101 return Err(tt::ExpansionError::ExpansionError(err.message));
101 } 102 }
102 Some(res) => Ok(res.try_into().map_err(|err| { 103 Some(res) => Ok(res.try_into().map_err(|err| {
103 ra_tt::ExpansionError::Unknown(format!( 104 tt::ExpansionError::Unknown(format!("Fail to get response, reason : {:#?} ", err))
104 "Fail to get response, reason : {:#?} ",
105 err
106 ))
107 })?), 105 })?),
108 None => Err(ra_tt::ExpansionError::Unknown("Empty result".into())), 106 None => Err(tt::ExpansionError::Unknown("Empty result".into())),
109 } 107 }
110 } 108 }
111} 109}
diff --git a/crates/ra_proc_macro/src/rpc.rs b/crates/ra_proc_macro/src/rpc.rs
index 4ce485926..5e5d78d06 100644
--- a/crates/ra_proc_macro/src/rpc.rs
+++ b/crates/ra_proc_macro/src/rpc.rs
@@ -1,17 +1,18 @@
1//! Data struture serialization related stuff for RPC 1//! Data struture serialization related stuff for RPC
2//! 2//!
3//! Defines all necessary rpc serialization data structures, 3//! Defines all necessary rpc serialization data structures,
4//! which includes `ra_tt` related data and some task messages. 4//! which includes `tt` related data and some task messages.
5//! Although adding `Serialize` and `Deserialize` traits to `ra_tt` directly seems 5//! Although adding `Serialize` and `Deserialize` traits to `tt` directly seems
6//! to be much easier, we deliberately duplicate `ra_tt` structs with `#[serde(with = "XXDef")]` 6//! to be much easier, we deliberately duplicate `tt` structs with `#[serde(with = "XXDef")]`
7//! for separation of code responsibility. 7//! for separation of code responsibility.
8 8
9use ra_tt::{ 9use std::path::PathBuf;
10
11use serde::{Deserialize, Serialize};
12use tt::{
10 Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, SmolStr, Spacing, Subtree, TokenId, 13 Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, SmolStr, Spacing, Subtree, TokenId,
11 TokenTree, 14 TokenTree,
12}; 15};
13use serde::{Deserialize, Serialize};
14use std::path::PathBuf;
15 16
16#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)] 17#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
17pub struct ListMacrosTask { 18pub struct ListMacrosTask {