blob: 1ad87fbc7ef78ad99bb925adb8640c9c8d430466 [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
Anders Carlssonbeb80192009-12-30 01:29:05 +000034class __class_type_info : public std::type_info { };
35
36class __si_class_type_info : public __class_type_info {
37public:
38 const __class_type_info *__base_type;
39};
40
Anders Carlsson08148092009-12-30 23:47:56 +000041struct __base_class_type_info {
42public:
43 const __class_type_info *__base_type;
44 long __offset_flags;
45
46 enum __offset_flags_masks {
47 __virtual_mask = 0x1,
48 __public_mask = 0x2,
49 __offset_shift = 8
50 };
51};
52
53class __vmi_class_type_info : public __class_type_info {
54public:
55 unsigned int __flags;
56 unsigned int __base_count;
57 __base_class_type_info __base_info[1];
58
59 enum __flags_masks {
60 __non_diamond_repeat_mask = 0x1,
61 __diamond_shaped_mask = 0x2
62 };
63};
64
Anders Carlsson0a4f1862009-12-17 04:41:05 +000065template<typename T> const T& to(const std::type_info &info) {
66return static_cast<const T&>(info);
67}
68struct Incomplete;
69
Anders Carlsson2c0769e2009-12-30 01:25:42 +000070struct A { int a; };
71struct Empty { };
72
73struct SI1 : A { };
74struct SI2 : Empty { };
75struct SI3 : Empty { virtual void f() { } };
76
77struct VMI1 : private A { };
78struct VMI2 : virtual A { };
79struct VMI3 : A { virtual void f() { } };
80struct VMI4 : A, Empty { };
Anders Carlsson17fa6f92009-12-20 23:37:55 +000081
Anders Carlsson08148092009-12-30 23:47:56 +000082struct VMIBase1 { int a; };
83struct VMIBase2 : VMIBase1 { int a; };
84struct VMI5 : VMIBase1, VMIBase2 { int a; };
85
86struct VMIBase3 : virtual VMIBase1 { int a; };
87struct VMI6 : virtual VMIBase1, VMIBase3 { int a; };
88
89struct VMI7 : VMIBase1, VMI5, private VMI6 { };
90
Anders Carlssonf64531a2009-12-30 01:00:12 +000091#define CHECK(x) if (!(x)) return __LINE__
Anders Carlsson08148092009-12-30 23:47:56 +000092#define CHECK_VTABLE(type, vtable) CHECK(&vtable##_type_info_vtable + 2 == (((void **)&(typeid(type)))[0]))
93#define CHECK_BASE_INFO_TYPE(type, index, base) CHECK(to<__vmi_class_type_info>(typeid(type)).__base_info[(index)].__base_type == &typeid(base))
94#define CHECK_BASE_INFO_OFFSET_FLAGS(type, index, offset, flags) CHECK(to<__vmi_class_type_info>(typeid(type)).__base_info[(index)].__offset_flags == (((offset) << 8) | (flags)))
Anders Carlsson8d145152009-12-20 22:30:54 +000095
Anders Carlsson0a4f1862009-12-17 04:41:05 +000096// CHECK: define i32 @_Z1fv()
97int f() {
Anders Carlssonf64531a2009-12-30 01:00:12 +000098 // Vectors should be treated as fundamental types.
99 typedef short __v4hi __attribute__ ((__vector_size__ (8)));
100 CHECK_VTABLE(__v4hi, fundamental);
Anders Carlsson2c0769e2009-12-30 01:25:42 +0000101
102 // A does not have any bases.
103 CHECK_VTABLE(A, class);
104
105 // SI1 has a single public base.
106 CHECK_VTABLE(SI1, si_class);
Anders Carlsson08148092009-12-30 23:47:56 +0000107 CHECK(to<__si_class_type_info>(typeid(SI1)).__base_type == &typeid(A));
Anders Carlsson2c0769e2009-12-30 01:25:42 +0000108
109 // SI2 has a single public empty base.
110 CHECK_VTABLE(SI2, si_class);
Anders Carlsson08148092009-12-30 23:47:56 +0000111 CHECK(to<__si_class_type_info>(typeid(SI2)).__base_type == &typeid(Empty));
Anders Carlsson2c0769e2009-12-30 01:25:42 +0000112
113 // SI3 has a single public empty base. SI3 is dynamic whereas Empty is not, but since Empty is
114 // an empty class, it will still be at offset zero.
115 CHECK_VTABLE(SI3, si_class);
Anders Carlsson08148092009-12-30 23:47:56 +0000116 CHECK(to<__si_class_type_info>(typeid(SI3)).__base_type == &typeid(Empty));
Anders Carlsson2c0769e2009-12-30 01:25:42 +0000117
118 // VMI1 has a single base, but it is private.
119 CHECK_VTABLE(VMI1, vmi_class);
120
121 // VMI2 has a single base, but it is virtual.
122 CHECK_VTABLE(VMI2, vmi_class);
123
124 // VMI3 has a single base, but VMI3 is dynamic whereas A is not, and A is not empty.
125 CHECK_VTABLE(VMI3, vmi_class);
126
127 // VMI4 has two bases.
128 CHECK_VTABLE(VMI4, vmi_class);
Anders Carlsson08148092009-12-30 23:47:56 +0000129
130 // VMI5 has non-diamond shaped inheritance.
131 CHECK_VTABLE(VMI5, vmi_class);
132 CHECK(to<__vmi_class_type_info>(typeid(VMI5)).__flags == __vmi_class_type_info::__non_diamond_repeat_mask);
133 CHECK(to<__vmi_class_type_info>(typeid(VMI5)).__base_count == 2);
134 CHECK_BASE_INFO_TYPE(VMI5, 0, VMIBase1);
135 CHECK_BASE_INFO_OFFSET_FLAGS(VMI5, 0, 0, __base_class_type_info::__public_mask);
136 CHECK_BASE_INFO_TYPE(VMI5, 1, VMIBase2);
137 CHECK_BASE_INFO_OFFSET_FLAGS(VMI5, 1, 4, __base_class_type_info::__public_mask);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000138
Anders Carlsson08148092009-12-30 23:47:56 +0000139 // VMI6 has diamond shaped inheritance.
140 CHECK_VTABLE(VMI6, vmi_class);
141 CHECK(to<__vmi_class_type_info>(typeid(VMI6)).__flags == __vmi_class_type_info::__diamond_shaped_mask);
142 CHECK(to<__vmi_class_type_info>(typeid(VMI6)).__base_count == 2);
143 CHECK_BASE_INFO_TYPE(VMI6, 0, VMIBase1);
144 CHECK_BASE_INFO_OFFSET_FLAGS(VMI6, 0, -24, __base_class_type_info::__public_mask | __base_class_type_info::__virtual_mask);
145 CHECK_BASE_INFO_TYPE(VMI6, 1, VMIBase3);
146 CHECK_BASE_INFO_OFFSET_FLAGS(VMI6, 1, 0, __base_class_type_info::__public_mask);
147
148 // VMI7 has both non-diamond and diamond shaped inheritance.
149 CHECK_VTABLE(VMI7, vmi_class);
150 CHECK(to<__vmi_class_type_info>(typeid(VMI7)).__flags == (__vmi_class_type_info::__non_diamond_repeat_mask | __vmi_class_type_info::__diamond_shaped_mask));
151 CHECK(to<__vmi_class_type_info>(typeid(VMI7)).__base_count == 3);
152 CHECK_BASE_INFO_TYPE(VMI7, 0, VMIBase1);
153 CHECK_BASE_INFO_OFFSET_FLAGS(VMI7, 0, 16, __base_class_type_info::__public_mask);
154 CHECK_BASE_INFO_TYPE(VMI7, 1, VMI5);
155 CHECK_BASE_INFO_OFFSET_FLAGS(VMI7, 1, 20, __base_class_type_info::__public_mask);
156 CHECK_BASE_INFO_TYPE(VMI7, 2, VMI6);
157 CHECK_BASE_INFO_OFFSET_FLAGS(VMI7, 2, 0, 0);
Anders Carlssonbeb80192009-12-30 01:29:05 +0000158
Anders Carlsson8d145152009-12-20 22:30:54 +0000159 // Pointers to incomplete classes.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000160 CHECK_VTABLE(Incomplete *, pointer);
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000161 CHECK(to<__pbase_type_info>(typeid(Incomplete *)).__flags == __pbase_type_info::__incomplete_mask);
162 CHECK(to<__pbase_type_info>(typeid(Incomplete **)).__flags == __pbase_type_info::__incomplete_mask);
163 CHECK(to<__pbase_type_info>(typeid(Incomplete ***)).__flags == __pbase_type_info::__incomplete_mask);
Anders Carlsson8d145152009-12-20 22:30:54 +0000164
165 // Member pointers.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000166 CHECK_VTABLE(int Incomplete::*, pointer_to_member);
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000167 CHECK(to<__pbase_type_info>(typeid(int Incomplete::*)).__flags == __pbase_type_info::__incomplete_class_mask);
168 CHECK(to<__pbase_type_info>(typeid(Incomplete Incomplete::*)).__flags == (__pbase_type_info::__incomplete_class_mask | __pbase_type_info::__incomplete_mask));
169 CHECK(to<__pbase_type_info>(typeid(Incomplete A::*)).__flags == (__pbase_type_info::__incomplete_mask));
Anders Carlsson8d145152009-12-20 22:30:54 +0000170
Anders Carlsson0a4f1862009-12-17 04:41:05 +0000171 // Success!
Anders Carlsson8d145152009-12-20 22:30:54 +0000172 // CHECK: ret i32 0
Anders Carlsson0a4f1862009-12-17 04:41:05 +0000173 return 0;
174}
175
176#ifdef HARNESS
177extern "C" void printf(const char *, ...);
178
179int main() {
180 int result = f();
181
182 if (result == 0)
183 printf("success!\n");
184 else
Anders Carlsson8d145152009-12-20 22:30:54 +0000185 printf("test on line %d failed!\n", result);
Anders Carlsson0a4f1862009-12-17 04:41:05 +0000186
187 return result;
188}
189#endif
190
191