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