diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 78 |
2 files changed, 33 insertions, 68 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 45199fa11..2b194f488 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -26,8 +26,7 @@ use crate::{ | |||
26 | path::{ModPath, PathKind}, | 26 | path::{ModPath, PathKind}, |
27 | per_ns::PerNs, | 27 | per_ns::PerNs, |
28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, | 28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, |
29 | LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, | 29 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
30 | TypeAliasLoc, UnionLoc, | ||
31 | }; | 30 | }; |
32 | 31 | ||
33 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { | 32 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { |
@@ -93,7 +92,7 @@ impl PartialResolvedImport { | |||
93 | #[derive(Clone, Debug, Eq, PartialEq)] | 92 | #[derive(Clone, Debug, Eq, PartialEq)] |
94 | struct ImportDirective { | 93 | struct ImportDirective { |
95 | module_id: LocalModuleId, | 94 | module_id: LocalModuleId, |
96 | import_id: LocalImportId, | 95 | import_id: raw::Import, |
97 | import: raw::ImportData, | 96 | import: raw::ImportData, |
98 | status: PartialResolvedImport, | 97 | status: PartialResolvedImport, |
99 | } | 98 | } |
@@ -110,7 +109,7 @@ struct MacroDirective { | |||
110 | struct DefCollector<'a, DB> { | 109 | struct DefCollector<'a, DB> { |
111 | db: &'a DB, | 110 | db: &'a DB, |
112 | def_map: CrateDefMap, | 111 | def_map: CrateDefMap, |
113 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, LocalImportId)>>, | 112 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::Import)>>, |
114 | unresolved_imports: Vec<ImportDirective>, | 113 | unresolved_imports: Vec<ImportDirective>, |
115 | resolved_imports: Vec<ImportDirective>, | 114 | resolved_imports: Vec<ImportDirective>, |
116 | unexpanded_macros: Vec<MacroDirective>, | 115 | unexpanded_macros: Vec<MacroDirective>, |
@@ -219,7 +218,7 @@ where | |||
219 | self.update( | 218 | self.update( |
220 | self.def_map.root, | 219 | self.def_map.root, |
221 | None, | 220 | None, |
222 | &[(name, Resolution { def: PerNs::macros(macro_), import: None })], | 221 | &[(name, Resolution { def: PerNs::macros(macro_), import: false })], |
223 | ); | 222 | ); |
224 | } | 223 | } |
225 | } | 224 | } |
@@ -404,7 +403,7 @@ where | |||
404 | let variant = EnumVariantId { parent: e, local_id }; | 403 | let variant = EnumVariantId { parent: e, local_id }; |
405 | let res = Resolution { | 404 | let res = Resolution { |
406 | def: PerNs::both(variant.into(), variant.into()), | 405 | def: PerNs::both(variant.into(), variant.into()), |
407 | import: Some(import_id), | 406 | import: true, |
408 | }; | 407 | }; |
409 | (name, res) | 408 | (name, res) |
410 | }) | 409 | }) |
@@ -431,7 +430,7 @@ where | |||
431 | } | 430 | } |
432 | } | 431 | } |
433 | 432 | ||
434 | let resolution = Resolution { def, import: Some(import_id) }; | 433 | let resolution = Resolution { def, import: true }; |
435 | self.update(module_id, Some(import_id), &[(name, resolution)]); | 434 | self.update(module_id, Some(import_id), &[(name, resolution)]); |
436 | } | 435 | } |
437 | None => tested_by!(bogus_paths), | 436 | None => tested_by!(bogus_paths), |
@@ -442,7 +441,7 @@ where | |||
442 | fn update( | 441 | fn update( |
443 | &mut self, | 442 | &mut self, |
444 | module_id: LocalModuleId, | 443 | module_id: LocalModuleId, |
445 | import: Option<LocalImportId>, | 444 | import: Option<raw::Import>, |
446 | resolutions: &[(Name, Resolution)], | 445 | resolutions: &[(Name, Resolution)], |
447 | ) { | 446 | ) { |
448 | self.update_recursive(module_id, import, resolutions, 0) | 447 | self.update_recursive(module_id, import, resolutions, 0) |
@@ -451,7 +450,7 @@ where | |||
451 | fn update_recursive( | 450 | fn update_recursive( |
452 | &mut self, | 451 | &mut self, |
453 | module_id: LocalModuleId, | 452 | module_id: LocalModuleId, |
454 | import: Option<LocalImportId>, | 453 | import: Option<raw::Import>, |
455 | resolutions: &[(Name, Resolution)], | 454 | resolutions: &[(Name, Resolution)], |
456 | depth: usize, | 455 | depth: usize, |
457 | ) { | 456 | ) { |
@@ -462,7 +461,7 @@ where | |||
462 | let scope = &mut self.def_map.modules[module_id].scope; | 461 | let scope = &mut self.def_map.modules[module_id].scope; |
463 | let mut changed = false; | 462 | let mut changed = false; |
464 | for (name, res) in resolutions { | 463 | for (name, res) in resolutions { |
465 | changed |= scope.push_res(name.clone(), res, import); | 464 | changed |= scope.push_res(name.clone(), res, import.is_some()); |
466 | } | 465 | } |
467 | 466 | ||
468 | if !changed { | 467 | if !changed { |
@@ -719,7 +718,7 @@ where | |||
719 | def: PerNs::types( | 718 | def: PerNs::types( |
720 | ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), | 719 | ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), |
721 | ), | 720 | ), |
722 | import: None, | 721 | import: false, |
723 | }; | 722 | }; |
724 | self.def_collector.update(self.module_id, None, &[(name, resolution)]); | 723 | self.def_collector.update(self.module_id, None, &[(name, resolution)]); |
725 | res | 724 | res |
@@ -791,7 +790,7 @@ where | |||
791 | PerNs::types(def.into()) | 790 | PerNs::types(def.into()) |
792 | } | 791 | } |
793 | }; | 792 | }; |
794 | let resolution = Resolution { def, import: None }; | 793 | let resolution = Resolution { def, import: false }; |
795 | self.def_collector.update(self.module_id, None, &[(name, resolution)]) | 794 | self.def_collector.update(self.module_id, None, &[(name, resolution)]) |
796 | } | 795 | } |
797 | 796 | ||
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index ecb4d7c03..73dc08745 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -7,24 +7,21 @@ | |||
7 | 7 | ||
8 | use std::{ops::Index, sync::Arc}; | 8 | use std::{ops::Index, sync::Arc}; |
9 | 9 | ||
10 | use either::Either; | ||
11 | use hir_expand::{ | 10 | use hir_expand::{ |
12 | ast_id_map::AstIdMap, | 11 | ast_id_map::AstIdMap, |
13 | db::AstDatabase, | 12 | db::AstDatabase, |
14 | hygiene::Hygiene, | 13 | hygiene::Hygiene, |
15 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
16 | }; | 15 | }; |
17 | use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; | 16 | use ra_arena::{impl_arena_id, Arena, RawId}; |
17 | use ra_prof::profile; | ||
18 | use ra_syntax::{ | 18 | use ra_syntax::{ |
19 | ast::{self, AttrsOwner, NameOwner}, | 19 | ast::{self, AttrsOwner, NameOwner}, |
20 | AstNode, AstPtr, | 20 | AstNode, |
21 | }; | 21 | }; |
22 | use test_utils::tested_by; | 22 | use test_utils::tested_by; |
23 | 23 | ||
24 | use crate::{ | 24 | use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile}; |
25 | attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile, | ||
26 | LocalImportId, | ||
27 | }; | ||
28 | 25 | ||
29 | /// `RawItems` is a set of top-level items in a file (except for impls). | 26 | /// `RawItems` is a set of top-level items in a file (except for impls). |
30 | /// | 27 | /// |
@@ -33,7 +30,7 @@ use crate::{ | |||
33 | #[derive(Debug, Default, PartialEq, Eq)] | 30 | #[derive(Debug, Default, PartialEq, Eq)] |
34 | pub struct RawItems { | 31 | pub struct RawItems { |
35 | modules: Arena<Module, ModuleData>, | 32 | modules: Arena<Module, ModuleData>, |
36 | imports: Arena<LocalImportId, ImportData>, | 33 | imports: Arena<Import, ImportData>, |
37 | defs: Arena<Def, DefData>, | 34 | defs: Arena<Def, DefData>, |
38 | macros: Arena<Macro, MacroData>, | 35 | macros: Arena<Macro, MacroData>, |
39 | impls: Arena<Impl, ImplData>, | 36 | impls: Arena<Impl, ImplData>, |
@@ -41,35 +38,15 @@ pub struct RawItems { | |||
41 | items: Vec<RawItem>, | 38 | items: Vec<RawItem>, |
42 | } | 39 | } |
43 | 40 | ||
44 | #[derive(Debug, Default, PartialEq, Eq)] | ||
45 | pub struct ImportSourceMap { | ||
46 | map: ArenaMap<LocalImportId, ImportSourcePtr>, | ||
47 | } | ||
48 | |||
49 | type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; | ||
50 | |||
51 | impl ImportSourceMap { | ||
52 | pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { | ||
53 | self.map[import].clone() | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl RawItems { | 41 | impl RawItems { |
58 | pub(crate) fn raw_items_query( | 42 | pub(crate) fn raw_items_query( |
59 | db: &(impl DefDatabase + AstDatabase), | 43 | db: &(impl DefDatabase + AstDatabase), |
60 | file_id: HirFileId, | 44 | file_id: HirFileId, |
61 | ) -> Arc<RawItems> { | 45 | ) -> Arc<RawItems> { |
62 | db.raw_items_with_source_map(file_id).0 | 46 | let _p = profile("raw_items_query"); |
63 | } | ||
64 | |||
65 | pub(crate) fn raw_items_with_source_map_query( | ||
66 | db: &(impl DefDatabase + AstDatabase), | ||
67 | file_id: HirFileId, | ||
68 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { | ||
69 | let mut collector = RawItemsCollector { | 47 | let mut collector = RawItemsCollector { |
70 | raw_items: RawItems::default(), | 48 | raw_items: RawItems::default(), |
71 | source_ast_id_map: db.ast_id_map(file_id), | 49 | source_ast_id_map: db.ast_id_map(file_id), |
72 | imports: Trace::new(), | ||
73 | file_id, | 50 | file_id, |
74 | hygiene: Hygiene::new(db, file_id), | 51 | hygiene: Hygiene::new(db, file_id), |
75 | }; | 52 | }; |
@@ -80,11 +57,8 @@ impl RawItems { | |||
80 | collector.process_module(None, item_list); | 57 | collector.process_module(None, item_list); |
81 | } | 58 | } |
82 | } | 59 | } |
83 | let mut raw_items = collector.raw_items; | 60 | let raw_items = collector.raw_items; |
84 | let (arena, map) = collector.imports.into_arena_and_map(); | 61 | Arc::new(raw_items) |
85 | raw_items.imports = arena; | ||
86 | let source_map = ImportSourceMap { map }; | ||
87 | (Arc::new(raw_items), Arc::new(source_map)) | ||
88 | } | 62 | } |
89 | 63 | ||
90 | pub(super) fn items(&self) -> &[RawItem] { | 64 | pub(super) fn items(&self) -> &[RawItem] { |
@@ -99,9 +73,9 @@ impl Index<Module> for RawItems { | |||
99 | } | 73 | } |
100 | } | 74 | } |
101 | 75 | ||
102 | impl Index<LocalImportId> for RawItems { | 76 | impl Index<Import> for RawItems { |
103 | type Output = ImportData; | 77 | type Output = ImportData; |
104 | fn index(&self, idx: LocalImportId) -> &ImportData { | 78 | fn index(&self, idx: Import) -> &ImportData { |
105 | &self.imports[idx] | 79 | &self.imports[idx] |
106 | } | 80 | } |
107 | } | 81 | } |
@@ -136,7 +110,7 @@ pub(super) struct RawItem { | |||
136 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 110 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
137 | pub(super) enum RawItemKind { | 111 | pub(super) enum RawItemKind { |
138 | Module(Module), | 112 | Module(Module), |
139 | Import(LocalImportId), | 113 | Import(Import), |
140 | Def(Def), | 114 | Def(Def), |
141 | Macro(Macro), | 115 | Macro(Macro), |
142 | Impl(Impl), | 116 | Impl(Impl), |
@@ -152,6 +126,10 @@ pub(super) enum ModuleData { | |||
152 | Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, | 126 | Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, |
153 | } | 127 | } |
154 | 128 | ||
129 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
130 | pub(crate) struct Import(RawId); | ||
131 | impl_arena_id!(Import); | ||
132 | |||
155 | #[derive(Debug, Clone, PartialEq, Eq)] | 133 | #[derive(Debug, Clone, PartialEq, Eq)] |
156 | pub struct ImportData { | 134 | pub struct ImportData { |
157 | pub(super) path: ModPath, | 135 | pub(super) path: ModPath, |
@@ -223,7 +201,6 @@ pub(super) struct ImplData { | |||
223 | 201 | ||
224 | struct RawItemsCollector { | 202 | struct RawItemsCollector { |
225 | raw_items: RawItems, | 203 | raw_items: RawItems, |
226 | imports: Trace<LocalImportId, ImportData, ImportSourcePtr>, | ||
227 | source_ast_id_map: Arc<AstIdMap>, | 204 | source_ast_id_map: Arc<AstIdMap>, |
228 | file_id: HirFileId, | 205 | file_id: HirFileId, |
229 | hygiene: Hygiene, | 206 | hygiene: Hygiene, |
@@ -330,7 +307,7 @@ impl RawItemsCollector { | |||
330 | ModPath::expand_use_item( | 307 | ModPath::expand_use_item( |
331 | InFile { value: use_item, file_id: self.file_id }, | 308 | InFile { value: use_item, file_id: self.file_id }, |
332 | &self.hygiene, | 309 | &self.hygiene, |
333 | |path, use_tree, is_glob, alias| { | 310 | |path, _use_tree, is_glob, alias| { |
334 | let import_data = ImportData { | 311 | let import_data = ImportData { |
335 | path, | 312 | path, |
336 | alias, | 313 | alias, |
@@ -339,11 +316,11 @@ impl RawItemsCollector { | |||
339 | is_extern_crate: false, | 316 | is_extern_crate: false, |
340 | is_macro_use: false, | 317 | is_macro_use: false, |
341 | }; | 318 | }; |
342 | buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); | 319 | buf.push(import_data); |
343 | }, | 320 | }, |
344 | ); | 321 | ); |
345 | for (import_data, ptr) in buf { | 322 | for import_data in buf { |
346 | self.push_import(current_module, attrs.clone(), import_data, ptr); | 323 | self.push_import(current_module, attrs.clone(), import_data); |
347 | } | 324 | } |
348 | } | 325 | } |
349 | 326 | ||
@@ -366,12 +343,7 @@ impl RawItemsCollector { | |||
366 | is_extern_crate: true, | 343 | is_extern_crate: true, |
367 | is_macro_use, | 344 | is_macro_use, |
368 | }; | 345 | }; |
369 | self.push_import( | 346 | self.push_import(current_module, attrs, import_data); |
370 | current_module, | ||
371 | attrs, | ||
372 | import_data, | ||
373 | Either::Right(AstPtr::new(&extern_crate)), | ||
374 | ); | ||
375 | } | 347 | } |
376 | } | 348 | } |
377 | 349 | ||
@@ -402,14 +374,8 @@ impl RawItemsCollector { | |||
402 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | 374 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) |
403 | } | 375 | } |
404 | 376 | ||
405 | fn push_import( | 377 | fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) { |
406 | &mut self, | 378 | let import = self.raw_items.imports.alloc(data); |
407 | current_module: Option<Module>, | ||
408 | attrs: Attrs, | ||
409 | data: ImportData, | ||
410 | source: ImportSourcePtr, | ||
411 | ) { | ||
412 | let import = self.imports.alloc(|| source, || data); | ||
413 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | 379 | self.push_item(current_module, attrs, RawItemKind::Import(import)) |
414 | } | 380 | } |
415 | 381 | ||