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.rs283
1 files changed, 74 insertions, 209 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index f085bbe87..8ed1599ff 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -45,7 +45,7 @@ use std::hash::Hash;
45use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; 45use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId};
46use ra_arena::{impl_arena_id, RawId}; 46use ra_arena::{impl_arena_id, RawId};
47use ra_db::{impl_intern_key, salsa, CrateId}; 47use ra_db::{impl_intern_key, salsa, CrateId};
48use ra_syntax::ast; 48use ra_syntax::{ast, AstNode};
49 49
50use crate::builtin_type::BuiltinType; 50use crate::builtin_type::BuiltinType;
51 51
@@ -65,101 +65,57 @@ pub struct ModuleId {
65pub struct LocalModuleId(RawId); 65pub struct LocalModuleId(RawId);
66impl_arena_id!(LocalModuleId); 66impl_arena_id!(LocalModuleId);
67 67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
69pub struct FunctionId(salsa::InternId);
70impl_intern_key!(FunctionId);
71
72#[derive(Debug, Clone, PartialEq, Eq, Hash)] 68#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct FunctionLoc { 69pub struct ItemLoc<N: AstNode> {
74 pub container: ContainerId, 70 pub container: ContainerId,
75 pub ast_id: AstId<ast::FnDef>, 71 pub ast_id: AstId<N>,
76} 72}
77 73
78impl Intern for FunctionLoc { 74#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79 type ID = FunctionId; 75pub struct AssocItemLoc<N: AstNode> {
80 fn intern(self, db: &impl db::DefDatabase) -> FunctionId { 76 pub container: AssocContainerId,
81 db.intern_function(self) 77 pub ast_id: AstId<N>,
82 }
83} 78}
84 79
85impl Lookup for FunctionId { 80macro_rules! impl_intern {
86 type Data = FunctionLoc; 81 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
87 fn lookup(&self, db: &impl db::DefDatabase) -> FunctionLoc { 82 impl_intern_key!($id);
88 db.lookup_intern_function(*self)
89 }
90}
91 83
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 84 impl Intern for $loc {
93pub struct StructId(salsa::InternId); 85 type ID = $id;
94impl_intern_key!(StructId); 86 fn intern(self, db: &impl db::DefDatabase) -> $id {
87 db.$intern(self)
88 }
89 }
95 90
96#[derive(Debug, Clone, PartialEq, Eq, Hash)] 91 impl Lookup for $id {
97pub struct StructLoc { 92 type Data = $loc;
98 pub container: ModuleId, 93 fn lookup(&self, db: &impl db::DefDatabase) -> $loc {
99 pub ast_id: AstId<ast::StructDef>, 94 db.$lookup(*self)
95 }
96 }
97 };
100} 98}
101 99
102impl Intern for StructLoc { 100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103 type ID = StructId; 101pub struct FunctionId(salsa::InternId);
104 fn intern(self, db: &impl db::DefDatabase) -> StructId { 102type FunctionLoc = AssocItemLoc<ast::FnDef>;
105 db.intern_struct(self) 103impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
106 }
107}
108 104
109impl Lookup for StructId { 105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
110 type Data = StructLoc; 106pub struct StructId(salsa::InternId);
111 fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc { 107type StructLoc = ItemLoc<ast::StructDef>;
112 db.lookup_intern_struct(*self) 108impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
113 }
114}
115 109
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
117pub struct UnionId(salsa::InternId); 111pub struct UnionId(salsa::InternId);
118impl_intern_key!(UnionId); 112pub type UnionLoc = ItemLoc<ast::UnionDef>;
119 113impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
121pub struct UnionLoc {
122 pub container: ModuleId,
123 pub ast_id: AstId<ast::UnionDef>,
124}
125
126impl Intern for UnionLoc {
127 type ID = UnionId;
128 fn intern(self, db: &impl db::DefDatabase) -> UnionId {
129 db.intern_union(self)
130 }
131}
132
133impl Lookup for UnionId {
134 type Data = UnionLoc;
135 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
136 db.lookup_intern_union(*self)
137 }
138}
139 114
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 115#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub struct EnumId(salsa::InternId); 116pub struct EnumId(salsa::InternId);
142impl_intern_key!(EnumId); 117pub type EnumLoc = ItemLoc<ast::EnumDef>;
143 118impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
145pub struct EnumLoc {
146 pub container: ModuleId,
147 pub ast_id: AstId<ast::EnumDef>,
148}
149
150impl Intern for EnumLoc {
151 type ID = EnumId;
152 fn intern(self, db: &impl db::DefDatabase) -> EnumId {
153 db.intern_enum(self)
154 }
155}
156
157impl Lookup for EnumId {
158 type Data = EnumLoc;
159 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
160 db.lookup_intern_enum(*self)
161 }
162}
163 119
164// FIXME: rename to `VariantId`, only enums can ave variants 120// FIXME: rename to `VariantId`, only enums can ave variants
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -184,122 +140,38 @@ impl_arena_id!(LocalStructFieldId);
184 140
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 141#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
186pub struct ConstId(salsa::InternId); 142pub struct ConstId(salsa::InternId);
187impl_intern_key!(ConstId); 143type ConstLoc = AssocItemLoc<ast::ConstDef>;
188#[derive(Debug, Clone, PartialEq, Eq, Hash)] 144impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
189pub struct ConstLoc {
190 pub container: ContainerId,
191 pub ast_id: AstId<ast::ConstDef>,
192}
193
194impl Intern for ConstLoc {
195 type ID = ConstId;
196 fn intern(self, db: &impl db::DefDatabase) -> ConstId {
197 db.intern_const(self)
198 }
199}
200
201impl Lookup for ConstId {
202 type Data = ConstLoc;
203 fn lookup(&self, db: &impl db::DefDatabase) -> ConstLoc {
204 db.lookup_intern_const(*self)
205 }
206}
207 145
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
209pub struct StaticId(salsa::InternId); 147pub struct StaticId(salsa::InternId);
210impl_intern_key!(StaticId); 148pub type StaticLoc = ItemLoc<ast::StaticDef>;
211 149impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
212#[derive(Debug, Clone, PartialEq, Eq, Hash)]
213pub struct StaticLoc {
214 pub container: ModuleId,
215 pub ast_id: AstId<ast::StaticDef>,
216}
217
218impl Intern for StaticLoc {
219 type ID = StaticId;
220 fn intern(self, db: &impl db::DefDatabase) -> StaticId {
221 db.intern_static(self)
222 }
223}
224
225impl Lookup for StaticId {
226 type Data = StaticLoc;
227 fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc {
228 db.lookup_intern_static(*self)
229 }
230}
231 150
232#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
233pub struct TraitId(salsa::InternId); 152pub struct TraitId(salsa::InternId);
234impl_intern_key!(TraitId); 153pub type TraitLoc = ItemLoc<ast::TraitDef>;
235 154impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
237pub struct TraitLoc {
238 pub container: ModuleId,
239 pub ast_id: AstId<ast::TraitDef>,
240}
241
242impl Intern for TraitLoc {
243 type ID = TraitId;
244 fn intern(self, db: &impl db::DefDatabase) -> TraitId {
245 db.intern_trait(self)
246 }
247}
248
249impl Lookup for TraitId {
250 type Data = TraitLoc;
251 fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
252 db.lookup_intern_trait(*self)
253 }
254}
255 155
256#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
257pub struct TypeAliasId(salsa::InternId); 157pub struct TypeAliasId(salsa::InternId);
258impl_intern_key!(TypeAliasId); 158type TypeAliasLoc = AssocItemLoc<ast::TypeAliasDef>;
259 159impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
260#[derive(Debug, Clone, PartialEq, Eq, Hash)]
261pub struct TypeAliasLoc {
262 pub container: ContainerId,
263 pub ast_id: AstId<ast::TypeAliasDef>,
264}
265
266impl Intern for TypeAliasLoc {
267 type ID = TypeAliasId;
268 fn intern(self, db: &impl db::DefDatabase) -> TypeAliasId {
269 db.intern_type_alias(self)
270 }
271}
272
273impl Lookup for TypeAliasId {
274 type Data = TypeAliasLoc;
275 fn lookup(&self, db: &impl db::DefDatabase) -> TypeAliasLoc {
276 db.lookup_intern_type_alias(*self)
277 }
278}
279 160
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
281pub struct ImplId(salsa::InternId); 162pub struct ImplId(salsa::InternId);
282impl_intern_key!(ImplId); 163type ImplLoc = ItemLoc<ast::ImplBlock>;
283 164impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
284#[derive(Debug, Clone, PartialEq, Eq, Hash)]
285pub struct ImplLoc {
286 pub container: ModuleId,
287 pub ast_id: AstId<ast::ImplBlock>,
288}
289 165
290impl Intern for ImplLoc { 166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
291 type ID = ImplId; 167pub struct TypeParamId {
292 fn intern(self, db: &impl db::DefDatabase) -> ImplId { 168 pub parent: GenericDefId,
293 db.intern_impl(self) 169 pub local_id: LocalTypeParamId,
294 }
295} 170}
296 171
297impl Lookup for ImplId { 172#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298 type Data = ImplLoc; 173pub struct LocalTypeParamId(RawId);
299 fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { 174impl_arena_id!(LocalTypeParamId);
300 db.lookup_intern_impl(*self)
301 }
302}
303 175
304macro_rules! impl_froms { 176macro_rules! impl_froms {
305 ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { 177 ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => {
@@ -321,21 +193,18 @@ macro_rules! impl_froms {
321} 193}
322 194
323#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
324pub struct TypeParamId { 196pub enum ContainerId {
325 pub parent: GenericDefId, 197 ModuleId(ModuleId),
326 pub local_id: LocalTypeParamId, 198 DefWithBodyId(DefWithBodyId),
327} 199}
328 200
329#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 201#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
330pub struct LocalTypeParamId(RawId); 202pub enum AssocContainerId {
331impl_arena_id!(LocalTypeParamId); 203 ContainerId(ContainerId),
332
333#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
334pub enum ContainerId {
335 ModuleId(ModuleId),
336 ImplId(ImplId), 204 ImplId(ImplId),
337 TraitId(TraitId), 205 TraitId(TraitId),
338} 206}
207impl_froms!(AssocContainerId: ContainerId);
339 208
340/// A Data Type 209/// A Data Type
341#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 210#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -478,33 +347,28 @@ pub trait HasModule {
478 fn module(&self, db: &impl db::DefDatabase) -> ModuleId; 347 fn module(&self, db: &impl db::DefDatabase) -> ModuleId;
479} 348}
480 349
481impl HasModule for FunctionLoc { 350impl HasModule for ContainerId {
482 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 351 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
483 match self.container { 352 match *self {
484 ContainerId::ModuleId(it) => it, 353 ContainerId::ModuleId(it) => it,
485 ContainerId::ImplId(it) => it.lookup(db).container, 354 ContainerId::DefWithBodyId(it) => it.module(db),
486 ContainerId::TraitId(it) => it.lookup(db).container,
487 } 355 }
488 } 356 }
489} 357}
490 358
491impl HasModule for TypeAliasLoc { 359impl HasModule for AssocContainerId {
492 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 360 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
493 match self.container { 361 match *self {
494 ContainerId::ModuleId(it) => it, 362 AssocContainerId::ContainerId(it) => it.module(db),
495 ContainerId::ImplId(it) => it.lookup(db).container, 363 AssocContainerId::ImplId(it) => it.lookup(db).container.module(db),
496 ContainerId::TraitId(it) => it.lookup(db).container, 364 AssocContainerId::TraitId(it) => it.lookup(db).container.module(db),
497 } 365 }
498 } 366 }
499} 367}
500 368
501impl HasModule for ConstLoc { 369impl<N: AstNode> HasModule for AssocItemLoc<N> {
502 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 370 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
503 match self.container { 371 self.container.module(db)
504 ContainerId::ModuleId(it) => it,
505 ContainerId::ImplId(it) => it.lookup(db).container,
506 ContainerId::TraitId(it) => it.lookup(db).container,
507 }
508 } 372 }
509} 373}
510 374
@@ -515,6 +379,7 @@ impl HasModule for AdtId {
515 AdtId::UnionId(it) => it.lookup(db).container, 379 AdtId::UnionId(it) => it.lookup(db).container,
516 AdtId::EnumId(it) => it.lookup(db).container, 380 AdtId::EnumId(it) => it.lookup(db).container,
517 } 381 }
382 .module(db)
518 } 383 }
519} 384}
520 385
@@ -533,17 +398,17 @@ impl HasModule for GenericDefId {
533 match self { 398 match self {
534 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 399 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
535 GenericDefId::AdtId(it) => it.module(db), 400 GenericDefId::AdtId(it) => it.module(db),
536 GenericDefId::TraitId(it) => it.lookup(db).container, 401 GenericDefId::TraitId(it) => it.lookup(db).container.module(db),
537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 402 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
538 GenericDefId::ImplId(it) => it.lookup(db).container, 403 GenericDefId::ImplId(it) => it.lookup(db).container.module(db),
539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, 404 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
540 GenericDefId::ConstId(it) => it.lookup(db).module(db), 405 GenericDefId::ConstId(it) => it.lookup(db).module(db),
541 } 406 }
542 } 407 }
543} 408}
544 409
545impl HasModule for StaticLoc { 410impl HasModule for StaticLoc {
546 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { 411 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
547 self.container 412 self.container.module(db)
548 } 413 }
549} 414}