/ compiler / ast / src / expressions / intrinsic.rs
intrinsic.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  use super::*;
18  use adl_span::Symbol;
19  
20  use itertools::Itertools as _;
21  
22  /// An intrinsic call, e.g.`_foo(args)`.
23  #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24  pub 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      /// Expressions for the arguments passed to the function's parameters.
30      pub arguments: Vec<Expression>,
31      /// Span of the entire call `function(arguments)`.
32      pub span: Span,
33      /// The ID of the node.
34      pub id: NodeID,
35  }
36  
37  impl fmt::Display for IntrinsicExpression {
38      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39          // Format type parameters if they exist.
40          let type_parameters = if !self.type_parameters.is_empty() {
41              format!("::[{}]", self.type_parameters.iter().map(|(t, _)| t.to_string()).format(", "))
42          } else {
43              String::new()
44          };
45          write!(f, "{}{type_parameters}({})", self.name, self.arguments.iter().format(", "))
46      }
47  }
48  
49  impl From<IntrinsicExpression> for Expression {
50      fn from(value: IntrinsicExpression) -> Self {
51          Expression::Intrinsic(Box::new(value))
52      }
53  }
54  
55  crate::simple_node_impl!(IntrinsicExpression);