2020-03-18 17:47:59 +08:00
|
|
|
//! Proc Macro Expander stub
|
|
|
|
|
2021-10-27 14:35:58 +02:00
|
|
|
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind};
|
2021-08-20 14:34:46 +02:00
|
|
|
use mbe::ExpandResult;
|
2020-03-18 17:47:59 +08:00
|
|
|
|
2021-10-27 14:35:58 +02:00
|
|
|
use crate::db::AstDatabase;
|
|
|
|
|
2020-03-18 17:47:59 +08:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
|
|
|
pub struct ProcMacroExpander {
|
|
|
|
krate: CrateId,
|
2020-09-18 16:43:50 +02:00
|
|
|
proc_macro_id: Option<ProcMacroId>,
|
2020-03-18 17:47:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcMacroExpander {
|
2020-09-18 16:43:50 +02:00
|
|
|
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
|
|
|
|
Self { krate, proc_macro_id: Some(proc_macro_id) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dummy(krate: CrateId) -> Self {
|
|
|
|
// FIXME: Should store the name for better errors
|
|
|
|
Self { krate, proc_macro_id: None }
|
2020-03-18 17:47:59 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 13:37:11 +02:00
|
|
|
pub fn is_dummy(&self) -> bool {
|
|
|
|
self.proc_macro_id.is_none()
|
|
|
|
}
|
|
|
|
|
2020-03-18 17:47:59 +08:00
|
|
|
pub fn expand(
|
2020-05-26 14:12:13 -04:00
|
|
|
self,
|
2020-03-18 17:47:59 +08:00
|
|
|
db: &dyn AstDatabase,
|
2020-12-11 14:57:50 +01:00
|
|
|
calling_crate: CrateId,
|
2020-03-18 20:56:46 +08:00
|
|
|
tt: &tt::Subtree,
|
2021-05-31 13:37:11 +02:00
|
|
|
attr_arg: Option<&tt::Subtree>,
|
2021-08-20 14:34:46 +02:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-09-18 16:43:50 +02:00
|
|
|
match self.proc_macro_id {
|
|
|
|
Some(id) => {
|
|
|
|
let krate_graph = db.crate_graph();
|
2021-08-20 14:34:46 +02:00
|
|
|
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
|
|
|
|
Some(proc_macro) => proc_macro,
|
2021-10-27 14:35:58 +02:00
|
|
|
None => return ExpandResult::str_err("No proc-macro found.".to_string()),
|
2021-08-20 14:34:46 +02:00
|
|
|
};
|
2020-09-18 16:43:50 +02:00
|
|
|
|
2020-12-11 14:57:50 +01:00
|
|
|
// Proc macros have access to the environment variables of the invoking crate.
|
|
|
|
let env = &krate_graph[calling_crate].env;
|
2021-10-27 14:35:58 +02:00
|
|
|
match proc_macro.expander.expand(tt, attr_arg, env) {
|
|
|
|
Ok(t) => ExpandResult::ok(t),
|
|
|
|
Err(err) => match err {
|
|
|
|
// Don't discard the item in case something unexpected happened while expanding attributes
|
|
|
|
ProcMacroExpansionError::System(text)
|
|
|
|
if proc_macro.kind == ProcMacroKind::Attr =>
|
|
|
|
{
|
|
|
|
ExpandResult {
|
|
|
|
value: tt.clone(),
|
2022-02-03 17:25:24 +01:00
|
|
|
err: Some(mbe::ExpandError::Other(text.into())),
|
2021-10-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcMacroExpansionError::System(text)
|
|
|
|
| ProcMacroExpansionError::Panic(text) => {
|
2022-02-03 17:25:24 +01:00
|
|
|
ExpandResult::only_err(mbe::ExpandError::Other(text.into()))
|
2021-10-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-09-18 16:43:50 +02:00
|
|
|
}
|
2021-08-20 14:34:46 +02:00
|
|
|
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
|
2020-09-18 16:43:50 +02:00
|
|
|
}
|
2020-03-18 17:47:59 +08:00
|
|
|
}
|
|
|
|
}
|