Commit | Line | Data |
---|---|---|
ed69573c MC |
1 | /* This testcase is part of GDB, the GNU debugger. |
2 | ||
28e7fd62 | 3 | Copyright 1993-2013 Free Software Foundation, Inc. |
ed69573c 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 |
ed69573c 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 | |
ed69573c | 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/>. |
ed69573c MC |
17 | */ |
18 | ||
19 | // Test various -*- C++ -*- things. | |
20 | ||
21 | // ====================== basic C++ types ======================= | |
22 | bool v_bool; | |
23 | bool v_bool_array[2]; | |
24 | ||
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) | |
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; | |
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 | ||
290 | // ======================== static member functions ===================== | |
291 | ||
292 | class Static { | |
293 | public: | |
294 | static void ii(int, int); | |
295 | }; | |
296 | void Static::ii (int, int) { } | |
297 | ||
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; } | |
3fe8f3b3 | 402 | ~Base1 () { } |
ed69573c MC |
403 | }; |
404 | ||
3fe8f3b3 KS |
405 | typedef Base1 base1; |
406 | ||
ed69573c MC |
407 | class Foo |
408 | { | |
409 | public: | |
410 | int x; | |
411 | int y; | |
412 | static int st; | |
413 | Foo (int i, int j) { x = i; y = j; } | |
414 | int operator! (); | |
415 | operator int (); | |
416 | int times (int y); | |
417 | }; | |
418 | ||
40f0318e KS |
419 | typedef Foo ByAnyOtherName; |
420 | ||
ed69573c MC |
421 | class Bar : public Base1, public Foo { |
422 | public: | |
423 | int z; | |
424 | Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; } | |
425 | }; | |
426 | ||
427 | int Foo::operator! () { return !x; } | |
428 | ||
429 | int Foo::times (int y) { return x * y; } | |
430 | ||
431 | int Foo::st = 100; | |
432 | ||
433 | Foo::operator int() { return x; } | |
434 | ||
40f0318e | 435 | ByAnyOtherName foo(10, 11); |
ed69573c MC |
436 | Bar bar(20, 21, 22); |
437 | ||
438 | class ClassWithEnum { | |
439 | public: | |
440 | enum PrivEnum { red, green, blue, yellow = 42 }; | |
441 | PrivEnum priv_enum; | |
442 | int x; | |
443 | }; | |
444 | ||
445 | void enums2 (void) | |
446 | { | |
447 | } | |
448 | ||
449 | /* classes.exp relies on statement order in this function for testing | |
450 | enumeration fields. */ | |
451 | ||
452 | void enums1 () | |
453 | { | |
454 | ClassWithEnum obj_with_enum; | |
455 | obj_with_enum.priv_enum = ClassWithEnum::red; | |
456 | obj_with_enum.x = 0; | |
457 | enums2 (); | |
458 | obj_with_enum.priv_enum = ClassWithEnum::green; | |
c7414a01 | 459 | obj_with_enum.x = 1; |
ed69573c MC |
460 | } |
461 | ||
462 | class ClassParam { | |
463 | public: | |
464 | int Aptr_a (A *a) { return a->a; } | |
465 | int Aptr_x (A *a) { return a->x; } | |
466 | int Aref_a (A &a) { return a.a; } | |
467 | int Aref_x (A &a) { return a.x; } | |
468 | int Aval_a (A a) { return a.a; } | |
469 | int Aval_x (A a) { return a.x; } | |
470 | }; | |
471 | ||
472 | ClassParam class_param; | |
473 | ||
474 | class Contains_static_instance | |
475 | { | |
476 | public: | |
477 | int x; | |
478 | int y; | |
479 | Contains_static_instance (int i, int j) { x = i; y = j; } | |
480 | static Contains_static_instance null; | |
481 | }; | |
482 | ||
483 | Contains_static_instance Contains_static_instance::null(0,0); | |
484 | Contains_static_instance csi(10,20); | |
485 | ||
486 | class Contains_nested_static_instance | |
487 | { | |
488 | public: | |
489 | class Nested | |
490 | { | |
491 | public: | |
492 | Nested(int i) : z(i) {} | |
493 | int z; | |
494 | static Contains_nested_static_instance xx; | |
495 | }; | |
496 | ||
497 | Contains_nested_static_instance(int i, int j) : x(i), y(j) {} | |
498 | ||
499 | int x; | |
500 | int y; | |
501 | ||
502 | static Contains_nested_static_instance null; | |
503 | static Nested yy; | |
504 | }; | |
505 | ||
506 | Contains_nested_static_instance Contains_nested_static_instance::null(0, 0); | |
507 | Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5); | |
508 | Contains_nested_static_instance | |
509 | Contains_nested_static_instance::Nested::xx(1,2); | |
510 | Contains_nested_static_instance cnsi(30,40); | |
511 | ||
512 | typedef struct { | |
513 | int one; | |
514 | int two; | |
515 | } tagless_struct; | |
516 | tagless_struct v_tagless; | |
517 | ||
518 | /* Try to get the compiler to allocate a class in a register. */ | |
519 | class small { | |
520 | public: | |
521 | int x; | |
522 | int method (); | |
523 | }; | |
524 | ||
525 | int | |
526 | small::method () | |
527 | { | |
528 | return x + 5; | |
529 | } | |
530 | ||
531 | void marker_reg1 () {} | |
532 | ||
533 | int | |
534 | register_class () | |
535 | { | |
536 | /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220 | |
537 | might put this variable in a register. This is a lose, though, because | |
538 | it means that GDB can't call any methods for that variable. */ | |
539 | register small v; | |
540 | ||
541 | int i; | |
542 | ||
543 | /* Perform a computation sufficiently complicated that optimizing compilers | |
544 | won't optimized out the variable. If some compiler constant-folds this | |
545 | whole loop, maybe using a parameter to this function here would help. */ | |
546 | v.x = 0; | |
547 | for (i = 0; i < 13; ++i) | |
548 | v.x += i; | |
549 | --v.x; /* v.x is now 77 */ | |
550 | marker_reg1 (); | |
551 | return v.x + 5; | |
552 | } | |
553 | ||
554 | void dummy() | |
555 | { | |
556 | v_bool = true; | |
557 | v_bool_array[0] = false; | |
558 | v_bool_array[1] = v_bool; | |
559 | } | |
560 | ||
561 | void use_methods () | |
562 | { | |
563 | /* Refer to methods so that they don't get optimized away. */ | |
564 | int i; | |
565 | i = class_param.Aptr_a (&g_A); | |
566 | i = class_param.Aptr_x (&g_A); | |
567 | i = class_param.Aref_a (g_A); | |
568 | i = class_param.Aref_x (g_A); | |
569 | i = class_param.Aval_a (g_A); | |
570 | i = class_param.Aval_x (g_A); | |
3fe8f3b3 KS |
571 | |
572 | base1 b (3); | |
ed69573c MC |
573 | } |
574 | ||
575 | ||
576 | int | |
577 | main() | |
578 | { | |
ed69573c MC |
579 | dummy(); |
580 | inheritance1 (); | |
581 | inheritance3 (); | |
582 | enums1 (); | |
583 | register_class (); | |
584 | ||
585 | /* FIXME: pmi gets optimized out. Need to do some more computation with | |
586 | it or something. (No one notices, because the test is xfail'd anyway, | |
587 | but that probably won't always be true...). */ | |
588 | int Foo::* pmi = &Foo::y; | |
589 | ||
590 | /* Make sure the AIX linker doesn't remove the variable. */ | |
591 | v_tagless.one = 5; | |
592 | ||
593 | use_methods (); | |
594 | ||
595 | return foo.*pmi; | |
596 | } | |
597 | ||
598 | /* Create an instance for some classes, otherwise they get optimized away. */ | |
599 | ||
600 | default_public_struct default_public_s; | |
601 | explicit_public_struct explicit_public_s; | |
602 | protected_struct protected_s; | |
603 | private_struct private_s; | |
604 | mixed_protection_struct mixed_protection_s; | |
605 | public_class public_c; | |
606 | protected_class protected_c; | |
607 | default_private_class default_private_c; | |
608 | explicit_private_class explicit_private_c; | |
609 | mixed_protection_class mixed_protection_c; |