rust-analyzer/crates/hir-expand/src/proc_macro.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.7 KiB
Rust
Raw Normal View History

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