aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand')
-rw-r--r--crates/hir_expand/src/db.rs36
-rw-r--r--crates/hir_expand/src/lib.rs2
2 files changed, 14 insertions, 24 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index fc512517c..a9099eb22 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -88,7 +88,7 @@ pub trait AstDatabase: SourceDatabase {
88 #[salsa::transparent] 88 #[salsa::transparent]
89 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>; 89 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
90 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>; 90 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
91 fn parse_macro( 91 fn parse_macro_expansion(
92 &self, 92 &self,
93 macro_file: MacroFile, 93 macro_file: MacroFile,
94 ) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>; 94 ) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
@@ -138,16 +138,13 @@ pub fn expand_hypothetical(
138 Some((node.syntax_node(), token)) 138 Some((node.syntax_node(), token))
139} 139}
140 140
141pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { 141fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
142 let map = 142 let map =
143 db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it)); 143 db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it));
144 Arc::new(map) 144 Arc::new(map)
145} 145}
146 146
147pub(crate) fn macro_def( 147fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
148 db: &dyn AstDatabase,
149 id: MacroDefId,
150) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
151 match id.kind { 148 match id.kind {
152 MacroDefKind::Declarative => { 149 MacroDefKind::Declarative => {
153 let macro_call = id.ast_id?.to_node(db); 150 let macro_call = id.ast_id?.to_node(db);
@@ -178,7 +175,7 @@ pub(crate) fn macro_def(
178 } 175 }
179} 176}
180 177
181pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> { 178fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
182 let id = match id { 179 let id = match id {
183 MacroCallId::LazyMacro(id) => id, 180 MacroCallId::LazyMacro(id) => id,
184 MacroCallId::EagerMacro(_id) => { 181 MacroCallId::EagerMacro(_id) => {
@@ -191,16 +188,13 @@ pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<Gr
191 Some(arg.green().clone()) 188 Some(arg.green().clone())
192} 189}
193 190
194pub(crate) fn macro_arg( 191fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
195 db: &dyn AstDatabase,
196 id: MacroCallId,
197) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
198 let arg = db.macro_arg_text(id)?; 192 let arg = db.macro_arg_text(id)?;
199 let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?; 193 let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?;
200 Some(Arc::new((tt, tmap))) 194 Some(Arc::new((tt, tmap)))
201} 195}
202 196
203pub(crate) fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> { 197fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> {
204 macro_expand_with_arg(db, id, None) 198 macro_expand_with_arg(db, id, None)
205} 199}
206 200
@@ -258,7 +252,7 @@ fn macro_expand_with_arg(
258 MacroResult { value: Some(Arc::new(tt)), error: err.map(|e| format!("{:?}", e)) } 252 MacroResult { value: Some(Arc::new(tt)), error: err.map(|e| format!("{:?}", e)) }
259} 253}
260 254
261pub(crate) fn expand_proc_macro( 255fn expand_proc_macro(
262 db: &dyn AstDatabase, 256 db: &dyn AstDatabase,
263 id: MacroCallId, 257 id: MacroCallId,
264) -> Result<tt::Subtree, mbe::ExpandError> { 258) -> Result<tt::Subtree, mbe::ExpandError> {
@@ -285,23 +279,23 @@ pub(crate) fn expand_proc_macro(
285 expander.expand(db, lazy_id, &macro_arg.0) 279 expander.expand(db, lazy_id, &macro_arg.0)
286} 280}
287 281
288pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> { 282fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
289 match file_id.0 { 283 match file_id.0 {
290 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), 284 HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
291 HirFileIdRepr::MacroFile(macro_file) => { 285 HirFileIdRepr::MacroFile(macro_file) => {
292 db.parse_macro(macro_file).map(|(it, _)| it.syntax_node()).value 286 db.parse_macro_expansion(macro_file).map(|(it, _)| it.syntax_node()).value
293 } 287 }
294 } 288 }
295} 289}
296 290
297pub(crate) fn parse_macro( 291fn parse_macro_expansion(
298 db: &dyn AstDatabase, 292 db: &dyn AstDatabase,
299 macro_file: MacroFile, 293 macro_file: MacroFile,
300) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> { 294) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
301 parse_macro_with_arg(db, macro_file, None) 295 parse_macro_with_arg(db, macro_file, None)
302} 296}
303 297
304pub fn parse_macro_with_arg( 298fn parse_macro_with_arg(
305 db: &dyn AstDatabase, 299 db: &dyn AstDatabase,
306 macro_file: MacroFile, 300 macro_file: MacroFile,
307 arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>, 301 arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
@@ -359,11 +353,7 @@ pub fn parse_macro_with_arg(
359 353
360 match result.error { 354 match result.error {
361 Some(error) => { 355 Some(error) => {
362 // FIXME: 356 // Safety check for recursive identity macro.
363 // In future, we should propagate the actual error with recovery information
364 // instead of ignore the error here.
365
366 // Safe check for recurisve identity macro
367 let node = parse.syntax_node(); 357 let node = parse.syntax_node();
368 let file: HirFileId = macro_file.into(); 358 let file: HirFileId = macro_file.into();
369 let call_node = match file.call_node(db) { 359 let call_node = match file.call_node(db) {
@@ -374,7 +364,7 @@ pub fn parse_macro_with_arg(
374 }; 364 };
375 365
376 if !diff(&node, &call_node.value).is_empty() { 366 if !diff(&node, &call_node.value).is_empty() {
377 MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: None } 367 MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: Some(error) }
378 } else { 368 } else {
379 return MacroResult::error(error); 369 return MacroResult::error(error);
380 } 370 }
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 9fc697d6f..83e09738b 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -144,7 +144,7 @@ impl HirFileId {
144 let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; 144 let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;
145 145
146 let macro_def = db.macro_def(loc.def)?; 146 let macro_def = db.macro_def(loc.def)?;
147 let (parse, exp_map) = db.parse_macro(macro_file).value?; 147 let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
148 let macro_arg = db.macro_arg(macro_file.macro_call_id)?; 148 let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
149 149
150 Some(ExpansionInfo { 150 Some(ExpansionInfo {