aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/lib.rs')
-rw-r--r--crates/ra_hir/src/lib.rs61
1 files changed, 30 insertions, 31 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index e7b6a81f4..9168dad3b 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -41,63 +41,58 @@ pub use self::{
41 41
42pub use self::function::FnSignatureInfo; 42pub use self::function::FnSignatureInfo;
43 43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 44/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
45pub struct FnId(u32); 45/// in a specific module.
46ra_db::impl_numeric_id!(FnId);
47
48impl FnId {
49 pub fn from_loc(
50 db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
51 loc: &SourceItemId,
52 ) -> FnId {
53 db.as_ref().loc2id(loc)
54 }
55 pub fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
56 db.as_ref().id2loc(self)
57 }
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
61pub struct DefId(u32); 47pub struct DefId(u32);
62ra_db::impl_numeric_id!(DefId); 48ra_db::impl_numeric_id!(DefId);
63 49
50#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
51pub(crate) enum DefKind {
52 Module,
53 Function,
54 Item,
55}
56
64#[derive(Clone, Debug, PartialEq, Eq, Hash)] 57#[derive(Clone, Debug, PartialEq, Eq, Hash)]
65pub enum DefLoc { 58pub struct DefLoc {
66 Module { 59 pub(crate) kind: DefKind,
67 id: ModuleId, 60 source_root_id: SourceRootId,
68 source_root: SourceRootId, 61 module_id: ModuleId,
69 }, 62 source_item_id: SourceItemId,
70 Item {
71 source_item_id: SourceItemId,
72 },
73} 63}
74 64
75impl DefId { 65impl DefId {
76 pub fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc { 66 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
77 db.as_ref().id2loc(self) 67 db.as_ref().id2loc(self)
78 } 68 }
79} 69}
80 70
81impl DefLoc { 71impl DefLoc {
82 pub fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId { 72 pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
83 db.as_ref().loc2id(&self) 73 db.as_ref().loc2id(&self)
84 } 74 }
85} 75}
86 76
87pub enum Def { 77pub enum Def {
88 Module(Module), 78 Module(Module),
79 Function(Function),
89 Item, 80 Item,
90} 81}
91 82
92impl DefId { 83impl DefId {
93 pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> { 84 pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
94 let loc = self.loc(db); 85 let loc = self.loc(db);
95 let res = match loc { 86 let res = match loc.kind {
96 DefLoc::Module { id, source_root } => { 87 DefKind::Module => {
97 let descr = Module::new(db, source_root, id)?; 88 let module = Module::new(db, loc.source_root_id, loc.module_id)?;
98 Def::Module(descr) 89 Def::Module(module)
99 } 90 }
100 DefLoc::Item { .. } => Def::Item, 91 DefKind::Function => {
92 let function = Function::new(self);
93 Def::Function(function)
94 }
95 DefKind::Item => Def::Item,
101 }; 96 };
102 Ok(res) 97 Ok(res)
103 } 98 }
@@ -131,6 +126,10 @@ impl SourceFileItems {
131 .unwrap(); 126 .unwrap();
132 id 127 id
133 } 128 }
129 pub fn id_of_source_file(&self) -> SourceFileItemId {
130 let (id, _syntax) = self.arena.iter().next().unwrap();
131 id
132 }
134} 133}
135 134
136impl Index<SourceFileItemId> for SourceFileItems { 135impl Index<SourceFileItemId> for SourceFileItems {