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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* hir/hir.cpp
* - Processed module tree (High-level Intermediate Representation)
*
* HIR type helper code
*/
#include "hir.hpp"
#include <algorithm>
#include <hir_typeck/common.hpp>
namespace HIR {
::std::ostream& operator<<(::std::ostream& os, const ::HIR::Literal& v)
{
TU_MATCH(::HIR::Literal, (v), (e),
(Invalid,
os << "!";
),
(List,
os << "[";
for(const auto& val : e)
os << " " << val << ",";
os << " ]";
),
(Variant,
os << "#" << e.idx << ":[";
for(const auto& val : e.vals)
os << " " << val << ",";
os << " ]";
),
(Integer,
os << e;
),
(Float,
os << e;
),
(BorrowOf,
os << "&" << e;
),
(String,
os << "\"" << e << "\"";
)
)
return os;
}
bool operator==(const Literal& l, const Literal& r)
{
if( l.tag() != r.tag() )
return false;
TU_MATCH(::HIR::Literal, (l,r), (le,re),
(Invalid,
),
(List,
if( le.size() != re.size() )
return false;
for(unsigned int i = 0; i < le.size(); i ++)
if( le[i] != re[i] )
return false;
),
(Variant,
if( le.idx != re.idx )
return false;
if( le.vals.size() != re.vals.size() )
return false;
for(unsigned int i = 0; i < le.vals.size(); i ++)
if( le.vals[i] != re.vals[i] )
return false;
),
(Integer,
return le == re;
),
(Float,
return le == re;
),
(BorrowOf,
return le == re;
),
(String,
return le == re;
)
)
return true;
}
}
const ::HIR::Enum::Variant* ::HIR::Enum::get_variant(const ::std::string& name) const
{
auto it = ::std::find_if(m_variants.begin(), m_variants.end(), [&](const auto& x){ return x.first == name; });
if( it == m_variants.end() )
return nullptr;
return &it->second;
}
bool HIR::Enum::is_value() const
{
return this->m_repr != ::HIR::Enum::Repr::Rust || ::std::all_of(m_variants.begin(), m_variants.end(), [](const auto& x){return x.second.is_Unit() || x.second.is_Value();});
}
uint32_t HIR::Enum::get_value(size_t idx) const
{
assert(idx < m_variants.size());
if( const auto* e = m_variants[idx].second.opt_Value() )
{
return e->val.as_Integer();
}
uint32_t val = 0;
for(size_t i = 0; i < idx; i ++)
{
if( const auto* e = m_variants[i].second.opt_Value() )
{
val = e->val.as_Integer();
}
val ++;
}
return val;
}
namespace {
bool matches_genericpath(const ::HIR::GenericParams& params, const ::HIR::GenericPath& left, const ::HIR::GenericPath& right, ::HIR::t_cb_resolve_type ty_res, bool expand_generic);
bool matches_type_int(const ::HIR::GenericParams& params, const ::HIR::TypeRef& left, const ::HIR::TypeRef& right_in, ::HIR::t_cb_resolve_type ty_res, bool expand_generic)
{
assert(! left.m_data.is_Infer() );
const auto& right = (right_in.m_data.is_Infer() || (right_in.m_data.is_Generic() && expand_generic) ? ty_res(right_in) : right_in);
if( right_in.m_data.is_Generic() )
expand_generic = false;
//DEBUG("left = " << left << ", right = " << right);
// TODO: What indicates what out of ty_res?
if( right.m_data.is_Infer() ) {
//DEBUG("left = " << left << ", right = " << right);
switch(right.m_data.as_Infer().ty_class)
{
case ::HIR::InferClass::None:
case ::HIR::InferClass::Diverge:
//return left.m_data.is_Generic();
return true;
case ::HIR::InferClass::Integer:
TU_IFLET(::HIR::TypeRef::Data, left.m_data, Primitive, le,
return is_integer(le);
)
else {
return left.m_data.is_Generic();
}
break;
case ::HIR::InferClass::Float:
TU_IFLET(::HIR::TypeRef::Data, left.m_data, Primitive, le,
return is_float(le);
)
else {
return left.m_data.is_Generic();
}
break;
}
throw "";
}
// A local generic could match anything, leave that up to the caller
if( left.m_data.is_Generic() ) {
return true;
}
// A local UfcsKnown can only be becuase it couldn't be expanded earlier, assume it could match
if( left.m_data.is_Path() && left.m_data.as_Path().path.m_data.is_UfcsKnown() ) {
// True?
return true;
}
// If the RHS (provided) is generic, it can only match if it binds to a local type parameter
if( right.m_data.is_Generic() ) {
return left.m_data.is_Generic();
}
if( left.m_data.tag() != right.m_data.tag() ) {
return false;
}
TU_MATCH(::HIR::TypeRef::Data, (left.m_data, right.m_data), (le, re),
(Infer, assert(!"infer");),
(Diverge, return true; ),
(Primitive, return le == re;),
(Path,
if( le.path.m_data.tag() != re.path.m_data.tag() )
return false;
TU_MATCH_DEF(::HIR::Path::Data, (le.path.m_data, re.path.m_data), (ple, pre),
(
return false;
),
(Generic,
return matches_genericpath(params, ple, pre, ty_res, expand_generic);
)
)
),
(Generic,
throw "";
),
(TraitObject,
if( !matches_genericpath(params, le.m_trait.m_path, re.m_trait.m_path, ty_res, expand_generic) )
return false;
if( le.m_markers.size() != re.m_markers.size() )
return false;
for(unsigned int i = 0; i < le.m_markers.size(); i ++)
{
const auto& lm = le.m_markers[i];
const auto& rm = re.m_markers[i];
if( !matches_genericpath(params, lm, rm, ty_res, expand_generic) )
return false;
}
return true;
),
(ErasedType,
throw "Unexpected ErasedType in matches_type_int";
),
(Array,
if( ! matches_type_int(params, *le.inner, *re.inner, ty_res, expand_generic) )
return false;
if( le.size_val != re.size_val )
return false;
return true;
),
(Slice,
return matches_type_int(params, *le.inner, *re.inner, ty_res, expand_generic);
),
(Tuple,
if( le.size() != re.size() )
return false;
for( unsigned int i = 0; i < le.size(); i ++ )
if( !matches_type_int(params, le[i], re[i], ty_res, expand_generic) )
return false;
return true;
),
(Borrow,
if( le.type != re.type )
return false;
return matches_type_int(params, *le.inner, *re.inner, ty_res, expand_generic);
),
(Pointer,
if( le.type != re.type )
return false;
return matches_type_int(params, *le.inner, *re.inner, ty_res, expand_generic);
),
(Function,
if( le.is_unsafe != re.is_unsafe )
return false;
if( le.m_abi != re.m_abi )
return false;
if( le.m_arg_types.size() != re.m_arg_types.size() )
return false;
for( unsigned int i = 0; i < le.m_arg_types.size(); i ++ )
if( !matches_type_int(params, le.m_arg_types[i], re.m_arg_types[i], ty_res, expand_generic) )
return false;
return matches_type_int(params, *le.m_rettype, *re.m_rettype, ty_res, expand_generic);
),
(Closure,
return le.node == re.node;
)
)
return false;
}
bool matches_genericpath(const ::HIR::GenericParams& params, const ::HIR::GenericPath& left, const ::HIR::GenericPath& right, ::HIR::t_cb_resolve_type ty_res, bool expand_generic)
{
if( left.m_path.m_crate_name != right.m_path.m_crate_name )
return false;
if( left.m_path.m_components.size() != right.m_path.m_components.size() )
return false;
for(unsigned int i = 0; i < left.m_path.m_components.size(); i ++ )
{
if( left.m_path.m_components[i] != right.m_path.m_components[i] )
return false;
}
if( left.m_params.m_types.size() > 0 || right.m_params.m_types.size() > 0 ) {
if( left.m_params.m_types.size() != right.m_params.m_types.size() ) {
return true;
//TODO(Span(), "Match generic paths " << left << " and " << right << " - count mismatch");
}
for( unsigned int i = 0; i < right.m_params.m_types.size(); i ++ )
{
if( ! matches_type_int(params, left.m_params.m_types[i], right.m_params.m_types[i], ty_res, expand_generic) )
return false;
}
}
return true;
}
}
//::HIR::TypeRef HIR::Function::make_ty(const Span& sp, const ::HIR::PathParams& params) const
//{
// // TODO: Obtain function type for this function (i.e. a type that is specifically for this function)
// auto fcn_ty_data = ::HIR::FunctionType {
// m_is_unsafe,
// m_abi,
// box$( monomorphise_type(sp, m_params, params, m_return) ),
// {}
// };
// fcn_ty_data.m_arg_types.reserve( m_args.size() );
// for(const auto& arg : m_args)
// {
// fcn_ty_data.m_arg_types.push_back( monomorphise_type(sp, m_params, params, arg.second) );
// }
// return ::HIR::TypeRef( mv$(fcn_ty_data) );
//}
namespace {
bool is_unbounded_infer(const ::HIR::TypeRef& type) {
TU_IFLET( ::HIR::TypeRef::Data, type.m_data, Infer, e,
return e.ty_class == ::HIR::InferClass::None || e.ty_class == ::HIR::InferClass::Diverge;
)
else {
return false;
}
}
}
bool ::HIR::TraitImpl::matches_type(const ::HIR::TypeRef& type, ::HIR::t_cb_resolve_type ty_res) const
{
// NOTE: Don't return any impls when the type is an unbouned ivar. Wouldn't be able to pick anything anyway
if( is_unbounded_infer(type) ) {
return false;
}
return matches_type_int(m_params, m_type, type, ty_res, true);
}
bool ::HIR::TypeImpl::matches_type(const ::HIR::TypeRef& type, ::HIR::t_cb_resolve_type ty_res) const
{
if( is_unbounded_infer(type) ) {
return false;
}
return matches_type_int(m_params, m_type, type, ty_res, true);
}
bool ::HIR::MarkerImpl::matches_type(const ::HIR::TypeRef& type, ::HIR::t_cb_resolve_type ty_res) const
{
if( is_unbounded_infer(type) ) {
return false;
}
return matches_type_int(m_params, m_type, type, ty_res, true);
}
namespace {
struct TypeOrdSpecific_MixedOrdering
{
};
::Ordering typelist_ord_specific(const Span& sp, const ::std::vector<::HIR::TypeRef>& left, const ::std::vector<::HIR::TypeRef>& right);
::Ordering type_ord_specific(const Span& sp, const ::HIR::TypeRef& left, const ::HIR::TypeRef& right)
{
// TODO: What happens if you get `impl<T> Foo<T> for T` vs `impl<T,U> Foo<U> for T`
// A generic can't be more specific than any other type we can see
// - It's equally as specific as another Generic, so still false
if( left.m_data.is_Generic() ) {
return right.m_data.is_Generic() ? ::OrdEqual : ::OrdLess;
}
// - A generic is always less specific than anything but itself (handled above)
if( right.m_data.is_Generic() ) {
return ::OrdGreater;
}
TU_MATCH(::HIR::TypeRef::Data, (left.m_data), (le),
(Generic,
throw "";
),
(Infer,
BUG(sp, "Hit infer");
),
(Diverge,
BUG(sp, "Hit diverge");
),
(Closure,
BUG(sp, "Hit closure");
),
(Primitive,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Primitive, re,
if( le != re )
BUG(sp, "Mismatched types - " << left << " and " << right);
return ::OrdEqual;
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Path,
if( !right.m_data.is_Path() || le.path.m_data.tag() != right.m_data.as_Path().path.m_data.tag() )
BUG(sp, "Mismatched types - " << left << " and " << right);
TU_MATCHA( (le.path.m_data, right.m_data.as_Path().path.m_data), (lpe, rpe),
(Generic,
if( lpe.m_path != rpe.m_path )
BUG(sp, "Mismatched types - " << left << " and " << right);
return typelist_ord_specific(sp, lpe.m_params.m_types, rpe.m_params.m_types);
),
(UfcsUnknown,
),
(UfcsKnown,
),
(UfcsInherent,
)
)
TODO(sp, "Path - " << le.path << " and " << right);
),
(TraitObject,
ASSERT_BUG(sp, right.m_data.is_TraitObject(), "Mismatched types - "<< left << " vs " << right);
const auto& re = right.m_data.as_TraitObject();
ASSERT_BUG(sp, le.m_trait.m_path.m_path == re.m_trait.m_path.m_path, "Mismatched types - "<< left << " vs " << right);
ASSERT_BUG(sp, le.m_markers.size() == re.m_markers.size(), "Mismatched types - "<< left << " vs " << right);
auto ord = typelist_ord_specific(sp, le.m_trait.m_path.m_params.m_types, re.m_trait.m_path.m_params.m_types);
if( ord != ::OrdEqual )
return ord;
for(size_t i = 0; i < le.m_markers.size(); i ++)
{
ASSERT_BUG(sp, le.m_markers[i].m_path == re.m_markers[i].m_path, "Mismatched types - " << left << " vs " << right);
ord = typelist_ord_specific(sp, le.m_markers[i].m_params.m_types, re.m_markers[i].m_params.m_types);
if(ord != ::OrdEqual)
return ord;
}
return ::OrdEqual;
),
(ErasedType,
TODO(sp, "ErasedType - " << left);
),
(Function,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Function, re,
TODO(sp, "Function");
//return typelist_ord_specific(sp, le.arg_types, re.arg_types);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Tuple,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Tuple, re,
return typelist_ord_specific(sp, le, re);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Slice,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Slice, re,
return type_ord_specific(sp, *le.inner, *re.inner);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Array,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Array, re,
if( le.size_val != re.size_val )
BUG(sp, "Mismatched types - " << left << " and " << right);
return type_ord_specific(sp, *le.inner, *re.inner);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Pointer,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Pointer, re,
if( le.type != re.type )
BUG(sp, "Mismatched types - " << left << " and " << right);
return type_ord_specific(sp, *le.inner, *re.inner);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
),
(Borrow,
TU_IFLET(::HIR::TypeRef::Data, right.m_data, Borrow, re,
if( le.type != re.type )
BUG(sp, "Mismatched types - " << left << " and " << right);
return type_ord_specific(sp, *le.inner, *re.inner);
)
else {
BUG(sp, "Mismatched types - " << left << " and " << right);
}
)
)
throw "Fell off end of type_ord_specific";
}
::Ordering typelist_ord_specific(const Span& sp, const ::std::vector<::HIR::TypeRef>& le, const ::std::vector<::HIR::TypeRef>& re)
{
auto rv = ::OrdEqual;
assert(le.size() == re.size());
for(unsigned int i = 0; i < le.size(); i ++) {
auto a = type_ord_specific(sp, le[i], re[i]);
if( a != ::OrdEqual ) {
if( rv != ::OrdEqual && a != rv )
{
DEBUG("Inconsistent ordering between type lists - i=" << i << " [" << le << "] vs [" << re << "]");
throw TypeOrdSpecific_MixedOrdering {};
}
rv = a;
}
}
return rv;
}
}
namespace {
void add_bound_from_trait(::std::vector< ::HIR::GenericBound>& rv, const ::HIR::TypeRef& type, const ::HIR::TraitPath& cur_trait)
{
static Span sp;
assert( cur_trait.m_trait_ptr );
const auto& tr = *cur_trait.m_trait_ptr;
auto monomorph_cb = monomorphise_type_get_cb(sp, &type, &cur_trait.m_path.m_params, nullptr);
for(const auto& trait_path_raw : tr.m_all_parent_traits)
{
// 1. Monomorph
auto trait_path_mono = monomorphise_traitpath_with(sp, trait_path_raw, monomorph_cb, false);
// 2. Add
rv.push_back( ::HIR::GenericBound::make_TraitBound({ type.clone(), mv$(trait_path_mono) }) );
}
// TODO: Add traits from `Self: Foo` bounds?
// TODO: Move associated types to the source trait.
}
::std::vector< ::HIR::GenericBound> flatten_bounds(const ::std::vector<::HIR::GenericBound>& bounds)
{
::std::vector< ::HIR::GenericBound > rv;
for(const auto& b : bounds)
{
TU_MATCHA( (b), (be),
(Lifetime,
rv.push_back( ::HIR::GenericBound(be) );
),
(TypeLifetime,
rv.push_back( ::HIR::GenericBound::make_TypeLifetime({ be.type.clone(), be.valid_for }) );
),
(TraitBound,
rv.push_back( ::HIR::GenericBound::make_TraitBound({ be.type.clone(), be.trait.clone() }) );
add_bound_from_trait(rv, be.type, be.trait);
),
(TypeEquality,
rv.push_back( ::HIR::GenericBound::make_TypeEquality({ be.type.clone(), be.other_type.clone() }) );
)
)
}
::std::sort(rv.begin(), rv.end(), [](const auto& a, const auto& b){ return ::ord(a,b) == OrdLess; });
return rv;
}
}
bool ::HIR::TraitImpl::more_specific_than(const ::HIR::TraitImpl& other) const
{
static const Span _sp;
const Span& sp = _sp;
TRACE_FUNCTION;
//DEBUG("this = " << *this);
//DEBUG("other = " << other);
// >> https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md#defining-the-precedence-rules
// 1. If this->m_type is less specific than other.m_type: return false
try
{
auto ord = type_ord_specific(sp, this->m_type, other.m_type);
if( ord != ::OrdEqual ) {
DEBUG("- Type " << (ord == ::OrdLess ? "less" : "more") << " specific - " << this->m_type << " AND " << other.m_type);
return ord == ::OrdLess;
}
// 2. If any in te.impl->m_params is less specific than oe.impl->m_params: return false
ord = typelist_ord_specific(sp, this->m_trait_args.m_types, other.m_trait_args.m_types);
if( ord != ::OrdEqual ) {
DEBUG("- Trait arguments " << (ord == ::OrdLess ? "less" : "more") << " specific");
return ord == ::OrdLess;
}
}
catch(const TypeOrdSpecific_MixedOrdering& e)
{
BUG(sp, "Mixed ordering in more_specific_than");
}
//if( other.m_params.m_bounds.size() == 0 ) {
// DEBUG("- Params (none in other, some in this)");
// return m_params.m_bounds.size() > 0;
//}
// 3. Compare bound set, if there is a rule in oe that is missing from te; return false
// TODO: Cache these lists (calculate after outer typecheck?)
auto bounds_t = flatten_bounds(m_params.m_bounds);
auto bounds_o = flatten_bounds(other.m_params.m_bounds);
DEBUG("bounds_t = " << bounds_t);
DEBUG("bounds_o = " << bounds_o);
// If there are less bounds in this impl, it can't be more specific.
if( bounds_t.size() < bounds_o.size() )
{
DEBUG("Bound count");
return false;
}
auto it_t = bounds_t.begin();
auto it_o = bounds_o.begin();
while( it_t != bounds_t.end() && it_o != bounds_o.end() )
{
auto cmp = ::ord(*it_t, *it_o);
if( cmp == OrdEqual )
{
++it_t;
++it_o;
continue ;
}
// If the two bounds are similar
if( it_t->tag() == it_o->tag() && it_t->is_TraitBound() )
{
const auto& b_t = it_t->as_TraitBound();
const auto& b_o = it_o->as_TraitBound();
// Check if the type is equal
if( b_t.type == b_o.type && b_t.trait.m_path.m_path == b_o.trait.m_path.m_path )
{
const auto& params_t = b_t.trait.m_path.m_params;
const auto& params_o = b_o.trait.m_path.m_params;
switch( typelist_ord_specific(sp, params_t.m_types, params_o.m_types) )
{
case ::OrdLess: return false;
case ::OrdGreater: return true;
case ::OrdEqual: break;
}
// TODO: Find cases where there's `T: Foo<T>` and `T: Foo<U>`
for(unsigned int i = 0; i < params_t.m_types.size(); i ++ )
{
if( params_t.m_types[i] != params_o.m_types[i] && params_t.m_types[i] == b_t.type )
{
return true;
}
}
TODO(sp, *it_t << " ?= " << *it_o);
}
}
if( cmp == OrdLess )
{
++ it_t;
}
else
{
//++ it_o;
return false;
}
}
if( it_t != bounds_t.end() )
{
return true;
}
else
{
return false;
}
}
// Returns `true` if the two impls overlap in the types they will accept
bool ::HIR::TraitImpl::overlaps_with(const ::HIR::TraitImpl& other) const
{
struct H {
static bool types_overlap(const ::HIR::PathParams& a, const ::HIR::PathParams& b)
{
for(unsigned int i = 0; i < ::std::min(a.m_types.size(), b.m_types.size()); i ++)
{
if( ! H::types_overlap(a.m_types[i], b.m_types[i]) )
return false;
}
return true;
}
static bool types_overlap(const ::HIR::TypeRef& a, const ::HIR::TypeRef& b)
{
static Span sp;
//DEBUG("(" << a << "," << b << ")");
if( a.m_data.is_Generic() || b.m_data.is_Generic() )
return true;
// TODO: Unbound/Opaque paths?
if( a.m_data.tag() != b.m_data.tag() )
return false;
TU_MATCHA( (a.m_data, b.m_data), (ae, be),
(Generic,
),
(Infer,
),
(Diverge,
),
(Closure,
BUG(sp, "Hit closure");
),
(Primitive,
if( ae != be )
return false;
),
(Path,
if( ae.path.m_data.tag() != be.path.m_data.tag() )
return false;
TU_MATCHA( (ae.path.m_data, be.path.m_data), (ape, bpe),
(Generic,
if( ape.m_path != bpe.m_path )
return false;
return H::types_overlap(ape.m_params, bpe.m_params);
),
(UfcsUnknown,
),
(UfcsKnown,
),
(UfcsInherent,
)
)
TODO(sp, "Path - " << ae.path << " and " << be.path);
),
(TraitObject,
if( ae.m_trait.m_path.m_path != be.m_trait.m_path.m_path )
return false;
if( !H::types_overlap(ae.m_trait.m_path.m_params, be.m_trait.m_path.m_params) )
return false;
// Marker traits only overlap if the lists are the same (with overlap)
if( ae.m_markers.size() != be.m_markers.size() )
return false;
for(size_t i = 0; i < ae.m_markers.size(); i++)
{
if( ae.m_markers[i].m_path != be.m_markers[i].m_path )
return false;
if( !H::types_overlap(ae.m_markers[i].m_params, be.m_markers[i].m_params) )
return false;
}
return true;
),
(ErasedType,
TODO(sp, "ErasedType - " << a);
),
(Function,
if( ae.is_unsafe != be.is_unsafe )
return false;
if( ae.m_abi != be.m_abi )
return false;
if( ae.m_arg_types.size() != be.m_arg_types.size() )
return false;
for(unsigned int i = 0; i < ae.m_arg_types.size(); i ++)
{
if( ! H::types_overlap(ae.m_arg_types[i], be.m_arg_types[i]) )
return false;
}
),
(Tuple,
if( ae.size() != be.size() )
return false;
for(unsigned int i = 0; i < ae.size(); i ++)
{
if( ! H::types_overlap(ae[i], be[i]) )
return false;
}
),
(Slice,
return H::types_overlap( *ae.inner, *be.inner );
),
(Array,
if( ae.size_val != be.size_val )
return false;
return H::types_overlap( *ae.inner, *be.inner );
),
(Pointer,
if( ae.type != be.type )
return false;
return H::types_overlap( *ae.inner, *be.inner );
),
(Borrow,
if( ae.type != be.type )
return false;
return H::types_overlap( *ae.inner, *be.inner );
)
)
return true;
}
};
// Quick Check: If the types are equal, they do overlap
if(this->m_type == other.m_type && this->m_trait_args == other.m_trait_args)
{
return true;
}
// 1. Are the impl types of the same form (or is one generic)
if( ! H::types_overlap(this->m_type, other.m_type) )
return false;
if( ! H::types_overlap(this->m_trait_args, other.m_trait_args) )
return false;
DEBUG("TODO: Handle potential overlap (when not exactly equal)");
//return this->m_type == other.m_type && this->m_trait_args == other.m_trait_args;
Span sp;
// TODO: Use `type_ord_specific` but treat any case of mixed ordering as this returning `false`
try
{
type_ord_specific(sp, this->m_type, other.m_type);
typelist_ord_specific(sp, this->m_trait_args.m_types, other.m_trait_args.m_types);
}
catch(const TypeOrdSpecific_MixedOrdering& /*e*/)
{
return false;
}
// TODO: Detect `impl<T> Foo<T> for Bar<T>` vs `impl<T> Foo<&T> for Bar<T>`
// > Create values for impl params from the type, then check if the trait params are compatible
// > Requires two lists, and telling which one to use by the end
auto cb_ident = [](const ::HIR::TypeRef& x)->const ::HIR::TypeRef& { return x; };
::std::vector<const ::HIR::TypeRef*> impl_tys;
auto cb_match = [&](unsigned int idx, const ::HIR::TypeRef& x)->::HIR::Compare {
assert(idx < impl_tys.size());
if( impl_tys.at(idx) )
{
DEBUG("Compare " << x << " and " << *impl_tys.at(idx));
return (x == *impl_tys.at(idx) ? ::HIR::Compare::Equal : ::HIR::Compare::Unequal);
}
else
{
impl_tys.at(idx) = &x;
return ::HIR::Compare::Equal;
}
};
impl_tys.resize( this->m_params.m_types.size() );
if( ! this->m_type.match_test_generics(sp, other.m_type, cb_ident, cb_match) )
{
DEBUG("- Type mismatch, try other ordering");
impl_tys.clear(); impl_tys.resize( other.m_params.m_types.size() );
if( !other.m_type.match_test_generics(sp, this->m_type, cb_ident, cb_match) )
{
DEBUG("- Type mismatch in both orderings");
return false;
}
if( other.m_trait_args.match_test_generics_fuzz(sp, this->m_trait_args, cb_ident, cb_match) != ::HIR::Compare::Equal )
{
DEBUG("- Params mismatch");
return false;
}
// Matched with second ording
}
else if( this->m_trait_args.match_test_generics_fuzz(sp, other.m_trait_args, cb_ident, cb_match) != ::HIR::Compare::Equal )
{
DEBUG("- Param mismatch, try other ordering");
impl_tys.clear(); impl_tys.resize( other.m_params.m_types.size() );
if( !other.m_type.match_test_generics(sp, this->m_type, cb_ident, cb_match) )
{
DEBUG("- Type mismatch in alt ordering");
return false;
}
if( other.m_trait_args.match_test_generics_fuzz(sp, this->m_trait_args, cb_ident, cb_match) != ::HIR::Compare::Equal )
{
DEBUG("- Params mismatch in alt ordering");
return false;
}
// Matched with second ordering
}
else
{
// Matched with first ordering
}
return true;
}
const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path(const Span& sp, const char* name) const
{
auto it = this->m_lang_items.find( name );
if( it == this->m_lang_items.end() ) {
ERROR(sp, E0000, "Undefined language item '" << name << "' required");
}
return it->second;
}
const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path_opt(const char* name) const
{
static ::HIR::SimplePath empty_path;
auto it = this->m_lang_items.find( name );
if( it == this->m_lang_items.end() ) {
return empty_path;
}
return it->second;
}
const ::HIR::TypeItem& ::HIR::Crate::get_typeitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name, bool ignore_last_node) const
{
ASSERT_BUG(sp, path.m_components.size() > 0, "get_typeitem_by_path received invalid path - " << path);
ASSERT_BUG(sp, path.m_components.size() > (ignore_last_node ? 1 : 0), "get_typeitem_by_path received invalid path - " << path);
const ::HIR::Module* mod;
if( !ignore_crate_name && path.m_crate_name != m_crate_name ) {
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded for " << path);
mod = &m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else {
mod = &this->m_root_module;
}
for( unsigned int i = 0; i < path.m_components.size() - (ignore_last_node ? 2 : 1); i ++ )
{
const auto& pc = path.m_components[i];
auto it = mod->m_mod_items.find( pc );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Couldn't find component " << i << " of " << path);
}
TU_IFLET(::HIR::TypeItem, it->second->ent, Module, e,
mod = &e;
)
else {
BUG(sp, "Node " << i << " of path " << path << " wasn't a module");
}
}
auto it = mod->m_mod_items.find( ignore_last_node ? path.m_components[path.m_components.size()-2] : path.m_components.back() );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Could not find type name in " << path);
}
return it->second->ent;
}
const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_last_node/*=false*/) const
{
if( ignore_last_node )
{
ASSERT_BUG(sp, path.m_components.size() > 0, "get_mod_by_path received invalid path with ignore_last_node=true - " << path);
}
if( path.m_components.size() == (ignore_last_node ? 1 : 0) )
{
if( path.m_crate_name != m_crate_name )
{
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded");
return m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else
{
return this->m_root_module;
}
}
else
{
const auto& ti = this->get_typeitem_by_path(sp, path, false, ignore_last_node);
TU_IFLET(::HIR::TypeItem, ti, Module, e,
return e;
)
else {
BUG(sp, "Module path " << path << " didn't point to a module");
}
}
}
const ::HIR::Trait& ::HIR::Crate::get_trait_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Trait, e,
return e;
)
else {
BUG(sp, "Trait path " << path << " didn't point to a trait");
}
}
const ::HIR::Struct& ::HIR::Crate::get_struct_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Struct, e,
return e;
)
else {
BUG(sp, "Struct path " << path << " didn't point to a struct");
}
}
const ::HIR::Union& ::HIR::Crate::get_union_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Union, e,
return e;
)
else {
BUG(sp, "Path " << path << " didn't point to a union");
}
}
const ::HIR::Enum& ::HIR::Crate::get_enum_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Enum, e,
return e;
)
else {
BUG(sp, "Enum path " << path << " didn't point to an enum");
}
}
const ::HIR::ValueItem& ::HIR::Crate::get_valitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name) const
{
if( path.m_components.size() == 0) {
BUG(sp, "get_valitem_by_path received invalid path");
}
const ::HIR::Module* mod;
if( !ignore_crate_name && path.m_crate_name != m_crate_name ) {
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded");
mod = &m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else {
mod = &this->m_root_module;
}
for( unsigned int i = 0; i < path.m_components.size() - 1; i ++ )
{
const auto& pc = path.m_components[i];
auto it = mod->m_mod_items.find( pc );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Couldn't find component " << i << " of " << path);
}
TU_IFLET(::HIR::TypeItem, it->second->ent, Module, e,
mod = &e;
)
else {
BUG(sp, "Node " << i << " of path " << path << " wasn't a module");
}
}
auto it = mod->m_value_items.find( path.m_components.back() );
if( it == mod->m_value_items.end() ) {
BUG(sp, "Could not find value name " << path);
}
return it->second->ent;
}
const ::HIR::Function& ::HIR::Crate::get_function_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_valitem_by_path(sp, path);
TU_IFLET(::HIR::ValueItem, ti, Function, e,
return e;
)
else {
BUG(sp, "Enum path " << path << " didn't point to an enum");
}
}
bool ::HIR::Crate::find_trait_impls(const ::HIR::SimplePath& trait, const ::HIR::TypeRef& type, t_cb_resolve_type ty_res, ::std::function<bool(const ::HIR::TraitImpl&)> callback) const
{
auto its = this->m_trait_impls.equal_range( trait );
for( auto it = its.first; it != its.second; ++ it )
{
const auto& impl = it->second;
if( impl.matches_type(type, ty_res) ) {
if( callback(impl) ) {
return true;
}
}
}
for( const auto& ec : this->m_ext_crates )
{
if( ec.second.m_data->find_trait_impls(trait, type, ty_res, callback) ) {
return true;
}
}
return false;
}
bool ::HIR::Crate::find_auto_trait_impls(const ::HIR::SimplePath& trait, const ::HIR::TypeRef& type, t_cb_resolve_type ty_res, ::std::function<bool(const ::HIR::MarkerImpl&)> callback) const
{
auto its = this->m_marker_impls.equal_range( trait );
for( auto it = its.first; it != its.second; ++ it )
{
const auto& impl = it->second;
if( impl.matches_type(type, ty_res) ) {
if( callback(impl) ) {
return true;
}
}
}
for( const auto& ec : this->m_ext_crates )
{
if( ec.second.m_data->find_auto_trait_impls(trait, type, ty_res, callback) ) {
return true;
}
}
return false;
}
bool ::HIR::Crate::find_type_impls(const ::HIR::TypeRef& type, t_cb_resolve_type ty_res, ::std::function<bool(const ::HIR::TypeImpl&)> callback) const
{
// TODO: Restrict which crate is searched based on the type.
for( const auto& impl : this->m_type_impls )
{
if( impl.matches_type(type, ty_res) ) {
if( callback(impl) ) {
return true;
}
}
}
for( const auto& ec : this->m_ext_crates )
{
//DEBUG("- " << ec.first);
if( ec.second.m_data->find_type_impls(type, ty_res, callback) ) {
return true;
}
}
return false;
}
|