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