]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.cp/misc.cc
2003-08-22 Michael Chastain <mec@shout.net>
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / 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 /* 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;
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 // ======================== static member functions =====================
273
274 class Static {
275 public:
276 static void ii(int, int);
277 };
278 void Static::ii (int, int) { }
279
280 // ======================== virtual base classes=========================
281
282 class vA {
283 public:
284 int va;
285 int vx;
286 };
287
288 vA g_vA;
289
290 class vB : public virtual vA {
291 public:
292 int vb;
293 int vx;
294 };
295
296 vB g_vB;
297
298 class vC : public virtual vA {
299 public:
300 int vc;
301 int vx;
302 };
303
304 vC g_vC;
305
306 class vD : public virtual vB, public virtual vC {
307 public:
308 int vd;
309 int vx;
310 };
311
312 vD g_vD;
313
314 class vE : public virtual vD {
315 public:
316 int ve;
317 int vx;
318 };
319
320 vE g_vE;
321
322 void inheritance4 (void)
323 {
324 }
325
326 void inheritance3 (void)
327 {
328 int ival;
329 int *intp;
330
331 // {vA::va, vA::vx}
332
333 g_vA.vA::va = 1;
334 g_vA.vA::vx = 2;
335
336 // {{vA::va, vA::vx}, vB::vb, vB::vx}
337
338 g_vB.vA::va = 3;
339 g_vB.vA::vx = 4;
340 g_vB.vB::vb = 5;
341 g_vB.vB::vx = 6;
342
343 // {{vA::va, vA::vx}, vC::vc, vC::vx}
344
345 g_vC.vA::va = 7;
346 g_vC.vA::vx = 8;
347 g_vC.vC::vc = 9;
348 g_vC.vC::vx = 10;
349
350 // {{{{vA::va, vA::vx}, vB::vb, vB::vx}, vC::vc, vC::vx}, vD::vd,vD::vx}
351
352 g_vD.vA::va = 11;
353 g_vD.vA::vx = 12;
354 g_vD.vB::vb = 13;
355 g_vD.vB::vx = 14;
356 g_vD.vC::vc = 15;
357 g_vD.vC::vx = 16;
358 g_vD.vD::vd = 17;
359 g_vD.vD::vx = 18;
360
361
362 // {{{{{vA::va,vA::vx},vB::vb,vB::vx},vC::vc,vC::vx},vD::vd,vD::vx},vE::ve,vE::vx}
363
364 g_vD.vA::va = 19;
365 g_vD.vA::vx = 20;
366 g_vD.vB::vb = 21;
367 g_vD.vB::vx = 22;
368 g_vD.vC::vc = 23;
369 g_vD.vC::vx = 24;
370 g_vD.vD::vd = 25;
371 g_vD.vD::vx = 26;
372 g_vE.vE::ve = 27;
373 g_vE.vE::vx = 28;
374
375 inheritance4 ();
376 }
377
378 // ======================================================================
379
380 class Base1 {
381 public:
382 int x;
383 Base1(int i) { x = i; }
384 };
385
386 class Foo
387 {
388 public:
389 int x;
390 int y;
391 static int st;
392 Foo (int i, int j) { x = i; y = j; }
393 int operator! ();
394 operator int ();
395 int times (int y);
396 };
397
398 class Bar : public Base1, public Foo {
399 public:
400 int z;
401 Bar (int i, int j, int k) : Base1 (10*k), Foo (i, j) { z = k; }
402 };
403
404 int Foo::operator! () { return !x; }
405
406 int Foo::times (int y) { return x * y; }
407
408 int Foo::st = 100;
409
410 Foo::operator int() { return x; }
411
412 Foo foo(10, 11);
413 Bar bar(20, 21, 22);
414
415 class ClassWithEnum {
416 public:
417 enum PrivEnum { red, green, blue, yellow = 42 };
418 PrivEnum priv_enum;
419 int x;
420 };
421
422 void enums2 (void)
423 {
424 }
425
426 /* classes.exp relies on statement order in this function for testing
427 enumeration fields. */
428
429 void enums1 ()
430 {
431 ClassWithEnum obj_with_enum;
432 obj_with_enum.priv_enum = ClassWithEnum::red;
433 obj_with_enum.x = 0;
434 enums2 ();
435 obj_with_enum.priv_enum = ClassWithEnum::green;
436 }
437
438 class ClassParam {
439 public:
440 int Aptr_a (A *a) { return a->a; }
441 int Aptr_x (A *a) { return a->x; }
442 int Aref_a (A &a) { return a.a; }
443 int Aref_x (A &a) { return a.x; }
444 int Aval_a (A a) { return a.a; }
445 int Aval_x (A a) { return a.x; }
446 };
447
448 ClassParam class_param;
449
450 class Contains_static_instance
451 {
452 public:
453 int x;
454 int y;
455 Contains_static_instance (int i, int j) { x = i; y = j; }
456 static Contains_static_instance null;
457 };
458
459 Contains_static_instance Contains_static_instance::null(0,0);
460 Contains_static_instance csi(10,20);
461
462 class Contains_nested_static_instance
463 {
464 public:
465 class Nested
466 {
467 public:
468 Nested(int i) : z(i) {}
469 int z;
470 static Contains_nested_static_instance xx;
471 };
472
473 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
474
475 int x;
476 int y;
477
478 static Contains_nested_static_instance null;
479 static Nested yy;
480 };
481
482 Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
483 Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
484 Contains_nested_static_instance
485 Contains_nested_static_instance::Nested::xx(1,2);
486 Contains_nested_static_instance cnsi(30,40);
487
488 typedef struct {
489 int one;
490 int two;
491 } tagless_struct;
492 tagless_struct v_tagless;
493
494 /* Try to get the compiler to allocate a class in a register. */
495 class small {
496 public:
497 int x;
498 int method ();
499 };
500
501 int
502 small::method ()
503 {
504 return x + 5;
505 }
506
507 void marker_reg1 () {}
508
509 int
510 register_class ()
511 {
512 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
513 might put this variable in a register. This is a lose, though, because
514 it means that GDB can't call any methods for that variable. */
515 register small v;
516
517 int i;
518
519 /* Perform a computation sufficiently complicated that optimizing compilers
520 won't optimized out the variable. If some compiler constant-folds this
521 whole loop, maybe using a parameter to this function here would help. */
522 v.x = 0;
523 for (i = 0; i < 13; ++i)
524 v.x += i;
525 --v.x; /* v.x is now 77 */
526 marker_reg1 ();
527 return v.x + 5;
528 }
529
530 void dummy()
531 {
532 v_bool = true;
533 v_bool_array[0] = false;
534 v_bool_array[1] = v_bool;
535 }
536
537 void use_methods ()
538 {
539 /* Refer to methods so that they don't get optimized away. */
540 int i;
541 i = class_param.Aptr_a (&g_A);
542 i = class_param.Aptr_x (&g_A);
543 i = class_param.Aref_a (g_A);
544 i = class_param.Aref_x (g_A);
545 i = class_param.Aval_a (g_A);
546 i = class_param.Aval_x (g_A);
547 }
548
549
550 int
551 main()
552 {
553 #ifdef usestubs
554 set_debug_traps();
555 breakpoint();
556 #endif
557 dummy();
558 inheritance1 ();
559 inheritance3 ();
560 enums1 ();
561 register_class ();
562
563 /* FIXME: pmi gets optimized out. Need to do some more computation with
564 it or something. (No one notices, because the test is xfail'd anyway,
565 but that probably won't always be true...). */
566 int Foo::* pmi = &Foo::y;
567
568 /* Make sure the AIX linker doesn't remove the variable. */
569 v_tagless.one = 5;
570
571 use_methods ();
572
573 return foo.*pmi;
574 }
575
576 /* Create an instance for some classes, otherwise they get optimized away. */
577
578 default_public_struct default_public_s;
579 explicit_public_struct explicit_public_s;
580 protected_struct protected_s;
581 private_struct private_s;
582 mixed_protection_struct mixed_protection_s;
583 public_class public_c;
584 protected_class protected_c;
585 default_private_class default_private_c;
586 explicit_private_class explicit_private_c;
587 mixed_protection_class mixed_protection_c;