leo_ast/expressions/
call.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::*;
18use leo_span::Symbol;
19
20use itertools::Itertools as _;
21
22/// A function call expression, e.g.`foo(args)` or `Foo::bar(args)`.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct CallExpression {
25    /// A path to a callable function, either a member of a structure or a free function.
26    pub function: Path,
27    /// Expressions for the const arguments passed to the function's const parameters.
28    pub const_arguments: Vec<Expression>,
29    /// Expressions for the arguments passed to the function's parameters.
30    pub arguments: Vec<Expression>,
31    /// The name of the parent program call, e.g.`bar` in `bar.aleo`.
32    pub program: Option<Symbol>,
33    /// Span of the entire call `function(arguments)`.
34    pub span: Span,
35    /// The ID of the node.
36    pub id: NodeID,
37}
38
39impl fmt::Display for CallExpression {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        write!(f, "{}", self.function)?;
42        if !self.const_arguments.is_empty() {
43            write!(f, "::[{}]", self.const_arguments.iter().format(", "))?;
44        }
45        write!(f, "({})", self.arguments.iter().format(", "))
46    }
47}
48
49impl From<CallExpression> for Expression {
50    fn from(value: CallExpression) -> Self {
51        Expression::Call(Box::new(value))
52    }
53}
54
55crate::simple_node_impl!(CallExpression);