This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathobject.d
4646 lines (4008 loc) · 118 KB
/
object.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
/**
* $(SCRIPT inhibitQuickIndex = 1;)
* $(DIVC quickindex,
* $(BOOKTABLE,
* $(TR $(TH Category) $(TH Symbols))
* $(TR $(TD Arrays) $(TD
* $(MYREF assumeSafeAppend)
* $(MYREF capacity)
* $(MYREF idup)
* $(MYREF reserve)
* ))
* $(TR $(TD Associative arrays) $(TD
* $(MYREF byKey)
* $(MYREF byKeyValue)
* $(MYREF byValue)
* $(MYREF clear)
* $(MYREF get)
* $(MYREF keys)
* $(MYREF rehash)
* $(MYREF require)
* $(MYREF update)
* $(MYREF values)
* ))
* $(TR $(TD General) $(TD
* $(MYREF destroy)
* $(MYREF dup)
* $(MYREF hashOf)
* $(MYREF opEquals)
* ))
* $(TR $(TD Types) $(TD
* $(MYREF Error)
* $(MYREF Exception)
* $(MYREF noreturn)
* $(MYREF Object)
* $(MYREF Throwable)
* ))
* $(TR $(TD Type info) $(TD
* $(MYREF Interface)
* $(MYREF ModuleInfo)
* $(MYREF OffsetTypeInfo)
* $(MYREF RTInfoImpl)
* $(MYREF rtinfoNoPointers)
* $(MYREF TypeInfo)
* $(MYREF TypeInfo_Class)
* ))
* ))
*
* Forms the symbols available to all D programs. Includes Object, which is
* the root of the class object hierarchy. This module is implicitly
* imported.
*
* Copyright: Copyright Digital Mars 2000 - 2011.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright, Sean Kelly
* Source: $(DRUNTIMESRC object.d)
*/
module object;
alias size_t = typeof(int.sizeof);
alias ptrdiff_t = typeof(cast(void*)0 - cast(void*)0);
alias sizediff_t = ptrdiff_t; // For backwards compatibility only.
alias noreturn = typeof(*null); /// bottom type
alias hash_t = size_t; // For backwards compatibility only.
alias equals_t = bool; // For backwards compatibility only.
alias string = immutable(char)[];
alias wstring = immutable(wchar)[];
alias dstring = immutable(dchar)[];
version (D_ObjectiveC)
{
deprecated("explicitly import `selector` instead using: `import core.attribute : selector;`")
public import core.attribute : selector;
}
version (Posix) public import core.attribute : gnuAbiTag;
// Some ABIs use a complex varargs implementation requiring TypeInfo.argTypes().
version (GNU)
{
// No TypeInfo-based core.vararg.va_arg().
}
else version (X86_64)
{
version (DigitalMars) version = WithArgTypes;
else version (Windows) { /* no need for Win64 ABI */ }
else version = WithArgTypes;
}
else version (AArch64)
{
// Apple uses a trivial varargs implementation
version (OSX) {}
else version (iOS) {}
else version (TVOS) {}
else version (WatchOS) {}
else version = WithArgTypes;
}
/**
* All D class objects inherit from Object.
*/
class Object
{
/**
* Convert Object to a human readable string.
*/
string toString()
{
return typeid(this).name;
}
@system unittest
{
enum unittest_sym_name = __traits(identifier, __traits(parent, (){}));
enum fqn_unittest = "object.Object." ~ unittest_sym_name; // object.__unittest_LX_CY
class C {}
Object obj = new Object;
C c = new C;
assert(obj.toString() == "object.Object");
assert(c.toString() == fqn_unittest ~ ".C");
}
/**
* Compute hash function for Object.
*/
size_t toHash() @trusted nothrow
{
// BUG: this prevents a compacting GC from working, needs to be fixed
size_t addr = cast(size_t) cast(void*) this;
// The bottom log2((void*).alignof) bits of the address will always
// be 0. Moreover it is likely that each Object is allocated with a
// separate call to malloc. The alignment of malloc differs from
// platform to platform, but rather than having special cases for
// each platform it is safe to use a shift of 4. To minimize
// collisions in the low bits it is more important for the shift to
// not be too small than for the shift to not be too big.
return addr ^ (addr >>> 4);
}
/**
* Compare with another Object obj.
* Returns:
* $(TABLE
* $(TR $(TD this < obj) $(TD < 0))
* $(TR $(TD this == obj) $(TD 0))
* $(TR $(TD this > obj) $(TD > 0))
* )
*/
int opCmp(Object o)
{
// BUG: this prevents a compacting GC from working, needs to be fixed
//return cast(int)cast(void*)this - cast(int)cast(void*)o;
throw new Exception("need opCmp for class " ~ typeid(this).name);
//return this !is o;
}
@system unittest
{
Object obj = new Object;
bool gotCaught;
try
{
obj.opCmp(new Object);
}
catch (Exception e)
{
gotCaught = true;
assert(e.msg == "need opCmp for class object.Object");
}
assert(gotCaught);
}
/**
* Test whether $(D this) is equal to $(D o).
* The default implementation only compares by identity (using the $(D is) operator).
* Generally, overrides for $(D opEquals) should attempt to compare objects by their contents.
*/
bool opEquals(Object o)
{
return this is o;
}
interface Monitor
{
void lock();
void unlock();
}
/**
* Create instance of class specified by the fully qualified name
* classname.
* The class must either have no constructors or have
* a default constructor.
* Returns:
* null if failed
* Example:
* ---
* module foo.bar;
*
* class C
* {
* this() { x = 10; }
* int x;
* }
*
* void main()
* {
* auto c = cast(C)Object.factory("foo.bar.C");
* assert(c !is null && c.x == 10);
* }
* ---
*/
static Object factory(string classname)
{
auto ci = TypeInfo_Class.find(classname);
if (ci)
{
return ci.create();
}
return null;
}
@system unittest
{
Object valid_obj = Object.factory("object.Object");
Object invalid_obj = Object.factory("object.__this_class_doesnt_exist__");
assert(valid_obj !is null);
assert(invalid_obj is null);
}
}
bool opEquals(Object lhs, Object rhs)
{
// If aliased to the same object or both null => equal
if (lhs is rhs) return true;
// If either is null => non-equal
if (lhs is null || rhs is null) return false;
if (!lhs.opEquals(rhs)) return false;
// If same exact type => one call to method opEquals
if (typeid(lhs) is typeid(rhs) ||
!__ctfe && typeid(lhs).opEquals(typeid(rhs)))
/* CTFE doesn't like typeid much. 'is' works, but opEquals doesn't
(issue 7147). But CTFE also guarantees that equal TypeInfos are
always identical. So, no opEquals needed during CTFE. */
{
return true;
}
// General case => symmetric calls to method opEquals
return rhs.opEquals(lhs);
}
/************************
* Returns true if lhs and rhs are equal.
*/
bool opEquals(const Object lhs, const Object rhs)
{
// A hack for the moment.
return opEquals(cast()lhs, cast()rhs);
}
/// If aliased to the same object or both null => equal
@system unittest
{
class F { int flag; this(int flag) { this.flag = flag; } }
F f;
assert(f == f); // both null
f = new F(1);
assert(f == f); // both aliased to the same object
}
/// If either is null => non-equal
@system unittest
{
class F { int flag; this(int flag) { this.flag = flag; } }
F f;
assert(!(new F(0) == f));
assert(!(f == new F(0)));
}
/// If same exact type => one call to method opEquals
@system unittest
{
class F
{
int flag;
this(int flag)
{
this.flag = flag;
}
override bool opEquals(const Object o)
{
return flag == (cast(F) o).flag;
}
}
F f;
assert(new F(0) == new F(0));
assert(!(new F(0) == new F(1)));
}
/// General case => symmetric calls to method opEquals
@system unittest
{
int fEquals, gEquals;
class Base
{
int flag;
this(int flag)
{
this.flag = flag;
}
}
class F : Base
{
this(int flag) { super(flag); }
override bool opEquals(const Object o)
{
fEquals++;
return flag == (cast(Base) o).flag;
}
}
class G : Base
{
this(int flag) { super(flag); }
override bool opEquals(const Object o)
{
gEquals++;
return flag == (cast(Base) o).flag;
}
}
assert(new F(1) == new G(1));
assert(fEquals == 1);
assert(gEquals == 1);
}
// To cover const Object opEquals
@system unittest
{
const Object obj1 = new Object;
const Object obj2 = new Object;
assert(obj1 == obj1);
assert(obj1 != obj2);
}
private extern(C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow;
void setSameMutex(shared Object ownee, shared Object owner)
{
_d_setSameMutex(ownee, owner);
}
@system unittest
{
shared Object obj1 = new Object;
synchronized class C
{
void bar() {}
}
shared C obj2 = new shared(C);
obj2.bar();
assert(obj1.__monitor != obj2.__monitor);
assert(obj1.__monitor is null);
setSameMutex(obj1, obj2);
assert(obj1.__monitor == obj2.__monitor);
assert(obj1.__monitor !is null);
}
/**
* Information about an interface.
* When an object is accessed via an interface, an Interface* appears as the
* first entry in its vtbl.
*/
struct Interface
{
TypeInfo_Class classinfo; /// .classinfo for this interface (not for containing class)
void*[] vtbl;
size_t offset; /// offset to Interface 'this' from Object 'this'
}
/**
* Array of pairs giving the offset and type information for each
* member in an aggregate.
*/
struct OffsetTypeInfo
{
size_t offset; /// Offset of member from start of object
TypeInfo ti; /// TypeInfo for this member
}
/**
* Runtime type information about a type.
* Can be retrieved for any type using a
* $(GLINK2 expression,TypeidExpression, TypeidExpression).
*/
class TypeInfo
{
override string toString() const pure @safe nothrow
{
return typeid(this).name;
}
override size_t toHash() @trusted const nothrow
{
return hashOf(this.toString());
}
override int opCmp(Object rhs)
{
if (this is rhs)
return 0;
auto ti = cast(TypeInfo) rhs;
if (ti is null)
return 1;
return __cmp(this.toString(), ti.toString());
}
@system unittest
{
assert(typeid(void) <= typeid(void));
assert(typeid(void).opCmp(null));
assert(!typeid(void).opCmp(typeid(void)));
}
override bool opEquals(Object o)
{
/* TypeInfo instances are singletons, but duplicates can exist
* across DLL's. Therefore, comparing for a name match is
* sufficient.
*/
if (this is o)
return true;
auto ti = cast(const TypeInfo)o;
return ti && this.toString() == ti.toString();
}
@system unittest
{
auto anotherObj = new Object();
assert(typeid(void).opEquals(typeid(void)));
assert(!typeid(void).opEquals(anotherObj));
}
/**
* Computes a hash of the instance of a type.
* Params:
* p = pointer to start of instance of the type
* Returns:
* the hash
* Bugs:
* fix https://issues.dlang.org/show_bug.cgi?id=12516 e.g. by changing this to a truly safe interface.
*/
size_t getHash(scope const void* p) @trusted nothrow const
{
return hashOf(p);
}
/// Compares two instances for equality.
bool equals(in void* p1, in void* p2) const { return p1 == p2; }
/// Compares two instances for <, ==, or >.
int compare(in void* p1, in void* p2) const { return _xopCmp(p1, p2); }
/// Returns size of the type.
@property size_t tsize() nothrow pure const @safe @nogc { return 0; }
/// Swaps two instances of the type.
void swap(void* p1, void* p2) const
{
size_t remaining = tsize;
// If the type might contain pointers perform the swap in pointer-sized
// chunks in case a garbage collection pass interrupts this function.
if ((cast(size_t) p1 | cast(size_t) p2) % (void*).alignof == 0)
{
while (remaining >= (void*).sizeof)
{
void* tmp = *cast(void**) p1;
*cast(void**) p1 = *cast(void**) p2;
*cast(void**) p2 = tmp;
p1 += (void*).sizeof;
p2 += (void*).sizeof;
remaining -= (void*).sizeof;
}
}
for (size_t i = 0; i < remaining; i++)
{
byte t = (cast(byte *)p1)[i];
(cast(byte*)p1)[i] = (cast(byte*)p2)[i];
(cast(byte*)p2)[i] = t;
}
}
@system unittest
{
class _TypeInfo_Dummy : TypeInfo
{
override const(void)[] initializer() const { return []; }
@property override size_t tsize() nothrow pure const @safe @nogc { return tsize_val; }
size_t tsize_val;
}
auto dummy = new _TypeInfo_Dummy();
cast(void)dummy.initializer(); // For coverage completeness
int a = 2, b = -2;
dummy.swap(&a, &b);
// does nothing because tsize is 0
assert(a == 2);
assert(b == -2);
dummy.tsize_val = int.sizeof;
dummy.swap(&a, &b);
assert(a == -2);
assert(b == 2);
void* ptr_a = null, ptr_b = cast(void*)1;
dummy.tsize_val = (void*).sizeof;
dummy.swap(&ptr_a, &ptr_b);
assert(ptr_a is cast(void*)1);
assert(ptr_b is null);
}
/** Get TypeInfo for 'next' type, as defined by what kind of type this is,
null if none. */
@property inout(TypeInfo) next() nothrow pure inout @nogc { return null; }
/**
* Return default initializer. If the type should be initialized to all
* zeros, an array with a null ptr and a length equal to the type size will
* be returned. For static arrays, this returns the default initializer for
* a single element of the array, use `tsize` to get the correct size.
*/
abstract const(void)[] initializer() nothrow pure const @safe @nogc;
/** Get flags for type: 1 means GC should scan for pointers,
2 means arg of this type is passed in SIMD register(s) if available */
@property uint flags() nothrow pure const @safe @nogc { return 0; }
/// Get type information on the contents of the type; null if not available
const(OffsetTypeInfo)[] offTi() const { return null; }
/// Run the destructor on the object and all its sub-objects
void destroy(void* p) const {}
/// Run the postblit on the object and all its sub-objects
void postblit(void* p) const {}
/// Return alignment of type
@property size_t talign() nothrow pure const @safe @nogc { return tsize; }
/** Return internal info on arguments fitting into 8byte.
* See X86-64 ABI 3.2.3
*/
version (WithArgTypes) int argTypes(out TypeInfo arg1, out TypeInfo arg2) @safe nothrow
{
arg1 = this;
return 0;
}
/** Return info used by the garbage collector to do precise collection.
*/
@property immutable(void)* rtInfo() nothrow pure const @safe @nogc { return rtinfoHasPointers; } // better safe than sorry
}
@system unittest
{
class _TypeInfo_Dummy : TypeInfo
{
override const(void)[] initializer() const { return []; }
}
auto dummy = new _TypeInfo_Dummy();
cast(void)dummy.initializer(); // For coverage completeness
assert(dummy.rtInfo() is rtinfoHasPointers);
assert(typeid(void).rtInfo() is rtinfoNoPointers);
assert(dummy.tsize() == 0);
bool gotCaught;
try
{
dummy.compare(null, null);
} catch (Error e)
{
gotCaught = true;
assert(e.msg == "TypeInfo.compare is not implemented");
}
assert(gotCaught);
assert(dummy.equals(null, null));
assert(!dummy.equals(cast(void*)1, null));
}
@system unittest
{
assert(typeid(void).next() is null);
assert(typeid(void).offTi() is null);
assert(typeid(void).tsize() == 1);
version (WithArgTypes)
{
TypeInfo ti1;
TypeInfo ti2;
assert(typeid(void).argTypes(ti1, ti2) == 0);
assert(typeid(void) is ti1);
assert(ti1 !is null);
assert(ti2 is null);
}
}
@system unittest
{
class _ZypeInfo_Dummy : TypeInfo
{
override const(void)[] initializer() const { return []; }
}
auto dummy2 = new _ZypeInfo_Dummy();
cast(void)dummy2.initializer(); // For coverage completeness
assert(typeid(void) > dummy2);
assert(dummy2 < typeid(void));
}
@safe unittest
{
enum unittest_sym_name = __traits(identifier, __traits(parent, (){}));
enum fqn_unittest = "object." ~ unittest_sym_name; // object.__unittest_LX_CY
class _TypeInfo_Dummy : TypeInfo
{
override const(void)[] initializer() const { return []; }
}
auto dummy = new _TypeInfo_Dummy();
cast(void)dummy.initializer(); // For coverage completeness
assert(dummy.toString() == fqn_unittest ~ "._TypeInfo_Dummy");
assert(dummy.toHash() == hashOf(dummy.toString()));
assert(dummy.getHash(null) == 0);
}
class TypeInfo_Enum : TypeInfo
{
override string toString() const { return name; }
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Enum)o;
return c && this.name == c.name &&
this.base == c.base;
}
@system unittest
{
enum E { A, B, C }
enum EE { A, B, C }
assert(typeid(E).opEquals(typeid(E)));
assert(!typeid(E).opEquals(typeid(EE)));
}
override size_t getHash(scope const void* p) const { return base.getHash(p); }
@system unittest
{
enum E { A, B, C }
E e1 = E.A;
E e2 = E.B;
assert(typeid(E).getHash(&e1) == hashOf(E.A));
assert(typeid(E).getHash(&e2) == hashOf(E.B));
enum ES : string { A = "foo", B = "bar" }
ES es1 = ES.A;
ES es2 = ES.B;
assert(typeid(ES).getHash(&es1) == hashOf("foo"));
assert(typeid(ES).getHash(&es2) == hashOf("bar"));
}
override bool equals(in void* p1, in void* p2) const { return base.equals(p1, p2); }
@system unittest
{
enum E { A, B, C }
E e1 = E.A;
E e2 = E.B;
assert(typeid(E).equals(&e1, &e1));
assert(!typeid(E).equals(&e1, &e2));
}
override int compare(in void* p1, in void* p2) const { return base.compare(p1, p2); }
@system unittest
{
enum E { A, B, C }
E e1 = E.A;
E e2 = E.B;
assert(typeid(E).compare(&e1, &e1) == 0);
assert(typeid(E).compare(&e1, &e2) < 0);
assert(typeid(E).compare(&e2, &e1) > 0);
}
override @property size_t tsize() nothrow pure const { return base.tsize; }
@safe unittest
{
enum E { A, B, C }
enum ES : string { A = "a", B = "b", C = "c"}
assert(typeid(E).tsize == E.sizeof);
assert(typeid(ES).tsize == ES.sizeof);
assert(typeid(E).tsize != ES.sizeof);
}
override void swap(void* p1, void* p2) const { return base.swap(p1, p2); }
@system unittest
{
enum E { A, B, C }
E e1 = E.A;
E e2 = E.B;
typeid(E).swap(&e1, &e2);
assert(e1 == E.B);
assert(e2 == E.A);
}
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; }
@system unittest
{
enum E { A, B, C }
assert(typeid(E).next is null);
}
override @property uint flags() nothrow pure const { return base.flags; }
@safe unittest
{
enum E { A, B, C }
assert(typeid(E).flags == 0);
}
override const(OffsetTypeInfo)[] offTi() const { return base.offTi; }
@system unittest
{
enum E { A, B, C }
assert(typeid(E).offTi is null);
}
override void destroy(void* p) const { return base.destroy(p); }
override void postblit(void* p) const { return base.postblit(p); }
override const(void)[] initializer() const
{
return m_init.length ? m_init : base.initializer();
}
override @property size_t talign() nothrow pure const { return base.talign; }
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
return base.argTypes(arg1, arg2);
}
override @property immutable(void)* rtInfo() const { return base.rtInfo; }
TypeInfo base;
string name;
void[] m_init;
}
@safe unittest
{
enum unittest_sym_name = __traits(identifier, __traits(parent, (){}));
enum fqn_unittest = "object." ~ unittest_sym_name; // object.__unittest_LX_CY
enum E { A, B, C }
enum EE { A, B, C }
assert(typeid(E).toString() == fqn_unittest ~ ".E");
}
@safe unittest // issue 12233
{
static assert(is(typeof(TypeInfo.init) == TypeInfo));
assert(TypeInfo.init is null);
}
// Please make sure to keep this in sync with TypeInfo_P (src/rt/typeinfo/ti_ptr.d)
class TypeInfo_Pointer : TypeInfo
{
override string toString() const { return m_next.toString() ~ "*"; }
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Pointer)o;
return c && this.m_next == c.m_next;
}
override size_t getHash(scope const void* p) @trusted const
{
size_t addr = cast(size_t) *cast(const void**)p;
return addr ^ (addr >> 4);
}
override bool equals(in void* p1, in void* p2) const
{
return *cast(void**)p1 == *cast(void**)p2;
}
override int compare(in void* p1, in void* p2) const
{
const v1 = *cast(void**) p1, v2 = *cast(void**) p2;
return (v1 > v2) - (v1 < v2);
}
override @property size_t tsize() nothrow pure const
{
return (void*).sizeof;
}
override const(void)[] initializer() const @trusted
{
return (cast(void *)null)[0 .. (void*).sizeof];
}
override void swap(void* p1, void* p2) const
{
void* tmp = *cast(void**)p1;
*cast(void**)p1 = *cast(void**)p2;
*cast(void**)p2 = tmp;
}
override @property inout(TypeInfo) next() nothrow pure inout { return m_next; }
override @property uint flags() nothrow pure const { return 1; }
TypeInfo m_next;
}
class TypeInfo_Array : TypeInfo
{
override string toString() const { return value.toString() ~ "[]"; }
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Array)o;
return c && this.value == c.value;
}
override size_t getHash(scope const void* p) @trusted const
{
void[] a = *cast(void[]*)p;
return getArrayHash(value, a.ptr, a.length);
}
override bool equals(in void* p1, in void* p2) const
{
void[] a1 = *cast(void[]*)p1;
void[] a2 = *cast(void[]*)p2;
if (a1.length != a2.length)
return false;
size_t sz = value.tsize;
for (size_t i = 0; i < a1.length; i++)
{
if (!value.equals(a1.ptr + i * sz, a2.ptr + i * sz))
return false;
}
return true;
}
override int compare(in void* p1, in void* p2) const
{
void[] a1 = *cast(void[]*)p1;
void[] a2 = *cast(void[]*)p2;
size_t sz = value.tsize;
size_t len = a1.length;
if (a2.length < len)
len = a2.length;
for (size_t u = 0; u < len; u++)
{
immutable int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz);
if (result)
return result;
}
return (a1.length > a2.length) - (a1.length < a2.length);
}
override @property size_t tsize() nothrow pure const
{
return (void[]).sizeof;
}
override const(void)[] initializer() const @trusted
{
return (cast(void *)null)[0 .. (void[]).sizeof];
}
override void swap(void* p1, void* p2) const
{
void[] tmp = *cast(void[]*)p1;
*cast(void[]*)p1 = *cast(void[]*)p2;
*cast(void[]*)p2 = tmp;
}
TypeInfo value;
override @property inout(TypeInfo) next() nothrow pure inout
{
return value;
}
override @property uint flags() nothrow pure const { return 1; }
override @property size_t talign() nothrow pure const
{
return (void[]).alignof;
}
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
arg1 = typeid(size_t);
arg2 = typeid(void*);
return 0;
}
override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(void[]); }
}
class TypeInfo_StaticArray : TypeInfo
{
override string toString() const
{
import core.internal.string : unsignedToTempString;
char[20] tmpBuff = void;
return value.toString() ~ "[" ~ unsignedToTempString(len, tmpBuff) ~ "]";
}
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_StaticArray)o;
return c && this.len == c.len &&
this.value == c.value;
}
override size_t getHash(scope const void* p) @trusted const
{
return getArrayHash(value, p, len);
}
override bool equals(in void* p1, in void* p2) const
{
size_t sz = value.tsize;