Rename ra_tt -> tt

This commit is contained in:
Aleksey Kladov 2020-08-12 16:46:20 +02:00
parent 208b7bd7ba
commit 550d7fbe3c
22 changed files with 77 additions and 77 deletions

32
Cargo.lock generated
View file

@ -939,8 +939,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"ra_mbe", "ra_mbe",
"ra_syntax", "ra_syntax",
"ra_tt",
"rustc-hash", "rustc-hash",
"tt",
] ]
[[package]] [[package]]
@ -950,11 +950,11 @@ dependencies = [
"profile", "profile",
"ra_cfg", "ra_cfg",
"ra_syntax", "ra_syntax",
"ra_tt",
"rustc-hash", "rustc-hash",
"salsa", "salsa",
"stdx", "stdx",
"test_utils", "test_utils",
"tt",
"vfs", "vfs",
] ]
@ -1004,11 +1004,11 @@ dependencies = [
"ra_hir_expand", "ra_hir_expand",
"ra_mbe", "ra_mbe",
"ra_syntax", "ra_syntax",
"ra_tt",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
"stdx", "stdx",
"test_utils", "test_utils",
"tt",
] ]
[[package]] [[package]]
@ -1023,9 +1023,9 @@ dependencies = [
"ra_mbe", "ra_mbe",
"ra_parser", "ra_parser",
"ra_syntax", "ra_syntax",
"ra_tt",
"rustc-hash", "rustc-hash",
"test_utils", "test_utils",
"tt",
] ]
[[package]] [[package]]
@ -1107,10 +1107,10 @@ dependencies = [
"log", "log",
"ra_parser", "ra_parser",
"ra_syntax", "ra_syntax",
"ra_tt",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
"test_utils", "test_utils",
"tt",
] ]
[[package]] [[package]]
@ -1127,9 +1127,9 @@ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"jod-thread", "jod-thread",
"log", "log",
"ra_tt",
"serde", "serde",
"serde_json", "serde_json",
"tt",
] ]
[[package]] [[package]]
@ -1144,9 +1144,9 @@ dependencies = [
"ra_mbe", "ra_mbe",
"ra_proc_macro", "ra_proc_macro",
"ra_toolchain", "ra_toolchain",
"ra_tt",
"serde_derive", "serde_derive",
"test_utils", "test_utils",
"tt",
] ]
[[package]] [[package]]
@ -1217,14 +1217,6 @@ dependencies = [
"home", "home",
] ]
[[package]]
name = "ra_tt"
version = "0.1.0"
dependencies = [
"smol_str",
"stdx",
]
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.3.1" version = "1.3.1"
@ -1329,7 +1321,6 @@ dependencies = [
"ra_syntax", "ra_syntax",
"ra_text_edit", "ra_text_edit",
"ra_toolchain", "ra_toolchain",
"ra_tt",
"rayon", "rayon",
"rustc-hash", "rustc-hash",
"serde", "serde",
@ -1337,6 +1328,7 @@ dependencies = [
"stdx", "stdx",
"test_utils", "test_utils",
"threadpool", "threadpool",
"tt",
"vfs", "vfs",
"vfs-notify", "vfs-notify",
"winapi 0.3.9", "winapi 0.3.9",
@ -1706,6 +1698,14 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
] ]
[[package]]
name = "tt"
version = "0.0.0"
dependencies = [
"smol_str",
"stdx",
]
[[package]] [[package]]
name = "ungrammar" name = "ungrammar"
version = "1.1.1" version = "1.1.1"

View file

@ -12,7 +12,7 @@ doctest = false
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
tt = { path = "../ra_tt", package = "ra_tt" } tt = { path = "../tt" }
[dev-dependencies] [dev-dependencies]
mbe = { path = "../ra_mbe", package = "ra_mbe" } mbe = { path = "../ra_mbe", package = "ra_mbe" }

View file

