Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / testsuite / gdb.cp / classes.cc
blobd09f38fc36db32c346f62a8f8de73cd6de4afa3a
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2004
4 Free Software Foundation, Inc.
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.
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.
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.
21 // Test various -*- C++ -*- things.
23 // ====================== basic C++ types =======================
24 bool v_bool;
25 bool v_bool_array[2];
27 typedef struct fleep fleep;
28 struct fleep { int a; } s;
30 // ====================== simple class structures =======================
32 struct default_public_struct {
33 // defaults to public:
34 int a;
35 int b;
38 struct explicit_public_struct {
39 public:
40 int a;
41 int b;
44 struct protected_struct {
45 protected:
46 int a;
47 int b;
50 struct private_struct {
51 private:
52 int a;
53 int b;
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;
74 class public_class {
75 public:
76 int a;
77 int b;
80 class protected_class {
81 protected:
82 int a;
83 int b;
86 class default_private_class {
87 // defaults to private:
88 int a;
89 int b;
92 class explicit_private_class {
93 private:
94 int a;
95 int b;
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;
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;
125 int const_vol_method_class::foo (int & ir) const
127 return ir + 3;
129 int const_vol_method_class::bar (int & ir) volatile
131 return ir + 4;
133 int const_vol_method_class::baz (int & ir) const volatile
135 return ir + 5;
138 // ========================= simple inheritance ==========================
140 class A {
141 public:
142 int a;
143 int x;
146 A g_A;
148 class B : public A {
149 public:
150 int b;
151 int x;
154 B g_B;
156 class C : public A {
157 public:
158 int c;
159 int x;
162 C g_C;
164 class D : public B, public C {
165 public:
166 int d;
167 int x;
170 D g_D;
172 class E : public D {
173 public:
174 int e;
175 int x;
178 E g_E;
180 class class_with_anon_union
182 public:
183 int one;
184 union
186 int a;
187 long b;
191 class_with_anon_union g_anon_union;
193 void inheritance2 (void)
197 void inheritance1 (void)
199 int ival;
200 int *intp;
202 // {A::a, A::x}
204 g_A.A::a = 1;
205 g_A.A::x = 2;
207 // {{A::a,A::x},B::b,B::x}
209 g_B.A::a = 3;
210 g_B.A::x = 4;
211 g_B.B::b = 5;
212 g_B.B::x = 6;
214 // {{A::a,A::x},C::c,C::x}
216 g_C.A::a = 7;
217 g_C.A::x = 8;
218 g_C.C::c = 9;
219 g_C.C::x = 10;
221 // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}
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.
227 for (intp = (int *) &g_D, ival = 11;
228 intp < ((int *) &g_D + sizeof (g_D) / sizeof (int));
229 intp++, ival++)
231 *intp = ival;
234 // Overlay the nonportable initialization with legal initialization.
236 // ????? = 11; (g_D.A::a = 11; is ambiguous)
237 // ????? = 12; (g_D.A::x = 12; is ambiguous)
238 /* djb 6-3-2000
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.
244 g_D.C::a = 15;
245 g_D.C::x = 12;
246 g_D.B::a = 11;
247 g_D.B::x = 12;
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;
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}
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.
264 for (intp = (int *) &g_E, ival = 21;
265 intp < ((int *) &g_E + sizeof (g_E) / sizeof (int));
266 intp++, ival++)
268 *intp = ival;
271 // Overlay the nonportable initialization with legal initialization.
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;
286 g_anon_union.one = 1;
287 g_anon_union.a = 2;
289 inheritance2 ();
292 // ======================== static member functions =====================
294 class Static {
295 public:
296 static void ii(int, int);
298 void Static::ii (int, int) { }
300 // ======================== virtual base classes=========================
302 class vA {
303 public:
304 int va;
305 int vx;
308 vA g_vA;
310 class vB : public virtual vA {
311 public:
312 int vb;
313 int vx;
316 vB g_vB;
318 class vC : public virtual vA {
319 public:
320 int vc;
321 int vx;
324 vC g_vC;
326 class vD : public virtual vB, public virtual vC {
327 public:
328 int vd;
329 int vx;
332 vD g_vD;
334 class vE : public virtual vD {
335 public:
336 int ve;
337 int vx;
340 vE g_vE;
342 void inheritance4 (void)
346 void inheritance3 (void)
348 int ival;
349 int *intp;
351 // {vA::va, vA::vx}
353 g_vA.vA::va = 1;
354 g_vA.vA::vx = 2;
356 // {{vA::va, vA::vx}, vB::vb, vB::vx}
358 g_vB.vA::va = 3;
359 g_vB.vA::vx = 4;
360 g_vB.vB::vb = 5;
361 g_vB.vB::vx = 6;
363 // {{vA::va, vA::vx}, vC::vc, vC::vx}
365 g_vC.vA::va = 7;
366 g_vC.vA::vx = 8;
367 g_vC.vC::vc = 9;
368 g_vC.vC::vx = 10;
370 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
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;
382 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
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;
395 inheritance4 ();
398 // ======================================================================
400 class Base1 {
401 public:
402 int x;
403 Base1(int i) { x = i; }
406 class Foo
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);
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; }
424 int Foo::operator! () { return !x; }
426 int Foo::times (int y) { return x * y; }
428 int Foo::st = 100;
430 Foo::operator int() { return x; }
432 Foo foo(10, 11);
433 Bar bar(20, 21, 22);
435 class ClassWithEnum {
436 public:
437 enum PrivEnum { red, green, blue, yellow = 42 };
438 PrivEnum priv_enum;
439 int x;
442 void enums2 (void)
446 /* classes.exp relies on statement order in this function for testing
447 enumeration fields. */
449 void enums1 ()
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 obj_with_enum.x = 1;
459 class ClassParam {
460 public:
461 int Aptr_a (A *a) { return a->a; }
462 int Aptr_x (A *a) { return a->x; }
463 int Aref_a (A &a) { return a.a; }
464 int Aref_x (A &a) { return a.x; }
465 int Aval_a (A a) { return a.a; }
466 int Aval_x (A a) { return a.x; }
469 ClassParam class_param;
471 class Contains_static_instance
473 public:
474 int x;
475 int y;
476 Contains_static_instance (int i, int j) { x = i; y = j; }
477 static Contains_static_instance null;
480 Contains_static_instance Contains_static_instance::null(0,0);
481 Contains_static_instance csi(10,20);
483 class Contains_nested_static_instance
485 public:
486 class Nested
488 public:
489 Nested(int i) : z(i) {}
490 int z;
491 static Contains_nested_static_instance xx;
494 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
496 int x;
497 int y;
499 static Contains_nested_static_instance null;
500 static Nested yy;
503 Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
504 Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
505 Contains_nested_static_instance
506 Contains_nested_static_instance::Nested::xx(1,2);
507 Contains_nested_static_instance cnsi(30,40);
509 typedef struct {
510 int one;
511 int two;
512 } tagless_struct;
513 tagless_struct v_tagless;
515 /* Try to get the compiler to allocate a class in a register. */
516 class small {
517 public:
518 int x;
519 int method ();
523 small::method ()
525 return x + 5;
528 void marker_reg1 () {}
531 register_class ()
533 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
534 might put this variable in a register. This is a lose, though, because
535 it means that GDB can't call any methods for that variable. */
536 register small v;
538 int i;
540 /* Perform a computation sufficiently complicated that optimizing compilers
541 won't optimized out the variable. If some compiler constant-folds this
542 whole loop, maybe using a parameter to this function here would help. */
543 v.x = 0;
544 for (i = 0; i < 13; ++i)
545 v.x += i;
546 --v.x; /* v.x is now 77 */
547 marker_reg1 ();
548 return v.x + 5;
551 void dummy()
553 v_bool = true;
554 v_bool_array[0] = false;
555 v_bool_array[1] = v_bool;
558 void use_methods ()
560 /* Refer to methods so that they don't get optimized away. */
561 int i;
562 i = class_param.Aptr_a (&g_A);
563 i = class_param.Aptr_x (&g_A);
564 i = class_param.Aref_a (g_A);
565 i = class_param.Aref_x (g_A);
566 i = class_param.Aval_a (g_A);
567 i = class_param.Aval_x (g_A);
572 main()
574 #ifdef usestubs
575 set_debug_traps();
576 breakpoint();
577 #endif
578 dummy();
579 inheritance1 ();
580 inheritance3 ();
581 enums1 ();
582 register_class ();
584 /* FIXME: pmi gets optimized out. Need to do some more computation with
585 it or something. (No one notices, because the test is xfail'd anyway,
586 but that probably won't always be true...). */
587 int Foo::* pmi = &Foo::y;
589 /* Make sure the AIX linker doesn't remove the variable. */
590 v_tagless.one = 5;
592 use_methods ();
594 return foo.*pmi;
597 /* Create an instance for some classes, otherwise they get optimized away. */
599 default_public_struct default_public_s;
600 explicit_public_struct explicit_public_s;
601 protected_struct protected_s;
602 private_struct private_s;
603 mixed_protection_struct mixed_protection_s;
604 public_class public_c;
605 protected_class protected_c;
606 default_private_class default_private_c;
607 explicit_private_class explicit_private_c;
608 mixed_protection_class mixed_protection_c;