aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ids.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ids.rs')
-rw-r--r--crates/ra_hir/src/ids.rs167
1 files changed, 3 insertions, 164 deletions
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index dea288eb7..fe083c0c6 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -5,16 +5,12 @@
5//! This module defines a bunch of ids we are using. The most important ones are 5//! This module defines a bunch of ids we are using. The most important ones are
6//! probably `HirFileId` and `DefId`. 6//! probably `HirFileId` and `DefId`.
7 7
8use std::hash::{Hash, Hasher};
9
10use ra_db::salsa; 8use ra_db::salsa;
11use ra_syntax::{ast, AstNode};
12 9
13use crate::{ 10pub use hir_def::{
14 db::{AstDatabase, InternDatabase}, 11 AstItemDef, ConstId, EnumId, FunctionId, ItemLoc, LocationCtx, StaticId, StructId, TraitId,
15 AstId, FileAstId, Module, Source, 12 TypeAliasId,
16}; 13};
17
18pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind}; 14pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind};
19 15
20macro_rules! impl_intern_key { 16macro_rules! impl_intern_key {
@@ -30,163 +26,6 @@ macro_rules! impl_intern_key {
30 }; 26 };
31} 27}
32 28
33#[derive(Debug)]
34pub struct ItemLoc<N: AstNode> {
35 pub(crate) module: Module,
36 ast_id: AstId<N>,
37}
38
39impl<N: AstNode> PartialEq for ItemLoc<N> {
40 fn eq(&self, other: &Self) -> bool {
41 self.module == other.module && self.ast_id == other.ast_id
42 }
43}
44impl<N: AstNode> Eq for ItemLoc<N> {}
45impl<N: AstNode> Hash for ItemLoc<N> {
46 fn hash<H: Hasher>(&self, hasher: &mut H) {
47 self.module.hash(hasher);
48 self.ast_id.hash(hasher);
49 }
50}
51
52impl<N: AstNode> Clone for ItemLoc<N> {
53 fn clone(&self) -> ItemLoc<N> {
54 ItemLoc { module: self.module, ast_id: self.ast_id }
55 }
56}
57
58#[derive(Clone, Copy)]
59pub(crate) struct LocationCtx<DB> {
60 db: DB,
61 module: Module,
62 file_id: HirFileId,
63}
64
65impl<'a, DB> LocationCtx<&'a DB> {
66 pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
67 LocationCtx { db, module, file_id }
68 }
69}
70
71impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> {
72 pub(crate) fn to_def<N, DEF>(self, ast: &N) -> DEF
73 where
74 N: AstNode,
75 DEF: AstItemDef<N>,
76 {
77 DEF::from_ast(self, ast)
78 }
79}
80
81pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
82 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
83 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
84
85 fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self {
86 let items = ctx.db.ast_id_map(ctx.file_id);
87 let item_id = items.ast_id(ast);
88 Self::from_ast_id(ctx, item_id)
89 }
90 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
91 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
92 Self::intern(ctx.db, loc)
93 }
94 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source<N> {
95 let loc = self.lookup_intern(db);
96 let ast = loc.ast_id.to_node(db);
97 Source { file_id: loc.ast_id.file_id(), ast }
98 }
99 fn module(self, db: &impl InternDatabase) -> Module {
100 let loc = self.lookup_intern(db);
101 loc.module
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub struct FunctionId(salsa::InternId);
107impl_intern_key!(FunctionId);
108
109impl AstItemDef<ast::FnDef> for FunctionId {
110 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
111 db.intern_function(loc)
112 }
113 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
114 db.lookup_intern_function(self)
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
119pub struct StructId(salsa::InternId);
120impl_intern_key!(StructId);
121impl AstItemDef<ast::StructDef> for StructId {
122 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
123 db.intern_struct(loc)
124 }
125 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
126 db.lookup_intern_struct(self)
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
131pub struct EnumId(salsa::InternId);
132impl_intern_key!(EnumId);
133impl AstItemDef<ast::EnumDef> for EnumId {
134 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
135 db.intern_enum(loc)
136 }
137 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
138 db.lookup_intern_enum(self)
139 }
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub struct ConstId(salsa::InternId);
144impl_intern_key!(ConstId);
145impl AstItemDef<ast::ConstDef> for ConstId {
146 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
147 db.intern_const(loc)
148 }
149 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
150 db.lookup_intern_const(self)
151 }
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
155pub struct StaticId(salsa::InternId);
156impl_intern_key!(StaticId);
157impl AstItemDef<ast::StaticDef> for StaticId {
158 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
159 db.intern_static(loc)
160 }
161 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
162 db.lookup_intern_static(self)
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
167pub struct TraitId(salsa::InternId);
168impl_intern_key!(TraitId);
169impl AstItemDef<ast::TraitDef> for TraitId {
170 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
171 db.intern_trait(loc)
172 }
173 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
174 db.lookup_intern_trait(self)
175 }
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
179pub struct TypeAliasId(salsa::InternId);
180impl_intern_key!(TypeAliasId);
181impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
182 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
183 db.intern_type_alias(loc)
184 }
185 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
186 db.lookup_intern_type_alias(self)
187 }
188}
189
190/// This exists just for Chalk, because Chalk just has a single `StructId` where 29/// This exists just for Chalk, because Chalk just has a single `StructId` where
191/// we have different kinds of ADTs, primitive types and special type 30/// we have different kinds of ADTs, primitive types and special type
192/// constructors like tuples and function pointers. 31/// constructors like tuples and function pointers.