diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/db.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/macros.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/module.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/query_definitions.rs | 28 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 8 |
7 files changed, 62 insertions, 30 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 289bec507..1da3fc1c1 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::SyntaxNode; | 3 | use ra_syntax::{SyntaxNode, SourceFileNode}; |
4 | use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable}; | 4 | use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | DefLoc, DefId, Name, | 7 | DefLoc, DefId, Name, MFileId, |
8 | SourceFileItems, SourceItemId, | 8 | SourceFileItems, SourceItemId, |
9 | query_definitions, | 9 | query_definitions, |
10 | FnScopes, | 10 | FnScopes, |
@@ -21,6 +21,10 @@ pub trait HirDatabase: SyntaxDatabase | |||
21 | + AsRef<LocationIntener<DefLoc, DefId>> | 21 | + AsRef<LocationIntener<DefLoc, DefId>> |
22 | + AsRef<LocationIntener<MacroCallLoc, MacroCallId>> | 22 | + AsRef<LocationIntener<MacroCallLoc, MacroCallId>> |
23 | { | 23 | { |
24 | fn m_source_file(mfile_id: MFileId) -> SourceFileNode { | ||
25 | type MSourceFileQuery; | ||
26 | use fn crate::query_definitions::m_source_file; | ||
27 | } | ||
24 | fn expand_macro_invocation(invoc: MacroCallId) -> Option<Arc<MacroExpansion>> { | 28 | fn expand_macro_invocation(invoc: MacroCallId) -> Option<Arc<MacroExpansion>> { |
25 | type ExpandMacroCallQuery; | 29 | type ExpandMacroCallQuery; |
26 | use fn crate::macros::expand_macro_invocation; | 30 | use fn crate::macros::expand_macro_invocation; |
@@ -56,7 +60,7 @@ pub trait HirDatabase: SyntaxDatabase | |||
56 | use fn crate::ty::type_for_field; | 60 | use fn crate::ty::type_for_field; |
57 | } | 61 | } |
58 | 62 | ||
59 | fn file_items(file_id: FileId) -> Arc<SourceFileItems> { | 63 | fn file_items(mfile_id: MFileId) -> Arc<SourceFileItems> { |
60 | type SourceFileItemsQuery; | 64 | type SourceFileItemsQuery; |
61 | use fn query_definitions::file_items; | 65 | use fn query_definitions::file_items; |
62 | } | 66 | } |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4c67921bd..1219b9fba 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -57,11 +57,18 @@ pub use self::function::FnSignatureInfo; | |||
57 | 57 | ||
58 | /// An `MFileId` is like a `FileId`, but it can also refer to code generated by | 58 | /// An `MFileId` is like a `FileId`, but it can also refer to code generated by |
59 | /// macros. | 59 | /// macros. |
60 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
60 | pub enum MFileId { | 61 | pub enum MFileId { |
61 | File(FileId), | 62 | File(FileId), |
62 | Macro(MacroCallId), | 63 | Macro(MacroCallId), |
63 | } | 64 | } |
64 | 65 | ||
66 | impl From<FileId> for MFileId { | ||
67 | fn from(file_id: FileId) -> MFileId { | ||
68 | MFileId::File(file_id) | ||
69 | } | ||
70 | } | ||
71 | |||
65 | /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) | 72 | /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) |
66 | /// in a specific module. | 73 | /// in a specific module. |
67 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 74 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -163,7 +170,7 @@ pub(crate) type SourceFileItemId = Id<SyntaxNode>; | |||
163 | 170 | ||
164 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 171 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
165 | pub struct SourceItemId { | 172 | pub struct SourceItemId { |
166 | file_id: FileId, | 173 | mfile_id: MFileId, |
167 | /// None for the whole file. | 174 | /// None for the whole file. |
168 | item_id: Option<SourceFileItemId>, | 175 | item_id: Option<SourceFileItemId>, |
169 | } | 176 | } |
@@ -171,14 +178,14 @@ pub struct SourceItemId { | |||
171 | /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. | 178 | /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. |
172 | #[derive(Debug, PartialEq, Eq)] | 179 | #[derive(Debug, PartialEq, Eq)] |
173 | pub struct SourceFileItems { | 180 | pub struct SourceFileItems { |
174 | file_id: FileId, | 181 | mfile_id: MFileId, |
175 | arena: Arena<SyntaxNode>, | 182 | arena: Arena<SyntaxNode>, |
176 | } | 183 | } |
177 | 184 | ||
178 | impl SourceFileItems { | 185 | impl SourceFileItems { |
179 | fn new(file_id: FileId, source_file: SourceFile) -> SourceFileItems { | 186 | fn new(mfile_id: MFileId, source_file: SourceFile) -> SourceFileItems { |
180 | let mut res = SourceFileItems { | 187 | let mut res = SourceFileItems { |
181 | file_id, | 188 | mfile_id, |
182 | arena: Arena::default(), | 189 | arena: Arena::default(), |
183 | }; | 190 | }; |
184 | res.init(source_file); | 191 | res.init(source_file); |
@@ -198,11 +205,11 @@ impl SourceFileItems { | |||
198 | fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { | 205 | fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { |
199 | self.arena.alloc(item) | 206 | self.arena.alloc(item) |
200 | } | 207 | } |
201 | pub fn id_of(&self, file_id: FileId, item: SyntaxNodeRef) -> SourceFileItemId { | 208 | pub fn id_of(&self, mfile_id: MFileId, item: SyntaxNodeRef) -> SourceFileItemId { |
202 | assert_eq!( | 209 | assert_eq!( |
203 | self.file_id, file_id, | 210 | self.mfile_id, mfile_id, |
204 | "SourceFileItems: wrong file, expected {:?}, got {:?}", | 211 | "SourceFileItems: wrong file, expected {:?}, got {:?}", |
205 | self.file_id, file_id | 212 | self.mfile_id, mfile_id |
206 | ); | 213 | ); |
207 | self.id_of_unchecked(item) | 214 | self.id_of_unchecked(item) |
208 | } | 215 | } |
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index 3eedd9e2d..1f141dbac 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs | |||
@@ -147,7 +147,9 @@ pub struct MacroExpansion { | |||
147 | } | 147 | } |
148 | 148 | ||
149 | impl MacroExpansion { | 149 | impl MacroExpansion { |
150 | pub fn file(&self) -> SourceFileNode { | 150 | //FIXME: does not really make sense, macro expansion is not neccessary a |
151 | //whole file. | ||
152 | pub(crate) fn file(&self) -> SourceFileNode { | ||
151 | SourceFileNode::parse(&self.text) | 153 | SourceFileNode::parse(&self.text) |
152 | } | 154 | } |
153 | 155 | ||
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index 87e30191f..dde036f2c 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs | |||
@@ -15,6 +15,7 @@ use relative_path::RelativePathBuf; | |||
15 | use crate::{ | 15 | use crate::{ |
16 | Def, DefKind, DefLoc, DefId, | 16 | Def, DefKind, DefLoc, DefId, |
17 | Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate, | 17 | Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate, |
18 | MFileId, | ||
18 | arena::{Arena, Id}, | 19 | arena::{Arena, Id}, |
19 | }; | 20 | }; |
20 | 21 | ||
@@ -292,7 +293,10 @@ pub struct ModuleData { | |||
292 | impl ModuleSource { | 293 | impl ModuleSource { |
293 | // precondition: item_id **must** point to module | 294 | // precondition: item_id **must** point to module |
294 | fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource { | 295 | fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource { |
295 | let source_item_id = SourceItemId { file_id, item_id }; | 296 | let source_item_id = SourceItemId { |
297 | mfile_id: file_id.into(), | ||
298 | item_id, | ||
299 | }; | ||
296 | ModuleSource(source_item_id) | 300 | ModuleSource(source_item_id) |
297 | } | 301 | } |
298 | 302 | ||
@@ -306,13 +310,16 @@ impl ModuleSource { | |||
306 | m: ast::Module, | 310 | m: ast::Module, |
307 | ) -> ModuleSource { | 311 | ) -> ModuleSource { |
308 | assert!(!m.has_semi()); | 312 | assert!(!m.has_semi()); |
309 | let file_items = db.file_items(file_id); | 313 | let file_items = db.file_items(file_id.into()); |
310 | let item_id = file_items.id_of(file_id, m.syntax()); | 314 | let item_id = file_items.id_of(file_id.into(), m.syntax()); |
311 | ModuleSource::new(file_id, Some(item_id)) | 315 | ModuleSource::new(file_id, Some(item_id)) |
312 | } | 316 | } |
313 | 317 | ||
314 | pub fn file_id(self) -> FileId { | 318 | pub fn file_id(self) -> FileId { |
315 | self.0.file_id | 319 | match self.0.mfile_id { |
320 | MFileId::File(file_id) => file_id, | ||
321 | MFileId::Macro(_) => unreachable!(), | ||
322 | } | ||
316 | } | 323 | } |
317 | 324 | ||
318 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { | 325 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { |
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 46b1dc0b1..94ada73d7 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs | |||
@@ -98,7 +98,7 @@ pub struct NamedImport { | |||
98 | impl NamedImport { | 98 | impl NamedImport { |
99 | pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { | 99 | pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { |
100 | let source_item_id = SourceItemId { | 100 | let source_item_id = SourceItemId { |
101 | file_id, | 101 | mfile_id: file_id.into(), |
102 | item_id: Some(self.file_item_id), | 102 | item_id: Some(self.file_item_id), |
103 | }; | 103 | }; |
104 | let syntax = db.file_item(source_item_id); | 104 | let syntax = db.file_item(source_item_id); |
@@ -360,7 +360,7 @@ where | |||
360 | source_root_id: self.source_root, | 360 | source_root_id: self.source_root, |
361 | module_id, | 361 | module_id, |
362 | source_item_id: SourceItemId { | 362 | source_item_id: SourceItemId { |
363 | file_id, | 363 | mfile_id: file_id.into(), |
364 | item_id: Some(item.id), | 364 | item_id: Some(item.id), |
365 | }, | 365 | }, |
366 | }; | 366 | }; |
@@ -376,7 +376,7 @@ where | |||
376 | source_root_id: self.source_root, | 376 | source_root_id: self.source_root, |
377 | module_id, | 377 | module_id, |
378 | source_item_id: SourceItemId { | 378 | source_item_id: SourceItemId { |
379 | file_id, | 379 | mfile_id: file_id.into(), |
380 | item_id: Some(item.id), | 380 | item_id: Some(item.id), |
381 | }, | 381 | }, |
382 | }; | 382 | }; |
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index f66231222..0c07f1444 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs | |||
@@ -5,13 +5,13 @@ use std::{ | |||
5 | 5 | ||
6 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | AstNode, SyntaxNode, | 8 | AstNode, SyntaxNode, SourceFileNode, |
9 | ast::{self, NameOwner, ModuleItemOwner} | 9 | ast::{self, NameOwner, ModuleItemOwner} |
10 | }; | 10 | }; |
11 | use ra_db::{SourceRootId, FileId, Cancelable,}; | 11 | use ra_db::{SourceRootId, FileId, Cancelable,}; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | SourceFileItems, SourceItemId, DefKind, Function, DefId, Name, AsName, | 14 | SourceFileItems, SourceItemId, DefKind, Function, DefId, Name, AsName, MFileId, |
15 | db::HirDatabase, | 15 | db::HirDatabase, |
16 | function::FnScopes, | 16 | function::FnScopes, |
17 | module::{ | 17 | module::{ |
@@ -47,17 +47,29 @@ pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc< | |||
47 | Ok(Arc::new(EnumData::new(enum_def.borrowed()))) | 47 | Ok(Arc::new(EnumData::new(enum_def.borrowed()))) |
48 | } | 48 | } |
49 | 49 | ||
50 | pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> { | 50 | pub(super) fn m_source_file(db: &impl HirDatabase, mfile_id: MFileId) -> SourceFileNode { |
51 | let source_file = db.source_file(file_id); | 51 | match mfile_id { |
52 | MFileId::File(file_id) => db.source_file(file_id), | ||
53 | MFileId::Macro(m) => { | ||
54 | if let Some(exp) = db.expand_macro_invocation(m) { | ||
55 | return exp.file(); | ||
56 | } | ||
57 | SourceFileNode::parse("") | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | pub(super) fn file_items(db: &impl HirDatabase, mfile_id: MFileId) -> Arc<SourceFileItems> { | ||
63 | let source_file = db.m_source_file(mfile_id); | ||
52 | let source_file = source_file.borrowed(); | 64 | let source_file = source_file.borrowed(); |
53 | let res = SourceFileItems::new(file_id, source_file); | 65 | let res = SourceFileItems::new(mfile_id, source_file); |
54 | Arc::new(res) | 66 | Arc::new(res) |
55 | } | 67 | } |
56 | 68 | ||
57 | pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { | 69 | pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { |
58 | match source_item_id.item_id { | 70 | match source_item_id.item_id { |
59 | Some(id) => db.file_items(source_item_id.file_id)[id].clone(), | 71 | Some(id) => db.file_items(source_item_id.mfile_id)[id].clone(), |
60 | None => db.source_file(source_item_id.file_id).syntax().owned(), | 72 | None => db.m_source_file(source_item_id.mfile_id).syntax().owned(), |
61 | } | 73 | } |
62 | } | 74 | } |
63 | 75 | ||
@@ -116,7 +128,7 @@ pub(super) fn input_module_items( | |||
116 | ) -> Cancelable<Arc<InputModuleItems>> { | 128 | ) -> Cancelable<Arc<InputModuleItems>> { |
117 | let module_tree = db.module_tree(source_root)?; | 129 | let module_tree = db.module_tree(source_root)?; |
118 | let source = module_id.source(&module_tree); | 130 | let source = module_id.source(&module_tree); |
119 | let file_items = db.file_items(source.file_id()); | 131 | let file_items = db.file_items(source.file_id().into()); |
120 | let res = match source.resolve(db) { | 132 | let res = match source.resolve(db) { |
121 | ModuleSourceNode::SourceFile(it) => { | 133 | ModuleSourceNode::SourceFile(it) => { |
122 | let items = it.borrowed().items(); | 134 | let items = it.borrowed().items(); |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index a0d1daf71..4a99dff84 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -102,11 +102,11 @@ pub fn function_from_module( | |||
102 | module: &Module, | 102 | module: &Module, |
103 | fn_def: ast::FnDef, | 103 | fn_def: ast::FnDef, |
104 | ) -> Function { | 104 | ) -> Function { |
105 | let file_id = module.source().file_id(); | 105 | let mfile_id = module.source().file_id().into(); |
106 | let file_items = db.file_items(file_id); | 106 | let file_items = db.file_items(mfile_id); |
107 | let item_id = file_items.id_of(file_id, fn_def.syntax()); | 107 | let item_id = file_items.id_of(mfile_id, fn_def.syntax()); |
108 | let source_item_id = SourceItemId { | 108 | let source_item_id = SourceItemId { |
109 | file_id, | 109 | mfile_id, |
110 | item_id: Some(item_id), | 110 | item_id: Some(item_id), |
111 | }; | 111 | }; |
112 | let def_loc = DefLoc { | 112 | let def_loc = DefLoc { |