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
219
220
221
222
223
224
#![deny(missing_docs)]
use num::{BigInt, BigRational, ToPrimitive};
use crate::common::SessionContext;
use crate::common::errors::*;
use crate::hir::prelude::*;
use crate::ty2::{UniversalIntegerType, UniversalRealType};
use crate::konst2::{Const2, FloatingConst, IntegerConst};
pub use crate::syntax::ast::Dir;
pub trait Expr2<'t>: Node<'t> {
fn typeval(&self, tyctx: Option<&'t Type>, ctx: &ExprContext<'t>) -> Result<&'t Type>;
fn constant_value(&self, ctx: &ExprContext<'t>) -> Result<&'t Const2<'t>>;
}
pub trait ExprContext<'t>
: SessionContext
+ AllocInto<'t, IntegerConst<'t>>
+ AllocOwnedInto<'t, Const2<'t>>
+ AllocOwnedInto<'t, Type> {
}
impl<'t, T> ExprContext<'t> for T
where
T: SessionContext
+ AllocInto<'t, IntegerConst<'t>>
+ AllocOwnedInto<'t, Const2<'t>>
+ AllocOwnedInto<'t, Type>,
{
}
#[derive(Debug)]
pub struct LitExpr {
span: Span,
value: LitExprValue,
}
#[derive(Debug)]
pub enum LitExprValue {
Integer(BigInt),
Float(BigRational),
}
impl LitExpr {
pub fn new_integer(span: Span, value: BigInt) -> LitExpr {
LitExpr {
span: span,
value: LitExprValue::Integer(value),
}
}
pub fn new_float(span: Span, value: BigRational) -> LitExpr {
LitExpr {
span: span,
value: LitExprValue::Float(value),
}
}
pub fn value(&self) -> &LitExprValue {
&self.value
}
pub fn is_integer(&self) -> bool {
match self.value {
LitExprValue::Integer(..) => true,
_ => false,
}
}
pub fn is_float(&self) -> bool {
match self.value {
LitExprValue::Float(..) => true,
_ => false,
}
}
pub fn integer_value(&self) -> Option<&BigInt> {
match self.value {
LitExprValue::Integer(ref v) => Some(v),
_ => None,
}
}
pub fn float_value(&self) -> Option<&BigRational> {
match self.value {
LitExprValue::Float(ref v) => Some(v),
_ => None,
}
}
}
impl<'t> Node<'t> for LitExpr {
fn span(&self) -> Span {
self.span
}
fn desc_kind(&self) -> String {
match self.value {
LitExprValue::Integer(..) => "integer literal".into(),
LitExprValue::Float(..) => "floating-point literal".into(),
}
}
fn accept(&'t self, visitor: &mut Visitor<'t>) {
visitor.visit_literal_expr(self);
}
fn walk(&'t self, _visitor: &mut Visitor<'t>) {}
}
impl<'t> Expr2<'t> for LitExpr {
fn typeval(&self, _: Option<&'t Type>, _: &ExprContext<'t>) -> Result<&'t Type> {
Ok(match self.value {
LitExprValue::Integer(..) => &UniversalIntegerType,
LitExprValue::Float(..) => &UniversalRealType,
})
}
fn constant_value(&self, ctx: &ExprContext<'t>) -> Result<&'t Const2<'t>> {
Ok(ctx.alloc_owned(match self.value {
LitExprValue::Integer(ref v) => IntegerConst::try_new(&UniversalIntegerType, v.clone())
.emit(ctx)?
.into_owned(),
LitExprValue::Float(ref v) => {
let f = v.numer().to_f64().unwrap() / v.denom().to_f64().unwrap();
FloatingConst::try_new(&UniversalRealType, f)
.emit(ctx)?
.into_owned()
}
}))
}
}
#[derive(Debug)]
pub enum Range2<'t> {
Immediate(Span, Spanned<Dir>, &'t Expr2<'t>, &'t Expr2<'t>),
}
impl<'t> Range2<'t> {
pub fn bound_type<C>(&self, ctx: C) -> Result<&'t Type>
where
C: ExprContext<'t> + Copy,
{
match *self {
Range2::Immediate(span, _, l, r) => {
let lt = l.typeval(None, &ctx);
let rt = r.typeval(None, &ctx);
let (lt, rt) = (lt?, rt?);
if lt == rt {
Ok(lt)
} else if lt.is_implicitly_castable(rt) {
Ok(rt)
} else if rt.is_implicitly_castable(lt) {
Ok(lt)
} else {
ctx.emit(
DiagBuilder2::error(format!(
"range bounds `{}` and `{}` have incompatible types",
l.span().extract(),
r.span().extract()
)).span(span)
.add_note(format!(" left bound type: {}", lt))
.add_note(format!("right bound type: {}", rt)),
);
Err(())
}
}
}
}
pub fn constant_value<C>(&self, ctx: C) -> Result<(Dir, &'t Const2<'t>, &'t Const2<'t>)>
where
C: ExprContext<'t> + Copy,
{
let ty = self.bound_type(ctx)?;
match *self {
Range2::Immediate(_, d, l, r) => {
let lc = l.constant_value(&ctx).and_then(|x| x.cast(ty).emit(ctx));
let rc = r.constant_value(&ctx).and_then(|x| x.cast(ty).emit(ctx));
Ok((d.value, ctx.maybe_alloc(lc?), ctx.maybe_alloc(rc?)))
}
}
}
}