Debian Codegraph @

gcc/testsuite/gdc.test/runnable_cxx/cppa.d

raw
1 // REQUIRED_ARGS: -preview=in
2 // PERMUTE_ARGS: -g
3 // EXTRA_CPP_SOURCES: cppb.cpp
4 // EXTRA_FILES: extra-files/cppb.h
5 // CXXFLAGS(linux freebsd osx openbsd netbsd dragonflybsd): -std=c++11
6 // druntime isn't linked, this prevents missing symbols '_d_arraybounds_slicep':
7 // REQUIRED_ARGS: -checkaction=C
8 // TRANSFORM_OUTPUT: remove_lines("warning: vsprintf\(\) is often misused")
9
10 // N.B MSVC doesn't have a C++11 switch, but it defaults to the latest fully-supported standard
11
12 import core.stdc.stdio;
13 import core.stdc.stdarg;
14 import core.stdc.config;
15 import core.stdc.stdint;
16
17 extern (C++)
18 int foob(int i, int j, int k);
19
20 class C
21 {
22 extern (C++) int bar(int i, int j, int k)
23 {
24 printf("this = %p\n", this);
25 printf("i = %d\n", i);
26 printf("j = %d\n", j);
27 printf("k = %d\n", k);
28 return 1;
29 }
30 }
31
32
33 extern (C++)
34 int foo(int i, int j, int k)
35 {
36 printf("i = %d\n", i);
37 printf("j = %d\n", j);
38 printf("k = %d\n", k);
39 assert(i == 1);
40 assert(j == 2);
41 assert(k == 3);
42 return 1;
43 }
44
45 void test1()
46 {
47 foo(1, 2, 3);
48
49 auto i = foob(1, 2, 3);
50 assert(i == 7);
51
52 C c = new C();
53 c.bar(4, 5, 6);
54 }
55
56 /****************************************/
57
58 extern (C++) interface D
59 {
60 int bar(int i, int j, int k);
61 }
62
63 extern (C++) D getD();
64
65 void test2()
66 {
67 D d = getD();
68 int i = d.bar(9,10,11);
69 assert(i == 8);
70 }
71
72 /****************************************/
73
74 extern (C++) int callE(E);
75
76 extern (C++) interface E
77 {
78 int bar(int i, int j, int k);
79 }
80
81 class F : E
82 {
83 extern (C++) int bar(int i, int j, int k)
84 {
85 printf("F.bar: i = %d\n", i);
86 printf("F.bar: j = %d\n", j);
87 printf("F.bar: k = %d\n", k);
88 assert(i == 11);
89 assert(j == 12);
90 assert(k == 13);
91 return 8;
92 }
93 }
94
95 void test3()
96 {
97 F f = new F();
98 int i = callE(f);
99 assert(i == 8);
100 }
101
102 /****************************************/
103
104 extern (C++) void foo4(char* p);
105
106 void test4()
107 {
108 foo4(null);
109 }
110
111 /****************************************/
112
113 extern(C++)
114 {
115 struct foo5 { int i; int j; void* p; }
116
117 interface bar5{
118 foo5 getFoo(int i);
119 }
120
121 bar5 newBar();
122 }
123
124 void test5()
125 {
126 bar5 b = newBar();
127 foo5 f = b.getFoo(4);
128 printf("f.p = %p, b = %p\n", f.p, cast(void*)b);
129 assert(f.p == cast(void*)b);
130 }
131
132
133 /****************************************/
134
135 extern(C++)
136 {
137 struct S6
138 {
139 int i;
140 double d;
141 }
142
143 union S6_2
144 {
145 int i;
146 double d;
147 }
148
149 enum S6_3
150 {
151 A, B
152 }
153
154 S6 foo6();
155 S6_2 foo6_2();
156 S6_3 foo6_3();
157 }
158
159 extern (C) int foosize6();
160
161 void test6()
162 {
163 S6 f = foo6();
164 printf("%d %zd\n", foosize6(), S6.sizeof);
165 assert(foosize6() == S6.sizeof);
166 version (X86)
167 {
168 assert(f.i == 42);
169 printf("f.d = %g\n", f.d);
170 assert(f.d == 2.5);
171 assert(foo6_2().i == 42);
172 assert(foo6_3() == S6_3.A);
173 }
174 }
175
176 /****************************************/
177
178 extern (C) int foo7();
179
180 struct S
181 {
182 int i;
183 long l;
184 }
185
186 void test7()
187 {
188 printf("%d %zd\n", foo7(), S.sizeof);
189 assert(foo7() == S.sizeof);
190 }
191
192 /****************************************/
193
194 extern (C++) void foo8(const(char)*);
195
196 void test8()
197 {
198 char c;
199 foo8(&c);
200 }
201
202 /****************************************/
203 // https://issues.dlang.org/show_bug.cgi?id=4059
204
205 struct elem9 { }
206
207 extern(C++) void foobar9(elem9*, elem9*);
208
209 void test9()
210 {
211 elem9 *a;
212 foobar9(a, a);
213 }
214
215 /****************************************/
216
217
218 struct A11802;
219 struct B11802;
220
221 extern(C++) class C11802
222 {
223 int x;
224 void fun(A11802*) { x += 2; }
225 void fun(B11802*) { x *= 2; }
226 }
227
228 extern(C++) class D11802 : C11802
229 {
230 override void fun(A11802*) { x += 3; }
231 override void fun(B11802*) { x *= 3; }
232 }
233
234 extern(C++) void test11802x(D11802);
235
236 void test11802()
237 {
238 auto x = new D11802();
239 x.x = 0;
240 test11802x(x);
241 assert(x.x == 9);
242 }
243
244
245 /****************************************/
246
247 struct S13956
248 {
249 }
250
251 extern(C++) void func13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
252
253 extern(C++) void check13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)
254 {
255 assert(arg0 == S13956());
256 assert(arg1 == 1);
257 assert(arg2 == 2);
258 assert(arg3 == 3);
259 assert(arg4 == 4);
260 assert(arg5 == 5);
261 assert(arg6 == 6);
262 }
263
264 void test13956()
265 {
266 func13956(S13956(), 1, 2, 3, 4, 5, 6);
267 }
268
269 /****************************************/
270 // https://issues.dlang.org/show_bug.cgi?id=5148
271
272 extern (C++)
273 {
274 void foo10(const(char)*, const(char)*);
275 void foo10(const int, const int);
276 void foo10(const char, const char);
277 void foo10(bool, bool);
278
279 struct MyStructType { }
280 void foo10(const MyStructType s, const MyStructType t);
281
282 enum MyEnumType { onemember }
283 void foo10(const MyEnumType s, const MyEnumType t);
284 }
285
286 void test10()
287 {
288 char* p;
289 foo10(p, p);
290 foo10(1,2);
291 foo10('c','d');
292 MyStructType s;
293 foo10(s,s);
294 MyEnumType e;
295 foo10(e,e);
296 }
297
298 /****************************************/
299
300 extern (C++, N11.M) { void bar11(); }
301
302 extern (C++, A11.B) { extern (C++, C) { void bar(); }}
303
304 void test11()
305 {
306 bar11();
307 A11.B.C.bar();
308 }
309 /****************************************/
310
311 struct Struct10071
312 {
313 void *p;
314 c_long_double r;
315 }
316
317 extern(C++) size_t offset10071();
318 void test10071()
319 {
320 assert(offset10071() == Struct10071.r.offsetof);
321 }
322
323 /****************************************/
324
325 char[100] valistbuffer;
326
327 extern(C++) void myvprintfx(const(char)* format, va_list va)
328 {
329 vsprintf(valistbuffer.ptr, format, va);
330 }
331 extern(C++) void myvprintf(const(char)*, va_list);
332 extern(C++) void myprintf(const(char)* format, ...)
333 {
334 va_list ap;
335 va_start(ap, format);
336 myvprintf(format, ap);
337 va_end(ap);
338 }
339
340 void testvalist()
341 {
342 myprintf("hello %d", 999);
343 assert(valistbuffer[0..9] == "hello 999");
344 }
345
346 /****************************************/
347 // https://issues.dlang.org/show_bug.cgi?id=12825
348
349 extern(C++) class C12825
350 {
351 uint a = 0x12345678;
352 }
353
354 void test12825()
355 {
356 auto c = new C12825();
357 }
358
359 /****************************************/
360
361 struct S13955a
362 {
363 float a;
364 double b;
365 }
366
367 struct S13955b
368 {
369 double a;
370 float b;
371 }
372
373 struct S13955c
374 {
375 float a;
376 float b;
377 }
378
379 struct S13955d
380 {
381 double a;
382 double b;
383 }
384
385 extern(C++) void check13955(S13955a a, S13955b b, S13955c c, S13955d d)
386 {
387 assert(a.a == 2);
388 assert(a.b == 4);
389 assert(b.a == 8);
390 assert(b.b == 16);
391 assert(c.a == 32);
392 assert(c.b == 64);
393 assert(d.a == 128);
394 assert(d.b == 256);
395 }
396
397 extern(C++) void func13955(S13955a a, S13955b b, S13955c c, S13955d d);
398
399 void test13955()
400 {
401 func13955(S13955a(2, 4), S13955b(8, 16), S13955c(32, 64), S13955d(128, 256));
402 }
403
404 /****************************************/
405
406 extern(C++) class C13161
407 {
408 void dummyfunc();
409 long val_5;
410 uint val_9;
411 }
412
413 extern(C++) class Test : C13161
414 {
415 uint val_0;
416 long val_1;
417 }
418
419 extern(C++) size_t getoffset13161();
420
421 extern(C++) class C13161a
422 {
423 void dummyfunc();
424 c_long_double val_5;
425 uint val_9;
426 }
427
428 extern(C++) class Testa : C13161a
429 {
430 bool val_0;
431 }
432
433 extern(C++) size_t getoffset13161a();
434
435 void test13161()
436 {
437 assert(getoffset13161() == Test.val_0.offsetof);
438 assert(getoffset13161a() == Testa.val_0.offsetof);
439 }
440
441 /****************************************/
442
443 extern (C++, std)
444 {
445 extern (C++, class) struct allocator(T)
446 {
447 }
448
449 class vector(T, A = allocator!T)
450 {
451 final void push_back(ref const T);
452 }
453
454 struct char_traits(T)
455 {
456 }
457
458 version (CppRuntime_GNU)
459 {
460 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
461 static if (__traits(getTargetInfo, "cppStd") >= 201103)
462 {
463 extern (C++, __cxx11)
464 {
465 struct basic_string(T, C = char_traits!T, A = allocator!T)
466 {
467 }
468 }
469 }
470 else
471 {
472 extern (C++, class) struct basic_string(T, C = char_traits!T, A = allocator!T)
473 {
474 }
475 }
476 }
477 else
478 {
479 extern (C++, class) struct basic_string(T, C = char_traits!T, A = allocator!T)
480 {
481 }
482 }
483
484 struct basic_istream(T, C = char_traits!T)
485 {
486 }
487
488 struct basic_ostream(T, C = char_traits!T)
489 {
490 }
491
492 struct basic_iostream(T, C = char_traits!T)
493 {
494 }
495
496 class exception { }
497
498 // https://issues.dlang.org/show_bug.cgi?id=14956
499 extern(C++, N14956)
500 {
501 struct S14956 { }
502 }
503 }
504
505 extern (C++)
506 {
507 version (linux)
508 {
509 void foo14(std.vector!(int) p);
510 void foo14a(std.basic_string!(char) *p);
511 void foo14b(std.basic_string!(int) *p);
512 void foo14c(std.basic_istream!(char) *p);
513 void foo14d(std.basic_ostream!(char) *p);
514 void foo14e(std.basic_iostream!(char) *p);
515
516 void foo14f(std.char_traits!char* x, std.basic_string!char* p, std.basic_string!char* q);
517 }
518 }
519
520 void test14()
521 {
522 version (linux)
523 {
524 std.vector!int p;
525 foo14(p);
526
527 foo14a(null);
528 foo14b(null);
529 foo14c(null);
530 foo14d(null);
531 foo14e(null);
532 foo14f(null, null, null);
533 }
534 }
535
536 version (linux)
537 {
538 void gun(std.vector!int pa)
539 {
540 int x = 42;
541 pa.push_back(x);
542 }
543 }
544
545 void test13289()
546 {
547 assert(f13289_cpp_wchar_t('a') == 'A');
548 assert(f13289_cpp_wchar_t('B') == 'B');
549 assert(f13289_d_wchar('c') == 'C');
550 assert(f13289_d_wchar('D') == 'D');
551 assert(f13289_d_dchar('e') == 'E');
552 assert(f13289_d_dchar('F') == 'F');
553 assert(f13289_cpp_test());
554 }
555
556 version(Posix)
557 {
558 enum __c_wchar_t : dchar;
559 }
560 else version(Windows)
561 {
562 enum __c_wchar_t : wchar;
563 }
564 alias wchar_t = __c_wchar_t;
565 extern(C++)
566 {
567 bool f13289_cpp_test();
568
569
570 wchar_t f13289_cpp_wchar_t(wchar_t);
571
572
573 wchar f13289_d_wchar(wchar ch)
574 {
575 if (ch <= 'z' && ch >= 'a')
576 {
577 return cast(wchar)(ch - ('a' - 'A'));
578 }
579 else
580 {
581 return ch;
582 }
583 }
584 dchar f13289_d_dchar(dchar ch)
585 {
586 if (ch <= 'z' && ch >= 'a')
587 {
588 return ch - ('a' - 'A');
589 }
590 else
591 {
592 return ch;
593 }
594 }
595 wchar_t f13289_d_wchar_t(wchar_t ch)
596 {
597 if (ch <= 'z' && ch >= 'a')
598 {
599 return cast(wchar_t)(ch - ('a' - 'A'));
600 }
601 else
602 {
603 return ch;
604 }
605 }
606 }
607
608 /****************************************/
609
610 version (CRuntime_Microsoft)
611 {
612 enum __c_long_double : double;
613 alias __c_long_double myld;
614 }
615 else
616 alias c_long_double myld;
617
618 extern (C++) myld testld(myld);
619 extern (C++) myld testldld(myld, myld);
620
621
622 void test15()
623 {
624 myld ld = 5.0;
625 ld = testld(ld);
626 assert(ld == 6.0);
627
628 myld ld2 = 5.0;
629 ld2 = testldld(ld2, ld2);
630 assert(ld2 == 6.0);
631 }
632
633 /****************************************/
634
635 version( Windows )
636 {
637 alias int x_long;
638 alias uint x_ulong;
639 }
640 else
641 {
642 static if( (void*).sizeof > int.sizeof )
643 {
644 alias long x_long;
645 alias ulong x_ulong;
646 }
647 else
648 {
649 alias int x_long;
650 alias uint x_ulong;
651 }
652 }
653
654 enum __c_long : x_long;
655 enum __c_ulong : x_ulong;
656 alias __c_long mylong;
657 alias __c_ulong myulong;
658
659 extern (C++) mylong testl(mylong);
660 extern (C++) myulong testul(myulong);
661
662
663 void test16()
664 {
665 {
666 mylong ld = 5;
667 ld = testl(ld);
668 printf("ld = %lld, mylong.sizeof = %lld\n", cast(long)ld, cast(long)mylong.sizeof);
669 assert(ld == 5 + mylong.sizeof);
670 }
671 {
672 myulong ld = 5;
673 ld = testul(ld);
674 assert(ld == 5 + myulong.sizeof);
675 }
676
677 static if (__c_long.sizeof == long.sizeof)
678 {
679 static assert(__c_long.max == long.max);
680 static assert(__c_long.min == long.min);
681 static assert(__c_long.init == long.init);
682
683 static assert(__c_ulong.max == ulong.max);
684 static assert(__c_ulong.min == ulong.min);
685 static assert(__c_ulong.init == ulong.init);
686
687 __c_long cl = 0;
688 cl = cl + 1;
689 long l = cl;
690 cl = l;
691
692 __c_ulong cul = 0;
693 cul = cul + 1;
694 ulong ul = cul;
695 cul = ul;
696 }
697 else static if (__c_long.sizeof == int.sizeof)
698 {
699 static assert(__c_long.max == int.max);
700 static assert(__c_long.min == int.min);
701 static assert(__c_long.init == int.init);
702
703 static assert(__c_ulong.max == uint.max);
704 static assert(__c_ulong.min == uint.min);
705 static assert(__c_ulong.init == uint.init);
706
707 __c_long cl = 0;
708 cl = cl + 1;
709 int i = cl;
710 cl = i;
711
712 __c_ulong cul = 0;
713 cul = cul + 1;
714 uint u = cul;
715 cul = u;
716 }
717 else
718 static assert(0);
719 }
720
721 /****************************************/
722
723 struct S13707
724 {
725 void* a;
726 void* b;
727 this(void* a, void* b)
728 {
729 this.a = a;
730 this.b = b;
731 }
732 }
733
734 extern(C++) S13707 func13707();
735
736 void test13707()
737 {
738 auto p = func13707();
739 assert(p.a == null);
740 assert(p.b == null);
741 }
742
743 /****************************************/
744
745 struct S13932(int x)
746 {
747 int member;
748 }
749
750 extern(C++) void func13932(S13932!(-1) s);
751
752 /****************************************/
753
754 extern(C++, N13337.M13337)
755 {
756 struct S13337{}
757 void foo13337(S13337 s);
758 }
759
760 /****************************************/
761 // https://issues.dlang.org/show_bug.cgi?id=14195
762
763 struct Delegate1(T) {}
764 struct Delegate2(T1, T2) {}
765
766 template Signature(T)
767 {
768 alias Signature = typeof(*(T.init));
769 }
770
771 extern(C++)
772 {
773 alias del1_t = Delegate1!(Signature!(void function()));
774 alias del2_t = Delegate2!(Signature!(int function(float, double)), Signature!(int function(float, double)));
775 void test14195a(del1_t);
776 void test14195b(del2_t);
777 }
778
779 void test14195()
780 {
781 test14195a(del1_t());
782 test14195b(del2_t());
783 }
784
785
786 /****************************************/
787 // https://issues.dlang.org/show_bug.cgi?id=14200
788
789 template Tuple14200(T...)
790 {
791 alias Tuple14200 = T;
792 }
793
794 extern(C++) void test14200a(Tuple14200!(int));
795 extern(C++) void test14200b(float, Tuple14200!(int, double));
796
797 void test14200()
798 {
799 test14200a(1);
800 test14200b(1.0f, 1, 1.0);
801 }
802
803 /****************************************/
804 // https://issues.dlang.org/show_bug.cgi?id=14956
805
806 extern(C++) void test14956(S14956 s);
807
808 /****************************************/
809 // check order of overloads in vtable
810
811 extern (C++) class Statement {}
812 extern (C++) class ErrorStatement {}
813 extern (C++) class PeelStatement {}
814 extern (C++) class ExpStatement {}
815 extern (C++) class DtorExpStatement {}
816
817 extern (C++) class Visitor
818 {
819 public:
820 int visit(Statement) { return 1; }
821 int visit(ErrorStatement) { return 2; }
822 int visit(PeelStatement) { return 3; }
823 }
824
825 extern (C++) class Visitor2 : Visitor
826 {
827 int visit2(ExpStatement) { return 4; }
828 int visit2(DtorExpStatement) { return 5; }
829 }
830
831 extern(C++) bool testVtableCpp(Visitor2 sv);
832 extern(C++) Visitor2 getVisitor2();
833
834 bool testVtableD(Visitor2 sv)
835 {
836 Statement s1;
837 ErrorStatement s2;
838 PeelStatement s3;
839 ExpStatement s4;
840 DtorExpStatement s5;
841
842 if (sv.visit(s1) != 1) return false;
843 if (sv.visit(s2) != 2) return false;
844 if (sv.visit(s3) != 3) return false;
845 if (sv.visit2(s4) != 4) return false;
846 if (sv.visit2(s5) != 5) return false;
847 return true;
848 }
849
850 void testVtable()
851 {
852 Visitor2 dinst = new Visitor2;
853 if (!testVtableCpp(dinst))
854 assert(0);
855
856 Visitor2 cppinst = getVisitor2();
857 if (!testVtableD(cppinst))
858 assert(0);
859 }
860
861 /****************************************/
862 /* problems detected by fuzzer */
863 extern(C++) void fuzz1_cppvararg(int64_t arg10, int64_t arg11, bool arg12);
864 extern(C++) void fuzz1_dvararg(int64_t arg10, int64_t arg11, bool arg12)
865 {
866 fuzz1_checkValues(arg10, arg11, arg12);
867 }
868
869 extern(C++) void fuzz1_checkValues(int64_t arg10, int64_t arg11, bool arg12)
870 {
871 assert(arg10 == 103);
872 assert(arg11 == 104);
873 assert(arg12 == false);
874 }
875
876 void fuzz1()
877 {
878 long arg10 = 103;
879 long arg11 = 104;
880 bool arg12 = false;
881 fuzz1_dvararg(arg10, arg11, arg12);
882 fuzz1_cppvararg(arg10, arg11, arg12);
883 }
884
885 ////////
886 extern(C++) void fuzz2_cppvararg(uint64_t arg10, uint64_t arg11, bool arg12);
887 extern(C++) void fuzz2_dvararg(uint64_t arg10, uint64_t arg11, bool arg12)
888 {
889 fuzz2_checkValues(arg10, arg11, arg12);
890 }
891
892 extern(C++) void fuzz2_checkValues(uint64_t arg10, uint64_t arg11, bool arg12)
893 {
894 assert(arg10 == 103);
895 assert(arg11 == 104);
896 assert(arg12 == false);
897 }
898
899 void fuzz2()
900 {
901 ulong arg10 = 103;
902 ulong arg11 = 104;
903 bool arg12 = false;
904 fuzz2_dvararg(arg10, arg11, arg12);
905 fuzz2_cppvararg(arg10, arg11, arg12);
906 }
907
908 ////////
909 version(CppRuntime_Microsoft)
910 enum UNICODE = false; //VS2013 doesn't support them
911 else
912 enum UNICODE = true;
913
914 static if (UNICODE)
915 {
916 extern(C++) void fuzz3_cppvararg(wchar arg10, dchar arg11, bool arg12);
917 extern(C++) void fuzz3_dvararg(wchar arg10, dchar arg11, bool arg12)
918 {
919 fuzz2_checkValues(arg10, arg11, arg12);
920 }
921
922 extern(C++) void fuzz3_checkValues(wchar arg10, dchar arg11, bool arg12)
923 {
924 assert(arg10 == 103);
925 assert(arg11 == 104);
926 assert(arg12 == false);
927 }
928
929 void fuzz3()
930 {
931 wchar arg10 = 103;
932 dchar arg11 = 104;
933 bool arg12 = false;
934 fuzz3_dvararg(arg10, arg11, arg12);
935 fuzz3_cppvararg(arg10, arg11, arg12);
936 }
937 }
938
939 void fuzz()
940 {
941 fuzz1();
942 fuzz2();
943 static if (UNICODE) fuzz3();
944 }
945
946 /****************************************/
947
948 extern (C++)
949 {
950 void throwit();
951 }
952
953 void testeh()
954 {
955 printf("testeh()\n");
956 version (linux)
957 {
958 version (X86_64)
959 {
960 bool caught;
961 try
962 {
963 throwit();
964 }
965 catch (std.exception e)
966 {
967 caught = true;
968 }
969 assert(caught);
970 }
971 }
972 }
973
974 /****************************************/
975
976 version (linux)
977 {
978 version (X86_64)
979 {
980 bool raii_works = false;
981 struct RAIITest
982 {
983 ~this()
984 {
985 raii_works = true;
986 }
987 }
988
989 void dFunction()
990 {
991 RAIITest rt;
992 throwit();
993 }
994
995 void testeh2()
996 {
997 printf("testeh2()\n");
998 try
999 {
1000 dFunction();
1001 }
1002 catch(std.exception e)
1003 {
1004 assert(raii_works);
1005 }
1006 }
1007 }
1008 else
1009 void testeh2() { }
1010 }
1011 else
1012 void testeh2() { }
1013
1014 /****************************************/
1015
1016 extern (C++) { void throwle(); void throwpe(); }
1017
1018 void testeh3()
1019 {
1020 printf("testeh3()\n");
1021 version (linux)
1022 {
1023 version (X86_64)
1024 {
1025 bool caught = false;
1026 try
1027 {
1028 throwle();
1029 }
1030 catch (std.exception e) //polymorphism test.
1031 {
1032 caught = true;
1033 }
1034 assert(caught);
1035 }
1036 }
1037 }
1038
1039 /****************************************/
1040 // https://issues.dlang.org/show_bug.cgi?id=15576
1041
1042 extern (C++, ns15576)
1043 {
1044 extern __gshared int global15576;
1045
1046 extern (C++, ns)
1047 {
1048 extern __gshared int n_global15576;
1049 }
1050 }
1051
1052 void test15576()
1053 {
1054 global15576 = n_global15576 = 123;
1055 }
1056
1057 /****************************************/
1058 // https://issues.dlang.org/show_bug.cgi?id=15579
1059
1060 version (DigitalMars)
1061 {
1062 version (linux)
1063 {
1064 // Test removed for DMD/linux-only.
1065 // https://issues.dlang.org/show_bug.cgi?id=23660
1066 }
1067 else
1068 version = TEST15579;
1069 }
1070 else
1071 version = TEST15579;
1072
1073 version (TEST15579)
1074 {
1075 extern (C++)
1076 {
1077 class Base
1078 {
1079 //~this() {}
1080 void based() { }
1081 ubyte x = 4;
1082 }
1083
1084 interface Interface
1085 {
1086 int MethodCPP();
1087 int MethodD();
1088 }
1089
1090 class Derived : Base, Interface
1091 {
1092 short y = 5;
1093 int MethodCPP();
1094 int MethodD() {
1095 printf("Derived.MethodD(): this = %p, x = %d, y = %d\n", this, x, y);
1096 Derived p = this;
1097 //p = cast(Derived)(cast(void*)p - 16);
1098 assert(p.x == 4 || p.x == 7);
1099 assert(p.y == 5 || p.y == 8);
1100 return 3;
1101 }
1102 int Method() { return 6; }
1103 }
1104
1105 Derived cppfoo(Derived);
1106 Interface cppfooi(Interface);
1107 }
1108
1109 void test15579()
1110 {
1111 Derived d = new Derived();
1112 printf("d = %p\n", d);
1113 assert(d.x == 4);
1114 assert(d.y == 5);
1115 assert((cast(Interface)d).MethodCPP() == 30);
1116 assert((cast(Interface)d).MethodD() == 3);
1117 assert(d.MethodCPP() == 30);
1118 assert(d.MethodD() == 3);
1119 assert(d.Method() == 6);
1120
1121 d = cppfoo(d);
1122 assert(d.x == 7);
1123 assert(d.y == 8);
1124
1125 printf("d2 = %p\n", d);
1126
1127 /* Casting to an interface involves thunks in the vtbl[].
1128 * g++ puts the thunks for MethodD in the same COMDAT as MethodD.
1129 * But D doesn't, so when the linker "picks one" of the D generated MethodD
1130 * or the g++ generated MethodD, it may wind up with a messed up thunk,
1131 * resulting in a seg fault. The solution is to not expect objects of the same
1132 * type to be constructed on both sides of the D/C++ divide if the same member
1133 * function (in this case, MethodD) is also defined on both sides.
1134 */
1135 version (Windows)
1136 {
1137 assert((cast(Interface)d).MethodD() == 3);
1138 }
1139 assert((cast(Interface)d).MethodCPP() == 30);
1140
1141 assert(d.Method() == 6);
1142
1143 printf("d = %p, i = %p\n", d, cast(Interface)d);
1144 version (Windows)
1145 {
1146 Interface i = cppfooi(d);
1147 printf("i2: %p\n", i);
1148 assert(i.MethodD() == 3);
1149 assert(i.MethodCPP() == 30);
1150 }
1151 printf("test15579() done\n");
1152 }
1153 }
1154
1155 /****************************************/
1156 // https://issues.dlang.org/show_bug.cgi?id=15610
1157
1158 extern(C++) class Base2
1159 {
1160 int i;
1161 void baser() { }
1162 }
1163
1164 extern(C++) interface Interface2 { abstract void f(); }
1165
1166 extern(C++) class Derived2 : Base2, Interface2
1167 {
1168 final
1169 override void f();
1170 }
1171
1172
1173 void test15610()
1174 {
1175 auto c = new Derived2();
1176 printf("test15610(): c = %p\n", c);
1177 c.i = 3;
1178 c.f();
1179 }
1180
1181 /******************************************/
1182 // https://issues.dlang.org/show_bug.cgi?id=15455
1183
1184 struct X6
1185 {
1186 ushort a;
1187 ushort b;
1188 ubyte c;
1189 ubyte d;
1190 }
1191
1192 static assert(X6.sizeof == 6);
1193
1194 struct X8
1195 {
1196 ushort a;
1197 X6 b;
1198 }
1199
1200 static assert(X8.sizeof == 8);
1201
1202 void test15455a(X8 s)
1203 {
1204 assert(s.a == 1);
1205 assert(s.b.a == 2);
1206 assert(s.b.b == 3);
1207 assert(s.b.c == 4);
1208 assert(s.b.d == 5);
1209 }
1210
1211 extern (C++) void test15455b(X8 s);
1212
1213 void test15455()
1214 {
1215 X8 s;
1216
1217 s.a = 1;
1218 s.b.a = 2;
1219 s.b.b = 3;
1220 s.b.c = 4;
1221 s.b.d = 5;
1222 test15455a(s);
1223 test15455b(s);
1224 }
1225
1226 /****************************************/
1227 // https://issues.dlang.org/show_bug.cgi?id=15372
1228
1229 extern(C++) int foo15372(T)(int v);
1230
1231 void test15372()
1232 {
1233 version(Windows){}
1234 else
1235 assert(foo15372!int(1) == 1);
1236 }
1237
1238 /****************************************/
1239 // https://issues.dlang.org/show_bug.cgi?id=15802
1240
1241 extern(C++) {
1242 template Foo15802(T) {
1243 static int boo(T v);
1244 }
1245 }
1246
1247 void test15802()
1248 {
1249 version(Windows){}
1250 else
1251 assert(Foo15802!(int).boo(1) == 1);
1252 }
1253
1254 /****************************************/
1255 // https://issues.dlang.org/show_bug.cgi?id=16536
1256 // mangling mismatch on OSX
1257
1258 version(OSX) extern(C++) uint64_t pass16536(uint64_t);
1259
1260 void test16536()
1261 {
1262 version(OSX) assert(pass16536(123) == 123);
1263 }
1264
1265 /****************************************/
1266 // https://issues.dlang.org/show_bug.cgi?id=15589
1267 // extern(C++) virtual destructors are not put in vtbl[]
1268
1269 extern(C++)
1270 {
1271 class A15589
1272 {
1273 extern(D) static int[] dtorSeq;
1274 struct S
1275 {
1276 this(int x) { this.x = x; }
1277 ~this() { dtorSeq ~= x; }
1278 int x;
1279 }
1280 int foo() { return 100; } // shift dtor to slot 1
1281 ~this() { dtorSeq ~= 10; }
1282 S s1 = S(1);
1283 S s2 = S(2);
1284 }
1285 class B15589 : A15589
1286 {
1287 int bar() { return 200;} // add an additional function AFTER the dtor at slot 2
1288 ~this() { dtorSeq ~= 20; }
1289 S s3 = S(3);
1290 }
1291
1292 void test15589b(A15589 p);
1293 }
1294
1295 void test15589()
1296 {
1297 A15589 c = new B15589;
1298 assert(A15589.dtorSeq == null);
1299 assert(c.foo() == 100);
1300 assert((cast(B15589)c).bar() == 200);
1301 c.__xdtor(); // virtual dtor call
1302 assert(A15589.dtorSeq[] == [ 20, 3, 10, 2, 1 ]); // destroyed full hierarchy!
1303
1304 A15589.dtorSeq = null;
1305 test15589b(c);
1306 assert(A15589.dtorSeq[] == [ 20, 3, 10, 2, 1 ]); // destroyed full hierarchy!
1307 }
1308
1309 extern(C++)
1310 {
1311 class Cpp15589Base
1312 {
1313 public:
1314 final ~this();
1315
1316 void nonVirtual();
1317 int a;
1318 }
1319
1320 class Cpp15589Derived : Cpp15589Base
1321 {
1322 public:
1323 this() scope;
1324 final ~this();
1325 int b;
1326 }
1327
1328 class Cpp15589BaseVirtual
1329 {
1330 public:
1331 void beforeDtor();
1332
1333 this() scope;
1334 ~this();
1335
1336 void afterDtor();
1337 int c = 1;
1338 }
1339
1340 class Cpp15589DerivedVirtual : Cpp15589BaseVirtual
1341 {
1342 public:
1343 this() scope;
1344 ~this();
1345
1346 override void afterDtor();
1347
1348 int d;
1349 }
1350
1351 class Cpp15589IntroducingVirtual : Cpp15589Base
1352 {
1353 public:
1354 this() scope;
1355 void beforeIntroducedVirtual();
1356 ~this();
1357 void afterIntroducedVirtual(int);
1358
1359 int e;
1360 }
1361
1362 struct Cpp15589Struct
1363 {
1364 ~this();
1365 int s;
1366 }
1367
1368 void trace15589(int ch)
1369 {
1370 traceBuf[traceBufPos++] = cast(char) ch;
1371 }
1372 }
1373
1374 __gshared char[32] traceBuf;
1375 __gshared size_t traceBufPos;
1376
1377 // workaround for https://issues.dlang.org/show_bug.cgi?id=18986
1378 version(OSX)
1379 enum cppCtorReturnsThis = false;
1380 else version(FreeBSD)
1381 enum cppCtorReturnsThis = false;
1382 else
1383 enum cppCtorReturnsThis = true;
1384
1385 mixin template scopeAllocCpp(C)
1386 {
1387 static if (cppCtorReturnsThis)
1388 scope C ptr = new C;
1389 else
1390 {
1391 ubyte[__traits(classInstanceSize, C)] data;
1392 C ptr = (){ auto p = cast(C) data.ptr; p.__ctor(); return p; }();
1393 }
1394 }
1395
1396 void test15589b()
1397 {
1398 traceBufPos = 0;
1399 {
1400 Cpp15589Struct struc = Cpp15589Struct();
1401 mixin scopeAllocCpp!Cpp15589Derived derived;
1402 mixin scopeAllocCpp!Cpp15589DerivedVirtual derivedVirtual;
1403 mixin scopeAllocCpp!Cpp15589IntroducingVirtual introducingVirtual;
1404
1405 // `scope` instances are destroyed automatically
1406 static if (!cppCtorReturnsThis)
1407 {
1408 introducingVirtual.ptr.destroy();
1409 derivedVirtual.ptr.destroy();
1410 derived.ptr.destroy();
1411 }
1412 }
1413 printf("traceBuf15589 %.*s\n", cast(int)traceBufPos, traceBuf.ptr);
1414 assert(traceBuf[0..traceBufPos] == "IbVvBbs");
1415 }
1416
1417 /****************************************/
1418
1419 // https://issues.dlang.org/show_bug.cgi?id=18928
1420 // Win64: extern(C++) bad codegen, wrong calling convention
1421
1422 extern(C++) struct Small18928
1423 {
1424 int x;
1425 }
1426
1427 extern(C++) class CC18928
1428 {
1429 Small18928 getVirtual(); // { return S(3); }
1430 final Small18928 getFinal(); // { return S(4); }
1431 static Small18928 getStatic(); // { return S(5); }
1432 }
1433
1434 extern(C++) CC18928 newCC18928();
1435
1436 void test18928()
1437 {
1438 auto cc = newCC18928();
1439 Small18928 v = cc.getVirtual();
1440 assert(v.x == 3);
1441 Small18928 f = cc.getFinal();
1442 assert(f.x == 4);
1443 Small18928 s = cc.getStatic();
1444 assert(s.x == 5);
1445 }
1446
1447 /****************************************/
1448 // https://issues.dlang.org/show_bug.cgi?id=18953
1449 // Win32: extern(C++) struct destructor not called correctly through runtime
1450
1451 extern(C++)
1452 struct S18953
1453 {
1454 char x;
1455 ~this() nothrow @nogc { traceBuf[traceBufPos++] = x; }
1456 }
1457
1458 void test18953()
1459 {
1460 traceBufPos = 0;
1461 S18953[] arr = new S18953[3];
1462 arr[1].x = '1';
1463 arr[2].x = '2';
1464 arr.length = 1;
1465 assumeSafeAppend(arr); // destroys arr[1] and arr[2]
1466 printf("traceBuf18953 %.*s\n", cast(int)traceBufPos, traceBuf.ptr);
1467 assert(traceBuf[0..traceBufPos] == "21");
1468 }
1469
1470 /****************************************/
1471
1472 // https://issues.dlang.org/show_bug.cgi?id=18966
1473
1474 extern(C++):
1475 class Base18966
1476 {
1477 this() @safe nothrow;
1478 ~this() @safe;
1479 void vf();
1480 int x;
1481 }
1482
1483 class Derived18966 : Base18966
1484 {
1485 override void vf() { x = 200; }
1486 }
1487
1488 class Explicit18966 : Base18966
1489 {
1490 this() @safe { super(); }
1491 override void vf() { x = 250; }
1492 }
1493
1494 class Implicit18966 : Base18966
1495 {
1496 this() nothrow {}
1497 override void vf() { x = 300; }
1498 }
1499
1500 // test vptr in full ctor chain of mixed D/C++ class hierarchies
1501
1502 // TODO: Make this a D class and let C++ derive from it. This works on Windows,
1503 // but results in linker errors on Posix due to extra base ctor (`C2`
1504 // mangling) being called by the B ctor.
1505 class A18966 // in C++
1506 {
1507 char[8] calledOverloads = 0;
1508 int i;
1509 this();
1510 void foo();
1511 }
1512
1513 class B18966 : A18966 // in C++
1514 {
1515 this();
1516 override void foo();
1517 }
1518
1519 class C18966 : B18966
1520 {
1521 this() { foo(); }
1522 override void foo() { calledOverloads[i++] = 'C'; }
1523 }
1524
1525 class D18966 : C18966
1526 {
1527 this() { foo(); }
1528 override void foo() { calledOverloads[i++] = 'D'; }
1529 }
1530
1531 void test18966()
1532 {
1533 Derived18966 d = new Derived18966;
1534 assert(d.x == 10);
1535 d.vf();
1536 assert(d.x == 200);
1537
1538 Explicit18966 e = new Explicit18966;
1539 assert(e.x == 10);
1540 e.vf();
1541 assert(e.x == 250);
1542
1543 Implicit18966 i = new Implicit18966;
1544 assert(i.x == 10);
1545 i.vf();
1546 assert(i.x == 300);
1547
1548 // TODO: Allocating + constructing a C++ class with the D GC is not
1549 // supported on Posix. The returned pointer (probably from C++ ctor)
1550 // seems to be an offset and not the actual object address.
1551 version (Windows)
1552 {
1553 auto a = new A18966;
1554 assert(a.calledOverloads[0..2] == "A\0");
1555
1556 auto b = new B18966;
1557 assert(b.calledOverloads[0..3] == "AB\0");
1558 }
1559
1560 auto c = new C18966;
1561 assert(c.calledOverloads[0..4] == "ABC\0");
1562
1563 auto d2 = new D18966;
1564 // note: the vptr semantics in ctors of extern(C++) classes may be revised (to "ABCD")
1565 assert(d2.calledOverloads[0..5] == "ABDD\0");
1566 }
1567
1568 /****************************************/
1569
1570 // https://issues.dlang.org/show_bug.cgi?id=19134
1571
1572 class Base19134
1573 {
1574 int a = 123;
1575 this() { a += 42; }
1576 int foo() const { return a; }
1577 }
1578
1579 class Derived19134 : Base19134
1580 {
1581 int b = 666;
1582 this()
1583 {
1584 a *= 2;
1585 b -= 6;
1586 }
1587 override int foo() const { return b; }
1588 }
1589
1590 void test19134()
1591 {
1592 static const d = new Derived19134;
1593 assert(d.a == (123 + 42) * 2);
1594 assert(d.b == 666 - 6);
1595 assert(d.foo() == 660);
1596 }
1597
1598 // https://issues.dlang.org/show_bug.cgi?id=18955
1599 version (linux)
1600 alias std_string = std.basic_string!(char);
1601 else
1602 {
1603 import core.stdcpp.string : core_basic_string = basic_string;
1604 alias std_string = core_basic_string!(char);
1605 }
1606
1607 extern(C++) void callback18955(ref const(std_string) str)
1608 {
1609 }
1610 extern(C++) void test18955();
1611
1612 /****************************************/
1613
1614 extern(C++) void testPreviewIn();
1615
1616 extern(C++) void previewInFunction(in int a, in std_string b, ref const(std_string) c)
1617 {
1618 assert(a == 42);
1619 assert(&b is &c);
1620 }
1621
1622 /****************************************/
1623
1624 void main()
1625 {
1626 test1();
1627 test2();
1628 test3();
1629 test4();
1630 test13956();
1631 test5();
1632 test6();
1633 test10071();
1634 test7();
1635 test8();
1636 test11802();
1637 test9();
1638 test10();
1639 test13955();
1640 test11();
1641 testvalist();
1642 test12825();
1643 test13161();
1644 test14();
1645 test13289();
1646 test15();
1647 test16();
1648 func13707();
1649 func13932(S13932!(-1)(0));
1650 foo13337(S13337());
1651 test14195();
1652 test14200();
1653 test14956(S14956());
1654 testVtable();
1655 fuzz();
1656 testeh();
1657 testeh2();
1658 testeh3();
1659 test15576();
1660 version (TEST15579) test15579();
1661 test15610();
1662 test15455();
1663 test15372();
1664 test15802();
1665 test16536();
1666 test15589();
1667 test15589b();
1668 test18928();
1669 test18953();
1670 test18966();
1671 test19134();
1672 test18955();
1673 testPreviewIn();
1674
1675 printf("Success\n");
1676 }