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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* hir_typeck/common.cpp
* - Typecheck common code
*/
#include "common.hpp"
#include <hir/path.hpp>
bool visit_ty_with__path_params(const ::HIR::PathParams& tpl, t_cb_visit_ty callback)
{
for(const auto& ty : tpl.m_types)
if( visit_ty_with(ty, callback) )
return true;
return false;
}
bool visit_ty_with__trait_path(const ::HIR::TraitPath& tpl, t_cb_visit_ty callback)
{
if( visit_ty_with__path_params(tpl.m_path.m_params, callback) )
return true;
for(const auto& assoc : tpl.m_type_bounds)
if( visit_ty_with(assoc.second, callback) )
return true;
return false;
}
bool visit_ty_with__path(const ::HIR::Path& tpl, t_cb_visit_ty callback)
{
TU_MATCH(::HIR::Path::Data, (tpl.m_data), (e),
(Generic,
return visit_ty_with__path_params(e.m_params, callback);
),
(UfcsInherent,
return visit_ty_with(*e.type, callback) || visit_ty_with__path_params(e.params, callback);
),
(UfcsKnown,
return visit_ty_with(*e.type, callback) || visit_ty_with__path_params(e.trait.m_params, callback) || visit_ty_with__path_params(e.params, callback);
),
(UfcsUnknown,
return visit_ty_with(*e.type, callback) || visit_ty_with__path_params(e.params, callback);
)
)
throw "";
}
bool visit_ty_with(const ::HIR::TypeRef& ty, t_cb_visit_ty callback)
{
if( callback(ty) ) {
return true;
}
TU_MATCH(::HIR::TypeRef::Data, (ty.m_data), (e),
(Infer,
),
(Diverge,
),
(Primitive,
),
(Generic,
),
(Path,
return visit_ty_with__path(e.path, callback);
),
(TraitObject,
if( visit_ty_with__trait_path(e.m_trait, callback) )
return true;
for(const auto& trait : e.m_markers)
if( visit_ty_with__path_params(trait.m_params, callback) )
return true;
return false;
),
(ErasedType,
if( visit_ty_with__path(e.m_origin, callback) )
return true;
for(const auto& trait : e.m_traits)
if( visit_ty_with__trait_path(trait, callback) )
return true;
return false;
),
(Array,
return visit_ty_with(*e.inner, callback);
),
(Slice,
return visit_ty_with(*e.inner, callback);
),
(Tuple,
for(const auto& ty : e) {
if( visit_ty_with(ty, callback) )
return true;
}
return false;
),
(Borrow,
return visit_ty_with(*e.inner, callback);
),
(Pointer,
return visit_ty_with(*e.inner, callback);
),
(Function,
for(const auto& ty : e.m_arg_types) {
if( visit_ty_with(ty, callback) )
return true;
}
return visit_ty_with(*e.m_rettype, callback);
),
(Closure,
for(const auto& ty : e.m_arg_types) {
if( visit_ty_with(ty, callback) )
return true;
}
return visit_ty_with(*e.m_rettype, callback);
)
)
return false;
}
bool visit_path_tys_with(const ::HIR::Path& path, t_cb_visit_ty callback)
{
return visit_ty_with__path(path, callback);
}
bool monomorphise_pathparams_needed(const ::HIR::PathParams& tpl)
{
return visit_ty_with__path_params(tpl, [&](const auto& ty) {
return (ty.m_data.is_Generic() ? true : false);
});
}
bool monomorphise_traitpath_needed(const ::HIR::TraitPath& tpl)
{
return visit_ty_with__trait_path(tpl, [&](const auto& ty) {
return (ty.m_data.is_Generic() ? true : false);
});
}
bool monomorphise_path_needed(const ::HIR::Path& tpl)
{
return visit_ty_with__path(tpl, [&](const auto& ty) {
return (ty.m_data.is_Generic() ? true : false);
});
}
bool monomorphise_type_needed(const ::HIR::TypeRef& tpl)
{
return visit_ty_with(tpl, [&](const auto& ty) {
return (ty.m_data.is_Generic() ? true : false);
});
}
::HIR::PathParams clone_path_params_with(const Span& sp, const ::HIR::PathParams& tpl, t_cb_clone_ty callback) {
::HIR::PathParams rv;
rv.m_types.reserve( tpl.m_types.size() );
for( const auto& ty : tpl.m_types)
rv.m_types.push_back( clone_ty_with(sp, ty, callback) );
return rv;
}
::HIR::GenericPath clone_ty_with__generic_path(const Span& sp, const ::HIR::GenericPath& tpl, t_cb_clone_ty callback) {
return ::HIR::GenericPath( tpl.m_path, clone_path_params_with(sp, tpl.m_params, callback) );
}
::HIR::TraitPath clone_ty_with__trait_path(const Span& sp, const ::HIR::TraitPath& tpl, t_cb_clone_ty callback) {
::HIR::TraitPath rv {
clone_ty_with__generic_path(sp, tpl.m_path, callback),
tpl.m_hrls,
{},
tpl.m_trait_ptr
};
for(const auto& assoc : tpl.m_type_bounds) {
rv.m_type_bounds.insert(::std::make_pair(
assoc.first,
clone_ty_with(sp, assoc.second, callback)
));
}
return rv;
}
::HIR::Path clone_ty_with__path(const Span& sp, const ::HIR::Path& tpl, t_cb_clone_ty callback) {
TU_MATCH(::HIR::Path::Data, (tpl.m_data), (e2),
(Generic,
return ::HIR::Path( clone_ty_with__generic_path(sp, e2, callback) );
),
(UfcsKnown,
return ::HIR::Path::Data::make_UfcsKnown({
box$( clone_ty_with(sp, *e2.type, callback) ),
clone_ty_with__generic_path(sp, e2.trait, callback),
e2.item,
clone_path_params_with(sp, e2.params, callback)
});
),
(UfcsUnknown,
return ::HIR::Path::Data::make_UfcsUnknown({
box$( clone_ty_with(sp, *e2.type, callback) ),
e2.item,
clone_path_params_with(sp, e2.params, callback)
});
),
(UfcsInherent,
return ::HIR::Path::Data::make_UfcsInherent({
box$( clone_ty_with(sp, *e2.type, callback) ),
e2.item,
clone_path_params_with(sp, e2.params, callback),
clone_path_params_with(sp, e2.impl_params, callback)
});
)
)
throw "";
}
::HIR::TypeRef clone_ty_with(const Span& sp, const ::HIR::TypeRef& tpl, t_cb_clone_ty callback)
{
::HIR::TypeRef rv;
if( callback(tpl, rv) ) {
//DEBUG(tpl << " => " << rv);
return rv;
}
TU_MATCH(::HIR::TypeRef::Data, (tpl.m_data), (e),
(Infer,
rv = ::HIR::TypeRef(e);
),
(Diverge,
rv = ::HIR::TypeRef(e);
),
(Primitive,
rv = ::HIR::TypeRef(e);
),
(Path,
rv = ::HIR::TypeRef( ::HIR::TypeRef::Data::Data_Path {
clone_ty_with__path(sp, e.path, callback),
e.binding.clone()
} );
// If the input binding was Opaque, AND the type changed, clear it back to Unbound
if( e.binding.is_Opaque() /*&& rv != tpl*/ ) {
// NOTE: The replacement can be Self=Self, which should trigger a binding clear.
rv.m_data.as_Path().binding = ::HIR::TypeRef::TypePathBinding();
}
),
(Generic,
rv = ::HIR::TypeRef(e);
),
(TraitObject,
::HIR::TypeRef::Data::Data_TraitObject to;
to.m_trait = clone_ty_with__trait_path(sp, e.m_trait, callback);
for(const auto& trait : e.m_markers)
{
to.m_markers.push_back( clone_ty_with__generic_path(sp, trait, callback) );
}
to.m_lifetime = e.m_lifetime;
rv = ::HIR::TypeRef( mv$(to) );
),
(ErasedType,
auto origin = clone_ty_with__path(sp, e.m_origin, callback);
::std::vector< ::HIR::TraitPath> traits;
traits.reserve( e.m_traits.size() );
for(const auto& trait : e.m_traits)
traits.push_back( clone_ty_with__trait_path(sp, trait, callback) );
rv = ::HIR::TypeRef( ::HIR::TypeRef::Data::Data_ErasedType {
mv$(origin), e.m_index,
mv$(traits),
e.m_lifetime
} );
),
(Array,
if( e.size_val == ~0u ) {
rv = ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Array({ box$(clone_ty_with(sp, *e.inner, callback)), e.size, ~0u }) );
}
else {
rv = ::HIR::TypeRef::new_array( clone_ty_with(sp, *e.inner, callback), e.size_val );
}
),
(Slice,
rv = ::HIR::TypeRef::new_slice( clone_ty_with(sp, *e.inner, callback) );
),
(Tuple,
::std::vector< ::HIR::TypeRef> types;
for(const auto& ty : e) {
types.push_back( clone_ty_with(sp, ty, callback) );
}
rv = ::HIR::TypeRef( mv$(types) );
),
(Borrow,
rv = ::HIR::TypeRef::new_borrow (e.type, clone_ty_with(sp, *e.inner, callback));
),
(Pointer,
rv = ::HIR::TypeRef::new_pointer(e.type, clone_ty_with(sp, *e.inner, callback));
),
(Function,
::HIR::FunctionType ft;
ft.is_unsafe = e.is_unsafe;
ft.m_abi = e.m_abi;
ft.m_rettype = box$( clone_ty_with(sp, *e.m_rettype, callback) );
for( const auto& arg : e.m_arg_types )
ft.m_arg_types.push_back( clone_ty_with(sp, arg, callback) );
rv = ::HIR::TypeRef( mv$(ft) );
),
(Closure,
::HIR::TypeRef::Data::Data_Closure oe;
oe.node = e.node;
oe.m_rettype = box$( clone_ty_with(sp, *e.m_rettype, callback) );
for(const auto& a : e.m_arg_types)
oe.m_arg_types.push_back( clone_ty_with(sp, a, callback) );
rv = ::HIR::TypeRef( mv$(oe) );
)
)
return rv;
}
namespace {
template<typename T>
t_cb_clone_ty monomorphise_type_with__closure(const Span& sp, const T& outer_tpl, t_cb_generic& callback, bool allow_infer)
{
return [&sp,&outer_tpl,callback,allow_infer](const auto& tpl, auto& rv) {
if( tpl.m_data.is_Infer() && !allow_infer )
BUG(sp, "_ type found in " << outer_tpl);
if( tpl.m_data.is_Generic() ) {
rv = callback(tpl).clone();
return true;
}
return false;
};
}
}
::HIR::PathParams monomorphise_path_params_with(const Span& sp, const ::HIR::PathParams& tpl, t_cb_generic callback, bool allow_infer)
{
return clone_path_params_with(sp, tpl, monomorphise_type_with__closure(sp, tpl, callback, allow_infer));
}
::HIR::GenericPath monomorphise_genericpath_with(const Span& sp, const ::HIR::GenericPath& tpl, t_cb_generic callback, bool allow_infer)
{
return clone_ty_with__generic_path(sp, tpl, monomorphise_type_with__closure(sp, tpl, callback, allow_infer));
}
::HIR::TraitPath monomorphise_traitpath_with(const Span& sp, const ::HIR::TraitPath& tpl, t_cb_generic callback, bool allow_infer)
{
return clone_ty_with__trait_path(sp, tpl, monomorphise_type_with__closure(sp, tpl, callback, allow_infer));
}
::HIR::Path monomorphise_path_with(const Span& sp, const ::HIR::Path& tpl, t_cb_generic callback, bool allow_infer)
{
return clone_ty_with__path(sp, tpl, monomorphise_type_with__closure(sp, tpl, callback, allow_infer));
}
::HIR::TypeRef monomorphise_type_with_inner(const Span& sp, const ::HIR::TypeRef& outer_tpl, t_cb_generic callback, bool allow_infer)
{
return clone_ty_with(sp, outer_tpl, monomorphise_type_with__closure(sp, outer_tpl, callback, allow_infer));
}
::HIR::TypeRef monomorphise_type_with(const Span& sp, const ::HIR::TypeRef& tpl, t_cb_generic callback, bool allow_infer)
{
::HIR::TypeRef rv;
TRACE_FUNCTION_FR("tpl = " << tpl, rv);
rv = monomorphise_type_with_inner(sp, tpl, callback, allow_infer);
return rv;
}
// TODO: Rework this function to support all three classes not just impl-level
// NOTE: This is _just_ for impl-level parameters
::HIR::TypeRef monomorphise_type(const Span& sp, const ::HIR::GenericParams& params_def, const ::HIR::PathParams& params, const ::HIR::TypeRef& tpl)
{
DEBUG("tpl = " << tpl);
ASSERT_BUG(sp, params.m_types.size() == params_def.m_types.size(),
"Parameter count mismatch - exp " << params_def.m_types.size() << ", got " << params.m_types.size() << " for " << params << " and " << params_def.fmt_args());
return monomorphise_type_with(sp, tpl, [&](const auto& gt)->const auto& {
const auto& e = gt.m_data.as_Generic();
if( e.binding == 0xFFFF ) {
TODO(sp, "Handle 'Self' in `monomorphise_type`");
}
else if( (e.binding >> 8) == 0 ) {
auto idx = e.binding & 0xFF;
if( idx >= params.m_types.size() ) {
BUG(sp, "Generic param out of input range - " << gt << " >= " << params.m_types.size());
}
return params.m_types[idx];
}
else if( (e.binding >> 8) == 1 ) {
TODO(sp, "Handle fn-level params in `monomorphise_type` - " << gt);
}
else {
BUG(sp, "Unknown param in `monomorphise_type` - " << gt);
}
}, false);
}
t_cb_generic MonomorphState::get_cb(const Span& sp) const
{
return monomorphise_type_get_cb(sp, this->self_ty, this->pp_impl, this->pp_method);
}
::std::ostream& operator<<(::std::ostream& os, const MonomorphState& ms)
{
os << "MonomorphState {";
if(ms.self_ty)
os << " self=" << *ms.self_ty;
if(ms.pp_impl)
os << " I=" << *ms.pp_impl;
if(ms.pp_method)
os << " M=" << *ms.pp_method;
os << " }";
return os;
}
void check_type_class_primitive(const Span& sp, const ::HIR::TypeRef& type, ::HIR::InferClass ic, ::HIR::CoreType ct)
{
switch(ic)
{
case ::HIR::InferClass::None:
case ::HIR::InferClass::Diverge:
break;
case ::HIR::InferClass::Float:
switch(ct)
{
case ::HIR::CoreType::F32:
case ::HIR::CoreType::F64:
break;
default:
ERROR(sp, E0000, "Type unificiation of integer literal with non-integer - " << type);
}
break;
case ::HIR::InferClass::Integer:
switch(ct)
{
case ::HIR::CoreType::I8: case ::HIR::CoreType::U8:
case ::HIR::CoreType::I16: case ::HIR::CoreType::U16:
case ::HIR::CoreType::I32: case ::HIR::CoreType::U32:
case ::HIR::CoreType::I64: case ::HIR::CoreType::U64:
case ::HIR::CoreType::I128: case ::HIR::CoreType::U128:
case ::HIR::CoreType::Isize: case ::HIR::CoreType::Usize:
break;
default:
ERROR(sp, E0000, "Type unificiation of integer literal with non-integer - " << type);
}
break;
}
}
|