midenc_hir/dialects/builtin/attributes/
list.rs1use core::fmt;
2
3use crate::{
4 AttrPrinter, Attribute, OpPrintingFlags, attributes::AttrList, derive::DialectAttribute,
5 dialects::builtin::BuiltinDialect, print::AsmPrinter,
6};
7
8#[derive(DialectAttribute, Default)]
9#[attribute(dialect = BuiltinDialect, implements(AttrPrinter))]
10pub struct List(AttrList);
11
12impl fmt::Debug for List {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 let mut list = f.debug_list();
15 for entry in self.0.iter() {
16 let attr = entry.as_trait::<dyn Attribute>().unwrap();
17 list.entry_with(|f| write!(f, "{attr:?}"));
18 }
19 list.finish()
20 }
21}
22
23impl fmt::Display for List {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 let mut list = f.debug_list();
26 let flags = OpPrintingFlags::default();
27 for entry in self.0.iter() {
28 let attr = entry.as_trait::<dyn Attribute>().unwrap();
29 list.entry_with(|f| {
30 let context = attr.context_rc();
31 let mut printer = AsmPrinter::new(context, &flags);
32 printer.print_attribute_value(attr);
33 write!(f, "{}", printer.finish())
34 });
35 }
36 list.finish()
37 }
38}
39
40impl Eq for List {}
41impl PartialEq for List {
42 fn eq(&self, other: &Self) -> bool {
43 let mut lhs = self.0.front();
44 let mut rhs = other.0.front();
45 loop {
46 match (lhs.get(), rhs.get()) {
47 (None, None) => break true,
48 (Some(l), Some(r)) => {
49 if !l
50 .as_trait::<dyn Attribute>()
51 .unwrap()
52 .dyn_eq(r.as_trait::<dyn Attribute>().unwrap())
53 {
54 break false;
55 }
56 }
57 _ => break false,
58 }
59
60 lhs.move_next();
61 rhs.move_next();
62 }
63 }
64}
65
66impl Clone for List {
67 fn clone(&self) -> Self {
68 let mut list = AttrList::new();
69 for attr in self.0.iter() {
70 let cloned = attr.as_trait::<dyn Attribute>().unwrap().dyn_clone();
71 let attr = cloned.borrow().as_attr().as_attr_ref();
72 list.push_back(attr);
73 }
74 Self(list)
75 }
76}
77
78impl core::hash::Hash for List {
79 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
80 for attr in self.0.iter() {
81 attr.as_trait::<dyn Attribute>().unwrap().dyn_hash(state);
82 }
83 }
84}
85
86impl From<AttrList> for List {
87 fn from(value: AttrList) -> Self {
88 Self(value)
89 }
90}
91
92impl From<List> for AttrList {
93 fn from(value: List) -> Self {
94 value.0
95 }
96}
97
98impl AsRef<AttrList> for List {
99 fn as_ref(&self) -> &AttrList {
100 &self.0
101 }
102}
103
104impl AsMut<AttrList> for List {
105 fn as_mut(&mut self) -> &mut AttrList {
106 &mut self.0
107 }
108}
109
110impl AttrPrinter for ListAttr {
111 fn print(&self, _printer: &mut AsmPrinter<'_>) {
112 todo!()
113 }
114}