Skip to main content

microcad_lang/lower/ir/function/
function_signature.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Function signature syntax element
5
6use crate::lower::ir;
7
8use microcad_lang_base::{Identifier, SrcRef};
9use microcad_lang_proc_macros::SrcReferrer;
10
11/// Parameters and return type of a function
12#[derive(Clone, Debug, SrcReferrer)]
13pub struct FunctionSignature {
14    /// Function's parameters
15    pub parameters: ir::ParameterList,
16    /// Function's return type
17    pub return_type: Option<ir::TypeAnnotation>,
18    /// Source code reference
19    pub src_ref: SrcRef,
20}
21
22impl FunctionSignature {
23    /// Get parameter by name
24    pub fn parameter_by_name(&self, name: &Identifier) -> Option<&ir::Parameter> {
25        use crate::lower::Identifiable;
26        self.parameters.iter().find(|arg| arg.id_ref() == name)
27    }
28}
29
30impl std::fmt::Display for FunctionSignature {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(
33            f,
34            "({}){}",
35            self.parameters,
36            if let Some(ret) = &self.return_type {
37                format!("-> {ret}")
38            } else {
39                String::default()
40            }
41        )
42    }
43}