aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/item_tree.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-04-05 02:50:10 +0100
committerJonas Schievink <[email protected]>2021-04-05 02:50:10 +0100
commit7c0c713a102ee86ee32af115acba63a5c3b3a657 (patch)
treeefa1f5aee1521d720bc6e83db26b53f00d7562a0 /crates/hir_def/src/item_tree.rs
parentadcf18e27dc04b60fede859f3d6c22b99d4fd513 (diff)
Intern `GenericParams`
Also share the same instance between `ItemTree` and `generic_params` query.
Diffstat (limited to 'crates/hir_def/src/item_tree.rs')
-rw-r--r--crates/hir_def/src/item_tree.rs61
1 files changed, 7 insertions, 54 deletions
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index 739906778..240662486 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -58,13 +58,6 @@ impl fmt::Debug for RawVisibilityId {
58 } 58 }
59} 59}
60 60
61#[derive(Debug, Copy, Clone, Eq, PartialEq)]
62pub struct GenericParamsId(u32);
63
64impl GenericParamsId {
65 pub const EMPTY: Self = GenericParamsId(u32::max_value());
66}
67
68/// The item tree of a source file. 61/// The item tree of a source file.
69#[derive(Debug, Default, Eq, PartialEq)] 62#[derive(Debug, Default, Eq, PartialEq)]
70pub struct ItemTree { 63pub struct ItemTree {
@@ -146,7 +139,6 @@ impl ItemTree {
146 macro_rules, 139 macro_rules,
147 macro_defs, 140 macro_defs,
148 vis, 141 vis,
149 generics,
150 inner_items, 142 inner_items,
151 } = &mut **data; 143 } = &mut **data;
152 144
@@ -170,7 +162,6 @@ impl ItemTree {
170 macro_defs.shrink_to_fit(); 162 macro_defs.shrink_to_fit();
171 163
172 vis.arena.shrink_to_fit(); 164 vis.arena.shrink_to_fit();
173 generics.arena.shrink_to_fit();
174 165
175 inner_items.shrink_to_fit(); 166 inner_items.shrink_to_fit();
176 } 167 }
@@ -242,32 +233,6 @@ static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKi
242static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate)); 233static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate));
243 234
244#[derive(Default, Debug, Eq, PartialEq)] 235#[derive(Default, Debug, Eq, PartialEq)]
245struct GenericParamsStorage {
246 arena: Arena<GenericParams>,
247}
248
249impl GenericParamsStorage {
250 fn alloc(&mut self, params: GenericParams) -> GenericParamsId {
251 if params.types.is_empty()
252 && params.lifetimes.is_empty()
253 && params.consts.is_empty()
254 && params.where_predicates.is_empty()
255 {
256 return GenericParamsId::EMPTY;
257 }
258
259 GenericParamsId(self.arena.alloc(params).into_raw().into())
260 }
261}
262
263static EMPTY_GENERICS: GenericParams = GenericParams {
264 types: Arena::new(),
265 lifetimes: Arena::new(),
266 consts: Arena::new(),
267 where_predicates: Vec::new(),
268};
269
270#[derive(Default, Debug, Eq, PartialEq)]
271struct ItemTreeData { 236struct ItemTreeData {
272 imports: Arena<Import>, 237 imports: Arena<Import>,
273 extern_crates: Arena<ExternCrate>, 238 extern_crates: Arena<ExternCrate>,
@@ -289,7 +254,6 @@ struct ItemTreeData {
289 macro_defs: Arena<MacroDef>, 254 macro_defs: Arena<MacroDef>,
290 255
291 vis: ItemVisibilities, 256 vis: ItemVisibilities,
292 generics: GenericParamsStorage,
293 257
294 inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>, 258 inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>,
295} 259}
@@ -508,17 +472,6 @@ impl Index<RawVisibilityId> for ItemTree {
508 } 472 }
509} 473}
510 474
511impl Index<GenericParamsId> for ItemTree {
512 type Output = GenericParams;
513
514 fn index(&self, index: GenericParamsId) -> &Self::Output {
515 match index {
516 GenericParamsId::EMPTY => &EMPTY_GENERICS,
517 _ => &self.data().generics.arena[Idx::from_raw(index.0.into())],
518 }
519 }
520}
521
522impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree { 475impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
523 type Output = N; 476 type Output = N;
524 fn index(&self, id: FileItemTreeId<N>) -> &N { 477 fn index(&self, id: FileItemTreeId<N>) -> &N {
@@ -555,7 +508,7 @@ pub struct ExternCrate {
555pub struct Function { 508pub struct Function {
556 pub name: Name, 509 pub name: Name,
557 pub visibility: RawVisibilityId, 510 pub visibility: RawVisibilityId,
558 pub generic_params: GenericParamsId, 511 pub generic_params: Interned<GenericParams>,
559 pub abi: Option<Interned<str>>, 512 pub abi: Option<Interned<str>>,
560 pub params: IdRange<Param>, 513 pub params: IdRange<Param>,
561 pub ret_type: Interned<TypeRef>, 514 pub ret_type: Interned<TypeRef>,
@@ -590,7 +543,7 @@ impl FnFlags {
590pub struct Struct { 543pub struct Struct {
591 pub name: Name, 544 pub name: Name,
592 pub visibility: RawVisibilityId, 545 pub visibility: RawVisibilityId,
593 pub generic_params: GenericParamsId, 546 pub generic_params: Interned<GenericParams>,
594 pub fields: Fields, 547 pub fields: Fields,
595 pub ast_id: FileAstId<ast::Struct>, 548 pub ast_id: FileAstId<ast::Struct>,
596 pub kind: StructDefKind, 549 pub kind: StructDefKind,
@@ -610,7 +563,7 @@ pub enum StructDefKind {
610pub struct Union { 563pub struct Union {
611 pub name: Name, 564 pub name: Name,
612 pub visibility: RawVisibilityId, 565 pub visibility: RawVisibilityId,
613 pub generic_params: GenericParamsId, 566 pub generic_params: Interned<GenericParams>,
614 pub fields: Fields, 567 pub fields: Fields,
615 pub ast_id: FileAstId<ast::Union>, 568 pub ast_id: FileAstId<ast::Union>,
616} 569}
@@ -619,7 +572,7 @@ pub struct Union {
619pub struct Enum { 572pub struct Enum {
620 pub name: Name, 573 pub name: Name,
621 pub visibility: RawVisibilityId, 574 pub visibility: RawVisibilityId,
622 pub generic_params: GenericParamsId, 575 pub generic_params: Interned<GenericParams>,
623 pub variants: IdRange<Variant>, 576 pub variants: IdRange<Variant>,
624 pub ast_id: FileAstId<ast::Enum>, 577 pub ast_id: FileAstId<ast::Enum>,
625} 578}
@@ -648,7 +601,7 @@ pub struct Static {
648pub struct Trait { 601pub struct Trait {
649 pub name: Name, 602 pub name: Name,
650 pub visibility: RawVisibilityId, 603 pub visibility: RawVisibilityId,
651 pub generic_params: GenericParamsId, 604 pub generic_params: Interned<GenericParams>,
652 pub is_auto: bool, 605 pub is_auto: bool,
653 pub is_unsafe: bool, 606 pub is_unsafe: bool,
654 pub bounds: Box<[TypeBound]>, 607 pub bounds: Box<[TypeBound]>,
@@ -658,7 +611,7 @@ pub struct Trait {
658 611
659#[derive(Debug, Clone, Eq, PartialEq)] 612#[derive(Debug, Clone, Eq, PartialEq)]
660pub struct Impl { 613pub struct Impl {
661 pub generic_params: GenericParamsId, 614 pub generic_params: Interned<GenericParams>,
662 pub target_trait: Option<Interned<TraitRef>>, 615 pub target_trait: Option<Interned<TraitRef>>,
663 pub self_ty: Interned<TypeRef>, 616 pub self_ty: Interned<TypeRef>,
664 pub is_negative: bool, 617 pub is_negative: bool,
@@ -672,7 +625,7 @@ pub struct TypeAlias {
672 pub visibility: RawVisibilityId, 625 pub visibility: RawVisibilityId,
673 /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`. 626 /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
674 pub bounds: Box<[TypeBound]>, 627 pub bounds: Box<[TypeBound]>,
675 pub generic_params: GenericParamsId, 628 pub generic_params: Interned<GenericParams>,
676 pub type_ref: Option<Interned<TypeRef>>, 629 pub type_ref: Option<Interned<TypeRef>>,
677 pub is_extern: bool, 630 pub is_extern: bool,
678 pub ast_id: FileAstId<ast::TypeAlias>, 631 pub ast_id: FileAstId<ast::TypeAlias>,