* coffread.c: Replace all occurrences of bfd_read with bfd_bread.
[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 /* 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 // ======================== virtual base classes=========================
273
274 class vA {
275 public:
276 int va;
277 int vx;
278 };
279
280 vA g_vA;
281
282 class vB : public virtual vA {
283 public:
284 int vb;
285 int vx;
286 };
287
288 vB g_vB;
289
290 class vC : public virtual vA {
291 public:
292 int vc;
293 int vx;
294 };
295
296 vC g_vC;
297
298 class vD : public virtual vB, public virtual vC {
299 public:
300 int vd;
301 int vx;
302 };
303
304 vD g_vD;
305
306 class vE : public virtual vD {
307 public:
308 int ve;
309 int vx;
310 };
311
312 vE g_vE;
313
314 void inheritance4 (void)
315 {
316 }
317
318 void 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
372 class Base1 {
373 public:
374 int x;
375 Base1(int i) { x = i; }
376 };
377
378 class 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
390 class 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
396 int Foo::operator! () { return !x; }
397
398 int Foo::times (int y) { return x * y; }
399
400 int Foo::st = 100;
401
402 Foo::operator int() { return x; }
403
404 Foo foo(10, 11);
405 Bar bar(20, 21, 22);
406
407 class ClassWithEnum {
408 public:
409 enum PrivEnum { red, green, blue, yellow = 42 };
410 PrivEnum priv_enum;
411 int x;
412 };
413
414 void enums2 (void)
415 {
416 }
417
418 /* classes.exp relies on statement order in this function for testing
419 enumeration fields. */
420
421 void 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
430 class ClassParam {
431 public:
432 int Aptr_a (A *a) { return a->a; }
433 int Aptr_x (A *a) { return a->x; }
434 int Aref_a (A &a) { return a.a; }
435 int Aref_x (A &a) { return a.x; }
436 int Aval_a (A a) { return a.a; }
437 int Aval_x (A a) { return a.x; }
438 };
439
440 ClassParam class_param;
441
442 class Contains_static_instance
443 {
444 public:
445 int x;
446 int y;
447 Contains_static_instance (int i, int j) { x = i; y = j; }
448 static Contains_static_instance null;
449 };
450
451 Contains_static_instance Contains_static_instance::null(0,0);
452 Contains_static_instance csi(10,20);
453
454 class Contains_nested_static_instance
455 {
456 public:
457 class Nested
458 {
459 public:
460 Nested(int i) : z(i) {}
461 int z;
462 static Contains_nested_static_instance xx;
463 };
464
465 Contains_nested_static_instance(int i, int j) : x(i), y(j) {}
466
467 int x;
468 int y;
469
470 static Contains_nested_static_instance null;
471 static Nested yy;
472 };
473
474 Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
475 Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
476 Contains_nested_static_instance
477 Contains_nested_static_instance::Nested::xx(1,2);
478 Contains_nested_static_instance cnsi(30,40);
479
480 typedef struct {
481 int one;
482 int two;
483 } tagless_struct;
484 tagless_struct v_tagless;
485
486 /* Try to get the compiler to allocate a class in a register. */
487 class small {
488 public:
489 int x;
490 int method ();
491 };
492
493 int
494 small::method ()
495 {
496 return x + 5;
497 }
498
499 void marker_reg1 () {}
500
501 int
502 register_class ()
503 {
504 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
505 might put this variable in a register. This is a lose, though, because
506 it means that GDB can't call any methods for that variable. */
507 register small v;
508
509 int i;
510
511 /* Perform a computation sufficiently complicated that optimizing compilers
512 won't optimized out the variable. If some compiler constant-folds this
513 whole loop, maybe using a parameter to this function here would help. */
514 v.x = 0;
515 for (i = 0; i < 13; ++i)
516 v.x += i;
517 --v.x; /* v.x is now 77 */
518 marker_reg1 ();
519 return v.x + 5;
520 }
521
522 void dummy()
523 {
524 v_bool = true;
525 v_bool_array[0] = false;
526 v_bool_array[1] = v_bool;
527 }
528
529 void use_methods ()
530 {
531 /* Refer to methods so that they don't get optimized away. */
532 int i;
533 i = class_param.Aptr_a (&g_A);
534 i = class_param.Aptr_x (&g_A);
535 i = class_param.Aref_a (g_A);
536 i = class_param.Aref_x (g_A);
537 i = class_param.Aval_a (g_A);
538 i = class_param.Aval_x (g_A);
539 }
540
541
542 int
543 main()
544 {
545 #ifdef usestubs
546 set_debug_traps();
547 breakpoint();
548 #endif
549 dummy();
550 inheritance1 ();
551 inheritance3 ();
552 enums1 ();
553 register_class ();
554
555 /* FIXME: pmi gets optimized out. Need to do some more computation with
556 it or something. (No one notices, because the test is xfail'd anyway,
557 but that probably won't always be true...). */
558 int Foo::* pmi = &Foo::y;
559
560 /* Make sure the AIX linker doesn't remove the variable. */
561 v_tagless.one = 5;
562
563 use_methods ();
564
565 return foo.*pmi;
566 }
567
568 /* Create an instance for some classes, otherwise they get optimized away. */
569
570 default_public_struct default_public_s;
571 explicit_public_struct explicit_public_s;
572 protected_struct protected_s;
573 private_struct private_s;
574 mixed_protection_struct mixed_protection_s;
575 public_class public_c;
576 protected_class protected_c;
577 default_private_class default_private_c;
578 explicit_private_class explicit_private_c;
579 mixed_protection_class mixed_protection_c;