blob: 6076444c25b02a8f4595e9623d8dd5a492037c86 [file] [log] [blame]
Richard Smitheb36ddf2014-04-24 22:45:46 +00001// RUN: %clang_cc1 -emit-llvm -triple i686-pc-linux-gnu -std=c++1y -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NO-OPT
2// RUN: %clang_cc1 -emit-llvm -triple i686-pc-linux-gnu -std=c++1y -O3 -disable-llvm-optzns -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT
Douglas Gregor28ad4b52009-05-26 20:50:29 +00003
Chandler Carruthcfe41db2010-08-25 08:27:02 +00004// This check logically is attached to 'template int S<int>::i;' below.
John McCalla97f3292011-04-12 01:46:54 +00005// CHECK: @_ZN1SIiE1iE = weak_odr global i32
Chandler Carruthcfe41db2010-08-25 08:27:02 +00006
Douglas Gregor28ad4b52009-05-26 20:50:29 +00007template<typename T, typename U, typename Result>
8struct plus {
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00009 Result operator()(const T& t, const U& u) const;
Douglas Gregor28ad4b52009-05-26 20:50:29 +000010};
11
Douglas Gregor34ec2ef2009-09-04 22:48:11 +000012template<typename T, typename U, typename Result>
13Result plus<T, U, Result>::operator()(const T& t, const U& u) const {
14 return t + u;
15}
16
Stephen Lin43622612013-08-15 06:47:53 +000017// CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl
Douglas Gregor28ad4b52009-05-26 20:50:29 +000018template struct plus<int, long, long>;
Chandler Carruthcfe41db2010-08-25 08:27:02 +000019
Richard Smitheb36ddf2014-04-24 22:45:46 +000020namespace EarlyInstantiation {
21 // Check that we emit definitions if we instantiate a function definition before
22 // it gets explicitly instantiatied.
23 template<typename T> struct S {
24 constexpr int constexpr_function() { return 0; }
25 auto deduced_return_type() { return 0; }
26 };
27
28 // From an implicit instantiation.
29 constexpr int a = S<char>().constexpr_function();
30 int b = S<char>().deduced_return_type();
31
32 // From an explicit instantiation declaration.
33 extern template struct S<int>;
34 constexpr int c = S<int>().constexpr_function();
35 int d = S<int>().deduced_return_type();
36
37 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE18constexpr_functionEv(
38 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE19deduced_return_typeEv(
39 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE18constexpr_functionEv(
40 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE19deduced_return_typeEv(
41 template struct S<char>;
42 template struct S<int>;
43
44 template<typename T> constexpr int constexpr_function() { return 0; }
45 template<typename T> auto deduced_return_type() { return 0; }
46
47 // From an implicit instantiation.
48 constexpr int e = constexpr_function<char>();
49 int f = deduced_return_type<char>();
50
51 // From an explicit instantiation declaration.
52 extern template int constexpr_function<int>();
53 extern template auto deduced_return_type<int>();
54 constexpr int g = constexpr_function<int>();
55 int h = deduced_return_type<int>();
56
57 // The FIXMEs below are for PR19551.
58 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIcEEiv(
59 // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIcEEiv(
60 // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIiEEiv(
61 // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIiEEiv(
62 template int constexpr_function<char>();
63 // FIXME template auto deduced_return_type<char>();
64 template int constexpr_function<int>();
65 // FIXME template auto deduced_return_type<int>();
66}
67
68namespace LateInstantiation {
69 // Check that we downgrade the linkage to available_externally if we see an
70 // explicit instantiation declaration after the function template is
71 // instantiated.
72 template<typename T> struct S { constexpr int f() { return 0; } };
73 template<typename T> constexpr int f() { return 0; }
74
75 // Trigger eager instantiation of the function definitions.
76 int a, b = S<char>().f() + f<char>() + a;
77 int c, d = S<int>().f() + f<int>() + a;
78
79 // Don't allow some of those definitions to be emitted.
80 extern template struct S<int>;
81 extern template int f<int>();
82
83 // Check that we declare, define, or provide an available-externally
84 // definition as appropriate.
85 // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1SIcE1fEv(
86 // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1fIcEEiv(
87 // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1SIiE1fEv(
88 // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1fIiEEiv(
89 // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1SIiE1fEv(
90 // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1fIiEEiv(
91}
92
Hans Wennborg43a0f992015-01-10 01:19:48 +000093namespace PR21718 {
94// The linkage of a used constexpr member function can change from linkonce_odr
95// to weak_odr after explicit instantiation without errors about defining the
96// same function twice.
97template <typename T>
98struct S {
99// CHECK-LABEL: define weak_odr i32 @_ZN7PR217181SIiE1fEv
100 __attribute__((used)) constexpr int f() { return 0; }
101};
102int g() { return S<int>().f(); }
103template struct S<int>;
104}
105
Chandler Carruthcfe41db2010-08-25 08:27:02 +0000106// Check that we emit definitions from explicit instantiations even when they
107// occur prior to the definition itself.
108template <typename T> struct S {
109 void f();
110 static void g();
111 static int i;
112 struct S2 {
113 void h();
114 };
115};
116
Stephen Lin43622612013-08-15 06:47:53 +0000117// CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv
Chandler Carruthcfe41db2010-08-25 08:27:02 +0000118template void S<int>::f();
119
Stephen Lin43622612013-08-15 06:47:53 +0000120// CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv
Chandler Carruthcfe41db2010-08-25 08:27:02 +0000121template void S<int>::g();
122
123// See the check line at the top of the file.
124template int S<int>::i;
125
Stephen Lin43622612013-08-15 06:47:53 +0000126// CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv
Chandler Carruthcfe41db2010-08-25 08:27:02 +0000127template void S<int>::S2::h();
128
129template <typename T> void S<T>::f() {}
130template <typename T> void S<T>::g() {}
131template <typename T> int S<T>::i;
132template <typename T> void S<T>::S2::h() {}