mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Add macro_expansion_info in hir_expand
This commit is contained in:
parent
9fd546bec2
commit
159da285e9
5 changed files with 212 additions and 47 deletions
|
@ -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<SyntaxNode>,
|
||||
pub expansion_info: Arc<ExpansionInfo>,
|
||||
}
|
||||
|
||||
// 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<Arc<tt::Subtree>>;
|
||||
fn macro_def(&self, id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
|
||||
fn macro_arg(&self, id: MacroCallId) -> Option<(Arc<tt::Subtree>, Arc<mbe::TokenMap>)>;
|
||||
fn macro_def(&self, id: MacroDefId) -> Option<(Arc<mbe::MacroRules>, Arc<mbe::TokenMap>)>;
|
||||
fn parse_macro(&self, macro_file: MacroFile) -> Option<Parse<SyntaxNode>>;
|
||||
fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>;
|
||||
fn parse_macro_with_info(&self, macro_file: MacroFile) -> Option<ParseMacroWithInfo>;
|
||||
fn macro_expand(
|
||||
&self,
|
||||
macro_call: MacroCallId,
|
||||
) -> Result<(Arc<tt::Subtree>, (Arc<mbe::TokenMap>, Arc<mbe::TokenMap>)), String>;
|
||||
|
||||
fn macro_expansion_info(&self, macro_file: MacroFile) -> Option<Arc<ExpansionInfo>>;
|
||||
}
|
||||
|
||||
pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
|
||||
|
@ -34,10 +46,13 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdM
|
|||
Arc::new(map)
|
||||
}
|
||||
|
||||
pub(crate) fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
|
||||
pub(crate) fn macro_def(
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroDefId,
|
||||
) -> Option<(Arc<mbe::MacroRules>, Arc<mbe::TokenMap>)> {
|
||||
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<Arc<Macr
|
|||
log::warn!("fail on macro_def parse: {:#?}", tt);
|
||||
None
|
||||
})?;
|
||||
Some(Arc::new(rules))
|
||||
Some((Arc::new(rules), Arc::new(tmap)))
|
||||
}
|
||||
|
||||
pub(crate) fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<tt::Subtree>> {
|
||||
pub(crate) fn macro_arg(
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroCallId,
|
||||
) -> Option<(Arc<tt::Subtree>, Arc<mbe::TokenMap>)> {
|
||||
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<Arc<tt::Subtree>, String> {
|
||||
) -> Result<(Arc<tt::Subtree>, (Arc<mbe::TokenMap>, Arc<mbe::TokenMap>)), 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<SyntaxNode> {
|
||||
|
@ -87,6 +106,13 @@ pub(crate) fn parse_macro(
|
|||
macro_file: MacroFile,
|
||||
) -> Option<Parse<SyntaxNode>> {
|
||||
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<ParseMacroWithInfo> {
|
||||
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<Arc<ExpansionInfo>> {
|
||||
db.parse_macro_with_info(macro_file).map(|res| res.expansion_info.clone())
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue