Skip to main content

leo_ast/expressions/
intrinsic.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::*;
18use leo_span::Symbol;
19
20use itertools::Itertools as _;
21
22/// An intrinsic call, e.g.`_foo(args)`.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct IntrinsicExpression {
25    /// Which intrinsic is being called
26    pub name: Symbol,
27    /// Type parameters for generic intrinsics.
28    pub type_parameters: Vec<(Type, Span)>,
29    /// Explicit input types for `_dynamic_call` with optional visibility modes.
30    /// Derived from all type parameters except the last.
31    /// Empty for all other intrinsics or when input types are not annotated.
32    pub input_types: Vec<(Mode, Type, Span)>,
33    /// Explicit return type for `_dynamic_call` with optional visibility mode.
34    /// Derived from the last type parameter (unpacking tuples into elements).
35    /// Empty for all other intrinsics or void calls.
36    pub return_types: Vec<(Mode, Type, Span)>,
37    /// Expressions for the arguments passed to the function's parameters.
38    pub arguments: Vec<Expression>,
39    /// Span of the entire call `function(arguments)`.
40    pub span: Span,
41    /// The ID of the node.
42    pub id: NodeID,
43}
44
45impl fmt::Display for IntrinsicExpression {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        // Format type parameters if they exist.
48        let type_parameters = if !self.type_parameters.is_empty() {
49            format!("::[{}]", self.type_parameters.iter().map(|(t, _)| t.to_string()).format(", "))
50        } else {
51            String::new()
52        };
53        write!(f, "{}{type_parameters}({})", self.name, self.arguments.iter().format(", "))
54    }
55}
56
57impl From<IntrinsicExpression> for Expression {
58    fn from(value: IntrinsicExpression) -> Self {
59        Expression::Intrinsic(Box::new(value))
60    }
61}
62
63crate::simple_node_impl!(IntrinsicExpression);