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};
|
2022-06-24 13:03:13 +02:00
|
|
|
use stdx::never;
|
2020-03-18 17:47:59 +08:00
|
|
|
|
2023-01-31 11:49:49 +01:00
|
|
|
use crate::{db::AstDatabase, tt, ExpandError, ExpandResult};
|
2021-10-27 14:35:58 +02:00
|
|
|
|
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();
|
2022-06-15 17:33:55 +02:00
|
|
|
let proc_macros = match &krate_graph[self.krate].proc_macro {
|
|
|
|
Ok(proc_macros) => proc_macros,
|
2022-06-24 13:03:13 +02:00
|
|
|
Err(_) => {
|
|
|
|
never!("Non-dummy expander even though there are no proc macros");
|
2023-01-31 11:49:49 +01:00
|
|
|
return ExpandResult::with_err(
|
|
|
|
tt::Subtree::empty(),
|
|
|
|
ExpandError::Other("Internal error".into()),
|
|
|
|
);
|
2022-06-15 17:33:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let proc_macro = match proc_macros.get(id.0 as usize) {
|
2021-08-20 14:34:46 +02:00
|
|
|
Some(proc_macro) => proc_macro,
|
2022-02-21 19:14:06 +01:00
|
|
|
None => {
|
2022-06-24 13:03:13 +02:00
|
|
|
never!(
|
|
|
|
"Proc macro index out of bounds: the length is {} but the index is {}",
|
|
|
|
proc_macros.len(),
|
|
|
|
id.0
|
|
|
|
);
|
2023-01-31 11:49:49 +01:00
|
|
|
return ExpandResult::with_err(
|
|
|
|
tt::Subtree::empty(),
|
|
|
|
ExpandError::Other("Internal error".into()),
|
|
|
|
);
|
2022-02-21 19:14:06 +01:00
|
|
|
}
|
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-21 19:14:06 +01:00
|
|
|
err: Some(ExpandError::Other(text.into())),
|
2021-10-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ProcMacroExpansionError::System(text)
|
2023-01-31 11:49:49 +01:00
|
|
|
| ProcMacroExpansionError::Panic(text) => ExpandResult::with_err(
|
|
|
|
tt::Subtree::empty(),
|
|
|
|
ExpandError::Other(text.into()),
|
|
|
|
),
|
2021-10-27 14:35:58 +02:00
|
|
|
},
|
|
|
|
}
|
2020-09-18 16:43:50 +02:00
|
|
|
}
|
2023-01-31 11:49:49 +01:00
|
|
|
None => ExpandResult::with_err(
|
|
|
|
tt::Subtree::empty(),
|
|
|
|
ExpandError::UnresolvedProcMacro(self.krate),
|
|
|
|
),
|
2020-09-18 16:43:50 +02:00
|
|
|
}
|
2020-03-18 17:47:59 +08:00
|
|
|
}
|
|
|
|
}
|