cairo_lang_lowering/objects/
blocks.rs1use std::ops::{Index, IndexMut};
2
3use cairo_lang_diagnostics::{DiagnosticAdded, Maybe};
4use cairo_lang_utils::require;
5
6use crate::analysis::StatementLocation;
7use crate::{Block, Statement};
8
9#[derive(Copy, Clone, PartialEq, Eq, Hash, salsa::Update)]
10pub struct BlockId(pub usize);
11impl BlockId {
12 pub fn root() -> Self {
13 Self(0)
14 }
15
16 pub fn is_root(&self) -> bool {
17 self.0 == 0
18 }
19
20 pub fn next_block_id(&self) -> BlockId {
21 BlockId(self.0 + 1)
22 }
23}
24impl core::fmt::Debug for BlockId {
25 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26 write!(f, "blk{}", self.0)
27 }
28}
29
30#[derive(Clone, Debug, Default, PartialEq, Eq)]
33pub struct BlocksBuilder<'db>(pub Vec<Block<'db>>);
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct Blocks<'db>(Vec<Block<'db>>);
36
37impl<'db> BlocksBuilder<'db> {
38 pub fn new() -> Self {
39 Self(vec![])
40 }
41 pub fn alloc(&mut self, block: Block<'db>) -> BlockId {
42 let id = BlockId(self.0.len());
43 self.0.push(block);
44 id
45 }
46 pub fn alloc_empty(&mut self) -> BlockId {
48 let id = BlockId(self.0.len());
49 self.0.push(Block::default());
50 id
51 }
52 pub fn set_block(&mut self, id: BlockId, block: Block<'db>) {
54 self.0[id.0] = block;
55 }
56
57 pub fn get_mut_block<'m>(&'m mut self, id: BlockId) -> &'m mut Block<'db> {
59 &mut self.0[id.0]
60 }
61
62 pub fn len(&self) -> usize {
63 self.0.len()
64 }
65
66 pub fn is_empty(&self) -> bool {
67 self.0.is_empty()
68 }
69
70 pub fn build(self) -> Option<Blocks<'db>> {
71 require(!self.is_empty())?;
72 Some(Blocks(self.0))
73 }
74}
75impl<'db> Blocks<'db> {
76 pub fn new_errored(_diag_added: DiagnosticAdded) -> Self {
77 Self(vec![])
78 }
79
80 pub fn get(&self) -> &[Block<'db>] {
81 &self.0
82 }
83
84 pub fn len(&self) -> usize {
85 self.0.len()
86 }
87
88 pub fn is_empty(&self) -> bool {
89 self.0.is_empty()
90 }
91
92 pub fn iter(
93 &self,
94 ) -> impl DoubleEndedIterator<Item = (BlockId, &Block<'db>)> + ExactSizeIterator {
95 self.0.iter().enumerate().map(|(i, b)| (BlockId(i), b))
96 }
97
98 pub fn root_block(&self) -> Maybe<&Block<'db>> {
102 if self.is_empty() { Err(DiagnosticAdded) } else { Ok(&self.0[0]) }
103 }
104
105 pub fn has_root(&self) -> Maybe<()> {
106 if self.is_empty() { Err(DiagnosticAdded) } else { Ok(()) }
107 }
108
109 pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Block<'db>> {
110 self.0.iter_mut()
111 }
112
113 pub fn push(&mut self, block: Block<'db>) -> BlockId {
114 let id = BlockId(self.0.len());
115 self.0.push(block);
116 id
117 }
118
119 pub fn reset_block(&mut self, block_id: BlockId, block: Block<'db>) {
120 self.0[block_id.0] = block;
121 }
122}
123impl<'db> Index<BlockId> for Blocks<'db> {
124 type Output = Block<'db>;
125
126 fn index(&self, index: BlockId) -> &Self::Output {
127 &self.0[index.0]
128 }
129}
130impl<'db> IndexMut<BlockId> for Blocks<'db> {
131 fn index_mut(&mut self, index: BlockId) -> &mut Self::Output {
132 &mut self.0[index.0]
133 }
134}
135impl<'db> Index<StatementLocation> for Blocks<'db> {
136 type Output = Statement<'db>;
137
138 fn index(&self, index: StatementLocation) -> &Self::Output {
139 &self[index.0].statements[index.1]
140 }
141}