llvm_wrap/
iter.rs

1//! Provides iterators for various items
2use super::*;
3
4/// An iterator over functions in a module
5#[derive(Clone, Debug)]
6pub struct Functions {
7    pub(crate) pointer: Value,
8}
9
10impl Iterator for Functions {
11    type Item = Value;
12
13    fn next(&mut self) -> Option<Value> {
14        if self.pointer.value.is_null() {
15            None
16        } else {
17            let next = self.pointer;
18            self.pointer = Value {
19                value: unsafe {
20                    LLVMGetNextFunction(self.pointer.value)
21                }
22            };
23            Some(next)
24        }
25    }
26}
27
28/// An iterator over global variables in a module
29#[derive(Clone, Debug)]
30pub struct Globals {
31    pub(crate) pointer: Value,
32}
33
34impl Iterator for Globals {
35    type Item = Value;
36
37    fn next(&mut self) -> Option<Value> {
38        if self.pointer.value.is_null() {
39            None
40        } else {
41            let next = self.pointer;
42            self.pointer = Value {
43                value: unsafe {
44                    LLVMGetNextGlobal(self.pointer.value)
45                }
46            };
47            Some(next)
48        }
49    }
50}
51
52/// An iterator over parameters in a function
53#[derive(Clone, Debug)]
54pub struct Params {
55    pub(crate) pointer: Value,
56}
57
58impl Iterator for Params {
59    type Item = Value;
60
61    fn next(&mut self) -> Option<Value> {
62        if self.pointer.value.is_null() {
63            None
64        } else {
65            let next = self.pointer;
66            self.pointer = Value {
67                value: unsafe {
68                    LLVMGetNextParam(self.pointer.value)
69                }
70            };
71            Some(next)
72        }
73    }
74}
75
76/// An iterator over basic blocks in a function
77#[derive(Clone, Debug)]
78pub struct Blocks {
79    pub(crate) pointer: BasicBlock,
80}
81
82impl Iterator for Blocks {
83    type Item = BasicBlock;
84
85    fn next(&mut self) -> Option<BasicBlock> {
86        if self.pointer.basic_block.is_null() {
87            None
88        } else {
89            let next = self.pointer;
90            self.pointer = BasicBlock {
91                basic_block: unsafe {
92                    LLVMGetNextBasicBlock(self.pointer.basic_block)
93                }
94            };
95            Some(next)
96        }
97    }
98}