1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 1993-2024 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/>. */
18 // Test various -*- C++ -*- things.
20 // ====================== basic C++ types =======================
24 typedef struct fleep fleep
;
25 struct fleep
{ int a
; } s
;
27 // ====================== simple class structures =======================
29 struct default_public_struct
{
30 // defaults to public:
35 struct explicit_public_struct
{
41 struct protected_struct
{
47 struct private_struct
{
53 struct mixed_protection_struct
{
77 class protected_class
{
83 class default_private_class
{
84 // defaults to private:
89 class explicit_private_class
{
95 class mixed_protection_class
{
113 class const_vol_method_class
{
117 int foo (int &) const;
118 int bar (int &) volatile;
119 int baz (int &) const volatile;
122 int const_vol_method_class::foo (int & ir
) const
126 int const_vol_method_class::bar (int & ir
) volatile
130 int const_vol_method_class::baz (int & ir
) const volatile
135 // ========================= simple inheritance ==========================
161 class D
: public B
, public C
{
177 class class_with_anon_union
188 class_with_anon_union g_anon_union
;
190 void inheritance2 (void)
194 void inheritance1 (void)
204 // {{A::a,A::x},B::b,B::x}
211 // {{A::a,A::x},C::c,C::x}
218 // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}
220 // The following initialization code is non-portable, but allows us
221 // to initialize all members of g_D until we can fill in the missing
222 // initialization code with legal C++ code.
224 for (intp
= (int *) &g_D
, ival
= 11;
225 intp
< ((int *) &g_D
+ sizeof (g_D
) / sizeof (int));
231 // Overlay the nonportable initialization with legal initialization.
233 // ????? = 11; (g_D.A::a = 11; is ambiguous)
234 // ????? = 12; (g_D.A::x = 12; is ambiguous)
237 This should take care of it. Rather than try to initialize using an ambiguous
238 construct, use 2 unambiguous ones for each. Since the ambiguous a/x member is
239 coming from C, and B, initialize D's C::a, and B::a, and D's C::x and B::x.
255 // {{{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}},E::e,E::x}
257 // The following initialization code is non-portable, but allows us
258 // to initialize all members of g_D until we can fill in the missing
259 // initialization code with legal C++ code.
261 for (intp
= (int *) &g_E
, ival
= 21;
262 intp
< ((int *) &g_E
+ sizeof (g_E
) / sizeof (int));
268 // Overlay the nonportable initialization with legal initialization.
270 // ????? = 21; (g_E.A::a = 21; is ambiguous)
271 // ????? = 22; (g_E.A::x = 22; is ambiguous)
283 g_anon_union
.one
= 1;
289 // ======================== static member functions =====================
293 static void ii(int, int);
295 void Static::ii (int, int) { }
297 // ======================== virtual base classes=========================
307 class vB
: public virtual vA
{
315 class vC
: public virtual vA
{
323 class vD
: public virtual vB
, public virtual vC
{
331 class vE
: public virtual vD
{
339 void inheritance4 (void)
343 void inheritance3 (void)
353 // {{vA::va, vA::vx}, vB::vb, vB::vx}
360 // {{vA::va, vA::vx}, vC::vc, vC::vx}
367 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
379 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
395 // ======================================================================
400 Base1(int i
) { x
= i
; }
412 Foo (int i
, int j
) { x
= i
; y
= j
; }
418 typedef Foo ByAnyOtherName
;
420 class Bar
: public Base1
, public Foo
{
423 Bar (int i
, int j
, int k
) : Base1 (10*k
), Foo (i
, j
) { z
= k
; }
426 int Foo::operator! () { return !x
; }
428 int Foo::times (int y
) { return x
* y
; }
432 Foo::operator int() { return x
; }
434 ByAnyOtherName
foo(10, 11);
437 /* Use a typedef for the baseclass to exercise gnu-v3-abi.c:gnuv3_dynamic_class
438 recursion. It's important that the class itself have no name to make sure
439 the typedef makes it through to the recursive call. */
443 virtual int get_x () { return x
; }
446 class DynamicBar
: public DynamicBase2
449 DynamicBar (int i
, int j
) { x
= i
; y
= j
; }
453 DynamicBar
dynbar (23, 24);
455 class ClassWithEnum
{
457 enum PrivEnum
{ red
, green
, blue
, yellow
= 42 };
466 /* classes.exp relies on statement order in this function for testing
467 enumeration fields. */
471 ClassWithEnum obj_with_enum
;
472 obj_with_enum
.priv_enum
= ClassWithEnum::red
;
475 obj_with_enum
.priv_enum
= ClassWithEnum::green
;
481 int Aptr_a (A
*a
) { return a
->a
; }
482 int Aptr_x (A
*a
) { return a
->x
; }
483 int Aref_a (A
&a
) { return a
.a
; }
484 int Aref_x (A
&a
) { return a
.x
; }
485 int Aval_a (A a
) { return a
.a
; }
486 int Aval_x (A a
) { return a
.x
; }
489 ClassParam class_param
;
491 class Contains_static_instance
496 Contains_static_instance (int i
, int j
) { x
= i
; y
= j
; }
497 static Contains_static_instance null
;
500 Contains_static_instance
Contains_static_instance::null(0,0);
501 Contains_static_instance
csi(10,20);
503 class Contains_nested_static_instance
509 Nested(int i
) : z(i
) {}
511 static Contains_nested_static_instance xx
;
514 Contains_nested_static_instance(int i
, int j
) : x(i
), y(j
) {}
519 static Contains_nested_static_instance null
;
523 Contains_nested_static_instance
Contains_nested_static_instance::null(0, 0);
524 Contains_nested_static_instance::Nested
Contains_nested_static_instance::yy(5);
525 Contains_nested_static_instance
526 Contains_nested_static_instance::Nested::xx(1,2);
527 Contains_nested_static_instance
cnsi(30,40);
533 tagless_struct v_tagless
;
535 class class_with_typedefs
538 typedef int public_int
;
540 typedef int protected_int
;
542 typedef int private_int
;
545 class_with_typedefs ()
546 : public_int_ (1), protected_int_ (2), private_int_ (3) {}
547 public_int
add_public (public_int a
) { return a
+ public_int_
; }
548 public_int
add_all (int a
)
549 { return add_public (a
) + add_protected (a
) + add_private (a
); }
552 protected_int
add_protected (protected_int a
) { return a
+ protected_int_
; }
555 private_int
add_private (private_int a
) { return a
+ private_int_
; }
558 public_int public_int_
;
559 protected_int protected_int_
;
560 private_int private_int_
;
563 class class_with_public_typedef
571 class class_with_protected_typedef
579 class class_with_private_typedef
587 struct struct_with_public_typedef
595 struct struct_with_protected_typedef
603 struct struct_with_private_typedef
614 v_bool_array
[0] = false;
615 v_bool_array
[1] = v_bool
;
620 /* Refer to methods so that they don't get optimized away. */
622 i
= class_param
.Aptr_a (&g_A
);
623 i
= class_param
.Aptr_x (&g_A
);
624 i
= class_param
.Aref_a (g_A
);
625 i
= class_param
.Aref_x (g_A
);
626 i
= class_param
.Aval_a (g_A
);
627 i
= class_param
.Aval_x (g_A
);
634 static Inner instance
;
640 static Outer instance
;
643 Inner
Inner::instance
;
644 Outer
Outer::instance
;
649 int Foo::* pmi
= &Foo::y
;
656 /* Make sure the AIX linker doesn't remove the variable. */
664 /* Create an instance for some classes, otherwise they get optimized away. */
666 default_public_struct default_public_s
;
667 explicit_public_struct explicit_public_s
;
668 protected_struct protected_s
;
669 private_struct private_s
;
670 mixed_protection_struct mixed_protection_s
;
671 public_class public_c
;
672 protected_class protected_c
;
673 default_private_class default_private_c
;
674 explicit_private_class explicit_private_c
;
675 mixed_protection_class mixed_protection_c
;
676 class_with_typedefs class_with_typedefs_c
;
677 class_with_public_typedef class_with_public_typedef_c
;
678 class_with_protected_typedef class_with_protected_typedef_c
;
679 class_with_private_typedef class_with_private_typedef_c
;
680 struct_with_public_typedef struct_with_public_typedef_s
;
681 struct_with_protected_typedef struct_with_protected_typedef_s
;
682 struct_with_private_typedef struct_with_private_typedef_s
;