From db162df264a222021dbc7f1f93af94029f3948d9 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 27 Mar 2020 00:41:44 +0800 Subject: Remove deps on tt_mbe --- crates/ra_db/Cargo.toml | 2 +- crates/ra_db/src/input.rs | 23 ++++++++++++++-- crates/ra_db/src/lib.rs | 1 - crates/ra_hir_def/src/nameres/collector.rs | 6 ++-- crates/ra_hir_expand/src/proc_macro.rs | 5 ++-- crates/ra_mbe/src/lib.rs | 7 +++++ crates/ra_proc_macro/Cargo.toml | 1 - crates/ra_proc_macro/src/lib.rs | 44 +++++++----------------------- crates/ra_tt/src/lib.rs | 15 ++++++++-- 9 files changed, 57 insertions(+), 47 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index 82fd842a6..8ab409158 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml @@ -15,5 +15,5 @@ rustc-hash = "1.1.0" ra_syntax = { path = "../ra_syntax" } ra_cfg = { path = "../ra_cfg" } ra_prof = { path = "../ra_prof" } -ra_proc_macro = { path = "../ra_proc_macro" } +ra_tt = { path = "../ra_tt" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 65b553a9f..5ddce98c6 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -10,6 +10,7 @@ use std::{ fmt, ops, path::{Path, PathBuf}, str::FromStr, + sync::Arc, }; use ra_cfg::CfgOptions; @@ -19,7 +20,7 @@ use rustc_hash::FxHashSet; use crate::{RelativePath, RelativePathBuf}; use fmt::Display; -use ra_proc_macro::ProcMacro; +use ra_tt::TokenExpander; /// `FileId` is an integer which uniquely identifies a file. File paths are /// messy and system-dependent, so most of the code should work directly with @@ -117,7 +118,20 @@ impl Display for CrateName { } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct ProcMacroId(pub usize); +pub struct ProcMacroId(pub u32); + +#[derive(Debug, Clone)] +pub struct ProcMacro { + pub name: SmolStr, + pub expander: Arc, +} + +impl Eq for ProcMacro {} +impl PartialEq for ProcMacro { + fn eq(&self, other: &ProcMacro) -> bool { + self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander) + } +} #[derive(Debug, Clone, PartialEq, Eq)] pub struct CrateData { @@ -171,8 +185,11 @@ impl CrateGraph { cfg_options: CfgOptions, env: Env, extern_source: ExternSource, - proc_macro: Vec, + proc_macro: Vec<(SmolStr, Arc)>, ) -> CrateId { + let proc_macro = + proc_macro.into_iter().map(|(name, it)| ProcMacro { name, expander: it }).collect(); + let data = CrateData { root_file_id: file_id, edition, diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 5829ae465..a06f59c14 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -15,7 +15,6 @@ pub use crate::{ FileId, ProcMacroId, SourceRoot, SourceRootId, }, }; -pub use ra_proc_macro::ProcMacro; pub use relative_path::{RelativePath, RelativePathBuf}; pub use salsa; diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 9b46431cb..8fe3f8617 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -12,7 +12,7 @@ use hir_expand::{ }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId, ProcMacroId}; -use ra_syntax::{ast, SmolStr}; +use ra_syntax::ast; use rustc_hash::FxHashMap; use test_utils::tested_by; @@ -59,8 +59,8 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr .enumerate() .map(|(idx, it)| { // FIXME: a hacky way to create a Name from string. - let name = tt::Ident { text: SmolStr::new(&it.name()), id: tt::TokenId::unspecified() }; - (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx))) + let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() }; + (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32))) }) .collect(); diff --git a/crates/ra_hir_expand/src/proc_macro.rs b/crates/ra_hir_expand/src/proc_macro.rs index 296325b05..4d270e0de 100644 --- a/crates/ra_hir_expand/src/proc_macro.rs +++ b/crates/ra_hir_expand/src/proc_macro.rs @@ -23,9 +23,10 @@ impl ProcMacroExpander { let krate_graph = db.crate_graph(); let proc_macro = krate_graph[self.krate] .proc_macro - .get(self.proc_macro_id.0) + .get(self.proc_macro_id.0 as usize) .clone() .ok_or_else(|| mbe::ExpandError::ConversionError)?; - proc_macro.custom_derive(tt) + + proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from) } } diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 6a9037bfc..535b7daa0 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -28,6 +28,13 @@ pub enum ExpandError { BindingError(String), ConversionError, InvalidRepeat, + ProcMacroError(tt::ExpansionError), +} + +impl From for ExpandError { + fn from(it: tt::ExpansionError) -> Self { + ExpandError::ProcMacroError(it) + } } pub use crate::syntax_bridge::{ diff --git a/crates/ra_proc_macro/Cargo.toml b/crates/ra_proc_macro/Cargo.toml index a0fbb442d..bc2c37296 100644 --- a/crates/ra_proc_macro/Cargo.toml +++ b/crates/ra_proc_macro/Cargo.toml @@ -10,4 +10,3 @@ doctest = false [dependencies] ra_tt = { path = "../ra_tt" } -ra_mbe = { path = "../ra_mbe" } \ No newline at end of file diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs index b7fb641c3..5e21dd487 100644 --- a/crates/ra_proc_macro/src/lib.rs +++ b/crates/ra_proc_macro/src/lib.rs @@ -5,56 +5,29 @@ //! is used to provide basic infrastructure for communication between two //! processes: Client (RA itself), Server (the external program) -use ra_mbe::ExpandError; -use ra_tt::Subtree; +use ra_tt::{SmolStr, Subtree}; use std::{ path::{Path, PathBuf}, sync::Arc, }; -trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe { - fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result; -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProcMacroProcessExpander { process: Arc, + name: SmolStr, } -impl ProcMacroExpander for ProcMacroProcessExpander { - fn custom_derive( +impl ra_tt::TokenExpander for ProcMacroProcessExpander { + fn expand( &self, _subtree: &Subtree, - _derive_name: &str, - ) -> Result { + _attr: Option<&Subtree>, + ) -> Result { // FIXME: do nothing for now Ok(Subtree::default()) } } -#[derive(Debug, Clone)] -pub struct ProcMacro { - expander: Arc, - name: String, -} - -impl Eq for ProcMacro {} -impl PartialEq for ProcMacro { - fn eq(&self, other: &ProcMacro) -> bool { - self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander) - } -} - -impl ProcMacro { - pub fn name(&self) -> String { - self.name.clone() - } - - pub fn custom_derive(&self, subtree: &Subtree) -> Result { - self.expander.custom_derive(subtree, &self.name) - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProcMacroProcessSrv { path: PathBuf, @@ -76,7 +49,10 @@ impl ProcMacroClient { ProcMacroClient::Dummy } - pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec { + pub fn by_dylib_path( + &self, + _dylib_path: &Path, + ) -> Vec<(SmolStr, Arc)> { // FIXME: return empty for now vec![] } diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs index 1e2fb8b91..1015ce0a6 100644 --- a/crates/ra_tt/src/lib.rs +++ b/crates/ra_tt/src/lib.rs @@ -14,9 +14,12 @@ macro_rules! impl_froms { } } -use std::fmt; +use std::{ + fmt::{self, Debug}, + panic::RefUnwindSafe, +}; -use smol_str::SmolStr; +pub use smol_str::SmolStr; /// Represents identity of the token. /// @@ -184,3 +187,11 @@ impl Subtree { } pub mod buffer; + +#[derive(Debug, PartialEq, Eq)] +pub enum ExpansionError {} + +pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe { + fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>) + -> Result; +} -- cgit v1.2.3