Skip to main content

pedant_types/resolution/
reference.rs

1//! A reference site: where a unit names something.
2
3use std::sync::Arc;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Language;
8
9use super::id::{DefinitionId, ReferenceId, ResolutionUnitId};
10use super::span::SourceSpan;
11
12/// The closed vocabulary of references a report emits.
13#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[serde(rename_all = "snake_case")]
15pub enum ReferenceKind {
16    /// A module declaration naming another source.
17    Module,
18    /// An import, including a rename, a group member, a glob, or a re-export.
19    Import,
20    /// A call.
21    Call,
22    /// A type mentioned in a signature, a body, or a bound.
23    Type,
24    /// An implementation naming its type or its trait.
25    Implementation,
26}
27
28/// One reference site inside one resolution unit.
29///
30/// Two identical path texts at two locations are two references: the span, not
31/// the text, is the site's identity.
32#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
33#[serde(deny_unknown_fields)]
34pub struct SymbolReference {
35    id: ReferenceId,
36    unit: ResolutionUnitId,
37    language: Language,
38    kind: ReferenceKind,
39    text: Arc<str>,
40    span: SourceSpan,
41    enclosing_definition: Option<DefinitionId>,
42}
43
44impl SymbolReference {
45    pub(crate) fn new(
46        id: ReferenceId,
47        unit: ResolutionUnitId,
48        language: Language,
49        kind: ReferenceKind,
50        text: Arc<str>,
51        span: SourceSpan,
52        enclosing_definition: Option<DefinitionId>,
53    ) -> Self {
54        Self {
55            id,
56            unit,
57            language,
58            kind,
59            text,
60            span,
61            enclosing_definition,
62        }
63    }
64
65    /// This reference's identifier.
66    pub fn id(&self) -> ReferenceId {
67        self.id
68    }
69
70    /// The unit this reference occurs in.
71    pub fn unit(&self) -> ResolutionUnitId {
72        self.unit
73    }
74
75    /// The language of the unit this reference belongs to.
76    pub fn language(&self) -> Language {
77        self.language
78    }
79
80    /// What kind of reference this is.
81    pub fn kind(&self) -> ReferenceKind {
82        self.kind
83    }
84
85    /// The source text of the reference, alias and all.
86    pub fn text(&self) -> &str {
87        &self.text
88    }
89
90    /// Where the reference sits in the snapshotted source.
91    pub fn span(&self) -> &SourceSpan {
92        &self.span
93    }
94
95    /// The span, for a decoder interning the paths it just allocated.
96    pub(super) fn span_mut(&mut self) -> &mut SourceSpan {
97        &mut self.span
98    }
99
100    /// The definition this reference occurs inside, in the same unit.
101    pub fn enclosing_definition(&self) -> Option<DefinitionId> {
102        self.enclosing_definition
103    }
104}