yz_basic_block/
jump.rs

1use core::iter;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
8pub enum Unconditional<T> {
9    Halt,
10    Jump(T),
11    Return,
12    Unknown,
13}
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
17pub struct Dummy<T>(pub core::marker::PhantomData<T>);
18
19pub trait ForeachTarget {
20    type JumpTarget;
21
22    fn foreach_target<F>(&self, f: F)
23    where
24        F: FnMut(&Self::JumpTarget);
25
26    fn foreach_target_mut<F>(&mut self, f: F)
27    where
28        F: FnMut(&mut Self::JumpTarget);
29}
30
31impl<T> ForeachTarget for Dummy<T> {
32    type JumpTarget = T;
33
34    #[inline]
35    fn foreach_target<F>(&self, _f: F)
36    where
37        F: FnMut(&Self::JumpTarget),
38    {
39    }
40
41    #[inline]
42    fn foreach_target_mut<F>(&mut self, _f: F)
43    where
44        F: FnMut(&mut Self::JumpTarget),
45    {
46    }
47}
48
49impl<T> ForeachTarget for Unconditional<T> {
50    type JumpTarget = T;
51
52    #[inline]
53    fn foreach_target<F>(&self, mut f: F)
54    where
55        F: FnMut(&Self::JumpTarget),
56    {
57        if let Unconditional::Jump(t) = self {
58            f(t);
59        }
60    }
61
62    #[inline]
63    fn foreach_target_mut<F>(&mut self, mut f: F)
64    where
65        F: FnMut(&mut Self::JumpTarget),
66    {
67        if let Unconditional::Jump(t) = self {
68            f(t);
69        }
70    }
71}
72
73impl<C, T> ForeachTarget for C
74where
75    for<'a> &'a C: iter::IntoIterator<Item = &'a T>,
76    for<'a> &'a mut C: iter::IntoIterator<Item = &'a mut T>,
77    T: ForeachTarget,
78{
79    type JumpTarget = T::JumpTarget;
80
81    #[inline]
82    fn foreach_target<F>(&self, mut f: F)
83    where
84        F: FnMut(&Self::JumpTarget),
85    {
86        for i in self {
87            i.foreach_target(&mut f);
88        }
89    }
90
91    #[inline]
92    fn foreach_target_mut<F>(&mut self, mut f: F)
93    where
94        F: FnMut(&mut Self::JumpTarget),
95    {
96        for i in self {
97            i.foreach_target_mut(&mut f);
98        }
99    }
100}