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
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 CastExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>,
pub inner: Cell<&'a Expression<'a>>,
pub target_type: Type<'a>,
}
impl<'a> Node for CastExpression<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}
impl<'a> ExpressionNode<'a> for CastExpression<'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>> {
Some(self.target_type.clone())
}
fn is_mut_ref(&self) -> bool {
false
}
fn const_value(&self) -> Option<ConstValue> {
let value = self.inner.get().const_value()?;
match value {
ConstValue::Int(int) => match &self.target_type {
Type::Integer(target) => Some(ConstValue::Int(int.cast_to(target))),
_ => None,
},
_ => None,
}
}
fn is_consty(&self) -> bool {
self.inner.get().is_consty()
}
}
impl<'a> FromAst<'a, leo_ast::CastExpression> for CastExpression<'a> {
fn from_ast(
scope: &'a Scope<'a>,
value: &leo_ast::CastExpression,
expected_type: Option<PartialType<'a>>,
) -> Result<CastExpression<'a>, AsgConvertError> {
let target_type = scope.resolve_ast_type(&value.target_type)?;
if let Some(expected_type) = &expected_type {
if !expected_type.matches(&target_type) {
return Err(AsgConvertError::unexpected_type(
&expected_type.to_string(),
Some(&target_type.to_string()),
&value.span,
));
}
}
let inner = <&Expression<'a>>::from_ast(scope, &*value.inner, None)?;
Ok(CastExpression {
parent: Cell::new(None),
span: Some(value.span.clone()),
inner: Cell::new(inner),
target_type,
})
}
}
impl<'a> Into<leo_ast::CastExpression> for &CastExpression<'a> {
fn into(self) -> leo_ast::CastExpression {
leo_ast::CastExpression {
target_type: (&self.target_type).into(),
inner: Box::new(self.inner.get().into()),
span: self.span.clone().unwrap_or_default(),
}
}
}