aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model_api.rs4
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs4
-rw-r--r--crates/ra_hir/src/db.rs9
-rw-r--r--crates/ra_hir/src/lib.rs2
-rw-r--r--crates/ra_hir/src/nameres.rs6
-rw-r--r--crates/ra_hir/src/nameres/lower.rs182
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_ide_api/src/db.rs1
8 files changed, 25 insertions, 185 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 865e5e809..860fd7539 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -7,7 +7,7 @@ use ra_syntax::{ast, TreeArc, SyntaxNode};
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::{ModuleScope, lower::LoweredImport}, 10 nameres::{ModuleScope, lower::ImportId},
11 db::HirDatabase, 11 db::HirDatabase,
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::InferenceResult,
@@ -100,7 +100,7 @@ impl Module {
100 pub fn import_source( 100 pub fn import_source(
101 &self, 101 &self,
102 db: &impl HirDatabase, 102 db: &impl HirDatabase,
103 import: LoweredImport, 103 import: ImportId,
104 ) -> TreeArc<ast::PathSegment> { 104 ) -> TreeArc<ast::PathSegment> {
105 self.import_source_impl(db, import) 105 self.import_source_impl(db, import)
106 } 106 }
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index f110548c6..e5394d440 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -5,7 +5,7 @@ use crate::{
5 Module, ModuleSource, Problem, 5 Module, ModuleSource, Problem,
6 Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, 6 Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def,
7 module_tree::ModuleId, 7 module_tree::ModuleId,
8 nameres::{ModuleScope, lower::LoweredImport}, 8 nameres::{ModuleScope, lower::ImportId},
9 db::HirDatabase, 9 db::HirDatabase,
10}; 10};
11 11
@@ -69,7 +69,7 @@ impl Module {
69 pub(crate) fn import_source_impl( 69 pub(crate) fn import_source_impl(
70 &self, 70 &self,
71 db: &impl HirDatabase, 71 db: &impl HirDatabase,
72 import: LoweredImport, 72 import: ImportId,
73 ) -> TreeArc<ast::PathSegment> { 73 ) -> TreeArc<ast::PathSegment> {
74 let loc = self.def_id.loc(db); 74 let loc = self.def_id.loc(db);
75 let source_map = db.lower_module_source_map(loc.source_root_id, loc.module_id); 75 let source_map = db.lower_module_source_map(loc.source_root_id, loc.module_id);
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index ccc53c454..60065cf6a 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -10,7 +10,7 @@ use crate::{
10 FnSignature, FnScopes, 10 FnSignature, FnScopes,
11 macros::MacroExpansion, 11 macros::MacroExpansion,
12 module_tree::{ModuleId, ModuleTree}, 12 module_tree::{ModuleId, ModuleTree},
13 nameres::{ItemMap, lower::{InputModuleItems, LoweredModule, ImportSourceMap}}, 13 nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}},
14 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks}, 14 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks},
15 adt::{StructData, EnumData, EnumVariantData}, 15 adt::{StructData, EnumData, EnumVariantData},
16 impl_block::ModuleImplBlocks, 16 impl_block::ModuleImplBlocks,
@@ -58,13 +58,6 @@ pub trait HirDatabase:
58 #[salsa::invoke(crate::module_tree::Submodule::submodules_query)] 58 #[salsa::invoke(crate::module_tree::Submodule::submodules_query)]
59 fn submodules(&self, source: SourceItemId) -> Arc<Vec<crate::module_tree::Submodule>>; 59 fn submodules(&self, source: SourceItemId) -> Arc<Vec<crate::module_tree::Submodule>>;
60 60
61 #[salsa::invoke(crate::nameres::lower::InputModuleItems::input_module_items_query)]
62 fn input_module_items(
63 &self,
64 source_root_id: SourceRootId,
65 module_id: ModuleId,
66 ) -> Arc<InputModuleItems>;
67
68 #[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_query)] 61 #[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_query)]
69 fn lower_module( 62 fn lower_module(
70 &self, 63 &self,
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index ef7d049ee..75b9c6b43 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -31,7 +31,7 @@ mod code_model_impl;
31use crate::{ 31use crate::{
32 db::HirDatabase, 32 db::HirDatabase,
33 name::{AsName, KnownName}, 33 name::{AsName, KnownName},
34 ids::{DefKind, SourceItemId, SourceFileItemId, SourceFileItems}, 34 ids::{DefKind, SourceItemId, SourceFileItems},
35}; 35};
36 36
37pub use self::{ 37pub use self::{
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index ab0a9041d..4efafd409 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -60,7 +60,7 @@ pub struct Resolution {
60 /// None for unresolved 60 /// None for unresolved
61 pub def_id: PerNs<DefId>, 61 pub def_id: PerNs<DefId>,
62 /// ident by whitch this is imported into local scope. 62 /// ident by whitch this is imported into local scope.
63 pub import: Option<LoweredImport>, 63 pub import: Option<ImportId>,
64} 64}
65 65
66#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] 66#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -154,7 +154,7 @@ pub(crate) struct Resolver<'a, DB> {
154 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>, 154 input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
155 source_root: SourceRootId, 155 source_root: SourceRootId,
156 module_tree: Arc<ModuleTree>, 156 module_tree: Arc<ModuleTree>,
157 processed_imports: FxHashSet<(ModuleId, LoweredImport)>, 157 processed_imports: FxHashSet<(ModuleId, ImportId)>,
158 result: ItemMap, 158 result: ItemMap,
159} 159}
160 160
@@ -296,7 +296,7 @@ where
296 fn resolve_import( 296 fn resolve_import(
297 &mut self, 297 &mut self,
298 module_id: ModuleId, 298 module_id: ModuleId,
299 import_id: LoweredImport, 299 import_id: ImportId,
300 import: &ImportData, 300 import: &ImportData,
301 ) -> bool { 301 ) -> bool {
302 log::debug!("resolving import: {:?}", import); 302 log::debug!("resolving import: {:?}", import);
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs
index 6bca14444..52448644c 100644
--- a/crates/ra_hir/src/nameres/lower.rs
+++ b/crates/ra_hir/src/nameres/lower.rs
@@ -1,157 +1,17 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 TextRange, SyntaxKind, AstNode, SourceFile, TreeArc, 4 SyntaxKind, AstNode, SourceFile, TreeArc,
5 ast::{self, ModuleItemOwner}, 5 ast::{self, ModuleItemOwner},
6}; 6};
7use ra_db::{SourceRootId, LocalSyntaxPtr}; 7use ra_db::{SourceRootId, LocalSyntaxPtr};
8use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; 8use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
9 9
10use crate::{ 10use crate::{
11 SourceItemId, SourceFileItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, 11 SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
12 HirFileId, MacroCallLoc, AsName, 12 HirFileId, MacroCallLoc, AsName,
13 module_tree::ModuleId 13 module_tree::ModuleId
14}; 14};
15/// A set of items and imports declared inside a module, without relation to
16/// other modules.
17///
18/// This sits in-between raw syntax and name resolution and allows us to avoid
19/// recomputing name res: if two instance of `InputModuleItems` are the same, we
20/// can avoid redoing name resolution.
21#[derive(Debug, Default, PartialEq, Eq)]
22pub struct InputModuleItems {
23 pub(crate) items: Vec<ModuleItem>,
24 pub(super) imports: Vec<Import>,
25}
26
27impl InputModuleItems {
28 pub(crate) fn input_module_items_query(
29 db: &impl HirDatabase,
30 source_root_id: SourceRootId,
31 module_id: ModuleId,
32 ) -> Arc<InputModuleItems> {
33 let module_tree = db.module_tree(source_root_id);
34 let source = module_id.source(&module_tree);
35 let file_id = source.file_id;
36 let source = ModuleSource::from_source_item_id(db, source);
37 let mut res = InputModuleItems::default();
38 match source {
39 ModuleSource::SourceFile(it) => res.fill(
40 db,
41 source_root_id,
42 module_id,
43 file_id,
44 &mut it.items_with_macros(),
45 ),
46 ModuleSource::Module(it) => {
47 if let Some(item_list) = it.item_list() {
48 res.fill(
49 db,
50 source_root_id,
51 module_id,
52 file_id,
53 &mut item_list.items_with_macros(),
54 )
55 }
56 }
57 };
58 Arc::new(res)
59 }
60
61 fn fill(
62 &mut self,
63 db: &impl HirDatabase,
64 source_root_id: SourceRootId,
65 module_id: ModuleId,
66 file_id: HirFileId,
67 items: &mut Iterator<Item = ast::ItemOrMacro>,
68 ) {
69 let file_items = db.file_items(file_id);
70
71 for item in items {
72 match item {
73 ast::ItemOrMacro::Item(it) => {
74 self.add_item(file_id, &file_items, it);
75 }
76 ast::ItemOrMacro::Macro(macro_call) => {
77 let item_id = file_items.id_of_unchecked(macro_call.syntax());
78 let loc = MacroCallLoc {
79 source_root_id,
80 module_id,
81 source_item_id: SourceItemId {
82 file_id,
83 item_id: Some(item_id),
84 },
85 };
86 let id = loc.id(db);
87 let file_id = HirFileId::from(id);
88 let file_items = db.file_items(file_id);
89 //FIXME: expand recursively
90 for item in db.hir_source_file(file_id).items() {
91 self.add_item(file_id, &file_items, item);
92 }
93 }
94 }
95 }
96 }
97
98 fn add_item(
99 &mut self,
100 file_id: HirFileId,
101 file_items: &SourceFileItems,
102 item: &ast::ModuleItem,
103 ) -> Option<()> {
104 match item.kind() {
105 ast::ModuleItemKind::StructDef(it) => {
106 self.items.push(ModuleItem::new(file_id, file_items, it)?)
107 }
108 ast::ModuleItemKind::EnumDef(it) => {
109 self.items.push(ModuleItem::new(file_id, file_items, it)?)
110 }
111 ast::ModuleItemKind::FnDef(it) => {
112 self.items.push(ModuleItem::new(file_id, file_items, it)?)
113 }
114 ast::ModuleItemKind::TraitDef(it) => {
115 self.items.push(ModuleItem::new(file_id, file_items, it)?)
116 }
117 ast::ModuleItemKind::TypeDef(it) => {
118 self.items.push(ModuleItem::new(file_id, file_items, it)?)
119 }
120 ast::ModuleItemKind::ImplBlock(_) => {
121 // impls don't define items
122 }
123 ast::ModuleItemKind::UseItem(it) => self.add_use_item(file_items, it),
124 ast::ModuleItemKind::ExternCrateItem(_) => {
125 // TODO
126 }
127 ast::ModuleItemKind::ConstDef(it) => {
128 self.items.push(ModuleItem::new(file_id, file_items, it)?)
129 }
130 ast::ModuleItemKind::StaticDef(it) => {
131 self.items.push(ModuleItem::new(file_id, file_items, it)?)
132 }
133 ast::ModuleItemKind::Module(it) => {
134 self.items.push(ModuleItem::new(file_id, file_items, it)?)
135 }
136 }
137 Some(())
138 }
139
140 fn add_use_item(&mut self, file_items: &SourceFileItems, item: &ast::UseItem) {
141 let file_item_id = file_items.id_of_unchecked(item.syntax());
142 let start_offset = item.syntax().range().start();
143 Path::expand_use_item(item, |path, segment| {
144 let kind = match segment {
145 None => ImportKind::Glob,
146 Some(segment) => ImportKind::Named(NamedImport {
147 file_item_id,
148 relative_range: segment.syntax().range() - start_offset,
149 }),
150 };
151 self.imports.push(Import { kind, path })
152 })
153 }
154}
155 15
156#[derive(Debug, PartialEq, Eq)] 16#[derive(Debug, PartialEq, Eq)]
157pub(super) enum Vis { 17pub(super) enum Vis {
@@ -188,27 +48,9 @@ impl ModuleItem {
188 } 48 }
189} 49}
190 50
191#[derive(Debug, Clone, PartialEq, Eq)]
192pub(super) struct Import {
193 pub(super) path: Path,
194 pub(super) kind: ImportKind,
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
198pub struct NamedImport {
199 pub file_item_id: SourceFileItemId,
200 pub relative_range: TextRange,
201}
202
203#[derive(Debug, Clone, PartialEq, Eq)]
204pub(super) enum ImportKind {
205 Glob,
206 Named(NamedImport),
207}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 51#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
210pub struct LoweredImport(RawId); 52pub struct ImportId(RawId);
211impl_arena_id!(LoweredImport); 53impl_arena_id!(ImportId);
212 54
213#[derive(Debug, PartialEq, Eq)] 55#[derive(Debug, PartialEq, Eq)]
214pub(super) struct ImportData { 56pub(super) struct ImportData {
@@ -216,24 +58,30 @@ pub(super) struct ImportData {
216 pub(super) is_glob: bool, 58 pub(super) is_glob: bool,
217} 59}
218 60
61/// A set of items and imports declared inside a module, without relation to
62/// other modules.
63///
64/// This sits in-between raw syntax and name resolution and allows us to avoid
65/// recomputing name res: if two instance of `InputModuleItems` are the same, we
66/// can avoid redoing name resolution.
219#[derive(Debug, Default, PartialEq, Eq)] 67#[derive(Debug, Default, PartialEq, Eq)]
220pub struct LoweredModule { 68pub struct LoweredModule {
221 pub(super) items: Vec<ModuleItem>, 69 pub(crate) items: Vec<ModuleItem>,
222 pub(super) imports: Arena<LoweredImport, ImportData>, 70 pub(super) imports: Arena<ImportId, ImportData>,
223} 71}
224 72
225#[derive(Debug, Default, PartialEq, Eq)] 73#[derive(Debug, Default, PartialEq, Eq)]
226pub struct ImportSourceMap { 74pub struct ImportSourceMap {
227 map: ArenaMap<LoweredImport, LocalSyntaxPtr>, 75 map: ArenaMap<ImportId, LocalSyntaxPtr>,
228} 76}
229 77
230impl ImportSourceMap { 78impl ImportSourceMap {
231 fn insert(&mut self, import: LoweredImport, segment: &ast::PathSegment) { 79 fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) {
232 self.map 80 self.map
233 .insert(import, LocalSyntaxPtr::new(segment.syntax())) 81 .insert(import, LocalSyntaxPtr::new(segment.syntax()))
234 } 82 }
235 83
236 pub fn get(&self, source: &ModuleSource, import: LoweredImport) -> TreeArc<ast::PathSegment> { 84 pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> {
237 let file = match source { 85 let file = match source {
238 ModuleSource::SourceFile(file) => &*file, 86 ModuleSource::SourceFile(file) => &*file,
239 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), 87 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index be82d804a..2fe354b04 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -142,7 +142,7 @@ pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, Te
142 None => return Vec::new(), 142 None => return Vec::new(),
143 }; 143 };
144 let loc = module.def_id.loc(db); 144 let loc = module.def_id.loc(db);
145 let items = db.input_module_items(loc.source_root_id, loc.module_id); 145 let items = db.lower_module_module(loc.source_root_id, loc.module_id);
146 let mut res = Vec::new(); 146 let mut res = Vec::new();
147 147
148 for macro_call_id in items 148 for macro_call_id in items
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs
index a1d3333b2..c2978f909 100644
--- a/crates/ra_ide_api/src/db.rs
+++ b/crates/ra_ide_api/src/db.rs
@@ -113,7 +113,6 @@ salsa::database_storage! {
113 fn fn_scopes() for hir::db::FnScopesQuery; 113 fn fn_scopes() for hir::db::FnScopesQuery;
114 fn file_items() for hir::db::FileItemsQuery; 114 fn file_items() for hir::db::FileItemsQuery;
115 fn file_item() for hir::db::FileItemQuery; 115 fn file_item() for hir::db::FileItemQuery;
116 fn input_module_items() for hir::db::InputModuleItemsQuery;
117 fn lower_module() for hir::db::LowerModuleQuery; 116 fn lower_module() for hir::db::LowerModuleQuery;
118 fn lower_module_module() for hir::db::LowerModuleModuleQuery; 117 fn lower_module_module() for hir::db::LowerModuleModuleQuery;
119 fn lower_module_source_map() for hir::db::LowerModuleSourceMapQuery; 118 fn lower_module_source_map() for hir::db::LowerModuleSourceMapQuery;