/
mtype.d
10346 lines (9320 loc) · 296 KB
/
mtype.d
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (c) 1999-2017 by Digital Mars, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(DMDSRC _mtype.d)
*/
module ddmd.mtype;
import core.checkedint;
import core.stdc.stdarg;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import ddmd.access;
import ddmd.aggregate;
import ddmd.aliasthis;
import ddmd.arrayop;
import ddmd.arraytypes;
import ddmd.gluelayer;
import ddmd.complex;
import ddmd.dcast;
import ddmd.dclass;
import ddmd.declaration;
import ddmd.denum;
import ddmd.dimport;
import ddmd.dmangle;
import ddmd.dscope;
import ddmd.dstruct;
import ddmd.dsymbol;
import ddmd.dtemplate;
import ddmd.errors;
import ddmd.expression;
import ddmd.func;
import ddmd.globals;
import ddmd.hdrgen;
import ddmd.id;
import ddmd.identifier;
import ddmd.imphint;
import ddmd.init;
import ddmd.opover;
import ddmd.root.ctfloat;
import ddmd.root.outbuffer;
import ddmd.root.rmem;
import ddmd.root.rootobject;
import ddmd.root.stringtable;
import ddmd.sideeffect;
import ddmd.target;
import ddmd.tokens;
import ddmd.visitor;
enum LOGDOTEXP = 0; // log ::dotExp()
enum LOGDEFAULTINIT = 0; // log ::defaultInit()
extern (C++) __gshared int Tsize_t = Tuns32;
extern (C++) __gshared int Tptrdiff_t = Tint32;
enum SIZE_INVALID = (~cast(d_uns64)0); // error return from size() functions
/***************************
* Return !=0 if modfrom can be implicitly converted to modto
*/
bool MODimplicitConv(MOD modfrom, MOD modto) pure nothrow @nogc @safe
{
if (modfrom == modto)
return true;
//printf("MODimplicitConv(from = %x, to = %x)\n", modfrom, modto);
auto X(T, U)(T m, U n)
{
return ((m << 4) | n);
}
switch (X(modfrom & ~MODshared, modto & ~MODshared))
{
case X(0, MODconst):
case X(MODwild, MODconst):
case X(MODwild, MODwildconst):
case X(MODwildconst, MODconst):
return (modfrom & MODshared) == (modto & MODshared);
case X(MODimmutable, MODconst):
case X(MODimmutable, MODwildconst):
return true;
default:
return false;
}
}
/***************************
* Return MATCHexact or MATCHconst if a method of type '() modfrom' can call a method of type '() modto'.
*/
MATCH MODmethodConv(MOD modfrom, MOD modto) pure nothrow @nogc @safe
{
if (modfrom == modto)
return MATCHexact;
if (MODimplicitConv(modfrom, modto))
return MATCHconst;
auto X(T, U)(T m, U n)
{
return ((m << 4) | n);
}
switch (X(modfrom, modto))
{
case X(0, MODwild):
case X(MODimmutable, MODwild):
case X(MODconst, MODwild):
case X(MODwildconst, MODwild):
case X(MODshared, MODshared | MODwild):
case X(MODshared | MODimmutable, MODshared | MODwild):
case X(MODshared | MODconst, MODshared | MODwild):
case X(MODshared | MODwildconst, MODshared | MODwild):
return MATCHconst;
default:
return MATCHnomatch;
}
}
/***************************
* Merge mod bits to form common mod.
*/
MOD MODmerge(MOD mod1, MOD mod2) pure nothrow @nogc @safe
{
if (mod1 == mod2)
return mod1;
//printf("MODmerge(1 = %x, 2 = %x)\n", mod1, mod2);
MOD result = 0;
if ((mod1 | mod2) & MODshared)
{
// If either type is shared, the result will be shared
result |= MODshared;
mod1 &= ~MODshared;
mod2 &= ~MODshared;
}
if (mod1 == 0 || mod1 == MODmutable || mod1 == MODconst || mod2 == 0 || mod2 == MODmutable || mod2 == MODconst)
{
// If either type is mutable or const, the result will be const.
result |= MODconst;
}
else
{
// MODimmutable vs MODwild
// MODimmutable vs MODwildconst
// MODwild vs MODwildconst
assert(mod1 & MODwild || mod2 & MODwild);
result |= MODwildconst;
}
return result;
}
/*********************************
* Store modifier name into buf.
*/
void MODtoBuffer(OutBuffer* buf, MOD mod)
{
switch (mod)
{
case 0:
break;
case MODimmutable:
buf.writestring(Token.toString(TOKimmutable));
break;
case MODshared:
buf.writestring(Token.toString(TOKshared));
break;
case MODshared | MODconst:
buf.writestring(Token.toString(TOKshared));
buf.writeByte(' ');
goto case; /+ fall through +/
case MODconst:
buf.writestring(Token.toString(TOKconst));
break;
case MODshared | MODwild:
buf.writestring(Token.toString(TOKshared));
buf.writeByte(' ');
goto case; /+ fall through +/
case MODwild:
buf.writestring(Token.toString(TOKwild));
break;
case MODshared | MODwildconst:
buf.writestring(Token.toString(TOKshared));
buf.writeByte(' ');
goto case; /+ fall through +/
case MODwildconst:
buf.writestring(Token.toString(TOKwild));
buf.writeByte(' ');
buf.writestring(Token.toString(TOKconst));
break;
default:
assert(0);
}
}
/*********************************
* Return modifier name.
*/
char* MODtoChars(MOD mod)
{
OutBuffer buf;
buf.reserve(16);
MODtoBuffer(&buf, mod);
return buf.extractString();
}
/************************************
* Convert MODxxxx to STCxxx
*/
StorageClass ModToStc(uint mod) pure nothrow @nogc @safe
{
StorageClass stc = 0;
if (mod & MODimmutable)
stc |= STCimmutable;
if (mod & MODconst)
stc |= STCconst;
if (mod & MODwild)
stc |= STCwild;
if (mod & MODshared)
stc |= STCshared;
return stc;
}
/************************************
* Strip all parameter's idenfiers and their default arguments for merging types.
* If some of parameter types or return type are function pointer, delegate, or
* the types which contains either, then strip also from them.
*/
private Type stripDefaultArgs(Type t)
{
static Parameters* stripParams(Parameters* parameters)
{
Parameters* params = parameters;
if (params && params.dim > 0)
{
foreach (i; 0 .. params.dim)
{
Parameter p = (*params)[i];
Type ta = stripDefaultArgs(p.type);
if (ta != p.type || p.defaultArg || p.ident)
{
if (params == parameters)
{
params = new Parameters();
params.setDim(parameters.dim);
foreach (j; 0 .. params.dim)
(*params)[j] = (*parameters)[j];
}
(*params)[i] = new Parameter(p.storageClass, ta, null, null);
}
}
}
return params;
}
if (t is null)
return t;
if (t.ty == Tfunction)
{
TypeFunction tf = cast(TypeFunction)t;
Type tret = stripDefaultArgs(tf.next);
Parameters* params = stripParams(tf.parameters);
if (tret == tf.next && params == tf.parameters)
goto Lnot;
tf = cast(TypeFunction)tf.copy();
tf.parameters = params;
tf.next = tret;
//printf("strip %s\n <- %s\n", tf.toChars(), t.toChars());
t = tf;
}
else if (t.ty == Ttuple)
{
TypeTuple tt = cast(TypeTuple)t;
Parameters* args = stripParams(tt.arguments);
if (args == tt.arguments)
goto Lnot;
t = t.copy();
(cast(TypeTuple)t).arguments = args;
}
else if (t.ty == Tenum)
{
// TypeEnum::nextOf() may be != NULL, but it's not necessary here.
goto Lnot;
}
else
{
Type tn = t.nextOf();
Type n = stripDefaultArgs(tn);
if (n == tn)
goto Lnot;
t = t.copy();
(cast(TypeNext)t).next = n;
}
//printf("strip %s\n", t.toChars());
Lnot:
return t;
}
enum TFLAGSintegral = 1;
enum TFLAGSfloating = 2;
enum TFLAGSunsigned = 4;
enum TFLAGSreal = 8;
enum TFLAGSimaginary = 0x10;
enum TFLAGScomplex = 0x20;
private Expression semanticLength(Scope* sc, TupleDeclaration tup, Expression exp)
{
ScopeDsymbol sym = new ArrayScopeSymbol(sc, tup);
sym.parent = sc.scopesym;
sc = sc.push(sym);
sc = sc.startCTFE();
exp = exp.semantic(sc);
sc = sc.endCTFE();
sc.pop();
return exp;
}
/**************************
* This evaluates exp while setting length to be the number
* of elements in the tuple t.
*/
private Expression semanticLength(Scope* sc, Type t, Expression exp)
{
if (t.ty == Ttuple)
{
ScopeDsymbol sym = new ArrayScopeSymbol(sc, cast(TypeTuple)t);
sym.parent = sc.scopesym;
sc = sc.push(sym);
sc = sc.startCTFE();
exp = exp.semantic(sc);
sc = sc.endCTFE();
sc.pop();
}
else
{
sc = sc.startCTFE();
exp = exp.semantic(sc);
sc = sc.endCTFE();
}
return exp;
}
enum ENUMTY : int
{
Tarray, // slice array, aka T[]
Tsarray, // static array, aka T[dimension]
Taarray, // associative array, aka T[type]
Tpointer,
Treference,
Tfunction,
Tident,
Tclass,
Tstruct,
Tenum,
Tdelegate,
Tnone,
Tvoid,
Tint8,
Tuns8,
Tint16,
Tuns16,
Tint32,
Tuns32,
Tint64,
Tuns64,
Tfloat32,
Tfloat64,
Tfloat80,
Timaginary32,
Timaginary64,
Timaginary80,
Tcomplex32,
Tcomplex64,
Tcomplex80,
Tbool,
Tchar,
Twchar,
Tdchar,
Terror,
Tinstance,
Ttypeof,
Ttuple,
Tslice,
Treturn,
Tnull,
Tvector,
Tint128,
Tuns128,
TMAX,
}
alias Tarray = ENUMTY.Tarray;
alias Tsarray = ENUMTY.Tsarray;
alias Taarray = ENUMTY.Taarray;
alias Tpointer = ENUMTY.Tpointer;
alias Treference = ENUMTY.Treference;
alias Tfunction = ENUMTY.Tfunction;
alias Tident = ENUMTY.Tident;
alias Tclass = ENUMTY.Tclass;
alias Tstruct = ENUMTY.Tstruct;
alias Tenum = ENUMTY.Tenum;
alias Tdelegate = ENUMTY.Tdelegate;
alias Tnone = ENUMTY.Tnone;
alias Tvoid = ENUMTY.Tvoid;
alias Tint8 = ENUMTY.Tint8;
alias Tuns8 = ENUMTY.Tuns8;
alias Tint16 = ENUMTY.Tint16;
alias Tuns16 = ENUMTY.Tuns16;
alias Tint32 = ENUMTY.Tint32;
alias Tuns32 = ENUMTY.Tuns32;
alias Tint64 = ENUMTY.Tint64;
alias Tuns64 = ENUMTY.Tuns64;
alias Tfloat32 = ENUMTY.Tfloat32;
alias Tfloat64 = ENUMTY.Tfloat64;
alias Tfloat80 = ENUMTY.Tfloat80;
alias Timaginary32 = ENUMTY.Timaginary32;
alias Timaginary64 = ENUMTY.Timaginary64;
alias Timaginary80 = ENUMTY.Timaginary80;
alias Tcomplex32 = ENUMTY.Tcomplex32;
alias Tcomplex64 = ENUMTY.Tcomplex64;
alias Tcomplex80 = ENUMTY.Tcomplex80;
alias Tbool = ENUMTY.Tbool;
alias Tchar = ENUMTY.Tchar;
alias Twchar = ENUMTY.Twchar;
alias Tdchar = ENUMTY.Tdchar;
alias Terror = ENUMTY.Terror;
alias Tinstance = ENUMTY.Tinstance;
alias Ttypeof = ENUMTY.Ttypeof;
alias Ttuple = ENUMTY.Ttuple;
alias Tslice = ENUMTY.Tslice;
alias Treturn = ENUMTY.Treturn;
alias Tnull = ENUMTY.Tnull;
alias Tvector = ENUMTY.Tvector;
alias Tint128 = ENUMTY.Tint128;
alias Tuns128 = ENUMTY.Tuns128;
alias TMAX = ENUMTY.TMAX;
alias TY = ubyte;
enum MODFlags : int
{
MODconst = 1, // type is const
MODimmutable = 4, // type is immutable
MODshared = 2, // type is shared
MODwild = 8, // type is wild
MODwildconst = (MODwild | MODconst), // type is wild const
MODmutable = 0x10, // type is mutable (only used in wildcard matching)
}
alias MODconst = MODFlags.MODconst;
alias MODimmutable = MODFlags.MODimmutable;
alias MODshared = MODFlags.MODshared;
alias MODwild = MODFlags.MODwild;
alias MODwildconst = MODFlags.MODwildconst;
alias MODmutable = MODFlags.MODmutable;
alias MOD = ubyte;
/***********************************************************
*/
extern (C++) abstract class Type : RootObject
{
TY ty;
MOD mod; // modifiers MODxxxx
char* deco;
/* These are cached values that are lazily evaluated by constOf(), immutableOf(), etc.
* They should not be referenced by anybody but mtype.c.
* They can be NULL if not lazily evaluated yet.
* Note that there is no "shared immutable", because that is just immutable
* Naked == no MOD bits
*/
Type cto; // MODconst ? naked version of this type : const version
Type ito; // MODimmutable ? naked version of this type : immutable version
Type sto; // MODshared ? naked version of this type : shared mutable version
Type scto; // MODshared | MODconst ? naked version of this type : shared const version
Type wto; // MODwild ? naked version of this type : wild version
Type wcto; // MODwildconst ? naked version of this type : wild const version
Type swto; // MODshared | MODwild ? naked version of this type : shared wild version
Type swcto; // MODshared | MODwildconst ? naked version of this type : shared wild const version
Type pto; // merged pointer to this type
Type rto; // reference to this type
Type arrayof; // array of this type
TypeInfoDeclaration vtinfo; // TypeInfo object for this Type
type* ctype; // for back end
extern (C++) static __gshared Type tvoid;
extern (C++) static __gshared Type tint8;
extern (C++) static __gshared Type tuns8;
extern (C++) static __gshared Type tint16;
extern (C++) static __gshared Type tuns16;
extern (C++) static __gshared Type tint32;
extern (C++) static __gshared Type tuns32;
extern (C++) static __gshared Type tint64;
extern (C++) static __gshared Type tuns64;
extern (C++) static __gshared Type tint128;
extern (C++) static __gshared Type tuns128;
extern (C++) static __gshared Type tfloat32;
extern (C++) static __gshared Type tfloat64;
extern (C++) static __gshared Type tfloat80;
extern (C++) static __gshared Type timaginary32;
extern (C++) static __gshared Type timaginary64;
extern (C++) static __gshared Type timaginary80;
extern (C++) static __gshared Type tcomplex32;
extern (C++) static __gshared Type tcomplex64;
extern (C++) static __gshared Type tcomplex80;
extern (C++) static __gshared Type tbool;
extern (C++) static __gshared Type tchar;
extern (C++) static __gshared Type twchar;
extern (C++) static __gshared Type tdchar;
// Some special types
extern (C++) static __gshared Type tshiftcnt;
extern (C++) static __gshared Type tvoidptr; // void*
extern (C++) static __gshared Type tstring; // immutable(char)[]
extern (C++) static __gshared Type twstring; // immutable(wchar)[]
extern (C++) static __gshared Type tdstring; // immutable(dchar)[]
extern (C++) static __gshared Type tvalist; // va_list alias
extern (C++) static __gshared Type terror; // for error recovery
extern (C++) static __gshared Type tnull; // for null type
extern (C++) static __gshared Type tsize_t; // matches size_t alias
extern (C++) static __gshared Type tptrdiff_t; // matches ptrdiff_t alias
extern (C++) static __gshared Type thash_t; // matches hash_t alias
extern (C++) static __gshared ClassDeclaration dtypeinfo;
extern (C++) static __gshared ClassDeclaration typeinfoclass;
extern (C++) static __gshared ClassDeclaration typeinfointerface;
extern (C++) static __gshared ClassDeclaration typeinfostruct;
extern (C++) static __gshared ClassDeclaration typeinfopointer;
extern (C++) static __gshared ClassDeclaration typeinfoarray;
extern (C++) static __gshared ClassDeclaration typeinfostaticarray;
extern (C++) static __gshared ClassDeclaration typeinfoassociativearray;
extern (C++) static __gshared ClassDeclaration typeinfovector;
extern (C++) static __gshared ClassDeclaration typeinfoenum;
extern (C++) static __gshared ClassDeclaration typeinfofunction;
extern (C++) static __gshared ClassDeclaration typeinfodelegate;
extern (C++) static __gshared ClassDeclaration typeinfotypelist;
extern (C++) static __gshared ClassDeclaration typeinfoconst;
extern (C++) static __gshared ClassDeclaration typeinfoinvariant;
extern (C++) static __gshared ClassDeclaration typeinfoshared;
extern (C++) static __gshared ClassDeclaration typeinfowild;
extern (C++) static __gshared TemplateDeclaration rtinfo;
extern (C++) static __gshared Type[TMAX] basic;
extern (C++) static __gshared StringTable stringtable;
extern (C++) static __gshared ubyte[TMAX] sizeTy = ()
{
ubyte[TMAX] sizeTy = __traits(classInstanceSize, TypeBasic);
sizeTy[Tsarray] = __traits(classInstanceSize, TypeSArray);
sizeTy[Tarray] = __traits(classInstanceSize, TypeDArray);
sizeTy[Taarray] = __traits(classInstanceSize, TypeAArray);
sizeTy[Tpointer] = __traits(classInstanceSize, TypePointer);
sizeTy[Treference] = __traits(classInstanceSize, TypeReference);
sizeTy[Tfunction] = __traits(classInstanceSize, TypeFunction);
sizeTy[Tdelegate] = __traits(classInstanceSize, TypeDelegate);
sizeTy[Tident] = __traits(classInstanceSize, TypeIdentifier);
sizeTy[Tinstance] = __traits(classInstanceSize, TypeInstance);
sizeTy[Ttypeof] = __traits(classInstanceSize, TypeTypeof);
sizeTy[Tenum] = __traits(classInstanceSize, TypeEnum);
sizeTy[Tstruct] = __traits(classInstanceSize, TypeStruct);
sizeTy[Tclass] = __traits(classInstanceSize, TypeClass);
sizeTy[Ttuple] = __traits(classInstanceSize, TypeTuple);
sizeTy[Tslice] = __traits(classInstanceSize, TypeSlice);
sizeTy[Treturn] = __traits(classInstanceSize, TypeReturn);
sizeTy[Terror] = __traits(classInstanceSize, TypeError);
sizeTy[Tnull] = __traits(classInstanceSize, TypeNull);
sizeTy[Tvector] = __traits(classInstanceSize, TypeVector);
return sizeTy;
}();
final extern (D) this(TY ty)
{
this.ty = ty;
}
const(char)* kind() const
{
assert(false); // should be overridden
}
final Type copy()
{
Type t = cast(Type)mem.xmalloc(sizeTy[ty]);
memcpy(cast(void*)t, cast(void*)this, sizeTy[ty]);
return t;
}
Type syntaxCopy()
{
print();
fprintf(stderr, "ty = %d\n", ty);
assert(0);
}
override bool equals(RootObject o)
{
Type t = cast(Type)o;
//printf("Type::equals(%s, %s)\n", toChars(), t.toChars());
// deco strings are unique
// and semantic() has been run
if (this == o || ((t && deco == t.deco) && deco !is null))
{
//printf("deco = '%s', t.deco = '%s'\n", deco, t.deco);
return true;
}
//if (deco && t && t.deco) printf("deco = '%s', t.deco = '%s'\n", deco, t.deco);
return false;
}
final bool equivalent(Type t)
{
return immutableOf().equals(t.immutableOf());
}
// kludge for template.isType()
override final DYNCAST dyncast() const
{
return DYNCAST.type;
}
/*******************************
* Covariant means that 'this' can substitute for 't',
* i.e. a pure function is a match for an impure type.
* Params:
* t = type 'this' is covariant with
* pstc = if not null, store STCxxxx which would make it covariant
* fix17349 = enable fix https://issues.dlang.org/show_bug.cgi?id=17349
* Returns:
* 0 types are distinct
* 1 this is covariant with t
* 2 arguments match as far as overloading goes,
* but types are not covariant
* 3 cannot determine covariance because of forward references
* *pstc STCxxxx which would make it covariant
*/
final int covariant(Type t, StorageClass* pstc = null, bool fix17349 = true)
{
version (none)
{
printf("Type::covariant(t = %s) %s\n", t.toChars(), toChars());
printf("deco = %p, %p\n", deco, t.deco);
// printf("ty = %d\n", next.ty);
printf("mod = %x, %x\n", mod, t.mod);
}
if (pstc)
*pstc = 0;
StorageClass stc = 0;
bool notcovariant = false;
TypeFunction t1;
TypeFunction t2;
if (equals(t))
return 1; // covariant
if (ty != Tfunction || t.ty != Tfunction)
goto Ldistinct;
t1 = cast(TypeFunction)this;
t2 = cast(TypeFunction)t;
if (t1.varargs != t2.varargs)
goto Ldistinct;
if (t1.parameters && t2.parameters)
{
size_t dim = Parameter.dim(t1.parameters);
if (dim != Parameter.dim(t2.parameters))
goto Ldistinct;
for (size_t i = 0; i < dim; i++)
{
Parameter fparam1 = Parameter.getNth(t1.parameters, i);
Parameter fparam2 = Parameter.getNth(t2.parameters, i);
if (!fparam1.type.equals(fparam2.type))
{
if (!fix17349)
goto Ldistinct;
Type tp1 = fparam1.type;
Type tp2 = fparam2.type;
if (tp1.ty == tp2.ty)
{
if (tp1.ty == Tclass)
{
if ((cast(TypeClass)tp1).sym == (cast(TypeClass)tp2).sym && MODimplicitConv(tp2.mod, tp1.mod))
goto Lcov;
}
else if (tp1.ty == Tstruct)
{
if ((cast(TypeStruct)tp1).sym == (cast(TypeStruct)tp2).sym && MODimplicitConv(tp2.mod, tp1.mod))
goto Lcov;
}
else if (tp1.ty == Tpointer)
{
if (tp2.implicitConvTo(tp1))
goto Lcov;
}
else if (tp1.ty == Tarray)
{
if (tp2.implicitConvTo(tp1))
goto Lcov;
}
else if (tp1.ty == Tdelegate)
{
if (tp1.implicitConvTo(tp2))
goto Lcov;
}
}
goto Ldistinct;
}
Lcov:
notcovariant |= !fparam1.isCovariant(t1.isref, fparam2);
}
}
else if (t1.parameters != t2.parameters)
{
size_t dim1 = !t1.parameters ? 0 : t1.parameters.dim;
size_t dim2 = !t2.parameters ? 0 : t2.parameters.dim;
if (dim1 || dim2)
goto Ldistinct;
}
// The argument lists match
if (notcovariant)
goto Lnotcovariant;
if (t1.linkage != t2.linkage)
goto Lnotcovariant;
{
// Return types
Type t1n = t1.next;
Type t2n = t2.next;
if (!t1n || !t2n) // happens with return type inference
goto Lnotcovariant;
if (t1n.equals(t2n))
goto Lcovariant;
if (t1n.ty == Tclass && t2n.ty == Tclass)
{
/* If same class type, but t2n is const, then it's
* covariant. Do this test first because it can work on
* forward references.
*/
if ((cast(TypeClass)t1n).sym == (cast(TypeClass)t2n).sym && MODimplicitConv(t1n.mod, t2n.mod))
goto Lcovariant;
// If t1n is forward referenced:
ClassDeclaration cd = (cast(TypeClass)t1n).sym;
if (cd.semanticRun < PASSsemanticdone)
cd.semantic(null);
if (!cd.isBaseInfoComplete())
{
return 3; // forward references
}
}
if (t1n.ty == Tstruct && t2n.ty == Tstruct)
{
if ((cast(TypeStruct)t1n).sym == (cast(TypeStruct)t2n).sym && MODimplicitConv(t1n.mod, t2n.mod))
goto Lcovariant;
}
else if (t1n.ty == t2n.ty && t1n.implicitConvTo(t2n))
goto Lcovariant;
else if (t1n.ty == Tnull && t1n.implicitConvTo(t2n) && t1n.size() == t2n.size())
goto Lcovariant;
}
goto Lnotcovariant;
Lcovariant:
if (t1.isref != t2.isref)
goto Lnotcovariant;
if (!t1.isref && (t1.isscope || t2.isscope))
{
StorageClass stc1 = t1.isscope ? STCscope : 0;
StorageClass stc2 = t2.isscope ? STCscope : 0;
if (t1.isreturn)
{
stc1 |= STCreturn;
if (!t1.isscope)
stc1 |= STCref;
}
if (t2.isreturn)
{
stc2 |= STCreturn;
if (!t2.isscope)
stc2 |= STCref;
}
if (!Parameter.isCovariantScope(t1.isref, stc1, stc2))
goto Lnotcovariant;
}
// We can subtract 'return ref' from 'this', but cannot add it
else if (t1.isreturn && !t2.isreturn)
goto Lnotcovariant;
/* Can convert mutable to const
*/
if (!MODimplicitConv(t2.mod, t1.mod))
{
version (none)
{
//stop attribute inference with const
// If adding 'const' will make it covariant
if (MODimplicitConv(t2.mod, MODmerge(t1.mod, MODconst)))
stc |= STCconst;
else
goto Lnotcovariant;
}
else
{
goto Ldistinct;
}
}
/* Can convert pure to impure, nothrow to throw, and nogc to gc
*/
if (!t1.purity && t2.purity)
stc |= STCpure;
if (!t1.isnothrow && t2.isnothrow)
stc |= STCnothrow;
if (!t1.isnogc && t2.isnogc)
stc |= STCnogc;
/* Can convert safe/trusted to system
*/
if (t1.trust <= TRUSTsystem && t2.trust >= TRUSTtrusted)
{
// Should we infer trusted or safe? Go with safe.
stc |= STCsafe;
}
if (stc)
{
if (pstc)
*pstc = stc;
goto Lnotcovariant;
}
//printf("\tcovaraint: 1\n");
return 1;
Ldistinct:
//printf("\tcovaraint: 0\n");
return 0;
Lnotcovariant:
//printf("\tcovaraint: 2\n");
return 2;
}
/********************************
* For pretty-printing a type.
*/
final override const(char)* toChars()
{
OutBuffer buf;
buf.reserve(16);
HdrGenState hgs;
hgs.fullQual = (ty == Tclass && !mod);
.toCBuffer(this, &buf, null, &hgs);
return buf.extractString();
}
final char* toPrettyChars(bool QualifyTypes = false)
{
OutBuffer buf;
buf.reserve(16);
HdrGenState hgs;
hgs.fullQual = QualifyTypes;
.toCBuffer(this, &buf, null, &hgs);
return buf.extractString();
}
static void _init()
{
stringtable._init(14000);
// Set basic types
static __gshared TY* basetab =
[
Tvoid,
Tint8,
Tuns8,
Tint16,
Tuns16,
Tint32,
Tuns32,
Tint64,
Tuns64,
Tint128,
Tuns128,
Tfloat32,
Tfloat64,
Tfloat80,
Timaginary32,
Timaginary64,
Timaginary80,
Tcomplex32,
Tcomplex64,
Tcomplex80,
Tbool,
Tchar,
Twchar,
Tdchar,
Terror
];
for (size_t i = 0; basetab[i] != Terror; i++)
{
Type t = new TypeBasic(basetab[i]);
t = t.merge();
basic[basetab[i]] = t;
}
basic[Terror] = new TypeError();
tvoid = basic[Tvoid];
tint8 = basic[Tint8];
tuns8 = basic[Tuns8];
tint16 = basic[Tint16];
tuns16 = basic[Tuns16];
tint32 = basic[Tint32];
tuns32 = basic[Tuns32];
tint64 = basic[Tint64];
tuns64 = basic[Tuns64];
tint128 = basic[Tint128];
tuns128 = basic[Tuns128];
tfloat32 = basic[Tfloat32];
tfloat64 = basic[Tfloat64];
tfloat80 = basic[Tfloat80];
timaginary32 = basic[Timaginary32];
timaginary64 = basic[Timaginary64];
timaginary80 = basic[Timaginary80];
tcomplex32 = basic[Tcomplex32];
tcomplex64 = basic[Tcomplex64];
tcomplex80 = basic[Tcomplex80];
tbool = basic[Tbool];
tchar = basic[Tchar];
twchar = basic[Twchar];
tdchar = basic[Tdchar];
tshiftcnt = tint32;
terror = basic[Terror];
tnull = basic[Tnull];
tnull = new TypeNull();
tnull.deco = tnull.merge().deco;
tvoidptr = tvoid.pointerTo();
tstring = tchar.immutableOf().arrayOf();
twstring = twchar.immutableOf().arrayOf();
tdstring = tdchar.immutableOf().arrayOf();
tvalist = Target.va_listType();
if (global.params.isLP64)
{
Tsize_t = Tuns64;
Tptrdiff_t = Tint64;
}
else
{
Tsize_t = Tuns32;
Tptrdiff_t = Tint32;
}
tsize_t = basic[Tsize_t];