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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/*
* MRustC - Mutabah's Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* types.cpp
* - Backing code for the TypeRef class
*
* Handles a chunk of type resolution (merging) and matching/comparing types
*/
#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";
}
/// Replace this type reference with a dereferenced version
bool TypeRef::deref(bool is_implicit)
{
switch(m_class)
{
case TypeRef::ANY:
// TODO: Check if the _ is bounded by Deref<Output=?>, if so use that
throw ::std::runtime_error("Dereferencing _");
break;
case TypeRef::UNIT:
throw ::std::runtime_error("Dereferencing ()");
case TypeRef::PRIMITIVE:
throw ::std::runtime_error("Dereferencing a primtive type");
case TypeRef::GENERIC:
throw ::std::runtime_error("Dereferencing a generic");
case TypeRef::REFERENCE:
*this = m_inner_types[0];
return true;
case TypeRef::POINTER:
// raw pointers can't be implicitly dereferenced
if( is_implicit )
return false;
*this = m_inner_types[0];
return true;
case TypeRef::TUPLE:
case TypeRef::ARRAY:
case TypeRef::PATH:
throw ::std::runtime_error("TODO: Search for an impl of Deref");
case TypeRef::ASSOCIATED:
throw ::std::runtime_error("TODO: TypeRef::deref on ASSOCIATED");
}
}
/// Merge the contents of the passed type with this type
///
/// \note Both types must be of the same form (i.e. both be tuples)
void TypeRef::merge_with(const TypeRef& other)
{
// Ignore if other is wildcard
if( other.m_class == TypeRef::ANY ) {
assert(other.m_inner_types.size() == 0 && "TODO: merge_with on bounded _");
return;
}
// If this is a wildcard, then replace with the othet type
if( m_class == TypeRef::ANY ) {
assert(m_inner_types.size() == 0 && "TODO: merge_with on bounded _");
*this = other;
return;
}
// If classes don't match, then merge is impossible
if( m_class != other.m_class )
throw ::std::runtime_error("TypeRef::merge_with - Types not compatible");
// If both types are concrete, then they must be the same
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");
}
}
/// Resolve all Generic/Argument types to the value returned by the passed closure
///
/// Replaces every instance of a TypeRef::GENERIC with the value returned from the passed
/// closure.
void TypeRef::resolve_args(::std::function<TypeRef(const char*)> fcn)
{
DEBUG("" << *this);
switch(m_class)
{
case TypeRef::ANY:
// TODO: Is resolving args on an ANY an erorr?
break;
case TypeRef::UNIT:
case TypeRef::PRIMITIVE:
break;
case TypeRef::TUPLE:
case TypeRef::REFERENCE:
case TypeRef::POINTER:
case TypeRef::ARRAY:
for( auto& t : m_inner_types )
t.resolve_args(fcn);
break;
case TypeRef::GENERIC:
*this = fcn(m_path[0].name().c_str());
break;
case TypeRef::PATH:
for(auto& n : m_path.nodes())
{
for(auto& p : n.args())
p.resolve_args(fcn);
}
break;
case TypeRef::ASSOCIATED:
for(auto& t : m_inner_types )
t.resolve_args(fcn);
break;
}
}
/// Match this type against another type, calling the provided function for all generics found in this
///
/// \param other Type containing (possibly) concrete types
/// \param fcn Function to call for all generics (called with matching type from \a other)
/// This is used to handle extracting types passsed to methods/enum variants
void TypeRef::match_args(const TypeRef& other, ::std::function<void(const char*,const TypeRef&)> fcn) const
{
// If the other type is a wildcard, early return
// - TODO - Might want to restrict the other type to be of the same form as this type
if( other.m_class == TypeRef::ANY )
return;
// If this type is a generic, then call the closure with the other type
if( m_class == TypeRef::GENERIC ) {
fcn( m_path[0].name().c_str(), other );
return ;
}
// Any other case, it's a "pattern" match
if( m_class != other.m_class )
throw ::std::runtime_error("Type mismatch (class)");
switch(m_class)
{
case TypeRef::ANY:
// Wait, isn't this an error?
throw ::std::runtime_error("Encountered '_' in match_args");
case TypeRef::UNIT:
break;
case TypeRef::PRIMITIVE:
// TODO: Should check if the type matches
if( m_core_type != other.m_core_type )
throw ::std::runtime_error("Type mismatch (core)");
break;
case TypeRef::TUPLE:
if( m_inner_types.size() != other.m_inner_types.size() )
throw ::std::runtime_error("Type mismatch (tuple size)");
for(unsigned int i = 0; i < m_inner_types.size(); i ++ )
m_inner_types[i].match_args( other.m_inner_types[i], fcn );
break;
case TypeRef::REFERENCE:
case TypeRef::POINTER:
if( m_is_inner_mutable != other.m_is_inner_mutable )
throw ::std::runtime_error("Type mismatch (inner mutable)");
m_inner_types[0].match_args( other.m_inner_types[0], fcn );
break;
case TypeRef::ARRAY:
throw ::std::runtime_error("TODO: TypeRef::match_args on ARRAY");
case TypeRef::GENERIC:
throw ::std::runtime_error("Encountered GENERIC in match_args");
case TypeRef::PATH:
throw ::std::runtime_error("TODO: TypeRef::match_args on PATH");
case TypeRef::ASSOCIATED:
throw ::std::runtime_error("TODO: TypeRef::match_args on ASSOCIATED");
}
}
/// Checks if the type is fully bounded
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 << "_";
if( tr.m_inner_types.size() ) {
os << ": {" << tr.m_inner_types << "}";
}
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 );
})
|