Anders Carlsson | e8820a8 | 2009-12-17 04:57:25 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -O3 -o - | FileCheck %s |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 2 | #include <typeinfo> |
| 3 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 4 | // vtables. |
| 5 | extern "C" { |
| 6 | const void *_ZTVN10__cxxabiv123__fundamental_type_infoE; |
| 7 | const void *_ZTVN10__cxxabiv117__class_type_infoE; |
| 8 | const void *_ZTVN10__cxxabiv120__si_class_type_infoE; |
Anders Carlsson | 2c0769e | 2009-12-30 01:25:42 +0000 | [diff] [blame^] | 9 | const void *_ZTVN10__cxxabiv121__vmi_class_type_infoE; |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 10 | const void *_ZTVN10__cxxabiv119__pointer_type_infoE; |
| 11 | const void *_ZTVN10__cxxabiv129__pointer_to_member_type_infoE; |
| 12 | }; |
| 13 | #define fundamental_type_info_vtable _ZTVN10__cxxabiv123__fundamental_type_infoE |
| 14 | #define class_type_info_vtable _ZTVN10__cxxabiv117__class_type_infoE |
| 15 | #define si_class_type_info_vtable _ZTVN10__cxxabiv120__si_class_type_infoE |
Anders Carlsson | 2c0769e | 2009-12-30 01:25:42 +0000 | [diff] [blame^] | 16 | #define vmi_class_type_info_vtable _ZTVN10__cxxabiv121__vmi_class_type_infoE |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 17 | #define pointer_type_info_vtable _ZTVN10__cxxabiv119__pointer_type_infoE |
| 18 | #define pointer_to_member_type_info_vtable _ZTVN10__cxxabiv129__pointer_to_member_type_infoE |
| 19 | |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 20 | class __pbase_type_info : public std::type_info { |
| 21 | public: |
| 22 | unsigned int __flags; |
| 23 | const std::type_info *__pointee; |
| 24 | |
| 25 | enum __masks { |
| 26 | __const_mask = 0x1, |
| 27 | __volatile_mask = 0x2, |
| 28 | __restrict_mask = 0x4, |
| 29 | __incomplete_mask = 0x8, |
| 30 | __incomplete_class_mask = 0x10 |
| 31 | }; |
| 32 | }; |
| 33 | |
| 34 | template<typename T> const T& to(const std::type_info &info) { |
| 35 | return static_cast<const T&>(info); |
| 36 | } |
| 37 | struct Incomplete; |
| 38 | |
Anders Carlsson | 2c0769e | 2009-12-30 01:25:42 +0000 | [diff] [blame^] | 39 | struct A { int a; }; |
| 40 | struct Empty { }; |
| 41 | |
| 42 | struct SI1 : A { }; |
| 43 | struct SI2 : Empty { }; |
| 44 | struct SI3 : Empty { virtual void f() { } }; |
| 45 | |
| 46 | struct VMI1 : private A { }; |
| 47 | struct VMI2 : virtual A { }; |
| 48 | struct VMI3 : A { virtual void f() { } }; |
| 49 | struct VMI4 : A, Empty { }; |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 51 | #define CHECK(x) if (!(x)) return __LINE__ |
| 52 | #define CHECK_VTABLE(type, vtable) if (&vtable##_type_info_vtable + 2 != (((void **)&(typeid(type)))[0])) return __LINE__ |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 53 | |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 54 | // CHECK: define i32 @_Z1fv() |
| 55 | int f() { |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 56 | // Vectors should be treated as fundamental types. |
| 57 | typedef short __v4hi __attribute__ ((__vector_size__ (8))); |
| 58 | CHECK_VTABLE(__v4hi, fundamental); |
Anders Carlsson | 2c0769e | 2009-12-30 01:25:42 +0000 | [diff] [blame^] | 59 | |
| 60 | // A does not have any bases. |
| 61 | CHECK_VTABLE(A, class); |
| 62 | |
| 63 | // SI1 has a single public base. |
| 64 | CHECK_VTABLE(SI1, si_class); |
| 65 | |
| 66 | // SI2 has a single public empty base. |
| 67 | CHECK_VTABLE(SI2, si_class); |
| 68 | |
| 69 | // SI3 has a single public empty base. SI3 is dynamic whereas Empty is not, but since Empty is |
| 70 | // an empty class, it will still be at offset zero. |
| 71 | CHECK_VTABLE(SI3, si_class); |
| 72 | |
| 73 | // VMI1 has a single base, but it is private. |
| 74 | CHECK_VTABLE(VMI1, vmi_class); |
| 75 | |
| 76 | // VMI2 has a single base, but it is virtual. |
| 77 | CHECK_VTABLE(VMI2, vmi_class); |
| 78 | |
| 79 | // VMI3 has a single base, but VMI3 is dynamic whereas A is not, and A is not empty. |
| 80 | CHECK_VTABLE(VMI3, vmi_class); |
| 81 | |
| 82 | // VMI4 has two bases. |
| 83 | CHECK_VTABLE(VMI4, vmi_class); |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 85 | // Pointers to incomplete classes. |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 86 | CHECK_VTABLE(Incomplete *, pointer); |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 87 | CHECK(to<__pbase_type_info>(typeid(Incomplete *)).__flags == __pbase_type_info::__incomplete_mask); |
| 88 | CHECK(to<__pbase_type_info>(typeid(Incomplete **)).__flags == __pbase_type_info::__incomplete_mask); |
| 89 | CHECK(to<__pbase_type_info>(typeid(Incomplete ***)).__flags == __pbase_type_info::__incomplete_mask); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 90 | |
| 91 | // Member pointers. |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 92 | CHECK_VTABLE(int Incomplete::*, pointer_to_member); |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 93 | CHECK(to<__pbase_type_info>(typeid(int Incomplete::*)).__flags == __pbase_type_info::__incomplete_class_mask); |
| 94 | CHECK(to<__pbase_type_info>(typeid(Incomplete Incomplete::*)).__flags == (__pbase_type_info::__incomplete_class_mask | __pbase_type_info::__incomplete_mask)); |
| 95 | CHECK(to<__pbase_type_info>(typeid(Incomplete A::*)).__flags == (__pbase_type_info::__incomplete_mask)); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 96 | |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 97 | // Success! |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 98 | // CHECK: ret i32 0 |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | #ifdef HARNESS |
| 103 | extern "C" void printf(const char *, ...); |
| 104 | |
| 105 | int main() { |
| 106 | int result = f(); |
| 107 | |
| 108 | if (result == 0) |
| 109 | printf("success!\n"); |
| 110 | else |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 111 | printf("test on line %d failed!\n", result); |
Anders Carlsson | 0a4f186 | 2009-12-17 04:41:05 +0000 | [diff] [blame] | 112 | |
| 113 | return result; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | |