1 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -o %t
2 // RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -O2 -disable-llvm-optzns -emit-llvm -o %t.opt
3 // RUN: FileCheck --check-prefix=CHECK-TEST1 %s < %t
4 // RUN: FileCheck --check-prefix=CHECK-TEST2 %s < %t
5 // RUN: FileCheck --check-prefix=CHECK-TEST5 %s < %t
6 // RUN: FileCheck --check-prefix=CHECK-TEST8 %s < %t.opt
7 // RUN: FileCheck --check-prefix=CHECK-TEST9 %s < %t.opt
8 // RUN: FileCheck --check-prefix=CHECK-TEST10 %s < %t.opt
9 // RUN: FileCheck --check-prefix=CHECK-TEST11 %s < %t.opt
10 
11 #include <typeinfo>
12 
13 // CHECK-TEST1: @_ZTVN5Test11AE = external unnamed_addr constant
14 namespace Test1 {
15 
16 struct A {
17   A();
18   virtual void f();
19   virtual ~A() { }
20 };
21 
22 A::A() { }
23 
24 void f(A* a) {
25   a->f();
26 };
27 
28 // CHECK-LABEL: define void @_ZN5Test11gEv
29 // CHECK: call void @_ZN5Test11A1fEv
30 void g() {
31   A a;
32   f(&a);
33 }
34 
35 }
36 
37 // Test2::A's key function (f) is defined in this translation unit, but when
38 // we're doing codegen for the typeid(A) call, we don't know that yet.
39 // This tests mainly that the typeinfo and typename constants have their linkage
40 // updated correctly.
41 
42 // CHECK-TEST2: @_ZTSN5Test21AE = constant
43 // CHECK-TEST2: @_ZTIN5Test21AE = constant
44 // CHECK-TEST2: @_ZTVN5Test21AE = unnamed_addr constant
45 namespace Test2 {
46   struct A {
47     virtual void f();
48   };
49 
50   const std::type_info &g() {
51     return typeid(A);
52   };
53 
54   void A::f() { }
55 }
56 
57 // Test that we don't assert on this test.
58 namespace Test3 {
59 
60 struct A {
61   virtual void f();
62   virtual ~A() { }
63 };
64 
65 struct B : A {
66   B();
67   virtual void f();
68 };
69 
70 B::B() { }
71 
72 void g(A* a) {
73   a->f();
74 };
75 
76 }
77 
78 // PR9114, test that we don't try to instantiate RefPtr<Node>.
79 namespace Test4 {
80 
81 template <class T> struct RefPtr {
82   T* p;
83   ~RefPtr() {
84     p->deref();
85   }
86 };
87 
88 struct A {
89   virtual ~A();
90 };
91 
92 struct Node;
93 
94 struct B : A {
95   virtual void deref();
96   RefPtr<Node> m;
97 };
98 
99 void f() {
100   RefPtr<B> b;
101 }
102 
103 }
104 
105 // PR9130, test that we emit a definition of A::f.
106 // CHECK-TEST5-LABEL: define linkonce_odr void @_ZN5Test51A1fEv
107 namespace Test5 {
108 
109 struct A {
110   virtual void f() { }
111 };
112 
113 struct B : A {
114   virtual ~B();
115 };
116 
117 B::~B() { }
118 
119 }
120 
121 // Check that we don't assert on this test.
122 namespace Test6 {
123 
124 struct A {
125   virtual ~A();
126   int a;
127 };
128 
129 struct B {
130   virtual ~B();
131   int b;
132 };
133 
134 struct C : A, B {
135   C();
136 };
137 
138 struct D : C {
139   virtual void f();
140   D();
141 };
142 
143 D::D() { }
144 
145 }
146 
147 namespace Test7 {
148 
149 struct c1 {};
150 struct c10 : c1{
151   virtual void foo ();
152 };
153 struct c11 : c10, c1{
154   virtual void f6 ();
155 };
156 struct c28 : virtual c11{
157   void f6 ();
158 };
159 }
160 
161 namespace Test8 {
162 // CHECK-TEST8: @_ZTVN5Test81YE = available_externally unnamed_addr constant
163 // vtable for X is not generated because there are no stores here
164 struct X {
165   X();
166   virtual void foo();
167 };
168 struct Y : X {
169   void foo();
170 };
171 
172 void g(X* p) { p->foo(); }
173 void f() {
174   Y y;
175   g(&y);
176   X x;
177   g(&x);
178 }
179 
180 }  // Test8
181 
182 namespace Test9 {
183 // all virtual functions are outline, so we can assume that it will
184 // be generated in translation unit where foo is defined
185 // CHECK-TEST9: @_ZTVN5Test91AE = available_externally unnamed_addr constant
186 // CHECK-TEST9: @_ZTVN5Test91BE = available_externally unnamed_addr constant
187 struct A {
188   virtual void foo();
189   virtual void bar();
190 };
191 void A::bar() {}
192 
193 struct B : A {
194   void foo();
195 };
196 
197 void g() {
198   A a;
199   a.foo();
200   B b;
201   b.foo();
202 }
203 
204 }  // Test9
205 
206 namespace Test10 {
207 
208 // because A's key function is defined here, vtable is generated in this TU
209 // CHECK-TEST10: @_ZTVN6Test101AE = unnamed_addr constant
210 struct A {
211   virtual void foo();
212   virtual void bar();
213 };
214 void A::foo() {}
215 
216 // Because key function is inline we will generate vtable as linkonce_odr
217 // CHECK-TEST10: @_ZTVN6Test101DE = linkonce_odr unnamed_addr constant
218 struct D : A {
219   void bar();
220 };
221 inline void D::bar() {}
222 
223 // because B has outline key function then we can refer to
224 // CHECK-TEST10: @_ZTVN6Test101BE = available_externally unnamed_addr constant
225 struct B : A {
226   void foo();
227   void bar();
228 };
229 
230 // C's key function (car) is outline, but C has inline virtual function so we
231 // can't guarantee that we will be able to refer to bar from name
232 // so (at the moment) we can't emit vtable available_externally
233 // CHECK-TEST10: @_ZTVN6Test101CE = external unnamed_addr constant
234 struct C : A {
235   void bar() {}               // defined in body - not key function
236   virtual inline void gar();  // inline in body - not key function
237   virtual void car();
238 };
239 
240 // no key function, vtable will be generated everywhere it will be used
241 // CHECK-TEST10: @_ZTVN6Test101EE = linkonce_odr unnamed_addr constant
242 struct E : A {};
243 
244 void g(A& a) {
245   a.foo();
246   a.bar();
247 }
248 
249 void f() {
250   A a;
251   g(a);
252   B b;
253   g(b);
254   C c;
255   g(c);
256   D d;
257   g(d);
258   E e;
259   g(e);
260 }
261 
262 }  // Test10
263 
264 namespace Test11 {
265 struct D;
266 // Can emit C's vtable available_externally.
267 // CHECK-TEST11: @_ZTVN6Test111CE = available_externally unnamed_addr constant
268 struct C {
269   virtual D& operator=(const D&);
270 };
271 
272 // Cannot emit B's vtable available_externally, because we cannot create
273 // a reference to the inline virtual B::operator= function.
274 // CHECK-TEST11: @_ZTVN6Test111DE = external unnamed_addr constant
275 struct D : C {
276   virtual void key();
277 };
278 D f();
279 
280 void g(D& a) {
281   C c;
282   c = a;
283   a.key();
284   a.key();
285 }
286 void g() {
287   D d;
288   d = f();
289   g(d);
290 }
291 }  // Test 11
292