Rename some things and turn macro to macro def into a query

This commit is contained in:
Lukas Wirth 2023-12-21 09:18:06 +01:00
parent 071fe4e4e9
commit 51a9e7831a
21 changed files with 165 additions and 163 deletions

View file

@ -199,6 +199,19 @@ impl AstIdMap {
FileAstId { raw, covariant: PhantomData }
}
pub fn ast_id_for_ptr<N: AstIdNode>(&self, ptr: AstPtr<N>) -> FileAstId<N> {
let ptr = ptr.syntax_node_ptr();
let hash = hash_ptr(&ptr);
match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
Some((&raw, &())) => FileAstId { raw, covariant: PhantomData },
None => panic!(
"Can't find {:?} in AstIdMap:\n{:?}",
ptr,
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
),
}
}
pub fn get<N: AstIdNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
AstPtr::try_from_raw(self.arena[id.raw].clone()).unwrap()
}

View file

@ -36,7 +36,7 @@ macro_rules! register_builtin {
$( BuiltinDeriveExpander::$trait => $expand, )*
};
let span = db.lookup_intern_macro_call(id).span(db);
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
expander(db, id, span, tt, token_map)
}

View file

@ -42,7 +42,7 @@ macro_rules! register_builtin {
$( BuiltinFnLikeExpander::$kind => $expand, )*
};
let span = db.lookup_intern_macro_call(id).span(db);
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
expander(db, id, tt, span)
}
@ -59,7 +59,7 @@ macro_rules! register_builtin {
$( EagerExpander::$e_kind => $e_expand, )*
};
let span = db.lookup_intern_macro_call(id).span(db);
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
expander(db, id, tt, span)
}

View file

@ -301,16 +301,15 @@ pub fn expand_speculative(
let mut speculative_expansion = match loc.def.kind {
MacroDefKind::ProcMacro(expander, ..) => {
tt.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
let call_site = loc.span(db);
expander.expand(
db,
loc.def.krate,
loc.krate,
&tt,
attr_arg.as_ref(),
call_site,
call_site,
call_site,
loc.call_site,
loc.call_site,
loc.call_site,
)
}
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
@ -782,7 +781,6 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
_ => None,
};
let call_site = loc.span(db);
let ExpandResult { value: mut tt, err } = expander.expand(
db,
loc.def.krate,
@ -790,10 +788,10 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
&macro_arg,
attr_arg,
// FIXME
call_site,
call_site,
loc.call_site,
loc.call_site,
// FIXME
call_site,
loc.call_site,
);
// Set a hard limit for the expanded tt

View file

@ -173,7 +173,6 @@ pub struct MacroCallLoc {
pub call_site: Span,
}
// FIXME: Might make sense to intern this? Given it's gonna be the same for a bunch of macro calls
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
pub krate: CrateId,
@ -467,18 +466,6 @@ impl MacroDefId {
}
impl MacroCallLoc {
pub fn span(&self, db: &dyn ExpandDatabase) -> Span {
let ast_id = self.kind.erased_ast_id();
let file_id = self.kind.file_id();
let range = db.ast_id_map(file_id).get_erased(ast_id).text_range();
match file_id.repr() {
HirFileIdRepr::FileId(file_id) => db.real_span_map(file_id).span_for_range(range),
HirFileIdRepr::MacroFile(m) => {
db.parse_macro_expansion(m).value.1.span_at(range.start())
}
}
}
pub fn to_node(&self, db: &dyn ExpandDatabase) -> InFile<SyntaxNode> {
match self.kind {
MacroCallKind::FnLike { ast_id, .. } => {
@ -546,7 +533,7 @@ impl MacroCallKind {
}
}
fn erased_ast_id(&self) -> ErasedFileAstId {
pub fn erased_ast_id(&self) -> ErasedFileAstId {
match *self {
MacroCallKind::FnLike { ast_id: InFile { value, .. }, .. } => value.erase(),
MacroCallKind::Derive { ast_id: InFile { value, .. }, .. } => value.erase(),