1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2004, 2007,
4 2008, 2009, 2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>.
20 // Test various -*- C++ -*- things.
22 // ====================== basic C++ types =======================
26 typedef struct fleep fleep
;
27 struct fleep
{ int a
; } s
;
29 // ====================== simple class structures =======================
31 struct default_public_struct
{
32 // defaults to public:
37 struct explicit_public_struct
{
43 struct protected_struct
{
49 struct private_struct
{
55 struct mixed_protection_struct
{
79 class protected_class
{
85 class default_private_class
{
86 // defaults to private:
91 class explicit_private_class
{
97 class mixed_protection_class
{
115 class const_vol_method_class
{
119 int foo (int &) const;
120 int bar (int &) volatile;
121 int baz (int &) const volatile;
124 int const_vol_method_class::foo (int & ir
) const
128 int const_vol_method_class::bar (int & ir
) volatile
132 int const_vol_method_class::baz (int & ir
) const volatile
137 // ========================= simple inheritance ==========================
163 class D
: public B
, public C
{
179 class class_with_anon_union
190 class_with_anon_union g_anon_union
;
192 void inheritance2 (void)
196 void inheritance1 (void)
206 // {{A::a,A::x},B::b,B::x}
213 // {{A::a,A::x},C::c,C::x}
220 // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}
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.
226 for (intp
= (int *) &g_D
, ival
= 11;
227 intp
< ((int *) &g_D
+ sizeof (g_D
) / sizeof (int));
233 // Overlay the nonportable initialization with legal initialization.
235 // ????? = 11; (g_D.A::a = 11; is ambiguous)
236 // ????? = 12; (g_D.A::x = 12; is ambiguous)
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.
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}
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.
263 for (intp
= (int *) &g_E
, ival
= 21;
264 intp
< ((int *) &g_E
+ sizeof (g_E
) / sizeof (int));
270 // Overlay the nonportable initialization with legal initialization.
272 // ????? = 21; (g_E.A::a = 21; is ambiguous)
273 // ????? = 22; (g_E.A::x = 22; is ambiguous)
285 g_anon_union
.one
= 1;
291 // ======================== static member functions =====================
295 static void ii(int, int);
297 void Static::ii (int, int) { }
299 // ======================== virtual base classes=========================
309 class vB
: public virtual vA
{
317 class vC
: public virtual vA
{
325 class vD
: public virtual vB
, public virtual vC
{
333 class vE
: public virtual vD
{
341 void inheritance4 (void)
345 void inheritance3 (void)
355 // {{vA::va, vA::vx}, vB::vb, vB::vx}
362 // {{vA::va, vA::vx}, vC::vc, vC::vx}
369 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
381 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
397 // ======================================================================
402 Base1(int i
) { x
= i
; }
414 Foo (int i
, int j
) { x
= i
; y
= j
; }
420 typedef Foo ByAnyOtherName
;
422 class Bar
: public Base1
, public Foo
{
425 Bar (int i
, int j
, int k
) : Base1 (10*k
), Foo (i
, j
) { z
= k
; }
428 int Foo::operator! () { return !x
; }
430 int Foo::times (int y
) { return x
* y
; }
434 Foo::operator int() { return x
; }
436 ByAnyOtherName
foo(10, 11);
439 class ClassWithEnum
{
441 enum PrivEnum
{ red
, green
, blue
, yellow
= 42 };
450 /* classes.exp relies on statement order in this function for testing
451 enumeration fields. */
455 ClassWithEnum obj_with_enum
;
456 obj_with_enum
.priv_enum
= ClassWithEnum::red
;
459 obj_with_enum
.priv_enum
= ClassWithEnum::green
;
465 int Aptr_a (A
*a
) { return a
->a
; }
466 int Aptr_x (A
*a
) { return a
->x
; }
467 int Aref_a (A
&a
) { return a
.a
; }
468 int Aref_x (A
&a
) { return a
.x
; }
469 int Aval_a (A a
) { return a
.a
; }
470 int Aval_x (A a
) { return a
.x
; }
473 ClassParam class_param
;
475 class Contains_static_instance
480 Contains_static_instance (int i
, int j
) { x
= i
; y
= j
; }
481 static Contains_static_instance null
;
484 Contains_static_instance
Contains_static_instance::null(0,0);
485 Contains_static_instance
csi(10,20);
487 class Contains_nested_static_instance
493 Nested(int i
) : z(i
) {}
495 static Contains_nested_static_instance xx
;
498 Contains_nested_static_instance(int i
, int j
) : x(i
), y(j
) {}
503 static Contains_nested_static_instance null
;
507 Contains_nested_static_instance
Contains_nested_static_instance::null(0, 0);
508 Contains_nested_static_instance::Nested
Contains_nested_static_instance::yy(5);
509 Contains_nested_static_instance
510 Contains_nested_static_instance::Nested::xx(1,2);
511 Contains_nested_static_instance
cnsi(30,40);
517 tagless_struct v_tagless
;
519 /* Try to get the compiler to allocate a class in a register. */
532 void marker_reg1 () {}
537 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
538 might put this variable in a register. This is a lose, though, because
539 it means that GDB can't call any methods for that variable. */
544 /* Perform a computation sufficiently complicated that optimizing compilers
545 won't optimized out the variable. If some compiler constant-folds this
546 whole loop, maybe using a parameter to this function here would help. */
548 for (i
= 0; i
< 13; ++i
)
550 --v
.x
; /* v.x is now 77 */
558 v_bool_array
[0] = false;
559 v_bool_array
[1] = v_bool
;
564 /* Refer to methods so that they don't get optimized away. */
566 i
= class_param
.Aptr_a (&g_A
);
567 i
= class_param
.Aptr_x (&g_A
);
568 i
= class_param
.Aref_a (g_A
);
569 i
= class_param
.Aref_x (g_A
);
570 i
= class_param
.Aval_a (g_A
);
571 i
= class_param
.Aval_x (g_A
);
586 /* FIXME: pmi gets optimized out. Need to do some more computation with
587 it or something. (No one notices, because the test is xfail'd anyway,
588 but that probably won't always be true...). */
589 int Foo::* pmi
= &Foo::y
;
591 /* Make sure the AIX linker doesn't remove the variable. */
599 /* Create an instance for some classes, otherwise they get optimized away. */
601 default_public_struct default_public_s
;
602 explicit_public_struct explicit_public_s
;
603 protected_struct protected_s
;
604 private_struct private_s
;
605 mixed_protection_struct mixed_protection_s
;
606 public_class public_c
;
607 protected_class protected_c
;
608 default_private_class default_private_c
;
609 explicit_private_class explicit_private_c
;
610 mixed_protection_class mixed_protection_c
;