Commit | Line | Data |
---|---|---|
28301461 MC |
1 | /* This testcase is part of GDB, the GNU debugger. |
2 | ||
28e7fd62 | 3 | Copyright 1993-2013 Free Software Foundation, Inc. |
28301461 MC |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 7 | the Free Software Foundation; either version 3 of the License, or |
28301461 MC |
8 | (at your option) any later version. |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
a9762ec7 | 14 | |
28301461 | 15 | You should have received a copy of the GNU General Public License |
a9762ec7 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28301461 MC |
17 | */ |
18 | ||
c906108c SS |
19 | // Test various -*- C++ -*- things. |
20 | ||
a0b3c4fd JM |
21 | // ====================== basic C++ types ======================= |
22 | bool v_bool; | |
23 | bool v_bool_array[2]; | |
24 | ||
c906108c SS |
25 | typedef struct fleep fleep; |
26 | struct fleep { int a; } s; | |
27 | ||
28 | // ====================== simple class structures ======================= | |
29 | ||
30 | struct default_public_struct { | |
31 | // defaults to public: | |
32 | int a; | |
33 | int b; | |
34 | }; | |
35 | ||
36 | struct explicit_public_struct { | |
37 | public: | |
38 | int a; | |
39 | int b; | |
40 | }; | |
41 | ||
42 | struct protected_struct { | |
43 | protected: | |
44 | int a; | |
45 | int b; | |
46 | }; | |
47 | ||
48 | struct private_struct { | |
49 | private: | |
50 | int a; | |
51 | int b; | |
52 | }; | |
53 | ||
54 | struct mixed_protection_struct { | |
55 | public: | |
56 | int a; | |
57 | int b; | |
58 | private: | |
59 | int c; | |
60 | int d; | |
61 | protected: | |
62 | int e; | |
63 | int f; | |
64 | public: | |
65 | int g; | |
66 | private: | |
67 | int h; | |
68 | protected: | |
69 | int i; | |
70 | }; | |
71 | ||
72 | class public_class { | |
73 | public: | |
74 | int a; | |
75 | int b; | |
76 | }; | |
77 | ||
78 | class protected_class { | |
79 | protected: | |
80 | int a; | |
81 | int b; | |
82 | }; | |
83 | ||
84 | class default_private_class { | |
85 | // defaults to private: | |
86 | int a; | |
87 | int b; | |
88 | }; | |
89 | ||
90 | class explicit_private_class { | |
91 | private: | |
92 | int a; | |
93 | int b; | |
94 | }; | |
95 | ||
96 | class mixed_protection_class { | |
97 | public: | |
98 | int a; | |
99 | int b; | |
100 | private: | |
101 | int c; | |
102 | int d; | |
103 | protected: | |
104 | int e; | |
105 | int f; | |
106 | public: | |
107 | int g; | |
108 | private: | |
109 | int h; | |
110 | protected: | |
111 | int i; | |
112 | }; | |
113 | ||
114 | class const_vol_method_class { | |
115 | public: | |
116 | int a; | |
117 | int b; | |
118 | int foo (int &) const; | |
119 | int bar (int &) volatile; | |
120 | int baz (int &) const volatile; | |
121 | }; | |
122 | ||
123 | int const_vol_method_class::foo (int & ir) const | |
124 | { | |
125 | return ir + 3; | |
126 | } | |
127 | int const_vol_method_class::bar (int & ir) volatile | |
128 | { | |
129 | return ir + 4; | |
130 | } | |
131 | int const_vol_method_class::baz (int & ir) const volatile | |
132 | { | |
133 | return ir + 5; | |
134 | } | |
135 | ||
136 | // ========================= simple inheritance ========================== | |
137 | ||
138 | class A { | |
139 | public: | |
140 | int a; | |
141 | int x; | |
142 | }; | |
143 | ||
144 | A g_A; | |
145 | ||
146 | class B : public A { | |
147 | public: | |
148 | int b; | |
149 | int x; | |
150 | }; | |
151 | ||
152 | B g_B; | |
153 | ||
154 | class C : public A { | |
155 | public: | |
156 | int c; | |
157 | int x; | |
158 | }; | |
159 | ||
160 | C g_C; | |
161 | ||
162 | class D : public B, public C { | |
163 | public: | |
164 | int d; | |
165 | int x; | |
166 | }; | |
167 | ||
168 | D g_D; | |
169 | ||
170 | class E : public D { | |
171 | public: | |
172 | int e; | |
173 | int x; | |
174 | }; | |
175 | ||
176 | E g_E; | |
177 | ||
178 | class class_with_anon_union | |
179 | { | |
180 | public: | |
181 | int one; | |
182 | union | |
183 | { | |
184 | int a; | |
185 | long b; | |
186 | }; | |
187 | }; | |
188 | ||
189 | class_with_anon_union g_anon_union; | |
190 | ||
191 | void inheritance2 (void) | |
192 | { | |
193 | } | |
194 | ||
195 | void inheritance1 (void) | |
196 | { | |
197 | int ival; | |
198 | int *intp; | |
199 | ||
200 | // {A::a, A::x} | |
201 | ||
202 | g_A.A::a = 1; | |
203 | g_A.A::x = 2; | |
204 | ||
205 | // {{A::a,A::x},B::b,B::x} | |
206 | ||
207 | g_B.A::a = 3; | |
208 | g_B.A::x = 4; | |
209 | g_B.B::b = 5; | |
210 | g_B.B::x = 6; | |
211 | ||
212 | // {{A::a,A::x},C::c,C::x} | |
213 | ||
214 | g_C.A::a = 7; | |
215 | g_C.A::x = 8; | |
216 | g_C.C::c = 9; | |
217 | g_C.C::x = 10; | |
218 | ||
219 | // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x} | |
220 | ||
221 | // The following initialization code is non-portable, but allows us | |
222 | // to initialize all members of g_D until we can fill in the missing | |
223 | // initialization code with legal C++ code. | |
224 | ||
225 | for (intp = (int *) &g_D, ival = 11; | |
226 | intp < ((int *) &g_D + sizeof (g_D) / sizeof (int)); | |
227 | intp++, ival++) | |
228 | { | |
229 | *intp = ival; | |
230 | } | |
231 | ||
232 | // Overlay the nonportable initialization with legal initialization. | |
233 | ||
234 | // ????? = 11; (g_D.A::a = 11; is ambiguous) | |
235 | // ????? = 12; (g_D.A::x = 12; is ambiguous) | |
e6d71bf3 DB |
236 | /* djb 6-3-2000 |
237 | ||
238 | This should take care of it. Rather than try to initialize using an ambiguous | |
239 | construct, use 2 unambiguous ones for each. Since the ambiguous a/x member is | |
240 | coming from C, and B, initialize D's C::a, and B::a, and D's C::x and B::x. | |
241 | */ | |
242 | g_D.C::a = 15; | |
243 | g_D.C::x = 12; | |
244 | g_D.B::a = 11; | |
245 | g_D.B::x = 12; | |
c906108c SS |
246 | g_D.B::b = 13; |
247 | g_D.B::x = 14; | |
248 | // ????? = 15; | |
249 | // ????? = 16; | |
250 | g_D.C::c = 17; | |
251 | g_D.C::x = 18; | |
252 | g_D.D::d = 19; | |
253 | g_D.D::x = 20; | |
254 | ||
255 | ||
256 | // {{{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}},E::e,E::x} | |
257 | ||
258 | // The following initialization code is non-portable, but allows us | |
259 | // to initialize all members of g_D until we can fill in the missing | |
260 | // initialization code with legal C++ code. | |
261 | ||
262 | for (intp = (int *) &g_E, ival = 21; | |
263 | intp < ((int *) &g_E + sizeof (g_E) / sizeof (int)); | |
264 | intp++, ival++) | |
265 | { | |
266 | *intp = ival; | |
267 | } | |
268 | ||
269 | // Overlay the nonportable initialization with legal initialization. | |
270 | ||
271 | // ????? = 21; (g_E.A::a = 21; is ambiguous) | |
272 | // ????? = 22; (g_E.A::x = 22; is ambiguous) | |
273 | g_E.B::b = 23; | |
274 | g_E.B::x = 24; | |
275 | // ????? = 25; | |
276 | // ????? = 26; | |
277 | g_E.C::c = 27; | |
278 | g_E.C::x = 28; | |
279 | g_E.D::d = 29; | |
280 | g_E.D::x = 30; | |
281 | g_E.E::e = 31; | |
282 | g_E.E::x = 32; | |
283 | ||
284 | g_anon_union.one = 1; | |
285 | g_anon_union.a = 2; | |
286 | ||
287 | inheritance2 (); | |
288 | } | |
289 | ||
af890c52 DJ |
290 | // ======================== static member functions ===================== |
291 | ||
292 | class Static { | |
293 | public: | |
294 | static void ii(int, int); | |
295 | }; | |
296 | void Static::ii (int, int) { } | |
297 | ||
c906108c SS |
298 | // ======================== virtual base classes========================= |
299 | ||
300 | class vA { | |
301 | public: | |
302 | int va; | |
303 | int vx; | |
304 | }; | |
305 | ||
306 | vA g_vA; | |
307 | ||
308 | class vB : public virtual vA { | |
309 | public: | |
310 | int vb; | |
311 | int vx; | |
312 | }; | |
313 | ||
314 | vB g_vB; | |
315 | ||
316 | class vC : public virtual vA { | |
317 | public: | |
318 | int vc; | |
319 | int vx; | |
320 | }; | |
321 | ||
322 | vC g_vC; | |
323 | ||
324 | class vD : public virtual vB, public virtual vC { | |
325 | public: | |
326 | int vd; | |
327 | int vx; | |
328 | }; | |
329 | ||
330 | vD g_vD; | |
331 | ||
332 | class vE : public virtual vD { | |
333 | public: | |
334 | int ve; | |
335 | int vx; | |
336 | }; | |
337 | ||
338 | vE g_vE; | |
339 | ||
340 | void inheritance4 (void) | |
341 | { | |
342 | } | |
343 | ||
344 | void inheritance3 (void) | |
345 | { | |
346 | int ival; | |
347 | int *intp; | |
348 | ||
349 | // {vA::va, vA::vx} | |
350 | ||
351 | g_vA.vA::va = 1; | |
352 | g_vA.vA::vx = 2; | |
353 | ||
354 | // {{vA::va, vA::vx}, vB::vb, vB::vx} | |
355 | ||
356 | g_vB.vA::va = 3; | |
357 | g_vB.vA::vx = 4; | |
358 | g_vB.vB::vb = 5; | |
359 | g_vB.vB::vx = 6; | |
360 | ||
361 | // {{vA::va, vA::vx}, vC::vc, vC::vx} | |
362 | ||
363 | g_vC.vA::va = 7; | |
364 | g_vC.vA::vx = 8; | |
365 | g_vC.vC::vc = 9; | |
366 | g_vC.vC::vx = 10; | |
367 | ||
368 | // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx} | |
369 | ||
370 | g_vD.vA::va = 11; | |
371 | g_vD.vA::vx = 12; | |
372 | g_vD.vB::vb = 13; | |
373 | g_vD.vB::vx = 14; | |
374 | g_vD.vC::vc = 15; | |
375 | g_vD.vC::vx = 16; | |
376 | g_vD.vD::vd = 17; | |
377 | g_vD.vD::vx = 18; | |
378 | ||
379 | ||
380 | // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx} | |
381 | ||
382 | g_vD.vA::va = 19; | |
383 | g_vD.vA::vx = 20; | |
384 | g_vD.vB::vb = 21; | |
385 | g_vD.vB::vx = 22; | |
386 | g_vD.vC::vc = 23; | |
387 | g_vD.vC::vx = 24; | |
388 | g_vD.vD::vd = 25; | |
389 | g_vD.vD::vx = 26; | |
390 | g_vE.vE::ve = 27; | |
391 | g_vE.vE::vx = 28; | |
392 | ||
393 | inheritance4 (); | |
394 | } | |
395 | ||
396 | // ====================================================================== | |
397 | ||
398 | class Base1 { | |
399 | public: | |
400 | int x; | |
401 | Base1(int i) { x = i; } | |
402 | }; | |
403 | ||
404 | class Foo | |
405 | { | |
406 | public: | |
407 | int x; | |
408 | int y; | |
409 | static int st; | |
410 | Foo (int i, int j) { x = i; y = j; } | |
411 | int operator! (); | |
412 | operator int (); | |
413 | int times (int y); | |
414 | }; | |
415 | ||
416 | class Bar : public Base1, public Foo { | |
417 | public: | |
418 | int z; | |
419 | Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; } | |
420 | }; | |
421 | ||
c906108c SS |
422 | int Foo::operator! () { return !x; } |
423 | ||
424 | int Foo::times (int y) { return x * y; } | |
425 | ||
426 | int Foo::st = 100; | |
427 | ||
428 | Foo::operator int() { return x; } | |
429 | ||
430 | Foo foo(10, 11); | |
431 | Bar bar(20, 21, 22); | |
432 | ||
2bec0572 ND |
433 | class ClassWithEnum { |
434 | public: | |
435 | enum PrivEnum { red, green, blue, yellow = 42 }; | |
436 | PrivEnum priv_enum; | |
437 | int x; | |
438 | }; | |
439 | ||
440 | void enums2 (void) | |
441 | { | |
442 | } | |
443 | ||
444 | /* classes.exp relies on statement order in this function for testing | |
445 | enumeration fields. */ | |
446 | ||
447 | void enums1 () | |
448 | { | |
449 | ClassWithEnum obj_with_enum; | |
450 | obj_with_enum.priv_enum = ClassWithEnum::red; | |
451 | obj_with_enum.x = 0; | |
452 | enums2 (); | |
453 | obj_with_enum.priv_enum = ClassWithEnum::green; | |
454 | } | |
455 | ||
54602222 ND |
456 | class ClassParam { |
457 | public: | |
458 | int Aptr_a (A *a) { return a->a; } | |
459 | int Aptr_x (A *a) { return a->x; } | |
460 | int Aref_a (A &a) { return a.a; } | |
461 | int Aref_x (A &a) { return a.x; } | |
462 | int Aval_a (A a) { return a.a; } | |
463 | int Aval_x (A a) { return a.x; } | |
464 | }; | |
465 | ||
466 | ClassParam class_param; | |
467 | ||
c906108c SS |
468 | class Contains_static_instance |
469 | { | |
470 | public: | |
471 | int x; | |
472 | int y; | |
473 | Contains_static_instance (int i, int j) { x = i; y = j; } | |
474 | static Contains_static_instance null; | |
475 | }; | |
476 | ||
477 | Contains_static_instance Contains_static_instance::null(0,0); | |
478 | Contains_static_instance csi(10,20); | |
479 | ||
480 | class Contains_nested_static_instance | |
481 | { | |
482 | public: | |
483 | class Nested | |
484 | { | |
485 | public: | |
486 | Nested(int i) : z(i) {} | |
487 | int z; | |
488 | static Contains_nested_static_instance xx; | |
489 | }; | |
490 | ||
491 | Contains_nested_static_instance(int i, int j) : x(i), y(j) {} | |
492 | ||
493 | int x; | |
494 | int y; | |
495 | ||
496 | static Contains_nested_static_instance null; | |
497 | static Nested yy; | |
498 | }; | |
499 | ||
500 | Contains_nested_static_instance Contains_nested_static_instance::null(0, 0); | |
501 | Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5); | |
502 | Contains_nested_static_instance | |
503 | Contains_nested_static_instance::Nested::xx(1,2); | |
504 | Contains_nested_static_instance cnsi(30,40); | |
505 | ||
506 | typedef struct { | |
507 | int one; | |
508 | int two; | |
509 | } tagless_struct; | |
510 | tagless_struct v_tagless; | |
511 | ||
512 | /* Try to get the compiler to allocate a class in a register. */ | |
513 | class small { | |
514 | public: | |
515 | int x; | |
516 | int method (); | |
517 | }; | |
518 | ||
519 | int | |
520 | small::method () | |
521 | { | |
522 | return x + 5; | |
523 | } | |
524 | ||
525 | void marker_reg1 () {} | |
526 | ||
527 | int | |
528 | register_class () | |
529 | { | |
530 | /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220 | |
531 | might put this variable in a register. This is a lose, though, because | |
532 | it means that GDB can't call any methods for that variable. */ | |
533 | register small v; | |
534 | ||
535 | int i; | |
536 | ||
537 | /* Perform a computation sufficiently complicated that optimizing compilers | |
538 | won't optimized out the variable. If some compiler constant-folds this | |
539 | whole loop, maybe using a parameter to this function here would help. */ | |
540 | v.x = 0; | |
541 | for (i = 0; i < 13; ++i) | |
542 | v.x += i; | |
543 | --v.x; /* v.x is now 77 */ | |
544 | marker_reg1 (); | |
545 | return v.x + 5; | |
546 | } | |
547 | ||
a0b3c4fd JM |
548 | void dummy() |
549 | { | |
550 | v_bool = true; | |
551 | v_bool_array[0] = false; | |
552 | v_bool_array[1] = v_bool; | |
553 | } | |
554 | ||
54602222 ND |
555 | void use_methods () |
556 | { | |
557 | /* Refer to methods so that they don't get optimized away. */ | |
558 | int i; | |
559 | i = class_param.Aptr_a (&g_A); | |
560 | i = class_param.Aptr_x (&g_A); | |
561 | i = class_param.Aref_a (g_A); | |
562 | i = class_param.Aref_x (g_A); | |
563 | i = class_param.Aval_a (g_A); | |
564 | i = class_param.Aval_x (g_A); | |
565 | } | |
566 | ||
a0b3c4fd | 567 | |
c906108c SS |
568 | int |
569 | main() | |
570 | { | |
a0b3c4fd | 571 | dummy(); |
c906108c SS |
572 | inheritance1 (); |
573 | inheritance3 (); | |
2bec0572 | 574 | enums1 (); |
c906108c SS |
575 | register_class (); |
576 | ||
577 | /* FIXME: pmi gets optimized out. Need to do some more computation with | |
578 | it or something. (No one notices, because the test is xfail'd anyway, | |
579 | but that probably won't always be true...). */ | |
580 | int Foo::* pmi = &Foo::y; | |
581 | ||
582 | /* Make sure the AIX linker doesn't remove the variable. */ | |
583 | v_tagless.one = 5; | |
584 | ||
54602222 ND |
585 | use_methods (); |
586 | ||
c906108c SS |
587 | return foo.*pmi; |
588 | } | |
589 | ||
590 | /* Create an instance for some classes, otherwise they get optimized away. */ | |
591 | ||
592 | default_public_struct default_public_s; | |
593 | explicit_public_struct explicit_public_s; | |
594 | protected_struct protected_s; | |
595 | private_struct private_s; | |
596 | mixed_protection_struct mixed_protection_s; | |
597 | public_class public_c; | |
598 | protected_class protected_c; | |
599 | default_private_class default_private_c; | |
600 | explicit_private_class explicit_private_c; | |
601 | mixed_protection_class mixed_protection_c; |