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
use std::borrow::{Borrow, Cow};
use std::fmt::{self, Debug, Display};
use crate::common::errors::*;
use crate::konst2::{FloatingConst, IntegerConst};
use crate::ty2::Type;
pub trait Const2<'t>: Debug + Display {
fn ty(&self) -> &'t Type;
fn into_owned(self) -> OwnedConst<'t>;
fn to_owned(&self) -> OwnedConst<'t>;
fn as_any<'r>(&'r self) -> AnyConst<'r, 't>;
fn cast(&self, ty: &'t Type) -> Result<Cow<Const2<'t> + 't>, ConstError>;
}
impl<'t> ToOwned for Const2<'t> + 't {
type Owned = OwnedConst<'t>;
fn to_owned(&self) -> OwnedConst<'t> {
Const2::to_owned(self)
}
}
#[derive(Debug, Clone)]
pub enum ConstError {
OutOfRange,
}
impl EmitError for ConstError {
type Output = ();
fn emit<C: DiagEmitter>(self, ctx: C) {
match self {
ConstError::OutOfRange => ctx.emit(DiagBuilder2::error("constant value out of range")),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum AnyConst<'r, 't: 'r> {
Integer(&'r IntegerConst<'t>),
Floating(&'r FloatingConst<'t>),
}
impl<'r, 't> Display for AnyConst<'r, 't> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AnyConst::Integer(t) => Display::fmt(t, f),
AnyConst::Floating(t) => Display::fmt(t, f),
}
}
}
impl<'r, 't> Debug for AnyConst<'r, 't> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AnyConst::Integer(t) => Debug::fmt(t, f),
AnyConst::Floating(t) => Debug::fmt(t, f),
}
}
}
impl<'r, 't, T: Const2<'t>> From<&'r T> for AnyConst<'r, 't> {
fn from(konst: &'r T) -> AnyConst<'r, 't> {
konst.as_any()
}
}
#[allow(unreachable_patterns)]
impl<'r, 't> AnyConst<'r, 't> {
pub fn as_const(self) -> &'r Const2<'t> {
match self {
AnyConst::Integer(k) => k,
AnyConst::Floating(k) => k,
}
}
pub fn as_integer(self) -> Option<&'r IntegerConst<'t>> {
match self {
AnyConst::Integer(k) => Some(k),
_ => None,
}
}
pub fn as_floating(self) -> Option<&'r FloatingConst<'t>> {
match self {
AnyConst::Floating(k) => Some(k),
_ => None,
}
}
pub fn unwrap_integer(self) -> &'r IntegerConst<'t> {
self.as_integer().expect("constant is not an integer")
}
pub fn unwrap_floating(self) -> &'r FloatingConst<'t> {
self.as_floating().expect("constant is not a float")
}
}
#[derive(Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum OwnedConst<'t> {
Integer(IntegerConst<'t>),
Floating(FloatingConst<'t>),
}
impl<'t> Borrow<Const2<'t> + 't> for OwnedConst<'t> {
fn borrow(&self) -> &(Const2<'t> + 't) {
match *self {
OwnedConst::Integer(ref k) => k,
OwnedConst::Floating(ref k) => k,
}
}
}
impl<'t> Display for OwnedConst<'t> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OwnedConst::Integer(ref t) => Display::fmt(t, f),
OwnedConst::Floating(ref t) => Display::fmt(t, f),
}
}
}
impl<'t> Debug for OwnedConst<'t> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OwnedConst::Integer(ref t) => Debug::fmt(t, f),
OwnedConst::Floating(ref t) => Debug::fmt(t, f),
}
}
}
impl<'t, T: Const2<'t>> From<T> for OwnedConst<'t> {
fn from(konst: T) -> OwnedConst<'t> {
konst.to_owned()
}
}