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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
|
// REQUIRED_ARGS: -preview=in
// PERMUTE_ARGS: -g
// EXTRA_CPP_SOURCES: cppb.cpp
// EXTRA_FILES: extra-files/cppb.h
// CXXFLAGS(linux freebsd osx netbsd dragonflybsd): -std=c++11
// druntime isn't linked, this prevents missing symbols '_d_arraybounds_slicep':
// REQUIRED_ARGS: -checkaction=C
// Filter a spurious warning on Semaphore:
// TRANSFORM_OUTPUT: remove_lines("warning: relocation refers to discarded section")
// N.B MSVC doesn't have a C++11 switch, but it defaults to the latest fully-supported standard
// Broken for unknown reasons since the OMF => MsCOFF switch
// DISABLED: win32omf
import core.stdc.stdio;
import core.stdc.stdarg;
import core.stdc.config;
import core.stdc.stdint;
extern (C++)
int foob(int i, int j, int k);
class C
{
extern (C++) int bar(int i, int j, int k)
{
printf("this = %p\n", this);
printf("i = %d\n", i);
printf("j = %d\n", j);
printf("k = %d\n", k);
return 1;
}
}
extern (C++)
int foo(int i, int j, int k)
{
printf("i = %d\n", i);
printf("j = %d\n", j);
printf("k = %d\n", k);
assert(i == 1);
assert(j == 2);
assert(k == 3);
return 1;
}
void test1()
{
foo(1, 2, 3);
auto i = foob(1, 2, 3);
assert(i == 7);
C c = new C();
c.bar(4, 5, 6);
}
/****************************************/
extern (C++) interface D
{
int bar(int i, int j, int k);
}
extern (C++) D getD();
void test2()
{
D d = getD();
int i = d.bar(9,10,11);
assert(i == 8);
}
/****************************************/
extern (C++) int callE(E);
extern (C++) interface E
{
int bar(int i, int j, int k);
}
class F : E
{
extern (C++) int bar(int i, int j, int k)
{
printf("F.bar: i = %d\n", i);
printf("F.bar: j = %d\n", j);
printf("F.bar: k = %d\n", k);
assert(i == 11);
assert(j == 12);
assert(k == 13);
return 8;
}
}
void test3()
{
F f = new F();
int i = callE(f);
assert(i == 8);
}
/****************************************/
extern (C++) void foo4(char* p);
void test4()
{
foo4(null);
}
/****************************************/
extern(C++)
{
struct foo5 { int i; int j; void* p; }
interface bar5{
foo5 getFoo(int i);
}
bar5 newBar();
}
void test5()
{
bar5 b = newBar();
foo5 f = b.getFoo(4);
printf("f.p = %p, b = %p\n", f.p, cast(void*)b);
assert(f.p == cast(void*)b);
}
/****************************************/
extern(C++)
{
struct S6
{
int i;
double d;
}
union S6_2
{
int i;
double d;
}
enum S6_3
{
A, B
}
S6 foo6();
S6_2 foo6_2();
S6_3 foo6_3();
}
extern (C) int foosize6();
void test6()
{
S6 f = foo6();
printf("%d %zd\n", foosize6(), S6.sizeof);
assert(foosize6() == S6.sizeof);
version (X86)
{
assert(f.i == 42);
printf("f.d = %g\n", f.d);
assert(f.d == 2.5);
assert(foo6_2().i == 42);
assert(foo6_3() == S6_3.A);
}
}
/****************************************/
extern (C) int foo7();
struct S
{
int i;
long l;
}
void test7()
{
printf("%d %zd\n", foo7(), S.sizeof);
assert(foo7() == S.sizeof);
}
/****************************************/
extern (C++) void foo8(const(char)*);
void test8()
{
char c;
foo8(&c);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=4059
struct elem9 { }
extern(C++) void foobar9(elem9*, elem9*);
void test9()
{
elem9 *a;
foobar9(a, a);
}
/****************************************/
struct A11802;
struct B11802;
extern(C++) class C11802
{
int x;
void fun(A11802*) { x += 2; }
void fun(B11802*) { x *= 2; }
}
extern(C++) class D11802 : C11802
{
override void fun(A11802*) { x += 3; }
override void fun(B11802*) { x *= 3; }
}
extern(C++) void test11802x(D11802);
void test11802()
{
auto x = new D11802();
x.x = 0;
test11802x(x);
assert(x.x == 9);
}
/****************************************/
struct S13956
{
}
extern(C++) void func13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
extern(C++) void check13956(S13956 arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)
{
assert(arg0 == S13956());
assert(arg1 == 1);
assert(arg2 == 2);
assert(arg3 == 3);
assert(arg4 == 4);
assert(arg5 == 5);
assert(arg6 == 6);
}
void test13956()
{
func13956(S13956(), 1, 2, 3, 4, 5, 6);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=5148
extern (C++)
{
void foo10(const(char)*, const(char)*);
void foo10(const int, const int);
void foo10(const char, const char);
void foo10(bool, bool);
struct MyStructType { }
void foo10(const MyStructType s, const MyStructType t);
enum MyEnumType { onemember }
void foo10(const MyEnumType s, const MyEnumType t);
}
void test10()
{
char* p;
foo10(p, p);
foo10(1,2);
foo10('c','d');
MyStructType s;
foo10(s,s);
MyEnumType e;
foo10(e,e);
}
/****************************************/
extern (C++, N11.M) { void bar11(); }
extern (C++, A11.B) { extern (C++, C) { void bar(); }}
void test11()
{
bar11();
A11.B.C.bar();
}
/****************************************/
struct Struct10071
{
void *p;
c_long_double r;
}
extern(C++) size_t offset10071();
void test10071()
{
assert(offset10071() == Struct10071.r.offsetof);
}
/****************************************/
char[100] valistbuffer;
extern(C++) void myvprintfx(const(char)* format, va_list va)
{
vsprintf(valistbuffer.ptr, format, va);
}
extern(C++) void myvprintf(const(char)*, va_list);
extern(C++) void myprintf(const(char)* format, ...)
{
va_list ap;
va_start(ap, format);
myvprintf(format, ap);
va_end(ap);
}
void testvalist()
{
myprintf("hello %d", 999);
assert(valistbuffer[0..9] == "hello 999");
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=12825
extern(C++) class C12825
{
uint a = 0x12345678;
}
void test12825()
{
auto c = new C12825();
}
/****************************************/
struct S13955a
{
float a;
double b;
}
struct S13955b
{
double a;
float b;
}
struct S13955c
{
float a;
float b;
}
struct S13955d
{
double a;
double b;
}
extern(C++) void check13955(S13955a a, S13955b b, S13955c c, S13955d d)
{
assert(a.a == 2);
assert(a.b == 4);
assert(b.a == 8);
assert(b.b == 16);
assert(c.a == 32);
assert(c.b == 64);
assert(d.a == 128);
assert(d.b == 256);
}
extern(C++) void func13955(S13955a a, S13955b b, S13955c c, S13955d d);
void test13955()
{
func13955(S13955a(2, 4), S13955b(8, 16), S13955c(32, 64), S13955d(128, 256));
}
/****************************************/
extern(C++) class C13161
{
void dummyfunc();
long val_5;
uint val_9;
}
extern(C++) class Test : C13161
{
uint val_0;
long val_1;
}
extern(C++) size_t getoffset13161();
extern(C++) class C13161a
{
void dummyfunc();
c_long_double val_5;
uint val_9;
}
extern(C++) class Testa : C13161a
{
bool val_0;
}
extern(C++) size_t getoffset13161a();
void test13161()
{
assert(getoffset13161() == Test.val_0.offsetof);
assert(getoffset13161a() == Testa.val_0.offsetof);
}
/****************************************/
version (linux)
{
static if (__traits(getTargetInfo, "cppStd") < 201703)
{
// See note on std::allocator below.
extern(C++, __gnu_cxx)
{
struct new_allocator(T)
{
alias size_type = size_t;
static if (is(T : char))
void deallocate(T*, size_type) { }
else
void deallocate(T*, size_type);
}
}
}
}
extern (C++, std)
{
version (linux)
{
static if (__traits(getTargetInfo, "cppStd") >= 201703)
{
// std::allocator no longer derives from __gnu_cxx::new_allocator,
// it derives from std::__new_allocator instead.
struct __new_allocator(T)
{
alias size_type = size_t;
static if (is(T : char))
void deallocate(T*, size_type) { }
else
void deallocate(T*, size_type);
}
}
}
extern (C++, class) struct allocator(T)
{
version (linux)
{
alias size_type = size_t;
void deallocate(T* p, size_type sz)
{
static if (__traits(getTargetInfo, "cppStd") >= 201703)
(cast(std.__new_allocator!T*)&this).deallocate(p, sz);
else
(cast(__gnu_cxx.new_allocator!T*)&this).deallocate(p, sz);
}
}
}
class vector(T, A = allocator!T)
{
final void push_back(ref const T);
}
struct char_traits(T)
{
}
version (CppRuntime_Gcc)
{
// https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
static if (__traits(getTargetInfo, "cppStd") >= 201103)
{
extern (C++, __cxx11)
{
struct basic_string(T, C = char_traits!T, A = allocator!T)
{
}
}
}
else
{
extern (C++, class) struct basic_string(T, C = char_traits!T, A = allocator!T)
{
}
}
}
else
{
extern (C++, class) struct basic_string(T, C = char_traits!T, A = allocator!T)
{
}
}
struct basic_istream(T, C = char_traits!T)
{
}
struct basic_ostream(T, C = char_traits!T)
{
}
struct basic_iostream(T, C = char_traits!T)
{
}
class exception { }
// https://issues.dlang.org/show_bug.cgi?id=14956
extern(C++, N14956)
{
struct S14956 { }
}
}
extern (C++)
{
version (linux)
{
void foo14(std.vector!(int) p);
void foo14a(std.basic_string!(char) *p);
void foo14b(std.basic_string!(int) *p);
void foo14c(std.basic_istream!(char) *p);
void foo14d(std.basic_ostream!(char) *p);
void foo14e(std.basic_iostream!(char) *p);
void foo14f(std.char_traits!char* x, std.basic_string!char* p, std.basic_string!char* q);
}
}
void test14()
{
version (linux)
{
std.vector!int p;
foo14(p);
foo14a(null);
foo14b(null);
foo14c(null);
foo14d(null);
foo14e(null);
foo14f(null, null, null);
}
}
version (linux)
{
void test14a(std.allocator!int * pa)
{
pa.deallocate(null, 0);
}
void gun(std.vector!int pa)
{
int x = 42;
pa.push_back(x);
}
}
void test13289()
{
assert(f13289_cpp_wchar_t('a') == 'A');
assert(f13289_cpp_wchar_t('B') == 'B');
assert(f13289_d_wchar('c') == 'C');
assert(f13289_d_wchar('D') == 'D');
assert(f13289_d_dchar('e') == 'E');
assert(f13289_d_dchar('F') == 'F');
assert(f13289_cpp_test());
}
version(Posix)
{
enum __c_wchar_t : dchar;
}
else version(Windows)
{
enum __c_wchar_t : wchar;
}
alias wchar_t = __c_wchar_t;
extern(C++)
{
bool f13289_cpp_test();
wchar_t f13289_cpp_wchar_t(wchar_t);
wchar f13289_d_wchar(wchar ch)
{
if (ch <= 'z' && ch >= 'a')
{
return cast(wchar)(ch - ('a' - 'A'));
}
else
{
return ch;
}
}
dchar f13289_d_dchar(dchar ch)
{
if (ch <= 'z' && ch >= 'a')
{
return ch - ('a' - 'A');
}
else
{
return ch;
}
}
wchar_t f13289_d_wchar_t(wchar_t ch)
{
if (ch <= 'z' && ch >= 'a')
{
return cast(wchar_t)(ch - ('a' - 'A'));
}
else
{
return ch;
}
}
}
/****************************************/
version (CRuntime_Microsoft)
{
enum __c_long_double : double;
alias __c_long_double myld;
}
else
alias c_long_double myld;
extern (C++) myld testld(myld);
extern (C++) myld testldld(myld, myld);
void test15()
{
myld ld = 5.0;
ld = testld(ld);
assert(ld == 6.0);
myld ld2 = 5.0;
ld2 = testldld(ld2, ld2);
assert(ld2 == 6.0);
}
/****************************************/
version( Windows )
{
alias int x_long;
alias uint x_ulong;
}
else
{
static if( (void*).sizeof > int.sizeof )
{
alias long x_long;
alias ulong x_ulong;
}
else
{
alias int x_long;
alias uint x_ulong;
}
}
enum __c_long : x_long;
enum __c_ulong : x_ulong;
alias __c_long mylong;
alias __c_ulong myulong;
extern (C++) mylong testl(mylong);
extern (C++) myulong testul(myulong);
void test16()
{
{
mylong ld = 5;
ld = testl(ld);
printf("ld = %lld, mylong.sizeof = %lld\n", cast(long)ld, cast(long)mylong.sizeof);
assert(ld == 5 + mylong.sizeof);
}
{
myulong ld = 5;
ld = testul(ld);
assert(ld == 5 + myulong.sizeof);
}
static if (__c_long.sizeof == long.sizeof)
{
static assert(__c_long.max == long.max);
static assert(__c_long.min == long.min);
static assert(__c_long.init == long.init);
static assert(__c_ulong.max == ulong.max);
static assert(__c_ulong.min == ulong.min);
static assert(__c_ulong.init == ulong.init);
__c_long cl = 0;
cl = cl + 1;
long l = cl;
cl = l;
__c_ulong cul = 0;
cul = cul + 1;
ulong ul = cul;
cul = ul;
}
else static if (__c_long.sizeof == int.sizeof)
{
static assert(__c_long.max == int.max);
static assert(__c_long.min == int.min);
static assert(__c_long.init == int.init);
static assert(__c_ulong.max == uint.max);
static assert(__c_ulong.min == uint.min);
static assert(__c_ulong.init == uint.init);
__c_long cl = 0;
cl = cl + 1;
int i = cl;
cl = i;
__c_ulong cul = 0;
cul = cul + 1;
uint u = cul;
cul = u;
}
else
static assert(0);
}
/****************************************/
struct S13707
{
void* a;
void* b;
this(void* a, void* b)
{
this.a = a;
this.b = b;
}
}
extern(C++) S13707 func13707();
void test13707()
{
auto p = func13707();
assert(p.a == null);
assert(p.b == null);
}
/****************************************/
struct S13932(int x)
{
int member;
}
extern(C++) void func13932(S13932!(-1) s);
/****************************************/
extern(C++, N13337.M13337)
{
struct S13337{}
void foo13337(S13337 s);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=14195
struct Delegate1(T) {}
struct Delegate2(T1, T2) {}
template Signature(T)
{
alias Signature = typeof(*(T.init));
}
extern(C++)
{
alias del1_t = Delegate1!(Signature!(void function()));
alias del2_t = Delegate2!(Signature!(int function(float, double)), Signature!(int function(float, double)));
void test14195a(del1_t);
void test14195b(del2_t);
}
void test14195()
{
test14195a(del1_t());
test14195b(del2_t());
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=14200
template Tuple14200(T...)
{
alias Tuple14200 = T;
}
extern(C++) void test14200a(Tuple14200!(int));
extern(C++) void test14200b(float, Tuple14200!(int, double));
void test14200()
{
test14200a(1);
test14200b(1.0f, 1, 1.0);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=14956
extern(C++) void test14956(S14956 s);
/****************************************/
// check order of overloads in vtable
extern (C++) class Statement {}
extern (C++) class ErrorStatement {}
extern (C++) class PeelStatement {}
extern (C++) class ExpStatement {}
extern (C++) class DtorExpStatement {}
extern (C++) class Visitor
{
public:
int visit(Statement) { return 1; }
int visit(ErrorStatement) { return 2; }
int visit(PeelStatement) { return 3; }
}
extern (C++) class Visitor2 : Visitor
{
int visit2(ExpStatement) { return 4; }
int visit2(DtorExpStatement) { return 5; }
}
extern(C++) bool testVtableCpp(Visitor2 sv);
extern(C++) Visitor2 getVisitor2();
bool testVtableD(Visitor2 sv)
{
Statement s1;
ErrorStatement s2;
PeelStatement s3;
ExpStatement s4;
DtorExpStatement s5;
if (sv.visit(s1) != 1) return false;
if (sv.visit(s2) != 2) return false;
if (sv.visit(s3) != 3) return false;
if (sv.visit2(s4) != 4) return false;
if (sv.visit2(s5) != 5) return false;
return true;
}
void testVtable()
{
Visitor2 dinst = new Visitor2;
if (!testVtableCpp(dinst))
assert(0);
Visitor2 cppinst = getVisitor2();
if (!testVtableD(cppinst))
assert(0);
}
/****************************************/
/* problems detected by fuzzer */
extern(C++) void fuzz1_cppvararg(int64_t arg10, int64_t arg11, bool arg12);
extern(C++) void fuzz1_dvararg(int64_t arg10, int64_t arg11, bool arg12)
{
fuzz1_checkValues(arg10, arg11, arg12);
}
extern(C++) void fuzz1_checkValues(int64_t arg10, int64_t arg11, bool arg12)
{
assert(arg10 == 103);
assert(arg11 == 104);
assert(arg12 == false);
}
void fuzz1()
{
long arg10 = 103;
long arg11 = 104;
bool arg12 = false;
fuzz1_dvararg(arg10, arg11, arg12);
fuzz1_cppvararg(arg10, arg11, arg12);
}
////////
extern(C++) void fuzz2_cppvararg(uint64_t arg10, uint64_t arg11, bool arg12);
extern(C++) void fuzz2_dvararg(uint64_t arg10, uint64_t arg11, bool arg12)
{
fuzz2_checkValues(arg10, arg11, arg12);
}
extern(C++) void fuzz2_checkValues(uint64_t arg10, uint64_t arg11, bool arg12)
{
assert(arg10 == 103);
assert(arg11 == 104);
assert(arg12 == false);
}
void fuzz2()
{
ulong arg10 = 103;
ulong arg11 = 104;
bool arg12 = false;
fuzz2_dvararg(arg10, arg11, arg12);
fuzz2_cppvararg(arg10, arg11, arg12);
}
////////
version(CppRuntime_DigitalMars)
enum UNICODE = false;
else version(CppRuntime_Microsoft)
enum UNICODE = false; //VS2013 doesn't support them
else
enum UNICODE = true;
static if (UNICODE)
{
extern(C++) void fuzz3_cppvararg(wchar arg10, dchar arg11, bool arg12);
extern(C++) void fuzz3_dvararg(wchar arg10, dchar arg11, bool arg12)
{
fuzz2_checkValues(arg10, arg11, arg12);
}
extern(C++) void fuzz3_checkValues(wchar arg10, dchar arg11, bool arg12)
{
assert(arg10 == 103);
assert(arg11 == 104);
assert(arg12 == false);
}
void fuzz3()
{
wchar arg10 = 103;
dchar arg11 = 104;
bool arg12 = false;
fuzz3_dvararg(arg10, arg11, arg12);
fuzz3_cppvararg(arg10, arg11, arg12);
}
}
void fuzz()
{
fuzz1();
fuzz2();
static if (UNICODE) fuzz3();
}
/****************************************/
extern (C++)
{
void throwit();
}
void testeh()
{
printf("testeh()\n");
version (linux)
{
version (X86_64)
{
bool caught;
try
{
throwit();
}
catch (std.exception e)
{
caught = true;
}
assert(caught);
}
}
}
/****************************************/
version (linux)
{
version (X86_64)
{
bool raii_works = false;
struct RAIITest
{
~this()
{
raii_works = true;
}
}
void dFunction()
{
RAIITest rt;
throwit();
}
void testeh2()
{
printf("testeh2()\n");
try
{
dFunction();
}
catch(std.exception e)
{
assert(raii_works);
}
}
}
else
void testeh2() { }
}
else
void testeh2() { }
/****************************************/
extern (C++) { void throwle(); void throwpe(); }
void testeh3()
{
printf("testeh3()\n");
version (linux)
{
version (X86_64)
{
bool caught = false;
try
{
throwle();
}
catch (std.exception e) //polymorphism test.
{
caught = true;
}
assert(caught);
}
}
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15576
extern (C++, ns15576)
{
extern __gshared int global15576;
extern (C++, ns)
{
extern __gshared int n_global15576;
}
}
void test15576()
{
global15576 = n_global15576 = 123;
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15579
extern (C++)
{
class Base
{
//~this() {}
void based() { }
ubyte x = 4;
}
interface Interface
{
int MethodCPP();
int MethodD();
}
class Derived : Base, Interface
{
short y = 5;
int MethodCPP();
int MethodD() {
printf("Derived.MethodD(): this = %p, x = %d, y = %d\n", this, x, y);
Derived p = this;
//p = cast(Derived)(cast(void*)p - 16);
assert(p.x == 4 || p.x == 7);
assert(p.y == 5 || p.y == 8);
return 3;
}
int Method() { return 6; }
}
Derived cppfoo(Derived);
Interface cppfooi(Interface);
}
void test15579()
{
Derived d = new Derived();
printf("d = %p\n", d);
assert(d.x == 4);
assert(d.y == 5);
assert((cast(Interface)d).MethodCPP() == 30);
assert((cast(Interface)d).MethodD() == 3);
assert(d.MethodCPP() == 30);
assert(d.MethodD() == 3);
assert(d.Method() == 6);
d = cppfoo(d);
assert(d.x == 7);
assert(d.y == 8);
printf("d2 = %p\n", d);
/* Casting to an interface involves thunks in the vtbl[].
* g++ puts the thunks for MethodD in the same COMDAT as MethodD.
* But D doesn't, so when the linker "picks one" of the D generated MethodD
* or the g++ generated MethodD, it may wind up with a messed up thunk,
* resulting in a seg fault. The solution is to not expect objects of the same
* type to be constructed on both sides of the D/C++ divide if the same member
* function (in this case, MethodD) is also defined on both sides.
*/
version (Windows)
{
assert((cast(Interface)d).MethodD() == 3);
}
assert((cast(Interface)d).MethodCPP() == 30);
assert(d.Method() == 6);
printf("d = %p, i = %p\n", d, cast(Interface)d);
version (Windows)
{
Interface i = cppfooi(d);
printf("i2: %p\n", i);
assert(i.MethodD() == 3);
assert(i.MethodCPP() == 30);
}
printf("test15579() done\n");
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15610
extern(C++) class Base2
{
int i;
void baser() { }
}
extern(C++) interface Interface2 { abstract void f(); }
extern(C++) class Derived2 : Base2, Interface2
{
final
override void f();
}
void test15610()
{
auto c = new Derived2();
printf("test15610(): c = %p\n", c);
c.i = 3;
c.f();
}
/******************************************/
// https://issues.dlang.org/show_bug.cgi?id=15455
struct X6
{
ushort a;
ushort b;
ubyte c;
ubyte d;
}
static assert(X6.sizeof == 6);
struct X8
{
ushort a;
X6 b;
}
static assert(X8.sizeof == 8);
void test15455a(X8 s)
{
assert(s.a == 1);
assert(s.b.a == 2);
assert(s.b.b == 3);
assert(s.b.c == 4);
assert(s.b.d == 5);
}
extern (C++) void test15455b(X8 s);
void test15455()
{
X8 s;
s.a = 1;
s.b.a = 2;
s.b.b = 3;
s.b.c = 4;
s.b.d = 5;
test15455a(s);
test15455b(s);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15372
extern(C++) int foo15372(T)(int v);
void test15372()
{
version(Windows){}
else
assert(foo15372!int(1) == 1);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15802
extern(C++) {
template Foo15802(T) {
static int boo(T v);
}
}
void test15802()
{
version(Windows){}
else
assert(Foo15802!(int).boo(1) == 1);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=16536
// mangling mismatch on OSX
version(OSX) extern(C++) uint64_t pass16536(uint64_t);
void test16536()
{
version(OSX) assert(pass16536(123) == 123);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=15589
// extern(C++) virtual destructors are not put in vtbl[]
extern(C++)
{
class A15589
{
extern(D) static int[] dtorSeq;
struct S
{
this(int x) { this.x = x; }
~this() { dtorSeq ~= x; }
int x;
}
int foo() { return 100; } // shift dtor to slot 1
~this() { dtorSeq ~= 10; }
S s1 = S(1);
S s2 = S(2);
}
class B15589 : A15589
{
int bar() { return 200;} // add an additional function AFTER the dtor at slot 2
~this() { dtorSeq ~= 20; }
S s3 = S(3);
}
void test15589b(A15589 p);
}
void test15589()
{
A15589 c = new B15589;
assert(A15589.dtorSeq == null);
assert(c.foo() == 100);
assert((cast(B15589)c).bar() == 200);
c.__xdtor(); // virtual dtor call
assert(A15589.dtorSeq[] == [ 20, 3, 10, 2, 1 ]); // destroyed full hierarchy!
A15589.dtorSeq = null;
test15589b(c);
assert(A15589.dtorSeq[] == [ 20, 3, 10, 2, 1 ]); // destroyed full hierarchy!
}
extern(C++)
{
class Cpp15589Base
{
public:
final ~this();
void nonVirtual();
int a;
}
class Cpp15589Derived : Cpp15589Base
{
public:
this();
final ~this();
int b;
}
class Cpp15589BaseVirtual
{
public:
void beforeDtor();
this();
~this();
void afterDtor();
int c = 1;
}
class Cpp15589DerivedVirtual : Cpp15589BaseVirtual
{
public:
this();
~this();
override void afterDtor();
int d;
}
class Cpp15589IntroducingVirtual : Cpp15589Base
{
public:
this();
void beforeIntroducedVirtual();
~this();
void afterIntroducedVirtual(int);
int e;
}
struct Cpp15589Struct
{
~this();
int s;
}
void trace15589(int ch)
{
traceBuf[traceBufPos++] = cast(char) ch;
}
}
__gshared char[32] traceBuf;
__gshared size_t traceBufPos;
// workaround for https://issues.dlang.org/show_bug.cgi?id=18986
version(OSX)
enum cppCtorReturnsThis = false;
else version(FreeBSD)
enum cppCtorReturnsThis = false;
else
enum cppCtorReturnsThis = true;
mixin template scopeAllocCpp(C)
{
static if (cppCtorReturnsThis)
scope C ptr = new C;
else
{
ubyte[__traits(classInstanceSize, C)] data;
C ptr = (){ auto p = cast(C) data.ptr; p.__ctor(); return p; }();
}
}
void test15589b()
{
traceBufPos = 0;
{
Cpp15589Struct struc = Cpp15589Struct();
mixin scopeAllocCpp!Cpp15589Derived derived;
mixin scopeAllocCpp!Cpp15589DerivedVirtual derivedVirtual;
mixin scopeAllocCpp!Cpp15589IntroducingVirtual introducingVirtual;
// `scope` instances are destroyed automatically
static if (!cppCtorReturnsThis)
{
introducingVirtual.ptr.destroy();
derivedVirtual.ptr.destroy();
derived.ptr.destroy();
}
}
printf("traceBuf15589 %.*s\n", cast(int)traceBufPos, traceBuf.ptr);
assert(traceBuf[0..traceBufPos] == "IbVvBbs");
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=18928
// Win64: extern(C++) bad codegen, wrong calling convention
extern(C++) struct Small18928
{
int x;
}
extern(C++) class CC18928
{
Small18928 getVirtual(); // { return S(3); }
final Small18928 getFinal(); // { return S(4); }
static Small18928 getStatic(); // { return S(5); }
}
extern(C++) CC18928 newCC18928();
void test18928()
{
auto cc = newCC18928();
Small18928 v = cc.getVirtual();
assert(v.x == 3);
Small18928 f = cc.getFinal();
assert(f.x == 4);
Small18928 s = cc.getStatic();
assert(s.x == 5);
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=18953
// Win32: extern(C++) struct destructor not called correctly through runtime
extern(C++)
struct S18953
{
char x;
~this() nothrow @nogc { traceBuf[traceBufPos++] = x; }
}
void test18953()
{
traceBufPos = 0;
S18953[] arr = new S18953[3];
arr[1].x = '1';
arr[2].x = '2';
arr.length = 1;
assumeSafeAppend(arr); // destroys arr[1] and arr[2]
printf("traceBuf18953 %.*s\n", cast(int)traceBufPos, traceBuf.ptr);
assert(traceBuf[0..traceBufPos] == "21");
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=18966
extern(C++):
class Base18966
{
this() @safe nothrow;
~this() @safe;
void vf();
int x;
}
class Derived18966 : Base18966
{
override void vf() { x = 200; }
}
class Explicit18966 : Base18966
{
this() @safe { super(); }
override void vf() { x = 250; }
}
class Implicit18966 : Base18966
{
this() nothrow {}
override void vf() { x = 300; }
}
// test vptr in full ctor chain of mixed D/C++ class hierarchies
// TODO: Make this a D class and let C++ derive from it. This works on Windows,
// but results in linker errors on Posix due to extra base ctor (`C2`
// mangling) being called by the B ctor.
class A18966 // in C++
{
char[8] calledOverloads = 0;
int i;
this();
void foo();
}
class B18966 : A18966 // in C++
{
this();
override void foo();
}
class C18966 : B18966
{
this() { foo(); }
override void foo() { calledOverloads[i++] = 'C'; }
}
class D18966 : C18966
{
this() { foo(); }
override void foo() { calledOverloads[i++] = 'D'; }
}
void test18966()
{
Derived18966 d = new Derived18966;
assert(d.x == 10);
d.vf();
assert(d.x == 200);
Explicit18966 e = new Explicit18966;
assert(e.x == 10);
e.vf();
assert(e.x == 250);
Implicit18966 i = new Implicit18966;
assert(i.x == 10);
i.vf();
assert(i.x == 300);
// TODO: Allocating + constructing a C++ class with the D GC is not
// supported on Posix. The returned pointer (probably from C++ ctor)
// seems to be an offset and not the actual object address.
version (Windows)
{
auto a = new A18966;
assert(a.calledOverloads[0..2] == "A\0");
auto b = new B18966;
assert(b.calledOverloads[0..3] == "AB\0");
}
auto c = new C18966;
assert(c.calledOverloads[0..4] == "ABC\0");
auto d2 = new D18966;
// note: the vptr semantics in ctors of extern(C++) classes may be revised (to "ABCD")
assert(d2.calledOverloads[0..5] == "ABDD\0");
}
/****************************************/
// https://issues.dlang.org/show_bug.cgi?id=19134
class Base19134
{
int a = 123;
this() { a += 42; }
int foo() const { return a; }
}
class Derived19134 : Base19134
{
int b = 666;
this()
{
a *= 2;
b -= 6;
}
override int foo() const { return b; }
}
void test19134()
{
static const d = new Derived19134;
assert(d.a == (123 + 42) * 2);
assert(d.b == 666 - 6);
assert(d.foo() == 660);
}
// https://issues.dlang.org/show_bug.cgi?id=18955
version (linux)
alias std_string = std.basic_string!(char);
else
{
import core.stdcpp.string : core_basic_string = basic_string;
alias std_string = core_basic_string!(char);
}
extern(C++) void callback18955(ref const(std_string) str)
{
}
extern(C++) void test18955();
/****************************************/
extern(C++) void testPreviewIn();
extern(C++) void previewInFunction(in int a, in std_string b, ref const(std_string) c)
{
assert(a == 42);
assert(&b is &c);
}
/****************************************/
void main()
{
test1();
test2();
test3();
test4();
test13956();
test5();
test6();
test10071();
test7();
test8();
test11802();
test9();
test10();
test13955();
test11();
testvalist();
test12825();
test13161();
test14();
test13289();
test15();
test16();
func13707();
func13932(S13932!(-1)(0));
foo13337(S13337());
test14195();
test14200();
test14956(S14956());
testVtable();
fuzz();
testeh();
testeh2();
testeh3();
test15576();
test15579();
test15610();
test15455();
test15372();
test15802();
test16536();
test15589();
test15589b();
test18928();
test18953();
test18966();
test19134();
test18955();
testPreviewIn();
printf("Success\n");
}
|