microcad_lang/resolve/symbol/
symbol_info.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{builtin::*, rc::*, src_ref::*, syntax::*, value::*};
5
6/// Retrieve symbol information.
7pub trait Info {
8    /// Get symbol information.
9    fn info(&self) -> SymbolInfo;
10}
11
12/// Single parameter information ion human readable form.
13pub struct ParameterInfo {
14    /// Parameter ID.
15    pub id: String,
16    /// Type if given.
17    pub ty: Option<String>,
18    /// Default value if given.
19    pub def: Option<String>,
20}
21
22impl From<&Parameter> for ParameterInfo {
23    fn from(param: &Parameter) -> Self {
24        Self {
25            id: param.id.to_string(),
26            ty: param.specified_type.clone().map(|ty| ty.to_string()),
27            def: param.default_value.clone().map(|def| def.to_string()),
28        }
29    }
30}
31
32impl From<(&Identifier, &ParameterValue)> for ParameterInfo {
33    fn from(param: (&Identifier, &ParameterValue)) -> Self {
34        Self {
35            id: param.0.to_string(),
36            ty: param.1.specified_type.clone().map(|ty| ty.to_string()),
37            def: param.1.default_value.clone().map(|def| def.to_string()),
38        }
39    }
40}
41
42/// Call signature information in human readable form.
43pub struct SignatureInfo {
44    /// Parameters of the call.
45    pub params: Vec<ParameterInfo>,
46    /// Documentation.
47    pub doc: Option<DocBlock>,
48}
49
50/// Symbol information in human readable form.
51#[derive(Default)]
52pub struct SymbolInfo {
53    /// ID of the symbol.
54    pub id: String,
55    /// Human readable symbol kind.
56    pub kind: String,
57    /// Optional documentation block.
58    pub doc: Option<DocBlock>,
59    /// Parameters and alternative parameters if any.
60    pub signatures: Vec<SignatureInfo>,
61    /// Source code reference.
62    pub src_ref: SrcRef,
63}
64
65impl SymbolInfo {
66    pub(super) fn new_constant(_: &Visibility, id: &Identifier, _: &Value) -> Self {
67        SymbolInfo {
68            id: id.to_string(),
69            kind: "Constant".into(),
70            ..Default::default()
71        }
72    }
73
74    pub(super) fn new_arg(_d: &Identifier, _: &Value) -> Self {
75        SymbolInfo {
76            kind: "Argument".into(),
77            ..Default::default()
78        }
79    }
80}
81
82impl From<&Rc<Assignment>> for SymbolInfo {
83    fn from(def: &Rc<Assignment>) -> Self {
84        SymbolInfo {
85            id: def.id.to_string(),
86            kind: "Assignment".into(),
87            doc: def.doc(),
88            signatures: vec![],
89            src_ref: def.src_ref(),
90        }
91    }
92}
93
94impl From<&Rc<SourceFile>> for SymbolInfo {
95    fn from(def: &Rc<SourceFile>) -> Self {
96        SymbolInfo {
97            id: def.id().to_string(),
98            kind: "SourceFile".into(),
99            doc: def.doc(),
100            signatures: vec![],
101            src_ref: def.src_ref(),
102        }
103    }
104}
105
106impl From<&Rc<ModuleDefinition>> for SymbolInfo {
107    fn from(def: &Rc<ModuleDefinition>) -> Self {
108        SymbolInfo {
109            id: def.id.to_string(),
110            kind: "ModuleDefinition".into(),
111            doc: def.doc(),
112            signatures: vec![],
113            src_ref: def.src_ref(),
114        }
115    }
116}
117
118impl From<&Rc<WorkbenchDefinition>> for SymbolInfo {
119    fn from(def: &Rc<WorkbenchDefinition>) -> Self {
120        SymbolInfo {
121            id: def.id.to_string(),
122            kind: def.kind.to_string(),
123            doc: def.doc(),
124            signatures: def
125                .inits()
126                .map(|init| SignatureInfo {
127                    params: init.parameters.iter().map(|p| p.into()).collect(),
128                    doc: init.doc(),
129                })
130                .collect(),
131            src_ref: def.src_ref(),
132        }
133    }
134}
135
136impl From<&Rc<FunctionDefinition>> for SymbolInfo {
137    fn from(def: &Rc<FunctionDefinition>) -> Self {
138        SymbolInfo {
139            id: def.id.to_string(),
140            kind: "Function".into(),
141            doc: def.doc(),
142            signatures: vec![SignatureInfo {
143                params: def.signature.parameters.iter().map(|p| p.into()).collect(),
144                doc: def.doc(),
145            }],
146            src_ref: def.src_ref(),
147        }
148    }
149}
150
151impl From<&Rc<Builtin>> for SymbolInfo {
152    fn from(def: &Rc<Builtin>) -> Self {
153        SymbolInfo {
154            id: def.id.to_string(),
155            kind: "Builtin".into(),
156            doc: def.doc.clone(),
157            signatures: vec![SignatureInfo {
158                params: def.parameters.iter().map(|p| p.into()).collect(),
159                doc: def.doc.clone(),
160            }],
161            src_ref: SrcRef(None),
162        }
163    }
164}