1 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
2 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -fhidden-weak-vtables -emit-llvm -o %t.hidden
3 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -disable-llvm-optzns -O3 -emit-llvm -o %t.opt
4 // RUN: FileCheck --check-prefix=CHECK %s < %t
5 // RUN: FileCheck --check-prefix=CHECK-HIDDEN %s < %t.hidden
6 // RUN: FileCheck --check-prefix=CHECK-OPT %s < %t.opt
7 
8 namespace {
9   struct A {
10     virtual void f() { }
11   };
12 }
13 
14 void f() { A b; }
15 
16 struct B {
17   B();
18   virtual void f();
19 };
20 
21 B::B() { }
22 
23 struct C : virtual B {
24   C();
25   virtual void f() { }
26 };
27 
28 C::C() { }
29 
30 struct D {
31   virtual void f();
32 };
33 
34 void D::f() { }
35 
36 static struct : D { } e;
37 
38 // The destructor is the key function.
39 template<typename T>
40 struct E {
41   virtual ~E();
42 };
43 
44 template<typename T> E<T>::~E() { }
45 
46 // Anchor is the key function
47 template<>
48 struct E<char> {
49   virtual void anchor();
50 };
51 
52 void E<char>::anchor() { }
53 
54 template struct E<short>;
55 extern template struct E<int>;
56 
57 void use_E() {
58   E<int> ei;
59   (void)ei;
60   E<long> el;
61   (void)el;
62 }
63 
64 // No key function
65 template<typename T>
66 struct F {
67   virtual void foo() { }
68 };
69 
70 // No key function
71 template<>
72 struct F<char> {
73   virtual void foo() { }
74 };
75 
76 template struct F<short>;
77 extern template struct F<int>;
78 
79 void use_F() {
80   F<char> fc;
81   fc.foo();
82   F<int> fi;
83   fi.foo();
84   F<long> fl;
85   (void)fl;
86 }
87 
88 // B has a key function that is not defined in this translation unit so its vtable
89 // has external linkage.
90 // CHECK-DAG: @_ZTV1B = external unnamed_addr constant
91 
92 // C has no key function, so its vtable should have weak_odr linkage
93 // and hidden visibility (rdar://problem/7523229).
94 // CHECK-DAG: @_ZTV1C = linkonce_odr unnamed_addr constant
95 // CHECK-DAG: @_ZTS1C = linkonce_odr constant
96 // CHECK-DAG: @_ZTI1C = linkonce_odr unnamed_addr constant
97 // CHECK-DAG: @_ZTT1C = linkonce_odr unnamed_addr constant
98 // CHECK-HIDDEN-DAG: @_ZTV1C = linkonce_odr hidden unnamed_addr constant
99 // CHECK-HIDDEN-DAG: @_ZTS1C = linkonce_odr constant
100 // CHECK-HIDDEN-DAG: @_ZTI1C = linkonce_odr hidden unnamed_addr constant
101 // CHECK-HIDDEN-DAG: @_ZTT1C = linkonce_odr hidden unnamed_addr constant
102 
103 // D has a key function that is defined in this translation unit so its vtable is
104 // defined in the translation unit.
105 // CHECK-DAG: @_ZTV1D = unnamed_addr constant
106 // CHECK-DAG: @_ZTS1D = constant
107 // CHECK-DAG: @_ZTI1D = unnamed_addr constant
108 
109 // E<char> is an explicit specialization with a key function defined
110 // in this translation unit, so its vtable should have external
111 // linkage.
112 // CHECK-DAG: @_ZTV1EIcE = unnamed_addr constant
113 // CHECK-DAG: @_ZTS1EIcE = constant
114 // CHECK-DAG: @_ZTI1EIcE = unnamed_addr constant
115 
116 // E<short> is an explicit template instantiation with a key function
117 // defined in this translation unit, so its vtable should have
118 // weak_odr linkage.
119 // CHECK-DAG: @_ZTV1EIsE = weak_odr unnamed_addr constant
120 // CHECK-DAG: @_ZTS1EIsE = weak_odr constant
121 // CHECK-DAG: @_ZTI1EIsE = weak_odr unnamed_addr constant
122 // CHECK-HIDDEN-DAG: @_ZTV1EIsE = weak_odr unnamed_addr constant
123 // CHECK-HIDDEN-DAG: @_ZTS1EIsE = weak_odr constant
124 // CHECK-HIDDEN-DAG: @_ZTI1EIsE = weak_odr unnamed_addr constant
125 
126 // F<short> is an explicit template instantiation without a key
127 // function, so its vtable should have weak_odr linkage
128 // CHECK-DAG: @_ZTV1FIsE = weak_odr unnamed_addr constant
129 // CHECK-DAG: @_ZTS1FIsE = weak_odr constant
130 // CHECK-DAG: @_ZTI1FIsE = weak_odr unnamed_addr constant
131 // CHECK-HIDDEN-DAG: @_ZTV1FIsE = weak_odr unnamed_addr constant
132 // CHECK-HIDDEN-DAG: @_ZTS1FIsE = weak_odr constant
133 // CHECK-HIDDEN-DAG: @_ZTI1FIsE = weak_odr unnamed_addr constant
134 
135 // E<long> is an implicit template instantiation with a key function
136 // defined in this translation unit, so its vtable should have
137 // linkonce_odr linkage.
138 // CHECK-DAG: @_ZTV1EIlE = linkonce_odr unnamed_addr constant
139 // CHECK-DAG: @_ZTS1EIlE = linkonce_odr constant
140 // CHECK-DAG: @_ZTI1EIlE = linkonce_odr unnamed_addr constant
141 
142 // F<long> is an implicit template instantiation with no key function,
143 // so its vtable should have linkonce_odr linkage.
144 // CHECK-DAG: @_ZTV1FIlE = linkonce_odr unnamed_addr constant
145 // CHECK-DAG: @_ZTS1FIlE = linkonce_odr constant
146 // CHECK-DAG: @_ZTI1FIlE = linkonce_odr unnamed_addr constant
147 
148 // F<int> is an explicit template instantiation declaration without a
149 // key function, so its vtable should have external linkage.
150 // CHECK-DAG: @_ZTV1FIiE = external unnamed_addr constant
151 // CHECK-OPT-DAG: @_ZTV1FIiE = external unnamed_addr constant
152 
153 // E<int> is an explicit template instantiation declaration. It has a
154 // key function that is not instantiated, so we should only reference
155 // its vtable, not define it.
156 // CHECK-DAG: @_ZTV1EIiE = external unnamed_addr constant
157 // CHECK-OPT-DAG: @_ZTV1EIiE = external unnamed_addr constant
158 
159 // The anonymous struct for e has no linkage, so the vtable should have
160 // internal linkage.
161 // CHECK-DAG: @"_ZTV3$_0" = internal unnamed_addr constant
162 // CHECK-DAG: @"_ZTS3$_0" = internal constant
163 // CHECK-DAG: @"_ZTI3$_0" = internal unnamed_addr constant
164 
165 // The A vtable should have internal linkage since it is inside an anonymous
166 // namespace.
167 // CHECK-DAG: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant
168 // CHECK-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant
169 // CHECK-DAG: @_ZTIN12_GLOBAL__N_11AE = internal unnamed_addr constant
170 
171 // F<char> is an explicit specialization without a key function, so
172 // its vtable should have linkonce_odr linkage.
173 // CHECK-DAG: @_ZTV1FIcE = linkonce_odr unnamed_addr constant
174 // CHECK-DAG: @_ZTS1FIcE = linkonce_odr constant
175 // CHECK-DAG: @_ZTI1FIcE = linkonce_odr unnamed_addr constant
176 
177 // CHECK-DAG: @_ZTV1GIiE = linkonce_odr unnamed_addr constant
178 template <typename T>
179 class G {
180 public:
181   G() {}
182   virtual void f0();
183   virtual void f1();
184 };
185 template <>
186 void G<int>::f1() {}
187 template <typename T>
188 void G<T>::f0() {}
189 void G_f0()  { new G<int>(); }
190 
191 // H<int> has a key function without a body but it's a template instantiation
192 // so its VTable must be emitted.
193 // CHECK-DAG: @_ZTV1HIiE = linkonce_odr unnamed_addr constant
194 template <typename T>
195 class H {
196 public:
197   virtual ~H();
198 };
199 
200 void use_H() {
201   H<int> h;
202 }
203 
204 // I<int> has an explicit instantiation declaration and needs a VTT and
205 // construction vtables.
206 
207 // CHECK-DAG: @_ZTV1IIiE = external unnamed_addr constant
208 // CHECK-DAG: @_ZTT1IIiE = external unnamed_addr constant
209 // CHECK-NOT: @_ZTC1IIiE
210 //
211 // CHECK-OPT-DAG: @_ZTV1IIiE = external unnamed_addr constant
212 // CHECK-OPT-DAG: @_ZTT1IIiE = external unnamed_addr constant
213 struct VBase1 { virtual void f(); }; struct VBase2 : virtual VBase1 {};
214 template<typename T>
215 struct I : VBase2 {};
216 extern template struct I<int>;
217 I<int> i;
218