1 // Test various aspects of vtable layout. 2 // Special g++ Options: -fno-strict-aliasing 3 // Origin: Mark Mitchell <mark@codesourcery.com> 4 5 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100 6 7 struct S0 8 { hS09 virtual void h () 10 { 11 } 12 13 int k; 14 }; 15 16 17 struct S1 18 { fS119 virtual void f () 20 { 21 } 22 23 int i; 24 }; 25 26 struct S2 : virtual public S0 27 { gS228 virtual void g () 29 { 30 } 31 32 int j; 33 }; 34 35 struct S3 36 { kS337 virtual void k () 38 { 39 } 40 41 int l; 42 }; 43 44 struct S4 : public virtual S1, public S2, public S3 45 { 46 }; 47 vtable(void * object)48inline void* vtable (void *object) 49 { 50 // The vptr is always the first part of the object. 51 return * (void **) object; 52 } 53 main()54int main () 55 { 56 // The vtable layout order for S4 should consist of S4's primary 57 // vtable (shared with S2), followed by the vtable for S3 (because 58 // it is a non-virtual base). Then, these should be followed by the 59 // the vtables for S1 and S0, which are virtual. 60 S4 s4; 61 S0 *s0 = &s4; 62 S1 *s1 = &s4; 63 S2 *s2 = &s4; 64 S3 *s3 = &s4; 65 66 if (vtable (&s4) != vtable (s2)) 67 return 1; 68 if (vtable (s2) >= vtable (s3)) 69 return 2; 70 if (vtable (s3) >= vtable (s1)) 71 return 3; 72 if (vtable (s1) >= vtable (s0)) 73 return 4; 74 } 75 76 #else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ 77 main()78int main () 79 { 80 } 81 82 #endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ 83