OSDN Git Service

9371b856ce7b69e5e06ab4adb6e31f4aeb2fc6bb
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.old-deja / g++.abi / vtable2.C
1 // { dg-do run  }
2 // { dg-require-alias "" }
3 // { dg-options "-fno-strict-aliasing" }
4 // Origin: Mark Mitchell <mark@codesourcery.com>
5
6 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100
7
8 #include <stddef.h>
9
10 struct S0
11 {
12   virtual void s0 ();
13 };
14
15 struct S1 : virtual public S0
16 {
17   virtual void s1 ();
18 };
19
20 struct S2 : virtual public S1
21 {
22   virtual void s1 ();
23   virtual void s0 ();
24 };
25
26 struct S3
27 {
28   virtual void s3 ();
29 };
30
31 struct S4 : public S3, virtual public S2
32 {
33   virtual void s1 ();
34 };
35
36 void S0::s0 ()
37 {
38 }
39
40 void S1::s1 ()
41 {
42 }
43
44 void S2::s1 ()
45 {
46 }
47
48 void S2::s0 ()
49 {
50 }
51
52 void S3::s3 ()
53 {
54 }
55
56 void S4::s1 ()
57 {
58 }
59
60 /* The vtables should look like:
61
62    S0 primary vtable
63    
64      S0 offset to top
65      S0 RTTI
66      S0::s0
67
68    =================
69
70    S1 primary vtable
71
72      S0::s0 vcall offset
73      S0 vbase offset
74      S1 offset to top
75      S1 RTTI
76      S0::s0
77      S1::s1
78
79    =================
80
81    S2 primary vtable
82    
83      S2::s1 vcall offset
84      S1 vbase offset
85      S2::s0 vcall offset
86      S0 vbase offset
87      S2 offset to top
88      S2 RTTI
89      S2::s0
90      S2::s1
91
92    =================
93
94    S3 primary vtable
95
96      S3 offset to top
97      S3 RTTI
98      S3::s3
99
100    =================
101
102    S4 primary vtable
103
104      vbase offset for S0
105      vbase offset for S1
106      vbase offset for S2
107      S4 offset to top
108      S4 RTTI
109      S3::s3
110      S4::s1
111
112    S2-in-S4 secondary vtable
113
114      S1 vbase offset
115      S4::s1 vcall offset
116      S0 vbase offset
117      S2:s0 vcall offset
118      S2 offset to top
119      S4 RTTI
120      S2::s0
121      S4::s1
122
123 */
124
125 // These are tricks to allow us to get raw function pointers for
126 // member functions.
127 extern "C" {
128   void S3_s3 () __attribute__((__alias__ ("_ZN2S32s3Ev")));
129   void S4_s1 () __attribute__((__alias__ ("_ZN2S42s1Ev")));
130 }
131
132 // IA-64 uses function descriptors not function pointers in its vtables.
133 #if defined __ia64__
134 #define CMP_VPTR(A, B)  (*(void **)(A) == *(void **)(B))
135 #ifdef _LP64
136 #define INC_VPTR(A)     ((A) += 2)
137 #define INC_VDATA(A,N)  ((A) += (N))
138 #else
139 #define INC_VPTR(A)     ((A) += 4)
140 #define INC_VDATA(A,N)  ((A) += 2*(N))
141 #endif
142 #else
143 #define CMP_VPTR(A, B)  (*(A) == (ptrdiff_t)(B))
144 #define INC_VPTR(A)     ((A) += 1)
145 #define INC_VDATA(A,N)  ((A) += (N))
146 #endif
147
148 int main ()
149 {
150   S4 s4;
151   ptrdiff_t **vptr;
152   ptrdiff_t *vtbl;
153
154   // Set vtbl to point at the beginning of S4's primary vtable.
155   vptr = (ptrdiff_t **) &s4;
156   vtbl = *vptr;
157   INC_VDATA (vtbl, -5);
158
159   if (*vtbl != ((char*) (S0*) &s4) - (char*) &s4)
160     return 1;
161   INC_VDATA (vtbl, 1);
162   if (*vtbl != ((char*) (S1*) &s4) - (char*) &s4)
163     return 2;
164   INC_VDATA (vtbl, 1);
165   if (*vtbl != ((char*) (S2*) &s4) - (char*) &s4)
166     return 3;
167   INC_VDATA (vtbl, 1);
168   if (*vtbl != 0)
169     return 4;
170   INC_VDATA (vtbl, 1);
171   // Skip the RTTI entry.
172   INC_VDATA (vtbl, 1);
173   if (! CMP_VPTR (vtbl, &S3_s3))
174     return 5;
175   INC_VPTR (vtbl);
176   if (! CMP_VPTR (vtbl, &S4_s1))
177     return 6;
178   INC_VPTR (vtbl);
179   // The S1 vbase offset.
180   if (*vtbl != 0)
181     return 7;
182   INC_VDATA (vtbl, 1);
183   // The S4::s1 vcall offset is negative; once you convert to S2, you
184   // have to convert to S4 to find the final overrider.
185   if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4))
186     return 8;
187   INC_VDATA (vtbl, 1);
188   if (*vtbl != 0)
189     return 9;
190   INC_VDATA (vtbl, 1);
191   if (*vtbl != 0)
192     return 10;
193   INC_VDATA (vtbl, 1);
194   // Now we're at the S2 offset to top entry.
195   if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4))
196     return 11;
197   INC_VDATA (vtbl, 1);
198   // Skip the RTTI entry.
199   INC_VDATA (vtbl, 1);
200   // Skip the remaining virtual functions -- they are thunks.
201   INC_VPTR (vtbl);
202   INC_VPTR (vtbl);
203 }
204
205 #else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */
206
207 int main ()
208 {
209 }
210
211 #endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */