gbf_core/decompiler/ast/
assignable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#![deny(missing_docs)]

use gbf_macros::AstNodeTransform;
use serde::{Deserialize, Serialize};

use super::{
    emit, expr::ExprKind, identifier::IdentifierNode, member_access::MemberAccessNode,
    ssa::SsaVersion, visitors::AstVisitor, AstKind, AstVisitable,
};

/// Represents an assignable expression node in the AST.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(ExprKind::Assignable, AstKind::Expression)]
pub enum AssignableKind {
    /// Represents a member access node in the AST.
    MemberAccess(MemberAccessNode),
    /// Represents an identifier node in the AST.
    Identifier(IdentifierNode),
}

impl AssignableKind {
    /// Sets the SSA version of the assignable expression.
    pub fn set_ssa_version(&mut self, ssa_version: SsaVersion) {
        match self {
            AssignableKind::MemberAccess(m) => m.ssa_version = Some(ssa_version),
            AssignableKind::Identifier(i) => i.ssa_version = Some(ssa_version),
        }
    }

    /// Returns the SSA string representation of the assignable expression.
    pub fn id_string(&self) -> String {
        match self {
            AssignableKind::MemberAccess(m) => emit(m.clone()),
            AssignableKind::Identifier(i) => emit(i.clone()),
        }
    }

    /// Returns the SSA version of the assignable expression.
    pub fn ssa_version(&self) -> Option<SsaVersion> {
        match self {
            AssignableKind::MemberAccess(m) => m.ssa_version,
            AssignableKind::Identifier(i) => i.ssa_version,
        }
    }

    /// Remove the SSA version from the assignable expression.
    pub fn remove_ssa_version(&mut self) {
        match self {
            AssignableKind::MemberAccess(m) => m.ssa_version = None,
            AssignableKind::Identifier(i) => i.ssa_version = None,
        }
    }
}

impl AstVisitable for AssignableKind {
    fn accept(&self, visitor: &mut dyn AstVisitor) {
        visitor.visit_assignable_expr(self);
    }
}

impl PartialEq for AssignableKind {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (AssignableKind::MemberAccess(m1), AssignableKind::MemberAccess(m2)) => m1 == m2,
            (AssignableKind::Identifier(i1), AssignableKind::Identifier(i2)) => i1 == i2,
            _ => false,
        }
    }
}