leo_ast/expressions/call.rs
1// Copyright (C) 2019-2026 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::*;
18
19use itertools::Itertools as _;
20
21/// A function call expression, e.g.`foo(args)` or `Foo::bar(args)`.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct CallExpression {
24 /// A path to a callable function, either a member of a composite or a free function.
25 pub function: Path,
26 /// Expressions for the const arguments passed to the function's const parameters.
27 pub const_arguments: Vec<Expression>,
28 /// Expressions for the arguments passed to the function's parameters.
29 pub arguments: Vec<Expression>,
30 /// Span of the entire call `function(arguments)`.
31 pub span: Span,
32 /// The ID of the node.
33 pub id: NodeID,
34}
35
36impl fmt::Display for CallExpression {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 write!(f, "{}", self.function)?;
39 if !self.const_arguments.is_empty() {
40 write!(f, "::[{}]", self.const_arguments.iter().format(", "))?;
41 }
42 write!(f, "({})", self.arguments.iter().format(", "))
43 }
44}
45
46impl From<CallExpression> for Expression {
47 fn from(value: CallExpression) -> Self {
48 Expression::Call(Box::new(value))
49 }
50}
51
52crate::simple_node_impl!(CallExpression);