Skip to main content

syn_sem/semantic/tree/
item.rs

1use super::{
2    format,
3    format::PrintFilter,
4    node::NodeIndex,
5    ty::{TypeId, OWNED_TYPE_CREATOR},
6    PathId,
7};
8use crate::{
9    etc::util,
10    syntax::common::{AttributeHelper, IdentifySyn, SynId},
11    TriOption, Which2,
12};
13use smallvec::SmallVec;
14use std::{
15    cmp, fmt, mem, ops,
16    path::{Path as StdPath, PathBuf},
17    ptr::NonNull,
18};
19
20#[derive(Default, Clone, PartialEq)]
21pub enum PrivItem {
22    Block(Block),
23    Const(Const),
24    Enum(Enum),
25    Field(Field),
26    Fn(Fn),
27    Local(Local),
28    Mod(Mod),
29    Struct(Struct),
30    Trait(Trait),
31    TypeAlias(TypeAlias),
32    Use(Use),
33    Variant(Variant),
34    RawConst(RawConst),
35    RawEnum(RawEnum),
36    RawField(RawField),
37    RawFn(RawFn),
38    RawLocal(RawLocal),
39    RawMod(RawMod),
40    RawStruct(RawStruct),
41    RawTrait(RawTrait),
42    RawTypeAlias(RawTypeAlias),
43    RawUse(RawUse),
44    RawVariant(RawVariant),
45    #[default]
46    None,
47}
48
49impl PrivItem {
50    pub(crate) fn as_block(&self) -> &Block {
51        let Self::Block(v) = self else {
52            panic!("expected `PrivItem::Block`, but found {self:#?}");
53        };
54        v
55    }
56
57    pub(crate) fn as_use(&self) -> &Use {
58        let Self::Use(v) = self else {
59            panic!("expected `PrivItem::Use`, but found {self:#?}");
60        };
61        v
62    }
63
64    pub(crate) fn as_mut_mod(&mut self) -> &mut Mod {
65        let Self::Mod(v) = self else {
66            panic!("expected `PrivItem::Mod`, but found {self:#?}");
67        };
68        v
69    }
70
71    pub(crate) fn as_mut_type_alias(&mut self) -> &mut TypeAlias {
72        let Self::TypeAlias(v) = self else {
73            panic!("expected `PrivItem::TypeAlias`, but found {self:#?}");
74        };
75        v
76    }
77
78    pub(crate) fn as_mut_use(&mut self) -> &mut Use {
79        let Self::Use(v) = self else {
80            panic!("expected `PrivItem::Use`, but found {self:#?}");
81        };
82        v
83    }
84
85    pub(crate) fn as_raw_const(&self) -> &RawConst {
86        let Self::RawConst(v) = self else {
87            panic!("expected `PrivItem::RawConst`, but found {self:#?}");
88        };
89        v
90    }
91
92    pub(crate) fn as_raw_enum(&self) -> &RawEnum {
93        let Self::RawEnum(v) = self else {
94            panic!("expected `PrivItem::RawEnum`, but found {self:#?}");
95        };
96        v
97    }
98
99    pub(crate) fn as_raw_field(&self) -> &RawField {
100        let Self::RawField(v) = self else {
101            panic!("expected `PrivItem::RawField`, but found {self:#?}");
102        };
103        v
104    }
105
106    pub(crate) fn as_raw_fn(&self) -> &RawFn {
107        let Self::RawFn(v) = self else {
108            panic!("expected `PrivItem::RawFn`, but found {self:#?}");
109        };
110        v
111    }
112
113    pub(crate) fn as_raw_local(&self) -> &RawLocal {
114        let Self::RawLocal(v) = self else {
115            panic!("expected `PrivItem::RawLocal`, but found {self:#?}");
116        };
117        v
118    }
119
120    pub(crate) fn as_raw_mod(&self) -> &RawMod {
121        let Self::RawMod(v) = self else {
122            panic!("expected `PrivItem::RawMod`, but found {self:#?}");
123        };
124        v
125    }
126
127    pub(crate) fn as_raw_struct(&self) -> &RawStruct {
128        let Self::RawStruct(v) = self else {
129            panic!("expected `PrivItem::RawStruct`, but found {self:#?}");
130        };
131        v
132    }
133
134    pub(crate) fn as_raw_trait(&self) -> &RawTrait {
135        let Self::RawTrait(v) = self else {
136            panic!("expected `PrivItem::RawTrait`, but found {self:#?}");
137        };
138        v
139    }
140
141    pub(crate) fn as_raw_type_alias(&self) -> &RawTypeAlias {
142        let Self::RawTypeAlias(v) = self else {
143            panic!("expected `PrivItem::RawTypeAlias`, but found {self:#?}");
144        };
145        v
146    }
147
148    pub(crate) fn as_raw_use(&self) -> &RawUse {
149        let Self::RawUse(v) = self else {
150            panic!("expected `PrivItem::RawUse`, but found {self:#?}");
151        };
152        v
153    }
154
155    pub(crate) fn as_raw_variant(&self) -> &RawVariant {
156        let Self::RawVariant(v) = self else {
157            panic!("expected `PrivItem::RawVariant`, but found {self:#?}");
158        };
159        v
160    }
161
162    pub(crate) fn as_mut_raw_const(&mut self) -> &mut RawConst {
163        let Self::RawConst(v) = self else {
164            panic!("expected `PrivItem::RawConst`, but found {self:#?}");
165        };
166        v
167    }
168
169    pub(crate) fn as_mut_raw_enum(&mut self) -> &mut RawEnum {
170        let Self::RawEnum(v) = self else {
171            panic!("expected `PrivItem::RawEnum`, but found {self:#?}");
172        };
173        v
174    }
175
176    pub(crate) fn as_mut_raw_field(&mut self) -> &mut RawField {
177        let Self::RawField(v) = self else {
178            panic!("expected `PrivItem::RawField`, but found {self:#?}");
179        };
180        v
181    }
182
183    pub(crate) fn as_mut_raw_fn(&mut self) -> &mut RawFn {
184        let Self::RawFn(v) = self else {
185            panic!("expected `PrivItem::RawFn`, but found {self:#?}");
186        };
187        v
188    }
189
190    pub(crate) fn as_mut_raw_mod(&mut self) -> &mut RawMod {
191        let Self::RawMod(v) = self else {
192            panic!("expected `PrivItem::RawMod`, but found {self:#?}");
193        };
194        v
195    }
196
197    pub(crate) fn as_mut_raw_struct(&mut self) -> &mut RawStruct {
198        let Self::RawStruct(v) = self else {
199            panic!("expected `PrivItem::RawStruct`, but found {self:#?}");
200        };
201        v
202    }
203
204    pub(crate) fn as_mut_raw_type_alias(&mut self) -> &mut RawTypeAlias {
205        let Self::RawTypeAlias(v) = self else {
206            panic!("expected `PrivItem::RawTypeAlias`, but found {self:#?}");
207        };
208        v
209    }
210
211    pub(crate) fn as_mut_raw_use(&mut self) -> &mut RawUse {
212        let Self::RawUse(v) = self else {
213            panic!("expected `PrivItem::RawUse`, but found {self:#?}");
214        };
215        v
216    }
217
218    pub(crate) fn as_mut_raw_variant(&mut self) -> &mut RawVariant {
219        let Self::RawVariant(v) = self else {
220            panic!("expected `PrivItem::RawVariant`, but found {self:#?}");
221        };
222        v
223    }
224
225    pub(crate) fn is_raw(&self) -> bool {
226        match self {
227            Self::Block(_)
228            | Self::Const(_)
229            | Self::Enum(_)
230            | Self::Field(_)
231            | Self::Fn(_)
232            | Self::Local(_)
233            | Self::Mod(_)
234            | Self::Struct(_)
235            | Self::Trait(_)
236            | Self::TypeAlias(_)
237            | Self::Use(_)
238            | Self::Variant(_)
239            | Self::None => false,
240
241            Self::RawConst(_)
242            | Self::RawEnum(_)
243            | Self::RawField(_)
244            | Self::RawFn(_)
245            | Self::RawLocal(_)
246            | Self::RawMod(_)
247            | Self::RawStruct(_)
248            | Self::RawTrait(_)
249            | Self::RawTypeAlias(_)
250            | Self::RawUse(_)
251            | Self::RawVariant(_) => true,
252        }
253    }
254}
255
256impl fmt::Debug for PrivItem {
257    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258        match self {
259            Self::Block(v) => v.fmt(f),
260            Self::Const(v) => v.fmt(f),
261            Self::Enum(v) => v.fmt(f),
262            Self::Field(v) => v.fmt(f),
263            Self::Fn(v) => v.fmt(f),
264            Self::Local(v) => v.fmt(f),
265            Self::Mod(v) => v.fmt(f),
266            Self::Struct(v) => v.fmt(f),
267            Self::Trait(v) => v.fmt(f),
268            Self::TypeAlias(v) => v.fmt(f),
269            Self::Use(v) => v.fmt(f),
270            Self::Variant(v) => v.fmt(f),
271            Self::RawConst(v) => v.fmt(f),
272            Self::RawEnum(v) => v.fmt(f),
273            Self::RawField(v) => v.fmt(f),
274            Self::RawFn(v) => v.fmt(f),
275            Self::RawLocal(v) => v.fmt(f),
276            Self::RawMod(v) => v.fmt(f),
277            Self::RawStruct(v) => v.fmt(f),
278            Self::RawTrait(v) => v.fmt(f),
279            Self::RawTypeAlias(v) => v.fmt(f),
280            Self::RawUse(v) => v.fmt(f),
281            Self::RawVariant(v) => v.fmt(f),
282            Self::None => f.write_str("None"),
283        }
284    }
285}
286
287impl format::DebugBriefly for PrivItem {
288    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, filter: &PrintFilter) -> fmt::Result {
289        match self {
290            Self::Block(v) => v.fmt_briefly(f, filter),
291            Self::Const(v) => v.fmt_briefly(f, filter),
292            Self::Enum(v) => v.fmt_briefly(f, filter),
293            Self::Field(v) => v.fmt_briefly(f, filter),
294            Self::Fn(v) => v.fmt_briefly(f, filter),
295            Self::Local(v) => v.fmt_briefly(f, filter),
296            Self::Mod(v) => v.fmt_briefly(f, filter),
297            Self::Struct(v) => v.fmt_briefly(f, filter),
298            Self::Trait(v) => v.fmt_briefly(f, filter),
299            Self::TypeAlias(v) => v.fmt_briefly(f, filter),
300            Self::Use(v) => v.fmt_briefly(f, filter),
301            Self::Variant(v) => v.fmt_briefly(f, filter),
302            Self::RawConst(v) => v.fmt_briefly(f, filter),
303            Self::RawEnum(v) => v.fmt_briefly(f, filter),
304            Self::RawField(v) => v.fmt_briefly(f, filter),
305            Self::RawFn(v) => v.fmt_briefly(f, filter),
306            Self::RawLocal(v) => v.fmt_briefly(f, filter),
307            Self::RawMod(v) => v.fmt_briefly(f, filter),
308            Self::RawStruct(v) => v.fmt_briefly(f, filter),
309            Self::RawTrait(v) => v.fmt_briefly(f, filter),
310            Self::RawTypeAlias(v) => v.fmt_briefly(f, filter),
311            Self::RawUse(v) => v.fmt_briefly(f, filter),
312            Self::RawVariant(v) => v.fmt_briefly(f, filter),
313            Self::None => f.write_str("None"),
314        }
315    }
316
317    fn name(&self) -> &'static str {
318        "PrivItem"
319    }
320}
321
322pub enum PubItem<'a> {
323    Block(&'a Block),
324    Const(&'a Const),
325    Enum(&'a Enum),
326    Field(&'a Field),
327    Fn(&'a Fn),
328    Local(&'a Local),
329    Mod(&'a Mod),
330    Struct(&'a Struct),
331    Trait(&'a Trait),
332    TypeAlias(&'a TypeAlias),
333    Use(&'a Use),
334    Variant(&'a Variant),
335}
336
337impl<'a> PubItem<'a> {
338    pub(crate) fn new(item: &'a PrivItem) -> Option<Self> {
339        match item {
340            PrivItem::Block(v) => Some(Self::Block(v)),
341            PrivItem::Const(v) => Some(Self::Const(v)),
342            PrivItem::Enum(v) => Some(Self::Enum(v)),
343            PrivItem::Field(v) => Some(Self::Field(v)),
344            PrivItem::Fn(v) => Some(Self::Fn(v)),
345            PrivItem::Local(v) => Some(Self::Local(v)),
346            PrivItem::Mod(v) => Some(Self::Mod(v)),
347            PrivItem::Struct(v) => Some(Self::Struct(v)),
348            PrivItem::Trait(v) => Some(Self::Trait(v)),
349            PrivItem::TypeAlias(v) => Some(Self::TypeAlias(v)),
350            PrivItem::Use(v) => Some(Self::Use(v)),
351            PrivItem::Variant(v) => Some(Self::Variant(v)),
352            PrivItem::RawConst(_)
353            | PrivItem::RawEnum(_)
354            | PrivItem::RawField(_)
355            | PrivItem::RawFn(_)
356            | PrivItem::RawLocal(_)
357            | PrivItem::RawMod(_)
358            | PrivItem::RawStruct(_)
359            | PrivItem::RawTrait(_)
360            | PrivItem::RawTypeAlias(_)
361            | PrivItem::RawUse(_)
362            | PrivItem::RawVariant(_)
363            | PrivItem::None => None,
364        }
365    }
366
367    pub fn as_block(&self) -> Option<&Block> {
368        if let Self::Block(inner) = self {
369            Some(inner)
370        } else {
371            None
372        }
373    }
374
375    pub fn as_const(&self) -> Option<&Const> {
376        if let Self::Const(inner) = self {
377            Some(inner)
378        } else {
379            None
380        }
381    }
382
383    pub fn as_field(&self) -> Option<&Field> {
384        if let Self::Field(inner) = self {
385            Some(inner)
386        } else {
387            None
388        }
389    }
390
391    pub fn as_fn(&self) -> Option<&Fn> {
392        if let Self::Fn(inner) = self {
393            Some(inner)
394        } else {
395            None
396        }
397    }
398
399    pub fn as_local(&self) -> Option<&Local> {
400        if let Self::Local(inner) = self {
401            Some(inner)
402        } else {
403            None
404        }
405    }
406
407    pub fn as_mod(&self) -> Option<&Mod> {
408        if let Self::Mod(inner) = self {
409            Some(inner)
410        } else {
411            None
412        }
413    }
414
415    pub fn as_struct(&self) -> Option<&Struct> {
416        if let Self::Struct(inner) = self {
417            Some(inner)
418        } else {
419            None
420        }
421    }
422
423    pub fn as_trait(&self) -> Option<&Trait> {
424        if let Self::Trait(inner) = self {
425            Some(inner)
426        } else {
427            None
428        }
429    }
430
431    pub fn as_type_alias(&self) -> Option<&TypeAlias> {
432        if let Self::TypeAlias(inner) = self {
433            Some(inner)
434        } else {
435            None
436        }
437    }
438
439    pub fn as_use(&self) -> Option<&Use> {
440        if let Self::Use(inner) = self {
441            Some(inner)
442        } else {
443            None
444        }
445    }
446}
447
448impl fmt::Debug for PubItem<'_> {
449    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
450        match self {
451            Self::Block(v) => v.fmt(f),
452            Self::Const(v) => v.fmt(f),
453            Self::Enum(v) => v.fmt(f),
454            Self::Field(v) => v.fmt(f),
455            Self::Fn(v) => v.fmt(f),
456            Self::Local(v) => v.fmt(f),
457            Self::Mod(v) => v.fmt(f),
458            Self::Struct(v) => v.fmt(f),
459            Self::Trait(v) => v.fmt(f),
460            Self::TypeAlias(v) => v.fmt(f),
461            Self::Use(v) => v.fmt(f),
462            Self::Variant(v) => v.fmt(f),
463        }
464    }
465}
466
467impl format::DebugBriefly for PubItem<'_> {
468    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, filter: &PrintFilter) -> fmt::Result {
469        match self {
470            Self::Block(v) => v.fmt_briefly(f, filter),
471            Self::Const(v) => v.fmt_briefly(f, filter),
472            Self::Enum(v) => v.fmt_briefly(f, filter),
473            Self::Field(v) => v.fmt_briefly(f, filter),
474            Self::Fn(v) => v.fmt_briefly(f, filter),
475            Self::Local(v) => v.fmt_briefly(f, filter),
476            Self::Mod(v) => v.fmt_briefly(f, filter),
477            Self::Struct(v) => v.fmt_briefly(f, filter),
478            Self::Trait(v) => v.fmt_briefly(f, filter),
479            Self::TypeAlias(v) => v.fmt_briefly(f, filter),
480            Self::Use(v) => v.fmt_briefly(f, filter),
481            Self::Variant(v) => v.fmt_briefly(f, filter),
482        }
483    }
484
485    fn name(&self) -> &'static str {
486        "PubItem"
487    }
488}
489
490impl AttributeHelper for PubItem<'_> {
491    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
492        match self {
493            Self::Block(v) => v.get_attributes(),
494            Self::Const(v) => v.get_attributes(),
495            Self::Enum(v) => v.get_attributes(),
496            Self::Field(v) => v.get_attributes(),
497            Self::Fn(v) => v.get_attributes(),
498            Self::Local(v) => v.get_attributes(),
499            Self::Mod(v) => v.get_attributes(),
500            Self::Struct(v) => v.get_attributes(),
501            Self::Trait(v) => v.get_attributes(),
502            Self::TypeAlias(v) => v.get_attributes(),
503            Self::Use(v) => v.get_attributes(),
504            Self::Variant(v) => v.get_attributes(),
505        }
506    }
507
508    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
509        None // Read-only
510    }
511}
512
513#[derive(Clone, PartialEq, Eq)]
514pub struct Block {
515    /// Read-only pointer to a syntax tree node that never changes.
516    pub(crate) ptr_block: NonNull<syn::Block>,
517}
518
519impl Block {
520    pub fn ptr_syn(&self) -> *const syn::Block {
521        self.ptr_block.as_ptr().cast_const()
522    }
523
524    pub fn as_syn<'o>(&self) -> &'o syn::Block {
525        unsafe { self.ptr_block.as_ref() }
526    }
527
528    pub fn syn_id(&self) -> SynId {
529        self.as_syn().syn_id()
530    }
531}
532
533impl fmt::Debug for Block {
534    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
535        let name = format::DebugBriefly::name(self);
536        f.debug_struct(name).finish()
537    }
538}
539
540impl format::DebugBriefly for Block {
541    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
542        let name = format::DebugBriefly::name(self);
543        f.debug_struct(name).finish()
544    }
545
546    fn name(&self) -> &'static str {
547        "Block"
548    }
549}
550
551impl AttributeHelper for Block {
552    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
553        None // No attributes
554    }
555
556    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
557        None // No attributes
558    }
559}
560
561#[derive(Clone, PartialEq)]
562pub enum Const {
563    Free {
564        /// Read-only pointer to a syntax tree node that never changes.
565        ptr_const: NonNull<syn::ItemConst>,
566        /// Refer to the [module documentation](self).
567        vis_node: NodeIndex,
568        /// Type of this item.
569        tid: TypeId,
570    },
571    Inher {
572        /// Read-only pointer to a syntax tree node that never changes.
573        ptr_const: NonNull<syn::ImplItemConst>,
574        /// Refer to the [module documentation](self).
575        vis_node: NodeIndex,
576        /// Type of this item.
577        tid: TypeId,
578    },
579    TraitDefault {
580        /// Read-only pointer to a syntax tree node that never changes.
581        ptr_const: NonNull<syn::TraitItemConst>,
582        /// Refer to the [module documentation](self).
583        vis_node: NodeIndex,
584        /// Type of this item.
585        tid: TypeId,
586    },
587    TraitImpl {
588        /// Read-only pointer to a syntax tree node that never changes.
589        ptr_const: NonNull<syn::ImplItemConst>,
590        /// Refer to the [module documentation](self).
591        vis_node: NodeIndex,
592        /// Type of this item.
593        tid: TypeId,
594    },
595}
596
597impl Const {
598    pub fn ptr_syn(&self) -> ConstPtr {
599        match self {
600            Self::Free { ptr_const, .. } => ConstPtr::Free(ptr_const.as_ptr().cast_const()),
601            Self::Inher { ptr_const, .. } => ConstPtr::Inher(ptr_const.as_ptr().cast_const()),
602            Self::TraitDefault { ptr_const, .. } => {
603                ConstPtr::TraitDefault(ptr_const.as_ptr().cast_const())
604            }
605            Self::TraitImpl { ptr_const, .. } => {
606                ConstPtr::TraitImpl(ptr_const.as_ptr().cast_const())
607            }
608        }
609    }
610
611    pub fn syn_id(&self) -> SynId {
612        match self {
613            Self::Free { ptr_const, .. } => unsafe { ptr_const.as_ref().syn_id() },
614            Self::Inher { ptr_const, .. } => unsafe { ptr_const.as_ref().syn_id() },
615            Self::TraitDefault { ptr_const, .. } => unsafe { ptr_const.as_ref().syn_id() },
616            Self::TraitImpl { ptr_const, .. } => unsafe { ptr_const.as_ref().syn_id() },
617        }
618    }
619
620    pub fn syn_type(&self) -> &syn::Type {
621        match self {
622            Self::Free { ptr_const, .. } => unsafe { &ptr_const.as_ref().ty },
623            Self::Inher { ptr_const, .. } => unsafe { &ptr_const.as_ref().ty },
624            Self::TraitDefault { ptr_const, .. } => unsafe { &ptr_const.as_ref().ty },
625            Self::TraitImpl { ptr_const, .. } => unsafe { &ptr_const.as_ref().ty },
626        }
627    }
628
629    pub fn syn_expr(&self) -> Option<&syn::Expr> {
630        match self {
631            Self::Free { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().expr) },
632            Self::Inher { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().expr) },
633            Self::TraitDefault { ptr_const, .. } => unsafe {
634                ptr_const.as_ref().default.as_ref().map(|(_, expr)| expr)
635            },
636            Self::TraitImpl { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().expr) },
637        }
638    }
639
640    pub fn vis_node(&self) -> NodeIndex {
641        match self {
642            Self::Free { vis_node, .. } => *vis_node,
643            Self::Inher { vis_node, .. } => *vis_node,
644            Self::TraitDefault { vis_node, .. } => *vis_node,
645            Self::TraitImpl { vis_node, .. } => *vis_node,
646        }
647    }
648
649    pub fn type_id(&self) -> TypeId {
650        match self {
651            Self::Free { tid, .. } => *tid,
652            Self::Inher { tid, .. } => *tid,
653            Self::TraitDefault { tid, .. } => *tid,
654            Self::TraitImpl { tid, .. } => *tid,
655        }
656    }
657}
658
659impl fmt::Debug for Const {
660    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
661        let name = format::DebugBriefly::name(self);
662        let vis_node = self.vis_node();
663        let tid = self.type_id();
664
665        let mut d = f.debug_struct(name);
666        d.field("vis_node", &vis_node);
667        if let Some(creator) =
668            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
669        {
670            d.field("ty", &creator.create_owned_type(tid));
671        } else {
672            d.field("tid", &tid);
673        }
674        d.finish()
675    }
676}
677
678impl format::DebugBriefly for Const {
679    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
680        let name = format::DebugBriefly::name(self);
681        let tid = self.type_id();
682
683        let mut d = f.debug_struct(name);
684        if let Some(creator) =
685            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
686        {
687            d.field("ty", &creator.create_owned_type(tid));
688        } else {
689            d.field("tid", &tid);
690        }
691        d.finish()
692    }
693
694    fn name(&self) -> &'static str {
695        "Const"
696    }
697}
698
699impl AttributeHelper for Const {
700    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
701        match self {
702            Self::Free { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().attrs) },
703            Self::Inher { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().attrs) },
704            Self::TraitDefault { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().attrs) },
705            Self::TraitImpl { ptr_const, .. } => unsafe { Some(&ptr_const.as_ref().attrs) },
706        }
707    }
708
709    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
710        None // Read only
711    }
712}
713
714#[derive(Clone, PartialEq, Eq)]
715pub enum RawConst {
716    Free {
717        /// Read-only pointer to a syntax tree node that never changes.
718        ptr_const: NonNull<syn::ItemConst>,
719        /// Refer to the [module documentation](self).
720        vis_node: Option<NodeIndex>,
721        /// Type of this item.
722        tid: Option<TypeId>,
723    },
724}
725
726impl RawConst {
727    pub(crate) fn ptr_syn(&self) -> ConstPtr {
728        match self {
729            Self::Free { ptr_const, .. } => ConstPtr::Free(ptr_const.as_ptr().cast_const()),
730        }
731    }
732
733    pub(crate) fn syn_id(&self) -> SynId {
734        match self {
735            Self::Free { ptr_const, .. } => unsafe { ptr_const.as_ref().syn_id() },
736        }
737    }
738
739    pub(crate) fn syn_type(&self) -> &syn::Type {
740        match self {
741            Self::Free { ptr_const, .. } => unsafe { &ptr_const.as_ref().ty },
742        }
743    }
744
745    pub(crate) fn vis_node(&self) -> Option<NodeIndex> {
746        match self {
747            Self::Free { vis_node, .. } => *vis_node,
748        }
749    }
750
751    pub(crate) fn type_id(&self) -> Option<TypeId> {
752        match self {
753            Self::Free { tid, .. } => *tid,
754        }
755    }
756
757    pub(crate) fn visibility(&self) -> PathVis {
758        let vis = match self {
759            Self::Free { ptr_const, .. } => unsafe { &ptr_const.as_ref().vis },
760        };
761        PathVis::new(vis)
762    }
763}
764
765impl fmt::Debug for RawConst {
766    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
767        let name = format::DebugBriefly::name(self);
768        let vis_node = self.vis_node();
769        let tid = self.type_id();
770
771        let mut d = f.debug_struct(name);
772        d.field("vis_node", &vis_node);
773        if let Some(creator) =
774            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
775        {
776            d.field("ty", &tid.map(|tid| creator.create_owned_type(tid)));
777        } else {
778            d.field("tid", &tid);
779        }
780        d.finish()
781    }
782}
783
784impl format::DebugBriefly for RawConst {
785    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
786        let name = format::DebugBriefly::name(self);
787        let tid = self.type_id();
788
789        let mut d = f.debug_struct(name);
790        if let Some(creator) =
791            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
792        {
793            d.field("ty", &tid.map(|tid| creator.create_owned_type(tid)));
794        } else {
795            d.field("tid", &tid);
796        }
797        d.finish()
798    }
799
800    fn name(&self) -> &'static str {
801        "RawConst"
802    }
803}
804
805#[derive(Clone, PartialEq, Eq)]
806pub struct Enum {
807    /// Read-only pointer to a syntax tree node that never changes.
808    pub(crate) ptr_enum: NonNull<syn::ItemEnum>,
809
810    /// Refer to the [module documentation](self).
811    pub(crate) vis_node: NodeIndex,
812
813    /// Type of this item.
814    pub(crate) tid: TypeId,
815}
816
817impl Enum {
818    pub fn ptr_syn(&self) -> *const syn::ItemEnum {
819        self.ptr_enum.as_ptr().cast_const()
820    }
821
822    pub fn as_syn<'o>(&self) -> &'o syn::ItemEnum {
823        unsafe { self.ptr_enum.as_ref() }
824    }
825
826    pub fn syn_id(&self) -> SynId {
827        self.as_syn().syn_id()
828    }
829}
830
831impl fmt::Debug for Enum {
832    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
833        let name = format::DebugBriefly::name(self);
834        if let Some(creator) =
835            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
836        {
837            f.debug_struct(name)
838                .field("vis_node", &self.vis_node)
839                .field("ty", &creator.create_owned_type(self.tid))
840                .finish()
841        } else {
842            f.debug_struct(name)
843                .field("vis_node", &self.vis_node)
844                .field("tid", &self.tid)
845                .finish()
846        }
847    }
848}
849
850impl format::DebugBriefly for Enum {
851    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
852        let name = format::DebugBriefly::name(self);
853        if let Some(creator) =
854            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
855        {
856            f.debug_struct(name)
857                .field("ty", &creator.create_owned_type(self.tid))
858                .finish()
859        } else {
860            f.debug_struct(name).field("tid", &self.tid).finish()
861        }
862    }
863
864    fn name(&self) -> &'static str {
865        "Enum"
866    }
867}
868
869impl AttributeHelper for Enum {
870    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
871        Some(&self.as_syn().attrs)
872    }
873
874    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
875        None // Read only
876    }
877}
878
879#[derive(Clone, PartialEq, Eq)]
880pub struct RawEnum {
881    /// Read-only pointer to a syntax tree node that never changes.
882    pub(crate) ptr_enum: NonNull<syn::ItemEnum>,
883
884    /// Refer to the [module documentation](self).
885    pub(crate) vis_node: Option<NodeIndex>,
886}
887
888impl RawEnum {
889    pub(crate) fn as_syn<'o>(&self) -> &'o syn::ItemEnum {
890        unsafe { self.ptr_enum.as_ref() }
891    }
892
893    pub(crate) fn syn_id(&self) -> SynId {
894        self.as_syn().syn_id()
895    }
896
897    pub(crate) fn visibility(&self) -> PathVis {
898        PathVis::new(&self.as_syn().vis)
899    }
900}
901
902impl fmt::Debug for RawEnum {
903    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
904        let name = format::DebugBriefly::name(self);
905        f.debug_struct(name)
906            .field("vis_node", &self.vis_node)
907            .finish()
908    }
909}
910
911impl format::DebugBriefly for RawEnum {
912    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
913        let name = format::DebugBriefly::name(self);
914        f.debug_struct(name).finish()
915    }
916
917    fn name(&self) -> &'static str {
918        "RawEnum"
919    }
920}
921
922#[derive(Clone, PartialEq, Eq)]
923pub struct Field {
924    /// Read-only pointer to a syntax tree node that never changes.
925    pub(crate) ptr_field: NonNull<syn::Field>,
926
927    /// Refer to the [module documentation](self).
928    pub(crate) vis_node: NodeIndex,
929
930    /// Type of this item.
931    pub(crate) tid: TypeId,
932}
933
934impl Field {
935    pub fn ptr_syn(&self) -> *const syn::Field {
936        self.ptr_field.as_ptr().cast_const()
937    }
938
939    pub fn as_syn<'o>(&self) -> &'o syn::Field {
940        unsafe { self.ptr_field.as_ref() }
941    }
942
943    pub fn syn_id(&self) -> SynId {
944        self.as_syn().syn_id()
945    }
946}
947
948impl fmt::Debug for Field {
949    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
950        let name = format::DebugBriefly::name(self);
951        if let Some(creator) =
952            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
953        {
954            f.debug_struct(name)
955                .field("vis_node", &self.vis_node)
956                .field("ty", &creator.create_owned_type(self.tid))
957                .finish()
958        } else {
959            f.debug_struct(name)
960                .field("vis_node", &self.vis_node)
961                .field("tid", &self.tid)
962                .finish()
963        }
964    }
965}
966
967impl format::DebugBriefly for Field {
968    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
969        let name = format::DebugBriefly::name(self);
970        if let Some(creator) =
971            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
972        {
973            f.debug_struct(name)
974                .field("ty", &creator.create_owned_type(self.tid))
975                .finish()
976        } else {
977            f.debug_struct(name).field("tid", &self.tid).finish()
978        }
979    }
980
981    fn name(&self) -> &'static str {
982        "Field"
983    }
984}
985
986impl AttributeHelper for Field {
987    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
988        Some(&self.as_syn().attrs)
989    }
990
991    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
992        None // Read only
993    }
994}
995
996#[derive(Clone, PartialEq, Eq)]
997pub struct RawField {
998    /// Read-only pointer to a syntax tree node that never changes.
999    pub(crate) ptr_field: NonNull<syn::Field>,
1000
1001    /// Refer to the [module documentation](self).
1002    pub(crate) vis_node: Option<NodeIndex>,
1003}
1004
1005impl RawField {
1006    pub(crate) fn as_syn<'o>(&self) -> &'o syn::Field {
1007        unsafe { self.ptr_field.as_ref() }
1008    }
1009
1010    pub(crate) fn syn_id(&self) -> SynId {
1011        self.as_syn().syn_id()
1012    }
1013
1014    pub(crate) fn visibility(&self) -> PathVis {
1015        PathVis::new(&self.as_syn().vis)
1016    }
1017}
1018
1019impl fmt::Debug for RawField {
1020    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1021        let name = format::DebugBriefly::name(self);
1022        f.debug_struct(name)
1023            .field("vis_node", &self.vis_node)
1024            .finish()
1025    }
1026}
1027
1028impl format::DebugBriefly for RawField {
1029    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1030        let name = format::DebugBriefly::name(self);
1031        f.debug_struct(name).finish()
1032    }
1033
1034    fn name(&self) -> &'static str {
1035        "RawField"
1036    }
1037}
1038
1039#[derive(Clone, PartialEq, Eq)]
1040pub struct Fn {
1041    /// Read-only pointer to a syntax tree node that never changes.
1042    pub(crate) ptr_attr: NonNull<Vec<syn::Attribute>>,
1043    pub(crate) ptr_sig: NonNull<syn::Signature>,
1044    pub(crate) ptr_block: NonNull<syn::Block>,
1045
1046    /// Refer to the [module documentation](self).
1047    pub(crate) vis_node: NodeIndex,
1048
1049    /// Type of this item.
1050    pub(crate) tid: TypeId,
1051}
1052
1053impl Fn {
1054    pub fn ptr_syn_attr(&self) -> *const Vec<syn::Attribute> {
1055        self.ptr_attr.as_ptr().cast_const()
1056    }
1057
1058    pub fn ptr_syn_sig(&self) -> *const syn::Signature {
1059        self.ptr_sig.as_ptr().cast_const()
1060    }
1061
1062    pub fn ptr_syn_block(&self) -> *const syn::Block {
1063        self.ptr_block.as_ptr().cast_const()
1064    }
1065
1066    pub fn syn_attr<'o>(&self) -> &'o Vec<syn::Attribute> {
1067        unsafe { self.ptr_attr.as_ref() }
1068    }
1069
1070    pub fn syn_sig<'o>(&self) -> &'o syn::Signature {
1071        unsafe { self.ptr_sig.as_ref() }
1072    }
1073
1074    pub fn syn_block<'o>(&self) -> &'o syn::Block {
1075        unsafe { self.ptr_block.as_ref() }
1076    }
1077
1078    pub fn syn_id(&self) -> SynId {
1079        self.syn_block().syn_id()
1080    }
1081
1082    pub fn type_id(&self) -> TypeId {
1083        self.tid
1084    }
1085}
1086
1087impl fmt::Debug for Fn {
1088    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1089        let name = format::DebugBriefly::name(self);
1090        if let Some(creator) =
1091            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1092        {
1093            f.debug_struct(name)
1094                .field("vis_node", &self.vis_node)
1095                .field("ty", &creator.create_owned_type(self.tid))
1096                .finish()
1097        } else {
1098            f.debug_struct(name)
1099                .field("vis_node", &self.vis_node)
1100                .field("tid", &self.tid)
1101                .finish()
1102        }
1103    }
1104}
1105
1106impl format::DebugBriefly for Fn {
1107    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1108        let name = format::DebugBriefly::name(self);
1109        if let Some(creator) =
1110            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1111        {
1112            f.debug_struct(name)
1113                .field("ty", &creator.create_owned_type(self.tid))
1114                .finish()
1115        } else {
1116            f.debug_struct(name).field("tid", &self.tid).finish()
1117        }
1118    }
1119
1120    fn name(&self) -> &'static str {
1121        "Fn"
1122    }
1123}
1124
1125impl AttributeHelper for Fn {
1126    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1127        Some(self.syn_attr())
1128    }
1129
1130    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1131        None // Read only
1132    }
1133}
1134
1135#[derive(Clone, PartialEq, Eq)]
1136pub struct RawFn {
1137    /// Read-only pointer to a syntax tree node that never changes.
1138    pub(crate) ptr_attr: NonNull<Vec<syn::Attribute>>,
1139    pub(crate) ptr_vis: NonNull<syn::Visibility>,
1140    pub(crate) ptr_sig: NonNull<syn::Signature>,
1141    pub(crate) ptr_block: NonNull<syn::Block>,
1142
1143    /// Refer to the [module documentation](self).
1144    pub(crate) vis_node: Option<NodeIndex>,
1145
1146    /// TODO: Need comment about this at the module level.
1147    //
1148    // Node index to the parent module.
1149    pub(crate) unscoped_base: Option<NodeIndex>,
1150}
1151
1152impl RawFn {
1153    pub(crate) fn as_syn_vis<'o>(&self) -> &'o syn::Visibility {
1154        unsafe { self.ptr_vis.as_ref() }
1155    }
1156
1157    pub(crate) fn as_syn_sig<'o>(&self) -> &'o syn::Signature {
1158        unsafe { self.ptr_sig.as_ref() }
1159    }
1160
1161    pub(crate) fn as_syn_block<'o>(&self) -> &'o syn::Block {
1162        unsafe { self.ptr_block.as_ref() }
1163    }
1164
1165    pub(crate) fn syn_id(&self) -> SynId {
1166        self.as_syn_block().syn_id()
1167    }
1168
1169    pub(crate) fn visibility(&self) -> PathVis {
1170        PathVis::new(self.as_syn_vis())
1171    }
1172}
1173
1174impl fmt::Debug for RawFn {
1175    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1176        let name = format::DebugBriefly::name(self);
1177        f.debug_struct(name)
1178            .field("vis_node", &self.vis_node)
1179            .field("unscoped_base", &self.unscoped_base)
1180            .finish()
1181    }
1182}
1183
1184impl format::DebugBriefly for RawFn {
1185    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1186        let name = format::DebugBriefly::name(self);
1187        f.debug_struct(name).finish()
1188    }
1189
1190    fn name(&self) -> &'static str {
1191        "RawFn"
1192    }
1193}
1194
1195#[derive(Clone, PartialEq, Eq)]
1196pub struct Local {
1197    /// Read-only pointer to a syntax tree node that never changes.
1198    pub(crate) ptr_attr: NonNull<Vec<syn::Attribute>>,
1199    pub(crate) ptr_ident: Which2<NonNull<syn::PatIdent>, NonNull<syn::Receiver>>,
1200
1201    /// Optional explicit syn type that may attach to the local.
1202    pub(crate) ptr_ty: Option<NonNull<syn::Type>>,
1203
1204    /// Type of this item.
1205    pub(crate) tid: TypeId,
1206}
1207
1208impl Local {
1209    pub fn ptr_syn_attr(&self) -> *const Vec<syn::Attribute> {
1210        self.ptr_attr.as_ptr().cast_const()
1211    }
1212
1213    pub fn ptr_syn_ident(&self) -> Which2<*const syn::PatIdent, *const syn::Receiver> {
1214        match self.ptr_ident {
1215            Which2::A(ptr) => Which2::A(ptr.as_ptr().cast_const()),
1216            Which2::B(ptr) => Which2::B(ptr.as_ptr().cast_const()),
1217        }
1218    }
1219
1220    pub fn ptr_syn_ty(&self) -> Option<*const syn::Type> {
1221        self.ptr_ty.map(|ptr| ptr.as_ptr().cast_const())
1222    }
1223
1224    pub fn as_syn<'o>(&self) -> Which2<&'o syn::PatIdent, &'o syn::Receiver> {
1225        match &self.ptr_ident {
1226            Which2::A(ptr) => Which2::A(unsafe { ptr.as_ref() }),
1227            Which2::B(ptr) => Which2::B(unsafe { ptr.as_ref() }),
1228        }
1229    }
1230
1231    pub fn as_syn_attr<'o>(&self) -> &'o Vec<syn::Attribute> {
1232        unsafe { self.ptr_attr.as_ref() }
1233    }
1234
1235    pub fn syn_id(&self) -> SynId {
1236        match self.as_syn() {
1237            Which2::A(pat_ident) => pat_ident.syn_id(),
1238            Which2::B(recv) => recv.syn_id(),
1239        }
1240    }
1241
1242    pub fn syn_type<'o>(&self) -> Option<&'o syn::Type> {
1243        self.ptr_ty.map(|ptr| unsafe { ptr.as_ref() })
1244    }
1245}
1246
1247impl fmt::Debug for Local {
1248    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1249        let name = format::DebugBriefly::name(self);
1250        if let Some(creator) =
1251            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1252        {
1253            f.debug_struct(name)
1254                .field("ty", &creator.create_owned_type(self.tid))
1255                .finish()
1256        } else {
1257            f.debug_struct(name).field("tid", &self.tid).finish()
1258        }
1259    }
1260}
1261
1262impl format::DebugBriefly for Local {
1263    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1264        let name = format::DebugBriefly::name(self);
1265        if let Some(creator) =
1266            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1267        {
1268            f.debug_struct(name)
1269                .field("ty", &creator.create_owned_type(self.tid))
1270                .finish()
1271        } else {
1272            f.debug_struct(name).field("tid", &self.tid).finish()
1273        }
1274    }
1275
1276    fn name(&self) -> &'static str {
1277        "Local"
1278    }
1279}
1280
1281impl AttributeHelper for Local {
1282    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1283        Some(self.as_syn_attr())
1284    }
1285
1286    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1287        None // Read only
1288    }
1289}
1290
1291#[derive(Clone, PartialEq, Eq)]
1292pub struct RawLocal {
1293    /// Read-only pointer to a syntax tree node that never changes.
1294    pub(crate) ptr_attr: NonNull<Vec<syn::Attribute>>,
1295    pub(crate) ptr_ident: Which2<NonNull<syn::PatIdent>, NonNull<syn::Receiver>>,
1296
1297    /// Optional explicit syn type that may attach to the local.
1298    pub(crate) ptr_ty: Option<NonNull<syn::Type>>,
1299
1300    pub(crate) mut_: bool,
1301}
1302
1303impl RawLocal {
1304    pub(crate) fn as_syn<'o>(&self) -> Which2<&'o syn::PatIdent, &'o syn::Receiver> {
1305        match &self.ptr_ident {
1306            Which2::A(ptr) => Which2::A(unsafe { ptr.as_ref() }),
1307            Which2::B(ptr) => Which2::B(unsafe { ptr.as_ref() }),
1308        }
1309    }
1310
1311    pub(crate) fn syn_id(&self) -> SynId {
1312        match self.as_syn() {
1313            Which2::A(pat_ident) => pat_ident.syn_id(),
1314            Which2::B(recv) => recv.syn_id(),
1315        }
1316    }
1317}
1318
1319impl fmt::Debug for RawLocal {
1320    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1321        let name = format::DebugBriefly::name(self);
1322        f.debug_struct(name).field("mut", &self.mut_).finish()
1323    }
1324}
1325
1326impl format::DebugBriefly for RawLocal {
1327    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1328        let name = format::DebugBriefly::name(self);
1329        f.debug_struct(name).finish()
1330    }
1331
1332    fn name(&self) -> &'static str {
1333        "RawLocal"
1334    }
1335}
1336
1337#[derive(Clone, PartialEq, Eq)]
1338pub struct Mod {
1339    /// Read-only pointer to a syntax tree node that never changes.
1340    ///
1341    /// This points to 'mod', not a file, so if the module was loaded as a file, then this could
1342    /// be None.
1343    pub(crate) ptr_mod: Option<NonNull<syn::ItemMod>>,
1344
1345    /// Pointer to a file if the module was loaded as a file, not by 'mod'.
1346    pub(crate) ptr_file: Option<NonNull<syn::File>>,
1347
1348    /// Refer to the [module documentation](self).
1349    pub(crate) vis_node: NodeIndex,
1350
1351    /// Absolute file path that represent this module.
1352    ///
1353    /// ```ignore
1354    /// // /a.rs
1355    /// mod b {           // fpath: /a/b
1356    ///     mod c;        // fpath: /a/b/c.rs or /a/b/c/mod.rs
1357    /// }
1358    /// #[path = "d.rs"]
1359    /// mod d;            // fpath: /d.rs
1360    /// mod e;            // fpath: /a/e.rs or /a/e/mod.rs
1361    /// ```
1362    pub(crate) fpath: PathBuf,
1363
1364    /// - True if the file is one of "mod.rs", "main.rs", or "lib.rs".
1365    /// - True if the file is determined by "path" attribute and the module is not inline (e.g.
1366    ///   #[path = "a.rs"] mod foo;)
1367    /// - False otherwise.
1368    pub(crate) mod_rs: bool,
1369}
1370
1371impl Mod {
1372    pub fn as_syn_mod<'o>(&self) -> Option<&'o syn::ItemMod> {
1373        self.ptr_mod.map(|ptr| unsafe { ptr.as_ref() })
1374    }
1375
1376    pub fn as_syn_file<'o>(&self) -> Option<&'o syn::File> {
1377        self.ptr_file.map(|ptr| unsafe { ptr.as_ref() })
1378    }
1379
1380    pub fn syn_id(&self) -> SynId {
1381        // Real mod must have one of the two pointers, but virtual mod, like root, may not.
1382        self.ptr_mod
1383            .map(|ptr| unsafe { ptr.as_ref().syn_id() })
1384            .unwrap_or(unsafe { self.ptr_file.expect("nullptr Mod").as_ref().syn_id() })
1385    }
1386
1387    pub fn file_path(&self) -> &StdPath {
1388        self.fpath.as_path()
1389    }
1390
1391    pub fn is_inline(&self) -> bool {
1392        if let Some(item_mod) = self.as_syn_mod() {
1393            item_mod.content.is_some()
1394        } else {
1395            false
1396        }
1397    }
1398}
1399
1400impl fmt::Debug for Mod {
1401    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1402        let name = format::DebugBriefly::name(self);
1403        f.debug_struct(name)
1404            .field("vis_node", &self.vis_node)
1405            .field("fpath", &self.fpath)
1406            .field("mod_rs", &self.mod_rs)
1407            .finish()
1408    }
1409}
1410
1411impl format::DebugBriefly for Mod {
1412    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1413        let name = format::DebugBriefly::name(self);
1414        f.debug_struct(name).finish()
1415    }
1416
1417    fn name(&self) -> &'static str {
1418        "Mod"
1419    }
1420}
1421
1422impl AttributeHelper for Mod {
1423    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1424        self.as_syn_mod().map(|item_mod| &item_mod.attrs)
1425    }
1426
1427    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1428        None // Read only
1429    }
1430}
1431
1432#[derive(Clone, PartialEq, Eq)]
1433pub struct RawMod {
1434    /// Read-only pointer to a syntax tree node that never changes.
1435    ///
1436    /// This points to 'mod', not a file, so if the module was loaded as a file, then this could
1437    /// be None.
1438    pub(crate) ptr_mod: Option<NonNull<syn::ItemMod>>,
1439
1440    /// Pointer to a file if the module was loaded as a file, not by 'mod'.
1441    pub(crate) ptr_file: Option<NonNull<syn::File>>,
1442
1443    /// Refer to the [module documentation](self).
1444    pub(crate) vis_node: Option<NodeIndex>,
1445    pub(crate) fpath: PathBuf,
1446    pub(crate) mod_rs: bool,
1447}
1448
1449impl RawMod {
1450    pub(crate) fn as_syn_mod<'o>(&self) -> Option<&'o syn::ItemMod> {
1451        self.ptr_mod.map(|ptr| unsafe { ptr.as_ref() })
1452    }
1453
1454    pub(crate) fn syn_id(&self) -> SynId {
1455        // Real mod must have one of the two pointers, but virtual mod, like root, may not.
1456        self.ptr_mod
1457            .map(|ptr| unsafe { ptr.as_ref().syn_id() })
1458            .unwrap_or(unsafe { self.ptr_file.expect("nullptr Mod").as_ref().syn_id() })
1459    }
1460
1461    pub(crate) fn is_inline(&self) -> bool {
1462        if let Some(item_mod) = self.as_syn_mod() {
1463            item_mod.content.is_some()
1464        } else {
1465            false
1466        }
1467    }
1468
1469    pub(crate) fn visibility(&self) -> PathVis {
1470        if let Some(item_mod) = self.as_syn_mod() {
1471            PathVis::new(&item_mod.vis)
1472        } else {
1473            PathVis::Private
1474        }
1475    }
1476}
1477
1478impl fmt::Debug for RawMod {
1479    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1480        let name = format::DebugBriefly::name(self);
1481        f.debug_struct(name)
1482            .field("vis_node", &self.vis_node)
1483            .field("fpath", &self.fpath)
1484            .field("mod_rs", &self.mod_rs)
1485            .finish()
1486    }
1487}
1488
1489impl format::DebugBriefly for RawMod {
1490    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1491        let name = format::DebugBriefly::name(self);
1492        f.debug_struct(name).finish()
1493    }
1494
1495    fn name(&self) -> &'static str {
1496        "RawMod"
1497    }
1498}
1499
1500#[derive(Clone, PartialEq, Eq)]
1501pub struct Struct {
1502    /// Read-only pointer to a syntax tree node that never changes.
1503    pub(crate) ptr_struct: NonNull<syn::ItemStruct>,
1504
1505    /// Refer to the [module documentation](self).
1506    pub(crate) vis_node: NodeIndex,
1507
1508    /// Type of this item.
1509    pub(crate) tid: TypeId,
1510}
1511
1512impl Struct {
1513    pub fn ptr_syn(&self) -> *const syn::ItemStruct {
1514        self.ptr_struct.as_ptr().cast_const()
1515    }
1516
1517    pub fn as_syn<'o>(&self) -> &'o syn::ItemStruct {
1518        unsafe { self.ptr_struct.as_ref() }
1519    }
1520
1521    pub fn syn_id(&self) -> SynId {
1522        self.as_syn().syn_id()
1523    }
1524
1525    pub fn type_id(&self) -> TypeId {
1526        self.tid
1527    }
1528}
1529
1530impl fmt::Debug for Struct {
1531    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1532        let name = format::DebugBriefly::name(self);
1533        if let Some(creator) =
1534            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1535        {
1536            f.debug_struct(name)
1537                .field("vis_node", &self.vis_node)
1538                .field("ty", &creator.create_owned_type(self.tid))
1539                .finish()
1540        } else {
1541            f.debug_struct(name)
1542                .field("vis_node", &self.vis_node)
1543                .field("tid", &self.tid)
1544                .finish()
1545        }
1546    }
1547}
1548
1549impl format::DebugBriefly for Struct {
1550    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1551        let name = format::DebugBriefly::name(self);
1552        f.debug_struct(name).finish()
1553    }
1554
1555    fn name(&self) -> &'static str {
1556        "Struct"
1557    }
1558}
1559
1560impl AttributeHelper for Struct {
1561    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1562        Some(&self.as_syn().attrs)
1563    }
1564
1565    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1566        None // Read only
1567    }
1568}
1569
1570#[derive(Clone, PartialEq, Eq)]
1571pub struct RawStruct {
1572    /// Read-only pointer to a syntax tree node that never changes.
1573    pub(crate) ptr_struct: NonNull<syn::ItemStruct>,
1574
1575    /// Refer to the [module documentation](self).
1576    pub(crate) vis_node: Option<NodeIndex>,
1577}
1578
1579impl RawStruct {
1580    pub(crate) fn as_syn<'o>(&self) -> &'o syn::ItemStruct {
1581        unsafe { self.ptr_struct.as_ref() }
1582    }
1583
1584    pub(crate) fn syn_id(&self) -> SynId {
1585        self.as_syn().syn_id()
1586    }
1587
1588    pub(crate) fn visibility(&self) -> PathVis {
1589        PathVis::new(&self.as_syn().vis)
1590    }
1591}
1592
1593impl fmt::Debug for RawStruct {
1594    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1595        let name = format::DebugBriefly::name(self);
1596        f.debug_struct(name)
1597            .field("vis_node", &self.vis_node)
1598            .finish()
1599    }
1600}
1601
1602impl format::DebugBriefly for RawStruct {
1603    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1604        let name = format::DebugBriefly::name(self);
1605        f.debug_struct(name).finish()
1606    }
1607
1608    fn name(&self) -> &'static str {
1609        "RawStruct"
1610    }
1611}
1612
1613#[derive(Clone, PartialEq, Eq)]
1614pub struct Trait {
1615    /// Read-only pointer to a syntax tree node that never changes.
1616    pub(crate) ptr_trait: NonNull<syn::ItemTrait>,
1617
1618    /// Refer to the [module documentation](self).
1619    pub(crate) vis_node: NodeIndex,
1620}
1621
1622impl Trait {
1623    pub fn ptr_syn(&self) -> *const syn::ItemTrait {
1624        self.ptr_trait.as_ptr().cast_const()
1625    }
1626
1627    pub fn as_syn<'o>(&self) -> &'o syn::ItemTrait {
1628        unsafe { self.ptr_trait.as_ref() }
1629    }
1630
1631    pub fn syn_id(&self) -> SynId {
1632        self.as_syn().syn_id()
1633    }
1634}
1635
1636impl fmt::Debug for Trait {
1637    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1638        let name = format::DebugBriefly::name(self);
1639        f.debug_struct(name)
1640            .field("vis_node", &self.vis_node)
1641            .finish()
1642    }
1643}
1644
1645impl format::DebugBriefly for Trait {
1646    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1647        let name = format::DebugBriefly::name(self);
1648        f.debug_struct(name).finish()
1649    }
1650
1651    fn name(&self) -> &'static str {
1652        "Trait"
1653    }
1654}
1655
1656impl AttributeHelper for Trait {
1657    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1658        Some(&self.as_syn().attrs)
1659    }
1660
1661    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1662        None // Read only
1663    }
1664}
1665
1666#[derive(Clone, PartialEq, Eq)]
1667pub struct RawTrait {
1668    /// Read-only pointer to a syntax tree node that never changes.
1669    pub(crate) ptr_trait: NonNull<syn::ItemTrait>,
1670
1671    /// Refer to the [module documentation](self).
1672    pub(crate) vis_node: Option<NodeIndex>,
1673}
1674
1675impl RawTrait {
1676    pub(crate) fn as_syn<'o>(&self) -> &'o syn::ItemTrait {
1677        unsafe { self.ptr_trait.as_ref() }
1678    }
1679
1680    pub(crate) fn syn_id(&self) -> SynId {
1681        self.as_syn().syn_id()
1682    }
1683
1684    pub(crate) fn visibility(&self) -> PathVis {
1685        PathVis::new(&self.as_syn().vis)
1686    }
1687}
1688
1689impl fmt::Debug for RawTrait {
1690    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1691        let name = format::DebugBriefly::name(self);
1692        f.debug_struct(name)
1693            .field("vis_node", &self.vis_node)
1694            .finish()
1695    }
1696}
1697
1698impl format::DebugBriefly for RawTrait {
1699    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1700        let name = format::DebugBriefly::name(self);
1701        f.debug_struct(name).finish()
1702    }
1703
1704    fn name(&self) -> &'static str {
1705        "RawTrait"
1706    }
1707}
1708
1709#[derive(Clone, PartialEq, Eq)]
1710pub struct TypeAlias {
1711    /// Read-only pointer to a syntax tree node that never changes.
1712    pub(crate) ptr_type: NonNull<syn::ItemType>,
1713
1714    /// Refer to the [module documentation](self).
1715    pub(crate) vis_node: NodeIndex,
1716
1717    /// Type of this item.
1718    pub(crate) tid: TypeId,
1719}
1720
1721impl TypeAlias {
1722    pub fn ptr_syn(&self) -> *const syn::ItemType {
1723        self.ptr_type.as_ptr().cast_const()
1724    }
1725
1726    pub fn as_syn<'o>(&self) -> &'o syn::ItemType {
1727        unsafe { self.ptr_type.as_ref() }
1728    }
1729
1730    pub fn syn_id(&self) -> SynId {
1731        self.as_syn().syn_id()
1732    }
1733}
1734
1735impl fmt::Debug for TypeAlias {
1736    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1737        let name = format::DebugBriefly::name(self);
1738        if let Some(creator) =
1739            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1740        {
1741            f.debug_struct(name)
1742                .field("vis_node", &self.vis_node)
1743                .field("ty", &creator.create_owned_type(self.tid))
1744                .finish()
1745        } else {
1746            f.debug_struct(name)
1747                .field("vis_node", &self.vis_node)
1748                .field("tid", &self.tid)
1749                .finish()
1750        }
1751    }
1752}
1753
1754impl format::DebugBriefly for TypeAlias {
1755    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1756        let name = format::DebugBriefly::name(self);
1757        if let Some(creator) =
1758            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1759        {
1760            f.debug_struct(name)
1761                .field("ty", &creator.create_owned_type(self.tid))
1762                .finish()
1763        } else {
1764            f.debug_struct(name).field("tid", &self.tid).finish()
1765        }
1766    }
1767
1768    fn name(&self) -> &'static str {
1769        "TypeAlias"
1770    }
1771}
1772
1773impl AttributeHelper for TypeAlias {
1774    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1775        Some(&self.as_syn().attrs)
1776    }
1777
1778    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1779        None // Read only
1780    }
1781}
1782
1783#[derive(Clone, PartialEq, Eq)]
1784pub struct RawTypeAlias {
1785    /// Read-only pointer to a syntax tree node that never changes.
1786    pub(crate) ptr_type: NonNull<syn::ItemType>,
1787
1788    /// Refer to the [module documentation](self).
1789    pub(crate) vis_node: Option<NodeIndex>,
1790}
1791
1792impl RawTypeAlias {
1793    pub(crate) fn as_syn<'o>(&self) -> &'o syn::ItemType {
1794        unsafe { self.ptr_type.as_ref() }
1795    }
1796
1797    pub(crate) fn syn_id(&self) -> SynId {
1798        self.as_syn().syn_id()
1799    }
1800
1801    pub(crate) fn visibility(&self) -> PathVis {
1802        PathVis::new(&self.as_syn().vis)
1803    }
1804}
1805
1806impl fmt::Debug for RawTypeAlias {
1807    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1808        let name = format::DebugBriefly::name(self);
1809        f.debug_struct(name)
1810            .field("vis_node", &self.vis_node)
1811            .finish()
1812    }
1813}
1814
1815impl format::DebugBriefly for RawTypeAlias {
1816    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1817        let name = format::DebugBriefly::name(self);
1818        f.debug_struct(name).finish()
1819    }
1820
1821    fn name(&self) -> &'static str {
1822        "RawTypeAlias"
1823    }
1824}
1825
1826#[derive(Clone, PartialEq, Eq)]
1827pub struct Use {
1828    /// Read-only pointer to a syntax tree node that never changes.
1829    pub(crate) ptr_group: NonNull<syn::ItemUse>,
1830
1831    /// Abstract pointer to a specific item in the [`syn::ItemUse`].
1832    ///
1833    /// For instance, if [`Self::syn_group`] points to 'use a::{b, c, \*}', this field points to
1834    /// 'b', 'c', or '\*'.
1835    pub(crate) syn_part: SynId,
1836
1837    /// Refer to the [module documentation](self).
1838    pub(crate) vis_node: NodeIndex,
1839    pub(crate) dst: PathId,
1840}
1841
1842impl Use {
1843    pub fn ptr_syn(&self) -> *const syn::ItemUse {
1844        self.ptr_group.as_ptr().cast_const()
1845    }
1846
1847    pub fn syn_id(&self) -> SynId {
1848        self.syn_part
1849    }
1850}
1851
1852impl fmt::Debug for Use {
1853    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1854        let name = format::DebugBriefly::name(self);
1855        f.debug_struct(name)
1856            .field("vis_node", &self.vis_node)
1857            .field("dst", &self.dst)
1858            .finish()
1859    }
1860}
1861
1862impl format::DebugBriefly for Use {
1863    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1864        let name = format::DebugBriefly::name(self);
1865        f.debug_struct(name).field("dst", &self.dst).finish()
1866    }
1867
1868    fn name(&self) -> &'static str {
1869        "Use"
1870    }
1871}
1872
1873impl AttributeHelper for Use {
1874    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
1875        unsafe { Some(&self.ptr_group.as_ref().attrs) }
1876    }
1877
1878    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
1879        None // Read only
1880    }
1881}
1882
1883#[derive(Clone, PartialEq, Eq)]
1884pub struct RawUse {
1885    /// Read-only pointer to a syntax tree node that never changes.
1886    pub(crate) ptr_group: NonNull<syn::ItemUse>,
1887
1888    /// Abstract pointer to a specific item in the [`syn::ItemUse`].
1889    ///
1890    /// For instance, if [`Self::syn_group`] points to 'use a::{b, c, \*}', this field points to
1891    /// 'b', 'c', or '\*'.
1892    pub(crate) syn_part: SynId,
1893
1894    /// Refer to the [module documentation](self).
1895    pub(crate) vis_node: Option<NodeIndex>,
1896    pub(crate) npath: String,
1897    pub(crate) dst_node: Option<NodeIndex>,
1898}
1899
1900impl RawUse {
1901    pub(crate) fn syn_id(&self) -> SynId {
1902        self.syn_part
1903    }
1904
1905    pub(crate) fn visibility(&self) -> PathVis {
1906        let group = unsafe { self.ptr_group.as_ref() };
1907        PathVis::new(&group.vis)
1908    }
1909}
1910
1911impl RawUse {
1912    pub(crate) fn is_glob(&self) -> bool {
1913        self.npath.ends_with("*")
1914    }
1915}
1916
1917impl fmt::Debug for RawUse {
1918    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1919        let name = format::DebugBriefly::name(self);
1920        f.debug_struct(name)
1921            .field("vis_node", &self.vis_node)
1922            .field("npath", &self.npath)
1923            .field("dst_node", &self.dst_node)
1924            .finish()
1925    }
1926}
1927
1928impl format::DebugBriefly for RawUse {
1929    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1930        let name = format::DebugBriefly::name(self);
1931        f.debug_struct(name).finish()
1932    }
1933
1934    fn name(&self) -> &'static str {
1935        "RawUse"
1936    }
1937}
1938
1939#[derive(Clone, PartialEq, Eq)]
1940pub struct Variant {
1941    /// Read-only pointer to a syntax tree node that never changes.
1942    pub(crate) ptr_variant: NonNull<syn::Variant>,
1943
1944    /// Refer to the [module documentation](self).
1945    pub(crate) vis_node: NodeIndex,
1946
1947    /// Type of this item. This is the same id as enum's for now.
1948    pub(crate) tid: TypeId,
1949
1950    pub(crate) nth: usize,
1951
1952    /// Discriminant of the variant.
1953    pub(crate) disc: isize,
1954}
1955
1956impl Variant {
1957    pub fn ptr_syn(&self) -> *const syn::Variant {
1958        self.ptr_variant.as_ptr().cast_const()
1959    }
1960
1961    pub fn as_syn<'o>(&self) -> &'o syn::Variant {
1962        unsafe { self.ptr_variant.as_ref() }
1963    }
1964
1965    pub fn syn_id(&self) -> SynId {
1966        self.as_syn().syn_id()
1967    }
1968
1969    pub fn type_id(&self) -> TypeId {
1970        self.tid
1971    }
1972}
1973
1974impl fmt::Debug for Variant {
1975    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1976        let name = format::DebugBriefly::name(self);
1977        if let Some(creator) =
1978            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
1979        {
1980            f.debug_struct(name)
1981                .field("vis_node", &self.vis_node)
1982                .field("ty", &creator.create_owned_type(self.tid))
1983                .field("disc", &self.disc)
1984                .finish()
1985        } else {
1986            f.debug_struct(name)
1987                .field("vis_node", &self.vis_node)
1988                .field("tid", &self.tid)
1989                .field("disc", &self.disc)
1990                .finish()
1991        }
1992    }
1993}
1994
1995impl format::DebugBriefly for Variant {
1996    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
1997        let name = format::DebugBriefly::name(self);
1998        if let Some(creator) =
1999            unsafe { OWNED_TYPE_CREATOR.with(|creator| creator.get().map(|ptr| ptr.as_ref())) }
2000        {
2001            f.debug_struct(name)
2002                .field("ty", &creator.create_owned_type(self.tid))
2003                .finish()
2004        } else {
2005            f.debug_struct(name).field("tid", &self.tid).finish()
2006        }
2007    }
2008
2009    fn name(&self) -> &'static str {
2010        "Variant"
2011    }
2012}
2013
2014impl AttributeHelper for Variant {
2015    fn get_attributes(&self) -> Option<&Vec<syn::Attribute>> {
2016        Some(&self.as_syn().attrs)
2017    }
2018
2019    fn get_mut_attributes(&mut self) -> Option<&mut Vec<syn::Attribute>> {
2020        None // Read only
2021    }
2022}
2023
2024#[derive(Clone, PartialEq, Eq)]
2025pub struct RawVariant {
2026    /// Read-only pointer to a syntax tree node that never changes.
2027    pub(crate) ptr_variant: NonNull<syn::Variant>,
2028
2029    /// Refer to the [module documentation](self).
2030    pub(crate) vis_node: Option<NodeIndex>,
2031
2032    pub(crate) nth: usize,
2033}
2034
2035impl RawVariant {
2036    pub(crate) fn as_syn<'o>(&self) -> &'o syn::Variant {
2037        unsafe { self.ptr_variant.as_ref() }
2038    }
2039
2040    pub(crate) fn syn_id(&self) -> SynId {
2041        self.as_syn().syn_id()
2042    }
2043}
2044
2045impl fmt::Debug for RawVariant {
2046    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2047        let name = format::DebugBriefly::name(self);
2048        f.debug_struct(name)
2049            .field("vis_node", &self.vis_node)
2050            .finish()
2051    }
2052}
2053
2054impl format::DebugBriefly for RawVariant {
2055    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, _filter: &PrintFilter) -> fmt::Result {
2056        let name = format::DebugBriefly::name(self);
2057        f.debug_struct(name).finish()
2058    }
2059
2060    fn name(&self) -> &'static str {
2061        "RawVariant"
2062    }
2063}
2064
2065#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2066pub struct ItemIndex(pub(crate) usize);
2067
2068impl From<usize> for ItemIndex {
2069    fn from(value: usize) -> Self {
2070        Self(value)
2071    }
2072}
2073
2074impl fmt::Display for ItemIndex {
2075    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2076        self.0.fmt(f)
2077    }
2078}
2079
2080impl fmt::Debug for ItemIndex {
2081    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2082        self.0.fmt(f)
2083    }
2084}
2085
2086impl PartialEq<usize> for ItemIndex {
2087    fn eq(&self, other: &usize) -> bool {
2088        self.0.eq(other)
2089    }
2090}
2091
2092impl PartialOrd<usize> for ItemIndex {
2093    fn partial_cmp(&self, other: &usize) -> Option<cmp::Ordering> {
2094        self.0.partial_cmp(other)
2095    }
2096}
2097
2098impl ops::Add<usize> for ItemIndex {
2099    type Output = Self;
2100
2101    fn add(self, rhs: usize) -> Self::Output {
2102        Self(self.0 + rhs)
2103    }
2104}
2105
2106impl ops::AddAssign<usize> for ItemIndex {
2107    fn add_assign(&mut self, rhs: usize) {
2108        self.0 += rhs;
2109    }
2110}
2111
2112impl ops::Index<ItemIndex> for [PrivItem] {
2113    type Output = PrivItem;
2114
2115    fn index(&self, index: ItemIndex) -> &Self::Output {
2116        &self[index.0]
2117    }
2118}
2119
2120impl ops::IndexMut<ItemIndex> for [PrivItem] {
2121    fn index_mut(&mut self, index: ItemIndex) -> &mut Self::Output {
2122        &mut self[index.0]
2123    }
2124}
2125
2126impl ops::Index<ItemIndex> for SmallVec<[PrivItem; 1]> {
2127    type Output = PrivItem;
2128
2129    fn index(&self, index: ItemIndex) -> &Self::Output {
2130        &self.as_slice()[index]
2131    }
2132}
2133
2134impl ops::IndexMut<ItemIndex> for SmallVec<[PrivItem; 1]> {
2135    fn index_mut(&mut self, index: ItemIndex) -> &mut Self::Output {
2136        &mut self.as_mut_slice()[index]
2137    }
2138}
2139
2140#[derive(Debug, Clone, Default)]
2141pub(crate) enum PathVis {
2142    Pub,
2143    PubCrate,
2144    PubSuper,
2145    PubPath(String),
2146    #[default]
2147    Private,
2148}
2149
2150impl PathVis {
2151    pub(crate) fn new(vis: &syn::Visibility) -> Self {
2152        match vis {
2153            syn::Visibility::Public(_) => PathVis::Pub,
2154            syn::Visibility::Restricted(syn::VisRestricted { path, .. }) => {
2155                let path = util::get_name_path_from_syn_path(path);
2156                match path.as_str() {
2157                    "crate" => PathVis::PubCrate,
2158                    "super" => PathVis::PubSuper,
2159                    "self" => PathVis::Private,
2160                    _ => PathVis::PubPath(path),
2161                }
2162            }
2163            syn::Visibility::Inherited => PathVis::Private,
2164        }
2165    }
2166}
2167
2168#[derive(Debug, PartialEq, Eq)]
2169pub enum ConstPtr {
2170    Free(*const syn::ItemConst),
2171    Inher(*const syn::ImplItemConst),
2172    TraitDefault(*const syn::TraitItemConst),
2173    TraitImpl(*const syn::ImplItemConst),
2174}
2175
2176pub trait ItemTrait {
2177    /// Returns true if both items are made from the same syn node and resolved data will be the
2178    /// same.
2179    ///
2180    /// For example, [`PrivItem::Fn`] and [`PrivItem::RawFn`] can be the same if they are made from
2181    /// the same syn function node.
2182    fn is_effective_same(&self, other: &Self) -> bool;
2183
2184    /// Returns a kind of the item regardless of whether the item is resolved or not.
2185    ///
2186    /// For example, [`PrivItem::Fn`] and [`PrivItem::RawFn`] returns the same
2187    /// [`EffectiveItemKind::Fn`].
2188    fn effective_kind(&self) -> EffectiveItemKind;
2189
2190    fn vis_node(&self) -> TriOption<NodeIndex, ()>;
2191    fn type_id(&self) -> TriOption<TypeId, ()>;
2192    fn syn_id(&self) -> Option<SynId>;
2193
2194    fn as_fn(&self) -> Option<&Fn>;
2195    fn as_struct(&self) -> Option<&Struct>;
2196    fn as_type_alias(&self) -> Option<&TypeAlias>;
2197    fn as_use(&self) -> Option<&Use>;
2198    fn as_variant(&self) -> Option<&Variant>;
2199    fn as_mut_mod(&mut self) -> Option<&mut Mod>;
2200}
2201
2202impl ItemTrait for PrivItem {
2203    #[rustfmt::skip]
2204    fn is_effective_same(&self, other: &Self) -> bool {
2205        let (l, r) = (self, other);
2206        match l {
2207            Self::Block(l) => matches!(r, Self::Block(r) if l.ptr_block == r.ptr_block),
2208            Self::Const(l) => match r {
2209                Self::Const(r) => l.ptr_syn() == r.ptr_syn(),
2210                Self::RawConst(r) => l.ptr_syn() == r.ptr_syn(),
2211                _ => false,
2212            },
2213            Self::Enum(Enum { ptr_enum: l_ptr, .. }) => matches!(r,
2214                Self::Enum(Enum { ptr_enum: r_ptr, .. })
2215                | Self::RawEnum(RawEnum { ptr_enum: r_ptr, .. }) if l_ptr == r_ptr
2216            ),
2217            Self::Field(Field { ptr_field: l_ptr, .. }) => matches!(r,
2218                Self::Field(Field { ptr_field: r_ptr, .. })
2219                | Self::RawField(RawField { ptr_field: r_ptr, .. }) if l_ptr == r_ptr,
2220            ),
2221            Self::Fn(l) => match r {
2222                Self::Fn(r) => l.syn_id() == r.syn_id(),
2223                Self::RawFn(r) => l.syn_id() == r.syn_id(),
2224                _ => false,
2225            },
2226            Self::Local(Local { ptr_ident: l_ptr, .. }) => matches!(r,
2227                Self::Local(Local { ptr_ident: r_ptr, .. })
2228                | Self::RawLocal(RawLocal { ptr_ident: r_ptr, .. }) if l_ptr == r_ptr,
2229            ),
2230            Self::Mod(l) => match r {
2231                Self::Mod(r) => l.syn_id() == r.syn_id(),
2232                Self::RawMod(r) => l.syn_id() == r.syn_id(),
2233                _ => false,
2234            },
2235            Self::Struct(Struct { ptr_struct: l_ptr, .. }) => matches!(r,
2236                Self::Struct(Struct { ptr_struct: r_ptr, .. })
2237                | Self::RawStruct(RawStruct { ptr_struct: r_ptr, .. }) if l_ptr == r_ptr,
2238            ),
2239            Self::Trait(Trait { ptr_trait: l_ptr, .. }) => matches!(r,
2240                Self::Trait(Trait { ptr_trait: r_ptr, .. })
2241                | Self::RawTrait(RawTrait { ptr_trait: r_ptr, .. }) if l_ptr == r_ptr,
2242            ),
2243            Self::TypeAlias(TypeAlias { ptr_type: l_ptr, .. }) => matches!(r,
2244                Self::TypeAlias(TypeAlias { ptr_type: r_ptr, .. })
2245                | Self::RawTypeAlias(RawTypeAlias { ptr_type: r_ptr, .. }) if l_ptr == r_ptr,
2246            ),
2247            Self::Use(l) => match r {
2248                Self::Use(r) => l.syn_id() == r.syn_id() && l.dst == r.dst,
2249                Self::RawUse(r) => !r.is_glob() && l.syn_id() == r.syn_id(),
2250                _ => false,
2251            },
2252            Self::Variant(Variant { ptr_variant: l_ptr, .. }) => matches!(r,
2253                Self::Variant(Variant { ptr_variant: r_ptr, .. })
2254                | Self::RawVariant(RawVariant { ptr_variant: r_ptr, .. }) if l_ptr == r_ptr,
2255            ),
2256            Self::RawConst(l) => match r {
2257                Self::Const(r) => l.ptr_syn() == r.ptr_syn(),
2258                Self::RawConst(r) => l.ptr_syn() == r.ptr_syn(),
2259                _ => false,
2260            },
2261            Self::RawEnum(RawEnum { ptr_enum: l_ptr, .. }) => matches!(r,
2262                Self::Enum(Enum { ptr_enum: r_ptr, .. })
2263                | Self::RawEnum(RawEnum { ptr_enum: r_ptr, .. }) if l_ptr == r_ptr,
2264            ),
2265            Self::RawField(RawField { ptr_field: l_ptr, .. }) => matches!(r,
2266                Self::Field(Field { ptr_field: r_ptr, .. })
2267                | Self::RawField(RawField { ptr_field: r_ptr, .. }) if l_ptr == r_ptr,
2268            ),
2269            Self::RawFn(l) => match r {
2270                Self::Fn(r) => l.syn_id() == r.syn_id(),
2271                Self::RawFn(r) => l.syn_id() == r.syn_id(),
2272                _ => false,
2273            },
2274            Self::RawLocal(RawLocal { ptr_ident: l_ptr, .. }) => matches!(r,
2275                Self::Local(Local { ptr_ident: r_ptr, .. })
2276                | Self::RawLocal(RawLocal { ptr_ident: r_ptr, .. }) if l_ptr == r_ptr,
2277            ),
2278            Self::RawMod(l) => match r {
2279                Self::Mod(r) => l.syn_id() == r.syn_id(),
2280                Self::RawMod(r) => l.syn_id() == r.syn_id(),
2281                _ => false,
2282            },
2283            Self::RawStruct(RawStruct { ptr_struct: l_ptr, .. }) => matches!(r,
2284                Self::Struct(Struct { ptr_struct: r_ptr, .. })
2285                | Self::RawStruct(RawStruct { ptr_struct: r_ptr, .. }) if l_ptr == r_ptr,
2286            ),
2287            Self::RawTrait(RawTrait { ptr_trait: l_ptr, .. }) => matches!(r,
2288                Self::Trait(Trait { ptr_trait: r_ptr, .. })
2289                | Self::RawTrait(RawTrait { ptr_trait: r_ptr, .. }) if l_ptr == r_ptr,
2290            ),
2291            Self::RawTypeAlias(RawTypeAlias { ptr_type: l_ptr, .. }) => matches!(r,
2292                Self::TypeAlias(TypeAlias { ptr_type: r_ptr, .. })
2293                | Self::RawTypeAlias(RawTypeAlias { ptr_type: r_ptr, .. }) if l_ptr == r_ptr,
2294            ),
2295            Self::RawUse(l) => match r {
2296                Self::Use(r) => !l.is_glob() && l.syn_id() == r.syn_id(),
2297                Self::RawUse(r) => l.syn_id() == r.syn_id(),
2298                _ => false,
2299            },
2300            Self::RawVariant(RawVariant { ptr_variant: l_ptr, .. }) => matches!(r,
2301                Self::Variant(Variant { ptr_variant: r_ptr, .. })
2302                | Self::RawVariant(RawVariant { ptr_variant: r_ptr, .. }) if l_ptr == r_ptr,
2303            ),
2304            Self::None => matches!(r, Self::None),
2305        }
2306    }
2307
2308    fn effective_kind(&self) -> EffectiveItemKind {
2309        match self {
2310            Self::Block(_) => EffectiveItemKind::Block,
2311            Self::Const(_) | Self::RawConst(_) => EffectiveItemKind::Const,
2312            Self::Enum(_) | Self::RawEnum(_) => EffectiveItemKind::Enum,
2313            Self::Field(_) | Self::RawField(_) => EffectiveItemKind::Field,
2314            Self::Fn(_) | Self::RawFn(_) => EffectiveItemKind::Fn,
2315            Self::Local(_) | Self::RawLocal(_) => EffectiveItemKind::Local,
2316            Self::Mod(_) | Self::RawMod(_) => EffectiveItemKind::Mod,
2317            Self::Struct(_) | Self::RawStruct(_) => EffectiveItemKind::Struct,
2318            Self::Trait(_) | Self::RawTrait(_) => EffectiveItemKind::Trait,
2319            Self::TypeAlias(_) | Self::RawTypeAlias(_) => EffectiveItemKind::TypeAlias,
2320            Self::Use(_) | Self::RawUse(_) => EffectiveItemKind::Use,
2321            Self::Variant(_) | Self::RawVariant(_) => EffectiveItemKind::Variant,
2322            Self::None => EffectiveItemKind::Extra,
2323        }
2324    }
2325
2326    fn vis_node(&self) -> TriOption<NodeIndex, ()> {
2327        match self {
2328            Self::Const(v) => TriOption::Some(v.vis_node()),
2329            Self::Enum(Enum { vis_node, .. })
2330            | Self::Field(Field { vis_node, .. })
2331            | Self::Fn(Fn { vis_node, .. })
2332            | Self::Mod(Mod { vis_node, .. })
2333            | Self::Struct(Struct { vis_node, .. })
2334            | Self::Trait(Trait { vis_node, .. })
2335            | Self::TypeAlias(TypeAlias { vis_node, .. })
2336            | Self::Use(Use { vis_node, .. })
2337            | Self::Variant(Variant { vis_node, .. }) => TriOption::Some(*vis_node),
2338
2339            Self::RawConst(v) => v
2340                .vis_node()
2341                .map(TriOption::Some)
2342                .unwrap_or(TriOption::NotYet(())),
2343            Self::RawEnum(RawEnum { vis_node, .. })
2344            | Self::RawField(RawField { vis_node, .. })
2345            | Self::RawFn(RawFn { vis_node, .. })
2346            | Self::RawMod(RawMod { vis_node, .. })
2347            | Self::RawStruct(RawStruct { vis_node, .. })
2348            | Self::RawTrait(RawTrait { vis_node, .. })
2349            | Self::RawTypeAlias(RawTypeAlias { vis_node, .. })
2350            | Self::RawUse(RawUse { vis_node, .. })
2351            | Self::RawVariant(RawVariant { vis_node, .. }) => (*vis_node)
2352                .map(TriOption::Some)
2353                .unwrap_or(TriOption::NotYet(())),
2354
2355            Self::Block(_) | Self::Local(_) | Self::RawLocal(_) | Self::None => TriOption::None,
2356        }
2357    }
2358
2359    fn type_id(&self) -> TriOption<TypeId, ()> {
2360        match self {
2361            Self::Const(v) => TriOption::Some(v.type_id()),
2362            Self::Enum(Enum { tid, .. })
2363            | Self::Field(Field { tid, .. })
2364            | Self::Fn(Fn { tid, .. })
2365            | Self::Local(Local { tid, .. })
2366            | Self::Struct(Struct { tid, .. })
2367            | Self::TypeAlias(TypeAlias { tid, .. })
2368            | Self::Variant(Variant { tid, .. }) => TriOption::Some(*tid),
2369
2370            // Some raw items will have their types when they are resolved.
2371            Self::RawConst(v) => v
2372                .type_id()
2373                .map(TriOption::Some)
2374                .unwrap_or(TriOption::NotYet(())),
2375            Self::RawEnum(_)
2376            | Self::RawField(_)
2377            | Self::RawFn(_)
2378            | Self::RawLocal(_)
2379            | Self::RawStruct(_)
2380            | Self::RawTypeAlias(_)
2381            | Self::RawVariant(_) => TriOption::NotYet(()),
2382
2383            Self::Block(_)
2384            | Self::Mod(_)
2385            | Self::Trait(_)
2386            | Self::Use(_)
2387            | Self::RawMod(_)
2388            | Self::RawTrait(_)
2389            | Self::RawUse(_)
2390            | Self::None => TriOption::None,
2391        }
2392    }
2393
2394    fn syn_id(&self) -> Option<SynId> {
2395        let sid = match self {
2396            Self::Block(v) => v.syn_id(),
2397            Self::Const(v) => v.syn_id(),
2398            Self::Enum(v) => v.syn_id(),
2399            Self::Field(v) => v.syn_id(),
2400            Self::Fn(v) => v.syn_id(),
2401            Self::Local(v) => v.syn_id(),
2402            Self::Mod(v) => v.syn_id(),
2403            Self::Struct(v) => v.syn_id(),
2404            Self::Trait(v) => v.syn_id(),
2405            Self::TypeAlias(v) => v.syn_id(),
2406            Self::Use(v) => v.syn_id(),
2407            Self::Variant(v) => v.syn_id(),
2408            Self::RawConst(v) => v.syn_id(),
2409            Self::RawEnum(v) => v.syn_id(),
2410            Self::RawField(v) => v.syn_id(),
2411            Self::RawFn(v) => v.syn_id(),
2412            Self::RawLocal(v) => v.syn_id(),
2413            Self::RawMod(v) => v.syn_id(),
2414            Self::RawStruct(v) => v.syn_id(),
2415            Self::RawTrait(v) => v.syn_id(),
2416            Self::RawTypeAlias(v) => v.syn_id(),
2417            Self::RawUse(v) => v.syn_id(),
2418            Self::RawVariant(v) => v.syn_id(),
2419            Self::None => return None,
2420        };
2421        Some(sid)
2422    }
2423
2424    fn as_fn(&self) -> Option<&Fn> {
2425        if let Self::Fn(v) = self {
2426            Some(v)
2427        } else {
2428            None
2429        }
2430    }
2431
2432    fn as_struct(&self) -> Option<&Struct> {
2433        if let Self::Struct(v) = self {
2434            Some(v)
2435        } else {
2436            None
2437        }
2438    }
2439
2440    fn as_type_alias(&self) -> Option<&TypeAlias> {
2441        if let Self::TypeAlias(v) = self {
2442            Some(v)
2443        } else {
2444            None
2445        }
2446    }
2447
2448    fn as_use(&self) -> Option<&Use> {
2449        if let Self::Use(v) = self {
2450            Some(v)
2451        } else {
2452            None
2453        }
2454    }
2455
2456    fn as_variant(&self) -> Option<&Variant> {
2457        if let Self::Variant(v) = self {
2458            Some(v)
2459        } else {
2460            None
2461        }
2462    }
2463
2464    fn as_mut_mod(&mut self) -> Option<&mut Mod> {
2465        if let Self::Mod(v) = self {
2466            Some(v)
2467        } else {
2468            None
2469        }
2470    }
2471}
2472
2473impl ItemTrait for PubItem<'_> {
2474    fn is_effective_same(&self, other: &Self) -> bool {
2475        mem::discriminant(self) == mem::discriminant(other)
2476    }
2477
2478    fn effective_kind(&self) -> EffectiveItemKind {
2479        match self {
2480            Self::Block(_) => EffectiveItemKind::Block,
2481            Self::Const(_) => EffectiveItemKind::Const,
2482            Self::Enum(_) => EffectiveItemKind::Enum,
2483            Self::Field(_) => EffectiveItemKind::Field,
2484            Self::Fn(_) => EffectiveItemKind::Fn,
2485            Self::Local(_) => EffectiveItemKind::Local,
2486            Self::Mod(_) => EffectiveItemKind::Mod,
2487            Self::Struct(_) => EffectiveItemKind::Struct,
2488            Self::Trait(_) => EffectiveItemKind::Trait,
2489            Self::TypeAlias(_) => EffectiveItemKind::TypeAlias,
2490            Self::Use(_) => EffectiveItemKind::Use,
2491            Self::Variant(_) => EffectiveItemKind::Variant,
2492        }
2493    }
2494
2495    fn vis_node(&self) -> TriOption<NodeIndex, ()> {
2496        match self {
2497            Self::Const(v) => TriOption::Some(v.vis_node()),
2498            Self::Enum(Enum { vis_node, .. })
2499            | Self::Field(Field { vis_node, .. })
2500            | Self::Fn(Fn { vis_node, .. })
2501            | Self::Mod(Mod { vis_node, .. })
2502            | Self::Struct(Struct { vis_node, .. })
2503            | Self::Trait(Trait { vis_node, .. })
2504            | Self::TypeAlias(TypeAlias { vis_node, .. })
2505            | Self::Use(Use { vis_node, .. })
2506            | Self::Variant(Variant { vis_node, .. }) => TriOption::Some(*vis_node),
2507
2508            Self::Block(_) | Self::Local(_) => TriOption::None,
2509        }
2510    }
2511
2512    fn type_id(&self) -> TriOption<TypeId, ()> {
2513        match self {
2514            Self::Const(v) => TriOption::Some(v.type_id()),
2515            Self::Enum(Enum { tid, .. })
2516            | Self::Field(Field { tid, .. })
2517            | Self::Fn(Fn { tid, .. })
2518            | Self::Struct(Struct { tid, .. })
2519            | Self::TypeAlias(TypeAlias { tid, .. })
2520            | Self::Local(Local { tid, .. })
2521            | Self::Variant(Variant { tid, .. }) => TriOption::Some(*tid),
2522
2523            Self::Block(_) | Self::Mod(_) | Self::Trait(_) | Self::Use(_) => TriOption::None,
2524        }
2525    }
2526
2527    fn syn_id(&self) -> Option<SynId> {
2528        let sid = match self {
2529            Self::Block(v) => v.syn_id(),
2530            Self::Const(v) => v.syn_id(),
2531            Self::Enum(v) => v.syn_id(),
2532            Self::Field(v) => v.syn_id(),
2533            Self::Fn(v) => v.syn_id(),
2534            Self::Local(v) => v.syn_id(),
2535            Self::Mod(v) => v.syn_id(),
2536            Self::Struct(v) => v.syn_id(),
2537            Self::Trait(v) => v.syn_id(),
2538            Self::TypeAlias(v) => v.syn_id(),
2539            Self::Use(v) => v.syn_id(),
2540            Self::Variant(v) => v.syn_id(),
2541        };
2542        Some(sid)
2543    }
2544
2545    fn as_fn(&self) -> Option<&Fn> {
2546        if let Self::Fn(v) = self {
2547            Some(v)
2548        } else {
2549            None
2550        }
2551    }
2552
2553    fn as_struct(&self) -> Option<&Struct> {
2554        if let Self::Struct(v) = self {
2555            Some(v)
2556        } else {
2557            None
2558        }
2559    }
2560
2561    fn as_type_alias(&self) -> Option<&TypeAlias> {
2562        if let Self::TypeAlias(v) = self {
2563            Some(v)
2564        } else {
2565            None
2566        }
2567    }
2568
2569    fn as_use(&self) -> Option<&Use> {
2570        if let Self::Use(v) = self {
2571            Some(v)
2572        } else {
2573            None
2574        }
2575    }
2576
2577    fn as_variant(&self) -> Option<&Variant> {
2578        if let Self::Variant(v) = self {
2579            Some(v)
2580        } else {
2581            None
2582        }
2583    }
2584
2585    fn as_mut_mod(&mut self) -> Option<&mut Mod> {
2586        None // Read-only
2587    }
2588}
2589
2590#[derive(Debug, PartialEq, Eq)]
2591pub enum EffectiveItemKind {
2592    Block,
2593    Const,
2594    Enum,
2595    Field,
2596    Fn,
2597    Local,
2598    Mod,
2599    Struct,
2600    Trait,
2601    TypeAlias,
2602    Use,
2603    Variant,
2604    Extra,
2605}