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