/
interface2.d
1161 lines (905 loc) · 16.7 KB
/
interface2.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
// PERMUTE_ARGS:
extern(C) int printf(const char*, ...);
/*******************************************************/
interface Foo { int bar(); }
void* p1;
class Bar : Foo
{
int bar()
{
printf("Bar.bar(this = %p)\n", this);
p1 = cast(void*)this;
return 0;
}
}
void test1()
{
Bar b = new Bar();
Foo f = b;
printf("b = %p\n", b);
printf("f = %p\n", f);
assert(cast(void*)b !is cast(void*)f);
printf("f.class = '%.*s'\n", f.classinfo.name.length, f.classinfo.name.ptr);
assert(f.classinfo.name == "interface2.Foo");
f.bar();
assert(p1 is cast(void*)b);
Bar b2 = cast(Bar)f;
printf("cast(Bar)f = %p\n", b2);
assert(b is b2);
delete f;
}
/*******************************************************/
interface A {}
interface B:A {}
interface C {}
class D:B,C {}
void test2()
{
D x = new D();
printf("D: %p\n",x);
Object o = x;
printf("o: %p\n",o);
B b = cast(B)o;
printf("B: %p\n",b);
C c = cast(C)o; // boom
printf("C: %p\n",c);
}
/*******************************************************/
interface B3
{
void close();
}
interface C3 : B3
{
}
class A3 : B3
{
void close()
{
}
}
class D3 : A3
{
}
class E3 : D3, C3
{
}
void test3()
{
C3 c = new E3();
delete c;
}
/*******************************************************/
interface K
{
}
interface X
{
}
interface Y : X
{
}
class Z : Y
{
}
void test4()
{
Z z = new Z();
if (cast(K) z)
{
printf("not ok\n");
assert(0);
}
}
/*******************************************************/
interface I5
{
char M ();
}
interface J5 : I5
{
char N ();
}
class A5 : I5
{
char M () { printf("M()\n"); return 'M'; }
}
class B5 : A5, J5
{
char N () { printf("N()\n"); return 'N'; }
}
void test5()
{
I5 f = new B5 ();
char c = f.M();
assert(c == 'M');
}
/*******************************************************/
interface A6
{
void ma ();
}
interface B6
{
void mb ();
}
class C6 : A6, B6
{
void ma () { }
void mb () { }
}
void test6()
{
A6 x = new C6 ();
assert (cast (B6) x);
}
/*******************************************************/
interface D7 { int foo(); }
class A7 : D7 { int foo() { return 1; } }
class B7 : A7
{
override int foo() { return 2; }
D7 me() { return this; }
}
void test7()
{
A7 a = new A7;
B7 b = new B7;
assert(b.me().foo() != a.foo());
}
/*******************************************************/
interface D8 { void foo(); }
class A8 : D8 { void foo() { printf("A8.foo()\n"); } }
class B8 : A8 {}
void test8()
{
B8 b = new B8();
D8 d = cast(D8) b;
d.foo();
}
/*******************************************************/
interface IA9
{
int i1();
}
interface IB9
{
int i2();
}
interface IC9 : IA9, IB9
{
}
class C9 : IC9
{
int i1() { printf("i1\n"); return 1; }
int i2() { printf("i2\n"); return 2; }
}
void f9(IA9 i1, IB9 i2)
{
int i;
printf("f9\n");
i = i1.i1();
assert(i == 1);
i = i2.i2();
assert(i == 2);
}
void test9()
{
IC9 i3 = new C9();
C9 c = new C9();
f9(c, c);
//printf("c = %p, IC9 = %p, IA9 = %p, IB9 = %p\n", c, i3, cast(IA9)i3, cast(IB9)i3);
f9(i3, i3);
}
/*******************************************************/
interface IOne
{
int one ();
}
interface ITwo
{
int two ();
}
interface IThree : IOne, ITwo
{
int three ();
}
class Three : IThree
{
int one () { printf ("one\n"); return 1; }
int two () { printf ("two\n"); return 2; }
int three () { printf ("three\n"); return 3; }
}
void test10()
{
int i;
IThree three = new Three;
i = three.one();
assert(i == 1);
i = three.two();
assert(i == 2);
i = three.three();
assert(i == 3);
ITwo two = cast(ITwo) three;
i = two.two();
assert(i == 2);
}
/*******************************************************/
interface A11{
}
interface B11 : A11{
}
class MyClass : B11{
}
void test11()
{
B11 b = new MyClass();
Object o = cast(Object)b;
printf("o = %p\n", o);
}
/*******************************************************/
interface I12
{
int foo();
}
class IA12 : I12
{
int foo() { return 1; }
}
class A12
{
I12 i;
I12 clone() { return i; }
}
class B12 : A12
{
IA12 ia;
override IA12 clone() // covariant return value
out (result)
{
printf("B12.clone()\n");
}
body
{
return ia;
}
}
void test12()
{
IA12 ia = new IA12;
assert(ia.foo() == 1);
I12 i = ia;
assert(i.foo() == 1);
A12 a = new A12;
a.i = i;
assert(a.clone().foo() == 1);
B12 b = new B12;
b.ia = ia;
assert(b.clone().foo() == 1);
a = b;
assert(a.clone().foo() == 1);
}
/*******************************************************/
class I13
{
int foo() { return 0; }
}
class IA13 : I13
{
override int foo() { return 1; }
}
class A13
{
I13 i;
I13 clone() { return i; }
}
class B13 : A13
{
IA13 ia;
override IA13 clone()
out (result)
{
printf("B13.clone()\n");
}
body { return ia; }
}
void test13()
{
IA13 ia = new IA13;
assert(ia.foo() == 1);
I13 i = ia;
assert(i.foo() == 1);
A13 a = new A13;
a.i = i;
assert(a.clone().foo() == 1);
B13 b = new B13;
b.ia = ia;
assert(b.clone().foo() == 1);
a = b;
assert(a.clone().foo() == 1);
bar(&b.clone);
}
void bar(IA13 delegate() dg)
{
}
/*******************************************************/
interface I14
{
I14 clone();
}
interface BabyI14: I14
{
}
class A14: BabyI14
{
int x;
BabyI14 clone()
{
A14 a = new A14;
a.x = x;
return a;
}
}
I14 foo14(I14 i)
{
return i.clone();
}
void test14()
{
A14 a = new A14;
a.x = 3;
a = cast(A14)a.clone();
assert(a.x == 3);
A14 b = cast(A14)foo14(a);
a.x = 4;
assert(b.x == 3);
}
/*******************************************************/
interface I15
{
Object clone();
}
class A15 : I15
{
int x;
A15 clone() { return this; }
}
void test15()
{
A15 a = new A15;
a.x = 3;
A15 a1 = a.clone();
assert(a1.x == 3);
I15 i = a1;
Object o = i.clone();
A15 a2 = cast(A15) o;
assert(a2.x == 3);
}
/*******************************************************/
interface I16 {}
class A16
{
I16 foo()
{
printf("Called A.foo\n");
return new B16(42);
}
}
class B16 : A16, I16
{
int data;
this(int d) { data = d; }
override B16 foo()
{
printf("Called B.foo\n");
return new B16(69);
}
}
void test16()
{
B16 b = new B16(105);
b.foo();
A16 a = b;
a.foo();
printf("foo\n");
B16 b2 = cast(B16) a.foo();
}
/*******************************************************/
interface Father
{
int showData();
}
class Mother
{
Father test() {
printf("Called Mother.test\n");
return new Child(42);
}
}
class Child : Mother, Father {
int data;
this(int d) { data = d; }
override Child test()
{
printf("Called Child.test\n");
return new Child(69);
}
int showData() {
printf("%d\n", data);
return data;
}
}
void test17()
{
Child aChild = new Child(105);
Mother childsMum = aChild;
aChild.test();
Father mumTest = childsMum.test();
int i;
i = aChild.showData();
assert(i == 105);
i = mumTest.showData();
assert(i == 69);
}
/*******************************************************/
int status18;
interface I18 {
int showData();
}
class Parent18 {
I18 test() {
status18 += 7;
return new Child18(42);
}
}
class Child18 : Parent18, I18 {
int data;
this(int d) {
data = d;
}
override Child18 test() {
status18 += 1;
return new Child18(69);
}
override
int showData(){
return data;
}
}
void test18()
{
Child18 a = new Child18(105);
assert(a);
assert(status18 == 0);
assert(a.data == 105);
Parent18 p = a;
assert(a);
assert(status18 == 0);
a.test();
assert(status18 == 1);
I18 i = p.test();
assert(i);
assert(status18 == 2);
assert(a.data == 105);
assert(a.showData() == 105);
assert(i.showData() == 69);
}
/*******************************************************/
interface IFoo19 {
}
interface ICov19 {
IFoo19 covfunc();
}
class Child19 : ICov19, IFoo19 {
Child19 covfunc() {
printf("in Child19.covfunc()\n");
return this;
}
}
void test19()
{
Child19 c = new Child19();
ICov19 icov = c;
IFoo19 ifoo = icov.covfunc();
printf("c = %p\n", c);
printf("icov = %p\n", icov);
printf("ifoo = %p\n", ifoo);
assert(cast(void*)c + (2*(void*).sizeof) == cast(void*)icov);
assert(cast(void*)c + (3*(void*).sizeof) == cast(void*)ifoo);
string s = ifoo.classinfo.name;
printf("%.*s\n", s.length, s.ptr);
assert(s == "interface2.IFoo19");
s = (cast(Object)ifoo).toString;
printf("%.*s\n", s.length, s.ptr);
assert(s == "interface2.Child19");
}
/*******************************************************/
interface Iface1
{
Iface2 func1();
}
interface Iface2
{
Iface1 func2();
}
class C1_20 : Iface1
{
C2_20 func1(){ return null; }
}
class C2_20 : Iface2
{
C1_20 func2(){ return null; }
}
void test20()
{
C1_20 c1 = new C1_20();
printf("c1.func1() == %p\n", c1.func1());
assert(c1.func1() is null);
printf("test1\n");
C2_20 c2 = new C2_20();
printf("c2.func2() == %p\n", c2.func2());
assert(c2.func2() is null);
}
/*******************************************************/
interface I21
{
int test(int);
}
class C21 : I21
{
int test(int i){
return i+1;
}
}
void test21()
{
C21[I21] aa;
C21 o = new C21();
aa[o] = o;
I21 i = aa[o];
assert(i.test(3) == 4);
}
/*******************************************************/
interface IFoo22
{
int foo();
}
class Foo22: IFoo22
{
final int foo() { return 7; }
}
void test22()
{
Foo22 f = new Foo22;
assert(f.foo() == 7);
IFoo22 i = f;
assert(i.foo() == 7);
}
/*******************************************************/
interface IFoo23
{
int foo();
}
class Foo23: IFoo23
{
final int foo() { return 7; }
}
class Baz23 : Foo23
{
}
void test23()
{
Baz23 f = new Baz23;
assert(f.foo() == 7);
IFoo23 i = f;
assert(i.foo() == 7);
}
/*******************************************************/
interface I24B() : I24A
{
}
interface I24A
{
I24B!() func ();
}
class Foo24 : I24B!()
{
I24B!() func()
{
return null;
}
}
void test24()
{
auto foo = new Foo24();
foo.func();
printf("foo.func() call passed\n");
I24A ifA = foo;
assert(ifA !is null);
ifA.func();
}
/*******************************************************/
interface IA25
{
char a();
}
interface IB25
{
char b();
}
interface IC25 : IA25, IB25
{
char c();
}
interface ID25
{
char d();
}
interface IE25 : IC25, ID25
{
char e();
}
class Foo25 : IE25
{
char a() { return('a'); }
char b() { return('b'); }
char c() { return('c'); }
char d() { return('d'); }
char e() { return('e'); }
}
void test25()
{
auto foo = new Foo25;
printf("Foo: %c %c %c %c %c\n", foo.a, foo.b, foo.c, foo.d, foo.e);
IA25 a = foo;
printf("A: %c\n", a.a);
assert(a.a == 'a');
IB25 b = foo;
printf("B: %c\n", b.b);
assert(b.b == 'b');
IC25 c = foo;
printf("C: %c %c %c\n", c.a, c.b, c.c);
assert(c.a == 'a');
assert(c.b == 'b');
assert(c.c == 'c');
ID25 d = foo;
printf("D: %c\n", d.d);
assert(d.d == 'd');
IE25 e = foo;
printf("E: %c %c %c %c %c\n", e.a, e.b, e.c, e.d, e.e);
assert(e.a == 'a');
assert(e.b == 'b');
assert(e.c == 'c');
assert(e.d == 'd');
assert(e.e == 'e');
b = e;
printf("IB25: %c\n", b.b);
assert(b.b == 'b');
}
/*******************************************************/
interface VisualElement {
void draw();
}
interface Actor {
}
interface VisualActor : Actor, VisualElement {
}
class Sprite3 : Actor, VisualActor {
override void draw() { }
}
void test26()
{
}
/*******************************************************/
interface I27
{
static int foo() { return 3; }
final int bar() { return 7 + abc(); }
int abc();
}
class C27 : I27
{
int x;
int abc() { return x * 10; }
}
void test27()
{
C27 c = new C27();
c.x = 8;
I27 i = c;
assert(i.foo() == 3);
assert(I27.foo() == 3);
assert(i.bar() == 87);
}
/*******************************************************/
// 1747 & 2013
void test1747()
{
interface IA { int mA(); }
interface IB : IA { int mB(); }
interface IC : IB { }
interface ID : IA, IC { int mD(); }
// offset: 0 +n +n + ptrsize
// (IA)
// IB
// IA, IC
static class C : ID
{
int mA() { return 1; }
int mB() { return 2; }
int mD() { return 3; }
}
C c = new C; void* pc = *cast(void**)&c;
ID id = c; void* pid = *cast(void**)&id;
IC ic = c; void* pic = *cast(void**)⁣
IB ib = c; void* pib = *cast(void**)&ib;
IA ia = c; void* pia = *cast(void**)&ia;
//printf(" c = %p\n", pc);
//printf("id = %p\n", pid);
//printf("ic = %p\n", pic);
//printf("ib = %p\n", pib);
//printf("ia = %p\n", pia);
size_t n = pid - pc;
assert(pic == pc + n + (void*).sizeof);
assert(pib == pc + n + (void*).sizeof); // OK <- NG
assert(pia == pc + n);
assert(id.mA() == 1);
assert(id.mB() == 2); // OK <- NG (bugzilla 2013 case)
assert(id.mD() == 3);
assert(ic.mA() == 1);
assert(ic.mB() == 2); // OK <- NG (bugzilla 2013 case)
assert(ib.mA() == 1);
assert(ib.mB() == 2); // OK <- NG
assert(ia.mA() == 1);
}
/*******************************************************/
private interface IFoo
{
void foo();
}
void test2553()
{
IFoo foo;
if (0)
foo.foo;
}
/*******************************************************/
interface I2524
{
void foo();
}
class C2524 : I2524
{
final override void foo() { }
}