leo_ast/expressions/dynamic_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 dynamic call expression, e.g. `MyInterface@(target)::foobar(args)`.
22///
23/// This represents a dynamic call where:
24/// - `interface` is the interface name (e.g. `MyInterface`)
25/// - `target_program` is the expression containing the target program (a `field` or `identifier` value)
26/// - `network` is the expression containing the target program's network (an optional `identifier` value)
27/// - `function` is the function to call on the target (e.g. `foobar`)
28/// - `arguments` are the arguments passed to the function
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct DynamicCallExpression {
31 /// The interface path.
32 pub interface: Type,
33 /// The target expression.
34 pub target_program: Expression,
35 /// The optional network expression (defaults to 'aleo' if None).
36 pub network: Option<Expression>,
37 /// The function to call.
38 pub function: Identifier,
39 /// The arguments to the function.
40 pub arguments: Vec<Expression>,
41 /// The span of the entire expression.
42 pub span: Span,
43 /// The ID of the node.
44 pub id: NodeID,
45}
46
47impl fmt::Display for DynamicCallExpression {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 if let Some(network) = &self.network {
50 write!(
51 f,
52 "{}@({}, {})::{}({})",
53 self.interface,
54 self.target_program,
55 network,
56 self.function,
57 self.arguments.iter().format(", ")
58 )
59 } else {
60 write!(
61 f,
62 "{}@({})::{}({})",
63 self.interface,
64 self.target_program,
65 self.function,
66 self.arguments.iter().format(", ")
67 )
68 }
69 }
70}
71
72impl From<DynamicCallExpression> for Expression {
73 fn from(value: DynamicCallExpression) -> Self {
74 Expression::DynamicCall(Box::new(value))
75 }
76}
77
78crate::simple_node_impl!(DynamicCallExpression);