pedant_core/resolution/rust/snapshot/module.rs
1//! The module-instance view: where one unit's modules live and how they nest.
2//!
3//! An instance is unit-local. The same source may be instantiated under two
4//! units, or twice inside one unit through separate `#[path]` declarations, and
5//! each occurrence is its own instance.
6
7use std::fmt;
8use std::sync::Arc;
9
10/// Opaque identity of one module instance inside a single resolution unit.
11#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct RustModuleId {
13 index: u32,
14}
15
16impl RustModuleId {
17 /// Issue an identity for the module instance at `index`.
18 pub(super) fn new(index: u32) -> Self {
19 Self { index }
20 }
21
22 /// The unit-local index this identity selects.
23 pub(in crate::resolution::rust) fn index(&self) -> u32 {
24 self.index
25 }
26}
27
28impl fmt::Debug for RustModuleId {
29 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(formatter, "RustModuleId({})", self.index)
31 }
32}
33
34/// One module instance: its place in the unit's module tree and the source
35/// whose items it owns.
36#[derive(Debug)]
37pub struct RustModuleInstance {
38 pub(super) id: RustModuleId,
39 pub(super) parent: Option<RustModuleId>,
40 pub(super) name: Arc<str>,
41 pub(super) path: Arc<str>,
42 pub(super) inline: bool,
43 pub(super) depth: u32,
44 /// Which lexical scope of [`Self::path`] this instance holds.
45 pub(in crate::resolution::rust) scope: u32,
46 /// This instance's position in the declaring source's IR declaration
47 /// table; absent for a unit's crate root.
48 pub(in crate::resolution::rust) declaration: Option<u32>,
49}
50
51impl RustModuleInstance {
52 /// This instance's unit-local identity.
53 pub fn id(&self) -> RustModuleId {
54 self.id
55 }
56
57 /// The instance that declares this one; absent for the crate root.
58 pub fn parent(&self) -> Option<RustModuleId> {
59 self.parent
60 }
61
62 /// The declared module name; `crate` for the root instance.
63 pub fn name(&self) -> &str {
64 &self.name
65 }
66
67 /// The repository-relative source holding this instance's items.
68 pub fn path(&self) -> &str {
69 &self.path
70 }
71
72 /// Whether the parent source declares this module inline.
73 pub fn is_inline(&self) -> bool {
74 self.inline
75 }
76
77 /// Module nesting depth below the crate root, which is zero.
78 pub fn depth(&self) -> u32 {
79 self.depth
80 }
81}