gbf_core/decompiler/ast/
array_access.rs

1#![deny(missing_docs)]
2
3use gbf_macros::AstNodeTransform;
4use serde::{Deserialize, Serialize};
5
6use super::{AstKind, AstVisitable, expr::ExprKind, ptr::P, ssa::SsaVersion, visitors::AstVisitor};
7
8/// Represents a function call
9#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
10#[convert_to(ExprKind::ArrayAccess, AstKind::Expression)]
11pub struct ArrayAccessNode {
12    /// The array to access.
13    pub arr: ExprKind,
14
15    /// The index to access.
16    pub index: ExprKind,
17
18    /// Represents the SSA version of a variable.
19    pub ssa_version: Option<SsaVersion>,
20}
21
22impl ArrayAccessNode {
23    /// Creates a new array access.
24    ///
25    /// # Arguments
26    /// - `arr`: The array to access.
27    /// - `index`: The index to access.
28    pub fn new(arr: ExprKind, index: ExprKind) -> Self {
29        Self {
30            arr,
31            index,
32            ssa_version: None,
33        }
34    }
35}
36
37impl AstVisitable for P<ArrayAccessNode> {
38    fn accept<V: AstVisitor>(&self, visitor: &mut V) -> V::Output {
39        visitor.visit_array_access(self)
40    }
41}
42
43// == Other implementations for unary operations ==
44impl PartialEq for ArrayAccessNode {
45    fn eq(&self, other: &Self) -> bool {
46        self.arr == other.arr && self.index == other.index
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::decompiler::ast::{new_array_access, new_id, new_num};
53
54    #[test]
55    fn test_array_access_node() {
56        let arr = new_id("arr");
57        let index = new_num(5);
58        let array_access = new_array_access(arr.clone(), index.clone());
59        let array_access_two = new_array_access(arr, index);
60        assert_eq!(array_access, array_access_two);
61    }
62
63    #[test]
64    fn test_array_access_node_emit() {
65        let arr = new_id("arr");
66        let index = new_num(5);
67        let array_access = new_array_access(arr, index);
68        assert_eq!(crate::decompiler::ast::emit(array_access), "arr[5]");
69    }
70}