| 1 |
// { dg-do run } |
| 2 |
// { dg-options "-Wno-attribute-alias -fno-strict-aliasing" } |
| 3 |
// Origin: Mark Mitchell <[email protected]> |
| 4 |
|
| 5 |
#if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100 |
| 6 |
|
| 7 |
#include <stddef.h> |
| 8 |
|
| 9 |
struct S0 |
| 10 |
{ |
| 11 |
virtual void s0 (); |
| 12 |
}; |
| 13 |
|
| 14 |
struct S1 : virtual public S0 |
| 15 |
{ |
| 16 |
virtual void s1 (); |
| 17 |
}; |
| 18 |
|
| 19 |
struct S2 : virtual public S1 |
| 20 |
{ |
| 21 |
virtual void s1 (); |
| 22 |
virtual void s0 (); |
| 23 |
}; |
| 24 |
|
| 25 |
struct S3 |
| 26 |
{ |
| 27 |
virtual void s3 (); |
| 28 |
}; |
| 29 |
|
| 30 |
struct S4 : public S3, virtual public S2 |
| 31 |
{ |
| 32 |
virtual void s1 (); |
| 33 |
}; |
| 34 |
|
| 35 |
void S0::s0 () |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
void S1::s1 () |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
void S2::s1 () |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
void S2::s0 () |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
void S3::s3 () |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
void S4::s1 () |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
/* The vtables should look like: |
| 60 |
|
| 61 |
S0 primary vtable |
| 62 |
|
| 63 |
S0 offset to top |
| 64 |
S0 RTTI |
| 65 |
S0::s0 |
| 66 |
|
| 67 |
================= |
| 68 |
|
| 69 |
S1 primary vtable |
| 70 |
|
| 71 |
S0::s0 vcall offset |
| 72 |
S0 vbase offset |
| 73 |
S1 offset to top |
| 74 |
S1 RTTI |
| 75 |
S0::s0 |
| 76 |
S1::s1 |
| 77 |
|
| 78 |
================= |
| 79 |
|
| 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 |
| 90 |
|
| 91 |
================= |
| 92 |
|
| 93 |
S3 primary vtable |
| 94 |
|
| 95 |
S3 offset to top |
| 96 |
S3 RTTI |
| 97 |
S3::s3 |
| 98 |
|
| 99 |
================= |
| 100 |
|
| 101 |
S4 primary vtable |
| 102 |
|
| 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 |
| 110 |
|
| 111 |
S2-in-S4 secondary vtable |
| 112 |
|
| 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 |
| 121 |
|
| 122 |
*/ |
| 123 |
|
| 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. The references to the incompatible virtual S3::s3() |
| 130 |
and S4::s1() trigger -Wattributes. */ |
| 131 |
static void S3_s3 () __attribute__((__weakref__ ("_ZN2S32s3Ev"))); |
| 132 |
static void S4_s1 () __attribute__((__weakref__ ("_ZN2S42s1Ev"))); |
| 133 |
} |
| 134 |
|
| 135 |
// IA-64 uses function descriptors not function pointers in its vtables. |
| 136 |
#if defined __ia64__ |
| 137 |
#define CMP_VPTR(A, B) (*(void **)(A) == *(void **)(B)) |
| 138 |
#ifdef _LP64 |
| 139 |
#define INC_VPTR(A) ((A) += 2) |
| 140 |
#define INC_VDATA(A,N) ((A) += (N)) |
| 141 |
#else |
| 142 |
#define INC_VPTR(A) ((A) += 4) |
| 143 |
#define INC_VDATA(A,N) ((A) += 2*(N)) |
| 144 |
#endif |
| 145 |
#else |
| 146 |
// HPPA uses function pointers but they point to function descriptors. |
| 147 |
#if defined __hppa__ |
| 148 |
#ifdef __hpux__ |
| 149 |
#ifdef _LP64 |
| 150 |
#define CMP_VPTR(A, B) (*(unsigned long *)(*(A)+16) == *(unsigned long *)((unsigned long)(B)+16)) |
| 151 |
#else |
| 152 |
#define CMP_VPTR(A, B) (*(A) == (ptrdiff_t)(B)) |
| 153 |
#endif /* _LP64 */ |
| 154 |
#else |
| 155 |
extern "C" { unsigned int __canonicalize_funcptr_for_compare (void*); } |
| 156 |
#define CMP_VPTR(A, B) (__canonicalize_funcptr_for_compare(*(void **)A) == __canonicalize_funcptr_for_compare((void *)B)) |
| 157 |
#endif /* __hpux__ */ |
| 158 |
#else |
| 159 |
#define CMP_VPTR(A, B) (*(A) == (ptrdiff_t)(B)) |
| 160 |
#endif /* __hppa__ */ |
| 161 |
#define INC_VPTR(A) ((A) += 1) |
| 162 |
#define INC_VDATA(A,N) ((A) += (N)) |
| 163 |
#endif /* __ia64__ */ |
| 164 |
|
| 165 |
int main () |
| 166 |
{ |
| 167 |
S4 s4; |
| 168 |
ptrdiff_t **vptr; |
| 169 |
ptrdiff_t *vtbl; |
| 170 |
|
| 171 |
// Set vtbl to point at the beginning of S4's primary vtable. |
| 172 |
vptr = (ptrdiff_t **) &s4; |
| 173 |
vtbl = *vptr; |
| 174 |
INC_VDATA (vtbl, -5); |
| 175 |
|
| 176 |
if (*vtbl != ((char*) (S0*) &s4) - (char*) &s4) |
| 177 |
return 1; |
| 178 |
INC_VDATA (vtbl, 1); |
| 179 |
if (*vtbl != ((char*) (S1*) &s4) - (char*) &s4) |
| 180 |
return 2; |
| 181 |
INC_VDATA (vtbl, 1); |
| 182 |
if (*vtbl != ((char*) (S2*) &s4) - (char*) &s4) |
| 183 |
return 3; |
| 184 |
INC_VDATA (vtbl, 1); |
| 185 |
if (*vtbl != 0) |
| 186 |
return 4; |
| 187 |
INC_VDATA (vtbl, 1); |
| 188 |
// Skip the RTTI entry. |
| 189 |
INC_VDATA (vtbl, 1); |
| 190 |
if (! CMP_VPTR (vtbl, &S3_s3)) |
| 191 |
return 5; |
| 192 |
INC_VPTR (vtbl); |
| 193 |
if (! CMP_VPTR (vtbl, &S4_s1)) |
| 194 |
return 6; |
| 195 |
INC_VPTR (vtbl); |
| 196 |
// The S1 vbase offset. |
| 197 |
if (*vtbl != 0) |
| 198 |
return 7; |
| 199 |
INC_VDATA (vtbl, 1); |
| 200 |
// The S4::s1 vcall offset is negative; once you convert to S2, you |
| 201 |
// have to convert to S4 to find the final overrider. |
| 202 |
if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4)) |
| 203 |
return 8; |
| 204 |
INC_VDATA (vtbl, 1); |
| 205 |
if (*vtbl != 0) |
| 206 |
return 9; |
| 207 |
INC_VDATA (vtbl, 1); |
| 208 |
if (*vtbl != 0) |
| 209 |
return 10; |
| 210 |
INC_VDATA (vtbl, 1); |
| 211 |
// Now we're at the S2 offset to top entry. |
| 212 |
if (*vtbl != ((char*) &s4 - (char*) (S2*) &s4)) |
| 213 |
return 11; |
| 214 |
INC_VDATA (vtbl, 1); |
| 215 |
// Skip the RTTI entry. |
| 216 |
INC_VDATA (vtbl, 1); |
| 217 |
// Skip the remaining virtual functions -- they are thunks. |
| 218 |
INC_VPTR (vtbl); |
| 219 |
INC_VPTR (vtbl); |
| 220 |
} |
| 221 |
|
| 222 |
#else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ |
| 223 |
|
| 224 |
int main () |
| 225 |
{ |
| 226 |
} |
| 227 |
|
| 228 |
#endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ |