Skip to main content

luaur_analysis/records/
block.rs

1//! @interface-stub
2use crate::enums::block_kind::BlockKind;
3use crate::records::symbol::Symbol;
4use crate::type_aliases::block_id::BlockId;
5use crate::type_aliases::definition::Definition;
6use crate::type_aliases::instr_id::InstrId;
7use alloc::string::String;
8use alloc::vec::Vec;
9use luaur_common::records::dense_hash_map::DenseHashMap;
10
11#[derive(Debug, Clone)]
12pub struct Block {
13    pub kind: BlockKind,
14    pub debug_name: String,
15    pub(crate) instructions: Vec<InstrId>,
16    pub(crate) predecessors: Vec<BlockId>,
17    pub(crate) successors: Vec<BlockId>,
18    pub(crate) reaching_definitions: DenseHashMap<Symbol, *mut Definition>,
19}
20
21impl Block {
22    /// `Block::Block(BlockKind kind, std::string debugName)`. Reference: `ControlFlowGraph.cpp:58-62`.
23    pub fn new(kind: BlockKind, debug_name: String) -> Self {
24        Self {
25            kind,
26            debug_name,
27            instructions: Vec::new(),
28            predecessors: Vec::new(),
29            successors: Vec::new(),
30            // `reachingDefinitions{Symbol{}}`.
31            reaching_definitions: DenseHashMap::new(Symbol::default()),
32        }
33    }
34
35    /// `bool Block::containsDefinition(Symbol sym) const`. Reference: `ControlFlowGraph.cpp:70-73`.
36    pub fn contains_definition(&self, sym: Symbol) -> bool {
37        self.reaching_definitions.contains(&sym)
38    }
39
40    /// `const std::vector<InstrId>& Block::getInstructions() const`.
41    pub fn get_instructions(&self) -> &Vec<InstrId> {
42        &self.instructions
43    }
44
45    /// `const std::vector<BlockId>& Block::getPredecessors() const`.
46    pub fn get_predecessors(&self) -> &Vec<BlockId> {
47        &self.predecessors
48    }
49
50    /// `Definition* Block::getReachingDefinition(Symbol sym) const`. Reference: `ControlFlowGraph.cpp:75-80`.
51    pub fn get_reaching_definition(&self, sym: Symbol) -> *mut Definition {
52        match self.reaching_definitions.find(&sym) {
53            Some(v) => *v,
54            None => core::ptr::null_mut(),
55        }
56    }
57
58    /// `const std::vector<BlockId>& Block::getSuccessors() const`.
59    pub fn get_successors(&self) -> &Vec<BlockId> {
60        &self.successors
61    }
62
63    /// `void Block::setReachingDefinition(Symbol sym, DefId def)`. Reference: `ControlFlowGraph.cpp:82-85`.
64    pub fn set_reaching_definition(&mut self, sym: Symbol, def: *mut Definition) {
65        *self.reaching_definitions.get_or_insert(sym) = def;
66    }
67}