Add LazyMacroId

This commit is contained in:
Edwin Cheng 2020-02-17 19:32:13 +08:00
parent cebb995d21
commit 0d55454073
5 changed files with 56 additions and 16 deletions

View file

@ -248,7 +248,7 @@ mod tests {
kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))),
};
let id = db.intern_macro(loc);
let id: MacroCallId = db.intern_macro(loc).into();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
// FIXME text() for syntax nodes parsed from token tree looks weird

View file

@ -80,6 +80,10 @@ fn stringify_expand(
id: MacroCallId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
let id = match id {
MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(id);
let macro_content = {
@ -241,7 +245,7 @@ mod tests {
)),
};
let id = db.intern_macro(loc);
let id: MacroCallId = db.intern_macro(loc).into();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
parsed.text().to_string()

View file

@ -10,7 +10,7 @@ use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
use crate::{
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
LazyMacroId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
};
#[derive(Debug, Clone, Eq, PartialEq)]
@ -60,7 +60,7 @@ pub trait AstDatabase: SourceDatabase {
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
#[salsa::interned]
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId;
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
fn parse_macro(&self, macro_file: MacroFile)
@ -108,6 +108,9 @@ pub(crate) fn macro_arg(
db: &dyn AstDatabase,
id: MacroCallId,
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
let id = match id {
MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(id);
let arg = loc.kind.arg(db)?;
let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
@ -118,7 +121,11 @@ pub(crate) fn macro_expand(
db: &dyn AstDatabase,
id: MacroCallId,
) -> Result<Arc<tt::Subtree>, String> {
let loc = db.lookup_intern_macro(id);
let lazy_id = match id {
MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(lazy_id);
let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
@ -167,8 +174,11 @@ pub(crate) fn parse_macro(
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
/// FIXME: Not completed
fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
let lazy_id = match id {
MacroCallId::LazyMacro(id) => id,
};
let syn = db.lookup_intern_macro(lazy_id).kind.node(db).value;
let parent = match syn.parent() {
Some(it) => it,

View file

@ -23,7 +23,10 @@ impl Hygiene {
let def_crate = match file_id.0 {
HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => {
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
let lazy_id = match macro_file.macro_call_id {
crate::MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(lazy_id);
match loc.def.kind {
MacroDefKind::Declarative => loc.def.krate,
MacroDefKind::BuiltIn(_) => None,

View file

@ -70,7 +70,10 @@ impl HirFileId {
match self.0 {
HirFileIdRepr::FileId(file_id) => file_id,
HirFileIdRepr::MacroFile(macro_file) => {
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
let lazy_id = match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(lazy_id);
loc.kind.file_id().original_file(db)
}
}
@ -81,7 +84,10 @@ impl HirFileId {
match self.0 {
HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => {
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
let lazy_id = match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => id,
};
let loc = db.lookup_intern_macro(lazy_id);
Some(loc.kind.node(db))
}
}
@ -92,7 +98,10 @@ impl HirFileId {
match self.0 {
HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => {
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
let lazy_id = match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => id,
};
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
let arg_tt = loc.kind.arg(db)?;
let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
@ -118,7 +127,10 @@ impl HirFileId {
match self.0 {
HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => {
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
let lazy_id = match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => id,
};
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
let item = match loc.def.kind {
MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
_ => return None,
@ -137,16 +149,27 @@ pub struct MacroFile {
/// `MacroCallId` identifies a particular macro invocation, like
/// `println!("Hello, {}", world)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroCallId(salsa::InternId);
impl salsa::InternKey for MacroCallId {
pub enum MacroCallId {
LazyMacro(LazyMacroId),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LazyMacroId(salsa::InternId);
impl salsa::InternKey for LazyMacroId {
fn from_intern_id(v: salsa::InternId) -> Self {
MacroCallId(v)
LazyMacroId(v)
}
fn as_intern_id(&self) -> salsa::InternId {
self.0
}
}
impl From<LazyMacroId> for MacroCallId {
fn from(it: LazyMacroId) -> Self {
MacroCallId::LazyMacro(it)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
// FIXME: krate and ast_id are currently optional because we don't have a
@ -162,7 +185,7 @@ pub struct MacroDefId {
impl MacroDefId {
pub fn as_call_id(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> MacroCallId {
db.intern_macro(MacroCallLoc { def: self, kind })
db.intern_macro(MacroCallLoc { def: self, kind }).into()
}
}