gbf_core/decompiler/ast/
function.rs1#![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#[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 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 pub fn params(&self) -> &ArrayKind {
43 &self.params
44 }
45
46 pub fn body(&self) -> &P<BlockNode> {
48 &self.body
49 }
50
51 pub fn name(&self) -> &Option<String> {
53 &self.name
54 }
55}
56
57impl 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}