Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/db.rs')
| -rw-r--r-- | crates/hir-expand/src/db.rs | 95 |
1 files changed, 39 insertions, 56 deletions
diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 2fe251d298..29b7b33fd0 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -4,19 +4,15 @@ use base_db::{Crate, RootQueryDb}; use either::Either; use mbe::MatchedArmIndex; use rustc_hash::FxHashSet; -use salsa::plumbing::AsId; -use span::{ - AstIdMap, Edition, EditionedFileId, HirFileId, HirFileIdRepr, MacroCallId, MacroFileId, Span, - SyntaxContext, -}; +use span::{AstIdMap, Edition, Span, SyntaxContext}; use syntax::{AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T, ast}; use syntax_bridge::{DocCommentDesugarMode, syntax_node_to_token_tree}; use triomphe::Arc; use crate::{ AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallInfo, - EagerExpander, ExpandError, ExpandResult, ExpandTo, MacroCallKind, MacroCallLoc, MacroDefId, - MacroDefKind, + EagerExpander, EditionedFileId, ExpandError, ExpandResult, ExpandTo, HirFileId, MacroCallId, + MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, attrs::{AttrId, collect_attrs}, builtin::pseudo_derive_attr_expansion, cfg_process, @@ -63,9 +59,10 @@ pub trait ExpandDatabase: RootQueryDb { fn proc_macros(&self) -> Arc<ProcMacros>; /// Incrementality query to prevent queries from directly depending on `ExpandDatabase::proc_macros`. - #[salsa::invoke_actual(crate::proc_macro::proc_macros_for_crate)] + #[salsa::invoke(crate::proc_macro::proc_macros_for_crate)] fn proc_macros_for_crate(&self, krate: Crate) -> Option<Arc<CrateProcMacros>>; + #[salsa::invoke(ast_id_map)] fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>; #[salsa::transparent] @@ -75,7 +72,7 @@ pub trait ExpandDatabase: RootQueryDb { #[salsa::lru(512)] fn parse_macro_expansion( &self, - macro_file: span::MacroFileId, + macro_file: MacroCallId, ) -> ExpandResult<(Parse<SyntaxNode>, Arc<ExpansionSpanMap>)>; #[salsa::transparent] @@ -84,7 +81,7 @@ pub trait ExpandDatabase: RootQueryDb { #[salsa::transparent] #[salsa::invoke(crate::span_map::expansion_span_map)] - fn expansion_span_map(&self, file_id: MacroFileId) -> Arc<ExpansionSpanMap>; + fn expansion_span_map(&self, file_id: MacroCallId) -> Arc<ExpansionSpanMap>; #[salsa::invoke(crate::span_map::real_span_map)] fn real_span_map(&self, file_id: EditionedFileId) -> Arc<RealSpanMap>; @@ -136,6 +133,7 @@ pub trait ExpandDatabase: RootQueryDb { /// directly depend on as that would cause to frequent invalidations, mainly because of the /// parse queries being LRU cached. If they weren't the invalidations would only happen if the /// user wrote in the file that defines the proc-macro. + #[salsa::invoke_interned(proc_macro_span)] fn proc_macro_span(&self, fun: AstId<ast::Fn>) -> Span; /// Firewall query that returns the errors from the `parse_macro_expansion` query. @@ -149,30 +147,17 @@ pub trait ExpandDatabase: RootQueryDb { fn syntax_context(&self, file: HirFileId, edition: Edition) -> SyntaxContext; } -#[salsa::interned(no_lifetime, id = span::MacroCallId)] -pub struct MacroCallWrapper { - pub loc: MacroCallLoc, -} - -fn intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallLoc) -> MacroCallId { - MacroCallWrapper::new(db, macro_call).0 -} - -fn lookup_intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallId) -> MacroCallLoc { - MacroCallWrapper::ingredient(db).data(db.as_dyn_database(), macro_call.as_id()).0.clone() -} - #[salsa::interned(no_lifetime, id = span::SyntaxContext)] pub struct SyntaxContextWrapper { pub data: SyntaxContext, } fn syntax_context(db: &dyn ExpandDatabase, file: HirFileId, edition: Edition) -> SyntaxContext { - match file.repr() { - HirFileIdRepr::FileId(_) => SyntaxContext::root(edition), - HirFileIdRepr::MacroFile(m) => { - let kind = db.lookup_intern_macro_call(m.macro_call_id).kind; - db.macro_arg_considering_derives(m.macro_call_id, &kind).2.ctx + match file { + HirFileId::FileId(_) => SyntaxContext::root(edition), + HirFileId::MacroFile(m) => { + let kind = db.lookup_intern_macro_call(m).kind; + db.macro_arg_considering_derives(m, &kind).2.ctx } } } @@ -300,9 +285,9 @@ pub fn expand_speculative( loc.krate, &tt, attr_arg.as_ref(), - span_with_def_site_ctxt(db, span, actual_macro_call, loc.def.edition), - span_with_call_site_ctxt(db, span, actual_macro_call, loc.def.edition), - span_with_mixed_site_ctxt(db, span, actual_macro_call, loc.def.edition), + span_with_def_site_ctxt(db, span, actual_macro_call.into(), loc.def.edition), + span_with_call_site_ctxt(db, span, actual_macro_call.into(), loc.def.edition), + span_with_mixed_site_ctxt(db, span, actual_macro_call.into(), loc.def.edition), ) } MacroDefKind::BuiltInAttr(_, it) if it.is_derive() => { @@ -346,20 +331,16 @@ pub fn expand_speculative( Some((node.syntax_node(), token)) } -fn ast_id_map(db: &dyn ExpandDatabase, file_id: span::HirFileId) -> triomphe::Arc<AstIdMap> { +fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> triomphe::Arc<AstIdMap> { triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id))) } /// Main public API -- parses a hir file, not caring whether it's a real /// file or a macro expansion. fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode { - match file_id.repr() { - HirFileIdRepr::FileId(editioned_file_id) => { - let file_id = base_db::EditionedFileId::new(db, editioned_file_id); - db.parse(file_id).syntax_node() - } - - HirFileIdRepr::MacroFile(macro_file) => { + match file_id { + HirFileId::FileId(file_id) => db.parse(file_id).syntax_node(), + HirFileId::MacroFile(macro_file) => { db.parse_macro_expansion(macro_file).value.0.syntax_node() } } @@ -369,14 +350,13 @@ fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode { // instead of having it be untyped fn parse_macro_expansion( db: &dyn ExpandDatabase, - macro_file: MacroFileId, + macro_file: MacroCallId, ) -> ExpandResult<(Parse<SyntaxNode>, Arc<ExpansionSpanMap>)> { let _p = tracing::info_span!("parse_macro_expansion").entered(); - let loc = db.lookup_intern_macro_call(macro_file.macro_call_id); + let loc = db.lookup_intern_macro_call(macro_file); let def_edition = loc.def.edition; let expand_to = loc.expand_to(); - let mbe::ValueResult { value: (tt, matched_arm), err } = - macro_expand(db, macro_file.macro_call_id, loc); + let mbe::ValueResult { value: (tt, matched_arm), err } = macro_expand(db, macro_file, loc); let (parse, mut rev_token_map) = token_tree_to_syntax_node( db, @@ -397,7 +377,7 @@ fn parse_macro_expansion_error( macro_call_id: MacroCallId, ) -> Option<Arc<ExpandResult<Arc<[SyntaxError]>>>> { let e: ExpandResult<Arc<[SyntaxError]>> = - db.parse_macro_expansion(MacroFileId { macro_call_id }).map(|it| Arc::from(it.0.errors())); + db.parse_macro_expansion(macro_call_id).map(|it| Arc::from(it.0.errors())); if e.value.is_empty() && e.err.is_none() { None } else { Some(Arc::new(e)) } } @@ -405,16 +385,11 @@ pub(crate) fn parse_with_map( db: &dyn ExpandDatabase, file_id: HirFileId, ) -> (Parse<SyntaxNode>, SpanMap) { - match file_id.repr() { - HirFileIdRepr::FileId(editioned_file_id) => { - let file_id = base_db::EditionedFileId::new(db, editioned_file_id); - - ( - db.parse(file_id).to_syntax(), - SpanMap::RealSpanMap(db.real_span_map(editioned_file_id)), - ) + match file_id { + HirFileId::FileId(file_id) => { + (db.parse(file_id).to_syntax(), SpanMap::RealSpanMap(db.real_span_map(file_id))) } - HirFileIdRepr::MacroFile(macro_file) => { + HirFileId::MacroFile(macro_file) => { let (parse, map) = db.parse_macro_expansion(macro_file).value; (parse, SpanMap::ExpansionSpanMap(map)) } @@ -734,9 +709,9 @@ fn expand_proc_macro( loc.krate, ¯o_arg, attr_arg, - span_with_def_site_ctxt(db, span, id, loc.def.edition), - span_with_call_site_ctxt(db, span, id, loc.def.edition), - span_with_mixed_site_ctxt(db, span, id, loc.def.edition), + span_with_def_site_ctxt(db, span, id.into(), loc.def.edition), + span_with_call_site_ctxt(db, span, id.into(), loc.def.edition), + span_with_mixed_site_ctxt(db, span, id.into(), loc.def.edition), ) }; @@ -784,3 +759,11 @@ fn check_tt_count(tt: &tt::TopSubtree) -> Result<(), ExpandResult<()>> { }) } } + +fn intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallLoc) -> MacroCallId { + MacroCallId::new(db, macro_call) +} + +fn lookup_intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallId) -> MacroCallLoc { + macro_call.loc(db) +} |