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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
*/
#include "types.hpp"
#include "ast/ast.hpp"
const char* coretype_name(const eCoreType ct ) {
switch(ct)
{
case CORETYPE_INVAL:return "-";
case CORETYPE_ANY: return "_";
case CORETYPE_CHAR: return "char";
case CORETYPE_UINT: return "usize";
case CORETYPE_INT: return "isize";
case CORETYPE_U8: return "u8";
case CORETYPE_I8: return "i8";
case CORETYPE_U16: return "u16";
case CORETYPE_I16: return "i16";
case CORETYPE_U32: return "u32";
case CORETYPE_I32: return "i32";
case CORETYPE_U64: return "u64";
case CORETYPE_I64: return "i64";
case CORETYPE_F32: return "f32";
case CORETYPE_F64: return "f64";
}
DEBUG("Unknown core type?! " << ct);
return "NFI";
}
void TypeRef::merge_with(const TypeRef& other)
{
// Ignore if other is wildcard
if( other.m_class == TypeRef::ANY )
return;
if( m_class == TypeRef::ANY ) {
*this = other;
return;
}
if( m_class != other.m_class )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible");
if( is_concrete() && other.is_concrete() )
{
if( *this != other )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible");
return;
}
switch(m_class)
{
case TypeRef::ANY:
case TypeRef::UNIT:
case TypeRef::PRIMITIVE:
throw ::std::runtime_error("TypeRef::merge_with - Reached concrete/wildcard");
case TypeRef::TUPLE:
// Other is known not to be wildcard, and is also a tuple, so it must be the same size
if( m_inner_types.size() != other.m_inner_types.size() )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible [tuple sz]");
for(unsigned int i = 0; i < m_inner_types.size(); i ++)
{
m_inner_types[i].merge_with( other.m_inner_types[i] );
}
break;
case TypeRef::REFERENCE:
case TypeRef::POINTER:
if( m_is_inner_mutable != other.m_is_inner_mutable )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible [inner mut]");
assert( m_inner_types.size() == 1 );
assert( other.m_inner_types.size() == 1 );
m_inner_types[0].merge_with( other.m_inner_types[0] );
break;
case TypeRef::ARRAY:
throw ::std::runtime_error("TODO: TypeRef::merge_with on ARRAY");
case TypeRef::GENERIC:
throw ::std::runtime_error("TODO: TypeRef::merge_with on GENERIC");
case TypeRef::PATH:
throw ::std::runtime_error("TODO: TypeRef::merge_with on PATH");
case TypeRef::ASSOCIATED:
throw ::std::runtime_error("TODO: TypeRef::merge_with on ASSOCIATED");
}
}
bool TypeRef::is_concrete() const
{
switch(m_class)
{
case TypeRef::ANY:
return false;
case TypeRef::UNIT:
case TypeRef::PRIMITIVE:
return true;
case TypeRef::TUPLE:
case TypeRef::REFERENCE:
case TypeRef::POINTER:
case TypeRef::ARRAY:
for(const auto& t : m_inner_types )
if( not t.is_concrete() )
return false;
return true;
case TypeRef::GENERIC:
// Well, I guess a generic param is "concrete"
return true;
case TypeRef::PATH:
for(const auto& n : m_path.nodes())
{
for(const auto& p : n.args())
if( not p.is_concrete() )
return false;
}
return true;
case TypeRef::ASSOCIATED:
for(const auto& t : m_inner_types )
if( not t.is_concrete() )
return false;
for(const auto& n : m_path.nodes())
{
for(const auto& p : n.args())
if( not p.is_concrete() )
return false;
}
return true;
}
throw ::std::runtime_error( FMT("BUGCHECK - Invalid type class on " << *this) );
}
bool TypeRef::operator==(const TypeRef& x) const
{
if(m_class != x.m_class)
return false;
switch(m_class)
{
case TypeRef::ANY:
case TypeRef::UNIT:
return true;
case TypeRef::PRIMITIVE:
return m_core_type == x.m_core_type;
case TypeRef::TUPLE:
return m_inner_types == x.m_inner_types;
case TypeRef::REFERENCE:
case TypeRef::POINTER:
return m_is_inner_mutable == x.m_is_inner_mutable && m_inner_types == x.m_inner_types;
case TypeRef::ARRAY:
if(m_inner_types[0] != x.m_inner_types[0])
return false;
if(m_size_expr.get())
{
throw ::std::runtime_error("TODO: Sized array comparisons");
}
return true;
case TypeRef::GENERIC:
DEBUG(*this << " == " << x);
throw ::std::runtime_error("BUGCHECK - Can't compare generic type");
case TypeRef::PATH:
return m_path == x.m_path;
case TypeRef::ASSOCIATED:
return m_path == x.m_path && m_inner_types == x.m_inner_types;
}
throw ::std::runtime_error(FMT("BUGCHECK - Unhandled TypeRef class '" << m_class << "'"));
}
::std::ostream& operator<<(::std::ostream& os, const eCoreType ct) {
return os << coretype_name(ct);
}
::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr) {
os << "TypeRef(";
switch(tr.m_class)
{
case TypeRef::ANY:
//os << "TagAny";
os << "_";
break;
case TypeRef::UNIT:
//os << "TagUnit";
os << "()";
break;
case TypeRef::PRIMITIVE:
//os << "TagPrimitive, " << tr.m_core_type;
os << tr.m_core_type;
break;
case TypeRef::TUPLE:
//os << "TagTuple, {" << tr.m_inner_types << "}";
os << "(" << tr.m_inner_types << ",)";
break;
case TypeRef::REFERENCE:
//os << "TagReference, " << (tr.m_is_inner_mutable ? "mut" : "const") << ", " << tr.m_inner_types[0];
os << "&" << (tr.m_is_inner_mutable ? "mut " : "") << tr.m_inner_types[0];
break;
case TypeRef::POINTER:
//os << "TagPointer, " << (tr.m_is_inner_mutable ? "mut" : "const") << ", " << tr.m_inner_types[0];
os << "*" << (tr.m_is_inner_mutable ? "mut" : "const") << " " << tr.m_inner_types[0];
break;
case TypeRef::ARRAY:
os << "TagSizedArray, " << tr.m_inner_types[0] << ", " << tr.m_size_expr;
break;
case TypeRef::GENERIC:
os << "TagArg, " << tr.m_path[0].name();
break;
case TypeRef::PATH:
//os << "TagPath, " << tr.m_path;
os << tr.m_path;
break;
case TypeRef::ASSOCIATED:
//os << "TagAssoc, <" << tr.m_inner_types[0] << " as " << tr.m_inner_types[1] << ">::" << tr.m_path[0].name();
os << "<" << tr.m_inner_types[0] << " as " << tr.m_inner_types[1] << ">::" << tr.m_path[0].name();
break;
}
os << ")";
return os;
}
void operator% (::Serialiser& s, eCoreType ct) {
s << coretype_name(ct);
}
void operator% (::Deserialiser& d, eCoreType& ct) {
::std::string n;
d.item(n);
/* */if(n == "-") ct = CORETYPE_INVAL;
else if(n == "_") ct = CORETYPE_ANY;
else if(n == "char") ct = CORETYPE_CHAR;
else if(n == "usize") ct = CORETYPE_UINT;
else if(n == "isize") ct = CORETYPE_INT;
else if(n == "u8") ct = CORETYPE_U8;
else if(n == "i8") ct = CORETYPE_I8;
else if(n == "u16") ct = CORETYPE_U16;
else if(n == "i16") ct = CORETYPE_I16;
else if(n == "u32") ct = CORETYPE_U32;
else if(n == "i32") ct = CORETYPE_I32;
else if(n == "u64") ct = CORETYPE_U64;
else if(n == "i64") ct = CORETYPE_I64;
else if(n == "f32") ct = CORETYPE_F32;
else if(n == "f64") ct = CORETYPE_F64;
else
throw ::std::runtime_error("Deserialise failure - coretype " + n);
}
const char* TypeRef::class_name(TypeRef::Class c) {
switch(c)
{
#define _(x) case TypeRef::x: return #x;
_(ANY)
_(UNIT)
_(PRIMITIVE)
_(TUPLE)
_(REFERENCE)
_(POINTER)
_(ARRAY)
_(GENERIC)
_(PATH)
_(ASSOCIATED)
#undef _
}
return "NFI";
}
void operator>>(::Deserialiser& d, TypeRef::Class& c) {
::std::string n;
d.item(n);
#define _(x) if(n == #x) c = TypeRef::x;
/**/ _(ANY)
else _(UNIT)
else _(PRIMITIVE)
else _(TUPLE)
else _(REFERENCE)
else _(POINTER)
else _(ARRAY)
else _(GENERIC)
else _(PATH)
else _(ASSOCIATED)
else
throw ::std::runtime_error("Deserialise failure - " + n);
#undef _
}
SERIALISE_TYPE(TypeRef::, "TypeRef", {
s << class_name(m_class);
if(m_class == PRIMITIVE)
s << coretype_name(m_core_type);
s << m_inner_types;
if(m_class == REFERENCE || m_class == POINTER)
s << m_is_inner_mutable;
s << m_size_expr;
s << m_path;
},{
s >> m_class;
if(m_class == PRIMITIVE)
s % m_core_type;
s.item( m_inner_types );
if(m_class == REFERENCE || m_class == POINTER)
s.item( m_is_inner_mutable );
bool size_expr_present;
s.item(size_expr_present);
if( size_expr_present )
m_size_expr = AST::ExprNode::from_deserialiser(s);
else
m_size_expr.reset();
s.item( m_path );
})
|