aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-03-26 15:57:57 +0000
committerAleksey Kladov <[email protected]>2019-03-26 15:57:57 +0000
commitb17217b34afbdbdc6b1d8ec480fcf06ec4bd587e (patch)
tree198b942a591d783dfcade9c1c6dc04920bafa9b5 /crates/ra_hir/src
parentcffa3f960e401ac8100fd450b32c336c93181959 (diff)
simplify
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/db.rs2
-rw-r--r--crates/ra_hir/src/ids.rs7
-rw-r--r--crates/ra_hir/src/source_id.rs33
3 files changed, 21 insertions, 21 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 3296b9b31..a7bbaefb8 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -22,7 +22,7 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
22 #[salsa::invoke(crate::ids::macro_def_query)] 22 #[salsa::invoke(crate::ids::macro_def_query)]
23 fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>; 23 fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
24 24
25 #[salsa::invoke(HirFileId::hir_parse)] 25 #[salsa::invoke(HirFileId::hir_parse_query)]
26 fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; 26 fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
27 27
28 #[salsa::invoke(crate::adt::StructData::struct_data_query)] 28 #[salsa::invoke(crate::adt::StructData::struct_data_query)]
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index c2df5ce00..81b3cfd22 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -82,7 +82,10 @@ impl HirFileId {
82 } 82 }
83 } 83 }
84 84
85 pub(crate) fn hir_parse(db: &impl DefDatabase, file_id: HirFileId) -> TreeArc<SourceFile> { 85 pub(crate) fn hir_parse_query(
86 db: &impl DefDatabase,
87 file_id: HirFileId,
88 ) -> TreeArc<SourceFile> {
86 match file_id.0 { 89 match file_id.0 {
87 HirFileIdRepr::File(file_id) => db.parse(file_id), 90 HirFileIdRepr::File(file_id) => db.parse(file_id),
88 HirFileIdRepr::Macro(macro_call_id) => { 91 HirFileIdRepr::Macro(macro_call_id) => {
@@ -122,7 +125,6 @@ impl From<MacroCallId> for HirFileId {
122} 125}
123 126
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
125
126pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); 128pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
127 129
128pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { 130pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
@@ -152,7 +154,6 @@ impl MacroCallId {
152} 154}
153 155
154impl MacroCallLoc { 156impl MacroCallLoc {
155 #[allow(unused)]
156 pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId { 157 pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId {
157 db.as_ref().macros.loc2id(&self) 158 db.as_ref().macros.loc2id(&self)
158 } 159 }
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs
index 04b7bb7b3..fb71417af 100644
--- a/crates/ra_hir/src/source_id.rs
+++ b/crates/ra_hir/src/source_id.rs
@@ -5,6 +5,7 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
5 5
6use crate::{HirFileId, DefDatabase}; 6use crate::{HirFileId, DefDatabase};
7 7
8/// `AstId` points to an AST node in any file
8#[derive(Debug)] 9#[derive(Debug)]
9pub(crate) struct AstId<N: AstNode> { 10pub(crate) struct AstId<N: AstNode> {
10 file_id: HirFileId, 11 file_id: HirFileId,
@@ -43,6 +44,7 @@ impl<N: AstNode> AstId<N> {
43 } 44 }
44} 45}
45 46
47/// `AstId` points to an AST node in a specific file.
46#[derive(Debug)] 48#[derive(Debug)]
47pub(crate) struct FileAstId<N: AstNode> { 49pub(crate) struct FileAstId<N: AstNode> {
48 raw: SourceFileItemId, 50 raw: SourceFileItemId,
@@ -89,7 +91,6 @@ pub struct SourceItemId {
89/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back. 91/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
90#[derive(Debug, PartialEq, Eq)] 92#[derive(Debug, PartialEq, Eq)]
91pub struct SourceFileItems { 93pub struct SourceFileItems {
92 file_id: HirFileId,
93 arena: Arena<SourceFileItemId, SyntaxNodePtr>, 94 arena: Arena<SourceFileItemId, SyntaxNodePtr>,
94} 95}
95 96
@@ -99,7 +100,7 @@ impl SourceFileItems {
99 file_id: HirFileId, 100 file_id: HirFileId,
100 ) -> Arc<SourceFileItems> { 101 ) -> Arc<SourceFileItems> {
101 let source_file = db.hir_parse(file_id); 102 let source_file = db.hir_parse(file_id);
102 Arc::new(SourceFileItems::from_source_file(&source_file, file_id)) 103 Arc::new(SourceFileItems::from_source_file(&source_file))
103 } 104 }
104 105
105 pub(crate) fn file_item_query( 106 pub(crate) fn file_item_query(
@@ -113,11 +114,21 @@ impl SourceFileItems {
113 } 114 }
114 115
115 pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> { 116 pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
116 FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } 117 let ptr = SyntaxNodePtr::new(item.syntax());
118 let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
119 Some((it, _)) => it,
120 None => panic!(
121 "Can't find {:?} in SourceFileItems:\n{:?}",
122 item.syntax(),
123 self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
124 ),
125 };
126
127 FileAstId { raw, _ty: PhantomData }
117 } 128 }
118 129
119 fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> SourceFileItems { 130 fn from_source_file(source_file: &SourceFile) -> SourceFileItems {
120 let mut res = SourceFileItems { file_id, arena: Arena::default() }; 131 let mut res = SourceFileItems { arena: Arena::default() };
121 // By walking the tree in bread-first order we make sure that parents 132 // By walking the tree in bread-first order we make sure that parents
122 // get lower ids then children. That is, adding a new child does not 133 // get lower ids then children. That is, adding a new child does not
123 // change parent's id. This means that, say, adding a new function to a 134 // change parent's id. This means that, say, adding a new function to a
@@ -135,18 +146,6 @@ impl SourceFileItems {
135 fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { 146 fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
136 self.arena.alloc(SyntaxNodePtr::new(item)) 147 self.arena.alloc(SyntaxNodePtr::new(item))
137 } 148 }
138
139 fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId {
140 let ptr = SyntaxNodePtr::new(item);
141 if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) {
142 return id;
143 }
144 panic!(
145 "Can't find {:?} in SourceFileItems:\n{:?}",
146 item,
147 self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
148 );
149 }
150} 149}
151 150
152/// Walks the subtree in bfs order, calling `f` for each node. 151/// Walks the subtree in bfs order, calling `f` for each node.