From 0d554540730925c074693b43503e65476eadbd65 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 17 Feb 2020 19:32:13 +0800 Subject: Add LazyMacroId --- crates/ra_hir_expand/src/builtin_derive.rs | 2 +- crates/ra_hir_expand/src/builtin_macro.rs | 6 ++++- crates/ra_hir_expand/src/db.rs | 20 +++++++++++---- crates/ra_hir_expand/src/hygiene.rs | 5 +++- crates/ra_hir_expand/src/lib.rs | 39 ++++++++++++++++++++++++------ 5 files changed, 56 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index d0e3eaf7e..0b70fb9e1 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs @@ -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 diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index f2bb0bddb..d91aa4ffa 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -80,6 +80,10 @@ fn stringify_expand( id: MacroCallId, _tt: &tt::Subtree, ) -> Result { + 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() diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 70d969238..b695c5b8d 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -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; #[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>; fn macro_def(&self, id: MacroDefId) -> Option>; fn parse_macro(&self, macro_file: MacroFile) @@ -108,6 +108,9 @@ pub(crate) fn macro_arg( db: &dyn AstDatabase, id: MacroCallId, ) -> Option> { + 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, 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, diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs index 2e8a533f7..ed0e7aa78 100644 --- a/crates/ra_hir_expand/src/hygiene.rs +++ b/crates/ra_hir_expand/src/hygiene.rs @@ -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, diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 9506f2e1c..3a1c6d2b0 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -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 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() } } -- cgit v1.2.3