diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres/raw.rs')
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 105 |
1 files changed, 43 insertions, 62 deletions
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 6eb106094..73dc08745 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -10,21 +10,18 @@ use std::{ops::Index, sync::Arc}; | |||
10 | use hir_expand::{ | 10 | use hir_expand::{ |
11 | ast_id_map::AstIdMap, | 11 | ast_id_map::AstIdMap, |
12 | db::AstDatabase, | 12 | db::AstDatabase, |
13 | either::Either, | ||
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::Path, trace::Trace, FileAstId, HirFileId, LocalImportId, | ||
26 | Source, | ||
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,9 +126,13 @@ 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: Path, | 135 | pub(super) path: ModPath, |
158 | pub(super) alias: Option<Name>, | 136 | pub(super) alias: Option<Name>, |
159 | pub(super) is_glob: bool, | 137 | pub(super) is_glob: bool, |
160 | pub(super) is_prelude: bool, | 138 | pub(super) is_prelude: bool, |
@@ -184,6 +162,21 @@ pub(super) enum DefKind { | |||
184 | TypeAlias(FileAstId<ast::TypeAliasDef>), | 162 | TypeAlias(FileAstId<ast::TypeAliasDef>), |
185 | } | 163 | } |
186 | 164 | ||
165 | impl DefKind { | ||
166 | pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> { | ||
167 | match self { | ||
168 | DefKind::Function(it) => it.upcast(), | ||
169 | DefKind::Struct(it) => it.upcast(), | ||
170 | DefKind::Union(it) => it.upcast(), | ||
171 | DefKind::Enum(it) => it.upcast(), | ||
172 | DefKind::Const(it) => it.upcast(), | ||
173 | DefKind::Static(it) => it.upcast(), | ||
174 | DefKind::Trait(it) => it.upcast(), | ||
175 | DefKind::TypeAlias(it) => it.upcast(), | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | |||
187 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 180 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
188 | pub(super) struct Macro(RawId); | 181 | pub(super) struct Macro(RawId); |
189 | impl_arena_id!(Macro); | 182 | impl_arena_id!(Macro); |
@@ -191,7 +184,7 @@ impl_arena_id!(Macro); | |||
191 | #[derive(Debug, PartialEq, Eq)] | 184 | #[derive(Debug, PartialEq, Eq)] |
192 | pub(super) struct MacroData { | 185 | pub(super) struct MacroData { |
193 | pub(super) ast_id: FileAstId<ast::MacroCall>, | 186 | pub(super) ast_id: FileAstId<ast::MacroCall>, |
194 | pub(super) path: Path, | 187 | pub(super) path: ModPath, |
195 | pub(super) name: Option<Name>, | 188 | pub(super) name: Option<Name>, |
196 | pub(super) export: bool, | 189 | pub(super) export: bool, |
197 | pub(super) builtin: bool, | 190 | pub(super) builtin: bool, |
@@ -208,7 +201,6 @@ pub(super) struct ImplData { | |||
208 | 201 | ||
209 | struct RawItemsCollector { | 202 | struct RawItemsCollector { |
210 | raw_items: RawItems, | 203 | raw_items: RawItems, |
211 | imports: Trace<LocalImportId, ImportData, ImportSourcePtr>, | ||
212 | source_ast_id_map: Arc<AstIdMap>, | 204 | source_ast_id_map: Arc<AstIdMap>, |
213 | file_id: HirFileId, | 205 | file_id: HirFileId, |
214 | hygiene: Hygiene, | 206 | hygiene: Hygiene, |
@@ -312,10 +304,10 @@ impl RawItemsCollector { | |||
312 | let attrs = self.parse_attrs(&use_item); | 304 | let attrs = self.parse_attrs(&use_item); |
313 | 305 | ||
314 | let mut buf = Vec::new(); | 306 | let mut buf = Vec::new(); |
315 | Path::expand_use_item( | 307 | ModPath::expand_use_item( |
316 | Source { value: use_item, file_id: self.file_id }, | 308 | InFile { value: use_item, file_id: self.file_id }, |
317 | &self.hygiene, | 309 | &self.hygiene, |
318 | |path, use_tree, is_glob, alias| { | 310 | |path, _use_tree, is_glob, alias| { |
319 | let import_data = ImportData { | 311 | let import_data = ImportData { |
320 | path, | 312 | path, |
321 | alias, | 313 | alias, |
@@ -324,11 +316,11 @@ impl RawItemsCollector { | |||
324 | is_extern_crate: false, | 316 | is_extern_crate: false, |
325 | is_macro_use: false, | 317 | is_macro_use: false, |
326 | }; | 318 | }; |
327 | buf.push((import_data, Either::A(AstPtr::new(use_tree)))); | 319 | buf.push(import_data); |
328 | }, | 320 | }, |
329 | ); | 321 | ); |
330 | for (import_data, ptr) in buf { | 322 | for import_data in buf { |
331 | self.push_import(current_module, attrs.clone(), import_data, ptr); | 323 | self.push_import(current_module, attrs.clone(), import_data); |
332 | } | 324 | } |
333 | } | 325 | } |
334 | 326 | ||
@@ -338,7 +330,7 @@ impl RawItemsCollector { | |||
338 | extern_crate: ast::ExternCrateItem, | 330 | extern_crate: ast::ExternCrateItem, |
339 | ) { | 331 | ) { |
340 | if let Some(name_ref) = extern_crate.name_ref() { | 332 | if let Some(name_ref) = extern_crate.name_ref() { |
341 | let path = Path::from_name_ref(&name_ref); | 333 | let path = ModPath::from_name_ref(&name_ref); |
342 | let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); | 334 | let alias = extern_crate.alias().and_then(|a| a.name()).map(|it| it.as_name()); |
343 | let attrs = self.parse_attrs(&extern_crate); | 335 | let attrs = self.parse_attrs(&extern_crate); |
344 | // FIXME: cfg_attr | 336 | // FIXME: cfg_attr |
@@ -351,18 +343,13 @@ impl RawItemsCollector { | |||
351 | is_extern_crate: true, | 343 | is_extern_crate: true, |
352 | is_macro_use, | 344 | is_macro_use, |
353 | }; | 345 | }; |
354 | self.push_import( | 346 | self.push_import(current_module, attrs, import_data); |
355 | current_module, | ||
356 | attrs, | ||
357 | import_data, | ||
358 | Either::B(AstPtr::new(&extern_crate)), | ||
359 | ); | ||
360 | } | 347 | } |
361 | } | 348 | } |
362 | 349 | ||
363 | fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) { | 350 | fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) { |
364 | let attrs = self.parse_attrs(&m); | 351 | let attrs = self.parse_attrs(&m); |
365 | let path = match m.path().and_then(|path| Path::from_src(path, &self.hygiene)) { | 352 | let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { |
366 | Some(it) => it, | 353 | Some(it) => it, |
367 | _ => return, | 354 | _ => return, |
368 | }; | 355 | }; |
@@ -387,14 +374,8 @@ impl RawItemsCollector { | |||
387 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | 374 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) |
388 | } | 375 | } |
389 | 376 | ||
390 | fn push_import( | 377 | fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) { |
391 | &mut self, | 378 | let import = self.raw_items.imports.alloc(data); |
392 | current_module: Option<Module>, | ||
393 | attrs: Attrs, | ||
394 | data: ImportData, | ||
395 | source: ImportSourcePtr, | ||
396 | ) { | ||
397 | let import = self.imports.alloc(|| source, || data); | ||
398 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | 379 | self.push_item(current_module, attrs, RawItemKind::Import(import)) |
399 | } | 380 | } |
400 | 381 | ||