aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/lower.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-23 20:14:13 +0000
committerAleksey Kladov <[email protected]>2019-01-24 10:29:19 +0000
commit3ab1519cb27b927074ed7fbbb18a856e6e7fabb8 (patch)
tree692c7a256604e188d38890966290bd1637d7dd60 /crates/ra_hir/src/nameres/lower.rs
parentcfb085ded8d61d7b744d0a83ecbb3da254f6ab9f (diff)
Change ids strategy
this is a part of larghish hir refactoring which aims to * replace per-source-root module trees with per crate trees * switch from a monotyped DedId to type-specific ids
Diffstat (limited to 'crates/ra_hir/src/nameres/lower.rs')
-rw-r--r--crates/ra_hir/src/nameres/lower.rs74
1 files changed, 22 insertions, 52 deletions
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs
index 4eea6ff1d..6f003bd66 100644
--- a/crates/ra_hir/src/nameres/lower.rs
+++ b/crates/ra_hir/src/nameres/lower.rs
@@ -4,14 +4,13 @@ use ra_syntax::{
4 SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr, 4 SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr,
5 ast::{self, ModuleItemOwner, NameOwner}, 5 ast::{self, ModuleItemOwner, NameOwner},
6}; 6};
7use ra_db::SourceRootId;
8use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; 7use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
9use rustc_hash::FxHashMap; 8use rustc_hash::FxHashMap;
10 9
11use crate::{ 10use crate::{
12 SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, 11 SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
13 HirFileId, MacroCallLoc, AsName, PerNs, DefId, DefKind, DefLoc, 12 HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc,
14 module_tree::ModuleId 13 ModuleDef, Module,
15}; 14};
16 15
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -32,7 +31,7 @@ pub(super) struct ImportData {
32/// can avoid redoing name resolution. 31/// can avoid redoing name resolution.
33#[derive(Debug, Default, PartialEq, Eq)] 32#[derive(Debug, Default, PartialEq, Eq)]
34pub struct LoweredModule { 33pub struct LoweredModule {
35 pub(crate) declarations: FxHashMap<Name, PerNs<DefId>>, 34 pub(crate) declarations: FxHashMap<Name, PerNs<ModuleDef>>,
36 pub(super) imports: Arena<ImportId, ImportData>, 35 pub(super) imports: Arena<ImportId, ImportData>,
37} 36}
38 37
@@ -59,37 +58,31 @@ impl ImportSourceMap {
59impl LoweredModule { 58impl LoweredModule {
60 pub(crate) fn lower_module_module_query( 59 pub(crate) fn lower_module_module_query(
61 db: &impl HirDatabase, 60 db: &impl HirDatabase,
62 source_root_id: SourceRootId, 61 module: Module,
63 module_id: ModuleId,
64 ) -> Arc<LoweredModule> { 62 ) -> Arc<LoweredModule> {
65 db.lower_module(source_root_id, module_id).0 63 db.lower_module(module).0
66 } 64 }
67 65
68 pub(crate) fn lower_module_source_map_query( 66 pub(crate) fn lower_module_source_map_query(
69 db: &impl HirDatabase, 67 db: &impl HirDatabase,
70 source_root_id: SourceRootId, 68 module: Module,
71 module_id: ModuleId,
72 ) -> Arc<ImportSourceMap> { 69 ) -> Arc<ImportSourceMap> {
73 db.lower_module(source_root_id, module_id).1 70 db.lower_module(module).1
74 } 71 }
75 72
76 pub(crate) fn lower_module_query( 73 pub(crate) fn lower_module_query(
77 db: &impl HirDatabase, 74 db: &impl HirDatabase,
78 source_root_id: SourceRootId, 75 module: Module,
79 module_id: ModuleId,
80 ) -> (Arc<LoweredModule>, Arc<ImportSourceMap>) { 76 ) -> (Arc<LoweredModule>, Arc<ImportSourceMap>) {
81 let module_tree = db.module_tree(source_root_id); 77 let (file_id, source) = module.definition_source(db);
82 let source = module_id.source(&module_tree); 78 let file_id: HirFileId = file_id.into();
83 let file_id = source.file_id;
84 let source = ModuleSource::from_source_item_id(db, source);
85 let mut source_map = ImportSourceMap::default(); 79 let mut source_map = ImportSourceMap::default();
86 let mut res = LoweredModule::default(); 80 let mut res = LoweredModule::default();
87 match source { 81 match source {
88 ModuleSource::SourceFile(it) => res.fill( 82 ModuleSource::SourceFile(it) => res.fill(
89 &mut source_map, 83 &mut source_map,
90 db, 84 db,
91 source_root_id, 85 module,
92 module_id,
93 file_id, 86 file_id,
94 &mut it.items_with_macros(), 87 &mut it.items_with_macros(),
95 ), 88 ),
@@ -98,8 +91,7 @@ impl LoweredModule {
98 res.fill( 91 res.fill(
99 &mut source_map, 92 &mut source_map,
100 db, 93 db,
101 source_root_id, 94 module,
102 module_id,
103 file_id, 95 file_id,
104 &mut item_list.items_with_macros(), 96 &mut item_list.items_with_macros(),
105 ) 97 )
@@ -113,8 +105,7 @@ impl LoweredModule {
113 &mut self, 105 &mut self,
114 source_map: &mut ImportSourceMap, 106 source_map: &mut ImportSourceMap,
115 db: &impl HirDatabase, 107 db: &impl HirDatabase,
116 source_root_id: SourceRootId, 108 module: Module,
117 module_id: ModuleId,
118 file_id: HirFileId, 109 file_id: HirFileId,
119 items: &mut Iterator<Item = ast::ItemOrMacro>, 110 items: &mut Iterator<Item = ast::ItemOrMacro>,
120 ) { 111 ) {
@@ -123,21 +114,12 @@ impl LoweredModule {
123 for item in items { 114 for item in items {
124 match item { 115 match item {
125 ast::ItemOrMacro::Item(it) => { 116 ast::ItemOrMacro::Item(it) => {
126 self.add_def_id( 117 self.add_def_id(source_map, db, module, file_id, &file_items, it);
127 source_map,
128 db,
129 source_root_id,
130 module_id,
131 file_id,
132 &file_items,
133 it,
134 );
135 } 118 }
136 ast::ItemOrMacro::Macro(macro_call) => { 119 ast::ItemOrMacro::Macro(macro_call) => {
137 let item_id = file_items.id_of_unchecked(macro_call.syntax()); 120 let item_id = file_items.id_of_unchecked(macro_call.syntax());
138 let loc = MacroCallLoc { 121 let loc = MacroCallLoc {
139 source_root_id, 122 module,
140 module_id,
141 source_item_id: SourceItemId { 123 source_item_id: SourceItemId {
142 file_id, 124 file_id,
143 item_id: Some(item_id), 125 item_id: Some(item_id),
@@ -148,15 +130,7 @@ impl LoweredModule {
148 let file_items = db.file_items(file_id); 130 let file_items = db.file_items(file_id);
149 //FIXME: expand recursively 131 //FIXME: expand recursively
150 for item in db.hir_source_file(file_id).items() { 132 for item in db.hir_source_file(file_id).items() {
151 self.add_def_id( 133 self.add_def_id(source_map, db, module, file_id, &file_items, item);
152 source_map,
153 db,
154 source_root_id,
155 module_id,
156 file_id,
157 &file_items,
158 item,
159 );
160 } 134 }
161 } 135 }
162 } 136 }
@@ -167,8 +141,7 @@ impl LoweredModule {
167 &mut self, 141 &mut self,
168 source_map: &mut ImportSourceMap, 142 source_map: &mut ImportSourceMap,
169 db: &impl HirDatabase, 143 db: &impl HirDatabase,
170 source_root_id: SourceRootId, 144 module: Module,
171 module_id: ModuleId,
172 file_id: HirFileId, 145 file_id: HirFileId,
173 file_items: &SourceFileItems, 146 file_items: &SourceFileItems,
174 item: &ast::ModuleItem, 147 item: &ast::ModuleItem,
@@ -199,7 +172,7 @@ impl LoweredModule {
199 } 172 }
200 }; 173 };
201 if let Some(name) = name { 174 if let Some(name) = name {
202 let def_id = assign_def_id(db, source_root_id, module_id, file_id, file_items, item); 175 let def_id = assign_def_id(db, module, file_id, file_items, item);
203 self.declarations.insert(name.as_name(), def_id); 176 self.declarations.insert(name.as_name(), def_id);
204 } 177 }
205 } 178 }
@@ -219,12 +192,11 @@ impl LoweredModule {
219 192
220fn assign_def_id( 193fn assign_def_id(
221 db: &impl HirDatabase, 194 db: &impl HirDatabase,
222 source_root_id: SourceRootId, 195 module: Module,
223 module_id: ModuleId,
224 file_id: HirFileId, 196 file_id: HirFileId,
225 file_items: &SourceFileItems, 197 file_items: &SourceFileItems,
226 item: &ast::ModuleItem, 198 item: &ast::ModuleItem,
227) -> PerNs<DefId> { 199) -> PerNs<ModuleDef> {
228 // depending on the item kind, the location can define something in 200 // depending on the item kind, the location can define something in
229 // the values namespace, the types namespace, or both 201 // the values namespace, the types namespace, or both
230 let kind = DefKind::for_syntax_kind(item.syntax().kind()); 202 let kind = DefKind::for_syntax_kind(item.syntax().kind());
@@ -232,14 +204,13 @@ fn assign_def_id(
232 let item_id = file_items.id_of_unchecked(item.syntax()); 204 let item_id = file_items.id_of_unchecked(item.syntax());
233 let def_loc = DefLoc { 205 let def_loc = DefLoc {
234 kind: k, 206 kind: k,
235 source_root_id, 207 module,
236 module_id,
237 source_item_id: SourceItemId { 208 source_item_id: SourceItemId {
238 file_id, 209 file_id,
239 item_id: Some(item_id), 210 item_id: Some(item_id),
240 }, 211 },
241 }; 212 };
242 def_loc.id(db) 213 def_loc.id(db).into()
243 }); 214 });
244 def_id 215 def_id
245} 216}
@@ -248,7 +219,6 @@ impl DefKind {
248 fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> { 219 fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> {
249 match kind { 220 match kind {
250 SyntaxKind::FN_DEF => PerNs::values(DefKind::Function), 221 SyntaxKind::FN_DEF => PerNs::values(DefKind::Function),
251 SyntaxKind::MODULE => PerNs::types(DefKind::Module),
252 SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), 222 SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor),
253 SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), 223 SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum),
254 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), 224 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),