Debian Codegraph @

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

raw
1 // EXTRA_CPP_SOURCES: cpp_abi_tests.cpp
2 // CXXFLAGS(linux freebsd osx openbsd netbsd dragonflybsd): -std=c++11
3
4 // N.B MSVC doesn't have a C++11 switch, but it defaults to the latest fully-supported standard
5 // N.B MSVC 2013 doesn't support char16_t/char32_t
6
7 version(Posix)
8 enum __c_wchar_t : dchar;
9 else version(Windows)
10 enum __c_wchar_t : wchar;
11 alias wchar_t = __c_wchar_t;
12
13 extern(C++) {
14
15 struct S
16 {
17 float a = 1;
18 }
19
20 struct S18784
21 {
22 int i;
23 this(int);
24 }
25
26 extern(C++, std)
27 {
28 struct test19248_ {int a = 42;}
29 }
30 extern(C++, `std`)
31 {
32 struct test19248 {int a = 34;}
33 }
34
35 struct Sdtor
36 {
37 extern __gshared int counter;
38 ~this();
39 }
40 void consume(Sdtor);
41 void consume2(Sdtor value){}
42 void doConsume2(ref Sdtor);
43
44 struct SPack(Args...)
45 {
46 int i;
47 }
48 alias SInt = SPack!int;
49
50 bool passthrough(bool value);
51 byte passthrough(byte value);
52 ubyte passthrough(ubyte value);
53 char passthrough(char value);
54 wchar passthrough(wchar value);
55 dchar passthrough(dchar value);
56 wchar_t passthrough(wchar_t value);
57 short passthrough(short value);
58 ushort passthrough(ushort value);
59 int passthrough(int value);
60 uint passthrough(uint value);
61 long passthrough(long value);
62 ulong passthrough(ulong value);
63 float passthrough(float value);
64 double passthrough(double value);
65 S passthrough(S value);
66 test19248 passthrough(const(test19248) value);
67 std.test19248_ passthrough(const(std.test19248_) value);
68 SInt passthrough(SInt value);
69
70 bool passthrough_ptr(bool *value);
71 byte passthrough_ptr(byte *value);
72 ubyte passthrough_ptr(ubyte *value);
73 char passthrough_ptr(char *value);
74 wchar passthrough_ptr(wchar *value);
75 dchar passthrough_ptr(dchar *value);
76 wchar_t passthrough_ptr(wchar_t *value);
77 short passthrough_ptr(short *value);
78 ushort passthrough_ptr(ushort *value);
79 int passthrough_ptr(int *value);
80 uint passthrough_ptr(uint *value);
81 long passthrough_ptr(long *value);
82 ulong passthrough_ptr(ulong *value);
83 float passthrough_ptr(float *value);
84 double passthrough_ptr(double *value);
85 S passthrough_ptr(S *value);
86 test19248 passthrough_ptr(const(test19248)* value);
87 std.test19248_ passthrough_ptr(const(std.test19248_)* value);
88 SInt passthrough_ptr(SInt *value);
89
90 bool passthrough_ref(ref bool value);
91 byte passthrough_ref(ref byte value);
92 ubyte passthrough_ref(ref ubyte value);
93 char passthrough_ref(ref char value);
94 wchar passthrough_ref(ref wchar value);
95 dchar passthrough_ref(ref dchar value);
96 wchar_t passthrough_ref(ref wchar_t value);
97 short passthrough_ref(ref short value);
98 ushort passthrough_ref(ref ushort value);
99 int passthrough_ref(ref int value);
100 uint passthrough_ref(ref uint value);
101 long passthrough_ref(ref long value);
102 ulong passthrough_ref(ref ulong value);
103 float passthrough_ref(ref float value);
104 double passthrough_ref(ref double value);
105 S passthrough_ref(ref S value);
106 test19248 passthrough_ref(ref const(test19248) value);
107 std.test19248_ passthrough_ref(ref const(std.test19248_) value);
108 SInt passthrough_ref(ref SInt value);
109 }
110
111 template IsSigned(T)
112 {
113 enum IsSigned = is(T==byte) ||
114 is(T==short) ||
115 is(T==int) ||
116 is(T==long);
117 }
118
119 template IsUnsigned(T)
120 {
121 enum IsUnsigned = is(T==ubyte) ||
122 is(T==ushort) ||
123 is(T==uint) ||
124 is(T==ulong);
125 }
126
127 template IsIntegral(T)
128 {
129 enum IsIntegral = IsSigned!T || IsUnsigned!T;
130 }
131
132 template IsFloatingPoint(T)
133 {
134 enum IsFloatingPoint = is(T==float) || is(T==double) || is(T==real);
135 }
136
137 template IsBoolean(T)
138 {
139 enum IsBoolean = is(T==bool);
140 }
141
142 template IsSomeChar(T)
143 {
144 enum IsSomeChar = is(T==char) || is(T==wchar) || is(T==dchar) || is(T==wchar_t);
145 }
146
147 void check(T)(T actual, T expected)
148 {
149 assert(actual is expected);
150 }
151
152 void check(T)(T value)
153 {
154 check(passthrough(value), value);
155 check(passthrough_ptr(&value), value);
156 check(passthrough_ref(value), value);
157 }
158
159 T[] values(T)()
160 {
161 T[] values;
162 static if(IsBoolean!T)
163 {
164 values ~= true;
165 values ~= false;
166 }
167 else static if(IsSomeChar!T)
168 {
169 values ~= T.init;
170 values ~= T('a');
171 values ~= T('z');
172 }
173 else
174 {
175 values ~= T(0);
176 values ~= T(1);
177 static if(IsIntegral!T)
178 {
179 static if(IsSigned!T) values ~= T.min;
180 values ~= T.max;
181 }
182 else static if(IsFloatingPoint!T)
183 {
184 values ~= T.nan;
185 values ~= T.min_normal;
186 values ~= T.max;
187 }
188 else
189 {
190 assert(0);
191 }
192 }
193 return values;
194 }
195
196 extern(C++, `ns1`)
197 {
198 // C++: `const char*, const char**`
199 int constFunction1(const(char)*, const(char)**);
200 // C++: `const char*, const char* const*`
201 int constFunction2(const(char)*, const(char*)*);
202 // C++: `const char* const, const char* const* const*`
203 int constFunction3(const(char*), const(char**)*);
204 // C++: `const char* const, const char* const* const* const`
205 int constFunction4(const(char*), const(char***));
206 }
207
208 extern(C++)
209 {
210 // https://issues.dlang.org/show_bug.cgi?id=19563
211
212 struct SmallStruct
213 {
214 int i;
215 this(int i) { this.i = i; }
216 this(ref const SmallStruct); // implemented in C++
217 }
218 void smallStructTest(SmallStruct p);
219 void smallStructCallBack(SmallStruct p)
220 {
221 assert(p.i == 62);
222 }
223 }
224
225 /*********************************************/
226 // https://issues.dlang.org/show_bug.cgi?id=23195
227
228 extern (C++)
229 {
230 struct FF
231 {
232 float x, y;
233
234 ~this() { }
235 }
236
237 float draw(FF min, FF max);
238
239 void test23195()
240 {
241 FF a = { 1, 2 };
242 FF b = { 3, 4 };
243 float f = draw(a, b);
244 assert(f == 1234);
245 }
246
247 /*********************/
248
249 struct FF2
250 {
251 float x, y;
252
253 this(int i) { }
254 }
255
256 float draw2(FF2 min, FF2 max);
257
258 void test23195_2()
259 {
260 FF2 a; a.x = 1; a.y = 2;
261 FF2 b; b.x = 3; b.y = 4;
262 float f = draw2(a, b);
263 assert(f == 1234);
264 }
265 }
266
267 /*********************************************/
268
269 void main()
270 {
271 foreach(bool val; values!bool()) check(val);
272 foreach(byte val; values!byte()) check(val);
273 foreach(ubyte val; values!ubyte()) check(val);
274 foreach(char val; values!char()) check(val);
275 version(CppRuntime_Microsoft)
276 {
277 // TODO: figure out how to detect VS2013 which doesn't support char16_t/char32_t
278 }
279 else
280 {
281 foreach(wchar val; values!wchar()) check(val);
282 foreach(dchar val; values!dchar()) check(val);
283 }
284 foreach(wchar_t val; values!wchar_t()) check(val);
285 foreach(short val; values!short()) check(val);
286 foreach(ushort val; values!ushort()) check(val);
287 foreach(int val; values!int()) check(val);
288 foreach(uint val; values!uint()) check(val);
289 foreach(long val; values!long()) check(val);
290 foreach(ulong val; values!ulong()) check(val);
291 foreach(float val; values!float()) check(val);
292 foreach(double val; values!double()) check(val);
293 check(S());
294 check(test19248());
295 check(std.test19248_());
296 check(SInt());
297
298 assert(constFunction1(null, null) == 1);
299 assert(constFunction2(null, null) == 2);
300 assert(constFunction3(null, null) == 3);
301 assert(constFunction4(null, null) == 42);
302
303 auto ss = SmallStruct(42);
304 smallStructTest(ss);
305 assert(ss.i == 42);
306 assert(S18784(1).i == 1);
307
308 {
309 Sdtor sd;
310 assert(Sdtor.counter == 0);
311 consume(sd);
312 assert(Sdtor.counter == 1);
313 doConsume2(sd);
314 assert(Sdtor.counter == 2);
315 }
316 test23195();
317 test23195_2();
318 }