Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/hir/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use hir_def::db::{
};
pub use hir_expand::db::{
AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery,
MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery,
MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroExpansionQuery,
};
pub use hir_ty::db::*;

Expand Down
36 changes: 13 additions & 23 deletions crates/hir_expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub trait AstDatabase: SourceDatabase {
#[salsa::transparent]
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
fn parse_macro(
fn parse_macro_expansion(
&self,
macro_file: MacroFile,
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
Expand Down Expand Up @@ -138,16 +138,13 @@ pub fn expand_hypothetical(
Some((node.syntax_node(), token))
}

pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
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)
}

pub(crate) fn macro_def(
db: &dyn AstDatabase,
id: MacroDefId,
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
match id.kind {
MacroDefKind::Declarative => {
let macro_call = id.ast_id?.to_node(db);
Expand Down Expand Up @@ -178,7 +175,7 @@ pub(crate) fn macro_def(
}
}

pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
let id = match id {
MacroCallId::LazyMacro(id) => id,
MacroCallId::EagerMacro(_id) => {
Expand All @@ -191,16 +188,13 @@ pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<Gr
Some(arg.green().clone())
}

pub(crate) fn macro_arg(
db: &dyn AstDatabase,
id: MacroCallId,
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
let arg = db.macro_arg_text(id)?;
let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?;
Some(Arc::new((tt, tmap)))
}

pub(crate) fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> {
fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> {
macro_expand_with_arg(db, id, None)
}

Expand Down Expand Up @@ -258,7 +252,7 @@ fn macro_expand_with_arg(
MacroResult { value: Some(Arc::new(tt)), error: err.map(|e| format!("{:?}", e)) }
}

pub(crate) fn expand_proc_macro(
fn expand_proc_macro(
db: &dyn AstDatabase,
id: MacroCallId,
) -> Result<tt::Subtree, mbe::ExpandError> {
Expand All @@ -285,23 +279,23 @@ pub(crate) fn expand_proc_macro(
expander.expand(db, lazy_id, &macro_arg.0)
}

pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
match file_id.0 {
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
HirFileIdRepr::MacroFile(macro_file) => {
db.parse_macro(macro_file).map(|(it, _)| it.syntax_node()).value
db.parse_macro_expansion(macro_file).map(|(it, _)| it.syntax_node()).value
}
}
}

pub(crate) fn parse_macro(
fn parse_macro_expansion(
db: &dyn AstDatabase,
macro_file: MacroFile,
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
parse_macro_with_arg(db, macro_file, None)
}

pub fn parse_macro_with_arg(
fn parse_macro_with_arg(
db: &dyn AstDatabase,
macro_file: MacroFile,
arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
Expand Down Expand Up @@ -359,11 +353,7 @@ pub fn parse_macro_with_arg(

match result.error {
Some(error) => {
// FIXME:
// In future, we should propagate the actual error with recovery information
// instead of ignore the error here.

// Safe check for recurisve identity macro
// Safety check for recursive identity macro.
let node = parse.syntax_node();
let file: HirFileId = macro_file.into();
let call_node = match file.call_node(db) {
Expand All @@ -374,7 +364,7 @@ pub fn parse_macro_with_arg(
};

if !diff(&node, &call_node.value).is_empty() {
MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: None }
MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: Some(error) }
} else {
return MacroResult::error(error);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl HirFileId {
let def_tt = loc.def.ast_id?.to_node(db).token_tree()?;

let macro_def = db.macro_def(loc.def)?;
let (parse, exp_map) = db.parse_macro(macro_file).value?;
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
let macro_arg = db.macro_arg(macro_file.macro_call_id)?;

Some(ExpansionInfo {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
}
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
hir::db::ParseMacroQuery.in_db(db).entries::<SyntaxTreeStats>()
hir::db::ParseMacroExpansionQuery.in_db(db).entries::<SyntaxTreeStats>()
}

// Feature: Status
Expand Down
4 changes: 2 additions & 2 deletions crates/ide_db/src/apply_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl RootDatabase {
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();

base_db::ParseQuery.in_db(self).sweep(sweep);
hir::db::ParseMacroQuery.in_db(self).sweep(sweep);
hir::db::ParseMacroExpansionQuery.in_db(self).sweep(sweep);

// Macros do take significant space, but less then the syntax trees
// self.query(hir::db::MacroDefQuery).sweep(sweep);
Expand Down Expand Up @@ -143,7 +143,7 @@ impl RootDatabase {
hir::db::AstIdMapQuery
hir::db::MacroArgTextQuery
hir::db::MacroDefQuery
hir::db::ParseMacroQuery
hir::db::ParseMacroExpansionQuery
hir::db::MacroExpandQuery

// DefDatabase
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl RootDatabase {
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
hir::db::ParseMacroQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
}
}
Expand Down