diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 163 |
1 files changed, 73 insertions, 90 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index d579f5c7e..1d195d65d 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -13,103 +13,48 @@ pub mod path; | |||
13 | pub mod type_ref; | 13 | pub mod type_ref; |
14 | pub mod builtin_type; | 14 | pub mod builtin_type; |
15 | pub mod adt; | 15 | pub mod adt; |
16 | pub mod impls; | ||
17 | pub mod diagnostics; | 16 | pub mod diagnostics; |
18 | pub mod expr; | 17 | pub mod expr; |
19 | pub mod body; | 18 | pub mod body; |
20 | pub mod generics; | 19 | pub mod generics; |
21 | pub mod traits; | ||
22 | pub mod resolver; | 20 | pub mod resolver; |
21 | pub mod data; | ||
22 | pub mod lang_item; | ||
23 | pub mod docs; | ||
24 | pub mod per_ns; | ||
25 | |||
26 | mod trace; | ||
27 | mod nameres; | ||
23 | 28 | ||
24 | #[cfg(test)] | 29 | #[cfg(test)] |
25 | mod test_db; | 30 | mod test_db; |
26 | #[cfg(test)] | 31 | #[cfg(test)] |
27 | mod marks; | 32 | mod marks; |
28 | 33 | ||
29 | // FIXME: this should be private | ||
30 | pub mod nameres; | ||
31 | |||
32 | use std::hash::{Hash, Hasher}; | 34 | use std::hash::{Hash, Hasher}; |
33 | 35 | ||
34 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, Source}; | 36 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source}; |
35 | use ra_arena::{impl_arena_id, RawId}; | 37 | use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; |
36 | use ra_db::{salsa, CrateId, FileId}; | 38 | use ra_db::{salsa, CrateId}; |
37 | use ra_syntax::{ast, AstNode, SyntaxNode}; | 39 | use ra_syntax::{ast, AstNode}; |
38 | 40 | ||
39 | use crate::{builtin_type::BuiltinType, db::InternDatabase}; | 41 | use crate::{builtin_type::BuiltinType, db::InternDatabase}; |
40 | 42 | ||
41 | pub enum ModuleSource { | 43 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
42 | SourceFile(ast::SourceFile), | 44 | pub struct LocalImportId(RawId); |
43 | Module(ast::Module), | 45 | impl_arena_id!(LocalImportId); |
44 | } | ||
45 | |||
46 | impl ModuleSource { | ||
47 | pub fn new( | ||
48 | db: &impl db::DefDatabase2, | ||
49 | file_id: Option<FileId>, | ||
50 | decl_id: Option<AstId<ast::Module>>, | ||
51 | ) -> ModuleSource { | ||
52 | match (file_id, decl_id) { | ||
53 | (Some(file_id), _) => { | ||
54 | let source_file = db.parse(file_id).tree(); | ||
55 | ModuleSource::SourceFile(source_file) | ||
56 | } | ||
57 | (None, Some(item_id)) => { | ||
58 | let module = item_id.to_node(db); | ||
59 | assert!(module.item_list().is_some(), "expected inline module"); | ||
60 | ModuleSource::Module(module) | ||
61 | } | ||
62 | (None, None) => panic!(), | ||
63 | } | ||
64 | } | ||
65 | |||
66 | // FIXME: this methods do not belong here | ||
67 | pub fn from_position( | ||
68 | db: &impl db::DefDatabase2, | ||
69 | position: ra_db::FilePosition, | ||
70 | ) -> ModuleSource { | ||
71 | let parse = db.parse(position.file_id); | ||
72 | match &ra_syntax::algo::find_node_at_offset::<ast::Module>( | ||
73 | parse.tree().syntax(), | ||
74 | position.offset, | ||
75 | ) { | ||
76 | Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), | ||
77 | _ => { | ||
78 | let source_file = parse.tree(); | ||
79 | ModuleSource::SourceFile(source_file) | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | pub fn from_child_node(db: &impl db::DefDatabase2, child: Source<&SyntaxNode>) -> ModuleSource { | ||
85 | if let Some(m) = | ||
86 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) | ||
87 | { | ||
88 | ModuleSource::Module(m) | ||
89 | } else { | ||
90 | let file_id = child.file_id.original_file(db); | ||
91 | let source_file = db.parse(file_id).tree(); | ||
92 | ModuleSource::SourceFile(source_file) | ||
93 | } | ||
94 | } | ||
95 | |||
96 | pub fn from_file_id(db: &impl db::DefDatabase2, file_id: FileId) -> ModuleSource { | ||
97 | let source_file = db.parse(file_id).tree(); | ||
98 | ModuleSource::SourceFile(source_file) | ||
99 | } | ||
100 | } | ||
101 | 46 | ||
102 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 47 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
103 | pub struct ModuleId { | 48 | pub struct ModuleId { |
104 | pub krate: CrateId, | 49 | pub krate: CrateId, |
105 | pub module_id: CrateModuleId, | 50 | pub module_id: LocalModuleId, |
106 | } | 51 | } |
107 | 52 | ||
108 | /// An ID of a module, **local** to a specific crate | 53 | /// An ID of a module, **local** to a specific crate |
109 | // FIXME: rename to `LocalModuleId`. | 54 | // FIXME: rename to `LocalModuleId`. |
110 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 55 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
111 | pub struct CrateModuleId(RawId); | 56 | pub struct LocalModuleId(RawId); |
112 | impl_arena_id!(CrateModuleId); | 57 | impl_arena_id!(LocalModuleId); |
113 | 58 | ||
114 | macro_rules! impl_intern_key { | 59 | macro_rules! impl_intern_key { |
115 | ($name:ident) => { | 60 | ($name:ident) => { |
@@ -208,14 +153,14 @@ pub struct FunctionLoc { | |||
208 | 153 | ||
209 | impl Intern for FunctionLoc { | 154 | impl Intern for FunctionLoc { |
210 | type ID = FunctionId; | 155 | type ID = FunctionId; |
211 | fn intern(self, db: &impl db::DefDatabase2) -> FunctionId { | 156 | fn intern(self, db: &impl db::DefDatabase) -> FunctionId { |
212 | db.intern_function(self) | 157 | db.intern_function(self) |
213 | } | 158 | } |
214 | } | 159 | } |
215 | 160 | ||
216 | impl Lookup for FunctionId { | 161 | impl Lookup for FunctionId { |
217 | type Data = FunctionLoc; | 162 | type Data = FunctionLoc; |
218 | fn lookup(&self, db: &impl db::DefDatabase2) -> FunctionLoc { | 163 | fn lookup(&self, db: &impl db::DefDatabase) -> FunctionLoc { |
219 | db.lookup_intern_function(*self) | 164 | db.lookup_intern_function(*self) |
220 | } | 165 | } |
221 | } | 166 | } |
@@ -279,8 +224,8 @@ pub enum VariantId { | |||
279 | 224 | ||
280 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 225 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
281 | pub struct StructFieldId { | 226 | pub struct StructFieldId { |
282 | parent: VariantId, | 227 | pub parent: VariantId, |
283 | local_id: LocalStructFieldId, | 228 | pub local_id: LocalStructFieldId, |
284 | } | 229 | } |
285 | 230 | ||
286 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 231 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -298,14 +243,14 @@ pub struct ConstLoc { | |||
298 | 243 | ||
299 | impl Intern for ConstLoc { | 244 | impl Intern for ConstLoc { |
300 | type ID = ConstId; | 245 | type ID = ConstId; |
301 | fn intern(self, db: &impl db::DefDatabase2) -> ConstId { | 246 | fn intern(self, db: &impl db::DefDatabase) -> ConstId { |
302 | db.intern_const(self) | 247 | db.intern_const(self) |
303 | } | 248 | } |
304 | } | 249 | } |
305 | 250 | ||
306 | impl Lookup for ConstId { | 251 | impl Lookup for ConstId { |
307 | type Data = ConstLoc; | 252 | type Data = ConstLoc; |
308 | fn lookup(&self, db: &impl db::DefDatabase2) -> ConstLoc { | 253 | fn lookup(&self, db: &impl db::DefDatabase) -> ConstLoc { |
309 | db.lookup_intern_const(*self) | 254 | db.lookup_intern_const(*self) |
310 | } | 255 | } |
311 | } | 256 | } |
@@ -346,14 +291,14 @@ pub struct TypeAliasLoc { | |||
346 | 291 | ||
347 | impl Intern for TypeAliasLoc { | 292 | impl Intern for TypeAliasLoc { |
348 | type ID = TypeAliasId; | 293 | type ID = TypeAliasId; |
349 | fn intern(self, db: &impl db::DefDatabase2) -> TypeAliasId { | 294 | fn intern(self, db: &impl db::DefDatabase) -> TypeAliasId { |
350 | db.intern_type_alias(self) | 295 | db.intern_type_alias(self) |
351 | } | 296 | } |
352 | } | 297 | } |
353 | 298 | ||
354 | impl Lookup for TypeAliasId { | 299 | impl Lookup for TypeAliasId { |
355 | type Data = TypeAliasLoc; | 300 | type Data = TypeAliasLoc; |
356 | fn lookup(&self, db: &impl db::DefDatabase2) -> TypeAliasLoc { | 301 | fn lookup(&self, db: &impl db::DefDatabase) -> TypeAliasLoc { |
357 | db.lookup_intern_type_alias(*self) | 302 | db.lookup_intern_type_alias(*self) |
358 | } | 303 | } |
359 | } | 304 | } |
@@ -476,22 +421,51 @@ impl_froms!( | |||
476 | ConstId | 421 | ConstId |
477 | ); | 422 | ); |
478 | 423 | ||
424 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
425 | pub enum AttrDefId { | ||
426 | ModuleId(ModuleId), | ||
427 | StructFieldId(StructFieldId), | ||
428 | AdtId(AdtId), | ||
429 | FunctionId(FunctionId), | ||
430 | EnumVariantId(EnumVariantId), | ||
431 | StaticId(StaticId), | ||
432 | ConstId(ConstId), | ||
433 | TraitId(TraitId), | ||
434 | TypeAliasId(TypeAliasId), | ||
435 | MacroDefId(MacroDefId), | ||
436 | ImplId(ImplId), | ||
437 | } | ||
438 | |||
439 | impl_froms!( | ||
440 | AttrDefId: ModuleId, | ||
441 | StructFieldId, | ||
442 | AdtId(StructId, EnumId, UnionId), | ||
443 | EnumVariantId, | ||
444 | StaticId, | ||
445 | ConstId, | ||
446 | FunctionId, | ||
447 | TraitId, | ||
448 | TypeAliasId, | ||
449 | MacroDefId, | ||
450 | ImplId | ||
451 | ); | ||
452 | |||
479 | trait Intern { | 453 | trait Intern { |
480 | type ID; | 454 | type ID; |
481 | fn intern(self, db: &impl db::DefDatabase2) -> Self::ID; | 455 | fn intern(self, db: &impl db::DefDatabase) -> Self::ID; |
482 | } | 456 | } |
483 | 457 | ||
484 | pub trait Lookup { | 458 | pub trait Lookup { |
485 | type Data; | 459 | type Data; |
486 | fn lookup(&self, db: &impl db::DefDatabase2) -> Self::Data; | 460 | fn lookup(&self, db: &impl db::DefDatabase) -> Self::Data; |
487 | } | 461 | } |
488 | 462 | ||
489 | pub trait HasModule { | 463 | pub trait HasModule { |
490 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId; | 464 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId; |
491 | } | 465 | } |
492 | 466 | ||
493 | impl HasModule for FunctionLoc { | 467 | impl HasModule for FunctionLoc { |
494 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | 468 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
495 | match self.container { | 469 | match self.container { |
496 | ContainerId::ModuleId(it) => it, | 470 | ContainerId::ModuleId(it) => it, |
497 | ContainerId::ImplId(it) => it.module(db), | 471 | ContainerId::ImplId(it) => it.module(db), |
@@ -501,7 +475,7 @@ impl HasModule for FunctionLoc { | |||
501 | } | 475 | } |
502 | 476 | ||
503 | impl HasModule for TypeAliasLoc { | 477 | impl HasModule for TypeAliasLoc { |
504 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | 478 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
505 | match self.container { | 479 | match self.container { |
506 | ContainerId::ModuleId(it) => it, | 480 | ContainerId::ModuleId(it) => it, |
507 | ContainerId::ImplId(it) => it.module(db), | 481 | ContainerId::ImplId(it) => it.module(db), |
@@ -511,7 +485,7 @@ impl HasModule for TypeAliasLoc { | |||
511 | } | 485 | } |
512 | 486 | ||
513 | impl HasModule for ConstLoc { | 487 | impl HasModule for ConstLoc { |
514 | fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { | 488 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
515 | match self.container { | 489 | match self.container { |
516 | ContainerId::ModuleId(it) => it, | 490 | ContainerId::ModuleId(it) => it, |
517 | ContainerId::ImplId(it) => it.module(db), | 491 | ContainerId::ImplId(it) => it.module(db), |
@@ -522,13 +496,13 @@ impl HasModule for ConstLoc { | |||
522 | 496 | ||
523 | pub trait HasSource { | 497 | pub trait HasSource { |
524 | type Value; | 498 | type Value; |
525 | fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>; | 499 | fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>; |
526 | } | 500 | } |
527 | 501 | ||
528 | impl HasSource for FunctionLoc { | 502 | impl HasSource for FunctionLoc { |
529 | type Value = ast::FnDef; | 503 | type Value = ast::FnDef; |
530 | 504 | ||
531 | fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::FnDef> { | 505 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::FnDef> { |
532 | let node = self.ast_id.to_node(db); | 506 | let node = self.ast_id.to_node(db); |
533 | Source::new(self.ast_id.file_id(), node) | 507 | Source::new(self.ast_id.file_id(), node) |
534 | } | 508 | } |
@@ -537,7 +511,7 @@ impl HasSource for FunctionLoc { | |||
537 | impl HasSource for TypeAliasLoc { | 511 | impl HasSource for TypeAliasLoc { |
538 | type Value = ast::TypeAliasDef; | 512 | type Value = ast::TypeAliasDef; |
539 | 513 | ||
540 | fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::TypeAliasDef> { | 514 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::TypeAliasDef> { |
541 | let node = self.ast_id.to_node(db); | 515 | let node = self.ast_id.to_node(db); |
542 | Source::new(self.ast_id.file_id(), node) | 516 | Source::new(self.ast_id.file_id(), node) |
543 | } | 517 | } |
@@ -546,8 +520,17 @@ impl HasSource for TypeAliasLoc { | |||
546 | impl HasSource for ConstLoc { | 520 | impl HasSource for ConstLoc { |
547 | type Value = ast::ConstDef; | 521 | type Value = ast::ConstDef; |
548 | 522 | ||
549 | fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::ConstDef> { | 523 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::ConstDef> { |
550 | let node = self.ast_id.to_node(db); | 524 | let node = self.ast_id.to_node(db); |
551 | Source::new(self.ast_id.file_id(), node) | 525 | Source::new(self.ast_id.file_id(), node) |
552 | } | 526 | } |
553 | } | 527 | } |
528 | |||
529 | pub trait HasChildSource { | ||
530 | type ChildId; | ||
531 | type Value; | ||
532 | fn child_source( | ||
533 | &self, | ||
534 | db: &impl db::DefDatabase, | ||
535 | ) -> Source<ArenaMap<Self::ChildId, Self::Value>>; | ||
536 | } | ||