@ -15,7 +15,7 @@ rustc-hash = "1.1.0"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_cfg = { path = "../ra_cfg" } ra_cfg = { path = "../ra_cfg" }
profile = { path = "../profile" } profile = { path = "../profile" }
ra_tt = { path = "../ra_tt" } tt = { path = "../tt" }
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
vfs = { path = "../vfs" } vfs = { path = "../vfs" }
stdx = { path = "../stdx" } stdx = { path = "../stdx" }

View file

@ -10,8 +10,8 @@ use std::{fmt, iter::FromIterator, ops, str::FromStr, sync::Arc};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_syntax::SmolStr; use ra_syntax::SmolStr;
use ra_tt::TokenExpander;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use tt::TokenExpander;
use vfs::file_set::FileSet; use vfs::file_set::FileSet;
pub use vfs::FileId; pub use vfs::FileId;
@ -156,7 +156,7 @@ impl CrateGraph {
display_name: Option<String>, display_name: Option<String>,
cfg_options: CfgOptions, cfg_options: CfgOptions,
env: Env, env: Env,
proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>, proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>,
) -> CrateId { ) -> CrateId {
let proc_macro = let proc_macro =
proc_macro.into_iter().map(|(name, it)| ProcMacro { name, expander: it }).collect(); proc_macro.into_iter().map(|(name, it)| ProcMacro { name, expander: it }).collect();

View file

@ -30,7 +30,7 @@ hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
mbe = { path = "../ra_mbe", package = "ra_mbe" } mbe = { path = "../ra_mbe", package = "ra_mbe" }
ra_cfg = { path = "../ra_cfg" } ra_cfg = { path = "../ra_cfg" }
tt = { path = "../ra_tt", package = "ra_tt" } tt = { path = "../tt" }
[dev-dependencies] [dev-dependencies]
expect = { path = "../expect" } expect = { path = "../expect" }

View file

@ -18,6 +18,6 @@ ra_db = { path = "../ra_db" }
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_parser = { path = "../ra_parser" } ra_parser = { path = "../ra_parser" }
profile = { path = "../profile" } profile = { path = "../profile" }
tt = { path = "../ra_tt", package = "ra_tt" } tt = { path = "../tt" }
mbe = { path = "../ra_mbe", package = "ra_mbe" } mbe = { path = "../ra_mbe", package = "ra_mbe" }
test_utils = { path = "../test_utils"} test_utils = { path = "../test_utils"}

View file

@ -11,7 +11,7 @@ doctest = false
[dependencies] [dependencies]
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_parser = { path = "../ra_parser" } ra_parser = { path = "../ra_parser" }
tt = { path = "../ra_tt", package = "ra_tt" } tt = { path = "../tt" }
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
smallvec = "1.2.0" smallvec = "1.2.0"
log = "0.4.8" log = "0.4.8"

View file

@ -1,5 +1,5 @@
//! `mbe` (short for Macro By Example) crate contains code for handling //! `mbe` (short for Macro By Example) crate contains code for handling
//! `macro_rules` macros. It uses `TokenTree` (from `ra_tt` package) as the //! `macro_rules` macros. It uses `TokenTree` (from `tt` package) as the
//! interface, although it contains some code to bridge `SyntaxNode`s and //! interface, although it contains some code to bridge `SyntaxNode`s and
//! `TokenTree`s as well! //! `TokenTree`s as well!

View file

@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
doctest = false doctest = false
[dependencies] [dependencies]
ra_tt = { path = "../ra_tt" } tt = { path = "../tt" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
log = "0.4.8" log = "0.4.8"

View file

@ -9,8 +9,6 @@ mod rpc;
mod process; mod process;
pub mod msg; pub mod msg;
use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
use ra_tt::{SmolStr, Subtree};
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
io, io,
@ -18,6 +16,10 @@ use std::{
sync::Arc, sync::Arc,
}; };
use tt::{SmolStr, Subtree};
use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind}; pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -36,12 +38,12 @@ impl PartialEq for ProcMacroProcessExpander {
} }
} }
impl ra_tt::TokenExpander for ProcMacroProcessExpander { impl tt::TokenExpander for ProcMacroProcessExpander {
fn expand( fn expand(
&self, &self,
subtree: &Subtree, subtree: &Subtree,
_attr: Option<&Subtree>, _attr: Option<&Subtree>,
) -> Result<Subtree, ra_tt::ExpansionError> { ) -> Result<Subtree, tt::ExpansionError> {
self.process.custom_derive(&self.dylib_path, subtree, &self.name) self.process.custom_derive(&self.dylib_path, subtree, &self.name)
} }
} }
@ -72,10 +74,7 @@ impl ProcMacroClient {
ProcMacroClient { kind: ProcMacroClientKind::Dummy } ProcMacroClient { kind: ProcMacroClientKind::Dummy }
} }
pub fn by_dylib_path( pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<(SmolStr, Arc<dyn tt::TokenExpander>)> {
&self,
dylib_path: &Path,
) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
match &self.kind { match &self.kind {
ProcMacroClientKind::Dummy => vec![], ProcMacroClientKind::Dummy => vec![],
ProcMacroClientKind::Process { process, .. } => { ProcMacroClientKind::Process { process, .. } => {
@ -94,7 +93,7 @@ impl ProcMacroClient {
match kind { match kind {
ProcMacroKind::CustomDerive => { ProcMacroKind::CustomDerive => {
let name = SmolStr::new(&name); let name = SmolStr::new(&name);
let expander: Arc<dyn ra_tt::TokenExpander> = let expander: Arc<dyn tt::TokenExpander> =
Arc::new(ProcMacroProcessExpander { Arc::new(ProcMacroProcessExpander {
process: process.clone(), process: process.clone(),
name: name.clone(), name: name.clone(),

View file

@ -5,11 +5,12 @@ use std::{
io::{self, BufRead, Write}, io::{self, BufRead, Write},
}; };
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{ use crate::{
rpc::{ListMacrosResult, ListMacrosTask}, rpc::{ListMacrosResult, ListMacrosTask},
ExpansionResult, ExpansionTask, ExpansionResult, ExpansionTask,
}; };
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Request { pub enum Request {

View file

@ -1,21 +1,22 @@
//! Handle process life-time and message passing for proc-macro client //! Handle process life-time and message passing for proc-macro client
use crossbeam_channel::{bounded, Receiver, Sender};
use ra_tt::Subtree;
use crate::msg::{ErrorCode, Message, Request, Response, ResponseError};
use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
use io::{BufRead, BufReader};
use std::{ use std::{
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
ffi::{OsStr, OsString}, ffi::{OsStr, OsString},
io::{self, Write}, io::{self, BufRead, BufReader, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Child, Command, Stdio}, process::{Child, Command, Stdio},
sync::{Arc, Weak}, sync::{Arc, Weak},
}; };
use crossbeam_channel::{bounded, Receiver, Sender};
use tt::Subtree;
use crate::{
msg::{ErrorCode, Message, Request, Response, ResponseError},
rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind},
};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(crate) struct ProcMacroProcessSrv { pub(crate) struct ProcMacroProcessSrv {
inner: Option<Weak<Sender<Task>>>, inner: Option<Weak<Sender<Task>>>,
@ -50,7 +51,7 @@ impl ProcMacroProcessSrv {
pub fn find_proc_macros( pub fn find_proc_macros(
&self, &self,
dylib_path: &Path, dylib_path: &Path,
) -> Result<Vec<(String, ProcMacroKind)>, ra_tt::ExpansionError> { ) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
let task = ListMacrosTask { lib: dylib_path.to_path_buf() }; let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?; let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?;
@ -62,7 +63,7 @@ impl ProcMacroProcessSrv {
dylib_path: &Path, dylib_path: &Path,
subtree: &Subtree, subtree: &Subtree,
derive_name: &str, derive_name: &str,
) -> Result<Subtree, ra_tt::ExpansionError> { ) -> Result<Subtree, tt::ExpansionError> {
let task = ExpansionTask { let task = ExpansionTask {
macro_body: subtree.clone(), macro_body: subtree.clone(),
macro_name: derive_name.to_string(), macro_name: derive_name.to_string(),
@ -74,38 +75,35 @@ impl ProcMacroProcessSrv {
Ok(result.expansion) Ok(result.expansion)
} }
pub fn send_task<R>(&self, req: Request) -> Result<R, ra_tt::ExpansionError> pub fn send_task<R>(&self, req: Request) -> Result<R, tt::ExpansionError>
where where
R: TryFrom<Response, Error = &'static str>, R: TryFrom<Response, Error = &'static str>,
{ {
let sender = match &self.inner { let sender = match &self.inner {
None => return Err(ra_tt::ExpansionError::Unknown("No sender is found.".to_string())), None => return Err(tt::ExpansionError::Unknown("No sender is found.".to_string())),
Some(it) => it, Some(it) => it,
}; };
let (result_tx, result_rx) = bounded(0); let (result_tx, result_rx) = bounded(0);
let sender = match sender.upgrade() { let sender = match sender.upgrade() {
None => { None => {
return Err(ra_tt::ExpansionError::Unknown("Proc macro process is closed.".into())) return Err(tt::ExpansionError::Unknown("Proc macro process is closed.".into()))
} }
Some(it) => it, Some(it) => it,
}; };
sender.send(Task { req, result_tx }).unwrap(); sender.send(Task { req, result_tx }).unwrap();
let res = result_rx let res = result_rx
.recv() .recv()
.map_err(|_| ra_tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?; .map_err(|_| tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?;
match res { match res {
Some(Response::Error(err)) => { Some(Response::Error(err)) => {
return Err(ra_tt::ExpansionError::ExpansionError(err.message)); return Err(tt::ExpansionError::ExpansionError(err.message));
} }
Some(res) => Ok(res.try_into().map_err(|err| { Some(res) => Ok(res.try_into().map_err(|err| {
ra_tt::ExpansionError::Unknown(format!( tt::ExpansionError::Unknown(format!("Fail to get response, reason : {:#?} ", err))
"Fail to get response, reason : {:#?} ",
err
))
})?), })?),
None => Err(ra_tt::ExpansionError::Unknown("Empty result".into())), None => Err(tt::ExpansionError::Unknown("Empty result".into())),
} }
} }
} }

View file

@ -1,17 +1,18 @@
//! Data struture serialization related stuff for RPC //! Data struture serialization related stuff for RPC
//! //!
//! Defines all necessary rpc serialization data structures, //! Defines all necessary rpc serialization data structures,
//! which includes `ra_tt` related data and some task messages. //! which includes `tt` related data and some task messages.
//! Although adding `Serialize` and `Deserialize` traits to `ra_tt` directly seems //! Although adding `Serialize` and `Deserialize` traits to `tt` directly seems
//! to be much easier, we deliberately duplicate `ra_tt` structs with `#[serde(with = "XXDef")]` //! to be much easier, we deliberately duplicate `tt` structs with `#[serde(with = "XXDef")]`
//! for separation of code responsibility. //! for separation of code responsibility.
use ra_tt::{ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use tt::{
Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, SmolStr, Spacing, Subtree, TokenId, Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, SmolStr, Spacing, Subtree, TokenId,
TokenTree, TokenTree,
}; };
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)] #[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct ListMacrosTask { pub struct ListMacrosTask {

View file

@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
doctest = false doctest = false
[dependencies] [dependencies]
ra_tt = { path = "../ra_tt" } tt = { path = "../tt" }
ra_mbe = { path = "../ra_mbe" } ra_mbe = { path = "../ra_mbe" }
ra_proc_macro = { path = "../ra_proc_macro" } ra_proc_macro = { path = "../ra_proc_macro" }
goblin = "0.2.1" goblin = "0.2.1"

View file

@ -128,9 +128,9 @@ impl Expander {
pub fn expand( pub fn expand(
&self, &self,
macro_name: &str, macro_name: &str,
macro_body: &ra_tt::Subtree, macro_body: &tt::Subtree,
attributes: Option<&ra_tt::Subtree>, attributes: Option<&tt::Subtree>,
) -> Result<ra_tt::Subtree, bridge::PanicMessage> { ) -> Result<tt::Subtree, bridge::PanicMessage> {
let parsed_body = TokenStream::with_subtree(macro_body.clone()); let parsed_body = TokenStream::with_subtree(macro_body.clone());
let parsed_attributes = attributes let parsed_attributes = attributes

View file

@ -5,7 +5,7 @@
//! //!
//! But we adapt it to better fit RA needs: //! But we adapt it to better fit RA needs:
//! //!
//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with //! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with
//! RA than `proc-macro2` token stream. //! RA than `proc-macro2` token stream.
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` //! * 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) //! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)

View file

@ -1,15 +1,14 @@
//! Rustc proc-macro server implementation with ra_tt //! Rustc proc-macro server implementation with tt
//! //!
//! Based on idea from https://github.com/fedochet/rust-proc-macro-expander //! Based on idea from https://github.com/fedochet/rust-proc-macro-expander
//! The lib-proc-macro server backend is `TokenStream`-agnostic, such that //! The lib-proc-macro server backend is `TokenStream`-agnostic, such that
//! we could provide any TokenStream implementation. //! we could provide any TokenStream implementation.
//! The original idea from fedochet is using proc-macro2 as backend, //! The original idea from fedochet is using proc-macro2 as backend,
//! we use ra_tt instead for better intergation with RA. //! we use tt instead for better intergation with RA.
//! //!
//! FIXME: No span and source file information is implemented yet //! FIXME: No span and source file information is implemented yet
use crate::proc_macro::bridge::{self, server}; use crate::proc_macro::bridge::{self, server};
use ra_tt as tt;
use std::collections::{Bound, HashMap}; use std::collections::{Bound, HashMap};
use std::hash::Hash; use std::hash::Hash;
@ -153,9 +152,10 @@ pub struct TokenStreamBuilder {
/// Public implementation details for the `TokenStream` type, such as iterators. /// Public implementation details for the `TokenStream` type, such as iterators.
pub mod token_stream { pub mod token_stream {
use super::{tt, TokenStream, TokenTree};
use std::str::FromStr; use std::str::FromStr;
use super::{TokenStream, TokenTree};
/// An iterator over `TokenStream`'s `TokenTree`s. /// An iterator over `TokenStream`'s `TokenTree`s.
/// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
/// and returns whole groups as token trees. /// and returns whole groups as token trees.

View file

@ -61,4 +61,4 @@ winapi = "0.3.8"
expect = { path = "../expect" } expect = { path = "../expect" }
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
mbe = { path = "../ra_mbe", package = "ra_mbe" } mbe = { path = "../ra_mbe", package = "ra_mbe" }
tt = { path = "../ra_tt", package = "ra_tt" } tt = { path = "../tt" }

View file

@ -1,15 +1,16 @@
[package] [package]
edition = "2018" name = "tt"
name = "ra_tt" version = "0.0.0"
version = "0.1.0"
authors = ["rust-analyzer developers"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
authors = ["rust-analyzer developers"]
edition = "2018"
[lib] [lib]
doctest = false doctest = false
[dependencies] [dependencies]
stdx = { path = "../stdx" }
# ideally, `serde` should be enabled by `rust-analyzer`, but we enable it here # ideally, `serde` should be enabled by `rust-analyzer`, but we enable it here
# to reduce number of compilations # to reduce number of compilations
smol_str = { version = "0.1.15", features = ["serde"] } smol_str = { version = "0.1.15", features = ["serde"] }
stdx = { path = "../stdx" }

View file

@ -200,7 +200,7 @@ impl TidyDocs {
"profile", "profile",
"ra_project_model", "ra_project_model",
"ra_syntax", "ra_syntax",
"ra_tt", "tt",
"ra_hir_ty", "ra_hir_ty",
]; ];