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