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: Identifier,
28 /// Expressions for the const arguments passed to the function's const parameters.
29 pub const_arguments: Vec<Expression>,
30 /// Expressions for the arguments passed to the function's parameters.
31 pub arguments: Vec<Expression>,
32 /// The name of the parent program call, e.g.`bar` in `bar.aleo`.
33 pub program: Option<Symbol>,
34 /// Span of the entire call `function(arguments)`.
35 pub span: Span,
36 /// The ID of the node.
37 pub id: NodeID,
38}
39
40impl fmt::Display for CallExpression {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 write!(f, "{}", self.function)?;
43 if !self.const_arguments.is_empty() {
44 write!(f, "::[{}]", self.const_arguments.iter().format(", "))?;
45 }
46 write!(f, "({})", self.arguments.iter().format(", "))
47 }
48}
49
50impl From<CallExpression> for Expression {
51 fn from(value: CallExpression) -> Self {
52 Expression::Call(Box::new(value))
53 }
54}
55
56crate::simple_node_impl!(CallExpression);