Skip to main content

pedant_types/resolution/
definition.rs

1//! A definition site: what a name in one unit declares.
2
3use std::sync::Arc;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Language;
8
9use super::id::{DefinitionId, ResolutionUnitId};
10use super::span::SourceSpan;
11
12/// The closed vocabulary of definitions a report emits.
13#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[serde(rename_all = "snake_case")]
15pub enum SymbolKind {
16    /// A module.
17    Module,
18    /// A free or associated function.
19    Function,
20    /// A method reached through a receiver.
21    Method,
22    /// A struct.
23    Struct,
24    /// An enum.
25    Enum,
26    /// A union.
27    Union,
28    /// A trait.
29    Trait,
30    /// A type alias.
31    TypeAlias,
32    /// A constant.
33    Constant,
34    /// A static.
35    Static,
36}
37
38/// One definition site inside one resolution unit.
39#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
40#[serde(deny_unknown_fields)]
41pub struct SymbolDefinition {
42    id: DefinitionId,
43    unit: ResolutionUnitId,
44    language: Language,
45    kind: SymbolKind,
46    name: Arc<str>,
47    span: SourceSpan,
48    parent: Option<DefinitionId>,
49}
50
51impl SymbolDefinition {
52    pub(crate) fn new(
53        id: DefinitionId,
54        unit: ResolutionUnitId,
55        language: Language,
56        kind: SymbolKind,
57        name: Arc<str>,
58        span: SourceSpan,
59        parent: Option<DefinitionId>,
60    ) -> Self {
61        Self {
62            id,
63            unit,
64            language,
65            kind,
66            name,
67            span,
68            parent,
69        }
70    }
71
72    /// This definition's identifier.
73    pub fn id(&self) -> DefinitionId {
74        self.id
75    }
76
77    /// The unit this definition is declared in.
78    pub fn unit(&self) -> ResolutionUnitId {
79        self.unit
80    }
81
82    /// The language of the unit this definition belongs to.
83    pub fn language(&self) -> Language {
84        self.language
85    }
86
87    /// What kind of definition this is.
88    pub fn kind(&self) -> SymbolKind {
89        self.kind
90    }
91
92    /// The declared name.
93    pub fn name(&self) -> &str {
94        &self.name
95    }
96
97    /// Where the definition sits in the snapshotted source.
98    pub fn span(&self) -> &SourceSpan {
99        &self.span
100    }
101
102    /// The span, for a decoder interning the paths it just allocated.
103    pub(super) fn span_mut(&mut self) -> &mut SourceSpan {
104        &mut self.span
105    }
106
107    /// The definition that lexically owns this one, in the same unit.
108    pub fn parent(&self) -> Option<DefinitionId> {
109        self.parent
110    }
111}