1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::internal::commas;
use crate::Assign;
use std::fmt;

/// The definition of an input to a block.
///
/// These are essentially phi nodes, and makes sure that there's a local
/// variable declaration available.
#[derive(Debug, Clone)]
pub struct Phi {
    /// The blocks which defines the variable.
    dependencies: Vec<Assign>,
}

impl Phi {
    /// Construct a new phi node.
    pub(crate) fn new() -> Self {
        Self {
            dependencies: Vec::new(),
        }
    }

    /// Extend with the given iterator.
    pub(crate) fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = Assign>,
    {
        self.dependencies.extend(iter);
    }
}

impl fmt::Display for Phi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.dependencies.is_empty() {
            write!(f, "φ(?)")?;
        } else {
            write!(f, "φ({})", commas(&self.dependencies))?;
        }

        Ok(())
    }
}