binaryninja/medium_level_il/
block.rs1use crate::basic_block::{BasicBlock, BlockContext};
2use crate::rc::Ref;
3use std::ops::Range;
4
5use super::{MediumLevelILFunction, MediumLevelILInstruction, MediumLevelInstructionIndex};
6
7pub struct MediumLevelILBlock {
8 pub(crate) function: Ref<MediumLevelILFunction>,
9}
10
11impl BlockContext for MediumLevelILBlock {
12 type Instruction = MediumLevelILInstruction;
13 type InstructionIndex = MediumLevelInstructionIndex;
14 type Iter = MediumLevelILBlockIter;
15
16 fn start(&self, block: &BasicBlock<Self>) -> MediumLevelILInstruction {
17 self.function
18 .instruction_from_index(block.start_index())
19 .unwrap()
20 }
21
22 fn iter(&self, block: &BasicBlock<Self>) -> MediumLevelILBlockIter {
23 MediumLevelILBlockIter {
24 function: self.function.to_owned(),
25 range: block.start_index().0..block.end_index().0,
26 }
27 }
28}
29
30impl std::fmt::Debug for MediumLevelILBlock {
31 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
32 f.debug_struct("MediumLevelILBlock")
33 .field("function", &self.function)
34 .finish()
35 }
36}
37
38impl Clone for MediumLevelILBlock {
39 fn clone(&self) -> Self {
40 MediumLevelILBlock {
41 function: self.function.to_owned(),
42 }
43 }
44}
45
46pub struct MediumLevelILBlockIter {
47 function: Ref<MediumLevelILFunction>,
48 range: Range<usize>,
49}
50
51impl Iterator for MediumLevelILBlockIter {
52 type Item = MediumLevelILInstruction;
53
54 fn next(&mut self) -> Option<Self::Item> {
55 self.range
56 .next()
57 .map(MediumLevelInstructionIndex)
58 .and_then(|i| self.function.instruction_from_index(i))
59 }
60}