aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-02-17 11:32:13 +0000
committerEdwin Cheng <[email protected]>2020-03-03 17:21:14 +0000
commit0d554540730925c074693b43503e65476eadbd65 (patch)
tree8f51b96f608bbbf694d30c62214800f5f28a3841
parentcebb995d21e18990939287f55628237563583b27 (diff)
Add LazyMacroId
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs2
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs6
-rw-r--r--crates/ra_hir_expand/src/db.rs20
-rw-r--r--crates/ra_hir_expand/src/hygiene.rs5
-rw-r--r--crates/ra_hir_expand/src/lib.rs39
5 files changed, 56 insertions, 16 deletions
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 {
248 kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))), 248 kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))),
249 }; 249 };
250 250
251 let id = db.intern_macro(loc); 251 let id: MacroCallId = db.intern_macro(loc).into();
252 let parsed = db.parse_or_expand(id.as_file()).unwrap(); 252 let parsed = db.parse_or_expand(id.as_file()).unwrap();
253 253
254 // FIXME text() for syntax nodes parsed from token tree looks weird 254 // 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(
80 id: MacroCallId, 80 id: MacroCallId,
81 _tt: &tt::Subtree, 81 _tt: &tt::Subtree,
82) -> Result<tt::Subtree, mbe::ExpandError> { 82) -> Result<tt::Subtree, mbe::ExpandError> {
83 let id = match id {
84 MacroCallId::LazyMacro(id) => id,
85 };
86
83 let loc = db.lookup_intern_macro(id); 87 let loc = db.lookup_intern_macro(id);
84 88
85 let macro_content = { 89 let macro_content = {
@@ -241,7 +245,7 @@ mod tests {
241 )), 245 )),
242 }; 246 };
243 247
244 let id = db.intern_macro(loc); 248 let id: MacroCallId = db.intern_macro(loc).into();
245 let parsed = db.parse_or_expand(id.as_file()).unwrap(); 249 let parsed = db.parse_or_expand(id.as_file()).unwrap();
246 250
247 parsed.text().to_string() 251 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};
10 10
11use crate::{ 11use crate::{
12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, 12 ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
13 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, 13 LazyMacroId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
14}; 14};
15 15
16#[derive(Debug, Clone, Eq, PartialEq)] 16#[derive(Debug, Clone, Eq, PartialEq)]
@@ -60,7 +60,7 @@ pub trait AstDatabase: SourceDatabase {
60 fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>; 60 fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
61 61
62 #[salsa::interned] 62 #[salsa::interned]
63 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; 63 fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId;
64 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>; 64 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
65 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>; 65 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
66 fn parse_macro(&self, macro_file: MacroFile) 66 fn parse_macro(&self, macro_file: MacroFile)
@@ -108,6 +108,9 @@ pub(crate) fn macro_arg(
108 db: &dyn AstDatabase, 108 db: &dyn AstDatabase,
109 id: MacroCallId, 109 id: MacroCallId,
110) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> { 110) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
111 let id = match id {
112 MacroCallId::LazyMacro(id) => id,
113 };
111 let loc = db.lookup_intern_macro(id); 114 let loc = db.lookup_intern_macro(id);
112 let arg = loc.kind.arg(db)?; 115 let arg = loc.kind.arg(db)?;
113 let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?; 116 let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
@@ -118,7 +121,11 @@ pub(crate) fn macro_expand(
118 db: &dyn AstDatabase, 121 db: &dyn AstDatabase,
119 id: MacroCallId, 122 id: MacroCallId,
120) -> Result<Arc<tt::Subtree>, String> { 123) -> Result<Arc<tt::Subtree>, String> {
121 let loc = db.lookup_intern_macro(id); 124 let lazy_id = match id {
125 MacroCallId::LazyMacro(id) => id,
126 };
127
128 let loc = db.lookup_intern_macro(lazy_id);
122 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; 129 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
123 130
124 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; 131 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
@@ -167,8 +174,11 @@ pub(crate) fn parse_macro(
167 174
168/// Given a `MacroCallId`, return what `FragmentKind` it belongs to. 175/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
169/// FIXME: Not completed 176/// FIXME: Not completed
170fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind { 177fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
171 let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value; 178 let lazy_id = match id {
179 MacroCallId::LazyMacro(id) => id,
180 };
181 let syn = db.lookup_intern_macro(lazy_id).kind.node(db).value;
172 182
173 let parent = match syn.parent() { 183 let parent = match syn.parent() {
174 Some(it) => it, 184 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 {
23 let def_crate = match file_id.0 { 23 let def_crate = match file_id.0 {
24 HirFileIdRepr::FileId(_) => None, 24 HirFileIdRepr::FileId(_) => None,
25 HirFileIdRepr::MacroFile(macro_file) => { 25 HirFileIdRepr::MacroFile(macro_file) => {
26 let loc = db.lookup_intern_macro(macro_file.macro_call_id); 26 let lazy_id = match macro_file.macro_call_id {
27 crate::MacroCallId::LazyMacro(id) => id,
28 };
29 let loc = db.lookup_intern_macro(lazy_id);
27 match loc.def.kind { 30 match loc.def.kind {
28 MacroDefKind::Declarative => loc.def.krate, 31 MacroDefKind::Declarative => loc.def.krate,
29 MacroDefKind::BuiltIn(_) => None, 32 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 {
70 match self.0 { 70 match self.0 {
71 HirFileIdRepr::FileId(file_id) => file_id, 71 HirFileIdRepr::FileId(file_id) => file_id,
72 HirFileIdRepr::MacroFile(macro_file) => { 72 HirFileIdRepr::MacroFile(macro_file) => {
73 let loc = db.lookup_intern_macro(macro_file.macro_call_id); 73 let lazy_id = match macro_file.macro_call_id {
74 MacroCallId::LazyMacro(id) => id,
75 };
76 let loc = db.lookup_intern_macro(lazy_id);
74 loc.kind.file_id().original_file(db) 77 loc.kind.file_id().original_file(db)
75 } 78 }
76 } 79 }
@@ -81,7 +84,10 @@ impl HirFileId {
81 match self.0 { 84 match self.0 {
82 HirFileIdRepr::FileId(_) => None, 85 HirFileIdRepr::FileId(_) => None,
83 HirFileIdRepr::MacroFile(macro_file) => { 86 HirFileIdRepr::MacroFile(macro_file) => {
84 let loc = db.lookup_intern_macro(macro_file.macro_call_id); 87 let lazy_id = match macro_file.macro_call_id {
88 MacroCallId::LazyMacro(id) => id,
89 };
90 let loc = db.lookup_intern_macro(lazy_id);
85 Some(loc.kind.node(db)) 91 Some(loc.kind.node(db))
86 } 92 }
87 } 93 }
@@ -92,7 +98,10 @@ impl HirFileId {
92 match self.0 { 98 match self.0 {
93 HirFileIdRepr::FileId(_) => None, 99 HirFileIdRepr::FileId(_) => None,
94 HirFileIdRepr::MacroFile(macro_file) => { 100 HirFileIdRepr::MacroFile(macro_file) => {
95 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); 101 let lazy_id = match macro_file.macro_call_id {
102 MacroCallId::LazyMacro(id) => id,
103 };
104 let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
96 105
97 let arg_tt = loc.kind.arg(db)?; 106 let arg_tt = loc.kind.arg(db)?;
98 let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; 107 let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
@@ -118,7 +127,10 @@ impl HirFileId {
118 match self.0 { 127 match self.0 {
119 HirFileIdRepr::FileId(_) => None, 128 HirFileIdRepr::FileId(_) => None,
120 HirFileIdRepr::MacroFile(macro_file) => { 129 HirFileIdRepr::MacroFile(macro_file) => {
121 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); 130 let lazy_id = match macro_file.macro_call_id {
131 MacroCallId::LazyMacro(id) => id,
132 };
133 let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
122 let item = match loc.def.kind { 134 let item = match loc.def.kind {
123 MacroDefKind::BuiltInDerive(_) => loc.kind.node(db), 135 MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
124 _ => return None, 136 _ => return None,
@@ -137,16 +149,27 @@ pub struct MacroFile {
137/// `MacroCallId` identifies a particular macro invocation, like 149/// `MacroCallId` identifies a particular macro invocation, like
138/// `println!("Hello, {}", world)`. 150/// `println!("Hello, {}", world)`.
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
140pub struct MacroCallId(salsa::InternId); 152pub enum MacroCallId {
141impl salsa::InternKey for MacroCallId { 153 LazyMacro(LazyMacroId),
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
157pub struct LazyMacroId(salsa::InternId);
158impl salsa::InternKey for LazyMacroId {
142 fn from_intern_id(v: salsa::InternId) -> Self { 159 fn from_intern_id(v: salsa::InternId) -> Self {
143 MacroCallId(v) 160 LazyMacroId(v)
144 } 161 }
145 fn as_intern_id(&self) -> salsa::InternId { 162 fn as_intern_id(&self) -> salsa::InternId {
146 self.0 163 self.0
147 } 164 }
148} 165}
149 166
167impl From<LazyMacroId> for MacroCallId {
168 fn from(it: LazyMacroId) -> Self {
169 MacroCallId::LazyMacro(it)
170 }
171}
172
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
151pub struct MacroDefId { 174pub struct MacroDefId {
152 // FIXME: krate and ast_id are currently optional because we don't have a 175 // FIXME: krate and ast_id are currently optional because we don't have a
@@ -162,7 +185,7 @@ pub struct MacroDefId {
162 185
163impl MacroDefId { 186impl MacroDefId {
164 pub fn as_call_id(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> MacroCallId { 187 pub fn as_call_id(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> MacroCallId {
165 db.intern_macro(MacroCallLoc { def: self, kind }) 188 db.intern_macro(MacroCallLoc { def: self, kind }).into()
166 } 189 }
167} 190}
168 191