Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s |
| 2 | |
| 3 | namespace Test1 { |
| 4 | |
| 5 | // Check that we emit a non-virtual thunk for C::f. |
| 6 | |
| 7 | struct A { |
| 8 | virtual void f(); |
| 9 | }; |
| 10 | |
| 11 | struct B { |
| 12 | virtual void f(); |
| 13 | }; |
| 14 | |
| 15 | struct C : A, B { |
| 16 | virtual void c(); |
| 17 | |
| 18 | virtual void f(); |
| 19 | }; |
| 20 | |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 21 | // CHECK: define void @_ZThn8_N5Test11C1fEv( |
Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 22 | void C::f() { } |
| 23 | |
| 24 | } |
| 25 | |
| 26 | namespace Test2 { |
| 27 | |
| 28 | // Check that we emit a thunk for B::f since it's overriding a virtual base. |
| 29 | |
| 30 | struct A { |
| 31 | virtual void f(); |
| 32 | }; |
| 33 | |
| 34 | struct B : virtual A { |
| 35 | virtual void b(); |
| 36 | virtual void f(); |
| 37 | }; |
| 38 | |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 39 | // CHECK: define void @_ZTv0_n24_N5Test21B1fEv( |
Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 40 | void B::f() { } |
| 41 | |
| 42 | } |
| 43 | |
| 44 | namespace Test3 { |
| 45 | |
| 46 | // Check that we emit a covariant thunk for B::f. |
| 47 | |
| 48 | struct V1 { }; |
| 49 | struct V2 : virtual V1 { }; |
| 50 | |
| 51 | struct A { |
| 52 | virtual V1 *f(); |
| 53 | }; |
| 54 | |
| 55 | struct B : A { |
| 56 | virtual void b(); |
| 57 | |
| 58 | virtual V2 *f(); |
| 59 | }; |
| 60 | |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 61 | // CHECK: define %{{.*}}* @_ZTch0_v0_n24_N5Test31B1fEv( |
Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 62 | V2 *B::f() { return 0; } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | namespace Test4 { |
| 67 | |
| 68 | // Check that the thunk for 'C::f' has the same visibility as the function itself. |
| 69 | |
| 70 | struct A { |
| 71 | virtual void f(); |
| 72 | }; |
| 73 | |
| 74 | struct B { |
| 75 | virtual void f(); |
| 76 | }; |
| 77 | |
| 78 | struct __attribute__((visibility("protected"))) C : A, B { |
| 79 | virtual void c(); |
| 80 | |
| 81 | virtual void f(); |
| 82 | }; |
| 83 | |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 84 | // CHECK: define protected void @_ZThn8_N5Test41C1fEv( |
Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 85 | void C::f() { } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | // Check that the thunk gets internal linkage. |
| 90 | namespace { |
| 91 | |
| 92 | struct A { |
| 93 | virtual void f(); |
| 94 | }; |
| 95 | |
| 96 | struct B { |
| 97 | virtual void f(); |
| 98 | }; |
| 99 | |
| 100 | struct C : A, B { |
| 101 | virtual void c(); |
| 102 | |
| 103 | virtual void f(); |
| 104 | }; |
| 105 | |
Anders Carlsson | d63fed4 | 2010-03-24 00:41:37 +0000 | [diff] [blame] | 106 | void C::f() { } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | // Force C::f to be used. |
| 111 | void f() { |
| 112 | C c; |
| 113 | |
| 114 | c.f(); |
| 115 | } |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 116 | |
| 117 | namespace Test5 { |
| 118 | |
| 119 | // Check that the thunk for 'B::f' gets the same linkage as the function itself. |
| 120 | struct A { |
| 121 | virtual void f(); |
| 122 | }; |
| 123 | |
| 124 | struct B : virtual A { |
| 125 | virtual void f() { } |
| 126 | }; |
| 127 | |
| 128 | void f(B b) { |
| 129 | b.f(); |
| 130 | } |
| 131 | } |
| 132 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 133 | namespace Test6 { |
| 134 | struct X { |
| 135 | X(); |
| 136 | X(const X&); |
| 137 | X &operator=(const X&); |
| 138 | ~X(); |
| 139 | }; |
Anders Carlsson | ada087c | 2010-03-27 20:50:27 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 141 | struct P { |
| 142 | P(); |
| 143 | P(const P&); |
| 144 | ~P(); |
| 145 | X first; |
| 146 | X second; |
| 147 | }; |
| 148 | |
| 149 | P getP(); |
| 150 | |
| 151 | struct Base1 { |
| 152 | int i; |
| 153 | |
| 154 | virtual X f() { return X(); } |
| 155 | }; |
| 156 | |
| 157 | struct Base2 { |
| 158 | float real; |
| 159 | |
| 160 | virtual X f() { return X(); } |
| 161 | }; |
| 162 | |
| 163 | struct Thunks : Base1, Base2 { |
| 164 | long l; |
| 165 | |
| 166 | virtual X f(); |
| 167 | }; |
| 168 | |
| 169 | // CHECK: define void @_ZThn16_N5Test66Thunks1fEv |
| 170 | // CHECK-NOT: memcpy |
| 171 | // CHECK: {{call void @_ZN5Test66Thunks1fEv.*sret}} |
| 172 | // CHECK: ret void |
| 173 | X Thunks::f() { return X(); } |
| 174 | } |
| 175 | |
Douglas Gregor | 663218b | 2010-05-21 17:55:12 +0000 | [diff] [blame] | 176 | namespace Test7 { |
| 177 | // PR7188 |
| 178 | struct X { |
| 179 | X(); |
| 180 | X(const X&); |
| 181 | X &operator=(const X&); |
| 182 | ~X(); |
| 183 | }; |
| 184 | |
| 185 | struct Small { short s; }; |
| 186 | struct Large { |
| 187 | char array[1024]; |
| 188 | }; |
| 189 | |
| 190 | class A { |
| 191 | protected: |
| 192 | virtual void foo() = 0; |
| 193 | }; |
| 194 | |
| 195 | class B : public A { |
| 196 | protected: |
| 197 | virtual void bar() = 0; |
| 198 | }; |
| 199 | |
| 200 | class C : public A { |
| 201 | protected: |
| 202 | virtual void baz(X, X&, _Complex float, Small, Small&, Large) = 0; |
| 203 | }; |
| 204 | |
| 205 | class D : public B, |
| 206 | public C { |
| 207 | |
| 208 | void foo() {} |
| 209 | void bar() {} |
| 210 | void baz(X, X&, _Complex float, Small, Small&, Large); |
| 211 | }; |
| 212 | |
| 213 | void D::baz(X, X&, _Complex float, Small, Small&, Large) { } |
| 214 | |
| 215 | // CHECK: define void @_ZThn8_N5Test71D3bazENS_1XERS1_CfNS_5SmallERS4_NS_5LargeE( |
| 216 | // CHECK-NOT: memcpy |
| 217 | // CHECK: ret void |
| 218 | void testD() { D d; } |
| 219 | } |
| 220 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 221 | namespace Test8 { |
| 222 | struct NonPOD { ~NonPOD(); int x, y, z; }; |
| 223 | struct A { virtual void foo(); }; |
| 224 | struct B { virtual void bar(NonPOD); }; |
| 225 | struct C : A, B { virtual void bar(NonPOD); static void helper(NonPOD); }; |
| 226 | |
| 227 | // CHECK: define void @_ZN5Test81C6helperENS_6NonPODE([[NONPODTYPE:%.*]]* |
| 228 | void C::helper(NonPOD var) {} |
| 229 | |
| 230 | // CHECK: define void @_ZThn8_N5Test81C3barENS_6NonPODE( |
| 231 | // CHECK-NOT: load [[NONPODTYPE]]* |
| 232 | // CHECK-NOT: memcpy |
| 233 | // CHECK: ret void |
| 234 | void C::bar(NonPOD var) {} |
| 235 | } |
| 236 | |
John McCall | e213235 | 2010-06-02 21:22:02 +0000 | [diff] [blame] | 237 | // PR7241: Emitting thunks for a method shouldn't require the vtable for |
| 238 | // that class to be emitted. |
| 239 | namespace Test9 { |
| 240 | struct A { virtual ~A() { } }; |
| 241 | struct B : A { virtual void test() const {} }; |
| 242 | struct C : B { C(); ~C(); }; |
| 243 | struct D : C { D() {} }; |
| 244 | void test() { |
| 245 | D d; |
| 246 | } |
| 247 | } |
| 248 | |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame^] | 249 | namespace Test10 { |
| 250 | struct A { virtual void foo(); }; |
| 251 | struct B { virtual void foo(); }; |
| 252 | struct C : A, B { void foo() {} }; |
| 253 | |
| 254 | // CHECK: define linkonce_odr void @_ZN6Test101C3fooEv |
| 255 | // CHECK: define linkonce_odr hidden void @_ZThn8_N6Test101C3fooEv |
| 256 | |
| 257 | void test() { |
| 258 | C c; |
| 259 | } |
| 260 | } |
| 261 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 262 | /**** The following has to go at the end of the file ****/ |
| 263 | |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 264 | // This is from Test5: |
John McCall | 6500553 | 2010-08-04 23:46:35 +0000 | [diff] [blame^] | 265 | // CHECK: define linkonce_odr hidden void @_ZTv0_n24_N5Test51B1fEv |
Douglas Gregor | cb359df | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 266 | // CHECK: define internal void @_ZThn8_N12_GLOBAL__N_11C1fEv( |