rust/crates/ra_hir_expand/src/db.rs

224 lines
7.7 KiB
Rust
Raw Normal View History

2019-10-29 08:03:29 -05:00
//! Defines database & queries for macro expansion.
use std::sync::Arc;
2019-10-29 07:11:42 -05:00
use mbe::MacroRules;
use ra_db::{salsa, SourceDatabase};
use ra_parser::FragmentKind;
2019-10-29 07:11:42 -05:00
use ra_prof::profile;
2019-12-08 02:16:52 -06:00
use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
use crate::{
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
2019-12-08 02:16:52 -06:00
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
};
2019-11-09 21:03:24 -06:00
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TokenExpander {
MacroRules(mbe::MacroRules),
Builtin(BuiltinFnLikeExpander),
BuiltinDerive(BuiltinDeriveExpander),
2019-11-09 21:03:24 -06:00
}
impl TokenExpander {
pub fn expand(
&self,
db: &dyn AstDatabase,
id: MacroCallId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
match self {
TokenExpander::MacroRules(it) => it.expand(tt),
2019-11-11 00:15:09 -06:00
TokenExpander::Builtin(it) => it.expand(db, id, tt),
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
2019-11-09 21:03:24 -06:00
}
}
pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
2019-11-09 21:03:24 -06:00
match self {
TokenExpander::MacroRules(it) => it.map_id_down(id),
TokenExpander::Builtin(..) => id,
TokenExpander::BuiltinDerive(..) => id,
}
}
pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
match self {
TokenExpander::MacroRules(it) => it.map_id_up(id),
2019-12-06 14:21:39 -06:00
TokenExpander::Builtin(..) => (id, mbe::Origin::Call),
TokenExpander::BuiltinDerive(..) => (id, mbe::Origin::Call),
2019-11-09 21:03:24 -06:00
}
}
}
2019-10-29 07:20:08 -05: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-08 22:00:46 -06:00
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
2019-11-09 21:03:24 -06:00
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
2019-11-18 07:08:41 -06:00
fn parse_macro(&self, macro_file: MacroFile)
-> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
2019-11-08 14:00:27 -06:00
fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>;
}
2019-10-29 08:12:54 -05: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 08:46:12 -06:00
pub(crate) fn macro_def(
db: &dyn AstDatabase,
id: MacroDefId,
2019-11-09 21:03:24 -06:00
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
2019-11-11 04:45:55 -06:00
match id.kind {
MacroDefKind::Declarative => {
let macro_call = id.ast_id?.to_node(db);
2019-11-09 21:03:24 -06:00
let arg = macro_call.token_tree()?;
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
log::warn!("fail on macro_def to token tree: {:#?}", arg);
None
})?;
2020-03-03 11:03:44 -06:00
let rules = match MacroRules::parse(&tt) {
Ok(it) => it,
Err(err) => {
log::warn!("fail on macro_def parse: error: {:#?} {:#?}", err, tt);
return None;
}
};
2019-11-09 21:03:24 -06:00
Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
}
2019-11-11 04:45:55 -06:00
MacroDefKind::BuiltIn(expander) => {
2019-12-20 14:14:30 -06:00
Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default())))
}
MacroDefKind::BuiltInDerive(expander) => {
Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default())))
2019-11-09 21:03:24 -06:00
}
}
2019-10-29 07:11:42 -05:00
}
2019-11-03 08:46:12 -06:00
pub(crate) fn macro_arg(
db: &dyn AstDatabase,
id: MacroCallId,
2019-11-08 22:00:46 -06:00
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
2019-10-29 07:11:42 -05:00
let loc = db.lookup_intern_macro(id);
let arg = loc.kind.arg(db)?;
let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
2019-11-08 22:00:46 -06:00
Some(Arc::new((tt, tmap)))
2019-10-29 07:11:42 -05:00
}
pub(crate) fn macro_expand(
2019-10-29 08:12:54 -05:00
db: &dyn AstDatabase,
2019-10-29 07:11:42 -05:00
id: MacroCallId,
2019-11-08 14:00:27 -06:00
) -> Result<Arc<tt::Subtree>, String> {
2019-10-29 07:11:42 -05: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-09 21:03:24 -06:00
let tt = macro_rules.0.expand(db, id, &macro_arg.0).map_err(|err| format!("{:?}", err))?;
2019-10-29 07:11:42 -05: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 14:00:27 -06:00
Ok(Arc::new(tt))
2019-10-29 07:11:42 -05:00
}
2019-10-29 08:12:54 -05:00
pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
2019-10-29 07:11:42 -05:00
match file_id.0 {
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
HirFileIdRepr::MacroFile(macro_file) => {
2019-11-08 14:00:27 -06:00
db.parse_macro(macro_file).map(|(it, _)| it.syntax_node())
2019-10-29 07:11:42 -05:00
}
}
}
pub(crate) fn parse_macro(
2019-10-29 08:12:54 -05:00
db: &dyn AstDatabase,
2019-10-29 07:11:42 -05:00
macro_file: MacroFile,
2019-11-18 07:08:41 -06:00
) -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
2019-10-29 07:11:42 -05:00
let _p = profile("parse_macro_query");
2019-11-03 08:46:12 -06:00
2019-10-29 07:11:42 -05: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.
2020-03-03 11:03:44 -06:00
let loc: MacroCallLoc = db.lookup_intern_macro(macro_call_id);
let node = loc.kind.node(db);
log::warn!("fail on macro_parse: (reason: {} macro_call: {:#})", err, node.value);
2019-10-29 07:11:42 -05:00
})
.ok()?;
2019-12-08 02:16:52 -06:00
let fragment_kind = to_fragment_kind(db, macro_call_id);
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 07:11:42 -05:00
}
2019-12-08 02:16:52 -06:00
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
/// FIXME: Not completed
fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
let parent = match syn.parent() {
Some(it) => it,
None => {
// FIXME:
// If it is root, which means the parent HirFile
// MacroKindFile must be non-items
// return expr now.
return FragmentKind::Expr;
}
};
match parent.kind() {
MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
ITEM_LIST => FragmentKind::Items,
2019-12-08 02:16:52 -06:00
LET_STMT => {
// FIXME: Handle Pattern
FragmentKind::Expr
}
// FIXME: Expand to statements in appropriate positions; HIR lowering needs to handle that
EXPR_STMT | BLOCK => FragmentKind::Expr,
2019-12-08 02:16:52 -06:00
ARG_LIST => FragmentKind::Expr,
TRY_EXPR => FragmentKind::Expr,
TUPLE_EXPR => FragmentKind::Expr,
2020-01-10 10:29:00 -06:00
PAREN_EXPR => FragmentKind::Expr,
FOR_EXPR => FragmentKind::Expr,
PATH_EXPR => FragmentKind::Expr,
LAMBDA_EXPR => FragmentKind::Expr,
CONDITION => FragmentKind::Expr,
BREAK_EXPR => FragmentKind::Expr,
RETURN_EXPR => FragmentKind::Expr,
BLOCK_EXPR => FragmentKind::Expr,
MATCH_EXPR => FragmentKind::Expr,
MATCH_ARM => FragmentKind::Expr,
MATCH_GUARD => FragmentKind::Expr,
RECORD_FIELD => FragmentKind::Expr,
CALL_EXPR => FragmentKind::Expr,
INDEX_EXPR => FragmentKind::Expr,
METHOD_CALL_EXPR => FragmentKind::Expr,
AWAIT_EXPR => FragmentKind::Expr,
CAST_EXPR => FragmentKind::Expr,
REF_EXPR => FragmentKind::Expr,
PREFIX_EXPR => FragmentKind::Expr,
RANGE_EXPR => FragmentKind::Expr,
BIN_EXPR => FragmentKind::Expr,
2019-12-08 02:16:52 -06:00
_ => {
// Unknown , Just guess it is `Items`
FragmentKind::Items
}
}
}