calyx_utils/
id.rs

1pub type GSym = symbol_table::GlobalSymbol;
2
3/// Represents an identifier in a Calyx program
4#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, PartialOrd, Ord)]
5#[cfg_attr(
6    feature = "serialize",
7    derive(serde::Deserialize),
8    serde(transparent)
9)]
10pub struct Id {
11    pub id: GSym,
12}
13
14impl Id {
15    pub fn new<S: ToString>(id: S) -> Self {
16        Self {
17            id: GSym::from(id.to_string()),
18        }
19    }
20}
21
22/* =================== Impls for Id to make them easier to use ============== */
23
24impl Default for Id {
25    fn default() -> Self {
26        Id::new("")
27    }
28}
29
30impl std::fmt::Display for Id {
31    fn fmt(
32        &self,
33        f: &mut std::fmt::Formatter<'_>,
34    ) -> Result<(), std::fmt::Error> {
35        write!(f, "{}", self.id)
36    }
37}
38
39impl AsRef<str> for Id {
40    fn as_ref(&self) -> &str {
41        self.id.as_str()
42    }
43}
44
45impl From<&str> for Id {
46    fn from(s: &str) -> Self {
47        Id::new(s)
48    }
49}
50
51impl From<String> for Id {
52    fn from(s: String) -> Self {
53        Id::new(s)
54    }
55}
56
57impl PartialEq<GSym> for Id {
58    fn eq(&self, other: &GSym) -> bool {
59        self.id == *other
60    }
61}
62impl PartialEq<str> for Id {
63    fn eq(&self, other: &str) -> bool {
64        self.id == GSym::from(other)
65    }
66}
67impl PartialEq<&str> for Id {
68    fn eq(&self, other: &&str) -> bool {
69        self.id == GSym::from(*other)
70    }
71}
72impl PartialEq<&Id> for Id {
73    fn eq(&self, other: &&Id) -> bool {
74        self.id == other.id
75    }
76}
77impl PartialEq<String> for Id {
78    fn eq(&self, other: &String) -> bool {
79        self.id == GSym::from(other)
80    }
81}
82
83impl From<Id> for GSym {
84    fn from(id: Id) -> Self {
85        id.id
86    }
87}
88
89impl From<&Id> for GSym {
90    fn from(id: &Id) -> Self {
91        id.id
92    }
93}
94
95#[cfg(feature = "serialize")]
96impl serde::Serialize for Id {
97    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
98    where
99        S: serde::Serializer,
100    {
101        self.id.serialize(serializer)
102    }
103}
104
105/// A trait representing something in the IR that has a name.
106pub trait GetName {
107    /// Return a reference to the object's name
108    fn name(&self) -> Id;
109}