]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.c++/misc.cc
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.c++ / misc.cc
CommitLineData
c906108c
SS
1// Test various -*- C++ -*- things.
2
3typedef struct fleep fleep;
4struct fleep { int a; } s;
5
6// ====================== simple class structures =======================
7
8struct default_public_struct {
9 // defaults to public:
10 int a;
11 int b;
12};
13
14struct explicit_public_struct {
15 public:
16 int a;
17 int b;
18};
19
20struct protected_struct {
21 protected:
22 int a;
23 int b;
24};
25
26struct private_struct {
27 private:
28 int a;
29 int b;
30};
31
32struct 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
50class public_class {
51 public:
52 int a;
53 int b;
54};
55
56class protected_class {
57 protected:
58 int a;
59 int b;
60};
61
62class default_private_class {
63 // defaults to private:
64 int a;
65 int b;
66};
67
68class explicit_private_class {
69 private:
70 int a;
71 int b;
72};
73
74class 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
92class const_vol_method_class {
93public:
94 int a;
95 int b;
96 int foo (int &) const;
97 int bar (int &) volatile;
98 int baz (int &) const volatile;
99};
100
101int const_vol_method_class::foo (int & ir) const
102{
103 return ir + 3;
104}
105int const_vol_method_class::bar (int & ir) volatile
106{
107 return ir + 4;
108}
109int const_vol_method_class::baz (int & ir) const volatile
110{
111 return ir + 5;
112}
113
114// ========================= simple inheritance ==========================
115
116class A {
117 public:
118 int a;
119 int x;
120};
121
122A g_A;
123
124class B : public A {
125 public:
126 int b;
127 int x;
128};
129
130B g_B;
131
132class C : public A {
133 public:
134 int c;
135 int x;
136};
137
138C g_C;
139
140class D : public B, public C {
141 public:
142 int d;
143 int x;
144};
145
146D g_D;
147
148class E : public D {
149 public:
150 int e;
151 int x;
152};
153
154E g_E;
155
156class class_with_anon_union
157{
158 public:
159 int one;
160 union
161 {
162 int a;
163 long b;
164 };
165};
166
167class_with_anon_union g_anon_union;
168
169void inheritance2 (void)
170{
171}
172
173void 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
260class vA {
261 public:
262 int va;
263 int vx;
264};
265
266vA g_vA;
267
268class vB : public virtual vA {
269 public:
270 int vb;
271 int vx;
272};
273
274vB g_vB;
275
276class vC : public virtual vA {
277 public:
278 int vc;
279 int vx;
280};
281
282vC g_vC;
283
284class vD : public virtual vB, public virtual vC {
285 public:
286 int vd;
287 int vx;
288};
289
290vD g_vD;
291
292class vE : public virtual vD {
293 public:
294 int ve;
295 int vx;
296};
297
298vE g_vE;
299
300void inheritance4 (void)
301{
302}
303
304void 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
358class Base1 {
359 public:
360 int x;
361 Base1(int i) { x = i; }
362};
363
364class 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
376class 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
382class ClassWithEnum {
383public:
384 enum PrivEnum { red, green, blue, yellow = 42 };
385 PrivEnum priv_enum;
386 int x;
387};
388
389int Foo::operator! () { return !x; }
390
391int Foo::times (int y) { return x * y; }
392
393int Foo::st = 100;
394
395Foo::operator int() { return x; }
396
397Foo foo(10, 11);
398Bar bar(20, 21, 22);
399
400class 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
409Contains_static_instance Contains_static_instance::null(0,0);
410Contains_static_instance csi(10,20);
411
412class 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
432Contains_nested_static_instance Contains_nested_static_instance::null(0, 0);
433Contains_nested_static_instance::Nested Contains_nested_static_instance::yy(5);
434Contains_nested_static_instance
435 Contains_nested_static_instance::Nested::xx(1,2);
436Contains_nested_static_instance cnsi(30,40);
437
438typedef struct {
439 int one;
440 int two;
441} tagless_struct;
442tagless_struct v_tagless;
443
444/* Try to get the compiler to allocate a class in a register. */
445class small {
446 public:
447 int x;
448 int method ();
449};
450
451int
452small::method ()
453{
454 return x + 5;
455}
456
457void marker_reg1 () {}
458
459int
460register_class ()
461{
462 /* We don't call any methods for v, so gcc version cygnus-2.3.3-930220
463 might put this variable in a register. This is a lose, though, because
464 it means that GDB can't call any methods for that variable. */
465 register small v;
466
467 int i;
468
469 /* Perform a computation sufficiently complicated that optimizing compilers
470 won't optimized out the variable. If some compiler constant-folds this
471 whole loop, maybe using a parameter to this function here would help. */
472 v.x = 0;
473 for (i = 0; i < 13; ++i)
474 v.x += i;
475 --v.x; /* v.x is now 77 */
476 marker_reg1 ();
477 return v.x + 5;
478}
479
480int
481main()
482{
483#ifdef usestubs
484 set_debug_traps();
485 breakpoint();
486#endif
487 inheritance1 ();
488 inheritance3 ();
489 register_class ();
490
491 /* FIXME: pmi gets optimized out. Need to do some more computation with
492 it or something. (No one notices, because the test is xfail'd anyway,
493 but that probably won't always be true...). */
494 int Foo::* pmi = &Foo::y;
495
496 /* Make sure the AIX linker doesn't remove the variable. */
497 v_tagless.one = 5;
498
499 /* Class with enumeration inside it */
500 ClassWithEnum obj_with_enum;
501 obj_with_enum.priv_enum = ClassWithEnum::green;
502
503 return foo.*pmi;
504}
505
506/* Create an instance for some classes, otherwise they get optimized away. */
507
508default_public_struct default_public_s;
509explicit_public_struct explicit_public_s;
510protected_struct protected_s;
511private_struct private_s;
512mixed_protection_struct mixed_protection_s;
513public_class public_c;
514protected_class protected_c;
515default_private_class default_private_c;
516explicit_private_class explicit_private_c;
517mixed_protection_class mixed_protection_c;