Ygen/IR/
builder.rs

1use std::collections::VecDeque;
2
3use super::Block;
4
5/// IRBuilder: used for building the ir of a function
6pub struct IRBuilder<'a> {
7    pub(crate)  blocks: VecDeque<&'a mut Block>,
8    /// The current block as an index in the blocks list
9    pub(crate)  curr: usize,
10}
11
12impl<'a> IRBuilder<'a> {
13    /// Creates an new ir builder
14    pub fn new() -> Self {
15        Self {
16            blocks: VecDeque::new(),
17            curr: 0,
18        }
19    }
20
21    /// Positions the block at the end of the blocks list
22    pub fn positionAtEnd(&mut self, block: &'a mut Block) {
23        self.blocks.push_back(block);
24        self.curr = self.blocks.len() - 1; // Can cause an intenger underflow but shouldn't
25    }
26
27    /// Positions the block at the start of the blocks list
28    pub fn positionAtStart(&mut self, block: &'a mut Block) {
29        self.blocks.push_front(block);
30        self.curr = 0; // Can cause an intenger underflow but shouldn't
31    }
32
33    /// Returns the last block of the builder
34    pub fn getLastBlock(&mut self) -> Option<&Block> {
35        Some(self.blocks.back()?.to_owned().to_owned())
36    }
37}
38
39/// Creates an new IRBuilder
40pub fn IRBuilder<'a>() -> IRBuilder<'a> {
41    IRBuilder::new()
42}