aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/hir/mod.rs')
-rw-r--r--crates/ra_analysis/src/hir/mod.rs131
1 files changed, 0 insertions, 131 deletions
diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs
deleted file mode 100644
index 83131384d..000000000
--- a/crates/ra_analysis/src/hir/mod.rs
+++ /dev/null
@@ -1,131 +0,0 @@
1//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust
2//! code.
3//!
4//! The principal difference between HIR and syntax trees is that HIR is bound
5//! to a particular crate instance. That is, it has cfg flags and features
6//! applied. So, there relation between syntax and HIR is many-to-one.
7
8pub(crate) mod db;
9mod query_definitions;
10mod function;
11mod module;
12mod path;
13
14use std::ops::Index;
15
16use ra_syntax::{SyntaxNodeRef, SyntaxNode};
17use ra_db::{LocationIntener, SourceRootId};
18
19use crate::{
20 FileId,
21 hir::db::HirDatabase,
22 Cancelable,
23 arena::{Arena, Id},
24};
25
26pub(crate) use self::{
27 path::{Path, PathKind},
28 module::{Module, ModuleId, Problem},
29 function::{Function, FnScopes},
30};
31
32pub use self::function::FnSignatureInfo;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35pub(crate) struct FnId(u32);
36ra_db::impl_numeric_id!(FnId);
37
38impl FnId {
39 pub(crate) fn from_loc(
40 db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
41 loc: &SourceItemId,
42 ) -> FnId {
43 db.as_ref().loc2id(loc)
44 }
45 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
46 db.as_ref().id2loc(self)
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub(crate) struct DefId(u32);
52ra_db::impl_numeric_id!(DefId);
53
54#[derive(Clone, Debug, PartialEq, Eq, Hash)]
55pub(crate) enum DefLoc {
56 Module {
57 id: ModuleId,
58 source_root: SourceRootId,
59 },
60 Item {
61 source_item_id: SourceItemId,
62 },
63}
64
65impl DefId {
66 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
67 db.as_ref().id2loc(self)
68 }
69}
70
71impl DefLoc {
72 pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
73 db.as_ref().loc2id(&self)
74 }
75}
76
77pub(crate) enum Def {
78 Module(Module),
79 Item,
80}
81
82impl DefId {
83 pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
84 let loc = self.loc(db);
85 let res = match loc {
86 DefLoc::Module { id, source_root } => {
87 let descr = Module::new(db, source_root, id)?;
88 Def::Module(descr)
89 }
90 DefLoc::Item { .. } => Def::Item,
91 };
92 Ok(res)
93 }
94}
95
96/// Identifier of item within a specific file. This is stable over reparses, so
97/// it's OK to use it as a salsa key/value.
98pub(crate) type SourceFileItemId = Id<SyntaxNode>;
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101pub(crate) struct SourceItemId {
102 file_id: FileId,
103 item_id: SourceFileItemId,
104}
105
106/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
107#[derive(Debug, PartialEq, Eq, Default)]
108pub(crate) struct SourceFileItems {
109 arena: Arena<SyntaxNode>,
110}
111
112impl SourceFileItems {
113 fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
114 self.arena.alloc(item)
115 }
116 fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId {
117 let (id, _item) = self
118 .arena
119 .iter()
120 .find(|(_id, i)| i.borrowed() == item)
121 .unwrap();
122 id
123 }
124}
125
126impl Index<SourceFileItemId> for SourceFileItems {
127 type Output = SyntaxNode;
128 fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
129 &self.arena[idx]
130 }
131}