-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtype.cpp
More file actions
813 lines (760 loc) · 21.4 KB
/
Copy pathtype.cpp
File metadata and controls
813 lines (760 loc) · 21.4 KB
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
#include "type.h"
void verify(const Type* a){
dbg4(if (a){)
dbg4(ASSERT(a->name>=0 && a->name<g_Names.nextId));
dbg4(verify(a->sub));
dbg4(verify(a->next));
dbg4(});
}
void verify(const Type* a,const Type* b){
verify(a);
verify(b);
}
void verify(const Type* a,const Type* b,const Type* c){
verify(a);
verify(b);
verify(c);
}
void Type::verify(){
verify_type(this);
for (auto x=this->sub; x;x=x->next)
x->verify();
}
bool Type::has_sub_constructors()const{
if (!this) return false;
if (this->name==TUPLE){
for (auto subt=this->sub; subt;subt=subt->next)
if (subt->has_sub_constructors())return true;
} else if (auto sd=this->struct_def())
return sd->has_sub_constructors();
return false;
}
bool Type::has_sub_destructors()const{
if (!this) return false;
if (this->name==TUPLE){
for (auto subt=this->sub; subt;subt=subt->next)
if (subt->has_sub_destructors())return true;
} else if (auto sd=this->struct_def())
return sd->has_sub_destructors();
return false;
}
const Type* Type::get_elem(int index)const{
if (this->struct_def())
return this->struct_def()->get_elem_type(index);
ASSERT(index>=0);
auto s=sub;
for (;s&&index>0;s=s->next,--index){};
ASSERT(index==0);
return s;
}
const Type* Type::get_elem_type_if(int index)const{
if (this->struct_def())
return this->struct_def()->get_elem_type(index);
ASSERT(index>=0);
auto s=sub;
for (;s&&index>0;s=s->next,--index){};
return s;
}
bool Type::is_userdefined()const{
if (!this)return false;
if (this->name==PTR)
return false;
if (this->is_ref()||this->is_qualifier())
return this->sub->is_userdefined();
if (this->name>=IDENT)
return true;
return false;
}
bool Type::is_primitive()const{
if (!this)return false;
if (this->name==PTR)
return false;
if (this->is_ref() || this->is_qualifier())
return this->sub->is_primitive();
if (this->name>=IDENT)
return true;
return false;
}
bool type_is_coercible(const Type* from,const Type* to,bool coerce){
/// TODO cleanup- move into is_equal/_sub instead of passing flag
/// coersions only happen at root level
// void pointers auto-coerce like they should,
// thats what they're there for, legacy C
// modern code just doesn't use void*
// dbg(from->dump(0));dbg(to->dump(0));dbg(newline(0));
if (from->is_pointer_not_ref() && to->is_pointer_not_ref() && coerce){
if (from->is_void_ptr() || to->is_void_ptr())
return true;
}
// coercible struct-pointers with inheritance
auto s1=from->get_struct_autoderef();
auto s2= to->get_struct_autoderef();
if (s1&&s2 && coerce){
if (s1->has_base_class(s2))
return true;
}
// TODO: 'intersection type' component coercion ?
// int coercions
// float
if ((from->is_pointer_not_ref() || from->is_int()) && to->is_bool())
return true;
if (to->size() >= from->size()) {
if (from->is_float() && to->is_float()){
return true;
}
if (from->is_int() && to->is_int()){
if (from->is_signed() && to->is_signed())
return true;
if (!(from->is_signed() || to->is_signed()))
return true;
}
}
if (to->size() > from->size()) {
if (from->is_int() && to->is_int())
return true;
}
return false;
}
ExprStructDef* Type::get_common_base(Type* other){
auto x=this->get_struct_autoderef();
auto y=other->get_struct_autoderef();
if (x&&y) return x->get_common_base(y);
else return nullptr;
}
bool Type::has_typeparam(Scope* sc){
if (this->def) {if (this->def->as_tparam_def()) return true;}
else
{
if (auto tpd=sc->get_typeparam_for(this)){
this->set_def(tpd);
return true;
}
}
for (auto s=this->sub; s; s=s->next){
if (s->has_typeparam(sc))
return true;
}
return false;
}
ResolveResult Type::resolve(Scope* sc,const Type* desired,int flags)
{
if(!this)
return COMPLETE;
if (!this->struct_def() && this->name>=IDENT && !::is_number(this->name)){
if (!strcmp(str(name),"Union")){
sc->owner_fn->dump_if(0);
this->dump(-1);newline(0);
}
if (!this->has_typeparam(sc)){
if (auto sd=sc->find_struct_named(this->name)){
this->set_struct_def(sd->get_instance(sc,this));
dbg_instancing("found struct %s in %s ins%p on t %p\n",this->name_str(), sc->name_str(),this->struct_def(),this);
}else{
dbg_instancing("failed to find struct %s in %s\n",this->name_str(), sc->name_str());
#if DEBUG >=2
sd=sc->find_struct_named(this->name);
sc->dump(0);
#endif
}
}
}
#if DEBUG >=3
dbprintf("%s structdef=%p def= %p\n",this->name_str(),this->struct_def(),this->def);
#endif
auto ds=desired?desired->sub:nullptr;
for (auto s=this->sub;s;s=s->next,ds=ds?ds->next:nullptr)
resolved|=s->resolve_if(sc,ds,flags);
return resolved;
}
Type::Type(Name i,SrcPos sp){
pos=sp;
sub=0;
next=0;
name=i; //todo: resolve-type should happen here.
}
Type::Type(Node* origin,Name i){
this->set_origin(origin);
sub=0;
next=0;
name=i; //todo: resolve-type should happen here.
}
Type* Type::clone_or_auto(){
if (this) return (Type*)this->clone();
else return new Type(AUTO);
}
ExprStructDef* Type::struct_def(){
return this->def?this->def->as_struct_def():nullptr;
}
ExprStructDef* Type::struct_def() const{
return const_cast<Type*>(this)->def?this->def->as_struct_def():nullptr;
}
ExprStructDef* Type::struct_def_noderef()const { // without autoderef
// if (struct_def) return struct_def->as_struct_def();
// else return nullptr;
return struct_def();
};
bool Type::is_equal_s(Type* other, int mode, Name self_t) {
/// TODO this is redundant, move all to typeparam based matcher
/// and just pass tp==0
// handle root level coersions
if ((!this) && (!other)) return true;
if ((mode & TC_COERCE) && this && other){
if (this->name!=CONST && other->name==CONST ){ // non-const coerces to const just fine - too mutability flag tracked on type itself, to handle mut/const from C++/rust. to be annotaed from fn
return is_equal_s(other->sub, mode, self_t);
}
if (this->name==MUT && other->name!=MUT){ // non-const coerces to const just fine - too mutability
return this->sub->is_equal_s(other, mode, self_t);
}
if (this->is_ref() && !other->is_ref()) { // ref coerces to value
return this->sub->is_equal_s(other,mode,self_t);
}
if (!this->is_ref() && other->is_ref()) { // object coerces to ref fine
return this->is_equal_s(other->sub,mode,self_t);
}
}
return is_equal_sub(other,mode,self_t);
}
void Type::infer_components_sub(const Type* src) {
//TODO - also for absent tparams.
// replace auto slots in 'this' from 'src'
// handle cases like auto<X,Y> == B<X,Y> , auto<auto,Y> = B<X,Y> ...
if (!src) return; // no source to replace from..
if (this->name==AUTO){
this->name = src->name;
}
auto d=this->sub; auto s=src->sub;
for (; d&& s; d=d->next, s=s->next){
d->infer_components_sub(s);
}
for (;s; s=s->next ) {
this->push_back((Type*)s->clone());
}
}
void infer_components(Type* dst, const Type* src){
if ((!dst->sub && src->sub) || dst->name==AUTO){
dst->infer_components_sub(src);
}
}
bool Type::is_equal_sub(Type* other,int mode,Name self_t){
/// TODO factor out common logic, is_coercible(),eq(),eq(,xlat)
if ((!this) && (!other)) return true;
// if its' auto[...] match contents; if its plain auto, match anything.
if (mode & TC_INFER_REV){
infer_components(this,other);
}
if (mode & TC_INFER_FWD){
infer_components(other,this);
}
if (this){
if (this->name==AUTO){
if (this->sub && other) {
return this->sub->is_equal(other->sub,mode);
}
else {
return true;
}
}
}
if (other){
if (other->name==AUTO){
if (other->sub && this) return other->sub->is_equal(this->sub,mode);
else return true;
}
}
if (!(this && other)) return false;
if (type_is_coercible(this,other,mode))
return true;
else{
if (this->name==SELF_T || other->name==SELF_T){
dbg_vtable("self found, self=%s\n",str(self_t));
}
auto n0=this->name==SELF_T?self_t:this->name;
auto n1=other->name==SELF_T?self_t:other->name;
if (n0!=n1)
return false;
}
// if (!this->sub && other->sub)) return true;
if (other->name==STR && type_compare(this,PTR,CHAR)) return true;
if (this->name==STR && type_compare(other,PTR,CHAR)) return true;
auto p=this->sub,o=other->sub;
for (; p && o; p=p->next,o=o->next) {
if (!p->is_equal(o,mode & ~TC_COERCE,self_t))// coercible only applies to root. eg vec<int>!=vec<bool> but int coerces to bool, ptr[Foo:>Bar] coerces to ptr[Bar]
return false;
}
if (o || p)
return false; // didnt reach both..
return true;
}
bool Type::is_equal(const Type* other,const TParamXlat& xlat,Name self_t) const{
//if (coerce)
if ((!this) && (!other)) return true;
if (this && other && 0)
{
if (this->name!=CONST && other->name==CONST){ // non-const coerces to const just fine - too mutability flag tracked on type itself, to handle mut/const from C++/rust. to be annotaed from fn
return this->is_equal(other->sub, xlat, self_t);
}
if (this->name==MUT && other->name!=MUT){ // non-const coerces to const just fine - too mutability
return this->sub->is_equal(other, xlat, self_t);
}
if (this->is_ref() && !other->is_ref()) { // ref coerces to val fine.
return this->sub->is_equal(other,xlat,self_t);
}
if (!this->is_ref() && other->is_ref()) { // val coerces to ref fine
return this->is_equal(other->sub,xlat,self_t);
}
}
return is_equal_sub(other,xlat,self_t);
}
bool Type::is_equal_sub(const Type* other,const TParamXlat& xlat,Name self_t) const{
if ((!this) && (!other)) return true;
// if its' auto[...] match contents; if its plain auto, match anything.
if (this &&this->name==AUTO){
if (this->sub && other) return this->sub->is_equal(other->sub,xlat);
else return true;
}
if (other && other->name==AUTO){
if (other->sub && this) return other->sub->is_equal(this->sub,xlat);
else return true;
}
if (!(this && other))
return false;
// TODO: might be more subtle than this for HKT
auto ti=xlat.typeparam_index(other->name);
dbg_type("%s %s\n",str(this->name),str(other->name));
if (ti>=0){
return this->is_equal(xlat.given_types[ti],xlat);
}
ti=xlat.typeparam_index(this->name);
if (ti>=0){
return xlat.given_types[ti]->is_equal(other,xlat);
}
if (this->name!=other->name)return false;
// if (!this->sub && other->sub)) return true;
if (other->name==STR && type_compare(this,PTR,CHAR)) return true;
if (this->name==STR && type_compare(other,PTR,CHAR)) return true;
if (type_is_coercible(this,other,true))
return true;
auto p=this->sub,o=other->sub;
for (; p && o; p=p->next,o=o->next) {
if (!p->is_equal(o,xlat)) return false;
}
if (o || p) return false; // didnt reach both..
return true;
}
bool g_print_types_raw=false;
void Type::dump_sub(int flags)const{
if (!this) return;
if (g_print_types_raw){
if (this->is_rvalue()) dbprintf("r.");
}
if (!g_print_types_raw){
if (this->is_pointer_or_ref()){
const char* sigil=0;
switch ((int)this->name){
case PTR: sigil="*"; break;
case REF: sigil="&"; break;
case RVALUE_REF: sigil="&&"; break;
}
if (sigil){
this->sub->dump(flags);
return;
}
}
if (this->name==FN || this->name==CLOSURE){
if (auto rect=this->get_elem_type_if(2)){
rect->dump_sub(flags);dbprintf("::");
}
if (this->name==FN) {
this->sub->dump_if(flags);
dbprintf("->");
} else {
dbprintf("|");
for (auto argt=this->sub->sub; argt; argt=argt->next){
argt->dump_if(flags);
if (argt->next)dbprintf(",");
}
dbprintf("|");
if (this->sub->next)
dbprintf("->");
}
if (this->sub){
this->sub->next->dump_if(flags);
}
return;
}
}
if (this->name==TUPLE) {
dbprintf("(");
for (auto t=sub; t; t=t->next){
t->dump_sub(flags);
if(t->next)dbprintf(",");
};
dbprintf(")");
} else{
dbprintf("%s",getString(name));
#if DEBUG>=3
if (this->struct_def())
dbprintf("( struct_def=%s )", str(this->struct_def()->get_mangled_name()));
if (this->def)
dbprintf("( def=%s )", str(this->def->get_mangled_name()));
#endif
if (sub){
dbprintf("<");
for (auto t=sub; t; t=t->next){
t->dump_sub(flags);
if(t->next)dbprintf(",");
};
dbprintf(">");
}
}
}
const char* Type::kind_str()const{return"type";}
bool Type::is_complex()const{
if (sub) return true; // todo: we assume anything with tparams is a struct, it might just be calculation
for (auto a=sub; a;a=a->next)if (a->is_complex()) return true;
if (this->is_struct()||this->name==ARRAY||this->name==VARIANT) return true;
return false;
}
// todo table of each 'intrinsic type', and pointer to it
Type* g_bool,*g_void,*g_void_ptr,*g_int,*g_i32,*g_u32,*g_auto,*g_u8,*g_float;
Type* Type::get_bool(){
/// todo type hash on inbuilt indices
if (g_bool)return g_bool;
return (g_bool=new Type(nullptr,BOOL));
}
Type* Type::get_auto(){
/// todo type hash on inbuilt indices
if (g_auto)return g_auto;
return (g_auto=new Type(nullptr,AUTO));
}
Type* Type::get_void(){
if (g_void)return g_void;
return (g_void=new Type(nullptr,VOID));
}
Type* Type::get_int(){
if (g_int)return g_int;
return (g_int=new Type(nullptr,INT));
}
Type* Type::get_i32(){
if (g_i32)return g_i32;
return (g_i32=new Type(nullptr,I32));
}
Type* Type::get_u32(){
if (g_u32)return g_u32;
return (g_u32=new Type(nullptr,I32));
}
Type* Type::get_float(){
if (g_float)return g_float;
return (g_float=new Type(nullptr,FLOAT));
}
Type* Type::get_u8(){
if (g_u8)return g_u8;
return (g_u8=new Type(nullptr,I8));
}
Type* Type::get_void_ptr(){
if (g_void_ptr)return g_void_ptr;
return (g_void_ptr=new Type(nullptr,PTR,VOID));
}
bool Type::is_struct()const{
return struct_def()!=0 || name>=IDENT ||(def && def->as_struct_def()); //TODO .. it might be a typedef.
}
int Type::num_pointers() const {
if (!this) return 0;
if (this->is_pointer_or_ref())
return 1+this->sub->is_pointer();
else return 0;
}
int Type::num_pointers_and_arrays() const {
if (!this) return 0;
if (this->is_pointer_or_ref()|| this->name==ARRAY)
return 1+this->sub->is_pointer();
else return 0;
}
size_t Type::alignment() const{
if (this->raw_type_flags()){
return this->size();
}
size_t align=0;
for (auto s=this->sub;s;s=s->next){
if (auto sz=s->size()>align){align=sz;};
}
if (this->struct_def()){
return this->struct_def()->alignment();
}
return align;
}
size_t Type::size() const{
auto tf=this->raw_type_flags();
if (tf){
return tf&RT_SIZEMASK
};
auto union_size=[](const Type *t){
size_t max_elem_size=0;
for (auto s=t->sub; s;s=s->next){
auto sz=s->size();
if (sz>max_elem_size)max_elem_size=sz;
}
return max_elem_size;
};
if (this->name==UNION){
return union_size(this);
}
if (this->name==VARIANT){
auto align=this->alignment();
return align+union_size(this);
}
if (this->name==TUPLE){
int size=0;
for (auto s=this->sub; s;s=s->next){
size+=s->size();
}
return size;
}
if (this->struct_def()){
return struct_def()->size();
}
return 0;
}
ExprStructDef* Type::get_struct_autoderef()const{
auto p=this;
// while (p && !p->is_struct()){
while (p && p->is_qualifier_or_ptr_or_ref()){
p=p->sub;
}
return p?p->struct_def():nullptr;
}
ExprStructDef* Type::get_receiver()const
{
if (this->sub)
if (this->sub->next)
if (this->sub->next->next)
return this->sub->next->next->struct_def();
return nullptr;
}
void Type::dump(PrinterRef depth)const{
if (!this) return;
newline(depth);dump_sub(depth);
}
Type::Type(ExprStructDef* sd)
{ set_struct_def(sd); name=sd->name; sub=0; next=0;
}
Type::Type(Name outer_name,ExprStructDef* sd)
{
name=outer_name;
push_back(new Type(sd));
}
Node*
Type::clone() const{
if (!this) return nullptr;
auto r= new Type(this->get_origin(),this->name);
r->set_struct_def(this->struct_def());
auto *src=this->sub;
Type** p=&r->sub;
while (src) {
*p= (Type*)src->clone();
p=&((*p)->next);
src=src->next;
}
r->rvalue=this->rvalue;
return r;
}
bool Type::has_non_instanced_typeparams()const{ if (!def) return true; if (def->as_tparam_def()) return false; return true;}
void Type::translate_tparams(const TParamXlat& tpx){
this->translate_typeparams_sub(tpx,nullptr);
}
void Type::translate_typeparams_sub(const TParamXlat& tpx,Type* inherit_replace){
// todo: replace wih 'instantiate' typparams, given complex cases
/*
example:
struct MyVec<T,N=int> {
data:*T;
count:N;
}
instantiate MyVec<string,short>
translate_tempalte_type( {T,N}, {string,short}, *T) -> *string
translate_tempalte_type( {T,N}, {string,short}, N) -> short.
HKT example
struct tree<S,T>{
S<tree<S,T>> sub;
}
instantiate tree<vector,int>
we want:
struct tree {
MyVec< tree<vector,int>> sub;
}
translate_tempalte_type( {S,T}, {vector,int}, S<tree<S,T>>)-> MyVec<tree<MyVec<int>>> //
*/
// TODO: assert there is no shadowing in this types' own definitions
Type* new_type=0;
if (!this->is_anon_struct())
this->clear_struct_def();
int param_index=tpx.typeparam_index(this->name);
if ((this->name==PLACEHOLDER || this->name==AUTO) && inherit_replace){
this->name=inherit_replace->name;
if (!this->sub && inherit_replace->sub){
error(this,"TODO - replace an auto typeparam with complex given typeparam ");
}
}
if (param_index>=0){
auto pi=param_index;
auto src_ty=tpx.given_types[pi];
if (!src_ty){
error(this,"typaram not given,partial instance?");
}
if (!src_ty->sub) {
this->name=src_ty->name;
} else if (!this->sub){
this->name=src_ty->name;
for (auto s=src_ty->sub;s;s=s->next){
this->push_back((Type*)s->clone());
};
}
else {
#if DEBUG>=2
tpx.dump(0);
tpx.tparams[pi]->dump(-1);
dbg_generic(" replace with ");
tpx.given_types[pi]->dump(-1);newline(0);
newline(0);
dbg_generic("substituting in:");
this->dump(-1);
newline(0);
#endif
//error_begin(this,"param index %d %s trying to instantiate complex typeparameter into non-root of another complex typeparameter,we dont support this yet\n",param_index, tpx.tparams[pi]->name_str());
//error_end(this);
this->name=src_ty->name;
auto inherit_sub=inherit_replace?inherit_replace->sub:nullptr;
auto* pps=&this->sub;
for (auto s=this->sub; s; pps=&s,s=s->next){
s->translate_typeparams_sub(tpx,inherit_sub);
inherit_sub=inherit_sub?inherit_sub->next:nullptr;
}
#if DEBUG>=2
dbg_generic("result:");
this->dump(-1);
newline(0);
#endif
}
}
//this->struct_def=tpx.
// translate child elems.
auto inherit_sub=inherit_replace?inherit_replace->sub:nullptr;
auto* pps=&this->sub;
for (auto sub=this->sub; sub; pps=&sub->next,sub=*pps) {
sub->translate_typeparams_sub(tpx,inherit_sub);
inherit_sub=inherit_sub?inherit_sub->next:nullptr;
}
// replace any not give with inherit..
while (inherit_sub){
*pps = (Type*)inherit_sub->clone();
inherit_sub=inherit_sub->next;
pps=&((*pps)->next);
}
}
void Type::clear_struct_def(){
this->clear_def();
}
void Type::set_struct_def(ExprStructDef* sd){
this->set_def(sd);
}
void Type::push_back(Type* t) {
if (!sub) sub=t;
else {
auto s=sub;
for (; s->next!=0; s=s->next){};
s->next =t;
}
}
CgValue Type::compile(CodeGen& cg, Scope* sc, CgValue input){
return CgValue(0,this,0); // propogate a type into compiler interface
}
void dump(const Type* a,const Type* b){
dbprintf("\ntype1:"); a->dump_if(-1);
dbprintf("\ttype2:"); b->dump_if(-1);
dbprintf("\n");
}
ResolveResult type_error(int flags, const Node* n, const Type* a, const Type* b){
if (!(flags & R_FINAL))
return ResolveResult(RS_ERROR);
// error is stronger than incomplete. incomplete means keep going, error means give up
dbg(n->dump(0));
a->is_coercible(b);
error_begin(n," type mismatch\n");
warning(a->get_origin(),"from here:");
a->dump(-1);
warning(b->get_origin(),"vs here:");
b->dump(-1);
newline(0);
#if DEBUG>=2
if (a->is_coercible(b)){
}
g_pRoot->dump(0);
newline(0);
dbprintf("type mismatch see above\n");
#endif
error_end(n);
return ResolveResult(RS_ERROR);
}
ResolveResult assert_types_eq(int flags, const Node* n, const Type* a,const Type* b) {
ASSERT(a && b);
// TODO: variadic args shouldn't get here:
if (a->name==ELIPSIS||b->name==ELIPSIS)
return ResolveResult(COMPLETE);
if (!a->is_equal(b)){
if (a->is_coercible(b)){
return ResolveResult(COMPLETE);
}
return type_error(flags,n,a,b);
}
return ResolveResult(COMPLETE);
}
ResolveResult infer_and_cmp_types(int flags, const Node* n, Type*& a, Type*& b) {
ASSERT(a && b);
// TODO: variadic args shouldn't get here:
if (a->name==ELIPSIS||b->name==ELIPSIS)
return ResolveResult(COMPLETE);
if (!a->is_equal(b)){
if (a->is_coercible(b)){
return ResolveResult(COMPLETE);
}
if (a->is_inferable(b)){
return (a->is_equal(b))?ResolveResult(COMPLETE):ResolveResult(INCOMPLETE);
}
return type_error(flags,n,a,b);
}
return ResolveResult(COMPLETE);
}
ResolveResult infer_and_cmp_types_rev(int flags, const Node* n, Type*& a, const Type* b) {
ASSERT(a && b);
// TODO: variadic args shouldn't get here:
if (a->name==ELIPSIS||b->name==ELIPSIS)
return ResolveResult(COMPLETE);
if (!a->is_equal(b)){
if (a->is_coercible(b)){
return ResolveResult(COMPLETE);
}
if (a->is_inferable_rev(b)){
return (a->is_equal(b))?ResolveResult(COMPLETE):ResolveResult(INCOMPLETE);
}
return type_error(flags,n,a,b);
}
return ResolveResult(COMPLETE);
}
void dump_tparams(const MyVec<TParamDef*>& ts, const MyVec<TParamVal*>* given) {
bool a=false;
if (ts.size() && given->size()==0) return;
if (ts.size()==0 && given->size()==0) return;
dbprintf("<tparams>");
for (int i=0; i<ts.size() || i<given->size(); i++){
if (a)dbprintf(",");
if (i<ts.size()){
print_tok(ts[i]->name);
} else dbprintf("%d",i);
Type* tv=nullptr; if (i<given->size()) tv=given->at(i);
if (!tv) {if (ts.size()){tv=ts[i]->defaultv;}}
if (tv){dbprintf("=");tv->dump(-1);}
a=true;
}
dbprintf("</tparams>");
}
// todo table of each 'intrinsic type', and pointer to it