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.rs99
1 files changed, 80 insertions, 19 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index af2a717c9..564434ccc 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -25,6 +25,8 @@ pub mod item_scope;
25pub mod dyn_map; 25pub mod dyn_map;
26pub mod keys; 26pub mod keys;
27 27
28pub mod item_tree;
29
28pub mod adt; 30pub mod adt;
29pub mod data; 31pub mod data;
30pub mod generics; 32pub mod generics;
@@ -48,7 +50,7 @@ pub mod import_map;
48#[cfg(test)] 50#[cfg(test)]
49mod test_db; 51mod test_db;
50 52
51use std::hash::Hash; 53use std::hash::{Hash, Hasher};
52 54
53use hir_expand::{ 55use hir_expand::{
54 ast_id_map::FileAstId, eager::expand_eager_macro, hygiene::Hygiene, AstId, HirFileId, InFile, 56 ast_id_map::FileAstId, eager::expand_eager_macro, hygiene::Hygiene, AstId, HirFileId, InFile,
@@ -56,10 +58,13 @@ use hir_expand::{
56}; 58};
57use ra_arena::Idx; 59use ra_arena::Idx;
58use ra_db::{impl_intern_key, salsa, CrateId}; 60use ra_db::{impl_intern_key, salsa, CrateId};
59use ra_syntax::{ast, AstNode}; 61use ra_syntax::ast;
60 62
61use crate::body::Expander;
62use crate::builtin_type::BuiltinType; 63use crate::builtin_type::BuiltinType;
64use item_tree::{
65 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
66 TypeAlias, Union,
67};
63 68
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub struct ModuleId { 70pub struct ModuleId {
@@ -70,16 +75,62 @@ pub struct ModuleId {
70/// An ID of a module, **local** to a specific crate 75/// An ID of a module, **local** to a specific crate
71pub type LocalModuleId = Idx<nameres::ModuleData>; 76pub type LocalModuleId = Idx<nameres::ModuleData>;
72 77
73#[derive(Debug, Clone, PartialEq, Eq, Hash)] 78#[derive(Debug)]
74pub struct ItemLoc<N: AstNode> { 79pub struct ItemLoc<N: ItemTreeNode> {
75 pub container: ContainerId, 80 pub container: ContainerId,
76 pub ast_id: AstId<N>, 81 pub id: ItemTreeId<N>,
82}
83
84impl<N: ItemTreeNode> Clone for ItemLoc<N> {
85 fn clone(&self) -> Self {
86 Self { container: self.container, id: self.id }
87 }
77} 88}
78 89
79#[derive(Debug, Clone, PartialEq, Eq, Hash)] 90impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
80pub struct AssocItemLoc<N: AstNode> { 91
92impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
93 fn eq(&self, other: &Self) -> bool {
94 self.container == other.container && self.id == other.id
95 }
96}
97
98impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
99
100impl<N: ItemTreeNode> Hash for ItemLoc<N> {
101 fn hash<H: Hasher>(&self, state: &mut H) {
102 self.container.hash(state);
103 self.id.hash(state);
104 }
105}
106
107#[derive(Debug)]
108pub struct AssocItemLoc<N: ItemTreeNode> {
81 pub container: AssocContainerId, 109 pub container: AssocContainerId,
82 pub ast_id: AstId<N>, 110 pub id: ItemTreeId<N>,
111}
112
113impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
114 fn clone(&self) -> Self {
115 Self { container: self.container, id: self.id }
116 }
117}
118
119impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
120
121impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
122 fn eq(&self, other: &Self) -> bool {
123 self.container == other.container && self.id == other.id
124 }
125}
126
127impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
128
129impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
130 fn hash<H: Hasher>(&self, state: &mut H) {
131 self.container.hash(state);
132 self.id.hash(state);
133 }
83} 134}
84 135
85macro_rules! impl_intern { 136macro_rules! impl_intern {
@@ -104,22 +155,22 @@ macro_rules! impl_intern {
104 155
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub struct FunctionId(salsa::InternId); 157pub struct FunctionId(salsa::InternId);
107type FunctionLoc = AssocItemLoc<ast::FnDef>; 158type FunctionLoc = AssocItemLoc<Function>;
108impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function); 159impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
109 160
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
111pub struct StructId(salsa::InternId); 162pub struct StructId(salsa::InternId);
112type StructLoc = ItemLoc<ast::StructDef>; 163type StructLoc = ItemLoc<Struct>;
113impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct); 164impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
114 165
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
116pub struct UnionId(salsa::InternId); 167pub struct UnionId(salsa::InternId);
117pub type UnionLoc = ItemLoc<ast::UnionDef>; 168pub type UnionLoc = ItemLoc<Union>;
118impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union); 169impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
119 170
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 171#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
121pub struct EnumId(salsa::InternId); 172pub struct EnumId(salsa::InternId);
122pub type EnumLoc = ItemLoc<ast::EnumDef>; 173pub type EnumLoc = ItemLoc<Enum>;
123impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum); 174impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
124 175
125// FIXME: rename to `VariantId`, only enums can ave variants 176// FIXME: rename to `VariantId`, only enums can ave variants
@@ -141,27 +192,27 @@ pub type LocalFieldId = Idx<adt::FieldData>;
141 192
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 193#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub struct ConstId(salsa::InternId); 194pub struct ConstId(salsa::InternId);
144type ConstLoc = AssocItemLoc<ast::ConstDef>; 195type ConstLoc = AssocItemLoc<Const>;
145impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const); 196impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
146 197
147#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 198#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
148pub struct StaticId(salsa::InternId); 199pub struct StaticId(salsa::InternId);
149pub type StaticLoc = ItemLoc<ast::StaticDef>; 200pub type StaticLoc = ItemLoc<Static>;
150impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static); 201impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
151 202
152#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 203#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
153pub struct TraitId(salsa::InternId); 204pub struct TraitId(salsa::InternId);
154pub type TraitLoc = ItemLoc<ast::TraitDef>; 205pub type TraitLoc = ItemLoc<Trait>;
155impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait); 206impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
156 207
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
158pub struct TypeAliasId(salsa::InternId); 209pub struct TypeAliasId(salsa::InternId);
159type TypeAliasLoc = AssocItemLoc<ast::TypeAliasDef>; 210type TypeAliasLoc = AssocItemLoc<TypeAlias>;
160impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias); 211impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
161 212
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] 213#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
163pub struct ImplId(salsa::InternId); 214pub struct ImplId(salsa::InternId);
164type ImplLoc = ItemLoc<ast::ImplDef>; 215type ImplLoc = ItemLoc<Impl>;
165impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); 216impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
166 217
167#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 218#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -365,7 +416,7 @@ impl HasModule for AssocContainerId {
365 } 416 }
366} 417}
367 418
368impl<N: AstNode> HasModule for AssocItemLoc<N> { 419impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
369 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { 420 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
370 self.container.module(db) 421 self.container.module(db)
371 } 422 }
@@ -392,6 +443,16 @@ impl HasModule for DefWithBodyId {
392 } 443 }
393} 444}
394 445
446impl DefWithBodyId {
447 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
448 match self {
449 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
450 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
451 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
452 }
453 }
454}
455
395impl HasModule for GenericDefId { 456impl HasModule for GenericDefId {
396 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId { 457 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
397 match self { 458 match self {