mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Add LazyMacroId
This commit is contained in:
parent
cebb995d21
commit
0d55454073
5 changed files with 56 additions and 16 deletions
|
@ -248,7 +248,7 @@ mod tests {
|
||||||
kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))),
|
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();
|
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
||||||
|
|
||||||
// FIXME text() for syntax nodes parsed from token tree looks weird
|
// FIXME text() for syntax nodes parsed from token tree looks weird
|
||||||
|
|
|
@ -80,6 +80,10 @@ fn stringify_expand(
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
_tt: &tt::Subtree,
|
_tt: &tt::Subtree,
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
|
let id = match id {
|
||||||
|
MacroCallId::LazyMacro(id) => id,
|
||||||
|
};
|
||||||
|
|
||||||
let loc = db.lookup_intern_macro(id);
|
let loc = db.lookup_intern_macro(id);
|
||||||
|
|
||||||
let macro_content = {
|
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();
|
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
||||||
|
|
||||||
parsed.text().to_string()
|
parsed.text().to_string()
|
||||||
|
|
|
@ -10,7 +10,7 @@ use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
|
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
|
||||||
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
|
LazyMacroId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
@ -60,7 +60,7 @@ pub trait AstDatabase: SourceDatabase {
|
||||||
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
|
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
|
||||||
|
|
||||||
#[salsa::interned]
|
#[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_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
|
||||||
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
|
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
|
||||||
fn parse_macro(&self, macro_file: MacroFile)
|
fn parse_macro(&self, macro_file: MacroFile)
|
||||||
|
@ -108,6 +108,9 @@ pub(crate) fn macro_arg(
|
||||||
db: &dyn AstDatabase,
|
db: &dyn AstDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
|
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
|
||||||
|
let id = match id {
|
||||||
|
MacroCallId::LazyMacro(id) => id,
|
||||||
|
};
|
||||||
let loc = db.lookup_intern_macro(id);
|
let loc = db.lookup_intern_macro(id);
|
||||||
let arg = loc.kind.arg(db)?;
|
let arg = loc.kind.arg(db)?;
|
||||||
let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
|
let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
|
||||||
|
@ -118,7 +121,11 @@ pub(crate) fn macro_expand(
|
||||||
db: &dyn AstDatabase,
|
db: &dyn AstDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
) -> Result<Arc<tt::Subtree>, String> {
|
) -> 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_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")?;
|
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.
|
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
|
||||||
/// FIXME: Not completed
|
/// FIXME: Not completed
|
||||||
fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
|
fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
|
||||||
let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
|
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() {
|
let parent = match syn.parent() {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
|
|
|
@ -23,7 +23,10 @@ impl Hygiene {
|
||||||
let def_crate = match file_id.0 {
|
let def_crate = match file_id.0 {
|
||||||
HirFileIdRepr::FileId(_) => None,
|
HirFileIdRepr::FileId(_) => None,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
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 {
|
match loc.def.kind {
|
||||||
MacroDefKind::Declarative => loc.def.krate,
|
MacroDefKind::Declarative => loc.def.krate,
|
||||||
MacroDefKind::BuiltIn(_) => None,
|
MacroDefKind::BuiltIn(_) => None,
|
||||||
|
|
|
@ -70,7 +70,10 @@ impl HirFileId {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
HirFileIdRepr::FileId(file_id) => file_id,
|
HirFileIdRepr::FileId(file_id) => file_id,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
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)
|
loc.kind.file_id().original_file(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +84,10 @@ impl HirFileId {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
HirFileIdRepr::FileId(_) => None,
|
HirFileIdRepr::FileId(_) => None,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
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))
|
Some(loc.kind.node(db))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +98,10 @@ impl HirFileId {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
HirFileIdRepr::FileId(_) => None,
|
HirFileIdRepr::FileId(_) => None,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
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 arg_tt = loc.kind.arg(db)?;
|
||||||
let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
|
let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
|
||||||
|
@ -118,7 +127,10 @@ impl HirFileId {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
HirFileIdRepr::FileId(_) => None,
|
HirFileIdRepr::FileId(_) => None,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
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 {
|
let item = match loc.def.kind {
|
||||||
MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
|
MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
|
@ -137,16 +149,27 @@ pub struct MacroFile {
|
||||||
/// `MacroCallId` identifies a particular macro invocation, like
|
/// `MacroCallId` identifies a particular macro invocation, like
|
||||||
/// `println!("Hello, {}", world)`.
|
/// `println!("Hello, {}", world)`.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct MacroCallId(salsa::InternId);
|
pub enum MacroCallId {
|
||||||
impl salsa::InternKey for 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 {
|
fn from_intern_id(v: salsa::InternId) -> Self {
|
||||||
MacroCallId(v)
|
LazyMacroId(v)
|
||||||
}
|
}
|
||||||
fn as_intern_id(&self) -> salsa::InternId {
|
fn as_intern_id(&self) -> salsa::InternId {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<LazyMacroId> for MacroCallId {
|
||||||
|
fn from(it: LazyMacroId) -> Self {
|
||||||
|
MacroCallId::LazyMacro(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct MacroDefId {
|
pub struct MacroDefId {
|
||||||
// FIXME: krate and ast_id are currently optional because we don't have a
|
// FIXME: krate and ast_id are currently optional because we don't have a
|
||||||
|
@ -162,7 +185,7 @@ pub struct MacroDefId {
|
||||||
|
|
||||||
impl MacroDefId {
|
impl MacroDefId {
|
||||||
pub fn as_call_id(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> MacroCallId {
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue