gbf_core/decompiler/ast/
expr.rs

1#![deny(missing_docs)]
2
3use gbf_macros::AstNodeTransform;
4use serde::{Deserialize, Serialize};
5
6use super::{
7    AstKind, AstVisitable, array_access::ArrayAccessNode, array_kind::ArrayKind,
8    bin_op::BinaryOperationNode, func_call::FunctionCallNode, identifier::IdentifierNode,
9    literal::LiteralNode, member_access::MemberAccessNode, new::NewNode, new_array::NewArrayNode,
10    phi::PhiNode, ptr::P, range::RangeNode, unary_op::UnaryOperationNode, visitors::AstVisitor,
11};
12
13/// Represents an expression node in the AST.
14#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
15#[convert_to(AstKind::Expression)]
16pub enum ExprKind {
17    /// Represents a literal node in the AST.
18    Literal(P<LiteralNode>),
19    /// Represents a binary operation node in the AST.
20    BinOp(P<BinaryOperationNode>),
21    /// Represents a unary operation node in the AST.
22    UnaryOp(P<UnaryOperationNode>),
23    /// Represents a function call node in the AST.
24    FunctionCall(P<FunctionCallNode>),
25    /// Represents an array node in the AST.
26    Array(ArrayKind),
27    /// Represents a new node in the AST.
28    New(P<NewNode>),
29    /// Represents a new array node in the AST.
30    NewArray(P<NewArrayNode>),
31    /// Represents a member access node in the AST.
32    MemberAccess(P<MemberAccessNode>),
33    /// Represents an identifier node in the AST.
34    Identifier(P<IdentifierNode>),
35    /// Represents an array access node in the AST.
36    ArrayAccess(P<ArrayAccessNode>),
37    /// Represents a phi node in the AST.
38    Phi(P<PhiNode>),
39    /// Represents a range node in the AST.
40    Range(P<RangeNode>),
41}
42
43impl AstVisitable for ExprKind {
44    fn accept<V: AstVisitor>(&self, visitor: &mut V) -> V::Output {
45        visitor.visit_expr(self)
46    }
47}
48
49// == Other implementations for literal ==
50
51impl PartialEq for ExprKind {
52    fn eq(&self, other: &Self) -> bool {
53        match (self, other) {
54            (ExprKind::Literal(l1), ExprKind::Literal(l2)) => l1 == l2,
55            (ExprKind::BinOp(b1), ExprKind::BinOp(b2)) => b1 == b2,
56            (ExprKind::UnaryOp(u1), ExprKind::UnaryOp(u2)) => u1 == u2,
57            (ExprKind::FunctionCall(f1), ExprKind::FunctionCall(f2)) => f1 == f2,
58            (ExprKind::Array(a1), ExprKind::Array(a2)) => a1 == a2,
59            (ExprKind::New(n1), ExprKind::New(n2)) => n1 == n2,
60            (ExprKind::NewArray(n1), ExprKind::NewArray(n2)) => n1 == n2,
61            (ExprKind::MemberAccess(m1), ExprKind::MemberAccess(m2)) => m1 == m2,
62            (ExprKind::Identifier(i1), ExprKind::Identifier(i2)) => i1 == i2,
63            (ExprKind::ArrayAccess(a1), ExprKind::ArrayAccess(a2)) => a1 == a2,
64            (ExprKind::Phi(p1), ExprKind::Phi(p2)) => p1 == p2,
65            (ExprKind::Range(r1), ExprKind::Range(r2)) => r1 == r2,
66            _ => false,
67        }
68    }
69}