1
2 // Test C++ name mangling.
3 // See Bugs 4059, 5148, 7024, 10058
4
5
6 import core.stdc.stdio;
7
8 extern (C++) int foob(int i, int j, int k);
9
10 class C
11 {
bar(int i,int j,int k)12 extern (C++) int bar(int i, int j, int k)
13 {
14 printf("this = %p\n", this);
15 printf("i = %d\n", i);
16 printf("j = %d\n", j);
17 printf("k = %d\n", k);
18 return 1;
19 }
20 }
21
22
23 extern (C++)
foo(int i,int j,int k)24 int foo(int i, int j, int k)
25 {
26 printf("i = %d\n", i);
27 printf("j = %d\n", j);
28 printf("k = %d\n", k);
29 assert(i == 1);
30 assert(j == 2);
31 assert(k == 3);
32 return 1;
33 }
34
test1()35 void test1()
36 {
37 foo(1, 2, 3);
38
39 auto i = foob(1, 2, 3);
40 assert(i == 7);
41
42 C c = new C();
43 c.bar(4, 5, 6);
44 }
45
version(linux)46 version (linux)
47 {
48 static assert(foo.mangleof == "_Z3fooiii");
49 static assert(foob.mangleof == "_Z4foobiii");
50 static assert(C.bar.mangleof == "_ZN1C3barEiii");
51 }
version(Win32)52 version (Win32)
53 {
54 static assert(foo.mangleof == "?foo@@YAHHHH@Z");
55 static assert(foob.mangleof == "?foob@@YAHHHH@Z");
56 static assert(C.bar.mangleof == "?bar@C@@UAEHHHH@Z");
57 }
version(Win64)58 version (Win64)
59 {
60 static assert(foo.mangleof == "?foo@@YAHHHH@Z");
61 static assert(foob.mangleof == "?foob@@YAHHHH@Z");
62 static assert(C.bar.mangleof == "?bar@C@@UEAAHHHH@Z");
63 }
64
65 /****************************************/
66
67 extern (C++)
68 interface D
69 {
70 int bar(int i, int j, int k);
71 }
72
73 extern (C++) D getD();
74
test2()75 void test2()
76 {
77 D d = getD();
78 int i = d.bar(9,10,11);
79 assert(i == 8);
80 }
81
version(linux)82 version (linux)
83 {
84 static assert (getD.mangleof == "_Z4getDv");
85 static assert (D.bar.mangleof == "_ZN1D3barEiii");
86 }
87
88 /****************************************/
89
90 extern (C++) int callE(E);
91
92 extern (C++)
93 interface E
94 {
95 int bar(int i, int j, int k);
96 }
97
98 class F : E
99 {
bar(int i,int j,int k)100 extern (C++) int bar(int i, int j, int k)
101 {
102 printf("F.bar: i = %d\n", i);
103 printf("F.bar: j = %d\n", j);
104 printf("F.bar: k = %d\n", k);
105 assert(i == 11);
106 assert(j == 12);
107 assert(k == 13);
108 return 8;
109 }
110 }
111
test3()112 void test3()
113 {
114 F f = new F();
115 int i = callE(f);
116 assert(i == 8);
117 }
118
version(linux)119 version (linux)
120 {
121 static assert (callE.mangleof == "_Z5callEP1E");
122 static assert (E.bar.mangleof == "_ZN1E3barEiii");
123 static assert (F.bar.mangleof == "_ZN1F3barEiii");
124 }
125
126 /****************************************/
127
128 extern (C++) void foo4(char* p);
129
test4()130 void test4()
131 {
132 foo4(null);
133 }
134
version(linux)135 version (linux)
136 {
137 static assert(foo4.mangleof == "_Z4foo4Pc");
138 }
139
140 /****************************************/
141
142 extern(C++)
143 {
144 struct foo5 { int i; int j; void* p; }
145
146 interface bar5{
147 foo5 getFoo(int i);
148 }
149
150 bar5 newBar();
151 }
152
test5()153 void test5()
154 {
155 bar5 b = newBar();
156 foo5 f = b.getFoo(4);
157 printf("f.p = %p, b = %p\n", f.p, cast(void*)b);
158 assert(f.p == cast(void*)b);
159 }
160
version(linux)161 version (linux)
162 {
163 static assert(bar5.getFoo.mangleof == "_ZN4bar56getFooEi");
164 static assert (newBar.mangleof == "_Z6newBarv");
165 }
166
167 /****************************************/
168
169 extern(C++)
170 {
171 struct S6
172 {
173 int i;
174 double d;
175 }
176 S6 foo6();
177 }
178
179 extern (C) int foosize6();
180
test6()181 void test6()
182 {
183 S6 f = foo6();
184 printf("%d %d\n", foosize6(), S6.sizeof);
185 assert(foosize6() == S6.sizeof);
186 assert(f.i == 42);
187 printf("f.d = %g\n", f.d);
188 assert(f.d == 2.5);
189 }
190
version(linux)191 version (linux)
192 {
193 static assert (foo6.mangleof == "_Z4foo6v");
194 }
195
196 /****************************************/
197
198 extern (C) int foo7();
199
200 struct S
201 {
202 int i;
203 long l;
204 }
205
test7()206 void test7()
207 {
208 printf("%d %d\n", foo7(), S.sizeof);
209 assert(foo7() == S.sizeof);
210 }
211
212 /****************************************/
213
214 extern (C++) void foo8(const char *);
215
test8()216 void test8()
217 {
218 char c;
219 foo8(&c);
220 }
221
version(linux)222 version (linux)
223 {
224 static assert(foo8.mangleof == "_Z4foo8PKc");
225 }
226
227 /****************************************/
228 // 4059
229
230 struct elem9 { }
231
232 extern(C++) void foobar9(elem9*, elem9*);
233
test9()234 void test9()
235 {
236 elem9 *a;
237 foobar9(a, a);
238 }
239
version(linux)240 version (linux)
241 {
242 static assert(foobar9.mangleof == "_Z7foobar9P5elem9S0_");
243 }
244
245 /****************************************/
246 // 5148
247
248 extern (C++)
249 {
250 void foo10(const char*, const char*);
251 void foo10(const int, const int);
252 void foo10(const char, const char);
253
254 struct MyStructType { }
255 void foo10(const MyStructType s, const MyStructType t);
256
257 enum MyEnumType { onemember }
258 void foo10(const MyEnumType s, const MyEnumType t);
259 }
260
test10()261 void test10()
262 {
263 char* p;
264 foo10(p, p);
265 foo10(1,2);
266 foo10('c','d');
267 MyStructType s;
268 foo10(s,s);
269 MyEnumType e;
270 foo10(e,e);
271 }
272
273 /**************************************/
274 // 10058
275
276 extern (C++)
277 {
test10058a(void *)278 void test10058a(void*) { }
test10058b(void function (void *))279 void test10058b(void function(void*)) { }
test10058c(void * function (void *))280 void test10058c(void* function(void*)) { }
test10058d(void function (void *),void *)281 void test10058d(void function(void*), void*) { }
test10058e(void * function (void *),void *)282 void test10058e(void* function(void*), void*) { }
test10058f(void * function (void *),void * function (void *))283 void test10058f(void* function(void*), void* function(void*)) { }
test10058g(void function (void *),void *,void *)284 void test10058g(void function(void*), void*, void*) { }
test10058h(void * function (void *),void *,void *)285 void test10058h(void* function(void*), void*, void*) { }
test10058i(void * function (void *),void * function (void *),void *)286 void test10058i(void* function(void*), void* function(void*), void*) { }
test10058j(void * function (void *),void * function (void *),void * function (void *))287 void test10058j(void* function(void*), void* function(void*), void* function(void*)) { }
test10058k(void * function (void *),void * function (const (void)*))288 void test10058k(void* function(void*), void* function(const (void)*)) { }
test10058l(void * function (void *),void * function (const (void)*),const (void)* function (void *))289 void test10058l(void* function(void*), void* function(const (void)*), const(void)* function(void*)) { }
290 }
291
version(linux)292 version (linux)
293 {
294 static assert(test10058a.mangleof == "_Z10test10058aPv");
295 static assert(test10058b.mangleof == "_Z10test10058bPFvPvE");
296 static assert(test10058c.mangleof == "_Z10test10058cPFPvS_E");
297 static assert(test10058d.mangleof == "_Z10test10058dPFvPvES_");
298 static assert(test10058e.mangleof == "_Z10test10058ePFPvS_ES_");
299 static assert(test10058f.mangleof == "_Z10test10058fPFPvS_ES1_");
300 static assert(test10058g.mangleof == "_Z10test10058gPFvPvES_S_");
301 static assert(test10058h.mangleof == "_Z10test10058hPFPvS_ES_S_");
302 static assert(test10058i.mangleof == "_Z10test10058iPFPvS_ES1_S_");
303 static assert(test10058j.mangleof == "_Z10test10058jPFPvS_ES1_S1_");
304 static assert(test10058k.mangleof == "_Z10test10058kPFPvS_EPFS_PKvE");
305 static assert(test10058l.mangleof == "_Z10test10058lPFPvS_EPFS_PKvEPFS3_S_E");
306 }
307
308 /**************************************/
309 // 11696
310
311 class Expression;
312 struct Loc {}
313
314 extern(C++)
315 class CallExp
316 {
317 static void test11696a(Loc, Expression, Expression);
318 static void test11696b(Loc, Expression, Expression*);
319 static void test11696c(Loc, Expression*, Expression);
320 static void test11696d(Loc, Expression*, Expression*);
321 }
322
version(linux)323 version (linux)
324 {
325 static assert(CallExp.test11696a.mangleof == "_ZN7CallExp10test11696aE3LocP10ExpressionS2_");
326 static assert(CallExp.test11696b.mangleof == "_ZN7CallExp10test11696bE3LocP10ExpressionPS2_");
327 static assert(CallExp.test11696c.mangleof == "_ZN7CallExp10test11696cE3LocPP10ExpressionS2_");
328 static assert(CallExp.test11696d.mangleof == "_ZN7CallExp10test11696dE3LocPP10ExpressionS3_");
329 }
330
331 /**************************************/
332 // 13337
333
334 extern(C++, N13337a.N13337b.N13337c)
335 {
336 struct S13337{}
337 void foo13337(S13337 s);
338 }
339
version(linux)340 version (linux)
341 {
342 static assert(foo13337.mangleof == "_ZN7N13337a7N13337b7N13337c8foo13337ENS1_6S13337E");
343 }
344
345 /**************************************/
346 // 15789
347
348 extern (C++) void test15789a(T...)(T args);
349
test15789()350 void test15789()
351 {
352 test15789a(0);
353 }
354
355 /**************************************/
356 // 7030
357
358 extern(C++)
359 {
360 struct T
361 {
362 void foo(int) const;
363 void bar(int);
364 static __gshared int boo;
365 }
366 }
367
368 version (Posix)
369 {
370 static assert(T.foo.mangleof == "_ZNK1T3fooEi");
371 static assert(T.bar.mangleof == "_ZN1T3barEi");
372 static assert(T.boo.mangleof == "_ZN1T3booE");
373 }
374
375