2020-03-18 04:47:59 -05:00
|
|
|
//! Proc Macro Expander stub
|
|
|
|
|
2021-10-27 07:35:58 -05:00
|
|
|
use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind};
|
2022-06-24 06:03:13 -05:00
|
|
|
use stdx::never;
|
2020-03-18 04:47:59 -05:00
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
|
2021-10-27 07:35:58 -05:00
|
|
|
|
2020-03-18 04:47:59 -05:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
|
|
|
pub struct ProcMacroExpander {
|
2023-05-20 03:38:25 -05:00
|
|
|
proc_macro_id: ProcMacroId,
|
2020-03-18 04:47:59 -05:00
|
|
|
}
|
|
|
|
|
2023-05-20 03:38:25 -05:00
|
|
|
const DUMMY_ID: u32 = !0;
|
|
|
|
|
2020-03-18 04:47:59 -05:00
|
|
|
impl ProcMacroExpander {
|
2023-03-13 10:49:32 -05:00
|
|
|
pub fn new(proc_macro_id: ProcMacroId) -> Self {
|
2023-05-20 03:38:25 -05:00
|
|
|
assert_ne!(proc_macro_id.0, DUMMY_ID);
|
|
|
|
Self { proc_macro_id }
|
2020-09-18 09:43:50 -05:00
|
|
|
}
|
|
|
|
|
2023-03-13 10:49:32 -05:00
|
|
|
pub fn dummy() -> Self {
|
2023-05-20 03:38:25 -05:00
|
|
|
Self { proc_macro_id: ProcMacroId(DUMMY_ID) }
|
2020-03-18 04:47:59 -05:00
|
|
|
}
|
|
|
|
|
2021-05-31 06:37:11 -05:00
|
|
|
pub fn is_dummy(&self) -> bool {
|
2023-05-20 03:38:25 -05:00
|
|
|
self.proc_macro_id.0 == DUMMY_ID
|
2021-05-31 06:37:11 -05:00
|
|
|
}
|
|
|
|
|
2020-03-18 04:47:59 -05:00
|
|
|
pub fn expand(
|
2020-05-26 13:12:13 -05:00
|
|
|
self,
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2023-03-13 10:49:32 -05:00
|
|
|
def_crate: CrateId,
|
2020-12-11 07:57:50 -06:00
|
|
|
calling_crate: CrateId,
|
2020-03-18 07:56:46 -05:00
|
|
|
tt: &tt::Subtree,
|
2021-05-31 06:37:11 -05:00
|
|
|
attr_arg: Option<&tt::Subtree>,
|
2021-08-20 07:34:46 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-09-18 09:43:50 -05:00
|
|
|
match self.proc_macro_id {
|
2023-05-20 03:38:25 -05:00
|
|
|
ProcMacroId(DUMMY_ID) => {
|
|
|
|
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
|
|
|
|
}
|
|
|
|
ProcMacroId(id) => {
|
2023-03-25 10:42:52 -05:00
|
|
|
let proc_macros = db.proc_macros();
|
|
|
|
let proc_macros = match proc_macros.get(&def_crate) {
|
|
|
|
Some(Ok(proc_macros)) => proc_macros,
|
|
|
|
Some(Err(_)) | None => {
|
2022-06-24 06:03:13 -05:00
|
|
|
never!("Non-dummy expander even though there are no proc macros");
|
2023-04-16 12:20:48 -05:00
|
|
|
return ExpandResult::new(
|
2023-01-31 04:49:49 -06:00
|
|
|
tt::Subtree::empty(),
|
2023-06-07 04:20:10 -05:00
|
|
|
ExpandError::other("Internal error"),
|
2023-01-31 04:49:49 -06:00
|
|
|
);
|
2022-06-15 10:33:55 -05:00
|
|
|
}
|
|
|
|
};
|
2023-05-20 03:38:25 -05:00
|
|
|
let proc_macro = match proc_macros.get(id as usize) {
|
2021-08-20 07:34:46 -05:00
|
|
|
Some(proc_macro) => proc_macro,
|
2022-02-21 12:14:06 -06:00
|
|
|
None => {
|
2022-06-24 06:03:13 -05:00
|
|
|
never!(
|
|
|
|
"Proc macro index out of bounds: the length is {} but the index is {}",
|
|
|
|
proc_macros.len(),
|
2023-05-20 03:38:25 -05:00
|
|
|
id
|
2022-06-24 06:03:13 -05:00
|
|
|
);
|
2023-04-16 12:20:48 -05:00
|
|
|
return ExpandResult::new(
|
2023-01-31 04:49:49 -06:00
|
|
|
tt::Subtree::empty(),
|
2023-06-07 04:20:10 -05:00
|
|
|
ExpandError::other("Internal error"),
|
2023-01-31 04:49:49 -06:00
|
|
|
);
|
2022-02-21 12:14:06 -06:00
|
|
|
}
|
2021-08-20 07:34:46 -05:00
|
|
|
};
|
2020-09-18 09:43:50 -05:00
|
|
|
|
2023-03-25 10:42:52 -05:00
|
|
|
let krate_graph = db.crate_graph();
|
2020-12-11 07:57:50 -06:00
|
|
|
// Proc macros have access to the environment variables of the invoking crate.
|
|
|
|
let env = &krate_graph[calling_crate].env;
|
2021-10-27 07:35:58 -05: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 =>
|
|
|
|
{
|
2023-06-07 04:20:10 -05:00
|
|
|
ExpandResult { value: tt.clone(), err: Some(ExpandError::other(text)) }
|
2021-10-27 07:35:58 -05:00
|
|
|
}
|
|
|
|
ProcMacroExpansionError::System(text)
|
2023-04-16 12:20:48 -05:00
|
|
|
| ProcMacroExpansionError::Panic(text) => {
|
2023-06-07 04:20:10 -05:00
|
|
|
ExpandResult::new(tt::Subtree::empty(), ExpandError::other(text))
|
2023-04-16 12:20:48 -05:00
|
|
|
}
|
2021-10-27 07:35:58 -05:00
|
|
|
},
|
|
|
|
}
|
2020-09-18 09:43:50 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 04:47:59 -05:00
|
|
|
}
|
|
|
|
}
|