mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-29 08:54:50 +00:00
make macro expansion into a proper query
This commit is contained in:
parent
87a1e276d5
commit
bcf45371ff
@ -45,6 +45,9 @@ pub trait DefDatabase: SourceDatabase {
|
|||||||
#[salsa::invoke(crate::ids::macro_arg_query)]
|
#[salsa::invoke(crate::ids::macro_arg_query)]
|
||||||
fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
|
fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
|
||||||
|
|
||||||
|
#[salsa::invoke(crate::ids::macro_expand_query)]
|
||||||
|
fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result<Arc<tt::Subtree>, String>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::ids::HirFileId::hir_parse_query)]
|
#[salsa::invoke(crate::ids::HirFileId::hir_parse_query)]
|
||||||
fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
|
fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
|
||||||
|
|
||||||
|
@ -5,14 +5,13 @@ use rustc_hash::FxHashMap;
|
|||||||
|
|
||||||
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
|
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
SyntaxNodePtr, AstPtr, AstNode,TreeArc,
|
SyntaxNodePtr, AstPtr, AstNode,
|
||||||
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}
|
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId,
|
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId,
|
||||||
name::AsName,
|
name::AsName,
|
||||||
ids::{MacroCallId},
|
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
};
|
};
|
||||||
use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}};
|
use crate::{path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}};
|
||||||
@ -826,8 +825,8 @@ where
|
|||||||
.with_file_id(self.current_file_id);
|
.with_file_id(self.current_file_id);
|
||||||
|
|
||||||
if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) {
|
if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) {
|
||||||
if let Some(arg) = self.db.macro_arg(call_id) {
|
if let Some(tt) = self.db.macro_expand(call_id).ok() {
|
||||||
if let Some(expr) = expand_macro_to_expr(self.db, call_id, &arg) {
|
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
|
||||||
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
||||||
let old_file_id =
|
let old_file_id =
|
||||||
std::mem::replace(&mut self.current_file_id, call_id.into());
|
std::mem::replace(&mut self.current_file_id, call_id.into());
|
||||||
@ -998,16 +997,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_macro_to_expr(
|
|
||||||
db: &impl HirDatabase,
|
|
||||||
macro_call: MacroCallId,
|
|
||||||
arg: &tt::Subtree,
|
|
||||||
) -> Option<TreeArc<ast::Expr>> {
|
|
||||||
let rules = db.macro_def(macro_call.loc(db).def)?;
|
|
||||||
let expanded = rules.expand(&arg).ok()?;
|
|
||||||
mbe::token_tree_to_expr(&expanded).ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn body_with_source_map_query(
|
pub(crate) fn body_with_source_map_query(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
def: DefWithBody,
|
def: DefWithBody,
|
||||||
|
@ -63,7 +63,9 @@ impl HirFileId {
|
|||||||
match file_id.0 {
|
match file_id.0 {
|
||||||
HirFileIdRepr::File(file_id) => db.parse(file_id),
|
HirFileIdRepr::File(file_id) => db.parse(file_id),
|
||||||
HirFileIdRepr::Macro(macro_call_id) => {
|
HirFileIdRepr::Macro(macro_call_id) => {
|
||||||
parse_macro(db, macro_call_id).unwrap_or_else(|err| {
|
match db.macro_expand(macro_call_id) {
|
||||||
|
Ok(tt) => mbe::token_tree_to_ast_item_list(&tt),
|
||||||
|
Err(err) => {
|
||||||
// Note:
|
// Note:
|
||||||
// The final goal we would like to make all parse_macro success,
|
// The final goal we would like to make all parse_macro success,
|
||||||
// such that the following log will not call anyway.
|
// such that the following log will not call anyway.
|
||||||
@ -75,7 +77,8 @@ impl HirFileId {
|
|||||||
|
|
||||||
// returning an empty string looks fishy...
|
// returning an empty string looks fishy...
|
||||||
SourceFile::parse("")
|
SourceFile::parse("")
|
||||||
})
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,23 +127,21 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<
|
|||||||
Some(Arc::new(tt))
|
Some(Arc::new(tt))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_macro(
|
pub(crate) fn macro_expand_query(
|
||||||
db: &impl DefDatabase,
|
db: &impl DefDatabase,
|
||||||
macro_call_id: MacroCallId,
|
id: MacroCallId,
|
||||||
) -> Result<TreeArc<SourceFile>, String> {
|
) -> Result<Arc<tt::Subtree>, String> {
|
||||||
let loc = macro_call_id.loc(db);
|
let loc = id.loc(db);
|
||||||
let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?;
|
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 macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
|
||||||
let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?;
|
let tt = macro_rules.expand(¯o_arg).map_err(|err| format!("{:?}", err))?;
|
||||||
|
|
||||||
// Set a hard limit for the expanded tt
|
// Set a hard limit for the expanded tt
|
||||||
let count = tt.count();
|
let count = tt.count();
|
||||||
if count > 65536 {
|
if count > 65536 {
|
||||||
return Err(format!("Total tokens count exceed limit : count = {}", count));
|
return Err(format!("Total tokens count exceed limit : count = {}", count));
|
||||||
}
|
}
|
||||||
|
Ok(Arc::new(tt))
|
||||||
Ok(mbe::token_tree_to_ast_item_list(&tt))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_intern_key {
|
macro_rules! impl_intern_key {
|
||||||
|
Loading…
Reference in New Issue
Block a user