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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
pub use self::TypeKind::*;
use std;
use std::sync::Arc;
use crate::util::write_implode;
pub type Type = Arc<TypeKind>;
#[derive(Debug, PartialEq, Eq)]
pub enum TypeKind {
VoidType,
TimeType,
IntType(usize),
EnumType(usize),
PointerType(Type),
SignalType(Type),
VectorType(usize, Type),
StructType(Vec<Type>),
FuncType(Vec<Type>, Type),
EntityType(Vec<Type>, Vec<Type>),
}
impl std::fmt::Display for TypeKind {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
VoidType => write!(f, "void"),
TimeType => write!(f, "time"),
IntType(l) => write!(f, "i{}", l),
EnumType(l) => write!(f, "n{}", l),
PointerType(ref ty) => write!(f, "{}*", ty),
SignalType(ref ty) => write!(f, "{}$", ty),
VectorType(l, ref ty) => write!(f, "<{} x {}>", l, ty),
StructType(ref tys) => {
write!(f, "{{")?;
write_implode(f, ", ", tys.iter())?;
write!(f, "}}")?;
Ok(())
}
FuncType(ref args, ref ret) => {
write!(f, "(")?;
write_implode(f, ", ", args.iter())?;
write!(f, ") {}", ret)?;
Ok(())
}
EntityType(ref ins, ref outs) => {
write!(f, "(")?;
write_implode(f, ", ", ins.iter())?;
write!(f, ";")?;
write_implode(f, ", ", outs.iter())?;
write!(f, ")")?;
Ok(())
}
}
}
}
impl TypeKind {
pub fn as_func(&self) -> (&[Type], &Type) {
match *self {
FuncType(ref args, ref ret) => (args, ret),
_ => panic!("as_func called on {}", self),
}
}
pub fn as_entity(&self) -> (&[Type], &[Type]) {
match *self {
EntityType(ref ins, ref outs) => (ins, outs),
_ => panic!("as_entity called on {}", self),
}
}
pub fn as_int(&self) -> usize {
match *self {
IntType(size) => size,
_ => panic!("as_int called on {}", self),
}
}
pub fn as_enum(&self) -> usize {
match *self {
EnumType(size) => size,
_ => panic!("as_enum called on {}", self),
}
}
pub fn as_signal(&self) -> &Type {
match *self {
SignalType(ref ty) => ty,
_ => panic!("as_signal called on {}", self),
}
}
pub fn is_void(&self) -> bool {
match *self {
VoidType => true,
_ => false,
}
}
}
pub fn void_ty() -> Type {
Type::new(VoidType)
}
pub fn time_ty() -> Type {
Type::new(TimeType)
}
pub fn int_ty(size: usize) -> Type {
Type::new(IntType(size))
}
pub fn enum_ty(size: usize) -> Type {
Type::new(EnumType(size))
}
pub fn pointer_ty(ty: Type) -> Type {
Type::new(PointerType(ty))
}
pub fn signal_ty(ty: Type) -> Type {
Type::new(SignalType(ty))
}
pub fn vector_ty(size: usize, ty: Type) -> Type {
Type::new(VectorType(size, ty))
}
pub fn struct_ty(fields: Vec<Type>) -> Type {
Type::new(StructType(fields))
}
pub fn func_ty(args: Vec<Type>, ret: Type) -> Type {
Type::new(FuncType(args, ret))
}
pub fn entity_ty(ins: Vec<Type>, outs: Vec<Type>) -> Type {
Type::new(EntityType(ins, outs))
}