aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ids.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r--crates/ra_hir/src/ids.rs31
1 files changed, 17 insertions, 14 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 06b6888f4..79e32e579 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -9,7 +9,7 @@ use ra_prof::profile;
9use mbe::MacroRules; 9use mbe::MacroRules;
10 10
11use crate::{ 11use crate::{
12 Module, DefDatabase, AstId, FileAstId, 12 Module, DefDatabase, AstId, FileAstId, AstDatabase,
13}; 13};
14 14
15/// hir makes heavy use of ids: integer (u32) handlers to various things. You 15/// hir makes heavy use of ids: integer (u32) handlers to various things. You
@@ -37,7 +37,7 @@ pub struct HirFileId(HirFileIdRepr);
37impl HirFileId { 37impl HirFileId {
38 /// For macro-expansion files, returns the file original source file the 38 /// For macro-expansion files, returns the file original source file the
39 /// expansion originated from. 39 /// expansion originated from.
40 pub fn original_file(self, db: &impl DefDatabase) -> FileId { 40 pub fn original_file(self, db: &impl AstDatabase) -> FileId {
41 match self.0 { 41 match self.0 {
42 HirFileIdRepr::File(file_id) => file_id, 42 HirFileIdRepr::File(file_id) => file_id,
43 HirFileIdRepr::Macro(macro_file) => { 43 HirFileIdRepr::Macro(macro_file) => {
@@ -58,7 +58,7 @@ impl HirFileId {
58 } 58 }
59 59
60 pub(crate) fn parse_or_expand_query( 60 pub(crate) fn parse_or_expand_query(
61 db: &impl DefDatabase, 61 db: &impl AstDatabase,
62 file_id: HirFileId, 62 file_id: HirFileId,
63 ) -> Option<TreeArc<SyntaxNode>> { 63 ) -> Option<TreeArc<SyntaxNode>> {
64 db.check_canceled(); 64 db.check_canceled();
@@ -120,7 +120,7 @@ impl From<FileId> for HirFileId {
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
121pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); 121pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
122 122
123pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { 123pub(crate) fn macro_def_query(db: &impl AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
124 let macro_call = id.0.to_node(db); 124 let macro_call = id.0.to_node(db);
125 let arg = macro_call.token_tree()?; 125 let arg = macro_call.token_tree()?;
126 let (tt, _) = mbe::ast_to_token_tree(arg).or_else(|| { 126 let (tt, _) = mbe::ast_to_token_tree(arg).or_else(|| {
@@ -134,7 +134,7 @@ pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<A
134 Some(Arc::new(rules)) 134 Some(Arc::new(rules))
135} 135}
136 136
137pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<Arc<tt::Subtree>> { 137pub(crate) fn macro_arg_query(db: &impl AstDatabase, id: MacroCallId) -> Option<Arc<tt::Subtree>> {
138 let loc = id.loc(db); 138 let loc = id.loc(db);
139 let macro_call = loc.ast_id.to_node(db); 139 let macro_call = loc.ast_id.to_node(db);
140 let arg = macro_call.token_tree()?; 140 let arg = macro_call.token_tree()?;
@@ -143,7 +143,7 @@ pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<
143} 143}
144 144
145pub(crate) fn macro_expand_query( 145pub(crate) fn macro_expand_query(
146 db: &impl DefDatabase, 146 db: &impl AstDatabase,
147 id: MacroCallId, 147 id: MacroCallId,
148) -> Result<Arc<tt::Subtree>, String> { 148) -> Result<Arc<tt::Subtree>, String> {
149 let loc = id.loc(db); 149 let loc = id.loc(db);
@@ -185,7 +185,7 @@ pub struct MacroCallLoc {
185} 185}
186 186
187impl MacroCallId { 187impl MacroCallId {
188 pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc { 188 pub(crate) fn loc(self, db: &impl AstDatabase) -> MacroCallLoc {
189 db.lookup_intern_macro(self) 189 db.lookup_intern_macro(self)
190 } 190 }
191 191
@@ -196,7 +196,7 @@ impl MacroCallId {
196} 196}
197 197
198impl MacroCallLoc { 198impl MacroCallLoc {
199 pub(crate) fn id(self, db: &impl DefDatabase) -> MacroCallId { 199 pub(crate) fn id(self, db: &impl AstDatabase) -> MacroCallId {
200 db.intern_macro(self) 200 db.intern_macro(self)
201 } 201 }
202} 202}
@@ -233,7 +233,7 @@ pub(crate) struct LocationCtx<DB> {
233 file_id: HirFileId, 233 file_id: HirFileId,
234} 234}
235 235
236impl<'a, DB: DefDatabase> LocationCtx<&'a DB> { 236impl<'a, DB: DefDatabase + AstDatabase> LocationCtx<&'a DB> {
237 pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> { 237 pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
238 LocationCtx { db, module, file_id } 238 LocationCtx { db, module, file_id }
239 } 239 }
@@ -250,16 +250,19 @@ pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
250 fn intern(db: &impl DefDatabase, loc: ItemLoc<N>) -> Self; 250 fn intern(db: &impl DefDatabase, loc: ItemLoc<N>) -> Self;
251 fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<N>; 251 fn lookup_intern(self, db: &impl DefDatabase) -> ItemLoc<N>;
252 252
253 fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self { 253 fn from_ast(ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>, ast: &N) -> Self {
254 let items = ctx.db.ast_id_map(ctx.file_id); 254 let items = ctx.db.ast_id_map(ctx.file_id);
255 let item_id = items.ast_id(ast); 255 let item_id = items.ast_id(ast);
256 Self::from_ast_id(ctx, item_id) 256 Self::from_ast_id(ctx, item_id)
257 } 257 }
258 fn from_ast_id(ctx: LocationCtx<&impl DefDatabase>, ast_id: FileAstId<N>) -> Self { 258 fn from_ast_id(
259 ctx: LocationCtx<&(impl AstDatabase + DefDatabase)>,
260 ast_id: FileAstId<N>,
261 ) -> Self {
259 let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) }; 262 let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
260 Self::intern(ctx.db, loc) 263 Self::intern(ctx.db, loc)
261 } 264 }
262 fn source(self, db: &impl DefDatabase) -> (HirFileId, TreeArc<N>) { 265 fn source(self, db: &(impl AstDatabase + DefDatabase)) -> (HirFileId, TreeArc<N>) {
263 let loc = self.lookup_intern(db); 266 let loc = self.lookup_intern(db);
264 let ast = loc.ast_id.to_node(db); 267 let ast = loc.ast_id.to_node(db);
265 (loc.ast_id.file_id(), ast) 268 (loc.ast_id.file_id(), ast)
@@ -356,7 +359,7 @@ impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
356} 359}
357 360
358impl MacroCallId { 361impl MacroCallId {
359 pub fn debug_dump(&self, db: &impl DefDatabase) -> String { 362 pub fn debug_dump(&self, db: &impl AstDatabase) -> String {
360 let loc = self.clone().loc(db); 363 let loc = self.clone().loc(db);
361 let node = loc.ast_id.to_node(db); 364 let node = loc.ast_id.to_node(db);
362 let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" "); 365 let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" ");
@@ -367,7 +370,7 @@ impl MacroCallId {
367 let macro_rules = db.macro_def(loc.def); 370 let macro_rules = db.macro_def(loc.def);
368 371
369 format!( 372 format!(
370 "macro call [file: {:#?}] : {}\nhas rules: {}", 373 "macro call [file: {:?}] : {}\nhas rules: {}",
371 db.file_relative_path(original), 374 db.file_relative_path(original),
372 syntax_str, 375 syntax_str,
373 macro_rules.is_some() 376 macro_rules.is_some()