Remove deps on tt_mbe

This commit is contained in:
Edwin Cheng 2020-03-27 00:41:44 +08:00
parent 72e68d0caf
commit db162df264
10 changed files with 58 additions and 49 deletions

3
Cargo.lock generated
View file

@ -926,9 +926,9 @@ name = "ra_db"
version = "0.1.0"
dependencies = [
"ra_cfg",
"ra_proc_macro",
"ra_prof",
"ra_syntax",
"ra_tt",
"relative-path",
"rustc-hash",
"salsa",
@ -1086,7 +1086,6 @@ dependencies = [
name = "ra_proc_macro"
version = "0.1.0"
dependencies = [
"ra_mbe",
"ra_tt",
]

View file

@ -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" }

View file

@ -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<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 {
@ -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,

View file

@ -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;

View file

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

View file

@ -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)
}
}

View file

@ -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::{

View file

@ -10,4 +10,3 @@ doctest = false
[dependencies]
ra_tt = { path = "../ra_tt" }
ra_mbe = { path = "../ra_mbe" }

View file

@ -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,
@ -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![]
}

View file

@ -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<Subtree, ExpansionError>;
}