blob: 6c1301bee1ef68f8bab2585f54c77606ff7e5325 [file] [log] [blame]
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
2// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
3// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
4// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
5// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
6// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
7// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
8// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
9// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
10// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
11// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
12// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
13// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
John McCall0c7d32b2010-08-04 06:38:15 +000014// RUN: FileCheck --check-prefix=CHECK-13 %s < %t
Anders Carlsson7ca46432009-12-05 17:04:47 +000015
16namespace {
Anders Carlsson7ca46432009-12-05 17:04:47 +000017 struct A {
18 virtual void f() { }
19 };
Anders Carlsson7ca46432009-12-05 17:04:47 +000020}
21
Anders Carlsson152d4dc2009-12-05 22:19:10 +000022void f() { A b; }
23
24struct B {
25 B();
26 virtual void f();
27};
28
29B::B() { }
30
Anders Carlsson891c8b72009-12-05 22:24:38 +000031struct C {
32 C();
33 virtual void f() { }
34};
35
36C::C() { }
37
Anders Carlsson5794c972009-12-06 00:53:22 +000038struct D {
39 virtual void f();
40};
41
42void D::f() { }
43
Eli Friedman470fb732009-12-11 20:48:18 +000044static struct : D { } e;
45
Douglas Gregor074a2cf2010-01-05 21:40:05 +000046// The destructor is the key function.
Douglas Gregorbd6d6192010-01-05 19:06:31 +000047template<typename T>
48struct E {
49 virtual ~E();
50};
51
52template<typename T> E<T>::~E() { }
53
Douglas Gregor074a2cf2010-01-05 21:40:05 +000054// Anchor is the key function
Douglas Gregorbd6d6192010-01-05 19:06:31 +000055template<>
56struct E<char> {
57 virtual void anchor();
58};
59
60void E<char>::anchor() { }
61
62template struct E<short>;
63extern template struct E<int>;
64
65void use_E() {
66 E<int> ei;
67 (void)ei;
68 E<long> el;
69 (void)el;
70}
71
Douglas Gregor074a2cf2010-01-05 21:40:05 +000072// No key function
73template<typename T>
74struct F {
75 virtual void foo() { }
76};
77
78// No key function
79template<>
80struct F<char> {
81 virtual void foo() { }
82};
83
84template struct F<short>;
85extern template struct F<int>;
86
John McCall0c7d32b2010-08-04 06:38:15 +000087void use_F() {
88 F<char> fc;
89 fc.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000090 F<int> fi;
Douglas Gregor6fb745b2010-05-13 16:44:06 +000091 fi.foo();
Douglas Gregor074a2cf2010-01-05 21:40:05 +000092 F<long> fl;
93 (void)fl;
Douglas Gregor074a2cf2010-01-05 21:40:05 +000094}
95
Anders Carlsson152d4dc2009-12-05 22:19:10 +000096// B has a key function that is not defined in this translation unit so its vtable
97// has external linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +000098// CHECK-1: @_ZTV1B = external constant
Anders Carlsson152d4dc2009-12-05 22:19:10 +000099
John McCall3d640e62010-08-03 07:24:12 +0000100// C has no key function, so its vtable should have weak_odr linkage
101// and hidden visibility (rdar://problem/7523229).
102// CHECK-2: @_ZTV1C = weak_odr hidden constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000103// CHECK-2: @_ZTS1C = weak_odr constant
John McCallcbfe5022010-08-04 08:34:44 +0000104// CHECK-2: @_ZTI1C = weak_odr hidden constant
Anders Carlsson891c8b72009-12-05 22:24:38 +0000105
Anders Carlsson5794c972009-12-06 00:53:22 +0000106// D has a key function that is defined in this translation unit so its vtable is
107// defined in the translation unit.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000108// CHECK-3: @_ZTV1D = constant
109// CHECK-3: @_ZTS1D = constant
110// CHECK-3: @_ZTI1D = constant
Anders Carlsson5794c972009-12-06 00:53:22 +0000111
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000112// E<char> is an explicit specialization with a key function defined
113// in this translation unit, so its vtable should have external
114// linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000115// CHECK-4: @_ZTV1EIcE = constant
116// CHECK-4: @_ZTS1EIcE = constant
117// CHECK-4: @_ZTI1EIcE = constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000118
119// E<short> is an explicit template instantiation with a key function
120// defined in this translation unit, so its vtable should have
121// weak_odr linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000122// CHECK-5: @_ZTV1EIsE = weak_odr constant
123// CHECK-5: @_ZTS1EIsE = weak_odr constant
124// CHECK-5: @_ZTI1EIsE = weak_odr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000125
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000126// F<short> is an explicit template instantiation without a key
127// function, so its vtable should have weak_odr linkage
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000128// CHECK-6: @_ZTV1FIsE = weak_odr constant
129// CHECK-6: @_ZTS1FIsE = weak_odr constant
130// CHECK-6: @_ZTI1FIsE = weak_odr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000131
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000132// E<long> is an implicit template instantiation with a key function
133// defined in this translation unit, so its vtable should have
134// weak_odr linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000135// CHECK-7: @_ZTV1EIlE = weak_odr constant
136// CHECK-7: @_ZTS1EIlE = weak_odr constant
137// CHECK-7: @_ZTI1EIlE = weak_odr constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000138
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000139// F<long> is an implicit template instantiation with no key function,
John McCall7a536902010-08-05 20:39:18 +0000140// so its vtable should have weak_odr linkage.
141// CHECK-8: @_ZTV1FIlE = weak_odr constant
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000142// CHECK-8: @_ZTS1FIlE = weak_odr constant
John McCall7a536902010-08-05 20:39:18 +0000143// CHECK-8: @_ZTI1FIlE = weak_odr constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000144
145// F<int> is an explicit template instantiation declaration without a
Rafael Espindola35d64612010-04-03 04:26:42 +0000146// key function, so its vtable should have external linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000147// CHECK-9: @_ZTV1FIiE = external constant
Douglas Gregor074a2cf2010-01-05 21:40:05 +0000148
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000149// E<int> is an explicit template instantiation declaration. It has a
Douglas Gregorc84622a2010-01-07 04:09:30 +0000150// key function that is not instantiated, so we should only reference
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000151// its vtable, not define it.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000152// CHECK-10: @_ZTV1EIiE = external constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000153
Eli Friedman6c6bda32010-01-08 00:50:11 +0000154// The anonymous struct for e has no linkage, so the vtable should have
155// internal linkage.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000156// CHECK-11: @"_ZTV3$_0" = internal constant
157// CHECK-11: @"_ZTS3$_0" = internal constant
158// CHECK-11: @"_ZTI3$_0" = internal constant
Eli Friedman6c6bda32010-01-08 00:50:11 +0000159
Anders Carlsson152d4dc2009-12-05 22:19:10 +0000160// The A vtable should have internal linkage since it is inside an anonymous
161// namespace.
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000162// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant
163// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
164// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000165
John McCall0c7d32b2010-08-04 06:38:15 +0000166// F<char> is an explicit specialization without a key function, so
John McCall7a536902010-08-05 20:39:18 +0000167// its vtable should have weak_odr linkage.
168// CHECK-13: @_ZTV1FIcE = weak_odr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000169// CHECK-13: @_ZTS1FIcE = weak_odr constant
John McCall7a536902010-08-05 20:39:18 +0000170// CHECK-13: @_ZTI1FIcE = weak_odr constant
John McCall0c7d32b2010-08-04 06:38:15 +0000171
Daniel Dunbarb9aefa72010-05-25 00:33:13 +0000172// RUN: FileCheck --check-prefix=CHECK-G %s < %t
173//
174// CHECK-G: @_ZTV1GIiE = weak_odr constant
175template <typename T>
176class G {
177public:
178 G() {}
179 virtual void f0();
180 virtual void f1();
181};
182template <>
183void G<int>::f1() {}
184template <typename T>
185void G<T>::f0() {}
186void G_f0() { new G<int>(); }