This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
object.d
4451 lines (3856 loc) · 112 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
/**
* 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
*/
module object;
private
{
extern (C) Object _d_newclass(const TypeInfo_Class ci);
extern (C) void rt_finalize(void *data, bool det=true);
}
public @trusted @nogc nothrow pure extern (C) void _d_delThrowable(scope Throwable);
// NOTE: For some reason, this declaration method doesn't work
// in this particular file (and this file only). It must
// be a DMD thing.
//alias typeof(int.sizeof) size_t;
//alias typeof(cast(void*)0 - cast(void*)0) ptrdiff_t;
version(D_LP64)
{
alias size_t = ulong;
alias ptrdiff_t = long;
}
else
{
alias size_t = uint;
alias ptrdiff_t = int;
}
alias sizediff_t = ptrdiff_t; //For backwards compatibility only.
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) public import core.attribute : selector;
/**
* All D class objects inherit from Object.
*/
class Object
{
/**
* Convert Object to a human readable string.
*/
string toString()
{
return typeid(this).name;
}
/**
* Compute hash function for Object.
*/
size_t toHash() @trusted nothrow
{
// BUG: this prevents a compacting GC from working, needs to be fixed
return cast(size_t)cast(void*)this;
}
/**
* 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;
}
/**
* 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;
}
}
auto 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 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 lhs.opEquals(rhs);
}
// General case => symmetric calls to method opEquals
return lhs.opEquals(rhs) && rhs.opEquals(lhs);
}
/************************
* Returns true if lhs and rhs are equal.
*/
auto opEquals(const Object lhs, const Object rhs)
{
// A hack for the moment.
return opEquals(cast()lhs, cast()rhs);
}
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);
}
/**
* 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
{
import core.internal.traits : externDFunc;
alias hashOf = externDFunc!("rt.util.hash.hashOf",
size_t function(const(void)[], size_t) @trusted pure nothrow @nogc);
return hashOf(this.toString(), 0);
}
override int opCmp(Object o)
{
import core.internal.traits : externDFunc;
alias dstrcmp = externDFunc!("core.internal.string.dstrcmp",
int function(scope const char[] s1, scope const char[] s2) @trusted pure nothrow @nogc);
if (this is o)
return 0;
TypeInfo ti = cast(TypeInfo)o;
if (ti is null)
return 1;
return dstrcmp(this.toString(), ti.toString());
}
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();
}
/**
* 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(in void* p) @trusted nothrow const { return cast(size_t)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
{
immutable size_t n = tsize;
for (size_t i = 0; i < n; i++)
{
byte t = (cast(byte *)p1)[i];
(cast(byte*)p1)[i] = (cast(byte*)p2)[i];
(cast(byte*)p2)[i] = t;
}
}
/** 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 XMM register */
@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 (X86_64) 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 null; }
}
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;
}
override size_t getHash(in void* p) const { return base.getHash(p); }
override bool equals(in void* p1, in void* p2) const { return base.equals(p1, p2); }
override int compare(in void* p1, in void* p2) const { return base.compare(p1, p2); }
override @property size_t tsize() nothrow pure const { return base.tsize; }
override void swap(void* p1, void* p2) const { return base.swap(p1, p2); }
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; }
override @property uint flags() nothrow pure const { return base.flags; }
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 (X86_64) 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;
}
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(in void* p) @trusted const
{
return cast(size_t)*cast(void**)p;
}
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
{
if (*cast(void**)p1 < *cast(void**)p2)
return -1;
else if (*cast(void**)p1 > *cast(void**)p2)
return 1;
else
return 0;
}
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(in 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 cast(int)a1.length - cast(int)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 (X86_64) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
arg1 = typeid(size_t);
arg2 = typeid(void*);
return 0;
}
}
class TypeInfo_StaticArray : TypeInfo
{
override string toString() const
{
import core.internal.string : unsignedToTempString;
char[20] tmpBuff = void;
return value.toString() ~ "[" ~ unsignedToTempString(len, tmpBuff, 10) ~ "]";
}
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(in void* p) @trusted const
{
return getArrayHash(value, p, len);
}
override bool equals(in void* p1, in void* p2) const
{
size_t sz = value.tsize;
for (size_t u = 0; u < len; u++)
{
if (!value.equals(p1 + u * sz, p2 + u * sz))
return false;
}
return true;
}
override int compare(in void* p1, in void* p2) const
{
size_t sz = value.tsize;
for (size_t u = 0; u < len; u++)
{
immutable int result = value.compare(p1 + u * sz, p2 + u * sz);
if (result)
return result;
}
return 0;
}
override @property size_t tsize() nothrow pure const
{
return len * value.tsize;
}
override void swap(void* p1, void* p2) const
{
import core.memory;
import core.stdc.string : memcpy;
void* tmp;
size_t sz = value.tsize;
ubyte[16] buffer;
void* pbuffer;
if (sz < buffer.sizeof)
tmp = buffer.ptr;
else
tmp = pbuffer = (new void[sz]).ptr;
for (size_t u = 0; u < len; u += sz)
{
size_t o = u * sz;
memcpy(tmp, p1 + o, sz);
memcpy(p1 + o, p2 + o, sz);
memcpy(p2 + o, tmp, sz);
}
if (pbuffer)
GC.free(pbuffer);
}
override const(void)[] initializer() nothrow pure const
{
return value.initializer();
}
override @property inout(TypeInfo) next() nothrow pure inout { return value; }
override @property uint flags() nothrow pure const { return value.flags; }
override void destroy(void* p) const
{
immutable sz = value.tsize;
p += sz * len;
foreach (i; 0 .. len)
{
p -= sz;
value.destroy(p);
}
}
override void postblit(void* p) const
{
immutable sz = value.tsize;
foreach (i; 0 .. len)
{
value.postblit(p);
p += sz;
}
}
TypeInfo value;
size_t len;
override @property size_t talign() nothrow pure const
{
return value.talign;
}
version (X86_64) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
arg1 = typeid(void*);
return 0;
}
}
class TypeInfo_AssociativeArray : TypeInfo
{
override string toString() const
{
return value.toString() ~ "[" ~ key.toString() ~ "]";
}
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_AssociativeArray)o;
return c && this.key == c.key &&
this.value == c.value;
}
override bool equals(in void* p1, in void* p2) @trusted const
{
return !!_aaEqual(this, *cast(const void**) p1, *cast(const void**) p2);
}
override hash_t getHash(in void* p) nothrow @trusted const
{
return _aaGetHash(cast(void*)p, this);
}
// BUG: need to add the rest of the functions
override @property size_t tsize() nothrow pure const
{
return (char[int]).sizeof;
}
override const(void)[] initializer() const @trusted
{
return (cast(void *)null)[0 .. (char[int]).sizeof];
}
override @property inout(TypeInfo) next() nothrow pure inout { return value; }
override @property uint flags() nothrow pure const { return 1; }
TypeInfo value;
TypeInfo key;
override @property size_t talign() nothrow pure const
{
return (char[int]).alignof;
}
version (X86_64) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
arg1 = typeid(void*);
return 0;
}
}
class TypeInfo_Vector : TypeInfo
{
override string toString() const { return "__vector(" ~ base.toString() ~ ")"; }
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Vector)o;
return c && this.base == c.base;
}
override size_t getHash(in void* p) const { return base.getHash(p); }
override bool equals(in void* p1, in void* p2) const { return base.equals(p1, p2); }
override int compare(in void* p1, in void* p2) const { return base.compare(p1, p2); }
override @property size_t tsize() nothrow pure const { return base.tsize; }
override void swap(void* p1, void* p2) const { return base.swap(p1, p2); }
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; }
override @property uint flags() nothrow pure const { return base.flags; }
override const(void)[] initializer() nothrow pure const
{
return base.initializer();
}
override @property size_t talign() nothrow pure const { return 16; }
version (X86_64) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
return base.argTypes(arg1, arg2);
}
TypeInfo base;
}
class TypeInfo_Function : TypeInfo
{
override string toString() const
{
import core.demangle : demangleType;
alias SafeDemangleFunctionType = char[] function (const(char)[] buf, char[] dst = null) @safe nothrow pure;
SafeDemangleFunctionType demangle = ( () @trusted => cast(SafeDemangleFunctionType)(&demangleType) ) ();
return (() @trusted => cast(string)(demangle(deco))) ();
}
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Function)o;
return c && this.deco == c.deco;
}
// BUG: need to add the rest of the functions
override @property size_t tsize() nothrow pure const
{
return 0; // no size for functions
}
override const(void)[] initializer() const @safe
{
return null;
}
TypeInfo next;
/**
* Mangled function type string
*/
string deco;
}
unittest
{
abstract class C
{
void func();
void func(int a);
int func(int a, int b);
}
alias functionTypes = typeof(__traits(getVirtualFunctions, C, "func"));
assert(typeid(functionTypes[0]).toString() == "void function()");
assert(typeid(functionTypes[1]).toString() == "void function(int)");
assert(typeid(functionTypes[2]).toString() == "int function(int, int)");
}
class TypeInfo_Delegate : TypeInfo
{
override string toString() const
{
return cast(string)(next.toString() ~ " delegate()");
}
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Delegate)o;
return c && this.deco == c.deco;
}
override size_t getHash(in void* p) @trusted const
{
return hashOf(*cast(void delegate()*)p);
}
override bool equals(in void* p1, in void* p2) const
{
auto dg1 = *cast(void delegate()*)p1;
auto dg2 = *cast(void delegate()*)p2;
return dg1 == dg2;
}
override int compare(in void* p1, in void* p2) const
{
auto dg1 = *cast(void delegate()*)p1;
auto dg2 = *cast(void delegate()*)p2;
if (dg1 < dg2)
return -1;
else if (dg1 > dg2)
return 1;
else
return 0;
}
override @property size_t tsize() nothrow pure const
{
alias dg = int delegate();
return dg.sizeof;
}
override const(void)[] initializer() const @trusted
{
return (cast(void *)null)[0 .. (int delegate()).sizeof];
}
override @property uint flags() nothrow pure const { return 1; }
TypeInfo next;
string deco;
override @property size_t talign() nothrow pure const
{
alias dg = int delegate();
return dg.alignof;
}
version (X86_64) override int argTypes(out TypeInfo arg1, out TypeInfo arg2)
{
arg1 = typeid(void*);
arg2 = typeid(void*);
return 0;
}
}
unittest
{
// Bugzilla 15367
void f1() {}
void f2() {}
// TypeInfo_Delegate.getHash
int[void delegate()] aa;
assert(aa.length == 0);
aa[&f1] = 1;
assert(aa.length == 1);
aa[&f1] = 1;
assert(aa.length == 1);
auto a1 = [&f2, &f1];
auto a2 = [&f2, &f1];
// TypeInfo_Delegate.equals
for (auto i = 0; i < 2; i++)
assert(a1[i] == a2[i]);
assert(a1 == a2);
// TypeInfo_Delegate.compare
for (auto i = 0; i < 2; i++)
assert(a1[i] <= a2[i]);
assert(a1 <= a2);
}
/**
* Runtime type information about a class.
* Can be retrieved from an object instance by using the
* $(DDSUBLINK spec/property,classinfo, .classinfo) property.
*/
class TypeInfo_Class : TypeInfo
{
override string toString() const { return info.name; }
override bool opEquals(Object o)
{
if (this is o)
return true;
auto c = cast(const TypeInfo_Class)o;
return c && this.info.name == c.info.name;
}
override size_t getHash(in void* p) @trusted const
{
auto o = *cast(Object*)p;
return o ? o.toHash() : 0;
}
override bool equals(in void* p1, in void* p2) const
{
Object o1 = *cast(Object*)p1;
Object o2 = *cast(Object*)p2;
return (o1 is o2) || (o1 && o1.opEquals(o2));
}
override int compare(in void* p1, in void* p2) const
{
Object o1 = *cast(Object*)p1;
Object o2 = *cast(Object*)p2;
int c = 0;
// Regard null references as always being "less than"
if (o1 !is o2)
{
if (o1)
{
if (!o2)
c = 1;
else
c = o1.opCmp(o2);
}
else
c = -1;
}
return c;
}
override @property size_t tsize() nothrow pure const
{
return Object.sizeof;
}
override const(void)[] initializer() nothrow pure const @safe
{
return m_init;
}
override @property uint flags() nothrow pure const { return 1; }
override @property const(OffsetTypeInfo)[] offTi() nothrow pure const
{
return m_offTi;
}
@property auto info() @safe nothrow pure const { return this; }
@property auto typeinfo() @safe nothrow pure const { return this; }
byte[] m_init; /** class static initializer
* (init.length gives size in bytes of class)
*/
string name; /// class name
void*[] vtbl; /// virtual function pointer table
Interface[] interfaces; /// interfaces this class implements
TypeInfo_Class base; /// base class
void* destructor;
void function(Object) classInvariant;
enum ClassFlags : uint
{
isCOMclass = 0x1,
noPointers = 0x2,
hasOffTi = 0x4,
hasCtor = 0x8,
hasGetMembers = 0x10,
hasTypeInfo = 0x20,
isAbstract = 0x40,
isCPPclass = 0x80,
hasDtor = 0x100,
}
ClassFlags m_flags;
void* deallocator;
OffsetTypeInfo[] m_offTi;
void function(Object) defaultConstructor; // default Constructor
immutable(void)* m_RTInfo; // data for precise GC
override @property immutable(void)* rtInfo() const { return m_RTInfo; }
/**
* Search all modules for TypeInfo_Class corresponding to classname.
* Returns: null if not found
*/
static const(TypeInfo_Class) find(in char[] classname)
{
foreach (m; ModuleInfo)
{
if (m)
{
//writefln("module %s, %d", m.name, m.localClasses.length);
foreach (c; m.localClasses)
{
if (c is null)
continue;
//writefln("\tclass %s", c.name);
if (c.name == classname)