aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/item_tree.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/item_tree.rs')
-rw-r--r--crates/ra_hir_def/src/item_tree.rs753
1 files changed, 753 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
new file mode 100644
index 000000000..3e603bd55
--- /dev/null
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -0,0 +1,753 @@
1//! A simplified AST that only contains items.
2
3mod lower;
4#[cfg(test)]
5mod tests;
6
7use std::{
8 any::type_name,
9 fmt::{self, Debug},
10 hash::{Hash, Hasher},
11 marker::PhantomData,
12 ops::{Index, Range},
13 sync::Arc,
14};
15
16use ast::{AstNode, AttrsOwner, NameOwner, StructKind, TypeAscriptionOwner};
17use either::Either;
18use hir_expand::{
19 ast_id_map::FileAstId,
20 hygiene::Hygiene,
21 name::{name, AsName, Name},
22 HirFileId, InFile,
23};
24use ra_arena::{Arena, Idx, RawId};
25use ra_syntax::{ast, match_ast};
26use rustc_hash::FxHashMap;
27use smallvec::SmallVec;
28use test_utils::mark;
29
30use crate::{
31 attr::Attrs,
32 db::DefDatabase,
33 generics::GenericParams,
34 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
35 type_ref::{Mutability, TypeBound, TypeRef},
36 visibility::RawVisibility,
37};
38
39#[derive(Copy, Clone, Eq, PartialEq)]
40pub struct RawVisibilityId(u32);
41
42impl RawVisibilityId {
43 pub const PUB: Self = RawVisibilityId(u32::max_value());
44 pub const PRIV: Self = RawVisibilityId(u32::max_value() - 1);
45 pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 2);
46}
47
48impl fmt::Debug for RawVisibilityId {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 let mut f = f.debug_tuple("RawVisibilityId");
51 match *self {
52 Self::PUB => f.field(&"pub"),
53 Self::PRIV => f.field(&"pub(self)"),
54 Self::PUB_CRATE => f.field(&"pub(crate)"),
55 _ => f.field(&self.0),
56 };
57 f.finish()
58 }
59}
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.
69#[derive(Debug, Eq, PartialEq)]
70pub struct ItemTree {
71 top_level: SmallVec<[ModItem; 1]>,
72 attrs: FxHashMap<AttrOwner, Attrs>,
73 inner_items: FxHashMap<FileAstId<ast::ModuleItem>, SmallVec<[ModItem; 1]>>,
74
75 data: Option<Box<ItemTreeData>>,
76}
77
78impl ItemTree {
79 pub fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
80 let _p = ra_prof::profile("item_tree_query").detail(|| format!("{:?}", file_id));
81 let syntax = if let Some(node) = db.parse_or_expand(file_id) {
82 node
83 } else {
84 return Arc::new(Self::empty());
85 };
86
87 let hygiene = Hygiene::new(db.upcast(), file_id);
88 let ctx = lower::Ctx::new(db, hygiene.clone(), file_id);
89 let mut top_attrs = None;
90 let mut item_tree = match_ast! {
91 match syntax {
92 ast::SourceFile(file) => {
93 top_attrs = Some(Attrs::new(&file, &hygiene));
94 ctx.lower_module_items(&file)
95 },
96 ast::MacroItems(items) => {
97 ctx.lower_module_items(&items)
98 },
99 // Macros can expand to expressions. We return an empty item tree in this case, but
100 // still need to collect inner items.
101 ast::Expr(e) => {
102 ctx.lower_inner_items(e.syntax())
103 },
104 _ => {
105 panic!("cannot create item tree from {:?}", syntax);
106 },
107 }
108 };
109
110 if let Some(attrs) = top_attrs {
111 item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
112 }
113 item_tree.shrink_to_fit();
114 Arc::new(item_tree)
115 }
116
117 fn empty() -> Self {
118 Self {
119 top_level: Default::default(),
120 attrs: Default::default(),
121 inner_items: Default::default(),
122 data: Default::default(),
123 }
124 }
125
126 fn shrink_to_fit(&mut self) {
127 if let Some(data) = &mut self.data {
128 let ItemTreeData {
129 imports,
130 extern_crates,
131 functions,
132 structs,
133 fields,
134 unions,
135 enums,
136 variants,
137 consts,
138 statics,
139 traits,
140 impls,
141 type_aliases,
142 mods,
143 macro_calls,
144 exprs,
145 vis,
146 generics,
147 } = &mut **data;
148
149 imports.shrink_to_fit();
150 extern_crates.shrink_to_fit();
151 functions.shrink_to_fit();
152 structs.shrink_to_fit();
153 fields.shrink_to_fit();
154 unions.shrink_to_fit();
155 enums.shrink_to_fit();
156 variants.shrink_to_fit();
157 consts.shrink_to_fit();
158 statics.shrink_to_fit();
159 traits.shrink_to_fit();
160 impls.shrink_to_fit();
161 type_aliases.shrink_to_fit();
162 mods.shrink_to_fit();
163 macro_calls.shrink_to_fit();
164 exprs.shrink_to_fit();
165
166 vis.arena.shrink_to_fit();
167 generics.arena.shrink_to_fit();
168 }
169 }
170
171 /// Returns an iterator over all items located at the top level of the `HirFileId` this
172 /// `ItemTree` was created from.
173 pub fn top_level_items(&self) -> &[ModItem] {
174 &self.top_level
175 }
176
177 /// Returns the inner attributes of the source file.
178 pub fn top_level_attrs(&self) -> &Attrs {
179 self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&Attrs::EMPTY)
180 }
181
182 pub fn attrs(&self, of: AttrOwner) -> &Attrs {
183 self.attrs.get(&of).unwrap_or(&Attrs::EMPTY)
184 }
185
186 /// Returns the lowered inner items that `ast` corresponds to.
187 ///
188 /// Most AST items are lowered to a single `ModItem`, but some (eg. `use` items) may be lowered
189 /// to multiple items in the `ItemTree`.
190 pub fn inner_items(&self, ast: FileAstId<ast::ModuleItem>) -> &[ModItem] {
191 &self.inner_items[&ast]
192 }
193
194 pub fn all_inner_items(&self) -> impl Iterator<Item = ModItem> + '_ {
195 self.inner_items.values().flatten().copied()
196 }
197
198 pub fn source<S: ItemTreeNode>(&self, db: &dyn DefDatabase, of: ItemTreeId<S>) -> S::Source {
199 // This unwrap cannot fail, since it has either succeeded above, or resulted in an empty
200 // ItemTree (in which case there is no valid `FileItemTreeId` to call this method with).
201 let root =
202 db.parse_or_expand(of.file_id).expect("parse_or_expand failed on constructed ItemTree");
203
204 let id = self[of.value].ast_id();
205 let map = db.ast_id_map(of.file_id);
206 let ptr = map.get(id);
207 ptr.to_node(&root)
208 }
209
210 fn data(&self) -> &ItemTreeData {
211 self.data.as_ref().expect("attempted to access data of empty ItemTree")
212 }
213
214 fn data_mut(&mut self) -> &mut ItemTreeData {
215 self.data.get_or_insert_with(Box::default)
216 }
217}
218
219#[derive(Default, Debug, Eq, PartialEq)]
220struct ItemVisibilities {
221 arena: Arena<RawVisibility>,
222}
223
224impl ItemVisibilities {
225 fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
226 match &vis {
227 RawVisibility::Public => RawVisibilityId::PUB,
228 RawVisibility::Module(path) if path.segments.is_empty() => match &path.kind {
229 PathKind::Super(0) => RawVisibilityId::PRIV,
230 PathKind::Crate => RawVisibilityId::PUB_CRATE,
231 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
232 },
233 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
234 }
235 }
236}
237
238static VIS_PUB: RawVisibility = RawVisibility::Public;
239static VIS_PRIV: RawVisibility =
240 RawVisibility::Module(ModPath { kind: PathKind::Super(0), segments: Vec::new() });
241static VIS_PUB_CRATE: RawVisibility =
242 RawVisibility::Module(ModPath { kind: PathKind::Crate, segments: Vec::new() });
243
244#[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() && params.where_predicates.is_empty() {
252 return GenericParamsId::EMPTY;
253 }
254
255 GenericParamsId(self.arena.alloc(params).into_raw().into())
256 }
257}
258
259static EMPTY_GENERICS: GenericParams =
260 GenericParams { types: Arena::new(), where_predicates: Vec::new() };
261
262#[derive(Default, Debug, Eq, PartialEq)]
263struct ItemTreeData {
264 imports: Arena<Import>,
265 extern_crates: Arena<ExternCrate>,
266 functions: Arena<Function>,
267 structs: Arena<Struct>,
268 fields: Arena<Field>,
269 unions: Arena<Union>,
270 enums: Arena<Enum>,
271 variants: Arena<Variant>,
272 consts: Arena<Const>,
273 statics: Arena<Static>,
274 traits: Arena<Trait>,
275 impls: Arena<Impl>,
276 type_aliases: Arena<TypeAlias>,
277 mods: Arena<Mod>,
278 macro_calls: Arena<MacroCall>,
279 exprs: Arena<Expr>,
280
281 vis: ItemVisibilities,
282 generics: GenericParamsStorage,
283}
284
285#[derive(Debug, Eq, PartialEq, Hash)]
286pub enum AttrOwner {
287 /// Attributes on an item.
288 ModItem(ModItem),
289 /// Inner attributes of the source file.
290 TopLevel,
291
292 Variant(Idx<Variant>),
293 Field(Idx<Field>),
294 // FIXME: Store variant and field attrs, and stop reparsing them in `attrs_query`.
295}
296
297macro_rules! from_attrs {
298 ( $( $var:ident($t:ty) ),+ ) => {
299 $(
300 impl From<$t> for AttrOwner {
301 fn from(t: $t) -> AttrOwner {
302 AttrOwner::$var(t)
303 }
304 }
305 )+
306 };
307}
308
309from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>));
310
311/// Trait implemented by all item nodes in the item tree.
312pub trait ItemTreeNode: Clone {
313 type Source: AstNode + Into<ast::ModuleItem>;
314
315 fn ast_id(&self) -> FileAstId<Self::Source>;
316
317 /// Looks up an instance of `Self` in an item tree.
318 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
319
320 /// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
321 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
322
323 /// Upcasts a `FileItemTreeId` to a generic `ModItem`.
324 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
325}
326
327pub struct FileItemTreeId<N: ItemTreeNode> {
328 index: Idx<N>,
329 _p: PhantomData<N>,
330}
331
332impl<N: ItemTreeNode> Clone for FileItemTreeId<N> {
333 fn clone(&self) -> Self {
334 Self { index: self.index, _p: PhantomData }
335 }
336}
337impl<N: ItemTreeNode> Copy for FileItemTreeId<N> {}
338
339impl<N: ItemTreeNode> PartialEq for FileItemTreeId<N> {
340 fn eq(&self, other: &FileItemTreeId<N>) -> bool {
341 self.index == other.index
342 }
343}
344impl<N: ItemTreeNode> Eq for FileItemTreeId<N> {}
345
346impl<N: ItemTreeNode> Hash for FileItemTreeId<N> {
347 fn hash<H: Hasher>(&self, state: &mut H) {
348 self.index.hash(state)
349 }
350}
351
352impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
353 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
354 self.index.fmt(f)
355 }
356}
357
358pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
359
360macro_rules! mod_items {
361 ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
362 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
363 pub enum ModItem {
364 $(
365 $typ(FileItemTreeId<$typ>),
366 )+
367 }
368
369 $(
370 impl From<FileItemTreeId<$typ>> for ModItem {
371 fn from(id: FileItemTreeId<$typ>) -> ModItem {
372 ModItem::$typ(id)
373 }
374 }
375 )+
376
377 $(
378 impl ItemTreeNode for $typ {
379 type Source = $ast;
380
381 fn ast_id(&self) -> FileAstId<Self::Source> {
382 self.ast_id
383 }
384
385 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
386 &tree.data().$fld[index]
387 }
388
389 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
390 if let ModItem::$typ(id) = mod_item {
391 Some(id)
392 } else {
393 None
394 }
395 }
396
397 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
398 ModItem::$typ(id)
399 }
400 }
401
402 impl Index<Idx<$typ>> for ItemTree {
403 type Output = $typ;
404
405 fn index(&self, index: Idx<$typ>) -> &Self::Output {
406 &self.data().$fld[index]
407 }
408 }
409 )+
410 };
411}
412
413mod_items! {
414 Import in imports -> ast::UseItem,
415 ExternCrate in extern_crates -> ast::ExternCrateItem,
416 Function in functions -> ast::FnDef,
417 Struct in structs -> ast::StructDef,
418 Union in unions -> ast::UnionDef,
419 Enum in enums -> ast::EnumDef,
420 Const in consts -> ast::ConstDef,
421 Static in statics -> ast::StaticDef,
422 Trait in traits -> ast::TraitDef,
423 Impl in impls -> ast::ImplDef,
424 TypeAlias in type_aliases -> ast::TypeAliasDef,
425 Mod in mods -> ast::Module,
426 MacroCall in macro_calls -> ast::MacroCall,
427}
428
429macro_rules! impl_index {
430 ( $($fld:ident: $t:ty),+ $(,)? ) => {
431 $(
432 impl Index<Idx<$t>> for ItemTree {
433 type Output = $t;
434
435 fn index(&self, index: Idx<$t>) -> &Self::Output {
436 &self.data().$fld[index]
437 }
438 }
439 )+
440 };
441}
442
443impl_index!(fields: Field, variants: Variant, exprs: Expr);
444
445impl Index<RawVisibilityId> for ItemTree {
446 type Output = RawVisibility;
447 fn index(&self, index: RawVisibilityId) -> &Self::Output {
448 match index {
449 RawVisibilityId::PRIV => &VIS_PRIV,
450 RawVisibilityId::PUB => &VIS_PUB,
451 RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
452 _ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
453 }
454 }
455}
456
457impl Index<GenericParamsId> for ItemTree {
458 type Output = GenericParams;
459
460 fn index(&self, index: GenericParamsId) -> &Self::Output {
461 match index {
462 GenericParamsId::EMPTY => &EMPTY_GENERICS,
463 _ => &self.data().generics.arena[Idx::from_raw(index.0.into())],
464 }
465 }
466}
467
468impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
469 type Output = N;
470 fn index(&self, id: FileItemTreeId<N>) -> &N {
471 N::lookup(self, id.index)
472 }
473}
474
475/// A desugared `use` import.
476#[derive(Debug, Clone, Eq, PartialEq)]
477pub struct Import {
478 pub path: ModPath,
479 pub alias: Option<ImportAlias>,
480 pub visibility: RawVisibilityId,
481 pub is_glob: bool,
482 pub is_prelude: bool,
483 /// AST ID of the `use` or `extern crate` item this import was derived from. Note that many
484 /// `Import`s can map to the same `use` item.
485 pub ast_id: FileAstId<ast::UseItem>,
486}
487
488#[derive(Debug, Clone, Eq, PartialEq)]
489pub struct ExternCrate {
490 pub path: ModPath,
491 pub alias: Option<ImportAlias>,
492 pub visibility: RawVisibilityId,
493 /// Whether this is a `#[macro_use] extern crate ...`.
494 pub is_macro_use: bool,
495 pub ast_id: FileAstId<ast::ExternCrateItem>,
496}
497
498#[derive(Debug, Clone, Eq, PartialEq)]
499pub struct Function {
500 pub name: Name,
501 pub visibility: RawVisibilityId,
502 pub generic_params: GenericParamsId,
503 pub has_self_param: bool,
504 pub is_unsafe: bool,
505 pub params: Box<[TypeRef]>,
506 pub ret_type: TypeRef,
507 pub ast_id: FileAstId<ast::FnDef>,
508}
509
510#[derive(Debug, Clone, Eq, PartialEq)]
511pub struct Struct {
512 pub name: Name,
513 pub visibility: RawVisibilityId,
514 pub generic_params: GenericParamsId,
515 pub fields: Fields,
516 pub ast_id: FileAstId<ast::StructDef>,
517 pub kind: StructDefKind,
518}
519
520#[derive(Debug, Clone, Eq, PartialEq)]
521pub enum StructDefKind {
522 /// `struct S { ... }` - type namespace only.
523 Record,
524 /// `struct S(...);`
525 Tuple,
526 /// `struct S;`
527 Unit,
528}
529
530#[derive(Debug, Clone, Eq, PartialEq)]
531pub struct Union {
532 pub name: Name,
533 pub visibility: RawVisibilityId,
534 pub generic_params: GenericParamsId,
535 pub fields: Fields,
536 pub ast_id: FileAstId<ast::UnionDef>,
537}
538
539#[derive(Debug, Clone, Eq, PartialEq)]
540pub struct Enum {
541 pub name: Name,
542 pub visibility: RawVisibilityId,
543 pub generic_params: GenericParamsId,
544 pub variants: IdRange<Variant>,
545 pub ast_id: FileAstId<ast::EnumDef>,
546}
547
548#[derive(Debug, Clone, Eq, PartialEq)]
549pub struct Const {
550 /// const _: () = ();
551 pub name: Option<Name>,
552 pub visibility: RawVisibilityId,
553 pub type_ref: TypeRef,
554 pub ast_id: FileAstId<ast::ConstDef>,
555}
556
557#[derive(Debug, Clone, Eq, PartialEq)]
558pub struct Static {
559 pub name: Name,
560 pub visibility: RawVisibilityId,
561 pub mutable: bool,
562 pub type_ref: TypeRef,
563 pub ast_id: FileAstId<ast::StaticDef>,
564}
565
566#[derive(Debug, Clone, Eq, PartialEq)]
567pub struct Trait {
568 pub name: Name,
569 pub visibility: RawVisibilityId,
570 pub generic_params: GenericParamsId,
571 pub auto: bool,
572 pub items: Box<[AssocItem]>,
573 pub ast_id: FileAstId<ast::TraitDef>,
574}
575
576#[derive(Debug, Clone, Eq, PartialEq)]
577pub struct Impl {
578 pub generic_params: GenericParamsId,
579 pub target_trait: Option<TypeRef>,
580 pub target_type: TypeRef,
581 pub is_negative: bool,
582 pub items: Box<[AssocItem]>,
583 pub ast_id: FileAstId<ast::ImplDef>,
584}
585
586#[derive(Debug, Clone, PartialEq, Eq)]
587pub struct TypeAlias {
588 pub name: Name,
589 pub visibility: RawVisibilityId,
590 /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
591 pub bounds: Box<[TypeBound]>,
592 pub generic_params: GenericParamsId,
593 pub type_ref: Option<TypeRef>,
594 pub ast_id: FileAstId<ast::TypeAliasDef>,
595}
596
597#[derive(Debug, Clone, Eq, PartialEq)]
598pub struct Mod {
599 pub name: Name,
600 pub visibility: RawVisibilityId,
601 pub kind: ModKind,
602 pub ast_id: FileAstId<ast::Module>,
603}
604
605#[derive(Debug, Clone, Eq, PartialEq)]
606pub enum ModKind {
607 /// `mod m { ... }`
608 Inline { items: Box<[ModItem]> },
609
610 /// `mod m;`
611 Outline {},
612}
613
614#[derive(Debug, Clone, Eq, PartialEq)]
615pub struct MacroCall {
616 /// For `macro_rules!` declarations, this is the name of the declared macro.
617 pub name: Option<Name>,
618 /// Path to the called macro.
619 pub path: ModPath,
620 /// Has `#[macro_export]`.
621 pub is_export: bool,
622 /// Has `#[macro_export(local_inner_macros)]`.
623 pub is_local_inner: bool,
624 /// Has `#[rustc_builtin_macro]`.
625 pub is_builtin: bool,
626 pub ast_id: FileAstId<ast::MacroCall>,
627}
628
629// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
630// lengths, but we don't do much with them yet.
631#[derive(Debug, Clone, Eq, PartialEq)]
632pub struct Expr;
633
634macro_rules! impl_froms {
635 ($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
636 $(
637 impl From<$t> for $e {
638 fn from(it: $t) -> $e {
639 $e::$v(it)
640 }
641 }
642 )*
643 }
644}
645
646impl ModItem {
647 pub fn as_assoc_item(&self) -> Option<AssocItem> {
648 match self {
649 ModItem::Import(_)
650 | ModItem::ExternCrate(_)
651 | ModItem::Struct(_)
652 | ModItem::Union(_)
653 | ModItem::Enum(_)
654 | ModItem::Static(_)
655 | ModItem::Trait(_)
656 | ModItem::Impl(_)
657 | ModItem::Mod(_) => None,
658 ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
659 ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
660 ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
661 ModItem::Function(func) => Some(AssocItem::Function(*func)),
662 }
663 }
664
665 pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
666 N::id_from_mod_item(self)
667 }
668}
669
670#[derive(Debug, Copy, Clone, Eq, PartialEq)]
671pub enum AssocItem {
672 Function(FileItemTreeId<Function>),
673 TypeAlias(FileItemTreeId<TypeAlias>),
674 Const(FileItemTreeId<Const>),
675 MacroCall(FileItemTreeId<MacroCall>),
676}
677
678impl_froms!(AssocItem {
679 Function(FileItemTreeId<Function>),
680 TypeAlias(FileItemTreeId<TypeAlias>),
681 Const(FileItemTreeId<Const>),
682 MacroCall(FileItemTreeId<MacroCall>),
683});
684
685impl From<AssocItem> for ModItem {
686 fn from(item: AssocItem) -> Self {
687 match item {
688 AssocItem::Function(it) => it.into(),
689 AssocItem::TypeAlias(it) => it.into(),
690 AssocItem::Const(it) => it.into(),
691 AssocItem::MacroCall(it) => it.into(),
692 }
693 }
694}
695
696#[derive(Debug, Eq, PartialEq)]
697pub struct Variant {
698 pub name: Name,
699 pub fields: Fields,
700}
701
702pub struct IdRange<T> {
703 range: Range<u32>,
704 _p: PhantomData<T>,
705}
706
707impl<T> IdRange<T> {
708 fn new(range: Range<Idx<T>>) -> Self {
709 Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
710 }
711}
712
713impl<T> Iterator for IdRange<T> {
714 type Item = Idx<T>;
715 fn next(&mut self) -> Option<Self::Item> {
716 self.range.next().map(|raw| Idx::from_raw(raw.into()))
717 }
718}
719
720impl<T> fmt::Debug for IdRange<T> {
721 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
722 f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
723 }
724}
725
726impl<T> Clone for IdRange<T> {
727 fn clone(&self) -> Self {
728 Self { range: self.range.clone(), _p: PhantomData }
729 }
730}
731
732impl<T> PartialEq for IdRange<T> {
733 fn eq(&self, other: &Self) -> bool {
734 self.range == other.range
735 }
736}
737
738impl<T> Eq for IdRange<T> {}
739
740#[derive(Debug, Clone, PartialEq, Eq)]
741pub enum Fields {
742 Record(IdRange<Field>),
743 Tuple(IdRange<Field>),
744 Unit,
745}
746
747/// A single field of an enum variant or struct
748#[derive(Debug, Clone, PartialEq, Eq)]
749pub struct Field {
750 pub name: Name,
751 pub type_ref: TypeRef,
752 pub visibility: RawVisibilityId,
753}