luaur_analysis/records/
block.rs1use 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 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 reaching_definitions: DenseHashMap::new(Symbol::default()),
32 }
33 }
34
35 pub fn contains_definition(&self, sym: Symbol) -> bool {
37 self.reaching_definitions.contains(&sym)
38 }
39
40 pub fn get_instructions(&self) -> &Vec<InstrId> {
42 &self.instructions
43 }
44
45 pub fn get_predecessors(&self) -> &Vec<BlockId> {
47 &self.predecessors
48 }
49
50 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 pub fn get_successors(&self) -> &Vec<BlockId> {
60 &self.successors
61 }
62
63 pub fn set_reaching_definition(&mut self, sym: Symbol, def: *mut Definition) {
65 *self.reaching_definitions.get_or_insert(sym) = def;
66 }
67}