Commit | Line | Data |
---|---|---|
7e745333 DT |
1 | // Test various -*- C++ -*- things. |
2 | ||
3 | typedef struct fleep fleep; | |
4 | struct fleep { int a; } s; | |
5 | ||
6 | // ====================== simple class structures ======================= | |
7 | ||
8 | struct default_public_struct { | |
9 | // defaults to public: | |
10 | int a; | |
11 | int b; | |
12 | }; | |
13 | ||
14 | struct explicit_public_struct { | |
15 | public: | |
16 | int a; | |
17 | int b; | |
18 | }; | |
19 | ||
20 | struct protected_struct { | |
21 | protected: | |
22 | int a; | |
23 | int b; | |
24 | }; | |
25 | ||
26 | struct private_struct { | |
27 | private: | |
28 | int a; | |
29 | int b; | |
30 | }; | |
31 | ||
32 | struct mixed_protection_struct { | |
33 | public: | |
34 | int a; | |
35 | int b; | |
36 | private: | |
37 | int c; | |
38 | int d; | |
39 | protected: | |
40 | int e; | |
41 | int f; | |
42 | public: | |
43 | int g; | |
44 | private: | |
45 | int h; | |
46 | protected: | |
47 | int i; | |
48 | }; | |
49 | ||
50 | class public_class { | |
51 | public: | |
52 | int a; | |
53 | int b; | |
54 | }; | |
55 | ||
56 | class protected_class { | |
57 | protected: | |
58 | int a; | |
59 | int b; | |
60 | }; | |
61 | ||
62 | class default_private_class { | |
63 | // defaults to private: | |
64 | int a; | |
65 | int b; | |
66 | }; | |
67 | ||
68 | class explicit_private_class { | |
69 | private: | |
70 | int a; | |
71 | int b; | |
72 | }; | |
73 | ||
74 | class mixed_protection_class { | |
75 | public: | |
76 | int a; | |
77 | int b; | |
78 | private: | |
79 | int c; | |
80 | int d; | |
81 | protected: | |
82 | int e; | |
83 | int f; | |
84 | public: | |
85 | int g; | |
86 | private: | |
87 | int h; | |
88 | protected: | |
89 | int i; | |
90 | }; | |
91 | ||
92 | // ========================= simple inheritance ========================== | |
93 | ||
94 | class A { | |
95 | public: | |
96 | int a; | |
97 | int x; | |
98 | }; | |
99 | ||
100 | A g_A; | |
101 | ||
102 | class B : public A { | |
103 | public: | |
104 | int b; | |
105 | int x; | |
106 | }; | |
107 | ||
108 | B g_B; | |
109 | ||
110 | class C : public A { | |
111 | public: | |
112 | int c; | |
113 | int x; | |
114 | }; | |
115 | ||
116 | C g_C; | |
117 | ||
118 | class D : public B, public C { | |
119 | public: | |
120 | int d; | |
121 | int x; | |
122 | }; | |
123 | ||
124 | D g_D; | |
125 | ||
126 | class E : public D { | |
127 | public: | |
128 | int e; | |
129 | int x; | |
130 | }; | |
131 | ||
132 | E g_E; | |
133 | ||
134 | class class_with_anon_union | |
135 | { | |
136 | public: | |
137 | int one; | |
138 | union | |
139 | { | |
140 | int a; | |
141 | long b; | |
142 | }; | |
143 | }; | |
144 | ||
145 | class_with_anon_union g_anon_union; | |
146 | ||
147 | void inheritance2 (void) | |
148 | { | |
149 | } | |
150 | ||
151 | void inheritance1 (void) | |
152 | { | |
153 | int ival; | |
154 | int *intp; | |
155 | ||
156 | // {A::a, A::x} | |
157 | ||
158 | g_A.A::a = 1; | |
159 | g_A.A::x = 2; | |
160 | ||
161 | // {{A::a,A::x},B::b,B::x} | |
162 | ||
163 | g_B.A::a = 3; | |
164 | g_B.A::x = 4; | |
165 | g_B.B::b = 5; | |
166 | g_B.B::x = 6; | |
167 | ||
168 | // {{A::a,A::x},C::c,C::x} | |
169 | ||
170 | g_C.A::a = 7; | |
171 | g_C.A::x = 8; | |
172 | g_C.C::c = 9; | |
173 | g_C.C::x = 10; | |
174 | ||
175 | // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x} | |
176 | ||
177 | // The following initialization code is non-portable, but allows us | |
178 | // to initialize all members of g_D until we can fill in the missing | |
179 | // initialization code with legal C++ code. | |
180 | ||
181 | for (intp = (int *) &g_D, ival = 11; | |
182 | intp < ((int *) &g_D + sizeof (g_D) / sizeof (int)); | |
183 | intp++, ival++) | |
184 | { | |
185 | *intp = ival; | |
186 | } | |
187 | ||
188 | // Overlay the nonportable initialization with legal initialization. | |
189 | ||
190 | // ????? = 11; (g_D.A::a = 11; is ambiguous) | |
191 | // ????? = 12; (g_D.A::x = 12; is ambiguous) | |
192 | g_D.B::b = 13; | |
193 | g_D.B::x = 14; | |
194 | // ????? = 15; | |
195 | // ????? = 16; | |
196 | g_D.C::c = 17; | |
197 | g_D.C::x = 18; | |
198 | g_D.D::d = 19; | |
199 | g_D.D::x = 20; | |
200 | ||
201 | ||
202 | // {{{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}},E::e,E::x} | |
203 | ||
204 | // The following initialization code is non-portable, but allows us | |
205 | // to initialize all members of g_D until we can fill in the missing | |
206 | // initialization code with legal C++ code. | |
207 | ||
208 | for (intp = (int *) &g_E, ival = 21; | |
209 | intp < ((int *) &g_E + sizeof (g_E) / sizeof (int)); | |
210 | intp++, ival++) | |
211 | { | |
212 | *intp = ival; | |
213 | } | |
214 | ||
215 | // Overlay the nonportable initialization with legal initialization. | |
216 | ||
217 | // ????? = 21; (g_E.A::a = 21; is ambiguous) | |
218 | // ????? = 22; (g_E.A::x = 22; is ambiguous) | |
219 | g_E.B::b = 23; | |
220 | g_E.B::x = 24; | |
221 | // ????? = 25; | |
222 | // ????? = 26; | |
223 | g_E.C::c = 27; | |
224 | g_E.C::x = 28; | |
225 | g_E.D::d = 29; | |
226 | g_E.D::x = 30; | |
227 | g_E.E::e = 31; | |
228 | g_E.E::x = 32; | |
229 | ||
230 | g_anon_union.one = 1; | |
231 | g_anon_union.a = 2; | |
232 | ||
233 | inheritance2 (); | |
234 | } | |
235 | ||
236 | // ======================== virtual base classes========================= | |
237 | ||
238 | class vA { | |
239 | public: | |
240 | int va; | |
241 | int vx; | |
242 | }; | |
243 | ||
244 | vA g_vA; | |
245 | ||
246 | class vB : public virtual vA { | |
247 | public: | |
248 | int vb; | |
249 | int vx; | |
250 | }; | |
251 | ||
252 | vB g_vB; | |
253 | ||
254 | class vC : public virtual vA { | |
255 | public: | |
256 | int vc; | |
257 | int vx; | |
258 | }; | |
259 | ||
260 | vC g_vC; | |
261 | ||
262 | class vD : public virtual vB, public virtual vC { | |
263 | public: | |
264 | int vd; | |
265 | int vx; | |
266 | }; | |
267 | ||
268 | vD g_vD; | |
269 | ||
270 | class vE : public virtual vD { | |
271 | public: | |
272 | int ve; | |
273 | int vx; | |
274 | }; | |
275 | ||
276 | vE g_vE; | |
277 | ||
278 | void inheritance4 (void) | |
279 | { | |
280 | } | |
281 | ||
282 | void inheritance3 (void) | |
283 | { | |
284 | int ival; | |
285 | int *intp; | |
286 | ||
287 | // {vA::va, vA::vx} | |
288 | ||
289 | g_vA.vA::va = 1; | |
290 | g_vA.vA::vx = 2; | |
291 | ||
292 | // {{vA::va, vA::vx}, vB::vb, vB::vx} | |
293 | ||
294 | g_vB.vA::va = 3; | |
295 | g_vB.vA::vx = 4; | |
296 | g_vB.vB::vb = 5; | |
297 | g_vB.vB::vx = 6; | |
298 | ||
299 | // {{vA::va, vA::vx}, vC::vc, vC::vx} | |
300 | ||
301 | g_vC.vA::va = 7; | |
302 | g_vC.vA::vx = 8; | |
303 | g_vC.vC::vc = 9; | |
304 | g_vC.vC::vx = 10; | |
305 | ||
306 | // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx} | |
307 | ||
308 | g_vD.vA::va = 11; | |
309 | g_vD.vA::vx = 12; | |
310 | g_vD.vB::vb = 13; | |
311 | g_vD.vB::vx = 14; | |
312 | g_vD.vC::vc = 15; | |
313 | g_vD.vC::vx = 16; | |
314 | g_vD.vD::vd = 17; | |
315 | g_vD.vD::vx = 18; | |
316 | ||
317 | ||
318 | // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx} | |
319 | ||
320 | g_vD.vA::va = 19; | |
321 | g_vD.vA::vx = 20; | |
322 | g_vD.vB::vb = 21; | |
323 | g_vD.vB::vx = 22; | |
324 | g_vD.vC::vc = 23; | |
325 | g_vD.vC::vx = 24; | |
326 | g_vD.vD::vd = 25; | |
327 | g_vD.vD::vx = 26; | |
328 | g_vE.vE::ve = 27; | |
329 | g_vE.vE::vx = 28; | |
330 | ||
331 | inheritance4 (); | |
332 | } | |
333 | ||
334 | // ====================================================================== | |
335 | ||
336 | class Base1 { | |
337 | public: | |
338 | int x; | |
339 | Base1(int i) { x = i; } | |
340 | }; | |
341 | ||
342 | class Foo | |
343 | { | |
344 | public: | |
345 | int x; | |
346 | int y; | |
347 | static int st; | |
348 | Foo (int i, int j) { x = i; y = j; } | |
349 | int operator! (); | |
350 | operator int (); | |
351 | int times (int y); | |
352 | }; | |
353 | ||
354 | class Bar : public Base1, public Foo { | |
355 | public: | |
356 | int z; | |
357 | Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; } | |
358 | }; | |
359 | ||
360 | int Foo::operator! () { return !x; } | |
361 | ||
362 | int Foo::times (int y) { return x * y; } | |
363 | ||
364 | int Foo::st = 100; | |
365 | ||
366 | Foo::operator int() { return x; } | |
367 | ||
368 | Foo foo(10, 11); | |
369 | Bar bar(20, 21, 22); | |
370 | ||
371 | class Contains_static_instance | |
372 | { | |
373 | public: | |
374 | int x; | |
375 | int y; | |
376 | Contains_static_instance (int i, int j) { x = i; y = j; } | |
377 | static Contains_static_instance null; | |
378 | }; | |
379 | ||
380 | Contains_static_instance Contains_static_instance::null(0,0); | |
381 | Contains_static_instance csi(10,20); | |
382 | ||
383 | class Contains_nested_static_instance | |
384 | { | |
385 | public: | |
386 | class Nested | |
387 | { | |
388 | public: | |
389 | Nested(int i) : z(i) {} | |
390 | int z; | |
391 | static Contains_nested_static_instance xx; | |
392 | }; | |
393 | ||
394 | Contains_nested_static_instance(int i, int j) : x(i), y(j) {} | |
395 | ||
396 | int x; | |
397 | int y; | |
398 | ||
399 | static Contains_nested_static_instance null; | |
400 | static Nested yy; | |
401 | }; | |
402 | ||
403 | Contains_nested_static_instance Contains_nested_static_instance::null(0, 0); | |
404 | Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5); | |
405 | Contains_nested_static_instance | |
406 | Contains_nested_static_instance::Nested::xx(1,2); | |
407 | Contains_nested_static_instance cnsi(30,40); | |
408 | ||
409 | typedef struct { | |
410 | int one; | |
411 | int two; | |
412 | } tagless_struct; | |
413 | tagless_struct v_tagless; | |
414 | ||
415 | /* Try to get the compiler to allocate a class in a register. */ | |
416 | class small { | |
417 | public: | |
418 | int x; | |
419 | int method (); | |
420 | }; | |
421 | int small::method () | |
422 | { | |
423 | return x + 5; | |
424 | } | |
425 | void marker_reg1 () {} | |
426 | ||
427 | int | |
428 | register_class () | |
429 | { | |
430 | /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220 | |
431 | might put this variable in a register. This is a lose, though, because | |
432 | it means that GDB can't call any methods for that variable. */ | |
433 | register small v; | |
434 | ||
435 | int i; | |
436 | ||
437 | /* Perform a computation sufficiently complicated that optimizing compilers | |
438 | won't optimized out the variable. If some compiler constant-folds this | |
439 | whole loop, maybe using a parameter to this function here would help. */ | |
440 | v.x = 0; | |
441 | for (i = 0; i < 13; ++i) | |
442 | v.x += i; | |
443 | --v.x; /* v.x is now 77 */ | |
444 | marker_reg1 (); | |
445 | return v.x + 5; | |
446 | } | |
447 | ||
448 | int | |
449 | main() | |
450 | { | |
451 | #ifdef usestubs | |
452 | set_debug_traps(); | |
453 | breakpoint(); | |
454 | #endif | |
455 | inheritance1 (); | |
456 | inheritance3 (); | |
457 | register_class (); | |
458 | ||
459 | /* FIXME: pmi gets optimized out. Need to do some more computation with | |
460 | it or something. (No one notices, because the test is xfail'd anyway, | |
461 | but that probably won't always be true...). */ | |
462 | int Foo::* pmi = &Foo::y; | |
463 | ||
464 | /* Make sure the AIX linker doesn't remove the variable. */ | |
465 | v_tagless.one = 5; | |
466 | ||
467 | return foo.*pmi; | |
468 | } | |
469 | ||
470 | /* Create an instance for some classes, otherwise they get optimized away. */ | |
471 | default_public_struct default_public_s; | |
472 | explicit_public_struct explicit_public_s; | |
473 | protected_struct protected_s; | |
474 | private_struct private_s; | |
475 | mixed_protection_struct mixed_protection_s; | |
476 | public_class public_c; | |
477 | protected_class protected_c; | |
478 | default_private_class default_private_c; | |
479 | explicit_private_class explicit_private_c; | |
480 | mixed_protection_class mixed_protection_c; |