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