microcad_lang/resolve/
symbol_definition.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{builtin::*, rc::*, syntax::*, value::*};
5
6/// Symbol definition
7#[derive(Debug, Clone)]
8pub enum SymbolDefinition {
9    /// Source file symbol.
10    SourceFile(Rc<SourceFile>),
11    /// Module symbol.
12    Module(Rc<ModuleDefinition>),
13    /// Part symbol.
14    Workbench(Rc<WorkbenchDefinition>),
15    /// Function symbol.
16    Function(Rc<FunctionDefinition>),
17    /// Builtin symbol.
18    Builtin(Rc<Builtin>),
19    /// Constant.
20    Constant(Visibility, Identifier, Value),
21    /// Argument value.
22    Argument(Identifier, Value),
23    /// Alias of a pub use statement.
24    Alias(Visibility, Identifier, QualifiedName),
25    /// Use all available symbols in the module with the given name.
26    UseAll(Visibility, QualifiedName),
27    /// Just a dummy for testing
28    #[cfg(test)]
29    Tester(Identifier),
30}
31
32impl SymbolDefinition {
33    /// Returns ID of this definition.
34    pub fn id(&self) -> Identifier {
35        match &self {
36            Self::Workbench(w) => w.id.clone(),
37            Self::Module(m) => m.id.clone(),
38            Self::Function(f) => f.id.clone(),
39            Self::SourceFile(s) => s.id(),
40            Self::Builtin(m) => m.id(),
41            Self::Constant(_, id, _) | Self::Argument(id, _) | Self::Alias(_, id, _) => id.clone(),
42            Self::UseAll(..) => Identifier::none(),
43            #[cfg(test)]
44            Self::Tester(id) => id.clone(),
45        }
46    }
47
48    /// Return visibility of this symbol.
49    pub fn visibility(&self) -> Visibility {
50        match &self {
51            SymbolDefinition::SourceFile(..) => Visibility::Public,
52            SymbolDefinition::Builtin(..) => Visibility::Public,
53            SymbolDefinition::Argument(..) => Visibility::Private,
54
55            SymbolDefinition::Module(md) => md.visibility,
56            SymbolDefinition::Workbench(wd) => wd.visibility,
57            SymbolDefinition::Function(fd) => fd.visibility,
58
59            SymbolDefinition::Constant(visibility, ..)
60            | SymbolDefinition::Alias(visibility, ..)
61            | SymbolDefinition::UseAll(visibility, ..) => *visibility,
62
63            #[cfg(test)]
64            SymbolDefinition::Tester(..) => Visibility::Public,
65        }
66    }
67}
68
69impl std::fmt::Display for SymbolDefinition {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        match self {
72            Self::Workbench(w) => write!(f, "({})", w.kind),
73            Self::Module(..) => write!(f, "(module)"),
74            Self::Function(..) => write!(f, "(function)"),
75            Self::SourceFile(..) => write!(f, "(file)"),
76            Self::Builtin(..) => write!(f, "(builtin)"),
77            Self::Constant(.., value) => write!(f, "(constant) = {value}"),
78            Self::Argument(.., value) => write!(f, "(call_argument) = {value}"),
79            Self::Alias(.., name) => write!(f, "(alias) => {name}"),
80            Self::UseAll(.., name) => write!(f, "(use all) => {name}"),
81            #[cfg(test)]
82            Self::Tester(id) => write!(f, "(tester) => {id}"),
83        }
84    }
85}