]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.cp/member-ptr.cc
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / member-ptr.cc
CommitLineData
56c97c6e
MC
1/* This testcase is part of GDB, the GNU debugger.
2
1d506c26 3 Copyright 1998-2024 Free Software Foundation, Inc.
56c97c6e
MC
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
56c97c6e
MC
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
a9762ec7 14
56c97c6e 15 You should have received a copy of the GNU General Public License
47d48711 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
56c97c6e 17
c906108c
SS
18extern "C" {
19#include <stdio.h>
20}
21
22
23class A {
24public:
25 A();
26 int foo (int x);
27 int bar (int y);
28 virtual int baz (int z);
29 char c;
30 int j;
31 int jj;
32 static int s;
33};
34
35class B {
36public:
37 static int s;
38};
39
40int A::s = 10;
41int B::s = 20;
42
43A::A()
44{
45 c = 'x';
46 j = 5;
47}
48
49int A::foo (int dummy)
50{
51 j += 3;
52 return j + dummy;
53}
54
55int A::bar (int dummy)
56{
57 int r;
58 j += 13;
59 r = this->foo(15);
60 return r + j + 2 * dummy;
61}
62
63int A::baz (int dummy)
64{
65 int r;
66 j += 15;
67 r = this->foo(15);
68 return r + j + 12 * dummy;
69}
70
71int fum (int dummy)
72{
73 return 2 + 13 * dummy;
74}
75
76typedef int (A::*PMF)(int);
77
78typedef int A::*PMI;
79
0d5de010
DJ
80/* This class is in front of the other base classes of Diamond, so
81 that we can detect if the offset for Left or the first Base is
82 added twice - otherwise it would be 2 * 0 == 0. */
83class Padding
84{
2f12c312 85public:
0d5de010
DJ
86 int spacer;
87 virtual int vspacer();
88};
89
90int Padding::vspacer()
91{
92 return this->spacer;
93}
94
95class Base
96{
97public:
98 int x;
99 int get_x();
100 virtual int vget_base ();
101};
102
103int Base::get_x ()
104{
105 return this->x;
106}
107
108int Base::vget_base ()
109{
110 return this->x + 1000;
111}
112
113class Left : public Base {
114public:
115 virtual int vget ();
116};
117
118int Left::vget ()
119{
120 return this->x + 100;
121}
122
123class Right : public Base {
124public:
125 virtual int vget ();
126};
127
128int Right::vget ()
129{
130 return this->x + 200;
131}
132
133class Diamond : public Padding, public Left, public Right
134{
135public:
136 virtual int vget_base ();
5edf51fe 137 int (*func_ptr) (int);
0d5de010
DJ
138};
139
140int Diamond::vget_base ()
141{
142 return this->Left::x + 2000;
143}
144
86430497
TT
145struct Container
146{
147 PMI member;
148};
149
5edf51fe
YQ
150int
151func (int x)
152{
153 return 19 + x;
154}
155
c906108c
SS
156int main ()
157{
158 A a;
159 A * a_p;
160 PMF pmf;
161
162 PMF * pmf_p;
163 PMI pmi;
164
0d5de010
DJ
165 Diamond diamond;
166 int (Diamond::*left_pmf) ();
167 int (Diamond::*right_pmf) ();
168 int (Diamond::*left_vpmf) ();
169 int (Diamond::*left_base_vpmf) ();
170 int (Diamond::*right_vpmf) ();
171 int (Base::*base_vpmf) ();
172 int Diamond::*diamond_pmi;
5edf51fe 173 int (* Diamond::*diamond_pfunc_ptr) (int);
0d5de010
DJ
174
175 PMI null_pmi;
176 PMF null_pmf;
177
c906108c
SS
178 a.j = 121;
179 a.jj = 1331;
180
181 int k;
182
183 a_p = &a;
184
185 pmi = &A::j;
186 pmf = &A::bar;
187 pmf_p = &pmf;
188
0d5de010
DJ
189 diamond.Left::x = 77;
190 diamond.Right::x = 88;
5edf51fe 191 diamond.func_ptr = func;
0d5de010
DJ
192
193 /* Some valid pointer to members from a base class. */
194 left_pmf = (int (Diamond::*) ()) (int (Left::*) ()) (&Base::get_x);
195 right_pmf = (int (Diamond::*) ()) (int (Right::*) ()) (&Base::get_x);
196 left_vpmf = &Left::vget;
197 left_base_vpmf = (int (Diamond::*) ()) (int (Left::*) ()) (&Base::vget_base);
198 right_vpmf = &Right::vget;
199
200 /* An unspecified, value preserving pointer to member cast. */
201 base_vpmf = (int (Base::*) ()) (int (Left::*) ()) &Diamond::vget_base;
202
203 /* A pointer to data member from a base class. */
204 diamond_pmi = (int Diamond::*) (int Left::*) &Base::x;
205
5edf51fe
YQ
206 /* A pointer to data member, where the member is itself a pointer to
207 a function. */
208 diamond_pfunc_ptr = (int (* Diamond::*) (int)) &Diamond::func_ptr;
209
0d5de010
DJ
210 null_pmi = NULL;
211 null_pmf = NULL;
212
86430497
TT
213 Container contain;
214 contain.member = &A::j;
215
0d5de010
DJ
216 pmi = NULL; /* Breakpoint 1 here. */
217
5edf51fe
YQ
218 (diamond.*diamond_pfunc_ptr) (20);
219
c906108c
SS
220 k = (a.*pmf)(3);
221
222 pmi = &A::jj;
223 pmf = &A::foo;
224 pmf_p = &pmf;
225
226 k = (a.*pmf)(4);
227
228 k = (a.**pmf_p)(5);
229
230 k = a.*pmi;
231
232
233 k = a.bar(2);
234
235 k += fum (4);
236
237 B b;
238
239 k += b.s;
240
dc3a371e 241 return 0;
c906108c 242}