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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* hir/path.hpp
* - Item paths (helper code)
*/
#include <hir/path.hpp>
#include <hir/type.hpp>
::HIR::SimplePath HIR::SimplePath::operator+(const RcString& s) const
{
::HIR::SimplePath ret(m_crate_name);
ret.m_components = m_components;
ret.m_components.push_back( s );
return ret;
}
namespace HIR {
::std::ostream& operator<<(::std::ostream& os, const ::HIR::SimplePath& x)
{
if( x.m_crate_name != "" ) {
os << "::\"" << x.m_crate_name << "\"";
}
else if( x.m_components.size() == 0 ) {
os << "::";
}
else {
}
for(const auto& n : x.m_components)
{
os << "::" << n;
}
return os;
}
::std::ostream& operator<<(::std::ostream& os, const PathParams& x)
{
bool has_args = ( x.m_types.size() > 0 );
if(has_args) {
os << "<";
}
for(const auto& ty : x.m_types) {
os << ty << ",";
}
if(has_args) {
os << ">";
}
return os;
}
::std::ostream& operator<<(::std::ostream& os, const GenericPath& x)
{
os << x.m_path << x.m_params;
return os;
}
::std::ostream& operator<<(::std::ostream& os, const TraitPath& x)
{
if( x.m_hrls.size() > 0 ) {
os << "for<";
for(const auto& lft : x.m_hrls)
os << "'" << lft << ",";
os << "> ";
}
os << x.m_path.m_path;
bool has_args = ( x.m_path.m_params.m_types.size() > 0 || x.m_type_bounds.size() > 0 );
if(has_args) {
os << "<";
}
for(const auto& ty : x.m_path.m_params.m_types) {
os << ty << ",";
}
for(const auto& assoc : x.m_type_bounds) {
os << assoc.first << "=" << assoc.second << ",";
}
if(has_args) {
os << ">";
}
return os;
}
::std::ostream& operator<<(::std::ostream& os, const Path& x)
{
TU_MATCH(::HIR::Path::Data, (x.m_data), (e),
(Generic,
return os << e;
),
(UfcsInherent,
return os << "<" << *e.type << " /*- " << e.impl_params << "*/>::" << e.item << e.params;
),
(UfcsKnown,
return os << "<" << *e.type << " as " << e.trait << ">::" << e.item << e.params;
),
(UfcsUnknown,
return os << "<" << *e.type << " as _>::" << e.item << e.params;
)
)
return os;
}
}
::HIR::SimplePath HIR::SimplePath::clone() const
{
return SimplePath( m_crate_name, m_components );
}
::HIR::PathParams::PathParams()
{
}
::HIR::PathParams::PathParams(::HIR::TypeRef ty0)
{
m_types.push_back( mv$(ty0) );
}
::HIR::PathParams HIR::PathParams::clone() const
{
PathParams rv;
for( const auto& t : m_types )
rv.m_types.push_back( t.clone() );
return rv;
}
bool ::HIR::PathParams::operator==(const ::HIR::PathParams& x) const
{
if( m_types.size() != x.m_types.size() )
return false;
for( unsigned int i = 0; i < m_types.size(); i ++ )
if( !(m_types[i] == x.m_types[i]) )
return false;
return true;
}
::HIR::GenericPath::GenericPath()
{
}
::HIR::GenericPath::GenericPath(::HIR::SimplePath sp):
m_path( mv$(sp) )
{
}
::HIR::GenericPath::GenericPath(::HIR::SimplePath sp, ::HIR::PathParams params):
m_path( mv$(sp) ),
m_params( mv$(params) )
{
}
::HIR::GenericPath HIR::GenericPath::clone() const
{
return GenericPath(m_path.clone(), m_params.clone());
}
bool ::HIR::GenericPath::operator==(const GenericPath& x) const
{
if( m_path != x.m_path )
return false;
return m_params == x.m_params;
}
::HIR::TraitPath HIR::TraitPath::clone() const
{
::HIR::TraitPath rv {
m_path.clone(),
m_hrls,
{},
m_trait_ptr
};
for( const auto& assoc : m_type_bounds )
rv.m_type_bounds.insert(::std::make_pair( assoc.first, assoc.second.clone() ));
return rv;
}
bool ::HIR::TraitPath::operator==(const ::HIR::TraitPath& x) const
{
if( m_path != x.m_path )
return false;
if( m_hrls != x.m_hrls )
return false;
if( m_type_bounds.size() != x.m_type_bounds.size() )
return false;
for(auto it_l = m_type_bounds.begin(), it_r = x.m_type_bounds.begin(); it_l != m_type_bounds.end(); it_l++, it_r++ ) {
if( it_l->first != it_r->first )
return false;
if( it_l->second != it_r->second )
return false;
}
return true;
}
::HIR::Path::Path(::HIR::GenericPath gp):
m_data( ::HIR::Path::Data::make_Generic( mv$(gp) ) )
{
}
::HIR::Path::Path(::HIR::SimplePath sp):
m_data( ::HIR::Path::Data::make_Generic(::HIR::GenericPath(mv$(sp))) )
{
}
::HIR::Path::Path(TypeRef ty, RcString item, PathParams item_params):
m_data(Data::make_UfcsInherent({ box$(ty), mv$(item), mv$(item_params) }))
{
}
::HIR::Path::Path(TypeRef ty, GenericPath trait, RcString item, PathParams item_params):
m_data( Data::make_UfcsKnown({ box$(mv$(ty)), mv$(trait), mv$(item), mv$(item_params) }) )
{
}
::HIR::Path HIR::Path::clone() const
{
TU_MATCH(Data, (m_data), (e),
(Generic,
return Path( Data::make_Generic(e.clone()) );
),
(UfcsInherent,
return Path(Data::make_UfcsInherent({
box$( e.type->clone() ),
e.item,
e.params.clone(),
e.impl_params.clone()
}));
),
(UfcsKnown,
return Path(Data::make_UfcsKnown({
box$( e.type->clone() ),
e.trait.clone(),
e.item,
e.params.clone()
}));
),
(UfcsUnknown,
return Path(Data::make_UfcsUnknown({
box$( e.type->clone() ),
e.item,
e.params.clone()
}));
)
)
throw "";
}
::HIR::Compare HIR::PathParams::compare_with_placeholders(const Span& sp, const ::HIR::PathParams& x, ::HIR::t_cb_resolve_type resolve_placeholder) const
{
using ::HIR::Compare;
auto rv = Compare::Equal;
if( this->m_types.size() > 0 || x.m_types.size() > 0 ) {
if( this->m_types.size() != x.m_types.size() ) {
return Compare::Unequal;
}
for( unsigned int i = 0; i < x.m_types.size(); i ++ )
{
auto rv2 = this->m_types[i].compare_with_placeholders( sp, x.m_types[i], resolve_placeholder );
if( rv2 == Compare::Unequal )
return Compare::Unequal;
if( rv2 == Compare::Fuzzy )
rv = Compare::Fuzzy;
}
}
return rv;
}
::HIR::Compare HIR::PathParams::match_test_generics_fuzz(const Span& sp, const PathParams& x, t_cb_resolve_type resolve_placeholder, t_cb_match_generics match) const
{
using ::HIR::Compare;
auto rv = Compare::Equal;
if( this->m_types.size() != x.m_types.size() ) {
return Compare::Unequal;
}
for( unsigned int i = 0; i < x.m_types.size(); i ++ )
{
rv &= this->m_types[i].match_test_generics_fuzz( sp, x.m_types[i], resolve_placeholder, match );
if( rv == Compare::Unequal )
return Compare::Unequal;
}
return rv;
}
::HIR::Compare HIR::GenericPath::compare_with_placeholders(const Span& sp, const ::HIR::GenericPath& x, ::HIR::t_cb_resolve_type resolve_placeholder) const
{
using ::HIR::Compare;
if( this->m_path.m_crate_name != x.m_path.m_crate_name )
return Compare::Unequal;
if( this->m_path.m_components.size() != x.m_path.m_components.size() )
return Compare::Unequal;
for(unsigned int i = 0; i < this->m_path.m_components.size(); i ++ )
{
if( this->m_path.m_components[i] != x.m_path.m_components[i] )
return Compare::Unequal;
}
return this->m_params. compare_with_placeholders(sp, x.m_params, resolve_placeholder);
}
namespace {
::HIR::Compare compare_with_placeholders(
const Span& sp,
const ::HIR::PathParams& l, const ::HIR::PathParams& r,
::HIR::t_cb_resolve_type resolve_placeholder
)
{
return l.compare_with_placeholders(sp, r, resolve_placeholder);
}
::HIR::Compare compare_with_placeholders(
const Span& sp,
const ::HIR::GenericPath& l, const ::HIR::GenericPath& r,
::HIR::t_cb_resolve_type resolve_placeholder
)
{
return l.compare_with_placeholders(sp, r, resolve_placeholder);
}
}
#define CMP(rv, cmp) do { \
switch(cmp) {\
case ::HIR::Compare::Unequal: return ::HIR::Compare::Unequal; \
case ::HIR::Compare::Fuzzy: rv = ::HIR::Compare::Fuzzy; break; \
case ::HIR::Compare::Equal: break; \
}\
} while(0)
::HIR::Compare HIR::TraitPath::compare_with_placeholders(const Span& sp, const TraitPath& x, t_cb_resolve_type resolve_placeholder) const
{
auto rv = m_path .compare_with_placeholders(sp, x.m_path, resolve_placeholder);
if( rv == Compare::Unequal )
return rv;
// TODO: HRLs
auto it_l = m_type_bounds.begin();
auto it_r = x.m_type_bounds.begin();
while( it_l != m_type_bounds.end() && it_r != x.m_type_bounds.end() )
{
if( it_l->first != it_r->first ) {
return Compare::Unequal;
}
CMP( rv, it_l->second .compare_with_placeholders( sp, it_r->second, resolve_placeholder ) );
++ it_l;
++ it_r;
}
if( it_l != m_type_bounds.end() || it_r != x.m_type_bounds.end() )
{
return Compare::Unequal;
}
return rv;
}
::HIR::Compare HIR::Path::compare_with_placeholders(const Span& sp, const Path& x, t_cb_resolve_type resolve_placeholder) const
{
if( this->m_data.tag() != x.m_data.tag() )
return Compare::Unequal;
TU_MATCH(::HIR::Path::Data, (this->m_data, x.m_data), (ple, pre),
(Generic,
return ::compare_with_placeholders(sp, ple, pre, resolve_placeholder);
),
(UfcsUnknown,
if( ple.item != pre.item)
return Compare::Unequal;
TODO(sp, "Path::compare_with_placeholders - UfcsUnknown");
),
(UfcsInherent,
if( ple.item != pre.item)
return Compare::Unequal;
::HIR::Compare rv = ::HIR::Compare::Equal;
CMP(rv, ple.type->compare_with_placeholders(sp, *pre.type, resolve_placeholder));
CMP(rv, ::compare_with_placeholders(sp, ple.params, pre.params, resolve_placeholder));
return rv;
),
(UfcsKnown,
if( ple.item != pre.item)
return Compare::Unequal;
::HIR::Compare rv = ::HIR::Compare::Equal;
CMP(rv, ple.type->compare_with_placeholders(sp, *pre.type, resolve_placeholder));
CMP(rv, ::compare_with_placeholders(sp, ple.trait, pre.trait, resolve_placeholder));
CMP(rv, ::compare_with_placeholders(sp, ple.params, pre.params, resolve_placeholder));
return rv;
)
)
throw "";
}
Ordering HIR::Path::ord(const ::HIR::Path& x) const
{
ORD( (unsigned)m_data.tag(), (unsigned)x.m_data.tag() );
TU_MATCH(::HIR::Path::Data, (this->m_data, x.m_data), (tpe, xpe),
(Generic,
return ::ord(tpe, xpe);
),
(UfcsInherent,
ORD(*tpe.type, *xpe.type);
ORD(tpe.item, xpe.item);
return ::ord(tpe.params, xpe.params);
),
(UfcsKnown,
ORD(*tpe.type, *xpe.type);
ORD(tpe.trait, xpe.trait);
ORD(tpe.item, xpe.item);
return ::ord(tpe.params, xpe.params);
),
(UfcsUnknown,
ORD(*tpe.type, *xpe.type);
ORD(tpe.item, xpe.item);
return ::ord(tpe.params, xpe.params);
)
)
throw "";
}
bool ::HIR::Path::operator==(const Path& x) const {
return this->ord(x) == ::OrdEqual;
}
|