Small ChangeLog tweak.
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.abi / vtable2.C
blob30228756f12752d64be676761c2fde47825c432b
1 // { dg-do run  }
2 // { dg-options "-fno-strict-aliasing" }
3 // Origin: Mark Mitchell <mark@codesourcery.com>
5 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100
7 #include <stddef.h>
9 struct S0
11   virtual void s0 ();
14 struct S1 : virtual public S0
16   virtual void s1 ();
19 struct S2 : virtual public S1
21   virtual void s1 ();
22   virtual void s0 ();
25 struct S3
27   virtual void s3 ();
30 struct S4 : public S3, virtual public S2
32   virtual void s1 ();
35 void S0::s0 ()
39 void S1::s1 ()
43 void S2::s1 ()
47 void S2::s0 ()
51 void S3::s3 ()
55 void S4::s1 ()
59 /* The vtables should look like:
61    S0 primary vtable
62    
63      S0 offset to top
64      S0 RTTI
65      S0::s0
67    =================
69    S1 primary vtable
71      S0::s0 vcall offset
72      S0 vbase offset
73      S1 offset to top
74      S1 RTTI
75      S0::s0
76      S1::s1
78    =================
80    S2 primary vtable
81    
82      S2::s1 vcall offset
83      S1 vbase offset
84      S2::s0 vcall offset
85      S0 vbase offset
86      S2 offset to top
87      S2 RTTI
88      S2::s0
89      S2::s1
91    =================
93    S3 primary vtable
95      S3 offset to top
96      S3 RTTI
97      S3::s3
99    =================
101    S4 primary vtable
103      vbase offset for S0
104      vbase offset for S1
105      vbase offset for S2
106      S4 offset to top
107      S4 RTTI
108      S3::s3
109      S4::s1
111    S2-in-S4 secondary vtable
113      S1 vbase offset
114      S4::s1 vcall offset
115      S0 vbase offset
116      S2:s0 vcall offset
117      S2 offset to top
118      S4 RTTI
119      S2::s0
120      S4::s1
124 // These are tricks to allow us to get raw function pointers for
125 // member functions.
126 extern "C" {
127   /* We can use weakref here without dg-require-weak, because we know
128      the symbols are defined, so we don't actually issue the .weak
129      directives.  */
130   static void S3_s3 () __attribute__((__weakref__ ("_ZN2S32s3Ev")));
131   static void S4_s1 () __attribute__((__weakref__ ("_ZN2S42s1Ev")));
134 // IA-64 uses function descriptors not function pointers in its vtables.
135 #if defined __ia64__
136 #define CMP_VPTR(A, B)  (*(void **)(A) == *(void **)(B))
137 #ifdef _LP64
138 #define INC_VPTR(A)     ((A) += 2)
139 #define INC_VDATA(A,N)  ((A) += (N))
140 #else
141 #define INC_VPTR(A)     ((A) += 4)
142 #define INC_VDATA(A,N)  ((A) += 2*(N))
143 #endif
144 #else
145 // HPPA uses function pointers but they point to function descriptors.
146 #if defined __hppa__
147 #ifdef __hpux__
148 #ifdef _LP64
149 #define CMP_VPTR(A, B)  (*(unsigned long *)(*(A)+16) == *(unsigned long *)((unsigned long)(B)+16))
150 #else
151 #define CMP_VPTR(A, B)  (*(A) == (ptrdiff_t)(B))
152 #endif /* _LP64 */
153 #else
154 extern "C" { unsigned int __canonicalize_funcptr_for_compare (void*); }
155 #define CMP_VPTR(A, B) (__canonicalize_funcptr_for_compare(*(void **)A) == __canonicalize_funcptr_for_compare((void *)B))
156 #endif /* __hpux__ */
157 #else
158 #define CMP_VPTR(A, B)  (*(A) == (ptrdiff_t)(B))
159 #endif /* __hppa__ */
160 #define INC_VPTR(A)     ((A) += 1)
161 #define INC_VDATA(A,N)  ((A) += (N))
162 #endif /* __ia64__ */
164 int main ()
166   S4 s4;
167   ptrdiff_t **vptr;
168   ptrdiff_t *vtbl;
170   // Set vtbl to point at the beginning of S4's primary vtable.
171   vptr = (ptrdiff_t **) &s4;
172   vtbl = *vptr;
173   INC_VDATA (vtbl, -5);
175   if (*vtbl != ((char*) (S0*) &s4) - (char*) &s4)
176     return 1;
177   INC_VDATA (vtbl, 1);
178   if (*vtbl != ((char*) (S1*) &s4) - (char*) &s4)
179     return 2;
180   INC_VDATA (vtbl, 1);
181   if (*vtbl != ((char*) (S2*) &s4) - (char*) &s4)
182     return 3;
183   INC_VDATA (vtbl, 1);
184   if (*vtbl != 0)
185     return 4;
186   INC_VDATA (vtbl, 1);
187   // Skip the RTTI entry.
188   INC_VDATA (vtbl, 1);
189   if (! CMP_VPTR (vtbl, &S3_s3))
190     return 5;
191   INC_VPTR (vtbl);
192   if (! CMP_VPTR (vtbl, &S4_s1))
193     return 6;
194   INC_VPTR (vtbl);
195   // The S1 vbase offset.
196   if (*vtbl != 0)
197     return 7;
198   INC_VDATA (vtbl, 1);
199   // The S4::s1 vcall offset is negative; once you convert to S2, you
200   // have to convert to S4 to find the final overrider.
201   if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4))
202     return 8;
203   INC_VDATA (vtbl, 1);
204   if (*vtbl != 0)
205     return 9;
206   INC_VDATA (vtbl, 1);
207   if (*vtbl != 0)
208     return 10;
209   INC_VDATA (vtbl, 1);
210   // Now we're at the S2 offset to top entry.
211   if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4))
212     return 11;
213   INC_VDATA (vtbl, 1);
214   // Skip the RTTI entry.
215   INC_VDATA (vtbl, 1);
216   // Skip the remaining virtual functions -- they are thunks.
217   INC_VPTR (vtbl);
218   INC_VPTR (vtbl);
221 #else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */
223 int main ()
227 #endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */