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
|
/*
* MRustC - Mutabah's Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* ast/types.hpp
* - AST Type reference (and helpers)
*/
#ifndef TYPES_HPP_INCLUDED
#define TYPES_HPP_INCLUDED
#include <memory>
#include "../common.hpp"
#include "coretypes.hpp"
#include "ast/path.hpp"
#include "ast/macro.hpp"
#include <tagged_union.hpp>
namespace AST {
class ExprNode;
class Expr;
class LifetimeParam;
}
namespace AST {
// Defined here for dependency reasons
class HigherRankedBounds
{
public:
::std::vector<LifetimeParam> m_lifetimes;
//::std::vector<TypeParam> m_types;
//::std::vector<GenericBound> m_bounds;
bool empty() const {
return m_lifetimes.empty();
}
friend ::std::ostream& operator<<(::std::ostream& os, const HigherRankedBounds& x);
};
class LifetimeRef
{
public:
static const uint16_t BINDING_STATIC = 0xFFFF;
static const uint16_t BINDING_UNBOUND = 0xFFFE;
static const uint16_t BINDING_INFER = 0xFFFD;
private:
Ident m_name;
uint16_t m_binding;
LifetimeRef(Ident name, uint32_t binding):
m_name( ::std::move(name) ),
m_binding( binding )
{
}
public:
LifetimeRef():
LifetimeRef("", BINDING_INFER)
{
}
LifetimeRef(Ident name):
LifetimeRef(::std::move(name), BINDING_UNBOUND)
{
}
static LifetimeRef new_static() {
return LifetimeRef("static", BINDING_STATIC);
}
void set_binding(uint16_t b) { assert(m_binding == BINDING_UNBOUND); m_binding = b; }
bool is_unbound() const { return m_binding == BINDING_UNBOUND; }
bool is_infer() const { return m_binding == BINDING_INFER; }
const Ident& name() const { return m_name; }
Ordering ord(const LifetimeRef& x) const { return ::ord(m_name.name, x.m_name.name); }
bool operator==(const LifetimeRef& x) const { return ord(x) == OrdEqual; }
bool operator!=(const LifetimeRef& x) const { return ord(x) != OrdEqual; }
bool operator<(const LifetimeRef& x) const { return ord(x) == OrdLess; };
friend ::std::ostream& operator<<(::std::ostream& os, const LifetimeRef& x);
};
}
class PrettyPrintType
{
const TypeRef& m_type;
public:
PrettyPrintType(const TypeRef& ty):
m_type(ty)
{}
void print(::std::ostream& os) const;
friend ::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& v);
};
struct TypeArgRef
{
::std::string name;
unsigned int level;
const AST::GenericParams* params;
};
struct Type_Function
{
AST::HigherRankedBounds hrbs;
bool is_unsafe;
::std::string m_abi;
::std::unique_ptr<TypeRef> m_rettype;
::std::vector<TypeRef> m_arg_types;
bool is_variadic;
Type_Function() {}
Type_Function(AST::HigherRankedBounds hrbs, bool is_unsafe, ::std::string abi, ::std::unique_ptr<TypeRef> ret, ::std::vector<TypeRef> args, bool is_variadic):
hrbs(mv$(hrbs)),
is_unsafe(is_unsafe),
m_abi(mv$(abi)),
m_rettype(mv$(ret)),
m_arg_types(mv$(args)),
is_variadic(is_variadic)
{}
Type_Function(Type_Function&& other) = default;
Type_Function(const Type_Function& other);
Ordering ord(const Type_Function& x) const;
};
struct Type_TraitPath
{
AST::HigherRankedBounds hrbs;
AST::Path path;
Ordering ord(const Type_TraitPath& x) const;
};
TAGGED_UNION(TypeData, None,
(None, struct { }),
(Any, struct { }),
(Bang, struct { }),
(Unit, struct { }),
(Macro, struct {
::AST::MacroInvocation inv;
}),
(Primitive, struct {
enum eCoreType core_type;
}),
(Function, struct {
Type_Function info;
}),
(Tuple, struct {
::std::vector<TypeRef> inner_types;
}),
(Borrow, struct {
AST::LifetimeRef lifetime;
bool is_mut;
::std::unique_ptr<TypeRef> inner;
}),
(Pointer, struct {
bool is_mut;
::std::unique_ptr<TypeRef> inner;
}),
(Array, struct {
::std::unique_ptr<TypeRef> inner;
::std::shared_ptr<AST::ExprNode> size;
}),
(Generic, struct {
::std::string name;
unsigned int index;
}),
(Path, struct {
AST::Path path;
}),
(TraitObject, struct {
::std::vector<Type_TraitPath> traits;
::std::vector<AST::LifetimeRef> lifetimes;
}),
(ErasedType, struct {
::std::vector<Type_TraitPath> traits;
::std::vector<AST::LifetimeRef> lifetimes;
})
);
/// A type
class TypeRef
{
Span m_span;
public:
TypeData m_data;
~TypeRef();
TypeRef(TypeRef&& other) = default;
TypeRef& operator=(TypeRef&& other) = default;
#if 1
TypeRef(const TypeRef& other) = delete;
TypeRef& operator=(const TypeRef& other) = delete;
#else
TypeRef(const TypeRef& other): m_span(other.m_span) {
*this = other.clone();
}
TypeRef& operator=(const TypeRef& other) {
m_data = mv$(other.clone().m_data);
return *this;
}
#endif
TypeRef(Span sp):
m_span( mv$(sp) ),
m_data( TypeData::make_Any({}) )
{}
TypeRef(Span sp, TypeData data):
m_span( mv$(sp) ),
m_data( mv$(data) )
{}
struct TagInvalid {};
TypeRef(TagInvalid, Span sp):
m_span(mv$(sp)),
m_data(TypeData::make_None({}))
{}
struct TagMacro {};
TypeRef(TagMacro, ::AST::MacroInvocation inv):
m_span(inv.span()),
m_data(TypeData::make_Macro({mv$(inv)}))
{}
struct TagUnit {}; // unit maps to a zero-length tuple, just easier to type
TypeRef(TagUnit, Span sp):
m_span(mv$(sp)),
m_data(TypeData::make_Unit({}))
{}
struct TagPrimitive {};
TypeRef(TagPrimitive, Span sp, enum eCoreType type):
m_span(mv$(sp)),
m_data(TypeData::make_Primitive({type}))
{}
TypeRef(Span sp, enum eCoreType type):
m_span(mv$(sp)),
m_data(TypeData::make_Primitive({type}))
{}
struct TagTuple {};
TypeRef(TagTuple , Span sp, ::std::vector<TypeRef> inner_types):
m_span(mv$(sp)),
m_data(TypeData::make_Tuple({::std::move(inner_types)}))
{}
struct TagFunction {};
TypeRef(TagFunction, Span sp, AST::HigherRankedBounds hrbs, bool is_unsafe, ::std::string abi, ::std::vector<TypeRef> args, bool is_variadic, TypeRef ret):
m_span(mv$(sp)),
m_data(TypeData::make_Function({ Type_Function( mv$(hrbs), is_unsafe, abi, box$(ret), mv$(args), is_variadic ) }))
{}
struct TagReference {};
TypeRef(TagReference , Span sp, AST::LifetimeRef lft, bool is_mut, TypeRef inner_type):
m_span(mv$(sp)),
m_data(TypeData::make_Borrow({ ::std::move(lft), is_mut, ::make_unique_ptr(mv$(inner_type)) }))
{}
struct TagPointer {};
TypeRef(TagPointer , Span sp, bool is_mut, TypeRef inner_type):
m_span(mv$(sp)),
m_data(TypeData::make_Pointer({ is_mut, ::make_unique_ptr(mv$(inner_type)) }))
{}
struct TagSizedArray {};
TypeRef(TagSizedArray , Span sp, TypeRef inner_type, ::std::shared_ptr<AST::ExprNode> size):
m_span(mv$(sp)),
m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), mv$(size) }))
{}
struct TagUnsizedArray {};
TypeRef(TagUnsizedArray , Span sp, TypeRef inner_type):
m_span(mv$(sp)),
m_data(TypeData::make_Array({ ::make_unique_ptr(mv$(inner_type)), ::std::shared_ptr<AST::ExprNode>() }))
{}
struct TagArg {};
TypeRef(TagArg, Span sp, ::std::string name, unsigned int binding = ~0u):
m_span( mv$(sp) ),
m_data(TypeData::make_Generic({ name, binding }))
{}
TypeRef(Span sp, ::std::string name, unsigned int binding = ~0u):
TypeRef(TagArg(), mv$(sp), mv$(name), binding)
{}
struct TagPath {};
TypeRef(TagPath, Span sp, AST::Path path):
m_span(mv$(sp)),
m_data(TypeData::make_Path({ ::std::move(path) }))
{}
TypeRef(Span sp, AST::Path path):
TypeRef(TagPath(), mv$(sp), mv$(path))
{}
TypeRef( Span sp, ::std::vector<Type_TraitPath> traits, ::std::vector<AST::LifetimeRef> lifetimes ):
m_span(mv$(sp)),
m_data(TypeData::make_TraitObject({ ::std::move(traits), mv$(lifetimes) }))
{}
const Span& span() const { return m_span; }
bool is_valid() const { return ! m_data.is_None(); }
bool is_unbounded() const { return m_data.is_Any(); }
bool is_wildcard() const { return m_data.is_Any(); }
bool is_unit() const { return m_data.is_Unit(); }
bool is_primitive() const { return m_data.is_Primitive(); }
bool is_path() const { return m_data.is_Path(); }
const AST::Path& path() const { return m_data.as_Path().path; }
AST::Path& path() { return m_data.as_Path().path; }
bool is_type_param() const { return m_data.is_Generic(); }
const ::std::string& type_param() const { return m_data.as_Generic().name; }
bool is_reference() const { return m_data.is_Borrow(); }
bool is_pointer() const { return m_data.is_Pointer(); }
bool is_tuple() const { return m_data.is_Tuple(); }
TypeRef clone() const;
const TypeRef& inner_type() const {
TU_MATCH_DEF(TypeData, (m_data), (e),
( throw ::std::runtime_error("Called inner_type on non-wrapper"); ),
(Borrow, return *e.inner; ),
(Pointer, return *e.inner; ),
(Array, return *e.inner; )
)
}
TypeRef& inner_type() {
TU_MATCH_DEF(TypeData, (m_data), (e),
( throw ::std::runtime_error("Called inner_type on non-wrapper"); ),
(Borrow, return *e.inner; ),
(Pointer, return *e.inner; ),
(Array, return *e.inner; )
)
}
Ordering ord(const TypeRef& x) const;
bool operator==(const TypeRef& x) const { return ord(x) == OrdEqual; }
bool operator!=(const TypeRef& x) const { return ord(x) != OrdEqual; }
bool operator<(const TypeRef& x) const { return ord(x) == OrdLess; };
void print(::std::ostream& os, bool is_debug=false) const;
PrettyPrintType print_pretty() const { return PrettyPrintType(*this); }
friend class PrettyPrintType;
friend ::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr);
};
#endif // TYPES_HPP_INCLUDED
|