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