rust/crates/hir_expand/src/eager.rs

145 lines
4.9 KiB
Rust
Raw Normal View History

2020-03-02 00:05:15 -06:00
//! Eager expansion related utils
//!
//! Here is a dump of a discussion from Vadim Petrochenkov about Eager Expansion and
//! Its name resolution :
//!
//! > Eagerly expanded macros (and also macros eagerly expanded by eagerly expanded macros,
//! > which actually happens in practice too!) are resolved at the location of the "root" macro
//! > that performs the eager expansion on its arguments.
//! > If some name cannot be resolved at the eager expansion time it's considered unresolved,
//! > even if becomes available later (e.g. from a glob import or other macro).
//!
//! > Eagerly expanded macros don't add anything to the module structure of the crate and
//! > don't build any speculative module structures, i.e. they are expanded in a "flat"
//! > way even if tokens in them look like modules.
//!
//! > In other words, it kinda works for simple cases for which it was originally intended,
//! > and we need to live with it because it's available on stable and widely relied upon.
//!
//!
//! See the full discussion : https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros
2020-03-02 00:05:15 -06:00
use crate::{
ast::{self, AstNode},
db::AstDatabase,
EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
};
2020-08-13 09:25:38 -05:00
use base_db::CrateId;
2020-08-12 10:06:49 -05:00
use parser::FragmentKind;
use std::sync::Arc;
2020-08-12 11:26:51 -05:00
use syntax::{algo::SyntaxRewriter, SyntaxNode};
2020-03-02 00:05:15 -06:00
2020-03-03 11:13:20 -06:00
pub fn expand_eager_macro(
db: &dyn AstDatabase,
2020-06-11 05:08:24 -05:00
krate: CrateId,
2020-03-03 11:13:20 -06:00
macro_call: InFile<ast::MacroCall>,
def: MacroDefId,
resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<EagerMacroId> {
let args = macro_call.value.token_tree()?;
let parsed_args = mbe::ast_to_token_tree(&args)?.0;
2020-03-03 12:41:33 -06:00
// Note:
// When `lazy_expand` is called, its *parent* file must be already exists.
2020-03-03 12:57:54 -06:00
// Here we store an eager macro id for the argument expanded subtree here
2020-03-03 12:41:33 -06:00
// for that purpose.
2020-03-06 08:58:45 -06:00
let arg_id = db.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
2020-06-11 05:08:24 -05:00
krate,
2020-03-06 08:58:45 -06:00
file_id: macro_call.file_id,
}
});
let arg_file_id: MacroCallId = arg_id.into();
2020-03-03 12:41:33 -06:00
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0;
2020-03-06 08:58:45 -06:00
let result = eager_macro_recur(
db,
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
2020-06-11 05:08:24 -05:00
krate,
2020-03-06 08:58:45 -06:00
resolver,
)?;
2020-03-03 11:13:20 -06:00
let subtree = to_subtree(&result)?;
if let MacroDefKind::BuiltInEager(eager) = def.kind {
2020-03-06 08:58:45 -06:00
let (subtree, fragment) = eager.expand(db, arg_id, &subtree).ok()?;
2020-06-11 05:08:24 -05:00
let eager = EagerCallLoc {
def,
fragment,
subtree: Arc::new(subtree),
krate,
file_id: macro_call.file_id,
};
2020-03-03 11:13:20 -06:00
Some(db.intern_eager_expansion(eager))
} else {
None
}
}
2020-03-02 00:05:15 -06:00
fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> {
let mut subtree = mbe::syntax_node_to_token_tree(node)?.0;
subtree.delimiter = None;
Some(subtree)
}
fn lazy_expand(
db: &dyn AstDatabase,
2020-03-02 00:05:15 -06:00
def: &MacroDefId,
macro_call: InFile<ast::MacroCall>,
2020-06-11 05:08:24 -05:00
krate: CrateId,
2020-03-02 00:05:15 -06:00
) -> Option<InFile<SyntaxNode>> {
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
let id: MacroCallId =
2020-06-11 05:08:24 -05:00
def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();
2020-03-02 00:05:15 -06:00
db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node))
}
fn eager_macro_recur(
db: &dyn AstDatabase,
2020-03-02 00:05:15 -06:00
curr: InFile<SyntaxNode>,
2020-06-11 05:08:24 -05:00
krate: CrateId,
2020-03-02 00:05:15 -06:00
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<SyntaxNode> {
let original = curr.value.clone();
2020-03-02 00:05:15 -06:00
let children = curr.value.descendants().filter_map(ast::MacroCall::cast);
let mut rewriter = SyntaxRewriter::default();
2020-03-02 00:05:15 -06:00
// Collect replacement
for child in children {
let def: MacroDefId = macro_resolver(child.path()?)?;
let insert = match def.kind {
MacroDefKind::BuiltInEager(_) => {
2020-06-11 05:08:24 -05:00
let id: MacroCallId = expand_eager_macro(
db,
krate,
curr.with_value(child.clone()),
def,
macro_resolver,
)?
.into();
2020-03-02 00:05:15 -06:00
db.parse_or_expand(id.as_file())?
}
MacroDefKind::Declarative
| MacroDefKind::BuiltIn(_)
2020-03-18 04:47:59 -05:00
| MacroDefKind::BuiltInDerive(_)
| MacroDefKind::ProcMacro(_) => {
2020-06-11 05:08:24 -05:00
let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?;
2020-03-02 00:05:15 -06:00
// replace macro inside
2020-06-11 05:08:24 -05:00
eager_macro_recur(db, expanded, krate, macro_resolver)?
2020-03-02 00:05:15 -06:00
}
};
rewriter.replace(child.syntax(), &insert);
2020-03-02 00:05:15 -06:00
}
let res = rewriter.rewrite(&original);
Some(res)
2020-03-02 00:05:15 -06:00
}