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
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
pub use leo_ast::UnaryOperation;
use std::cell::Cell;
#[derive(Clone)]
pub struct UnaryExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>,
pub operation: UnaryOperation,
pub inner: Cell<&'a Expression<'a>>,
}
impl<'a> Node for UnaryExpression<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}
impl<'a> ExpressionNode<'a> for UnaryExpression<'a> {
fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent));
}
fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.get()
}
fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.inner.get().set_parent(expr);
}
fn get_type(&self) -> Option<Type<'a>> {
self.inner.get().get_type()
}
fn is_mut_ref(&self) -> bool {
false
}
fn const_value(&self) -> Option<ConstValue> {
if let Some(inner) = self.inner.get().const_value() {
match self.operation {
UnaryOperation::Not => match inner {
ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)),
_ => None,
},
UnaryOperation::Negate => {
match inner {
ConstValue::Int(value) => Some(ConstValue::Int(value.value_negate()?)),
_ => None,
}
}
UnaryOperation::BitNot => match inner {
ConstValue::Int(value) => Some(ConstValue::Int(value.value_bit_negate()?)),
_ => None,
},
}
} else {
None
}
}
fn is_consty(&self) -> bool {
self.inner.get().is_consty()
}
}
impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
fn from_ast(
scope: &'a Scope<'a>,
value: &leo_ast::UnaryExpression,
expected_type: Option<PartialType<'a>>,
) -> Result<UnaryExpression<'a>, AsgConvertError> {
let expected_type = match value.op {
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
Some(Type::Boolean) | None => Some(Type::Boolean),
Some(type_) => {
return Err(AsgConvertError::unexpected_type(
&type_.to_string(),
Some(&*Type::Boolean.to_string()),
&value.span,
));
}
},
UnaryOperation::Negate => match expected_type.map(|x| x.full()).flatten() {
Some(type_ @ Type::Integer(_)) => Some(type_),
Some(Type::Group) => Some(Type::Group),
Some(Type::Field) => Some(Type::Field),
None => None,
Some(type_) => {
return Err(AsgConvertError::unexpected_type(
&type_.to_string(),
Some("integer, group, field"),
&value.span,
));
}
},
UnaryOperation::BitNot => match expected_type.map(|x| x.full()).flatten() {
Some(type_ @ Type::Integer(_)) => Some(type_),
None => None,
Some(type_) => {
return Err(AsgConvertError::unexpected_type(
&type_.to_string(),
Some("integer"),
&value.span,
));
}
},
};
let expr = <&Expression<'a>>::from_ast(scope, &*value.inner, expected_type.map(Into::into))?;
if matches!(value.op, UnaryOperation::Negate) {
let is_expr_unsigned = expr
.get_type()
.map(|x| match x {
Type::Integer(x) => !x.is_signed(),
_ => false,
})
.unwrap_or(false);
if is_expr_unsigned {
return Err(AsgConvertError::unsigned_negation(&value.span));
}
}
Ok(UnaryExpression {
parent: Cell::new(None),
span: Some(value.span.clone()),
operation: value.op.clone(),
inner: Cell::new(expr),
})
}
}
impl<'a> Into<leo_ast::UnaryExpression> for &UnaryExpression<'a> {
fn into(self) -> leo_ast::UnaryExpression {
leo_ast::UnaryExpression {
op: self.operation.clone(),
inner: Box::new(self.inner.get().into()),
span: self.span.clone().unwrap_or_default(),
}
}
}