Simplify eager macro representation

This commit is contained in:
Jonas Schievink 2021-05-19 20:19:08 +02:00
parent c7196620ab
commit ea8555b155
10 changed files with 166 additions and 278 deletions

View file

@ -1,7 +1,7 @@
//! Builtin macro
use crate::{
db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
MacroCallLoc, MacroDefId, MacroDefKind, TextSize,
db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroCallLoc, MacroDefId,
MacroDefKind, TextSize,
};
use base_db::{AnchoredPath, Edition, FileId};
@ -27,7 +27,7 @@ macro_rules! register_builtin {
pub fn expand(
&self,
db: &dyn AstDatabase,
id: LazyMacroId,
id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
let expander = match *self {
@ -41,7 +41,7 @@ macro_rules! register_builtin {
pub fn expand(
&self,
db: &dyn AstDatabase,
arg_id: EagerMacroId,
arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let expander = match *self {
@ -128,7 +128,7 @@ register_builtin! {
fn module_path_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// Just return a dummy result.
@ -137,7 +137,7 @@ fn module_path_expand(
fn line_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// dummy implementation for type-checking purposes
@ -151,7 +151,7 @@ fn line_expand(
fn stringify_expand(
db: &dyn AstDatabase,
id: LazyMacroId,
id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
let loc = db.lookup_intern_macro(id);
@ -176,7 +176,7 @@ fn stringify_expand(
fn column_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// dummy implementation for type-checking purposes
@ -190,7 +190,7 @@ fn column_expand(
fn assert_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// A hacky implementation for goto def and hover
@ -214,7 +214,7 @@ fn assert_expand(
fn file_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// FIXME: RA purposefully lacks knowledge of absolute file names
@ -230,7 +230,7 @@ fn file_expand(
fn format_args_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// We expand `format_args!("", a1, a2)` to
@ -265,7 +265,7 @@ fn format_args_expand(
fn asm_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// both asm and llvm_asm don't return anything, so we can expand them to nothing,
@ -278,7 +278,7 @@ fn asm_expand(
fn global_asm_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// Expand to nothing (at item-level)
@ -287,7 +287,7 @@ fn global_asm_expand(
fn cfg_expand(
db: &dyn AstDatabase,
id: LazyMacroId,
id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
let loc = db.lookup_intern_macro(id);
@ -299,7 +299,7 @@ fn cfg_expand(
fn panic_expand(
db: &dyn AstDatabase,
id: LazyMacroId,
id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
let loc: MacroCallLoc = db.lookup_intern_macro(id);
@ -324,7 +324,7 @@ fn unquote_str(lit: &tt::Literal) -> Option<String> {
fn compile_error_expand(
_db: &dyn AstDatabase,
_id: EagerMacroId,
_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let err = match &*tt.token_trees {
@ -345,7 +345,7 @@ fn compile_error_expand(
fn concat_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
_arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let mut err = None;
@ -376,7 +376,7 @@ fn concat_expand(
fn concat_idents_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
_arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let mut err = None;
@ -427,7 +427,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
fn include_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let res = (|| {
@ -457,7 +457,7 @@ fn include_expand(
fn include_bytes_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
_arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
if let Err(e) = parse_string(tt) {
@ -477,7 +477,7 @@ fn include_bytes_expand(
fn include_str_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let path = match parse_string(tt) {
@ -502,14 +502,14 @@ fn include_str_expand(
ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)))
}
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
let krate = db.lookup_intern_eager_expansion(arg_id).krate;
fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
let krate = db.lookup_intern_macro(arg_id).krate;
db.crate_graph()[krate].env.get(key)
}
fn env_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let key = match parse_string(tt) {
@ -540,7 +540,7 @@ fn env_expand(
fn option_env_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
arg_id: MacroCallId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let key = match parse_string(tt) {
@ -560,7 +560,7 @@ fn option_env_expand(
mod tests {
use super::*;
use crate::{
name::AsName, test_db::TestDB, AstNode, EagerCallLoc, MacroCallId, MacroCallKind,
name::AsName, test_db::TestDB, AstNode, EagerCallInfo, MacroCallId, MacroCallKind,
MacroCallLoc,
};
use base_db::{fixture::WithFixture, SourceDatabase};
@ -599,6 +599,7 @@ mod tests {
let loc = MacroCallLoc {
def,
krate,
eager: None,
kind: MacroCallKind::FnLike {
ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
fragment: FragmentKind::Expr,
@ -620,28 +621,28 @@ mod tests {
let parsed_args = mbe::ast_to_token_tree(&args).0;
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
let arg_id = db.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
krate,
call: call_id,
let arg_id = db.intern_macro(MacroCallLoc {
def,
krate,
eager: Some(EagerCallInfo {
expansion: Arc::new(parsed_args.clone()),
included_file: None,
}
}),
kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr },
});
let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap();
let eager = EagerCallLoc {
let loc = MacroCallLoc {
def,
fragment: expanded.fragment,
subtree: Arc::new(expanded.subtree),
krate,
call: call_id,
included_file: expanded.included_file,
eager: Some(EagerCallInfo {
expansion: Arc::new(expanded.subtree),
included_file: expanded.included_file,
}),
kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment },
};
let id: MacroCallId = db.intern_eager_expansion(eager).into();
let id: MacroCallId = db.intern_macro(loc).into();
id.as_file()
}
};