gbf_core/decompiler/ast/
function.rs

1#![deny(missing_docs)]
2
3use gbf_macros::AstNodeTransform;
4use serde::{Deserialize, Serialize};
5
6use super::{
7    AstKind, AstVisitable, array_kind::ArrayKind, block::BlockNode, ptr::P, visitors::AstVisitor,
8};
9
10/// Represents a metadata node in the AST
11#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
12#[convert_to(AstKind::Function)]
13pub struct FunctionNode {
14    name: Option<String>,
15    params: ArrayKind,
16    body: P<BlockNode>,
17}
18
19impl FunctionNode {
20    /// Creates a new `FunctionNode` with the given `params` and `body`.
21    ///
22    /// # Arguments
23    /// - `name` - The name of the function.
24    /// - `params` - The parameters of the function.
25    /// - `body` - The body of the function.
26    ///
27    /// # Returns
28    /// A new `FunctionNode`.
29    pub fn new<N, V>(name: N, params: ArrayKind, body: Vec<V>) -> Self
30    where
31        N: Into<Option<String>>,
32        V: Into<AstKind>,
33    {
34        Self {
35            name: name.into(),
36            params,
37            body: BlockNode::new(body).into(),
38        }
39    }
40
41    /// Returns the parameters of the function.
42    pub fn params(&self) -> &ArrayKind {
43        &self.params
44    }
45
46    /// Returns the body of the function.
47    pub fn body(&self) -> &P<BlockNode> {
48        &self.body
49    }
50
51    /// Returns the name of the function.
52    pub fn name(&self) -> &Option<String> {
53        &self.name
54    }
55}
56
57// == Other implementations for literal ==
58impl AstVisitable for P<FunctionNode> {
59    fn accept<V: AstVisitor>(&self, visitor: &mut V) -> V::Output {
60        visitor.visit_function(self)
61    }
62}
63
64impl PartialEq for FunctionNode {
65    fn eq(&self, other: &Self) -> bool {
66        self.params == other.params && self.body == other.body
67    }
68}