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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* hir/type.hpp
* - HIR Type representation
*/
#ifndef _HIR_TYPE_HPP_
#define _HIR_TYPE_HPP_
#pragma once
#include <tagged_union.hpp>
#include <hir/path.hpp>
#include <hir/expr_ptr.hpp>
#include <span.hpp>
/// Binding index for a Generic that indicates "Self"
#define GENERIC_Self 0xFFFF
constexpr const char* CLOSURE_PATH_PREFIX = "closure#";
namespace HIR {
struct TraitMarkings;
class ExternType;
class Struct;
class Union;
class Enum;
struct ExprNode_Closure;
class TypeRef;
enum class InferClass
{
None,
Diverge,
Integer,
Float,
};
enum class CoreType
{
Usize, Isize,
U8, I8,
U16, I16,
U32, I32,
U64, I64,
U128, I128,
F32, F64,
Bool,
Char, Str,
};
extern ::std::ostream& operator<<(::std::ostream& os, const CoreType& ct);
static inline bool is_integer(const CoreType& v) {
switch(v)
{
case CoreType::Usize: case CoreType::Isize:
case CoreType::U8 : case CoreType::I8:
case CoreType::U16: case CoreType::I16:
case CoreType::U32: case CoreType::I32:
case CoreType::U64: case CoreType::I64:
case CoreType::U128: case CoreType::I128:
return true;
default:
return false;
}
}
static inline bool is_float(const CoreType& v) {
switch(v)
{
case CoreType::F32:
case CoreType::F64:
return true;
default:
return false;
}
}
enum class BorrowType
{
Shared,
Unique,
Owned,
};
extern ::std::ostream& operator<<(::std::ostream& os, const BorrowType& bt);
struct LifetimeRef
{
static const uint32_t UNKNOWN = 0;
static const uint32_t STATIC = 0xFFFF;
//RcString name;
// Values below 2^16 are parameters/static, values above are per-function region IDs allocated during region inferrence.
uint32_t binding = UNKNOWN;
LifetimeRef()
:binding(UNKNOWN)
{
}
LifetimeRef(uint32_t binding)
:binding(binding)
{
}
static LifetimeRef new_static() {
LifetimeRef rv;
rv.binding = STATIC;
return rv;
}
Ordering ord(const LifetimeRef& x) const {
return ::ord(binding, x.binding);
}
bool operator==(const LifetimeRef& x) const {
return binding == x.binding;
}
bool operator!=(const LifetimeRef& x) const {
return !(*this == x);
}
friend ::std::ostream& operator<<(::std::ostream& os, const LifetimeRef& x) {
if( x.binding == UNKNOWN )
{
os << "'_";
}
else if( x.binding == STATIC )
{
os << "'static";
}
else if( x.binding < 0xFFFF )
{
switch( x.binding & 0xFF00 )
{
case 0: os << "'I" << (x.binding & 0xFF); break;
case 1: os << "'M" << (x.binding & 0xFF); break;
default: os << "'unk" << x.binding; break;
}
}
else
{
os << "'_" << (x.binding - 0x1000);
}
return os;
}
};
struct FunctionType
{
bool is_unsafe;
::std::string m_abi;
::std::unique_ptr<TypeRef> m_rettype;
::std::vector<TypeRef> m_arg_types;
};
class TypeRef
{
public:
// Options:
// - Primitive
// - Parameter
// - Path
// - Array
// - Tuple
// - Borrow
// - Pointer
TAGGED_UNION_EX(TypePathBinding, (), Unbound, (
(Unbound, struct {}), // Not yet bound, either during lowering OR during resolution (when associated and still being resolved)
(Opaque, struct {}), // Opaque, i.e. An associated type of a generic (or Self in a trait)
(ExternType, const ::HIR::ExternType*),
(Struct, const ::HIR::Struct*),
(Union, const ::HIR::Union*),
(Enum, const ::HIR::Enum*)
), (), (), (
TypePathBinding clone() const;
const TraitMarkings* get_trait_markings() const;
bool operator==(const TypePathBinding& x) const;
bool operator!=(const TypePathBinding& x) const { return !(*this == x); }
)
);
TAGGED_UNION(Data, Diverge,
(Infer, struct {
unsigned int index;
InferClass ty_class;
/// Returns true if the ivar is a literal
bool is_lit() const {
switch(this->ty_class)
{
case InferClass::None:
case InferClass::Diverge:
return false;
case InferClass::Integer:
case InferClass::Float:
return true;
}
throw "";
}
}),
(Diverge, struct {}),
(Primitive, ::HIR::CoreType),
(Path, struct {
::HIR::Path path;
TypePathBinding binding;
bool is_closure() const {
return path.m_data.is_Generic()
&& path.m_data.as_Generic().m_path.m_components.back().size() > 8
&& path.m_data.as_Generic().m_path.m_components.back().compare(0,strlen(CLOSURE_PATH_PREFIX), CLOSURE_PATH_PREFIX) == 0
;
}
}),
(Generic, struct {
RcString name;
// 0xFFFF = Self, 0-255 = Type/Trait, 256-511 = Method, 512-767 = Placeholder
unsigned int binding;
bool is_placeholder() const {
return (binding >> 8) == 2;
}
}),
(TraitObject, struct {
::HIR::TraitPath m_trait;
::std::vector< ::HIR::GenericPath > m_markers;
::HIR::LifetimeRef m_lifetime;
}),
(ErasedType, struct {
::HIR::Path m_origin;
unsigned int m_index;
::std::vector< ::HIR::TraitPath> m_traits;
::HIR::LifetimeRef m_lifetime;
}),
(Array, struct {
::std::unique_ptr<TypeRef> inner;
::std::shared_ptr<::HIR::ExprPtr> size;
uint64_t size_val;
}),
(Slice, struct {
::std::unique_ptr<TypeRef> inner;
}),
(Tuple, ::std::vector<TypeRef>),
(Borrow, struct {
::HIR::LifetimeRef lifetime;
::HIR::BorrowType type;
::std::unique_ptr<TypeRef> inner;
}),
(Pointer, struct {
::HIR::BorrowType type;
::std::unique_ptr<TypeRef> inner;
}),
(Function, FunctionType),
(Closure, struct {
const ::HIR::ExprNode_Closure* node;
::std::unique_ptr<TypeRef> m_rettype;
::std::vector<TypeRef> m_arg_types;
})
);
Data m_data;
TypeRef():
m_data(Data::make_Infer({ ~0u, InferClass::None }))
{}
TypeRef(TypeRef&& ) = default;
TypeRef(const TypeRef& ) = delete;
TypeRef& operator=(TypeRef&& ) = default;
TypeRef& operator=(const TypeRef&) = delete;
struct TagUnit {};
TypeRef(TagUnit ):
m_data( Data::make_Tuple({}) )
{}
TypeRef(::std::vector< ::HIR::TypeRef> sts):
m_data( Data::make_Tuple(mv$(sts)) )
{}
TypeRef(RcString name, unsigned int slot):
m_data( Data::make_Generic({ mv$(name), slot }) )
{}
TypeRef(::HIR::TypeRef::Data x):
m_data( mv$(x) )
{}
TypeRef(::HIR::CoreType ct):
m_data( Data::make_Primitive(mv$(ct)) )
{}
TypeRef(::HIR::Path p, TypePathBinding pb=TypePathBinding()):
m_data( Data::make_Path( {mv$(p), mv$(pb)} ) )
{}
static TypeRef new_unit() {
return TypeRef(Data::make_Tuple({}));
}
static TypeRef new_diverge() {
return TypeRef(Data::make_Diverge({}));
}
static TypeRef new_infer(unsigned int idx = ~0u, InferClass ty_class = InferClass::None) {
return TypeRef(Data::make_Infer({idx, ty_class}));
}
static TypeRef new_borrow(BorrowType bt, TypeRef inner) {
return TypeRef(Data::make_Borrow({ ::HIR::LifetimeRef(), bt, box$(mv$(inner)) }));
}
static TypeRef new_pointer(BorrowType bt, TypeRef inner) {
return TypeRef(Data::make_Pointer({bt, box$(mv$(inner))}));
}
static TypeRef new_slice(TypeRef inner) {
return TypeRef(Data::make_Slice({box$(mv$(inner))}));
}
static TypeRef new_array(TypeRef inner, unsigned int size) {
assert(size != ~0u);
return TypeRef(Data::make_Array({box$(mv$(inner)), nullptr, size}));
}
static TypeRef new_array(TypeRef inner, ::HIR::ExprPtr size_expr) {
return TypeRef(Data::make_Array({box$(mv$(inner)), ::std::shared_ptr< ::HIR::ExprPtr>( new ::HIR::ExprPtr(mv$(size_expr)) ), ~0u}));
}
static TypeRef new_path(::HIR::Path path, TypePathBinding binding) {
return TypeRef(Data::make_Path({ mv$(path), mv$(binding) }));
}
static TypeRef new_closure(::HIR::ExprNode_Closure* node_ptr, ::std::vector< ::HIR::TypeRef> args, ::HIR::TypeRef rv) {
return TypeRef(Data::make_Closure({ node_ptr, box$(mv$(rv)), mv$(args) }));
}
TypeRef clone() const;
void fmt(::std::ostream& os) const;
bool operator==(const ::HIR::TypeRef& x) const;
bool operator!=(const ::HIR::TypeRef& x) const { return !(*this == x); }
bool operator<(const ::HIR::TypeRef& x) const { return ord(x) == OrdLess; }
Ordering ord(const ::HIR::TypeRef& x) const;
bool contains_generics() const;
// Match generics in `this` with types from `x`
// Raises a bug against `sp` if there is a form mismatch or `this` has an infer
void match_generics(const Span& sp, const ::HIR::TypeRef& x, t_cb_resolve_type resolve_placeholder, t_cb_match_generics) const;
bool match_test_generics(const Span& sp, const ::HIR::TypeRef& x, t_cb_resolve_type resolve_placeholder, t_cb_match_generics) const;
// Compares this type with another, calling the first callback to resolve placeholders in the other type, and the second callback for generics in this type
::HIR::Compare match_test_generics_fuzz(const Span& sp, const ::HIR::TypeRef& x_in, t_cb_resolve_type resolve_placeholder, t_cb_match_generics callback) const;
// Compares this type with another, using `resolve_placeholder` to get replacements for generics/infers in `x`
Compare compare_with_placeholders(const Span& sp, const ::HIR::TypeRef& x, t_cb_resolve_type resolve_placeholder) const;
const ::HIR::SimplePath* get_sort_path() const {
// - Generic paths get sorted
if( TU_TEST1(this->m_data, Path, .path.m_data.is_Generic()) )
{
return &this->m_data.as_Path().path.m_data.as_Generic().m_path;
}
// - So do trait objects
else if( this->m_data.is_TraitObject() )
{
return &this->m_data.as_TraitObject().m_trait.m_path.m_path;
}
else
{
// Keep as nullptr, will search primitive list
return nullptr;
}
}
};
extern ::std::ostream& operator<<(::std::ostream& os, const ::HIR::TypeRef& ty);
} // namespace HIR
#endif
|