Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / testsuite / gdb.cp / misc.cc
blob7d69ed225ae523c7c1586632231c25edbdc93751
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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;
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; }
468 ClassParam class_param;
470 class Contains_static_instance
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;
479 Contains_static_instance Contains_static_instance::null(0,0);
480 Contains_static_instance csi(10,20);
482 class Contains_nested_static_instance
484 public:
485 class Nested
487 public:
488 Nested(int i) : z(i) {}
489 int z;
490 static Contains_nested_static_instance xx;
493 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
495 int x;
496 int y;
498 static Contains_nested_static_instance null;
499 static Nested yy;
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);
508 typedef struct {
509 int one;
510 int two;
511 } tagless_struct;
512 tagless_struct v_tagless;
514 /* Try to get the compiler to allocate a class in a register. */
515 class small {
516 public:
517 int x;
518 int method ();
522 small::method ()
524 return x + 5;
527 void marker_reg1 () {}
530 register_class ()
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;
537 int i;
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;
550 void dummy()
552 v_bool = true;
553 v_bool_array[0] = false;
554 v_bool_array[1] = v_bool;
557 void use_methods ()
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);
571 main()
573 #ifdef usestubs
574 set_debug_traps();
575 breakpoint();
576 #endif
577 dummy();
578 inheritance1 ();
579 inheritance3 ();
580 enums1 ();
581 register_class ();
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;
588 /* Make sure the AIX linker doesn't remove the variable. */
589 v_tagless.one = 5;
591 use_methods ();
593 return foo.*pmi;
596 /* Create an instance for some classes, otherwise they get optimized away. */
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;