make macro expansion into a proper query

This commit is contained in:
Aleksey Kladov 2019-05-04 18:01:43 +03:00
parent 87a1e276d5
commit bcf45371ff
3 changed files with 27 additions and 34 deletions

View file

@ -63,19 +63,22 @@ impl HirFileId {
match file_id.0 {
HirFileIdRepr::File(file_id) => db.parse(file_id),
HirFileIdRepr::Macro(macro_call_id) => {
parse_macro(db, macro_call_id).unwrap_or_else(|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,
macro_call_id.debug_dump(db)
);
match db.macro_expand(macro_call_id) {
Ok(tt) => mbe::token_tree_to_ast_item_list(&tt),
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,
macro_call_id.debug_dump(db)
);
// returning an empty string looks fishy...
SourceFile::parse("")
})
// returning an empty string looks fishy...
SourceFile::parse("")
}
}
}
}
}
@ -124,23 +127,21 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<
Some(Arc::new(tt))
}
fn parse_macro(
pub(crate) fn macro_expand_query(
db: &impl DefDatabase,
macro_call_id: MacroCallId,
) -> Result<TreeArc<SourceFile>, String> {
let loc = macro_call_id.loc(db);
let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?;
id: MacroCallId,
) -> Result<Arc<tt::Subtree>, String> {
let loc = id.loc(db);
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(&macro_arg).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(mbe::token_tree_to_ast_item_list(&tt))
Ok(Arc::new(tt))
}
macro_rules! impl_intern_key {