Skip to main content

vyre_spec/
pg_node_kind.rs

1//! Canonical node kinds for the Performance Graph (PG) layout.
2
3/// Canonical node kinds for the Performance Graph (PG) layout.
4/// Shared between surgec and vyre as the definitive source of truth.
5#[repr(u32)]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
7#[non_exhaustive]
8pub enum PgNodeKind {
9    /// Variable declaration node.
10    VariableDecl = 1,
11    /// Variable usage node.
12    VariableUse = 2,
13    /// Assignment statement node.
14    Assignment = 3,
15    /// Binary operation node.
16    Binary = 4,
17    /// Comparison operation node.
18    Comparison = 5,
19    /// Function call node.
20    FunctionCall = 6,
21    /// Function definition node.
22    FunctionDef = 7,
23    /// If statement node.
24    IfStmt = 8,
25    /// For loop node.
26    ForStmt = 9,
27    /// While loop node.
28    WhileStmt = 10,
29    /// Return statement node.
30    ReturnStmt = 11,
31    /// Pointer dereference node.
32    Deref = 12,
33    /// Address-of node.
34    AddrOf = 13,
35    /// Type cast node.
36    Cast = 14,
37    /// Member access node.
38    MemberAccess = 15,
39    /// Array access node.
40    ArrayAccess = 16,
41    /// Struct declaration node.
42    StructDecl = 17,
43    /// Integer literal node.
44    LiteralInt = 18,
45    /// String literal node.
46    LiteralStr = 19,
47    /// Floating point literal node.
48    LiteralFloat = 20,
49}
50
51impl PgNodeKind {
52    /// Converts a u32 into a `PgNodeKind` if it is valid.
53    #[must_use]
54    pub const fn from_u32(value: u32) -> Option<Self> {
55        match value {
56            1 => Some(Self::VariableDecl),
57            2 => Some(Self::VariableUse),
58            3 => Some(Self::Assignment),
59            4 => Some(Self::Binary),
60            5 => Some(Self::Comparison),
61            6 => Some(Self::FunctionCall),
62            7 => Some(Self::FunctionDef),
63            8 => Some(Self::IfStmt),
64            9 => Some(Self::ForStmt),
65            10 => Some(Self::WhileStmt),
66            11 => Some(Self::ReturnStmt),
67            12 => Some(Self::Deref),
68            13 => Some(Self::AddrOf),
69            14 => Some(Self::Cast),
70            15 => Some(Self::MemberAccess),
71            16 => Some(Self::ArrayAccess),
72            17 => Some(Self::StructDecl),
73            18 => Some(Self::LiteralInt),
74            19 => Some(Self::LiteralStr),
75            20 => Some(Self::LiteralFloat),
76            _ => None,
77        }
78    }
79}