* gdbtypes.c (is_ancestor): Infer type equivalence from name
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.c++ / misc.cc
CommitLineData
c906108c
SS
1// Test various -*- C++ -*- things.
2
a0b3c4fd
JM
3// ====================== basic C++ types =======================
4bool v_bool;
5bool v_bool_array[2];
6
c906108c
SS
7typedef struct fleep fleep;
8struct fleep { int a; } s;
9
10// ====================== simple class structures =======================
11
12struct default_public_struct {
13 // defaults to public:
14 int a;
15 int b;
16};
17
18struct explicit_public_struct {
19 public:
20 int a;
21 int b;
22};
23
24struct protected_struct {
25 protected:
26 int a;
27 int b;
28};
29
30struct private_struct {
31 private:
32 int a;
33 int b;
34};
35
36struct mixed_protection_struct {
37 public:
38 int a;
39 int b;
40 private:
41 int c;
42 int d;
43 protected:
44 int e;
45 int f;
46 public:
47 int g;
48 private:
49 int h;
50 protected:
51 int i;
52};
53
54class public_class {
55 public:
56 int a;
57 int b;
58};
59
60class protected_class {
61 protected:
62 int a;
63 int b;
64};
65
66class default_private_class {
67 // defaults to private:
68 int a;
69 int b;
70};
71
72class explicit_private_class {
73 private:
74 int a;
75 int b;
76};
77
78class mixed_protection_class {
79 public:
80 int a;
81 int b;
82 private:
83 int c;
84 int d;
85 protected:
86 int e;
87 int f;
88 public:
89 int g;
90 private:
91 int h;
92 protected:
93 int i;
94};
95
96class const_vol_method_class {
97public:
98 int a;
99 int b;
100 int foo (int &) const;
101 int bar (int &) volatile;
102 int baz (int &) const volatile;
103};
104
105int const_vol_method_class::foo (int & ir) const
106{
107 return ir + 3;
108}
109int const_vol_method_class::bar (int & ir) volatile
110{
111 return ir + 4;
112}
113int const_vol_method_class::baz (int & ir) const volatile
114{
115 return ir + 5;
116}
117
118// ========================= simple inheritance ==========================
119
120class A {
121 public:
122 int a;
123 int x;
124};
125
126A g_A;
127
128class B : public A {
129 public:
130 int b;
131 int x;
132};
133
134B g_B;
135
136class C : public A {
137 public:
138 int c;
139 int x;
140};
141
142C g_C;
143
144class D : public B, public C {
145 public:
146 int d;
147 int x;
148};
149
150D g_D;
151
152class E : public D {
153 public:
154 int e;
155 int x;
156};
157
158E g_E;
159
160class class_with_anon_union
161{
162 public:
163 int one;
164 union
165 {
166 int a;
167 long b;
168 };
169};
170
171class_with_anon_union g_anon_union;
172
173void inheritance2 (void)
174{
175}
176
177void inheritance1 (void)
178{
179 int ival;
180 int *intp;
181
182 // {A::a, A::x}
183
184 g_A.A::a = 1;
185 g_A.A::x = 2;
186
187 // {{A::a,A::x},B::b,B::x}
188
189 g_B.A::a = 3;
190 g_B.A::x = 4;
191 g_B.B::b = 5;
192 g_B.B::x = 6;
193
194 // {{A::a,A::x},C::c,C::x}
195
196 g_C.A::a = 7;
197 g_C.A::x = 8;
198 g_C.C::c = 9;
199 g_C.C::x = 10;
200
201 // {{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}
202
203 // The following initialization code is non-portable, but allows us
204 // to initialize all members of g_D until we can fill in the missing
205 // initialization code with legal C++ code.
206
207 for (intp = (int *) &g_D, ival = 11;
208 intp < ((int *) &g_D + sizeof (g_D) / sizeof (int));
209 intp++, ival++)
210 {
211 *intp = ival;
212 }
213
214 // Overlay the nonportable initialization with legal initialization.
215
216 // ????? = 11; (g_D.A::a = 11; is ambiguous)
217 // ????? = 12; (g_D.A::x = 12; is ambiguous)
e6d71bf3
DB
218/* djb 6-3-2000
219
220 This should take care of it. Rather than try to initialize using an ambiguous
221 construct, use 2 unambiguous ones for each. Since the ambiguous a/x member is
222 coming from C, and B, initialize D's C::a, and B::a, and D's C::x and B::x.
223 */
224 g_D.C::a = 15;
225 g_D.C::x = 12;
226 g_D.B::a = 11;
227 g_D.B::x = 12;
c906108c
SS
228 g_D.B::b = 13;
229 g_D.B::x = 14;
230 // ????? = 15;
231 // ????? = 16;
232 g_D.C::c = 17;
233 g_D.C::x = 18;
234 g_D.D::d = 19;
235 g_D.D::x = 20;
236
237
238 // {{{{A::a,A::x},B::b,B::x},{{A::a,A::x},C::c,C::x},D::d,D::x}},E::e,E::x}
239
240 // The following initialization code is non-portable, but allows us
241 // to initialize all members of g_D until we can fill in the missing
242 // initialization code with legal C++ code.
243
244 for (intp = (int *) &g_E, ival = 21;
245 intp < ((int *) &g_E + sizeof (g_E) / sizeof (int));
246 intp++, ival++)
247 {
248 *intp = ival;
249 }
250
251 // Overlay the nonportable initialization with legal initialization.
252
253 // ????? = 21; (g_E.A::a = 21; is ambiguous)
254 // ????? = 22; (g_E.A::x = 22; is ambiguous)
255 g_E.B::b = 23;
256 g_E.B::x = 24;
257 // ????? = 25;
258 // ????? = 26;
259 g_E.C::c = 27;
260 g_E.C::x = 28;
261 g_E.D::d = 29;
262 g_E.D::x = 30;
263 g_E.E::e = 31;
264 g_E.E::x = 32;
265
266 g_anon_union.one = 1;
267 g_anon_union.a = 2;
268
269 inheritance2 ();
270}
271
272// ======================== virtual base classes=========================
273
274class vA {
275 public:
276 int va;
277 int vx;
278};
279
280vA g_vA;
281
282class vB : public virtual vA {
283 public:
284 int vb;
285 int vx;
286};
287
288vB g_vB;
289
290class vC : public virtual vA {
291 public:
292 int vc;
293 int vx;
294};
295
296vC g_vC;
297
298class vD : public virtual vB, public virtual vC {
299 public:
300 int vd;
301 int vx;
302};
303
304vD g_vD;
305
306class vE : public virtual vD {
307 public:
308 int ve;
309 int vx;
310};
311
312vE g_vE;
313
314void inheritance4 (void)
315{
316}
317
318void inheritance3 (void)
319{
320 int ival;
321 int *intp;
322
323 // {vA::va, vA::vx}
324
325 g_vA.vA::va = 1;
326 g_vA.vA::vx = 2;
327
328 // {{vA::va, vA::vx}, vB::vb, vB::vx}
329
330 g_vB.vA::va = 3;
331 g_vB.vA::vx = 4;
332 g_vB.vB::vb = 5;
333 g_vB.vB::vx = 6;
334
335 // {{vA::va, vA::vx}, vC::vc, vC::vx}
336
337 g_vC.vA::va = 7;
338 g_vC.vA::vx = 8;
339 g_vC.vC::vc = 9;
340 g_vC.vC::vx = 10;
341
342 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
343
344 g_vD.vA::va = 11;
345 g_vD.vA::vx = 12;
346 g_vD.vB::vb = 13;
347 g_vD.vB::vx = 14;
348 g_vD.vC::vc = 15;
349 g_vD.vC::vx = 16;
350 g_vD.vD::vd = 17;
351 g_vD.vD::vx = 18;
352
353
354 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
355
356 g_vD.vA::va = 19;
357 g_vD.vA::vx = 20;
358 g_vD.vB::vb = 21;
359 g_vD.vB::vx = 22;
360 g_vD.vC::vc = 23;
361 g_vD.vC::vx = 24;
362 g_vD.vD::vd = 25;
363 g_vD.vD::vx = 26;
364 g_vE.vE::ve = 27;
365 g_vE.vE::vx = 28;
366
367 inheritance4 ();
368}
369
370// ======================================================================
371
372class Base1 {
373 public:
374 int x;
375 Base1(int i) { x = i; }
376};
377
378class Foo
379{
380 public:
381 int x;
382 int y;
383 static int st;
384 Foo (int i, int j) { x = i; y = j; }
385 int operator! ();
386 operator int ();
387 int times (int y);
388};
389
390class Bar : public Base1, public Foo {
391 public:
392 int z;
393 Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; }
394};
395
c906108c
SS
396int Foo::operator! () { return !x; }
397
398int Foo::times (int y) { return x * y; }
399
400int Foo::st = 100;
401
402Foo::operator int() { return x; }
403
404Foo foo(10, 11);
405Bar bar(20, 21, 22);
406
2bec0572
ND
407class ClassWithEnum {
408public:
409 enum PrivEnum { red, green, blue, yellow = 42 };
410 PrivEnum priv_enum;
411 int x;
412};
413
414void enums2 (void)
415{
416}
417
418/* classes.exp relies on statement order in this function for testing
419 enumeration fields. */
420
421void enums1 ()
422{
423 ClassWithEnum obj_with_enum;
424 obj_with_enum.priv_enum = ClassWithEnum::red;
425 obj_with_enum.x = 0;
426 enums2 ();
427 obj_with_enum.priv_enum = ClassWithEnum::green;
428}
429
c906108c
SS
430class Contains_static_instance
431{
432 public:
433 int x;
434 int y;
435 Contains_static_instance (int i, int j) { x = i; y = j; }
436 static Contains_static_instance null;
437};
438
439Contains_static_instance Contains_static_instance::null(0,0);
440Contains_static_instance csi(10,20);
441
442class Contains_nested_static_instance
443{
444 public:
445 class Nested
446 {
447 public:
448 Nested(int i) : z(i) {}
449 int z;
450 static Contains_nested_static_instance xx;
451 };
452
453 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
454
455 int x;
456 int y;
457
458 static Contains_nested_static_instance null;
459 static Nested yy;
460};
461
462Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
463Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
464Contains_nested_static_instance
465 Contains_nested_static_instance::Nested::xx(1,2);
466Contains_nested_static_instance cnsi(30,40);
467
468typedef struct {
469 int one;
470 int two;
471} tagless_struct;
472tagless_struct v_tagless;
473
474/* Try to get the compiler to allocate a class in a register. */
475class small {
476 public:
477 int x;
478 int method ();
479};
480
481int
482small::method ()
483{
484 return x + 5;
485}
486
487void marker_reg1 () {}
488
489int
490register_class ()
491{
492 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
493 might put this variable in a register. This is a lose, though, because
494 it means that GDB can't call any methods for that variable. */
495 register small v;
496
497 int i;
498
499 /* Perform a computation sufficiently complicated that optimizing compilers
500 won't optimized out the variable. If some compiler constant-folds this
501 whole loop, maybe using a parameter to this function here would help. */
502 v.x = 0;
503 for (i = 0; i < 13; ++i)
504 v.x += i;
505 --v.x; /* v.x is now 77 */
506 marker_reg1 ();
507 return v.x + 5;
508}
509
a0b3c4fd
JM
510void dummy()
511{
512 v_bool = true;
513 v_bool_array[0] = false;
514 v_bool_array[1] = v_bool;
515}
516
517
c906108c
SS
518int
519main()
520{
521#ifdef usestubs
522 set_debug_traps();
523 breakpoint();
524#endif
a0b3c4fd 525 dummy();
c906108c
SS
526 inheritance1 ();
527 inheritance3 ();
2bec0572 528 enums1 ();
c906108c
SS
529 register_class ();
530
531 /* FIXME: pmi gets optimized out. Need to do some more computation with
532 it or something. (No one notices, because the test is xfail'd anyway,
533 but that probably won't always be true...). */
534 int Foo::* pmi = &Foo::y;
535
536 /* Make sure the AIX linker doesn't remove the variable. */
537 v_tagless.one = 5;
538
c906108c
SS
539 return foo.*pmi;
540}
541
542/* Create an instance for some classes, otherwise they get optimized away. */
543
544default_public_struct default_public_s;
545explicit_public_struct explicit_public_s;
546protected_struct protected_s;
547private_struct private_s;
548mixed_protection_struct mixed_protection_s;
549public_class public_c;
550protected_class protected_c;
551default_private_class default_private_c;
552explicit_private_class explicit_private_c;
553mixed_protection_class mixed_protection_c;
This page took 0.088708 seconds and 4 git commands to generate.