aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/item_tree
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/item_tree')
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs46
1 files changed, 27 insertions, 19 deletions
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index 733fcac7a..7d28fe7c6 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -66,6 +66,10 @@ impl Ctx {
66 self.tree 66 self.tree
67 } 67 }
68 68
69 fn data(&mut self) -> &mut ItemTreeData {
70 self.tree.data_mut()
71 }
72
69 fn lower_mod_item(&mut self, item: &ast::ModuleItem, inner: bool) -> Option<ModItems> { 73 fn lower_mod_item(&mut self, item: &ast::ModuleItem, inner: bool) -> Option<ModItems> {
70 assert!(inner || self.inner_items.is_empty()); 74 assert!(inner || self.inner_items.is_empty());
71 75
@@ -124,7 +128,7 @@ impl Ctx {
124 } 128 }
125 129
126 fn add_attrs(&mut self, item: ModItem, attrs: Attrs) { 130 fn add_attrs(&mut self, item: ModItem, attrs: Attrs) {
127 match self.tree.attrs.entry(item) { 131 match self.tree.attrs.entry(AttrOwner::ModItem(item)) {
128 Entry::Occupied(mut entry) => { 132 Entry::Occupied(mut entry) => {
129 *entry.get_mut() = entry.get().merge(attrs); 133 *entry.get_mut() = entry.get().merge(attrs);
130 } 134 }
@@ -169,7 +173,7 @@ impl Ctx {
169 ast::StructKind::Unit => StructDefKind::Unit, 173 ast::StructKind::Unit => StructDefKind::Unit,
170 }; 174 };
171 let res = Struct { name, visibility, generic_params, fields, ast_id, kind }; 175 let res = Struct { name, visibility, generic_params, fields, ast_id, kind };
172 Some(id(self.tree.structs.alloc(res))) 176 Some(id(self.data().structs.alloc(res)))
173 } 177 }
174 178
175 fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields { 179 fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
@@ -190,7 +194,7 @@ impl Ctx {
190 let start = self.next_field_idx(); 194 let start = self.next_field_idx();
191 for field in fields.fields() { 195 for field in fields.fields() {
192 if let Some(data) = self.lower_record_field(&field) { 196 if let Some(data) = self.lower_record_field(&field) {
193 self.tree.fields.alloc(data); 197 self.data().fields.alloc(data);
194 } 198 }
195 } 199 }
196 let end = self.next_field_idx(); 200 let end = self.next_field_idx();
@@ -209,7 +213,7 @@ impl Ctx {
209 let start = self.next_field_idx(); 213 let start = self.next_field_idx();
210 for (i, field) in fields.fields().enumerate() { 214 for (i, field) in fields.fields().enumerate() {
211 if let Some(data) = self.lower_tuple_field(i, &field) { 215 if let Some(data) = self.lower_tuple_field(i, &field) {
212 self.tree.fields.alloc(data); 216 self.data().fields.alloc(data);
213 } 217 }
214 } 218 }
215 let end = self.next_field_idx(); 219 let end = self.next_field_idx();
@@ -236,7 +240,7 @@ impl Ctx {
236 }; 240 };
237 let ast_id = self.source_ast_id_map.ast_id(union); 241 let ast_id = self.source_ast_id_map.ast_id(union);
238 let res = Union { name, visibility, generic_params, fields, ast_id }; 242 let res = Union { name, visibility, generic_params, fields, ast_id };
239 Some(id(self.tree.unions.alloc(res))) 243 Some(id(self.data().unions.alloc(res)))
240 } 244 }
241 245
242 fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option<FileItemTreeId<Enum>> { 246 fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option<FileItemTreeId<Enum>> {
@@ -249,14 +253,14 @@ impl Ctx {
249 }; 253 };
250 let ast_id = self.source_ast_id_map.ast_id(enum_); 254 let ast_id = self.source_ast_id_map.ast_id(enum_);
251 let res = Enum { name, visibility, generic_params, variants, ast_id }; 255 let res = Enum { name, visibility, generic_params, variants, ast_id };
252 Some(id(self.tree.enums.alloc(res))) 256 Some(id(self.data().enums.alloc(res)))
253 } 257 }
254 258
255 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range<Idx<Variant>> { 259 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range<Idx<Variant>> {
256 let start = self.next_variant_idx(); 260 let start = self.next_variant_idx();
257 for variant in variants.variants() { 261 for variant in variants.variants() {
258 if let Some(data) = self.lower_variant(&variant) { 262 if let Some(data) = self.lower_variant(&variant) {
259 self.tree.variants.alloc(data); 263 self.data().variants.alloc(data);
260 } 264 }
261 } 265 }
262 let end = self.next_variant_idx(); 266 let end = self.next_variant_idx();
@@ -327,7 +331,7 @@ impl Ctx {
327 }; 331 };
328 res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func); 332 res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
329 333
330 Some(id(self.tree.functions.alloc(res))) 334 Some(id(self.data().functions.alloc(res)))
331 } 335 }
332 336
333 fn lower_type_alias( 337 fn lower_type_alias(
@@ -341,7 +345,7 @@ impl Ctx {
341 let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); 345 let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
342 let ast_id = self.source_ast_id_map.ast_id(type_alias); 346 let ast_id = self.source_ast_id_map.ast_id(type_alias);
343 let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id }; 347 let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id };
344 Some(id(self.tree.type_aliases.alloc(res))) 348 Some(id(self.data().type_aliases.alloc(res)))
345 } 349 }
346 350
347 fn lower_static(&mut self, static_: &ast::StaticDef) -> Option<FileItemTreeId<Static>> { 351 fn lower_static(&mut self, static_: &ast::StaticDef) -> Option<FileItemTreeId<Static>> {
@@ -351,7 +355,7 @@ impl Ctx {
351 let mutable = static_.mut_token().is_some(); 355 let mutable = static_.mut_token().is_some();
352 let ast_id = self.source_ast_id_map.ast_id(static_); 356 let ast_id = self.source_ast_id_map.ast_id(static_);
353 let res = Static { name, visibility, mutable, type_ref, ast_id }; 357 let res = Static { name, visibility, mutable, type_ref, ast_id };
354 Some(id(self.tree.statics.alloc(res))) 358 Some(id(self.data().statics.alloc(res)))
355 } 359 }
356 360
357 fn lower_const(&mut self, konst: &ast::ConstDef) -> FileItemTreeId<Const> { 361 fn lower_const(&mut self, konst: &ast::ConstDef) -> FileItemTreeId<Const> {
@@ -360,7 +364,7 @@ impl Ctx {
360 let visibility = self.lower_visibility(konst); 364 let visibility = self.lower_visibility(konst);
361 let ast_id = self.source_ast_id_map.ast_id(konst); 365 let ast_id = self.source_ast_id_map.ast_id(konst);
362 let res = Const { name, visibility, type_ref, ast_id }; 366 let res = Const { name, visibility, type_ref, ast_id };
363 id(self.tree.consts.alloc(res)) 367 id(self.data().consts.alloc(res))
364 } 368 }
365 369
366 fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> { 370 fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
@@ -386,7 +390,7 @@ impl Ctx {
386 }; 390 };
387 let ast_id = self.source_ast_id_map.ast_id(module); 391 let ast_id = self.source_ast_id_map.ast_id(module);
388 let res = Mod { name, visibility, kind, ast_id }; 392 let res = Mod { name, visibility, kind, ast_id };
389 Some(id(self.tree.mods.alloc(res))) 393 Some(id(self.data().mods.alloc(res)))
390 } 394 }
391 395
392 fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option<FileItemTreeId<Trait>> { 396 fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option<FileItemTreeId<Trait>> {
@@ -417,7 +421,7 @@ impl Ctx {
417 items: items.unwrap_or_default(), 421 items: items.unwrap_or_default(),
418 ast_id, 422 ast_id,
419 }; 423 };
420 Some(id(self.tree.traits.alloc(res))) 424 Some(id(self.data().traits.alloc(res)))
421 } 425 }
422 426
423 fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option<FileItemTreeId<Impl>> { 427 fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option<FileItemTreeId<Impl>> {
@@ -440,7 +444,7 @@ impl Ctx {
440 .collect(); 444 .collect();
441 let ast_id = self.source_ast_id_map.ast_id(impl_def); 445 let ast_id = self.source_ast_id_map.ast_id(impl_def);
442 let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }; 446 let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id };
443 Some(id(self.tree.impls.alloc(res))) 447 Some(id(self.data().impls.alloc(res)))
444 } 448 }
445 449
446 fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec<FileItemTreeId<Import>> { 450 fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec<FileItemTreeId<Import>> {
@@ -451,7 +455,7 @@ impl Ctx {
451 455
452 // Every use item can expand to many `Import`s. 456 // Every use item can expand to many `Import`s.
453 let mut imports = Vec::new(); 457 let mut imports = Vec::new();
454 let tree = &mut self.tree; 458 let tree = self.tree.data_mut();
455 ModPath::expand_use_item( 459 ModPath::expand_use_item(
456 InFile::new(self.file, use_item.clone()), 460 InFile::new(self.file, use_item.clone()),
457 &self.hygiene, 461 &self.hygiene,
@@ -484,7 +488,7 @@ impl Ctx {
484 let is_macro_use = extern_crate.has_atom_attr("macro_use"); 488 let is_macro_use = extern_crate.has_atom_attr("macro_use");
485 489
486 let res = ExternCrate { path, alias, visibility, is_macro_use, ast_id }; 490 let res = ExternCrate { path, alias, visibility, is_macro_use, ast_id };
487 Some(id(self.tree.extern_crates.alloc(res))) 491 Some(id(self.data().extern_crates.alloc(res)))
488 } 492 }
489 493
490 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> { 494 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
@@ -511,7 +515,7 @@ impl Ctx {
511 515
512 let is_builtin = attrs.by_key("rustc_builtin_macro").exists(); 516 let is_builtin = attrs.by_key("rustc_builtin_macro").exists();
513 let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id }; 517 let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id };
514 Some(id(self.tree.macro_calls.alloc(res))) 518 Some(id(self.data().macro_calls.alloc(res)))
515 } 519 }
516 520
517 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> { 521 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> {
@@ -619,10 +623,14 @@ impl Ctx {
619 } 623 }
620 624
621 fn next_field_idx(&self) -> Idx<Field> { 625 fn next_field_idx(&self) -> Idx<Field> {
622 Idx::from_raw(RawId::from(self.tree.fields.len() as u32)) 626 Idx::from_raw(RawId::from(
627 self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
628 ))
623 } 629 }
624 fn next_variant_idx(&self) -> Idx<Variant> { 630 fn next_variant_idx(&self) -> Idx<Variant> {
625 Idx::from_raw(RawId::from(self.tree.variants.len() as u32)) 631 Idx::from_raw(RawId::from(
632 self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
633 ))
626 } 634 }
627} 635}
628 636