diff options
Diffstat (limited to 'crates/hir/src/from_id.rs')
-rw-r--r-- | crates/hir/src/from_id.rs | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs new file mode 100644 index 000000000..a53ac1e08 --- /dev/null +++ b/crates/hir/src/from_id.rs | |||
@@ -0,0 +1,247 @@ | |||
1 | //! Utility module for converting between hir_def ids and code_model wrappers. | ||
2 | //! | ||
3 | //! It's unclear if we need this long-term, but it's definitelly useful while we | ||
4 | //! are splitting the hir. | ||
5 | |||
6 | use hir_def::{ | ||
7 | expr::PatId, AdtId, AssocItemId, AttrDefId, DefWithBodyId, EnumVariantId, FieldId, | ||
8 | GenericDefId, ModuleDefId, VariantId, | ||
9 | }; | ||
10 | |||
11 | use crate::{ | ||
12 | code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, Field, GenericDef, | ||
13 | Local, MacroDef, ModuleDef, VariantDef, | ||
14 | }; | ||
15 | |||
16 | macro_rules! from_id { | ||
17 | ($(($id:path, $ty:path)),*) => {$( | ||
18 | impl From<$id> for $ty { | ||
19 | fn from(id: $id) -> $ty { | ||
20 | $ty { id } | ||
21 | } | ||
22 | } | ||
23 | impl From<$ty> for $id { | ||
24 | fn from(ty: $ty) -> $id { | ||
25 | ty.id | ||
26 | } | ||
27 | } | ||
28 | )*} | ||
29 | } | ||
30 | |||
31 | from_id![ | ||
32 | (base_db::CrateId, crate::Crate), | ||
33 | (hir_def::ModuleId, crate::Module), | ||
34 | (hir_def::StructId, crate::Struct), | ||
35 | (hir_def::UnionId, crate::Union), | ||
36 | (hir_def::EnumId, crate::Enum), | ||
37 | (hir_def::TypeAliasId, crate::TypeAlias), | ||
38 | (hir_def::TraitId, crate::Trait), | ||
39 | (hir_def::StaticId, crate::Static), | ||
40 | (hir_def::ConstId, crate::Const), | ||
41 | (hir_def::FunctionId, crate::Function), | ||
42 | (hir_def::ImplId, crate::ImplDef), | ||
43 | (hir_def::TypeParamId, crate::TypeParam), | ||
44 | (hir_expand::MacroDefId, crate::MacroDef) | ||
45 | ]; | ||
46 | |||
47 | impl From<AdtId> for Adt { | ||
48 | fn from(id: AdtId) -> Self { | ||
49 | match id { | ||
50 | AdtId::StructId(it) => Adt::Struct(it.into()), | ||
51 | AdtId::UnionId(it) => Adt::Union(it.into()), | ||
52 | AdtId::EnumId(it) => Adt::Enum(it.into()), | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl From<Adt> for AdtId { | ||
58 | fn from(id: Adt) -> Self { | ||
59 | match id { | ||
60 | Adt::Struct(it) => AdtId::StructId(it.id), | ||
61 | Adt::Union(it) => AdtId::UnionId(it.id), | ||
62 | Adt::Enum(it) => AdtId::EnumId(it.id), | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | impl From<EnumVariantId> for EnumVariant { | ||
68 | fn from(id: EnumVariantId) -> Self { | ||
69 | EnumVariant { parent: id.parent.into(), id: id.local_id } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | impl From<EnumVariant> for EnumVariantId { | ||
74 | fn from(def: EnumVariant) -> Self { | ||
75 | EnumVariantId { parent: def.parent.id, local_id: def.id } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | impl From<ModuleDefId> for ModuleDef { | ||
80 | fn from(id: ModuleDefId) -> Self { | ||
81 | match id { | ||
82 | ModuleDefId::ModuleId(it) => ModuleDef::Module(it.into()), | ||
83 | ModuleDefId::FunctionId(it) => ModuleDef::Function(it.into()), | ||
84 | ModuleDefId::AdtId(it) => ModuleDef::Adt(it.into()), | ||
85 | ModuleDefId::EnumVariantId(it) => ModuleDef::EnumVariant(it.into()), | ||
86 | ModuleDefId::ConstId(it) => ModuleDef::Const(it.into()), | ||
87 | ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()), | ||
88 | ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()), | ||
89 | ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()), | ||
90 | ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it), | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | impl From<ModuleDef> for ModuleDefId { | ||
96 | fn from(id: ModuleDef) -> Self { | ||
97 | match id { | ||
98 | ModuleDef::Module(it) => ModuleDefId::ModuleId(it.into()), | ||
99 | ModuleDef::Function(it) => ModuleDefId::FunctionId(it.into()), | ||
100 | ModuleDef::Adt(it) => ModuleDefId::AdtId(it.into()), | ||
101 | ModuleDef::EnumVariant(it) => ModuleDefId::EnumVariantId(it.into()), | ||
102 | ModuleDef::Const(it) => ModuleDefId::ConstId(it.into()), | ||
103 | ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()), | ||
104 | ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()), | ||
105 | ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()), | ||
106 | ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it), | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | |||
111 | impl From<DefWithBody> for DefWithBodyId { | ||
112 | fn from(def: DefWithBody) -> Self { | ||
113 | match def { | ||
114 | DefWithBody::Function(it) => DefWithBodyId::FunctionId(it.id), | ||
115 | DefWithBody::Static(it) => DefWithBodyId::StaticId(it.id), | ||
116 | DefWithBody::Const(it) => DefWithBodyId::ConstId(it.id), | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | impl From<DefWithBodyId> for DefWithBody { | ||
122 | fn from(def: DefWithBodyId) -> Self { | ||
123 | match def { | ||
124 | DefWithBodyId::FunctionId(it) => DefWithBody::Function(it.into()), | ||
125 | DefWithBodyId::StaticId(it) => DefWithBody::Static(it.into()), | ||
126 | DefWithBodyId::ConstId(it) => DefWithBody::Const(it.into()), | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | |||
131 | impl From<AssocItemId> for AssocItem { | ||
132 | fn from(def: AssocItemId) -> Self { | ||
133 | match def { | ||
134 | AssocItemId::FunctionId(it) => AssocItem::Function(it.into()), | ||
135 | AssocItemId::TypeAliasId(it) => AssocItem::TypeAlias(it.into()), | ||
136 | AssocItemId::ConstId(it) => AssocItem::Const(it.into()), | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | impl From<GenericDef> for GenericDefId { | ||
142 | fn from(def: GenericDef) -> Self { | ||
143 | match def { | ||
144 | GenericDef::Function(it) => GenericDefId::FunctionId(it.id), | ||
145 | GenericDef::Adt(it) => GenericDefId::AdtId(it.into()), | ||
146 | GenericDef::Trait(it) => GenericDefId::TraitId(it.id), | ||
147 | GenericDef::TypeAlias(it) => GenericDefId::TypeAliasId(it.id), | ||
148 | GenericDef::ImplDef(it) => GenericDefId::ImplId(it.id), | ||
149 | GenericDef::EnumVariant(it) => { | ||
150 | GenericDefId::EnumVariantId(EnumVariantId { parent: it.parent.id, local_id: it.id }) | ||
151 | } | ||
152 | GenericDef::Const(it) => GenericDefId::ConstId(it.id), | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl From<Adt> for GenericDefId { | ||
158 | fn from(id: Adt) -> Self { | ||
159 | match id { | ||
160 | Adt::Struct(it) => it.id.into(), | ||
161 | Adt::Union(it) => it.id.into(), | ||
162 | Adt::Enum(it) => it.id.into(), | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | impl From<VariantId> for VariantDef { | ||
168 | fn from(def: VariantId) -> Self { | ||
169 | match def { | ||
170 | VariantId::StructId(it) => VariantDef::Struct(it.into()), | ||
171 | VariantId::EnumVariantId(it) => VariantDef::EnumVariant(it.into()), | ||
172 | VariantId::UnionId(it) => VariantDef::Union(it.into()), | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | impl From<VariantDef> for VariantId { | ||
178 | fn from(def: VariantDef) -> Self { | ||
179 | match def { | ||
180 | VariantDef::Struct(it) => VariantId::StructId(it.id), | ||
181 | VariantDef::EnumVariant(it) => VariantId::EnumVariantId(it.into()), | ||
182 | VariantDef::Union(it) => VariantId::UnionId(it.id), | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | |||
187 | impl From<Field> for FieldId { | ||
188 | fn from(def: Field) -> Self { | ||
189 | FieldId { parent: def.parent.into(), local_id: def.id } | ||
190 | } | ||
191 | } | ||
192 | |||
193 | impl From<FieldId> for Field { | ||
194 | fn from(def: FieldId) -> Self { | ||
195 | Field { parent: def.parent.into(), id: def.local_id } | ||
196 | } | ||
197 | } | ||
198 | |||
199 | impl From<AttrDef> for AttrDefId { | ||
200 | fn from(def: AttrDef) -> Self { | ||
201 | match def { | ||
202 | AttrDef::Module(it) => AttrDefId::ModuleId(it.id), | ||
203 | AttrDef::Field(it) => AttrDefId::FieldId(it.into()), | ||
204 | AttrDef::Adt(it) => AttrDefId::AdtId(it.into()), | ||
205 | AttrDef::Function(it) => AttrDefId::FunctionId(it.id), | ||
206 | AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()), | ||
207 | AttrDef::Static(it) => AttrDefId::StaticId(it.id), | ||
208 | AttrDef::Const(it) => AttrDefId::ConstId(it.id), | ||
209 | AttrDef::Trait(it) => AttrDefId::TraitId(it.id), | ||
210 | AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id), | ||
211 | AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id), | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | |||
216 | impl From<AssocItem> for GenericDefId { | ||
217 | fn from(item: AssocItem) -> Self { | ||
218 | match item { | ||
219 | AssocItem::Function(f) => f.id.into(), | ||
220 | AssocItem::Const(c) => c.id.into(), | ||
221 | AssocItem::TypeAlias(t) => t.id.into(), | ||
222 | } | ||
223 | } | ||
224 | } | ||
225 | |||
226 | impl From<(DefWithBodyId, PatId)> for Local { | ||
227 | fn from((parent, pat_id): (DefWithBodyId, PatId)) -> Self { | ||
228 | Local { parent, pat_id } | ||
229 | } | ||
230 | } | ||
231 | |||
232 | impl From<MacroDef> for ItemInNs { | ||
233 | fn from(macro_def: MacroDef) -> Self { | ||
234 | ItemInNs::Macros(macro_def.into()) | ||
235 | } | ||
236 | } | ||
237 | |||
238 | impl From<ModuleDef> for ItemInNs { | ||
239 | fn from(module_def: ModuleDef) -> Self { | ||
240 | match module_def { | ||
241 | ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { | ||
242 | ItemInNs::Values(module_def.into()) | ||
243 | } | ||
244 | _ => ItemInNs::Types(module_def.into()), | ||
245 | } | ||
246 | } | ||
247 | } | ||