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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::Circuit;
pub use leo_ast::IntegerType;
use std::fmt;
#[derive(Clone, PartialEq)]
pub enum Type<'a> {
Address,
Boolean,
Char,
Field,
Group,
Integer(IntegerType),
Array(Box<Type<'a>>, usize),
Tuple(Vec<Type<'a>>),
Circuit(&'a Circuit<'a>),
}
#[derive(Clone, PartialEq)]
pub enum PartialType<'a> {
Type(Type<'a>),
Integer(Option<IntegerType>, Option<IntegerType>),
Array(Option<Box<PartialType<'a>>>, Option<usize>),
Tuple(Vec<Option<PartialType<'a>>>),
}
impl<'a> Into<Option<Type<'a>>> for PartialType<'a> {
fn into(self) -> Option<Type<'a>> {
match self {
PartialType::Type(t) => Some(t),
PartialType::Integer(sub_type, contextual_type) => Some(Type::Integer(sub_type.or(contextual_type)?)),
PartialType::Array(element, len) => Some(Type::Array(Box::new((*element?).full()?), len?)),
PartialType::Tuple(sub_types) => Some(Type::Tuple(
sub_types
.into_iter()
.map(|x| x.map(|x| x.full()).flatten())
.collect::<Option<Vec<Type>>>()?,
)),
}
}
}
impl<'a> PartialType<'a> {
pub fn full(self) -> Option<Type<'a>> {
self.into()
}
pub fn matches(&self, other: &Type<'a>) -> bool {
match (self, other) {
(PartialType::Type(t), other) => t.is_assignable_from(other),
(PartialType::Integer(self_sub_type, _), Type::Integer(sub_type)) => {
self_sub_type.as_ref().map(|x| x == sub_type).unwrap_or(true)
}
(PartialType::Array(element, len), Type::Array(other_element, other_len)) => {
if let Some(element) = element {
if !element.matches(&*other_element) {
return false;
}
}
if let Some(len) = len {
return len == other_len;
}
true
}
(PartialType::Tuple(sub_types), Type::Tuple(other_sub_types)) => {
if sub_types.len() > other_sub_types.len() {
return false;
}
for (sub_type, other_sub_type) in sub_types.iter().zip(other_sub_types.iter()) {
if let Some(sub_type) = sub_type {
if !sub_type.matches(other_sub_type) {
return false;
}
}
}
true
}
_ => false,
}
}
}
impl<'a> Into<PartialType<'a>> for Type<'a> {
fn into(self) -> PartialType<'a> {
match self {
Type::Integer(sub_type) => PartialType::Integer(Some(sub_type), None),
Type::Array(element, len) => PartialType::Array(Some(Box::new((*element).into())), Some(len)),
Type::Tuple(sub_types) => PartialType::Tuple(sub_types.into_iter().map(Into::into).map(Some).collect()),
x => PartialType::Type(x),
}
}
}
impl<'a> Type<'a> {
pub fn is_assignable_from(&self, from: &Type<'a>) -> bool {
self == from
}
pub fn partial(self) -> PartialType<'a> {
self.into()
}
pub fn is_unit(&self) -> bool {
matches!(self, Type::Tuple(t) if t.is_empty())
}
pub fn can_cast_to(&self, to: &Type<'a>) -> bool {
matches!(self, Type::Integer(_)) && matches!(to, Type::Integer(_))
}
}
impl<'a> fmt::Display for Type<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Type::Address => write!(f, "address"),
Type::Boolean => write!(f, "bool"),
Type::Char => write!(f, "char"),
Type::Field => write!(f, "field"),
Type::Group => write!(f, "group"),
Type::Integer(sub_type) => sub_type.fmt(f),
Type::Array(sub_type, len) => write!(f, "[{}; {}]", sub_type, len),
Type::Tuple(sub_types) => {
write!(f, "(")?;
for (i, sub_type) in sub_types.iter().enumerate() {
write!(f, "{}", sub_type)?;
if i < sub_types.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, ")")
}
Type::Circuit(circuit) => write!(f, "{}", &circuit.name.borrow().name),
}
}
}
impl<'a> fmt::Display for PartialType<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PartialType::Type(t) => t.fmt(f),
PartialType::Integer(Some(sub_type), _) => write!(f, "{}", sub_type),
PartialType::Integer(_, Some(sub_type)) => write!(f, "<{}>", sub_type),
PartialType::Integer(_, _) => write!(f, "integer"),
PartialType::Array(sub_type, len) => {
write!(f, "[")?;
if let Some(sub_type) = sub_type {
write!(f, "{}", *sub_type)?;
} else {
write!(f, "?")?;
}
write!(f, "; ")?;
if let Some(len) = len {
write!(f, "{}", len)?;
} else {
write!(f, "?")?;
}
write!(f, "]")
}
PartialType::Tuple(sub_types) => {
write!(f, "(")?;
for (i, sub_type) in sub_types.iter().enumerate() {
if let Some(sub_type) = sub_type {
write!(f, "{}", *sub_type)?;
} else {
write!(f, "?")?;
}
if i < sub_types.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, ")")
}
}
}
}
impl<'a> Into<leo_ast::Type> for &Type<'a> {
fn into(self) -> leo_ast::Type {
use Type::*;
match self {
Address => leo_ast::Type::Address,
Boolean => leo_ast::Type::Boolean,
Char => leo_ast::Type::Char,
Field => leo_ast::Type::Field,
Group => leo_ast::Type::Group,
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),
Array(type_, len) => leo_ast::Type::Array(
Box::new(type_.as_ref().into()),
leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: len.to_string().into(),
}]),
),
Tuple(subtypes) => leo_ast::Type::Tuple(subtypes.iter().map(Into::into).collect()),
Circuit(circuit) => leo_ast::Type::Circuit(circuit.name.borrow().clone()),
}
}
}