rust-analyzer/crates/ra_hir_expand/src/db.rs

119 lines
4 KiB
Rust
Raw Normal View History

2019-10-29 13:03:29 +00:00
//! Defines database & queries for macro expansion.
use std::sync::Arc;
2019-10-29 12:11:42 +00:00
use mbe::MacroRules;
use ra_db::{salsa, SourceDatabase};
use ra_parser::FragmentKind;
2019-10-29 12:11:42 +00:00
use ra_prof::profile;
use ra_syntax::{AstNode, Parse, SyntaxNode};
use crate::{
2019-11-08 20:00:27 +00:00
ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId,
MacroFile, MacroFileKind,
};
2019-10-29 12:20:08 +00:00
// FIXME: rename to ExpandDatabase
#[salsa::query_group(AstDatabaseStorage)]
pub trait AstDatabase: SourceDatabase {
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
#[salsa::transparent]
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
#[salsa::interned]
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
2019-11-09 04:00:46 +00:00
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>>;
2019-11-08 20:00:27 +00:00
fn parse_macro(
2019-11-03 14:46:12 +00:00
&self,
2019-11-08 20:00:27 +00:00
macro_file: MacroFile,
) -> Option<(Parse<SyntaxNode>, Arc<mbe::RevTokenMap>)>;
fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>;
}
2019-10-29 13:12:54 +00:00
pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let map =
db.parse_or_expand(file_id).map_or_else(AstIdMap::default, |it| AstIdMap::from_source(&it));
Arc::new(map)
}
2019-11-03 14:46:12 +00:00
pub(crate) fn macro_def(
db: &dyn AstDatabase,
id: MacroDefId,
2019-11-09 04:00:46 +00:00
) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> {
2019-10-29 12:11:42 +00:00
let macro_call = id.ast_id.to_node(db);
let arg = macro_call.token_tree()?;
2019-11-03 14:46:12 +00:00
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
2019-10-29 12:11:42 +00:00
log::warn!("fail on macro_def to token tree: {:#?}", arg);
None
})?;
let rules = MacroRules::parse(&tt).ok().or_else(|| {
log::warn!("fail on macro_def parse: {:#?}", tt);
None
})?;
2019-11-09 04:00:46 +00:00
Some(Arc::new((rules, tmap)))
2019-10-29 12:11:42 +00:00
}
2019-11-03 14:46:12 +00:00
pub(crate) fn macro_arg(
db: &dyn AstDatabase,
id: MacroCallId,
2019-11-09 04:00:46 +00:00
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
2019-10-29 12:11:42 +00:00
let loc = db.lookup_intern_macro(id);
let macro_call = loc.ast_id.to_node(db);
let arg = macro_call.token_tree()?;
2019-11-03 14:46:12 +00:00
let (tt, tmap) = mbe::ast_to_token_tree(&arg)?;
2019-11-09 04:00:46 +00:00
Some(Arc::new((tt, tmap)))
2019-10-29 12:11:42 +00:00
}
pub(crate) fn macro_expand(
2019-10-29 13:12:54 +00:00
db: &dyn AstDatabase,
2019-10-29 12:11:42 +00:00
id: MacroCallId,
2019-11-08 20:00:27 +00:00
) -> Result<Arc<tt::Subtree>, String> {
2019-10-29 12:11:42 +00:00
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")?;
2019-11-03 14:46:12 +00:00
let tt = macro_rules.0.expand(&macro_arg.0).map_err(|err| format!("{:?}", err))?;
2019-10-29 12:11:42 +00:00
// 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));
}
2019-11-08 20:00:27 +00:00
Ok(Arc::new(tt))
2019-10-29 12:11:42 +00:00
}
2019-10-29 13:12:54 +00:00
pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
2019-10-29 12:11:42 +00:00
match file_id.0 {
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
HirFileIdRepr::MacroFile(macro_file) => {
2019-11-08 20:00:27 +00:00
db.parse_macro(macro_file).map(|(it, _)| it.syntax_node())
2019-10-29 12:11:42 +00:00
}
}
}
pub(crate) fn parse_macro(
2019-10-29 13:12:54 +00:00
db: &dyn AstDatabase,
2019-10-29 12:11:42 +00:00
macro_file: MacroFile,
2019-11-08 20:00:27 +00:00
) -> Option<(Parse<SyntaxNode>, Arc<mbe::RevTokenMap>)> {
2019-10-29 12:11:42 +00:00
let _p = profile("parse_macro_query");
2019-11-03 14:46:12 +00:00
2019-10-29 12:11:42 +00:00
let macro_call_id = macro_file.macro_call_id;
let tt = db
.macro_expand(macro_call_id)
.map_err(|err| {
// Note:
// The final goal we would like to make all parse_macro success,
// such that the following log will not call anyway.
log::warn!("fail on macro_parse: (reason: {})", err,);
})
.ok()?;
let fragment_kind = match macro_file.macro_file_kind {
MacroFileKind::Items => FragmentKind::Items,
MacroFileKind::Expr => FragmentKind::Expr,
};
let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
Some((parse, Arc::new(rev_token_map)))
2019-10-29 12:11:42 +00:00
}