Skip to main content

luaur_analysis/methods/
cfg_builder_cfg_builder.rs

1//! Source: `Analysis/src/ControlFlowGraph.cpp:127-133` (hand-ported)
2//! C++ `explicit CFGBuilder::CFGBuilder(NotNull<CFGAllocator> allocator)`.
3use crate::enums::block_kind::BlockKind;
4use crate::records::cfg_allocator::CfgAllocator;
5use crate::records::cfg_builder::CfgBuilder;
6use crate::records::control_flow_graph::ControlFlowGraph;
7use crate::records::symbol::Symbol;
8use alloc::string::ToString;
9use luaur_common::records::dense_hash_map::DenseHashMap;
10use luaur_common::records::dense_hash_set::DenseHashSet;
11
12impl CfgBuilder {
13    pub fn new(allocator: *mut CfgAllocator) -> Self {
14        // C++ member-init order:
15        //   cfg(std::make_unique<ControlFlowGraph>(allocator))
16        //   allocator(allocator)
17        //   currentBlock(cfg->newBlock(BlockKind::Entry, "Entry Block"))
18        let mut cfg = ControlFlowGraph::control_flow_graph(allocator);
19        let current_block = cfg.new_block(BlockKind::Entry, "Entry Block".to_string());
20
21        let mut builder = Self {
22            cfg: Some(cfg),
23            allocator,
24            current_block,
25            // C++ `sealedBlocks{nullptr}`
26            sealed_blocks: DenseHashSet::new(core::ptr::null_mut()),
27            // C++ `incompleteJoins{nullptr}`
28            incomplete_joins: DenseHashMap::new(core::ptr::null_mut()),
29            // C++ `versionCounter{Symbol{}}`
30            version_counter: DenseHashMap::new(Symbol::default()),
31        };
32
33        // C++ constructor body: seal(currentBlock);
34        builder.seal(current_block);
35        builder
36    }
37}