1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 1993-2001, 2007-2012 Free Software Foundation, Inc.
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
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Test various -*- C++ -*- things.
21 // ====================== basic C++ types =======================
25 typedef struct fleep fleep;
26 struct fleep { int a; } s;
28 // ====================== simple class structures =======================
30 struct default_public_struct {
31 // defaults to public:
36 struct explicit_public_struct {
42 struct protected_struct {
48 struct private_struct {
54 struct mixed_protection_struct {
78 class protected_class {
84 class default_private_class {
85 // defaults to private:
90 class explicit_private_class {
96 class mixed_protection_class {
114 class const_vol_method_class {
118 int foo (int &) const;
119 int bar (int &) volatile;
120 int baz (int &) const volatile;
123 int const_vol_method_class::foo (int & ir) const
127 int const_vol_method_class::bar (int & ir) volatile
131 int const_vol_method_class::baz (int & ir) const volatile
136 // ========================= simple inheritance ==========================
162 class D : public B, public C {
178 class class_with_anon_union
189 class_with_anon_union g_anon_union;
191 void inheritance2 (void)
195 void inheritance1 (void)
205 // {{A::a,A::x},B::b,B::x}
212 // {{A::a,A::x},C::c,C::x}
219 // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}
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.
225 for (intp = (int *) &g_D, ival = 11;
226 intp < ((int *) &g_D + sizeof (g_D) / sizeof (int));
232 // Overlay the nonportable initialization with legal initialization.
234 // ????? = 11; (g_D.A::a = 11; is ambiguous)
235 // ????? = 12; (g_D.A::x = 12; is ambiguous)
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.
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}
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.
262 for (intp = (int *) &g_E, ival = 21;
263 intp < ((int *) &g_E + sizeof (g_E) / sizeof (int));
269 // Overlay the nonportable initialization with legal initialization.
271 // ????? = 21; (g_E.A::a = 21; is ambiguous)
272 // ????? = 22; (g_E.A::x = 22; is ambiguous)
284 g_anon_union.one = 1;
290 // ======================== static member functions =====================
294 static void ii(int, int);
296 void Static::ii (int, int) { }
298 // ======================== virtual base classes=========================
308 class vB : public virtual vA {
316 class vC : public virtual vA {
324 class vD : public virtual vB, public virtual vC {
332 class vE : public virtual vD {
340 void inheritance4 (void)
344 void inheritance3 (void)
354 // {{vA::va, vA::vx}, vB::vb, vB::vx}
361 // {{vA::va, vA::vx}, vC::vc, vC::vx}
368 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
380 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
396 // ======================================================================
401 Base1(int i) { x = i; }
410 Foo (int i, int j) { x = i; y = j; }
416 class Bar : public Base1, public Foo {
419 Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; }
422 int Foo::operator! () { return !x; }
424 int Foo::times (int y) { return x * y; }
428 Foo::operator int() { return x; }
433 class ClassWithEnum {
435 enum PrivEnum { red, green, blue, yellow = 42 };
444 /* classes.exp relies on statement order in this function for testing
445 enumeration fields. */
449 ClassWithEnum obj_with_enum;
450 obj_with_enum.priv_enum = ClassWithEnum::red;
453 obj_with_enum.priv_enum = ClassWithEnum::green;
458 int Aptr_a (A *a) { return a->a; }
459 int Aptr_x (A *a) { return a->x; }
460 int Aref_a (A &a) { return a.a; }
461 int Aref_x (A &a) { return a.x; }
462 int Aval_a (A a) { return a.a; }
463 int Aval_x (A a) { return a.x; }
466 ClassParam class_param;
468 class Contains_static_instance
473 Contains_static_instance (int i, int j) { x = i; y = j; }
474 static Contains_static_instance null;
477 Contains_static_instance Contains_static_instance::null(0,0);
478 Contains_static_instance csi(10,20);
480 class Contains_nested_static_instance
486 Nested(int i) : z(i) {}
488 static Contains_nested_static_instance xx;
491 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
496 static Contains_nested_static_instance null;
500 Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
501 Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
502 Contains_nested_static_instance
503 Contains_nested_static_instance::Nested::xx(1,2);
504 Contains_nested_static_instance cnsi(30,40);
510 tagless_struct v_tagless;
512 /* Try to get the compiler to allocate a class in a register. */
525 void marker_reg1 () {}
530 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
531 might put this variable in a register. This is a lose, though, because
532 it means that GDB can't call any methods for that variable. */
537 /* Perform a computation sufficiently complicated that optimizing compilers
538 won't optimized out the variable. If some compiler constant-folds this
539 whole loop, maybe using a parameter to this function here would help. */
541 for (i = 0; i < 13; ++i)
543 --v.x; /* v.x is now 77 */
551 v_bool_array[0] = false;
552 v_bool_array[1] = v_bool;
557 /* Refer to methods so that they don't get optimized away. */
559 i = class_param.Aptr_a (&g_A);
560 i = class_param.Aptr_x (&g_A);
561 i = class_param.Aref_a (g_A);
562 i = class_param.Aref_x (g_A);
563 i = class_param.Aval_a (g_A);
564 i = class_param.Aval_x (g_A);
577 /* FIXME: pmi gets optimized out. Need to do some more computation with
578 it or something. (No one notices, because the test is xfail'd anyway,
579 but that probably won't always be true...). */
580 int Foo::* pmi = &Foo::y;
582 /* Make sure the AIX linker doesn't remove the variable. */
590 /* Create an instance for some classes, otherwise they get optimized away. */
592 default_public_struct default_public_s;
593 explicit_public_struct explicit_public_s;
594 protected_struct protected_s;
595 private_struct private_s;
596 mixed_protection_struct mixed_protection_s;
597 public_class public_c;
598 protected_class protected_c;
599 default_private_class default_private_c;
600 explicit_private_class explicit_private_c;
601 mixed_protection_class mixed_protection_c;