smt_lang/problem/
attribute.rs1use super::*;
2use crate::parser::Position;
3
4#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
7pub struct AttributeId<T: Id>(pub T, pub usize);
8
9impl<T: Id> Id for AttributeId<T> {
10 fn empty() -> Self {
11 Self(T::empty(), 0)
12 }
13}
14
15#[derive(Clone)]
18pub struct Attribute<T: Id> {
19 id: AttributeId<T>,
20 name: String,
21 typ: Type,
22 expr: Option<Expr>,
23 position: Option<Position>,
24}
25
26impl<T: Id> Attribute<T> {
27 pub fn new<S: Into<String>>(
28 name: S,
29 typ: Type,
30 expr: Option<Expr>,
31 position: Option<Position>,
32 ) -> Self {
33 let id = AttributeId::empty();
34 let name = name.into();
35 Self {
36 id,
37 name,
38 typ,
39 expr,
40 position,
41 }
42 }
43
44 pub fn resolve_type_expr(&mut self, entries: &TypeEntries) -> Result<(), Error> {
47 if let Some(expr) = &self.expr {
48 let e = expr.resolve_type(entries)?;
49 self.expr = Some(e);
50 }
51 Ok(())
52 }
53}
54
55impl<T: Id> WithPosition for Attribute<T> {
58 fn position(&self) -> &Option<Position> {
59 &self.position
60 }
61}
62impl<T: Id> Named<AttributeId<T>> for Attribute<T> {
65 fn id(&self) -> AttributeId<T> {
66 self.id
67 }
68
69 fn set_id(&mut self, id: AttributeId<T>) {
70 self.id = id;
71 }
72
73 fn name(&self) -> &str {
74 &self.name
75 }
76}
77
78impl<T: Id> WithType for Attribute<T> {
81 fn typ(&self) -> &Type {
82 &self.typ
83 }
84
85 fn set_type(&mut self, typ: Type) {
86 self.typ = typ;
87 }
88
89 fn resolve_type_children(&mut self, _: &TypeEntries) -> Result<(), Error> {
90 Ok(())
91 }
92
93 fn check_interval_children(&self, _: &Problem) -> Result<(), Error> {
94 Ok(())
95 }
96}
97
98impl WithExpr for Attribute<StructureId> {
101 fn expr(&self) -> &Option<Expr> {
102 &self.expr
103 }
104
105 fn set_expr(&mut self, expr: Option<Expr>) {
106 self.expr = expr;
107 }
108
109 fn entries(&self) -> Entries {
110 let AttributeId(structure_id, _) = self.id();
111 Entries::new(vec![Entry::new(
112 "self".to_string(),
113 EntryType::StrucSelf(structure_id),
114 )])
115 }
116}
117
118impl WithExpr for Attribute<ClassId> {
119 fn expr(&self) -> &Option<Expr> {
120 &self.expr
121 }
122
123 fn set_expr(&mut self, expr: Option<Expr>) {
124 self.expr = expr;
125 }
126
127 fn entries(&self) -> Entries {
128 let AttributeId(class_id, _) = self.id();
129 Entries::new(vec![Entry::new(
130 "self".to_string(),
131 EntryType::ClassSelf(class_id),
132 )])
133 }
134}
135
136impl<T: Id> ToLang for Attribute<T> {
139 fn to_lang(&self, problem: &Problem) -> String {
140 let mut s = format!(" {}: {}", self.name(), self.typ.to_lang(problem));
141 if let Some(e) = &self.expr {
142 s.push_str(&format!(" = {}", e.to_lang(problem)));
143 }
144 s
145 }
146}