-
-
Notifications
You must be signed in to change notification settings - Fork 612
/
Copy pathtraits.d
1182 lines (1061 loc) · 35.2 KB
/
traits.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
/**
* Contains traits for runtime internal usage.
*
* Copyright: Copyright Digital Mars 2014 -.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Martin Nowak
* Source: $(DRUNTIMESRC core/internal/_traits.d)
*/
module core.internal.traits;
alias AliasSeq(TList...) = TList;
template Fields(T)
{
static if (is(T == struct) || is(T == union))
alias Fields = typeof(T.tupleof[0 .. $ - __traits(isNested, T)]);
else static if (is(T == class) || is(T == interface))
alias Fields = typeof(T.tupleof);
else
alias Fields = AliasSeq!T;
}
T trustedCast(T, U)(auto ref U u) @trusted pure nothrow
{
return cast(T)u;
}
alias Unconst(T : const U, U) = U;
/// taken from std.traits.Unqual
template Unqual(T : const U, U)
{
static if (is(U == shared V, V))
alias Unqual = V;
else
alias Unqual = U;
}
template BaseElemOf(T)
{
static if (is(OriginalType!T == E[N], E, size_t N))
alias BaseElemOf = BaseElemOf!E;
else
alias BaseElemOf = T;
}
unittest
{
static assert(is(BaseElemOf!(int) == int));
static assert(is(BaseElemOf!(int[1]) == int));
static assert(is(BaseElemOf!(int[1][2]) == int));
static assert(is(BaseElemOf!(int[1][]) == int[1][]));
static assert(is(BaseElemOf!(int[][1]) == int[]));
enum E : int[2]{ test = [0, 1] }
static assert(is(BaseElemOf!(E) == int));
}
// [For internal use]
template ModifyTypePreservingTQ(alias Modifier, T)
{
static if (is(T U == immutable U)) alias ModifyTypePreservingTQ = immutable Modifier!U;
else static if (is(T U == shared inout const U)) alias ModifyTypePreservingTQ = shared inout const Modifier!U;
else static if (is(T U == shared inout U)) alias ModifyTypePreservingTQ = shared inout Modifier!U;
else static if (is(T U == shared const U)) alias ModifyTypePreservingTQ = shared const Modifier!U;
else static if (is(T U == shared U)) alias ModifyTypePreservingTQ = shared Modifier!U;
else static if (is(T U == inout const U)) alias ModifyTypePreservingTQ = inout const Modifier!U;
else static if (is(T U == inout U)) alias ModifyTypePreservingTQ = inout Modifier!U;
else static if (is(T U == const U)) alias ModifyTypePreservingTQ = const Modifier!U;
else alias ModifyTypePreservingTQ = Modifier!T;
}
@safe unittest
{
alias Intify(T) = int;
static assert(is(ModifyTypePreservingTQ!(Intify, real) == int));
static assert(is(ModifyTypePreservingTQ!(Intify, const real) == const int));
static assert(is(ModifyTypePreservingTQ!(Intify, inout real) == inout int));
static assert(is(ModifyTypePreservingTQ!(Intify, inout const real) == inout const int));
static assert(is(ModifyTypePreservingTQ!(Intify, shared real) == shared int));
static assert(is(ModifyTypePreservingTQ!(Intify, shared const real) == shared const int));
static assert(is(ModifyTypePreservingTQ!(Intify, shared inout real) == shared inout int));
static assert(is(ModifyTypePreservingTQ!(Intify, shared inout const real) == shared inout const int));
static assert(is(ModifyTypePreservingTQ!(Intify, immutable real) == immutable int));
}
// Substitute all `inout` qualifiers that appears in T to `const`
template substInout(T)
{
static if (is(T == immutable))
{
alias substInout = T;
}
else static if (is(T : shared const U, U) || is(T : const U, U))
{
// U is top-unqualified
mixin("alias substInout = "
~ (is(T == shared) ? "shared " : "")
~ (is(T == const) || is(T == inout) ? "const " : "") // substitute inout to const
~ "substInoutForm!U;");
}
else
static assert(0);
}
private template substInoutForm(T)
{
static if (is(T == struct) || is(T == class) || is(T == union) || is(T == interface))
{
alias substInoutForm = T; // prevent matching to the form of alias-this-ed type
}
else static if (is(T : V[K], K, V)) alias substInoutForm = substInout!V[substInout!K];
else static if (is(T : U[n], U, size_t n)) alias substInoutForm = substInout!U[n];
else static if (is(T : U[], U)) alias substInoutForm = substInout!U[];
else static if (is(T : U*, U)) alias substInoutForm = substInout!U*;
else alias substInoutForm = T;
}
/// used to declare an extern(D) function that is defined in a different module
template externDFunc(string fqn, T:FT*, FT) if (is(FT == function))
{
static if (is(FT RT == return) && is(FT Args == function))
{
import core.demangle : mangleFunc;
enum decl = {
string s = "extern(D) RT externDFunc(Args)";
foreach (attr; __traits(getFunctionAttributes, FT))
s ~= " " ~ attr;
return s ~ ";";
}();
pragma(mangle, mangleFunc!T(fqn)) mixin(decl);
}
else
static assert(0);
}
template staticIota(int beg, int end)
{
static if (beg + 1 >= end)
{
static if (beg >= end)
{
alias staticIota = AliasSeq!();
}
else
{
alias staticIota = AliasSeq!(+beg);
}
}
else
{
enum mid = beg + (end - beg) / 2;
alias staticIota = AliasSeq!(staticIota!(beg, mid), staticIota!(mid, end));
}
}
private struct __InoutWorkaroundStruct {}
@property T rvalueOf(T)(T val) { return val; }
@property T rvalueOf(T)(inout __InoutWorkaroundStruct = __InoutWorkaroundStruct.init);
@property ref T lvalueOf(T)(inout __InoutWorkaroundStruct = __InoutWorkaroundStruct.init);
// taken from std.traits.isAssignable
template isAssignable(Lhs, Rhs = Lhs)
{
enum isAssignable = __traits(compiles, lvalueOf!Lhs = rvalueOf!Rhs) && __traits(compiles, lvalueOf!Lhs = lvalueOf!Rhs);
}
// taken from std.traits.isInnerClass
template isInnerClass(T) if (is(T == class))
{
static if (is(typeof(T.outer)))
{
template hasOuterMember(T...)
{
static if (T.length == 0)
enum hasOuterMember = false;
else
enum hasOuterMember = T[0] == "outer" || hasOuterMember!(T[1 .. $]);
}
enum isInnerClass = __traits(isSame, typeof(T.outer), __traits(parent, T)) && !hasOuterMember!(__traits(allMembers, T));
}
else
enum isInnerClass = false;
}
template dtorIsNothrow(T)
{
enum dtorIsNothrow = is(typeof(function{T t=void;}) : void function() nothrow);
}
// taken from std.meta.allSatisfy
template allSatisfy(alias F, T...)
{
static foreach (Ti; T)
{
static if (!is(typeof(allSatisfy) == bool) && // not yet defined
!F!(Ti))
{
enum allSatisfy = false;
}
}
static if (!is(typeof(allSatisfy) == bool)) // if not yet defined
{
enum allSatisfy = true;
}
}
// taken from std.meta.anySatisfy
template anySatisfy(alias F, Ts...)
{
static foreach (T; Ts)
{
static if (!is(typeof(anySatisfy) == bool) && // not yet defined
F!T)
{
enum anySatisfy = true;
}
}
static if (!is(typeof(anySatisfy) == bool)) // if not yet defined
{
enum anySatisfy = false;
}
}
// simplified from std.traits.maxAlignment
template maxAlignment(Ts...)
if (Ts.length > 0)
{
enum maxAlignment =
{
size_t result = 0;
static foreach (T; Ts)
if (T.alignof > result) result = T.alignof;
return result;
}();
}
template classInstanceAlignment(T)
if (is(T == class))
{
enum classInstanceAlignment = __traits(classInstanceAlignment, T);
}
/// See $(REF hasElaborateMove, std,traits)
template hasElaborateMove(S)
{
static if (__traits(isStaticArray, S))
{
enum bool hasElaborateMove = S.sizeof && hasElaborateMove!(BaseElemOf!S);
}
else static if (is(S == struct))
{
enum hasElaborateMove = (is(typeof(S.init.opPostMove(lvalueOf!S))) &&
!is(typeof(S.init.opPostMove(rvalueOf!S)))) ||
anySatisfy!(.hasElaborateMove, Fields!S);
}
else
{
enum bool hasElaborateMove = false;
}
}
// std.traits.hasElaborateDestructor
template hasElaborateDestructor(S)
{
static if (__traits(isStaticArray, S))
{
enum bool hasElaborateDestructor = S.sizeof && hasElaborateDestructor!(BaseElemOf!S);
}
else static if (is(S == struct))
{
// Once https://issues.dlang.org/show_bug.cgi?id=24865 is fixed, then
// this should be the implementation, but until that's fixed, we need the
// uncommented code.
// enum hasElaborateDestructor = __traits(hasMember, S, "__xdtor");
enum hasElaborateDestructor = hasDtor([__traits(allMembers, S)]);
}
else
{
enum bool hasElaborateDestructor = false;
}
}
private bool hasDtor(string[] members)
{
foreach (name; members)
{
if (name == "__xdtor")
return true;
}
return false;
}
@safe unittest
{
static struct NoDestructor {}
static assert(!hasElaborateDestructor!NoDestructor);
static assert(!hasElaborateDestructor!(NoDestructor[42]));
static assert(!hasElaborateDestructor!(NoDestructor[0]));
static assert(!hasElaborateDestructor!(NoDestructor[]));
static struct HasDestructor { ~this() {} }
static assert( hasElaborateDestructor!HasDestructor);
static assert( hasElaborateDestructor!(HasDestructor[42]));
static assert(!hasElaborateDestructor!(HasDestructor[0]));
static assert(!hasElaborateDestructor!(HasDestructor[]));
static struct HasDestructor2 { HasDestructor s; }
static assert( hasElaborateDestructor!HasDestructor2);
static assert( hasElaborateDestructor!(HasDestructor2[42]));
static assert(!hasElaborateDestructor!(HasDestructor2[0]));
static assert(!hasElaborateDestructor!(HasDestructor2[]));
static class HasFinalizer { ~this() {} }
static assert(!hasElaborateDestructor!HasFinalizer);
static struct HasUnion { union { HasDestructor s; } }
static assert(!hasElaborateDestructor!HasUnion);
static assert(!hasElaborateDestructor!(HasUnion[42]));
static assert(!hasElaborateDestructor!(HasUnion[0]));
static assert(!hasElaborateDestructor!(HasUnion[]));
static assert(!hasElaborateDestructor!int);
static assert(!hasElaborateDestructor!(int[0]));
static assert(!hasElaborateDestructor!(int[42]));
static assert(!hasElaborateDestructor!(int[]));
}
// https://issues.dlang.org/show_bug.cgi?id=24865
@safe unittest
{
static struct S2 { ~this() {} }
static struct S3 { S2 field; }
static struct S6 { S3[0] field; }
static assert( hasElaborateDestructor!S2);
static assert( hasElaborateDestructor!S3);
static assert(!hasElaborateDestructor!S6);
}
// std.traits.hasElaborateCopyDestructor
template hasElaborateCopyConstructor(S)
{
static if (__traits(isStaticArray, S))
{
enum bool hasElaborateCopyConstructor = S.sizeof && hasElaborateCopyConstructor!(BaseElemOf!S);
}
else static if (is(S == struct))
{
enum hasElaborateCopyConstructor = __traits(hasCopyConstructor, S) || __traits(hasPostblit, S);
}
else
{
enum bool hasElaborateCopyConstructor = false;
}
}
@safe unittest
{
static struct S
{
int x;
this(return scope ref typeof(this) rhs) { }
this(int x, int y) {}
}
static assert( hasElaborateCopyConstructor!S);
static assert(!hasElaborateCopyConstructor!(S[0][1]));
static struct S2
{
int x;
this(int x, int y) {}
}
static assert(!hasElaborateCopyConstructor!S2);
static struct S3
{
int x;
this(return scope ref typeof(this) rhs, int x = 42) { }
this(int x, int y) {}
}
static assert( hasElaborateCopyConstructor!S3);
static struct S4 { union { S s; } }
static assert(!hasElaborateCopyConstructor!S4);
}
template hasElaborateAssign(S)
{
static if (__traits(isStaticArray, S))
{
enum bool hasElaborateAssign = S.sizeof && hasElaborateAssign!(BaseElemOf!S);
}
else static if (is(S == struct))
{
enum hasElaborateAssign = is(typeof(S.init.opAssign(rvalueOf!S))) ||
is(typeof(S.init.opAssign(lvalueOf!S)));
}
else
{
enum bool hasElaborateAssign = false;
}
}
unittest
{
{
static struct S {}
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { int i; }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { void opAssign(S) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { void opAssign(ref S) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { void opAssign(int) {} }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { this(this) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
// https://issues.dlang.org/show_bug.cgi?id=24834
/+
{
static struct S { this(ref S) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
+/
{
static struct S { ~this() {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct S { @disable void opAssign(S); }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member {}
static struct S { Member member; }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member { void opAssign(Member) {} }
static struct S { Member member; }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member {}
static struct S { Member member; void opAssign(S) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member { @disable void opAssign(Member); }
static struct S { Member member; }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member { @disable void opAssign(Member); }
static struct S { Member member; void opAssign(S) {} }
static assert( hasElaborateAssign!S);
static assert( hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member { void opAssign(Member) {} }
static struct S { Member member; @disable void opAssign(S); }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
{
static struct Member { void opAssign(Member) {} }
static struct S { union { Member member; } }
static assert(!hasElaborateAssign!S);
static assert(!hasElaborateAssign!(S[10]));
static assert(!hasElaborateAssign!(S[0]));
static assert(!hasElaborateAssign!(S[]));
}
static assert(!hasElaborateAssign!int);
static assert(!hasElaborateAssign!(string[]));
static assert(!hasElaborateAssign!Object);
}
template hasIndirections(T)
{
static if (is(T == enum))
enum hasIndirections = hasIndirections!(OriginalType!T);
else static if (is(T == struct) || is(T == union))
enum hasIndirections = anySatisfy!(.hasIndirections, typeof(T.tupleof));
else static if (__traits(isAssociativeArray, T) || is(T == class) || is(T == interface))
enum hasIndirections = true;
else static if (is(T == E[N], E, size_t N))
enum hasIndirections = T.sizeof && (is(E == void) || hasIndirections!(BaseElemOf!E));
else static if (isFunctionPointer!T)
enum hasIndirections = false;
else
enum hasIndirections = isPointer!T || isDelegate!T || isDynamicArray!T;
}
@safe unittest
{
static assert(!hasIndirections!int);
static assert(!hasIndirections!(const int));
static assert( hasIndirections!(int*));
static assert( hasIndirections!(const int*));
static assert( hasIndirections!(int[]));
static assert(!hasIndirections!(int[42]));
static assert(!hasIndirections!(int[0]));
static assert( hasIndirections!(int*));
static assert( hasIndirections!(int*[]));
static assert( hasIndirections!(int*[42]));
static assert(!hasIndirections!(int*[0]));
static assert( hasIndirections!string);
static assert( hasIndirections!(string[]));
static assert( hasIndirections!(string[42]));
static assert(!hasIndirections!(string[0]));
static assert( hasIndirections!(void[]));
static assert( hasIndirections!(void[17]));
static assert(!hasIndirections!(void[0]));
static assert( hasIndirections!(string[int]));
static assert( hasIndirections!(string[int]*));
static assert( hasIndirections!(string[int][]));
static assert( hasIndirections!(string[int][12]));
static assert(!hasIndirections!(string[int][0]));
static assert(!hasIndirections!(int function(string)));
static assert( hasIndirections!(int delegate(string)));
static assert(!hasIndirections!(const(int function(string))));
static assert( hasIndirections!(const(int delegate(string))));
static assert(!hasIndirections!(immutable(int function(string))));
static assert( hasIndirections!(immutable(int delegate(string))));
static class C {}
static assert( hasIndirections!C);
static interface I {}
static assert( hasIndirections!I);
{
enum E : int { a }
static assert(!hasIndirections!E);
}
{
enum E : int* { a }
static assert( hasIndirections!E);
}
{
enum E : string { a = "" }
static assert( hasIndirections!E);
}
{
enum E : int[] { a = null }
static assert( hasIndirections!E);
}
{
enum E : int[3] { a = [1, 2, 3] }
static assert(!hasIndirections!E);
}
{
enum E : int*[3] { a = [null, null, null] }
static assert( hasIndirections!E);
}
{
enum E : int*[0] { a = int*[0].init }
static assert(!hasIndirections!E);
}
{
enum E : C { a = null }
static assert( hasIndirections!E);
}
{
enum E : I { a = null }
static assert( hasIndirections!E);
}
{
static struct S {}
static assert(!hasIndirections!S);
enum E : S { a = S.init }
static assert(!hasIndirections!S);
}
{
static struct S { int i; }
static assert(!hasIndirections!S);
enum E : S { a = S.init }
static assert(!hasIndirections!S);
}
{
static struct S { C c; }
static assert( hasIndirections!S);
enum E : S { a = S.init }
static assert( hasIndirections!S);
}
{
static struct S { int[] arr; }
static assert( hasIndirections!S);
enum E : S { a = S.init }
static assert( hasIndirections!S);
}
{
int local;
struct S { void foo() { ++local; } }
static assert( hasIndirections!S);
enum E : S { a = S.init }
static assert( hasIndirections!S);
}
{
static union U {}
static assert(!hasIndirections!U);
}
{
static union U { int i; }
static assert(!hasIndirections!U);
}
{
static union U { C c; }
static assert( hasIndirections!U);
}
{
static union U { int[] arr; }
static assert( hasIndirections!U);
}
}
// https://issues.dlang.org/show_bug.cgi?id=12000
@safe unittest
{
static struct S(T)
{
static assert(hasIndirections!T);
}
static class A(T)
{
S!A a;
}
A!int dummy;
}
template hasUnsharedIndirections(T)
{
static if (is(T == immutable))
enum hasUnsharedIndirections = false;
else static if (is(T == struct) || is(T == union))
enum hasUnsharedIndirections = anySatisfy!(.hasUnsharedIndirections, Fields!T);
else static if (is(T : E[N], E, size_t N))
enum hasUnsharedIndirections = is(E == void) ? false : hasUnsharedIndirections!E;
else static if (isFunctionPointer!T)
enum hasUnsharedIndirections = false;
else static if (isPointer!T)
enum hasUnsharedIndirections = !is(T : shared(U)*, U) && !is(T : immutable(U)*, U);
else static if (isDynamicArray!T)
enum hasUnsharedIndirections = !is(T : shared(V)[], V) && !is(T : immutable(V)[], V);
else static if (is(T == class) || is(T == interface))
enum hasUnsharedIndirections = !is(T : shared(W), W);
else
enum hasUnsharedIndirections = isDelegate!T || __traits(isAssociativeArray, T); // TODO: how to handle these?
}
unittest
{
static struct Foo { shared(int)* val; }
static assert(!hasUnsharedIndirections!(immutable(char)*));
static assert(!hasUnsharedIndirections!(string));
static assert(!hasUnsharedIndirections!(Foo));
static assert( hasUnsharedIndirections!(Foo*));
static assert(!hasUnsharedIndirections!(shared(Foo)*));
static assert(!hasUnsharedIndirections!(immutable(Foo)*));
int local;
struct HasContextPointer { int opCall() { return ++local; } }
static assert(hasIndirections!HasContextPointer);
}
enum bool isAggregateType(T) = is(T == struct) || is(T == union) ||
is(T == class) || is(T == interface);
enum bool isPointer(T) = is(T == U*, U) && !isAggregateType!T;
enum bool isDynamicArray(T) = is(DynamicArrayTypeOf!T) && !isAggregateType!T;
template OriginalType(T)
{
template Impl(T)
{
static if (is(T U == enum)) alias Impl = OriginalType!U;
else alias Impl = T;
}
alias OriginalType = ModifyTypePreservingTQ!(Impl, T);
}
template DynamicArrayTypeOf(T)
{
static if (is(AliasThisTypeOf!T AT) && !is(AT[] == AT))
alias X = DynamicArrayTypeOf!AT;
else
alias X = OriginalType!T;
static if (is(Unqual!X : E[], E) && !is(typeof({ enum n = X.length; })))
alias DynamicArrayTypeOf = X;
else
static assert(0, T.stringof ~ " is not a dynamic array");
}
private template AliasThisTypeOf(T)
if (isAggregateType!T)
{
alias members = __traits(getAliasThis, T);
static if (members.length == 1)
alias AliasThisTypeOf = typeof(__traits(getMember, T.init, members[0]));
else
static assert(0, T.stringof~" does not have alias this type");
}
template isFunctionPointer(T...)
if (T.length == 1)
{
static if (is(T[0] U) || is(typeof(T[0]) U))
{
static if (is(U F : F*) && is(F == function))
enum bool isFunctionPointer = true;
else
enum bool isFunctionPointer = false;
}
else
enum bool isFunctionPointer = false;
}
template isDelegate(T...)
if (T.length == 1)
{
static if (is(typeof(& T[0]) U : U*) && is(typeof(& T[0]) U == delegate))
{
// T is a (nested) function symbol.
enum bool isDelegate = true;
}
else static if (is(T[0] W) || is(typeof(T[0]) W))
{
// T is an expression or a type. Take the type of it and examine.
enum bool isDelegate = is(W == delegate);
}
else
enum bool isDelegate = false;
}
// std.meta.Filter
template Filter(alias pred, TList...)
{
static if (TList.length == 0)
{
alias Filter = AliasSeq!();
}
else static if (TList.length == 1)
{
static if (pred!(TList[0]))
alias Filter = AliasSeq!(TList[0]);
else
alias Filter = AliasSeq!();
}
/* The next case speeds up compilation by reducing
* the number of Filter instantiations
*/
else static if (TList.length == 2)
{
static if (pred!(TList[0]))
{
static if (pred!(TList[1]))
alias Filter = AliasSeq!(TList[0], TList[1]);
else
alias Filter = AliasSeq!(TList[0]);
}
else
{
static if (pred!(TList[1]))
alias Filter = AliasSeq!(TList[1]);
else
alias Filter = AliasSeq!();
}
}
else
{
alias Filter =
AliasSeq!(
Filter!(pred, TList[ 0 .. $/2]),
Filter!(pred, TList[$/2 .. $ ]));
}
}
// std.meta.staticMap
template staticMap(alias F, T...)
{
static if (T.length == 0)
{
alias staticMap = AliasSeq!();
}
else static if (T.length == 1)
{
alias staticMap = AliasSeq!(F!(T[0]));
}
/* Cases 2 to 8 improve compile performance by reducing
* the number of recursive instantiations of staticMap
*/
else static if (T.length == 2)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]));
}
else static if (T.length == 3)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]));
}
else static if (T.length == 4)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]));
}
else static if (T.length == 5)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]));
}
else static if (T.length == 6)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5]));
}
else static if (T.length == 7)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5]), F!(T[6]));
}
else static if (T.length == 8)
{
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5]), F!(T[6]), F!(T[7]));
}
else
{
alias staticMap =
AliasSeq!(
staticMap!(F, T[ 0 .. $/2]),
staticMap!(F, T[$/2 .. $ ]));
}
}
// std.exception.assertCTFEable
version (CoreUnittest) package(core)
void assertCTFEable(alias dg)()
{
static assert({ cast(void) dg(); return true; }());
cast(void) dg();
}
// std.traits.FunctionTypeOf
/*
Get the function type from a callable object `func`.
Using builtin `typeof` on a property function yields the types of the
property value, not of the property function itself. Still,
`FunctionTypeOf` is able to obtain function types of properties.
Note:
Do not confuse function types with function pointer types; function types are
usually used for compile-time reflection purposes.
*/
template FunctionTypeOf(func...)
if (func.length == 1 /*&& isCallable!func*/)
{
static if (is(typeof(& func[0]) Fsym : Fsym*) && is(Fsym == function) || is(typeof(& func[0]) Fsym == delegate))
{
alias FunctionTypeOf = Fsym; // HIT: (nested) function symbol
}
else static if (is(typeof(& func[0].opCall) Fobj == delegate))
{
alias FunctionTypeOf = Fobj; // HIT: callable object
}
else static if (is(typeof(& func[0].opCall) Ftyp : Ftyp*) && is(Ftyp == function))
{
alias FunctionTypeOf = Ftyp; // HIT: callable type
}
else static if (is(func[0] T) || is(typeof(func[0]) T))
{
static if (is(T == function))
alias FunctionTypeOf = T; // HIT: function
else static if (is(T Fptr : Fptr*) && is(Fptr == function))
alias FunctionTypeOf = Fptr; // HIT: function pointer
else static if (is(T Fdlg == delegate))
alias FunctionTypeOf = Fdlg; // HIT: delegate
else
static assert(0);
}
else
static assert(0);
}
@safe unittest
{
class C
{
int value() @property { return 0; }
}
static assert(is( typeof(C.value) == int ));
static assert(is( FunctionTypeOf!(C.value) == function ));
}
@system unittest
{
int test(int a);
int propGet() @property;
int propSet(int a) @property;
int function(int) test_fp;
int delegate(int) test_dg;
static assert(is( typeof(test) == FunctionTypeOf!(typeof(test)) ));
static assert(is( typeof(test) == FunctionTypeOf!test ));
static assert(is( typeof(test) == FunctionTypeOf!test_fp ));
static assert(is( typeof(test) == FunctionTypeOf!test_dg ));
alias int GetterType() @property;
alias int SetterType(int) @property;
static assert(is( FunctionTypeOf!propGet == GetterType ));
static assert(is( FunctionTypeOf!propSet == SetterType ));
interface Prop { int prop() @property; }
Prop prop;
static assert(is( FunctionTypeOf!(Prop.prop) == GetterType ));
static assert(is( FunctionTypeOf!(prop.prop) == GetterType ));
class Callable { int opCall(int) { return 0; } }
auto call = new Callable;
static assert(is( FunctionTypeOf!call == typeof(test) ));
struct StaticCallable { static int opCall(int) { return 0; } }
StaticCallable stcall_val;
StaticCallable* stcall_ptr;
static assert(is( FunctionTypeOf!stcall_val == typeof(test) ));