blob: 5f9cdab8f526e3e3cf254a3890d9418f2eca5e6b [file] [log] [blame]
Anders Carlssone8820a82009-12-17 04:57:25 +00001// RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -O3 -o - | FileCheck %s
Anders Carlsson0a4f1862009-12-17 04:41:05 +00002#include <typeinfo>
3
Anders Carlssonf64531a2009-12-30 01:00:12 +00004// vtables.
5extern "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 Carlsson2c0769e2009-12-30 01:25:42 +00009 const void *_ZTVN10__cxxabiv121__vmi_class_type_infoE;
Anders Carlssonf64531a2009-12-30 01:00:12 +000010 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 Carlsson2c0769e2009-12-30 01:25:42 +000016#define vmi_class_type_info_vtable _ZTVN10__cxxabiv121__vmi_class_type_infoE
Anders Carlssonf64531a2009-12-30 01:00:12 +000017#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 Carlsson0a4f1862009-12-17 04:41:05 +000020class __pbase_type_info : public std::type_info {
21public:
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
34template<typename T> const T& to(const std::type_info &info) {
35return static_cast<const T&>(info);
36}
37struct Incomplete;
38
Anders Carlsson2c0769e2009-12-30 01:25:42 +000039struct A { int a; };
40struct Empty { };
41
42struct SI1 : A { };
43struct SI2 : Empty { };
44struct SI3 : Empty { virtual void f() { } };
45
46struct VMI1 : private A { };
47struct VMI2 : virtual A { };
48struct VMI3 : A { virtual void f() { } };
49struct VMI4 : A, Empty { };
Anders Carlsson17fa6f92009-12-20 23:37:55 +000050
Anders Carlssonf64531a2009-12-30 01:00:12 +000051#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 Carlsson8d145152009-12-20 22:30:54 +000053
Anders Carlsson0a4f1862009-12-17 04:41:05 +000054// CHECK: define i32 @_Z1fv()
55int f() {
Anders Carlssonf64531a2009-12-30 01:00:12 +000056 // Vectors should be treated as fundamental types.
57 typedef short __v4hi __attribute__ ((__vector_size__ (8)));
58 CHECK_VTABLE(__v4hi, fundamental);
Anders Carlsson2c0769e2009-12-30 01:25:42 +000059
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 Carlssonf64531a2009-12-30 01:00:12 +000084
Anders Carlsson8d145152009-12-20 22:30:54 +000085 // Pointers to incomplete classes.
Anders Carlssonf64531a2009-12-30 01:00:12 +000086 CHECK_VTABLE(Incomplete *, pointer);
Anders Carlsson17fa6f92009-12-20 23:37:55 +000087 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 Carlsson8d145152009-12-20 22:30:54 +000090
91 // Member pointers.
Anders Carlssonf64531a2009-12-30 01:00:12 +000092 CHECK_VTABLE(int Incomplete::*, pointer_to_member);
Anders Carlsson17fa6f92009-12-20 23:37:55 +000093 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 Carlsson8d145152009-12-20 22:30:54 +000096
Anders Carlsson0a4f1862009-12-17 04:41:05 +000097 // Success!
Anders Carlsson8d145152009-12-20 22:30:54 +000098 // CHECK: ret i32 0
Anders Carlsson0a4f1862009-12-17 04:41:05 +000099 return 0;
100}
101
102#ifdef HARNESS
103extern "C" void printf(const char *, ...);
104
105int main() {
106 int result = f();
107
108 if (result == 0)
109 printf("success!\n");
110 else
Anders Carlsson8d145152009-12-20 22:30:54 +0000111 printf("test on line %d failed!\n", result);
Anders Carlsson0a4f1862009-12-17 04:41:05 +0000112
113 return result;
114}
115#endif
116
117