Skip to content

Commit dfcd749

Browse files
authored
Add attribute displaying to IR printer (#493)
* attribute display trait * add display to IR printer * cleanup * manual display trait implementation
1 parent 8014f2f commit dfcd749

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

crates/filament/src/ast_passes/toplevel.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ impl Visitor for TopLevel {
7272
// If no toplevel component was found, find the component with the name "main"
7373
if self.has_toplevel.is_none() {
7474
for comp in ast.components.iter_mut() {
75-
if comp.sig.name.as_ref() == "main" {
75+
if comp.sig.name.as_ref() == "main"
76+
&& !comp.sig.attributes.has(utils::CompBool::TopLevel)
77+
{
7678
// Add the toplevel attribute to the component
7779
comp.sig.attributes.set(
7880
utils::CompBool::TopLevel,

crates/ir/src/printer/comp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ impl<'a, 'b> Printer<'a, 'b> {
133133

134134
fn port(&self, idx: ir::PortIdx, indent: usize) -> String {
135135
let port = self.comp.get(idx);
136+
let port_attrs = self.comp.port_attrs.get(idx);
136137
format!(
137-
"{:indent$}{}: {} {}",
138+
"{:indent$}{}{}: {} {}",
138139
"",
140+
port_attrs,
139141
self.comp.display(idx),
140142
self.comp.display(&port.live),
141143
self.comp.display(port.width),
@@ -149,6 +151,8 @@ impl<'a, 'b> Printer<'a, 'b> {
149151
indent: usize,
150152
f: &mut F,
151153
) -> io::Result<()> {
154+
writeln!(f, "{}", self.comp.attrs)?;
155+
152156
if self.comp.is_ext() {
153157
write!(f, "ext ")?;
154158
};

crates/utils/src/attr/attributes.rs

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use super::{AttrCtx, AttrStore};
22
use crate::GPosIdx;
3-
use std::{hash::Hash, str::FromStr};
3+
use std::{fmt::Display, hash::Hash};
44

55
/// Stores the attributes of a component
66
#[derive(Clone)]
77
pub struct Attributes<Bool, Num, Float>
88
where
9-
Bool: FromStr + Hash + Eq + Copy,
10-
Num: FromStr + Hash + Eq + Copy,
11-
Float: FromStr + Hash + Eq + Copy,
9+
Bool: Hash + Eq + Copy,
10+
Num: Hash + Eq + Copy,
11+
Float: Hash + Eq + Copy,
1212
{
1313
/// Numerical attributes
1414
num_attrs: AttrStore<Num, u64>,
@@ -20,9 +20,9 @@ where
2020

2121
impl<Bool, Num, Float> AttrCtx<Num, u64> for Attributes<Bool, Num, Float>
2222
where
23-
Bool: FromStr + Hash + Eq + Copy,
24-
Num: FromStr + Hash + Eq + Copy,
25-
Float: FromStr + Hash + Eq + Copy,
23+
Bool: Hash + Eq + Copy,
24+
Num: Hash + Eq + Copy,
25+
Float: Hash + Eq + Copy,
2626
{
2727
fn get(&self, attr: Num) -> Option<&u64> {
2828
self.num_attrs.get(attr)
@@ -43,9 +43,9 @@ where
4343

4444
impl<Bool, Num, Float> AttrCtx<Bool, bool> for Attributes<Bool, Num, Float>
4545
where
46-
Bool: FromStr + Hash + Eq + Copy,
47-
Num: FromStr + Hash + Eq + Copy,
48-
Float: FromStr + Hash + Eq + Copy,
46+
Bool: Hash + Eq + Copy,
47+
Num: Hash + Eq + Copy,
48+
Float: Hash + Eq + Copy,
4949
{
5050
fn get(&self, attr: Bool) -> Option<&bool> {
5151
self.bool_attrs.get(attr)
@@ -66,9 +66,9 @@ where
6666

6767
impl<Bool, Num, Float> AttrCtx<Float, f64> for Attributes<Bool, Num, Float>
6868
where
69-
Bool: FromStr + Hash + Eq + Copy,
70-
Num: FromStr + Hash + Eq + Copy,
71-
Float: FromStr + Hash + Eq + Copy,
69+
Bool: Hash + Eq + Copy,
70+
Num: Hash + Eq + Copy,
71+
Float: Hash + Eq + Copy,
7272
{
7373
fn get(&self, attr: Float) -> Option<&f64> {
7474
self.float_attrs.get(attr)
@@ -89,9 +89,9 @@ where
8989

9090
impl<Bool, Num, Float> Default for Attributes<Bool, Num, Float>
9191
where
92-
Bool: FromStr + Hash + Eq + Copy,
93-
Num: FromStr + Hash + Eq + Copy,
94-
Float: FromStr + Hash + Eq + Copy,
92+
Bool: Hash + Eq + Copy,
93+
Num: Hash + Eq + Copy,
94+
Float: Hash + Eq + Copy,
9595
{
9696
fn default() -> Self {
9797
Self {
@@ -101,3 +101,39 @@ where
101101
}
102102
}
103103
}
104+
105+
impl<Bool, Num, Float> Display for Attributes<Bool, Num, Float>
106+
where
107+
Bool: Display + Hash + Eq + Copy,
108+
Num: Display + Hash + Eq + Copy,
109+
Float: Display + Hash + Eq + Copy,
110+
{
111+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112+
write!(f, "#[")?;
113+
let bool_attrs =
114+
self.bool_attrs.iter().map(|(attr, value)| match value {
115+
true => format!("{}", attr),
116+
false => format!("not({})", attr),
117+
});
118+
let num_attrs = self
119+
.num_attrs
120+
.iter()
121+
.map(|(attr, value)| format!("{}={}", attr, value));
122+
let float_attrs = self
123+
.float_attrs
124+
.iter()
125+
.map(|(attr, value)| format!("{}={}", attr, value));
126+
127+
write!(
128+
f,
129+
"{}",
130+
bool_attrs
131+
.chain(num_attrs)
132+
.chain(float_attrs)
133+
.collect::<Vec<_>>()
134+
.join(", ")
135+
)?;
136+
137+
write!(f, "]")
138+
}
139+
}

crates/utils/src/attr/store.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use crate::{attr::AttrCtx, GPosIdx};
2-
use std::collections::HashMap;
2+
use std::{collections::HashMap, hash::Hash};
33

44
/// A store for attributes
55
#[derive(Clone)]
66
pub struct AttrStore<Attr, Value>
77
where
8-
Attr: Eq + std::hash::Hash + Copy,
8+
Attr: Eq + Hash + Copy,
99
{
1010
attrs: HashMap<Attr, (Value, GPosIdx)>,
1111
}
1212

1313
impl<Attr, Value> AttrStore<Attr, Value>
1414
where
15-
Attr: Eq + std::hash::Hash + Copy,
15+
Attr: Eq + Hash + Copy,
1616
{
1717
/// iterates over the attributes
1818
pub fn iter(&self) -> impl Iterator<Item = (Attr, &Value)> {
@@ -22,7 +22,7 @@ where
2222

2323
impl<Attr, Value> AttrCtx<Attr, Value> for AttrStore<Attr, Value>
2424
where
25-
Attr: Eq + std::hash::Hash + Copy,
25+
Attr: Eq + Hash + Copy,
2626
{
2727
fn get(&self, attr: Attr) -> Option<&Value> {
2828
self.attrs.get(&attr).map(|(value, _)| value)
@@ -43,7 +43,7 @@ where
4343

4444
impl<Attr, Value> Default for AttrStore<Attr, Value>
4545
where
46-
Attr: Eq + std::hash::Hash + Copy,
46+
Attr: Eq + Hash + Copy,
4747
{
4848
fn default() -> Self {
4949
Self {

crates/utils/src/macros.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! attr_enum {
3535
)*
3636
};
3737
) => {
38-
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, Eq, Hash)]
38+
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, Debug, Eq, Hash)]
3939
pub enum $name {
4040
$(
4141
$(#[$pub_meta])*
@@ -48,6 +48,23 @@ macro_rules! attr_enum {
4848
$priv,
4949
)*
5050
}
51+
52+
impl std::fmt::Display for $name {
53+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54+
match self {
55+
$(
56+
Self::$pub => write!(f, "{}", $pub_str),
57+
)*
58+
$(
59+
Self::$priv => write!(f, "{}", stringify!($priv)),
60+
)*
61+
// Unreachable pattern here to handle the case where the enum
62+
// has no variants
63+
#[allow(unreachable_patterns)]
64+
_ => write!(f, "{}", stringify!($name)),
65+
}
66+
}
67+
}
5168
};
5269
(enum $name:ident;) => {
5370
$crate::attr_enum! {
@@ -89,7 +106,7 @@ macro_rules! attr_enum {
89106
priv {
90107
$(
91108
$(#[$priv_meta])*
92-
$priv,
109+
$priv: $priv_str,
93110
)*
94111
};
95112
}

0 commit comments

Comments
 (0)