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 /// An expression evaluating to a callable function,
26 /// either a member of a structure or a free function.
27 pub function: Expression, // todo: make this identifier?
28 /// Expressions for the arguments passed to the functions parameters.
29 pub arguments: Vec<Expression>,
30 /// The name of the parent program call, e.g.`bar` in `bar.aleo`.
31 pub program: Option<Symbol>,
32 /// Span of the entire call `function(arguments)`.
33 pub span: Span,
34 /// The ID of the node.
35 pub id: NodeID,
36}
37
38impl fmt::Display for CallExpression {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 write!(f, "{}({})", self.function, self.arguments.iter().format(", "))
41 }
42}
43
44impl From<CallExpression> for Expression {
45 fn from(value: CallExpression) -> Self {
46 Expression::Call(Box::new(value))
47 }
48}
49
50crate::simple_node_impl!(CallExpression);