miden_assembly_syntax/ast/procedure/
mod.rs

1mod alias;
2mod id;
3mod name;
4#[allow(clippy::module_inception)]
5mod procedure;
6mod resolver;
7
8use alloc::string::String;
9
10use miden_debug_types::{SourceSpan, Span, Spanned};
11use midenc_hir_type::FunctionType;
12
13pub use self::{
14    alias::{AliasTarget, ProcedureAlias},
15    id::ProcedureIndex,
16    name::{ProcedureName, QualifiedProcedureName},
17    procedure::{Procedure, Visibility},
18    resolver::{LocalNameResolver, ResolvedProcedure},
19};
20use crate::ast::{AttributeSet, Invoke};
21
22// EXPORT
23// ================================================================================================
24
25/// Represents an exportable entity from a [super::Module].
26///
27/// Currently only procedures (either locally-defined or re-exported) are exportable, but in the
28/// future this may be expanded.
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum Export {
31    /// A locally-defined procedure.
32    Procedure(Procedure),
33    /// An alias for an externally-defined procedure, i.e. a re-exported import.
34    Alias(ProcedureAlias),
35}
36
37impl Export {
38    /// Adds documentation to this export.
39    pub fn with_docs(self, docs: Option<Span<String>>) -> Self {
40        match self {
41            Self::Procedure(proc) => Self::Procedure(proc.with_docs(docs)),
42            Self::Alias(alias) => Self::Alias(alias.with_docs(docs)),
43        }
44    }
45
46    /// Returns the name of the exported procedure.
47    pub fn name(&self) -> &ProcedureName {
48        match self {
49            Self::Procedure(proc) => proc.name(),
50            Self::Alias(alias) => alias.name(),
51        }
52    }
53
54    /// Returns the documentation for this procedure.
55    pub fn docs(&self) -> Option<&str> {
56        match self {
57            Self::Procedure(proc) => proc.docs().map(|spanned| spanned.into_inner()),
58            Self::Alias(alias) => alias.docs().map(|spanned| spanned.into_inner()),
59        }
60    }
61
62    /// Returns the attributes for this procedure.
63    pub fn attributes(&self) -> Option<&AttributeSet> {
64        match self {
65            Self::Procedure(proc) => Some(proc.attributes()),
66            Self::Alias(_) => None,
67        }
68    }
69
70    /// Returns the visibility of this procedure (e.g. public or private).
71    ///
72    /// See [Visibility] for more details on what visibilities are supported.
73    pub fn visibility(&self) -> Visibility {
74        match self {
75            Self::Procedure(proc) => proc.visibility(),
76            Self::Alias(_) => Visibility::Public,
77        }
78    }
79
80    /// Returns a reference to the type signature of this procedure, if known.
81    pub fn signature(&self) -> Option<&FunctionType> {
82        match self {
83            Self::Procedure(proc) => proc.signature(),
84            Self::Alias(_) => None,
85        }
86    }
87
88    /// Returns the number of automatically-allocated words of memory this function requires
89    /// for the storage of temporaries/local variables.
90    pub fn num_locals(&self) -> usize {
91        match self {
92            Self::Procedure(proc) => proc.num_locals() as usize,
93            Self::Alias(_) => 0,
94        }
95    }
96
97    /// Returns true if this procedure is the program entrypoint.
98    pub fn is_main(&self) -> bool {
99        self.name().is_main()
100    }
101
102    /// Unwraps this [Export] as a [Procedure], or panic.
103    #[track_caller]
104    pub fn unwrap_procedure(&self) -> &Procedure {
105        match self {
106            Self::Procedure(proc) => proc,
107            Self::Alias(_) => panic!("attempted to unwrap alias export as procedure definition"),
108        }
109    }
110
111    /// Get an iterator over the set of other procedures invoked from this procedure.
112    ///
113    /// NOTE: This only applies to [Procedure]s, other types currently return an empty
114    /// iterator whenever called.
115    pub fn invoked<'a, 'b: 'a>(&'b self) -> impl Iterator<Item = &'a Invoke> + 'a {
116        match self {
117            Self::Procedure(proc) if proc.invoked.is_empty() => procedure::InvokedIter::Empty,
118            Self::Procedure(proc) => procedure::InvokedIter::NonEmpty(proc.invoked.iter()),
119            Self::Alias(_) => procedure::InvokedIter::Empty,
120        }
121    }
122}
123
124impl crate::prettier::PrettyPrint for Export {
125    fn render(&self) -> crate::prettier::Document {
126        match self {
127            Self::Procedure(proc) => proc.render(),
128            Self::Alias(proc) => proc.render(),
129        }
130    }
131}
132
133impl Spanned for Export {
134    fn span(&self) -> SourceSpan {
135        match self {
136            Self::Procedure(spanned) => spanned.span(),
137            Self::Alias(spanned) => spanned.span(),
138        }
139    }
140}