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