aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/Cargo.toml2
-rw-r--r--crates/ra_db/src/input.rs23
-rw-r--r--crates/ra_db/src/lib.rs1
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs6
-rw-r--r--crates/ra_hir_expand/src/proc_macro.rs5
-rw-r--r--crates/ra_mbe/src/lib.rs7
-rw-r--r--crates/ra_proc_macro/Cargo.toml1
-rw-r--r--crates/ra_proc_macro/src/lib.rs44
-rw-r--r--crates/ra_tt/src/lib.rs15
9 files changed, 57 insertions, 47 deletions
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"
15ra_syntax = { path = "../ra_syntax" } 15ra_syntax = { path = "../ra_syntax" }
16ra_cfg = { path = "../ra_cfg" } 16ra_cfg = { path = "../ra_cfg" }
17ra_prof = { path = "../ra_prof" } 17ra_prof = { path = "../ra_prof" }
18ra_proc_macro = { path = "../ra_proc_macro" } 18ra_tt = { path = "../ra_tt" }
19test_utils = { path = "../test_utils" } 19test_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::{
10 fmt, ops, 10 fmt, ops,
11 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
12 str::FromStr, 12 str::FromStr,
13 sync::Arc,
13}; 14};
14 15
15use ra_cfg::CfgOptions; 16use ra_cfg::CfgOptions;
@@ -19,7 +20,7 @@ use rustc_hash::FxHashSet;
19 20
20use crate::{RelativePath, RelativePathBuf}; 21use crate::{RelativePath, RelativePathBuf};
21use fmt::Display; 22use fmt::Display;
22use ra_proc_macro::ProcMacro; 23use ra_tt::TokenExpander;
23 24
24/// `FileId` is an integer which uniquely identifies a file. File paths are 25/// `FileId` is an integer which uniquely identifies a file. File paths are
25/// messy and system-dependent, so most of the code should work directly with 26/// messy and system-dependent, so most of the code should work directly with
@@ -117,7 +118,20 @@ impl Display for CrateName {
117} 118}
118 119
119#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 120#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
120pub struct ProcMacroId(pub usize); 121pub struct ProcMacroId(pub u32);
122
123#[derive(Debug, Clone)]
124pub struct ProcMacro {
125 pub name: SmolStr,
126 pub expander: Arc<dyn TokenExpander>,
127}
128
129impl Eq for ProcMacro {}
130impl PartialEq for ProcMacro {
131 fn eq(&self, other: &ProcMacro) -> bool {
132 self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander)
133 }
134}
121 135
122#[derive(Debug, Clone, PartialEq, Eq)] 136#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct CrateData { 137pub struct CrateData {
@@ -171,8 +185,11 @@ impl CrateGraph {
171 cfg_options: CfgOptions, 185 cfg_options: CfgOptions,
172 env: Env, 186 env: Env,
173 extern_source: ExternSource, 187 extern_source: ExternSource,
174 proc_macro: Vec<ProcMacro>, 188 proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>,
175 ) -> CrateId { 189 ) -> CrateId {
190 let proc_macro =
191 proc_macro.into_iter().map(|(name, it)| ProcMacro { name, expander: it }).collect();
192
176 let data = CrateData { 193 let data = CrateData {
177 root_file_id: file_id, 194 root_file_id: file_id,
178 edition, 195 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::{
15 FileId, ProcMacroId, SourceRoot, SourceRootId, 15 FileId, ProcMacroId, SourceRoot, SourceRootId,
16 }, 16 },
17}; 17};
18pub use ra_proc_macro::ProcMacro;
19pub use relative_path::{RelativePath, RelativePathBuf}; 18pub use relative_path::{RelativePath, RelativePathBuf};
20pub use salsa; 19pub use salsa;
21 20
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::{
12}; 12};
13use ra_cfg::CfgOptions; 13use ra_cfg::CfgOptions;
14use ra_db::{CrateId, FileId, ProcMacroId}; 14use ra_db::{CrateId, FileId, ProcMacroId};
15use ra_syntax::{ast, SmolStr}; 15use ra_syntax::ast;
16use rustc_hash::FxHashMap; 16use rustc_hash::FxHashMap;
17use test_utils::tested_by; 17use test_utils::tested_by;
18 18
@@ -59,8 +59,8 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr
59 .enumerate() 59 .enumerate()
60 .map(|(idx, it)| { 60 .map(|(idx, it)| {
61 // FIXME: a hacky way to create a Name from string. 61 // FIXME: a hacky way to create a Name from string.
62 let name = tt::Ident { text: SmolStr::new(&it.name()), id: tt::TokenId::unspecified() }; 62 let name = tt::Ident { text: it.name.clone(), id: tt::TokenId::unspecified() };
63 (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx))) 63 (name.as_name(), ProcMacroExpander::new(def_map.krate, ProcMacroId(idx as u32)))
64 }) 64 })
65 .collect(); 65 .collect();
66 66
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 {
23 let krate_graph = db.crate_graph(); 23 let krate_graph = db.crate_graph();
24 let proc_macro = krate_graph[self.krate] 24 let proc_macro = krate_graph[self.krate]
25 .proc_macro 25 .proc_macro
26 .get(self.proc_macro_id.0) 26 .get(self.proc_macro_id.0 as usize)
27 .clone() 27 .clone()
28 .ok_or_else(|| mbe::ExpandError::ConversionError)?; 28 .ok_or_else(|| mbe::ExpandError::ConversionError)?;
29 proc_macro.custom_derive(tt) 29
30 proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
30 } 31 }
31} 32}
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 {
28 BindingError(String), 28 BindingError(String),
29 ConversionError, 29 ConversionError,
30 InvalidRepeat, 30 InvalidRepeat,
31 ProcMacroError(tt::ExpansionError),
32}
33
34impl From<tt::ExpansionError> for ExpandError {
35 fn from(it: tt::ExpansionError) -> Self {
36 ExpandError::ProcMacroError(it)
37 }
31} 38}
32 39
33pub use crate::syntax_bridge::{ 40pub 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
10 10
11[dependencies] 11[dependencies]
12ra_tt = { path = "../ra_tt" } 12ra_tt = { path = "../ra_tt" }
13ra_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 @@
5//! is used to provide basic infrastructure for communication between two 5//! is used to provide basic infrastructure for communication between two
6//! processes: Client (RA itself), Server (the external program) 6//! processes: Client (RA itself), Server (the external program)
7 7
8use ra_mbe::ExpandError; 8use ra_tt::{SmolStr, Subtree};
9use ra_tt::Subtree;
10use std::{ 9use std::{
11 path::{Path, PathBuf}, 10 path::{Path, PathBuf},
12 sync::Arc, 11 sync::Arc,
13}; 12};
14 13
15trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe {
16 fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result<Subtree, ExpandError>;
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)] 14#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct ProcMacroProcessExpander { 15pub struct ProcMacroProcessExpander {
21 process: Arc<ProcMacroProcessSrv>, 16 process: Arc<ProcMacroProcessSrv>,
17 name: SmolStr,
22} 18}
23 19
24impl ProcMacroExpander for ProcMacroProcessExpander { 20impl ra_tt::TokenExpander for ProcMacroProcessExpander {
25 fn custom_derive( 21 fn expand(
26 &self, 22 &self,
27 _subtree: &Subtree, 23 _subtree: &Subtree,
28 _derive_name: &str, 24 _attr: Option<&Subtree>,
29 ) -> Result<Subtree, ExpandError> { 25 ) -> Result<Subtree, ra_tt::ExpansionError> {
30 // FIXME: do nothing for now 26 // FIXME: do nothing for now
31 Ok(Subtree::default()) 27 Ok(Subtree::default())
32 } 28 }
33} 29}
34 30
35#[derive(Debug, Clone)]
36pub struct ProcMacro {
37 expander: Arc<dyn ProcMacroExpander>,
38 name: String,
39}
40
41impl Eq for ProcMacro {}
42impl PartialEq for ProcMacro {
43 fn eq(&self, other: &ProcMacro) -> bool {
44 self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander)
45 }
46}
47
48impl ProcMacro {
49 pub fn name(&self) -> String {
50 self.name.clone()
51 }
52
53 pub fn custom_derive(&self, subtree: &Subtree) -> Result<Subtree, ExpandError> {
54 self.expander.custom_derive(subtree, &self.name)
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)] 31#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct ProcMacroProcessSrv { 32pub struct ProcMacroProcessSrv {
60 path: PathBuf, 33 path: PathBuf,
@@ -76,7 +49,10 @@ impl ProcMacroClient {
76 ProcMacroClient::Dummy 49 ProcMacroClient::Dummy
77 } 50 }
78 51
79 pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> { 52 pub fn by_dylib_path(
53 &self,
54 _dylib_path: &Path,
55 ) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
80 // FIXME: return empty for now 56 // FIXME: return empty for now
81 vec![] 57 vec![]
82 } 58 }
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 {
14 } 14 }
15} 15}
16 16
17use std::fmt; 17use std::{
18 fmt::{self, Debug},
19 panic::RefUnwindSafe,
20};
18 21
19use smol_str::SmolStr; 22pub use smol_str::SmolStr;
20 23
21/// Represents identity of the token. 24/// Represents identity of the token.
22/// 25///
@@ -184,3 +187,11 @@ impl Subtree {
184} 187}
185 188
186pub mod buffer; 189pub mod buffer;
190
191#[derive(Debug, PartialEq, Eq)]
192pub enum ExpansionError {}
193
194pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe {
195 fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>)
196 -> Result<Subtree, ExpansionError>;
197}