From 159da285e9d8594a2cc4436b5cee7874b07806c9 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 3 Nov 2019 22:46:12 +0800 Subject: Add macro_expansion_info in hir_expand --- crates/ra_hir_expand/src/db.rs | 93 +++++++++++++++++++++++++++++++++-------- crates/ra_hir_expand/src/lib.rs | 35 +++++++++++++++- 2 files changed, 109 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index a4ee9a529..8abfbb4ff 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -8,10 +8,16 @@ use ra_prof::profile; use ra_syntax::{AstNode, Parse, SyntaxNode}; use crate::{ - ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, - MacroFile, MacroFileKind, + ast_id_map::AstIdMap, ExpansionInfo, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, + MacroDefId, MacroFile, MacroFileKind, }; +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct ParseMacroWithInfo { + pub parsed: Parse, + pub expansion_info: Arc, +} + // FIXME: rename to ExpandDatabase #[salsa::query_group(AstDatabaseStorage)] pub trait AstDatabase: SourceDatabase { @@ -22,10 +28,16 @@ pub trait AstDatabase: SourceDatabase { #[salsa::interned] fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; - fn macro_arg(&self, id: MacroCallId) -> Option>; - fn macro_def(&self, id: MacroDefId) -> Option>; + fn macro_arg(&self, id: MacroCallId) -> Option<(Arc, Arc)>; + fn macro_def(&self, id: MacroDefId) -> Option<(Arc, Arc)>; fn parse_macro(&self, macro_file: MacroFile) -> Option>; - fn macro_expand(&self, macro_call: MacroCallId) -> Result, String>; + fn parse_macro_with_info(&self, macro_file: MacroFile) -> Option; + fn macro_expand( + &self, + macro_call: MacroCallId, + ) -> Result<(Arc, (Arc, Arc)), String>; + + fn macro_expansion_info(&self, macro_file: MacroFile) -> Option>; } pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc { @@ -34,10 +46,13 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc Option> { +pub(crate) fn macro_def( + db: &dyn AstDatabase, + id: MacroDefId, +) -> Option<(Arc, Arc)> { let macro_call = id.ast_id.to_node(db); let arg = macro_call.token_tree()?; - let (tt, _) = mbe::ast_to_token_tree(&arg).or_else(|| { + let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { log::warn!("fail on macro_def to token tree: {:#?}", arg); None })?; @@ -45,32 +60,36 @@ pub(crate) fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option Option> { +pub(crate) fn macro_arg( + db: &dyn AstDatabase, + id: MacroCallId, +) -> Option<(Arc, Arc)> { let loc = db.lookup_intern_macro(id); let macro_call = loc.ast_id.to_node(db); let arg = macro_call.token_tree()?; - let (tt, _) = mbe::ast_to_token_tree(&arg)?; - Some(Arc::new(tt)) + let (tt, tmap) = mbe::ast_to_token_tree(&arg)?; + Some((Arc::new(tt), Arc::new(tmap))) } pub(crate) fn macro_expand( db: &dyn AstDatabase, id: MacroCallId, -) -> Result, String> { +) -> Result<(Arc, (Arc, Arc)), String> { let loc = db.lookup_intern_macro(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")?; - let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?; + let tt = macro_rules.0.expand(¯o_arg.0).map_err(|err| format!("{:?}", err))?; // Set a hard limit for the expanded tt let count = tt.count(); if count > 65536 { return Err(format!("Total tokens count exceed limit : count = {}", count)); } - Ok(Arc::new(tt)) + + Ok((Arc::new(tt), (macro_arg.1.clone(), macro_rules.1.clone()))) } pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { @@ -87,6 +106,13 @@ pub(crate) fn parse_macro( macro_file: MacroFile, ) -> Option> { let _p = profile("parse_macro_query"); + db.parse_macro_with_info(macro_file).map(|r| r.parsed) +} + +pub(crate) fn parse_macro_with_info( + db: &dyn AstDatabase, + macro_file: MacroFile, +) -> Option { let macro_call_id = macro_file.macro_call_id; let tt = db .macro_expand(macro_call_id) @@ -97,8 +123,39 @@ pub(crate) fn parse_macro( log::warn!("fail on macro_parse: (reason: {})", err,); }) .ok()?; - match macro_file.macro_file_kind { - MacroFileKind::Items => mbe::token_tree_to_items(&tt).ok().map(Parse::to_syntax), - MacroFileKind::Expr => mbe::token_tree_to_expr(&tt).ok().map(Parse::to_syntax), - } + let res = match macro_file.macro_file_kind { + MacroFileKind::Items => { + mbe::token_tree_to_items(&tt.0).ok().map(|(p, map)| (Parse::to_syntax(p), map)) + } + MacroFileKind::Expr => { + mbe::token_tree_to_expr(&tt.0).ok().map(|(p, map)| (Parse::to_syntax(p), map)) + } + }; + + res.map(|(parsed, exp_map)| { + let (arg_map, def_map) = tt.1; + let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id); + + let def_start = + loc.def.ast_id.to_node(db).token_tree().map(|t| t.syntax().text_range().start()); + let arg_start = + loc.ast_id.to_node(db).token_tree().map(|t| t.syntax().text_range().start()); + + let arg_map = + arg_start.map(|start| exp_map.ranges(&arg_map, start)).unwrap_or_else(|| Vec::new()); + + let def_map = + def_start.map(|start| exp_map.ranges(&def_map, start)).unwrap_or_else(|| Vec::new()); + + let info = ExpansionInfo { arg_map, def_map }; + + ParseMacroWithInfo { parsed, expansion_info: Arc::new(info) } + }) +} + +pub(crate) fn macro_expansion_info( + db: &dyn AstDatabase, + macro_file: MacroFile, +) -> Option> { + db.parse_macro_with_info(macro_file).map(|res| res.expansion_info.clone()) } diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index dd07a16b4..194020b45 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher}; use ra_db::{salsa, CrateId, FileId}; use ra_syntax::{ ast::{self, AstNode}, - SyntaxNode, + SyntaxNode, TextRange, }; use crate::ast_id_map::FileAstId; @@ -112,6 +112,39 @@ impl MacroCallId { } } +#[derive(Debug, Clone, PartialEq, Eq)] +/// ExpansionInfo mainly describle how to map text range between src and expaned macro +pub struct ExpansionInfo { + pub arg_map: Vec<(TextRange, TextRange)>, + pub def_map: Vec<(TextRange, TextRange)>, +} + +impl ExpansionInfo { + pub fn find_range( + &self, + from: TextRange, + (arg_file_id, def_file_id): (HirFileId, HirFileId), + ) -> Option<(HirFileId, TextRange)> { + for (src, dest) in &self.arg_map { + dbg!((src, *dest, "arg_map")); + if src.is_subrange(&from) { + dbg!((arg_file_id, *dest)); + return Some((arg_file_id, *dest)); + } + } + + for (src, dest) in &self.def_map { + dbg!((src, *dest, "def_map")); + if src.is_subrange(&from) { + dbg!((arg_file_id, *dest)); + return Some((def_file_id, *dest)); + } + } + + None + } +} + /// `AstId` points to an AST node in any file. /// /// It is stable across reparses, and can be used as salsa key/value. -- cgit v1.2.3 From ae609d7953297b355616c7862b8deefe74a8f95f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 01:44:23 +0800 Subject: Add parent_expansion to HirFileId --- crates/ra_hir_expand/src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 194020b45..180cd1f57 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -20,6 +20,7 @@ use ra_syntax::{ }; use crate::ast_id_map::FileAstId; +use std::sync::Arc; /// Input to the analyzer is a set of files, where each file is identified by /// `FileId` and contains source code. However, another source of source code in @@ -66,6 +67,24 @@ impl HirFileId { } } } + + /// Return expansion information if it is a macro-expansion file + pub fn parent_expansion( + self, + db: &dyn db::AstDatabase, + ) -> Option<((HirFileId, HirFileId), Arc)> { + match self.0 { + HirFileIdRepr::FileId(_) => None, + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + + let def_file = loc.def.ast_id.file_id; + let arg_file = loc.ast_id.file_id; + + db.macro_expansion_info(macro_file).map(|ex| ((arg_file, def_file), ex)) + } + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3 From 67226ebc8288b9489867ea2454f9d976b65ff15e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 01:47:59 +0800 Subject: Remove dbg! --- crates/ra_hir_expand/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 180cd1f57..b6e55aa6a 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -145,17 +145,13 @@ impl ExpansionInfo { (arg_file_id, def_file_id): (HirFileId, HirFileId), ) -> Option<(HirFileId, TextRange)> { for (src, dest) in &self.arg_map { - dbg!((src, *dest, "arg_map")); if src.is_subrange(&from) { - dbg!((arg_file_id, *dest)); return Some((arg_file_id, *dest)); } } for (src, dest) in &self.def_map { - dbg!((src, *dest, "def_map")); if src.is_subrange(&from) { - dbg!((arg_file_id, *dest)); return Some((def_file_id, *dest)); } } -- cgit v1.2.3 From e6709f64af1836460aee41aca34eb19ed3a337dd Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 03:12:19 +0800 Subject: Rename and fix typos --- crates/ra_hir_expand/src/db.rs | 28 +++++++++++++++++++--------- crates/ra_hir_expand/src/lib.rs | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 8abfbb4ff..b3746924d 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -18,6 +18,12 @@ pub struct ParseMacroWithInfo { pub expansion_info: Arc, } +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct MacroExpandInfo { + pub arg_map: Arc, + pub def_map: Arc, +} + // FIXME: rename to ExpandDatabase #[salsa::query_group(AstDatabaseStorage)] pub trait AstDatabase: SourceDatabase { @@ -35,7 +41,7 @@ pub trait AstDatabase: SourceDatabase { fn macro_expand( &self, macro_call: MacroCallId, - ) -> Result<(Arc, (Arc, Arc)), String>; + ) -> Result<(Arc, MacroExpandInfo), String>; fn macro_expansion_info(&self, macro_file: MacroFile) -> Option>; } @@ -77,7 +83,7 @@ pub(crate) fn macro_arg( pub(crate) fn macro_expand( db: &dyn AstDatabase, id: MacroCallId, -) -> Result<(Arc, (Arc, Arc)), String> { +) -> Result<(Arc, MacroExpandInfo), String> { let loc = db.lookup_intern_macro(id); let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; @@ -89,7 +95,10 @@ pub(crate) fn macro_expand( return Err(format!("Total tokens count exceed limit : count = {}", count)); } - Ok((Arc::new(tt), (macro_arg.1.clone(), macro_rules.1.clone()))) + Ok(( + Arc::new(tt), + MacroExpandInfo { arg_map: macro_arg.1.clone(), def_map: macro_rules.1.clone() }, + )) } pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { @@ -133,7 +142,7 @@ pub(crate) fn parse_macro_with_info( }; res.map(|(parsed, exp_map)| { - let (arg_map, def_map) = tt.1; + let expand_info = tt.1; let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id); let def_start = @@ -141,11 +150,12 @@ pub(crate) fn parse_macro_with_info( let arg_start = loc.ast_id.to_node(db).token_tree().map(|t| t.syntax().text_range().start()); - let arg_map = - arg_start.map(|start| exp_map.ranges(&arg_map, start)).unwrap_or_else(|| Vec::new()); - - let def_map = - def_start.map(|start| exp_map.ranges(&def_map, start)).unwrap_or_else(|| Vec::new()); + let arg_map = arg_start + .map(|start| exp_map.ranges(&expand_info.arg_map, start)) + .unwrap_or_else(|| Vec::new()); + let def_map = def_start + .map(|start| exp_map.ranges(&expand_info.def_map, start)) + .unwrap_or_else(|| Vec::new()); let info = ExpansionInfo { arg_map, def_map }; diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index b6e55aa6a..63ba17158 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -132,7 +132,7 @@ impl MacroCallId { } #[derive(Debug, Clone, PartialEq, Eq)] -/// ExpansionInfo mainly describle how to map text range between src and expaned macro +/// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { pub arg_map: Vec<(TextRange, TextRange)>, pub def_map: Vec<(TextRange, TextRange)>, -- cgit v1.2.3 From 7e28924012409352bbacbfebf9fac4e4409f09b8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 21:43:36 +0800 Subject: Use ? and destructing to simplifed long code --- crates/ra_hir_expand/src/db.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index b3746924d..8369c2b40 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -132,35 +132,33 @@ pub(crate) fn parse_macro_with_info( log::warn!("fail on macro_parse: (reason: {})", err,); }) .ok()?; - let res = match macro_file.macro_file_kind { + + let (parsed, exp_map) = match macro_file.macro_file_kind { MacroFileKind::Items => { - mbe::token_tree_to_items(&tt.0).ok().map(|(p, map)| (Parse::to_syntax(p), map)) + mbe::token_tree_to_items(&tt.0).map(|(p, map)| (p.to_syntax(), map)).ok()? } MacroFileKind::Expr => { - mbe::token_tree_to_expr(&tt.0).ok().map(|(p, map)| (Parse::to_syntax(p), map)) + mbe::token_tree_to_expr(&tt.0).map(|(p, map)| (p.to_syntax(), map)).ok()? } }; - res.map(|(parsed, exp_map)| { - let expand_info = tt.1; - let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id); + let expand_info = tt.1; + let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id); + + let arg_tt = loc.ast_id.to_node(db).token_tree(); + let def_tt = loc.def.ast_id.to_node(db).token_tree(); - let def_start = - loc.def.ast_id.to_node(db).token_tree().map(|t| t.syntax().text_range().start()); - let arg_start = - loc.ast_id.to_node(db).token_tree().map(|t| t.syntax().text_range().start()); + let arg_start = arg_tt.map(|t| t.syntax().text_range().start()); + let def_start = def_tt.map(|t| t.syntax().text_range().start()); - let arg_map = arg_start - .map(|start| exp_map.ranges(&expand_info.arg_map, start)) - .unwrap_or_else(|| Vec::new()); - let def_map = def_start - .map(|start| exp_map.ranges(&expand_info.def_map, start)) - .unwrap_or_else(|| Vec::new()); + let arg_map = + arg_start.map(|start| exp_map.ranges(&expand_info.arg_map, start)).unwrap_or_default(); + let def_map = + def_start.map(|start| exp_map.ranges(&expand_info.def_map, start)).unwrap_or_default(); - let info = ExpansionInfo { arg_map, def_map }; + let info = ExpansionInfo { arg_map, def_map }; - ParseMacroWithInfo { parsed, expansion_info: Arc::new(info) } - }) + Some(ParseMacroWithInfo { parsed, expansion_info: Arc::new(info) }) } pub(crate) fn macro_expansion_info( -- cgit v1.2.3 From 604bdc6ffeea69dffed6d03f10b2fd4e05d04cda Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 02:09:16 +0800 Subject: Use macro_rules shift to map text ranges --- crates/ra_hir_expand/src/db.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 8369c2b40..72c728c93 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -151,10 +151,15 @@ pub(crate) fn parse_macro_with_info( let arg_start = arg_tt.map(|t| t.syntax().text_range().start()); let def_start = def_tt.map(|t| t.syntax().text_range().start()); - let arg_map = - arg_start.map(|start| exp_map.ranges(&expand_info.arg_map, start)).unwrap_or_default(); - let def_map = - def_start.map(|start| exp_map.ranges(&expand_info.def_map, start)).unwrap_or_default(); + let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition").ok()?.0; + let shift = macro_rules.shift(); + + let arg_map = arg_start + .map(|start| exp_map.map_ranges(&expand_info.arg_map, start, shift)) + .unwrap_or_default(); + let def_map = def_start + .map(|start| exp_map.map_ranges(&expand_info.def_map, start, 0)) + .unwrap_or_default(); let info = ExpansionInfo { arg_map, def_map }; -- cgit v1.2.3 From 2a76bb5a0b70ae83824aa2e094ae2f592dcee694 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Nov 2019 03:20:54 +0800 Subject: Refactor a bit --- crates/ra_hir_expand/src/db.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 72c728c93..e2860371f 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -151,8 +151,7 @@ pub(crate) fn parse_macro_with_info( let arg_start = arg_tt.map(|t| t.syntax().text_range().start()); let def_start = def_tt.map(|t| t.syntax().text_range().start()); - let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition").ok()?.0; - let shift = macro_rules.shift(); + let shift = db.macro_def(loc.def)?.0.shift(); let arg_map = arg_start .map(|start| exp_map.map_ranges(&expand_info.arg_map, start, shift)) -- cgit v1.2.3 From 62ed93db13877888379ea38c843729419bdf99d0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 8 Nov 2019 09:57:01 +0800 Subject: Refactor and add more comments --- crates/ra_hir_expand/src/db.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index e2860371f..1eaf8bf30 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -148,17 +148,15 @@ pub(crate) fn parse_macro_with_info( let arg_tt = loc.ast_id.to_node(db).token_tree(); let def_tt = loc.def.ast_id.to_node(db).token_tree(); - let arg_start = arg_tt.map(|t| t.syntax().text_range().start()); - let def_start = def_tt.map(|t| t.syntax().text_range().start()); + let arg_range = arg_tt.map(|t| t.syntax().text_range()); + let def_range = def_tt.map(|t| t.syntax().text_range()); let shift = db.macro_def(loc.def)?.0.shift(); - let arg_map = arg_start - .map(|start| exp_map.map_ranges(&expand_info.arg_map, start, shift)) - .unwrap_or_default(); - let def_map = def_start - .map(|start| exp_map.map_ranges(&expand_info.def_map, start, 0)) - .unwrap_or_default(); + let arg_map = + arg_range.map(|it| exp_map.map_ranges(&expand_info.arg_map, it, shift)).unwrap_or_default(); + let def_map = + def_range.map(|it| exp_map.map_ranges(&expand_info.def_map, it, 0)).unwrap_or_default(); let info = ExpansionInfo { arg_map, def_map }; -- cgit v1.2.3 From d01e0abdb57c5be340934cb51e2f6b1a2e6c0373 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 9 Nov 2019 04:00:27 +0800 Subject: Refactor and simpfily --- crates/ra_hir_expand/src/db.rs | 80 ++++++++--------------------------------- crates/ra_hir_expand/src/lib.rs | 40 ++++++++++++--------- 2 files changed, 38 insertions(+), 82 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 1eaf8bf30..e6d2e8d9d 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -8,22 +8,10 @@ use ra_prof::profile; use ra_syntax::{AstNode, Parse, SyntaxNode}; use crate::{ - ast_id_map::AstIdMap, ExpansionInfo, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, - MacroDefId, MacroFile, MacroFileKind, + ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, + MacroFile, MacroFileKind, }; -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct ParseMacroWithInfo { - pub parsed: Parse, - pub expansion_info: Arc, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct MacroExpandInfo { - pub arg_map: Arc, - pub def_map: Arc, -} - // FIXME: rename to ExpandDatabase #[salsa::query_group(AstDatabaseStorage)] pub trait AstDatabase: SourceDatabase { @@ -36,14 +24,11 @@ pub trait AstDatabase: SourceDatabase { fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; fn macro_arg(&self, id: MacroCallId) -> Option<(Arc, Arc)>; fn macro_def(&self, id: MacroDefId) -> Option<(Arc, Arc)>; - fn parse_macro(&self, macro_file: MacroFile) -> Option>; - fn parse_macro_with_info(&self, macro_file: MacroFile) -> Option; - fn macro_expand( + fn parse_macro( &self, - macro_call: MacroCallId, - ) -> Result<(Arc, MacroExpandInfo), String>; - - fn macro_expansion_info(&self, macro_file: MacroFile) -> Option>; + macro_file: MacroFile, + ) -> Option<(Parse, Arc)>; + fn macro_expand(&self, macro_call: MacroCallId) -> Result, String>; } pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc { @@ -83,7 +68,7 @@ pub(crate) fn macro_arg( pub(crate) fn macro_expand( db: &dyn AstDatabase, id: MacroCallId, -) -> Result<(Arc, MacroExpandInfo), String> { +) -> Result, String> { let loc = db.lookup_intern_macro(id); let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; @@ -94,18 +79,14 @@ pub(crate) fn macro_expand( if count > 65536 { return Err(format!("Total tokens count exceed limit : count = {}", count)); } - - Ok(( - Arc::new(tt), - MacroExpandInfo { arg_map: macro_arg.1.clone(), def_map: macro_rules.1.clone() }, - )) + Ok(Arc::new(tt)) } pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option { match file_id.0 { HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), HirFileIdRepr::MacroFile(macro_file) => { - db.parse_macro(macro_file).map(|it| it.syntax_node()) + db.parse_macro(macro_file).map(|(it, _)| it.syntax_node()) } } } @@ -113,15 +94,9 @@ pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Optio pub(crate) fn parse_macro( db: &dyn AstDatabase, macro_file: MacroFile, -) -> Option> { +) -> Option<(Parse, Arc)> { let _p = profile("parse_macro_query"); - db.parse_macro_with_info(macro_file).map(|r| r.parsed) -} -pub(crate) fn parse_macro_with_info( - db: &dyn AstDatabase, - macro_file: MacroFile, -) -> Option { let macro_call_id = macro_file.macro_call_id; let tt = db .macro_expand(macro_call_id) @@ -133,39 +108,12 @@ pub(crate) fn parse_macro_with_info( }) .ok()?; - let (parsed, exp_map) = match macro_file.macro_file_kind { + match macro_file.macro_file_kind { MacroFileKind::Items => { - mbe::token_tree_to_items(&tt.0).map(|(p, map)| (p.to_syntax(), map)).ok()? + mbe::token_tree_to_items(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) } MacroFileKind::Expr => { - mbe::token_tree_to_expr(&tt.0).map(|(p, map)| (p.to_syntax(), map)).ok()? + mbe::token_tree_to_expr(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) } - }; - - let expand_info = tt.1; - let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id); - - let arg_tt = loc.ast_id.to_node(db).token_tree(); - let def_tt = loc.def.ast_id.to_node(db).token_tree(); - - let arg_range = arg_tt.map(|t| t.syntax().text_range()); - let def_range = def_tt.map(|t| t.syntax().text_range()); - - let shift = db.macro_def(loc.def)?.0.shift(); - - let arg_map = - arg_range.map(|it| exp_map.map_ranges(&expand_info.arg_map, it, shift)).unwrap_or_default(); - let def_map = - def_range.map(|it| exp_map.map_ranges(&expand_info.def_map, it, 0)).unwrap_or_default(); - - let info = ExpansionInfo { arg_map, def_map }; - - Some(ParseMacroWithInfo { parsed, expansion_info: Arc::new(info) }) -} - -pub(crate) fn macro_expansion_info( - db: &dyn AstDatabase, - macro_file: MacroFile, -) -> Option> { - db.parse_macro_with_info(macro_file).map(|res| res.expansion_info.clone()) + } } diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 63ba17158..1a1d6bdd4 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -20,7 +20,6 @@ use ra_syntax::{ }; use crate::ast_id_map::FileAstId; -use std::sync::Arc; /// Input to the analyzer is a set of files, where each file is identified by /// `FileId` and contains source code. However, another source of source code in @@ -69,19 +68,29 @@ impl HirFileId { } /// Return expansion information if it is a macro-expansion file - pub fn parent_expansion( - self, - db: &dyn db::AstDatabase, - ) -> Option<((HirFileId, HirFileId), Arc)> { + pub fn parent_expansion(self, db: &dyn db::AstDatabase) -> Option { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); - let def_file = loc.def.ast_id.file_id; + let arg_range = loc.ast_id.to_node(db).token_tree()?.syntax().text_range(); + let def_range = loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range(); + + let macro_def = db.macro_def(loc.def)?; + let shift = macro_def.0.shift(); + let rev_map = db.parse_macro(macro_file)?.1; + + let arg_token_map = db.macro_arg(macro_file.macro_call_id)?.1; + let def_token_map = macro_def.1; + + let arg_map = rev_map.map_ranges(&arg_token_map, arg_range, shift); + let def_map = rev_map.map_ranges(&def_token_map, def_range, 0); + let arg_file = loc.ast_id.file_id; + let def_file = loc.def.ast_id.file_id; - db.macro_expansion_info(macro_file).map(|ex| ((arg_file, def_file), ex)) + Some(ExpansionInfo { arg_file, def_file, arg_map, def_map }) } } } @@ -134,25 +143,24 @@ impl MacroCallId { #[derive(Debug, Clone, PartialEq, Eq)] /// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { - pub arg_map: Vec<(TextRange, TextRange)>, - pub def_map: Vec<(TextRange, TextRange)>, + pub(crate) arg_file: HirFileId, + pub(crate) def_file: HirFileId, + + pub(crate) arg_map: Vec<(TextRange, TextRange)>, + pub(crate) def_map: Vec<(TextRange, TextRange)>, } impl ExpansionInfo { - pub fn find_range( - &self, - from: TextRange, - (arg_file_id, def_file_id): (HirFileId, HirFileId), - ) -> Option<(HirFileId, TextRange)> { + pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { for (src, dest) in &self.arg_map { if src.is_subrange(&from) { - return Some((arg_file_id, *dest)); + return Some((self.arg_file, *dest)); } } for (src, dest) in &self.def_map { if src.is_subrange(&from) { - return Some((def_file_id, *dest)); + return Some((self.def_file, *dest)); } } -- cgit v1.2.3 From 0a5ec69404a2556dd82e5bb00b295aebaa291f04 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 9 Nov 2019 12:00:46 +0800 Subject: Remove map_ranges in RevTokenMap --- crates/ra_hir_expand/src/db.rs | 12 ++++----- crates/ra_hir_expand/src/lib.rs | 55 ++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index e6d2e8d9d..b789c6e7b 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -22,8 +22,8 @@ pub trait AstDatabase: SourceDatabase { #[salsa::interned] fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; - fn macro_arg(&self, id: MacroCallId) -> Option<(Arc, Arc)>; - fn macro_def(&self, id: MacroDefId) -> Option<(Arc, Arc)>; + fn macro_arg(&self, id: MacroCallId) -> Option>; + fn macro_def(&self, id: MacroDefId) -> Option>; fn parse_macro( &self, macro_file: MacroFile, @@ -40,7 +40,7 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc Option<(Arc, Arc)> { +) -> Option> { let macro_call = id.ast_id.to_node(db); let arg = macro_call.token_tree()?; let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { @@ -51,18 +51,18 @@ pub(crate) fn macro_def( log::warn!("fail on macro_def parse: {:#?}", tt); None })?; - Some((Arc::new(rules), Arc::new(tmap))) + Some(Arc::new((rules, tmap))) } pub(crate) fn macro_arg( db: &dyn AstDatabase, id: MacroCallId, -) -> Option<(Arc, Arc)> { +) -> Option> { let loc = db.lookup_intern_macro(id); let macro_call = loc.ast_id.to_node(db); let arg = macro_call.token_tree()?; let (tt, tmap) = mbe::ast_to_token_tree(&arg)?; - Some((Arc::new(tt), Arc::new(tmap))) + Some(Arc::new((tt, tmap))) } pub(crate) fn macro_expand( diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 1a1d6bdd4..b219b8fbf 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -12,11 +12,12 @@ pub mod hygiene; pub mod diagnostics; use std::hash::{Hash, Hasher}; +use std::sync::Arc; use ra_db::{salsa, CrateId, FileId}; use ra_syntax::{ ast::{self, AstNode}, - SyntaxNode, TextRange, + SyntaxNode, TextRange, TextUnit, }; use crate::ast_id_map::FileAstId; @@ -68,29 +69,25 @@ impl HirFileId { } /// Return expansion information if it is a macro-expansion file - pub fn parent_expansion(self, db: &dyn db::AstDatabase) -> Option { + pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); - let arg_range = loc.ast_id.to_node(db).token_tree()?.syntax().text_range(); - let def_range = loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range(); + let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); + let def_start = + loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let macro_def = db.macro_def(loc.def)?; let shift = macro_def.0.shift(); - let rev_map = db.parse_macro(macro_file)?.1; + let exp_map = db.parse_macro(macro_file)?.1; + let macro_arg = db.macro_arg(macro_file.macro_call_id)?; - let arg_token_map = db.macro_arg(macro_file.macro_call_id)?.1; - let def_token_map = macro_def.1; + let arg_start = (loc.ast_id.file_id, arg_start); + let def_start = (loc.def.ast_id.file_id, def_start); - let arg_map = rev_map.map_ranges(&arg_token_map, arg_range, shift); - let def_map = rev_map.map_ranges(&def_token_map, def_range, 0); - - let arg_file = loc.ast_id.file_id; - let def_file = loc.def.ast_id.file_id; - - Some(ExpansionInfo { arg_file, def_file, arg_map, def_map }) + Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) } } } @@ -143,28 +140,30 @@ impl MacroCallId { #[derive(Debug, Clone, PartialEq, Eq)] /// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { - pub(crate) arg_file: HirFileId, - pub(crate) def_file: HirFileId, + pub(crate) arg_start: (HirFileId, TextUnit), + pub(crate) def_start: (HirFileId, TextUnit), + pub(crate) shift: u32, - pub(crate) arg_map: Vec<(TextRange, TextRange)>, - pub(crate) def_map: Vec<(TextRange, TextRange)>, + pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, + pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, + pub(crate) exp_map: Arc, } impl ExpansionInfo { pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { - for (src, dest) in &self.arg_map { - if src.is_subrange(&from) { - return Some((self.arg_file, *dest)); - } + fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option { + exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1) } - for (src, dest) in &self.def_map { - if src.is_subrange(&from) { - return Some((self.def_file, *dest)); - } - } + let token_id = look_in_rev_map(&self.exp_map, from)?; + let (token_map, file_offset, token_id) = if token_id.0 >= self.shift { + (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into()) + } else { + (&self.macro_def.1, self.def_start, token_id) + }; - None + let range = token_map.relative_range_of(token_id)?; + Some((file_offset.0, TextRange::offset_len(range.start() + file_offset.1, range.len()))) } } -- cgit v1.2.3