Skip to content

Commit

Permalink
Remove deps on tt_mbe
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin0cheng committed Mar 26, 2020
1 parent 72e68d0 commit db162df
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 49 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/ra_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
23 changes: 20 additions & 3 deletions crates/ra_db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
fmt, ops,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};

use ra_cfg::CfgOptions;
Expand All @@ -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
Expand Down Expand Up @@ -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<dyn TokenExpander>,
}

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 {
Expand Down Expand Up @@ -171,8 +185,11 @@ impl CrateGraph {
cfg_options: CfgOptions,
env: Env,
extern_source: ExternSource,
proc_macro: Vec<ProcMacro>,
proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>,
) -> 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,
Expand Down
1 change: 0 additions & 1 deletion crates/ra_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions crates/ra_hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions crates/ra_hir_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions crates/ra_mbe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ pub enum ExpandError {
BindingError(String),
ConversionError,
InvalidRepeat,
ProcMacroError(tt::ExpansionError),
}

impl From<tt::ExpansionError> for ExpandError {
fn from(it: tt::ExpansionError) -> Self {
ExpandError::ProcMacroError(it)
}
}

pub use crate::syntax_bridge::{
Expand Down
1 change: 0 additions & 1 deletion crates/ra_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ doctest = false

[dependencies]
ra_tt = { path = "../ra_tt" }
ra_mbe = { path = "../ra_mbe" }
44 changes: 10 additions & 34 deletions crates/ra_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subtree, ExpandError>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcMacroProcessExpander {
process: Arc<ProcMacroProcessSrv>,
name: SmolStr,
}

impl ProcMacroExpander for ProcMacroProcessExpander {
fn custom_derive(
impl ra_tt::TokenExpander for ProcMacroProcessExpander {
fn expand(
&self,
_subtree: &Subtree,
_derive_name: &str,
) -> Result<Subtree, ExpandError> {
_attr: Option<&Subtree>,
) -> Result<Subtree, ra_tt::ExpansionError> {
// FIXME: do nothing for now
Ok(Subtree::default())
}
}

#[derive(Debug, Clone)]
pub struct ProcMacro {
expander: Arc<dyn ProcMacroExpander>,
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<Subtree, ExpandError> {
self.expander.custom_derive(subtree, &self.name)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcMacroProcessSrv {
path: PathBuf,
Expand All @@ -76,7 +49,10 @@ impl ProcMacroClient {
ProcMacroClient::Dummy
}

pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> {
pub fn by_dylib_path(
&self,
_dylib_path: &Path,
) -> Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)> {
// FIXME: return empty for now
vec![]
}
Expand Down
15 changes: 13 additions & 2 deletions crates/ra_tt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Subtree, ExpansionError>;
}

0 comments on commit db162df

Please sign in to comment.