-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathmtype.d
4972 lines (4374 loc) · 136 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
/**
* Defines a D type.
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/mtype.d, _mtype.d)
* Documentation: https://dlang.org/phobos/dmd_mtype.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/mtype.d
*/
module dmd.mtype;
import core.checkedint;
import core.stdc.stdarg;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.ast_node;
import dmd.dcast : implicitConvTo;
import dmd.dclass;
import dmd.declaration;
import dmd.denum;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.enumsem;
import dmd.errors;
import dmd.expression;
import dmd.globals;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.location;
import dmd.root.ctfloat;
import dmd.common.outbuffer;
import dmd.root.rmem;
import dmd.rootobject;
import dmd.root.stringtable;
import dmd.target;
import dmd.tokens;
import dmd.typesem;
import dmd.visitor;
enum LOGDOTEXP = 0; // log ::dotExp()
enum LOGDEFAULTINIT = 0; // log ::defaultInit()
enum SIZE_INVALID = (~cast(uinteger_t)0); // error return from size() functions
static if (__VERSION__ < 2095)
{
// Fix linker errors when building with older compilers.
// See: https://issues.dlang.org/show_bug.cgi?id=21299
private alias StringValueType = StringValue!Type;
}
/***************************
* 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 & ~MODFlags.shared_, modto & ~MODFlags.shared_))
{
case X(0, MODFlags.const_):
case X(MODFlags.wild, MODFlags.const_):
case X(MODFlags.wild, MODFlags.wildconst):
case X(MODFlags.wildconst, MODFlags.const_):
return (modfrom & MODFlags.shared_) == (modto & MODFlags.shared_);
case X(MODFlags.immutable_, MODFlags.const_):
case X(MODFlags.immutable_, MODFlags.wildconst):
return true;
default:
return false;
}
}
/***************************
* Return MATCH.exact or MATCH.constant 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 MATCH.exact;
if (MODimplicitConv(modfrom, modto))
return MATCH.constant;
auto X(T, U)(T m, U n)
{
return ((m << 4) | n);
}
switch (X(modfrom, modto))
{
case X(0, MODFlags.wild):
case X(MODFlags.immutable_, MODFlags.wild):
case X(MODFlags.const_, MODFlags.wild):
case X(MODFlags.wildconst, MODFlags.wild):
case X(MODFlags.shared_, MODFlags.shared_ | MODFlags.wild):
case X(MODFlags.shared_ | MODFlags.immutable_, MODFlags.shared_ | MODFlags.wild):
case X(MODFlags.shared_ | MODFlags.const_, MODFlags.shared_ | MODFlags.wild):
case X(MODFlags.shared_ | MODFlags.wildconst, MODFlags.shared_ | MODFlags.wild):
return MATCH.constant;
default:
return MATCH.nomatch;
}
}
/***************************
* 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) & MODFlags.shared_)
{
// If either type is shared, the result will be shared
result |= MODFlags.shared_;
mod1 &= ~MODFlags.shared_;
mod2 &= ~MODFlags.shared_;
}
if (mod1 == 0 || mod1 == MODFlags.mutable || mod1 == MODFlags.const_ || mod2 == 0 || mod2 == MODFlags.mutable || mod2 == MODFlags.const_)
{
// If either type is mutable or const, the result will be const.
result |= MODFlags.const_;
}
else
{
// MODFlags.immutable_ vs MODFlags.wild
// MODFlags.immutable_ vs MODFlags.wildconst
// MODFlags.wild vs MODFlags.wildconst
assert(mod1 & MODFlags.wild || mod2 & MODFlags.wild);
result |= MODFlags.wildconst;
}
return result;
}
/*********************************
* Store modifier name into buf.
*/
void MODtoBuffer(ref OutBuffer buf, MOD mod) nothrow @safe
{
buf.writestring(MODtoString(mod));
}
/*********************************
* Returns:
* a human readable representation of `mod`,
* which is the token `mod` corresponds to
*/
const(char)* MODtoChars(MOD mod) nothrow pure
{
/// Works because we return a literal
return MODtoString(mod).ptr;
}
/// Ditto
string MODtoString(MOD mod) nothrow pure @safe
{
final switch (mod)
{
case 0:
return "";
case MODFlags.immutable_:
return "immutable";
case MODFlags.shared_:
return "shared";
case MODFlags.shared_ | MODFlags.const_:
return "shared const";
case MODFlags.const_:
return "const";
case MODFlags.shared_ | MODFlags.wild:
return "shared inout";
case MODFlags.wild:
return "inout";
case MODFlags.shared_ | MODFlags.wildconst:
return "shared inout const";
case MODFlags.wildconst:
return "inout const";
}
}
/*************************************************
* Pick off one of the trust flags from trust,
* and return a string representation of it.
*/
string trustToString(TRUST trust) pure nothrow @nogc @safe
{
final switch (trust)
{
case TRUST.default_:
return null;
case TRUST.system:
return "@system";
case TRUST.trusted:
return "@trusted";
case TRUST.safe:
return "@safe";
}
}
unittest
{
assert(trustToString(TRUST.default_) == "");
assert(trustToString(TRUST.system) == "@system");
assert(trustToString(TRUST.trusted) == "@trusted");
assert(trustToString(TRUST.safe) == "@safe");
}
/************************************
* Convert MODxxxx to STCxxx
*/
StorageClass ModToStc(uint mod) pure nothrow @nogc @safe
{
StorageClass stc = 0;
if (mod & MODFlags.immutable_)
stc |= STC.immutable_;
if (mod & MODFlags.const_)
stc |= STC.const_;
if (mod & MODFlags.wild)
stc |= STC.wild;
if (mod & MODFlags.shared_)
stc |= STC.shared_;
return stc;
}
///Returns true if ty is char, wchar, or dchar
bool isSomeChar(TY ty) pure nothrow @nogc @safe
{
return ty == Tchar || ty == Twchar || ty == Tdchar;
}
/****************
* dotExp() bit flags
*/
enum DotExpFlag
{
none = 0,
gag = 1, // don't report "not a property" error and just return null
noDeref = 2, // the use of the expression will not attempt a dereference
noAliasThis = 4, // don't do 'alias this' resolution
}
/// Result of a check whether two types are covariant
enum Covariant
{
distinct = 0, /// types are distinct
yes = 1, /// types are covariant
no = 2, /// arguments match as far as overloading goes, but types are not covariant
fwdref = 3, /// cannot determine covariance because of forward references
}
/***********************************************************
*/
extern (C++) abstract class Type : ASTNode
{
TY ty;
MOD mod; // modifiers MODxxxx
char* deco;
static struct Mcache
{
/* These are cached values that are lazily evaluated by constOf(), immutableOf(), etc.
* They should not be referenced by anybody but mtype.d.
* They can be null if not lazily evaluated yet.
* Note that there is no "shared immutable", because that is just immutable
* The point of this is to reduce the size of each Type instance as
* we bank on the idea that usually only one of variants exist.
* It will also speed up code because these are rarely referenced and
* so need not be in the cache.
*/
Type cto; // MODFlags.const_
Type ito; // MODFlags.immutable_
Type sto; // MODFlags.shared_
Type scto; // MODFlags.shared_ | MODFlags.const_
Type wto; // MODFlags.wild
Type wcto; // MODFlags.wildconst
Type swto; // MODFlags.shared_ | MODFlags.wild
Type swcto; // MODFlags.shared_ | MODFlags.wildconst
}
Mcache* mcache;
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
void* ctype; // for back end
extern (C++) __gshared Type tvoid;
extern (C++) __gshared Type tint8;
extern (C++) __gshared Type tuns8;
extern (C++) __gshared Type tint16;
extern (C++) __gshared Type tuns16;
extern (C++) __gshared Type tint32;
extern (C++) __gshared Type tuns32;
extern (C++) __gshared Type tint64;
extern (C++) __gshared Type tuns64;
extern (C++) __gshared Type tint128;
extern (C++) __gshared Type tuns128;
extern (C++) __gshared Type tfloat32;
extern (C++) __gshared Type tfloat64;
extern (C++) __gshared Type tfloat80;
extern (C++) __gshared Type timaginary32;
extern (C++) __gshared Type timaginary64;
extern (C++) __gshared Type timaginary80;
extern (C++) __gshared Type tcomplex32;
extern (C++) __gshared Type tcomplex64;
extern (C++) __gshared Type tcomplex80;
extern (C++) __gshared Type tbool;
extern (C++) __gshared Type tchar;
extern (C++) __gshared Type twchar;
extern (C++) __gshared Type tdchar;
// Some special types
extern (C++) __gshared Type tshiftcnt;
extern (C++) __gshared Type tvoidptr; // void*
extern (C++) __gshared Type tstring; // immutable(char)[]
extern (C++) __gshared Type twstring; // immutable(wchar)[]
extern (C++) __gshared Type tdstring; // immutable(dchar)[]
extern (C++) __gshared Type terror; // for error recovery
extern (C++) __gshared Type tnull; // for null type
extern (C++) __gshared Type tnoreturn; // for bottom type typeof(*null)
extern (C++) __gshared Type tsize_t; // matches size_t alias
extern (C++) __gshared Type tptrdiff_t; // matches ptrdiff_t alias
extern (C++) __gshared Type thash_t; // matches hash_t alias
extern (C++) __gshared ClassDeclaration dtypeinfo;
extern (C++) __gshared ClassDeclaration typeinfoclass;
extern (C++) __gshared ClassDeclaration typeinfointerface;
extern (C++) __gshared ClassDeclaration typeinfostruct;
extern (C++) __gshared ClassDeclaration typeinfopointer;
extern (C++) __gshared ClassDeclaration typeinfoarray;
extern (C++) __gshared ClassDeclaration typeinfostaticarray;
extern (C++) __gshared ClassDeclaration typeinfoassociativearray;
extern (C++) __gshared ClassDeclaration typeinfovector;
extern (C++) __gshared ClassDeclaration typeinfoenum;
extern (C++) __gshared ClassDeclaration typeinfofunction;
extern (C++) __gshared ClassDeclaration typeinfodelegate;
extern (C++) __gshared ClassDeclaration typeinfotypelist;
extern (C++) __gshared ClassDeclaration typeinfoconst;
extern (C++) __gshared ClassDeclaration typeinfoinvariant;
extern (C++) __gshared ClassDeclaration typeinfoshared;
extern (C++) __gshared ClassDeclaration typeinfowild;
extern (C++) __gshared TemplateDeclaration rtinfo;
version (IN_LLVM)
{
extern (C++) __gshared TemplateDeclaration rtinfoImpl;
}
extern (C++) __gshared Type[TMAX] basic;
extern (D) __gshared StringTable!Type stringtable;
extern (D) private static immutable 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);
sizeTy[Ttraits] = __traits(classInstanceSize, TypeTraits);
sizeTy[Tmixin] = __traits(classInstanceSize, TypeMixin);
sizeTy[Tnoreturn] = __traits(classInstanceSize, TypeNoreturn);
sizeTy[Ttag] = __traits(classInstanceSize, TypeTag);
return sizeTy;
}();
final extern (D) this(TY ty) scope @safe
{
this.ty = ty;
}
const(char)* kind() const nothrow pure @nogc @safe
{
assert(false); // should be overridden
}
final Type copy() nothrow const
{
Type t = cast(Type)mem.xmalloc(sizeTy[ty]);
memcpy(cast(void*)t, cast(void*)this, sizeTy[ty]);
return t;
}
Type syntaxCopy()
{
fprintf(stderr, "this = %s, ty = %d\n", toChars(), ty);
assert(0);
}
override bool equals(const RootObject o) const
{
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;
}
// kludge for template.isType()
override final DYNCAST dyncast() const
{
return DYNCAST.type;
}
/// Returns a non-zero unique ID for this Type, or returns 0 if the Type does not (yet) have a unique ID.
/// If `semantic()` has not been run, 0 is returned.
final size_t getUniqueID() const
{
return cast(size_t) deco;
}
extern (D)
final Mcache* getMcache()
{
if (!mcache)
mcache = cast(Mcache*) mem.xcalloc(Mcache.sizeof, 1);
return mcache;
}
/********************************
* For pretty-printing a type.
*/
final override const(char)* toChars() const
{
return dmd.hdrgen.toChars(this);
}
/// ditto
final char* toPrettyChars(bool QualifyTypes = false)
{
OutBuffer buf;
buf.reserve(16);
HdrGenState hgs;
hgs.fullQual = QualifyTypes;
toCBuffer(this, buf, null, hgs);
return buf.extractChars();
}
static void _init()
{
stringtable._init(14_000);
// Set basic types
__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
];
static Type merge(Type t)
{
import dmd.basicmangle : tyToDecoBuffer;
OutBuffer buf;
buf.reserve(3);
if (t.ty == Tnoreturn)
buf.writestring("Nn");
else
tyToDecoBuffer(buf, t.ty);
auto sv = t.stringtable.update(buf[]);
if (sv.value)
return sv.value;
else
{
t.deco = cast(char*)sv.toDchars();
sv.value = t;
return t;
}
}
for (size_t i = 0; basetab[i] != Terror; i++)
{
Type t = new TypeBasic(basetab[i]);
t = merge(t);
basic[basetab[i]] = t;
}
basic[Terror] = new TypeError();
tnoreturn = new TypeNoreturn();
tnoreturn.deco = merge(tnoreturn).deco;
basic[Tnoreturn] = tnoreturn;
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];
tnoreturn = basic[Tnoreturn];
tnull = new TypeNull();
tnull.deco = merge(tnull).deco;
tvoidptr = tvoid.pointerTo();
tstring = tchar.immutableOf().arrayOf();
twstring = twchar.immutableOf().arrayOf();
tdstring = tdchar.immutableOf().arrayOf();
const isLP64 = target.isLP64;
tsize_t = basic[isLP64 ? Tuns64 : Tuns32];
tptrdiff_t = basic[isLP64 ? Tint64 : Tint32];
thash_t = tsize_t;
static if (__VERSION__ == 2081)
{
// Related issue: https://issues.dlang.org/show_bug.cgi?id=19134
// D 2.081.x regressed initializing class objects at compile time.
// As a workaround initialize this global at run-time instead.
TypeTuple.empty = new TypeTuple();
}
}
/**
* Deinitializes the global state of the compiler.
*
* This can be used to restore the state set by `_init` to its original
* state.
*/
static void deinitialize()
{
stringtable = stringtable.init;
}
uint alignsize()
{
return cast(uint)size(this, Loc.initial);
}
/*********************************
* Store this type's modifier name into buf.
*/
final void modToBuffer(ref OutBuffer buf) nothrow const
{
if (mod)
{
buf.writeByte(' ');
MODtoBuffer(buf, mod);
}
}
/*********************************
* Return this type's modifier name.
*/
final char* modToChars() nothrow const
{
OutBuffer buf;
buf.reserve(16);
modToBuffer(buf);
return buf.extractChars();
}
bool isintegral()
{
return false;
}
// real, imaginary, or complex
bool isfloating()
{
return false;
}
bool isreal()
{
return false;
}
bool isimaginary()
{
return false;
}
bool iscomplex()
{
return false;
}
bool isscalar()
{
return false;
}
bool isunsigned()
{
return false;
}
bool isscope()
{
return false;
}
bool isString()
{
return false;
}
/**************************
* When T is mutable,
* Given:
* T a, b;
* Can we bitwise assign:
* a = b;
* ?
*/
bool isAssignable()
{
return true;
}
/**************************
* Returns true if T can be converted to boolean value.
*/
bool isBoolean()
{
return isscalar();
}
final bool isConst() const nothrow pure @nogc @safe
{
return (mod & MODFlags.const_) != 0;
}
final bool isImmutable() const nothrow pure @nogc @safe
{
return (mod & MODFlags.immutable_) != 0;
}
final bool isMutable() const nothrow pure @nogc @safe
{
return (mod & (MODFlags.const_ | MODFlags.immutable_ | MODFlags.wild)) == 0;
}
final bool isShared() const nothrow pure @nogc @safe
{
return (mod & MODFlags.shared_) != 0;
}
final bool isSharedConst() const nothrow pure @nogc @safe
{
return (mod & (MODFlags.shared_ | MODFlags.const_)) == (MODFlags.shared_ | MODFlags.const_);
}
final bool isWild() const nothrow pure @nogc @safe
{
return (mod & MODFlags.wild) != 0;
}
final bool isWildConst() const nothrow pure @nogc @safe
{
return (mod & MODFlags.wildconst) == MODFlags.wildconst;
}
final bool isSharedWild() const nothrow pure @nogc @safe
{
return (mod & (MODFlags.shared_ | MODFlags.wild)) == (MODFlags.shared_ | MODFlags.wild);
}
final bool isNaked() const nothrow pure @nogc @safe
{
return mod == 0;
}
/********************************
* Return a copy of this type with all attributes null-initialized.
* Useful for creating a type with different modifiers.
*/
final Type nullAttributes() nothrow const
{
uint sz = sizeTy[ty];
Type t = cast(Type)mem.xmalloc(sz);
memcpy(cast(void*)t, cast(void*)this, sz);
// t.mod = NULL; // leave mod unchanged
t.deco = null;
t.arrayof = null;
t.pto = null;
t.rto = null;
t.vtinfo = null;
t.ctype = null;
t.mcache = null;
if (t.ty == Tstruct)
(cast(TypeStruct)t).att = AliasThisRec.fwdref;
if (t.ty == Tclass)
(cast(TypeClass)t).att = AliasThisRec.fwdref;
return t;
}
/**********************************
* For our new type 'this', which is type-constructed from t,
* fill in the cto, ito, sto, scto, wto shortcuts.
*/
extern (D) final void fixTo(Type t)
{
// If fixing this: immutable(T*) by t: immutable(T)*,
// cache t to this.xto won't break transitivity.
Type mto = null;
Type tn = nextOf();
if (!tn || ty != Tsarray && tn.mod == t.nextOf().mod)
{
switch (t.mod)
{
case 0:
mto = t;
break;
case MODFlags.const_:
getMcache();
mcache.cto = t;
break;
case MODFlags.wild:
getMcache();
mcache.wto = t;
break;
case MODFlags.wildconst:
getMcache();
mcache.wcto = t;
break;
case MODFlags.shared_:
getMcache();
mcache.sto = t;
break;
case MODFlags.shared_ | MODFlags.const_:
getMcache();
mcache.scto = t;
break;
case MODFlags.shared_ | MODFlags.wild:
getMcache();
mcache.swto = t;
break;
case MODFlags.shared_ | MODFlags.wildconst:
getMcache();
mcache.swcto = t;
break;
case MODFlags.immutable_:
getMcache();
mcache.ito = t;
break;
default:
break;
}
}
assert(mod != t.mod);
if (mod)
{
getMcache();
t.getMcache();
}
switch (mod)
{
case 0:
break;
case MODFlags.const_:
mcache.cto = mto;
t.mcache.cto = this;
break;
case MODFlags.wild:
mcache.wto = mto;
t.mcache.wto = this;
break;
case MODFlags.wildconst:
mcache.wcto = mto;
t.mcache.wcto = this;
break;
case MODFlags.shared_:
mcache.sto = mto;
t.mcache.sto = this;
break;
case MODFlags.shared_ | MODFlags.const_:
mcache.scto = mto;
t.mcache.scto = this;
break;
case MODFlags.shared_ | MODFlags.wild:
mcache.swto = mto;
t.mcache.swto = this;
break;
case MODFlags.shared_ | MODFlags.wildconst:
mcache.swcto = mto;
t.mcache.swcto = this;
break;
case MODFlags.immutable_:
t.mcache.ito = this;
if (t.mcache.cto)
t.mcache.cto.getMcache().ito = this;
if (t.mcache.sto)
t.mcache.sto.getMcache().ito = this;
if (t.mcache.scto)
t.mcache.scto.getMcache().ito = this;
if (t.mcache.wto)
t.mcache.wto.getMcache().ito = this;
if (t.mcache.wcto)
t.mcache.wcto.getMcache().ito = this;
if (t.mcache.swto)
t.mcache.swto.getMcache().ito = this;
if (t.mcache.swcto)
t.mcache.swcto.getMcache().ito = this;
break;
default:
assert(0);
}
check();
t.check();
//printf("fixTo: %s, %s\n", toChars(), t.toChars());
}
/***************************
* Look for bugs in constructing types.
*/
extern (D) final void check()
{
if (mcache)
with (mcache)
switch (mod)
{
case 0:
if (cto)
assert(cto.mod == MODFlags.const_);
if (ito)
assert(ito.mod == MODFlags.immutable_);
if (sto)
assert(sto.mod == MODFlags.shared_);
if (scto)
assert(scto.mod == (MODFlags.shared_ | MODFlags.const_));
if (wto)
assert(wto.mod == MODFlags.wild);
if (wcto)
assert(wcto.mod == MODFlags.wildconst);
if (swto)
assert(swto.mod == (MODFlags.shared_ | MODFlags.wild));
if (swcto)
assert(swcto.mod == (MODFlags.shared_ | MODFlags.wildconst));
break;
case MODFlags.const_:
if (cto)
assert(cto.mod == 0);
if (ito)
assert(ito.mod == MODFlags.immutable_);
if (sto)
assert(sto.mod == MODFlags.shared_);
if (scto)
assert(scto.mod == (MODFlags.shared_ | MODFlags.const_));
if (wto)
assert(wto.mod == MODFlags.wild);
if (wcto)
assert(wcto.mod == MODFlags.wildconst);
if (swto)
assert(swto.mod == (MODFlags.shared_ | MODFlags.wild));
if (swcto)
assert(swcto.mod == (MODFlags.shared_ | MODFlags.wildconst));
break;
case MODFlags.wild:
if (cto)
assert(cto.mod == MODFlags.const_);
if (ito)
assert(ito.mod == MODFlags.immutable_);
if (sto)
assert(sto.mod == MODFlags.shared_);
if (scto)
assert(scto.mod == (MODFlags.shared_ | MODFlags.const_));
if (wto)
assert(wto.mod == 0);
if (wcto)
assert(wcto.mod == MODFlags.wildconst);
if (swto)
assert(swto.mod == (MODFlags.shared_ | MODFlags.wild));
if (swcto)
assert(swcto.mod == (MODFlags.shared_ | MODFlags.wildconst));
break;
case MODFlags.wildconst:
assert(!cto || cto.mod == MODFlags.const_);
assert(!ito || ito.mod == MODFlags.immutable_);
assert(!sto || sto.mod == MODFlags.shared_);
assert(!scto || scto.mod == (MODFlags.shared_ | MODFlags.const_));
assert(!wto || wto.mod == MODFlags.wild);
assert(!wcto || wcto.mod == 0);
assert(!swto || swto.mod == (MODFlags.shared_ | MODFlags.wild));
assert(!swcto || swcto.mod == (MODFlags.shared_ | MODFlags.wildconst));
break;