1*0a6a1f1dSLionel Sambuc // 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*0a6a1f1dSLionel Sambuc // 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
3f4a2713aSLionel Sambuc
4f4a2713aSLionel Sambuc // This check logically is attached to 'template int S<int>::i;' below.
5f4a2713aSLionel Sambuc // CHECK: @_ZN1SIiE1iE = weak_odr global i32
6f4a2713aSLionel Sambuc
7f4a2713aSLionel Sambuc template<typename T, typename U, typename Result>
8f4a2713aSLionel Sambuc struct plus {
9f4a2713aSLionel Sambuc Result operator()(const T& t, const U& u) const;
10f4a2713aSLionel Sambuc };
11f4a2713aSLionel Sambuc
12f4a2713aSLionel Sambuc template<typename T, typename U, typename Result>
operator ()(const T & t,const U & u) const13f4a2713aSLionel Sambuc Result plus<T, U, Result>::operator()(const T& t, const U& u) const {
14f4a2713aSLionel Sambuc return t + u;
15f4a2713aSLionel Sambuc }
16f4a2713aSLionel Sambuc
17f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl
18f4a2713aSLionel Sambuc template struct plus<int, long, long>;
19f4a2713aSLionel Sambuc
20*0a6a1f1dSLionel Sambuc namespace EarlyInstantiation {
21*0a6a1f1dSLionel Sambuc // Check that we emit definitions if we instantiate a function definition before
22*0a6a1f1dSLionel Sambuc // it gets explicitly instantiatied.
23*0a6a1f1dSLionel Sambuc template<typename T> struct S {
constexpr_functionEarlyInstantiation::S24*0a6a1f1dSLionel Sambuc constexpr int constexpr_function() { return 0; }
deduced_return_typeEarlyInstantiation::S25*0a6a1f1dSLionel Sambuc auto deduced_return_type() { return 0; }
26*0a6a1f1dSLionel Sambuc };
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc // From an implicit instantiation.
29*0a6a1f1dSLionel Sambuc constexpr int a = S<char>().constexpr_function();
30*0a6a1f1dSLionel Sambuc int b = S<char>().deduced_return_type();
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc // From an explicit instantiation declaration.
33*0a6a1f1dSLionel Sambuc extern template struct S<int>;
34*0a6a1f1dSLionel Sambuc constexpr int c = S<int>().constexpr_function();
35*0a6a1f1dSLionel Sambuc int d = S<int>().deduced_return_type();
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE18constexpr_functionEv(
38*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE19deduced_return_typeEv(
39*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE18constexpr_functionEv(
40*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE19deduced_return_typeEv(
41*0a6a1f1dSLionel Sambuc template struct S<char>;
42*0a6a1f1dSLionel Sambuc template struct S<int>;
43*0a6a1f1dSLionel Sambuc
constexpr_function()44*0a6a1f1dSLionel Sambuc template<typename T> constexpr int constexpr_function() { return 0; }
deduced_return_type()45*0a6a1f1dSLionel Sambuc template<typename T> auto deduced_return_type() { return 0; }
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc // From an implicit instantiation.
48*0a6a1f1dSLionel Sambuc constexpr int e = constexpr_function<char>();
49*0a6a1f1dSLionel Sambuc int f = deduced_return_type<char>();
50*0a6a1f1dSLionel Sambuc
51*0a6a1f1dSLionel Sambuc // From an explicit instantiation declaration.
52*0a6a1f1dSLionel Sambuc extern template int constexpr_function<int>();
53*0a6a1f1dSLionel Sambuc extern template auto deduced_return_type<int>();
54*0a6a1f1dSLionel Sambuc constexpr int g = constexpr_function<int>();
55*0a6a1f1dSLionel Sambuc int h = deduced_return_type<int>();
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc // The FIXMEs below are for PR19551.
58*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIcEEiv(
59*0a6a1f1dSLionel Sambuc // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIcEEiv(
60*0a6a1f1dSLionel Sambuc // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIiEEiv(
61*0a6a1f1dSLionel Sambuc // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIiEEiv(
62*0a6a1f1dSLionel Sambuc template int constexpr_function<char>();
63*0a6a1f1dSLionel Sambuc // FIXME template auto deduced_return_type<char>();
64*0a6a1f1dSLionel Sambuc template int constexpr_function<int>();
65*0a6a1f1dSLionel Sambuc // FIXME template auto deduced_return_type<int>();
66*0a6a1f1dSLionel Sambuc }
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc namespace LateInstantiation {
69*0a6a1f1dSLionel Sambuc // Check that we downgrade the linkage to available_externally if we see an
70*0a6a1f1dSLionel Sambuc // explicit instantiation declaration after the function template is
71*0a6a1f1dSLionel Sambuc // instantiated.
fLateInstantiation::S72*0a6a1f1dSLionel Sambuc template<typename T> struct S { constexpr int f() { return 0; } };
f()73*0a6a1f1dSLionel Sambuc template<typename T> constexpr int f() { return 0; }
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc // Trigger eager instantiation of the function definitions.
76*0a6a1f1dSLionel Sambuc int a, b = S<char>().f() + f<char>() + a;
77*0a6a1f1dSLionel Sambuc int c, d = S<int>().f() + f<int>() + a;
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc // Don't allow some of those definitions to be emitted.
80*0a6a1f1dSLionel Sambuc extern template struct S<int>;
81*0a6a1f1dSLionel Sambuc extern template int f<int>();
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc // Check that we declare, define, or provide an available-externally
84*0a6a1f1dSLionel Sambuc // definition as appropriate.
85*0a6a1f1dSLionel Sambuc // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1SIcE1fEv(
86*0a6a1f1dSLionel Sambuc // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1fIcEEiv(
87*0a6a1f1dSLionel Sambuc // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1SIiE1fEv(
88*0a6a1f1dSLionel Sambuc // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1fIiEEiv(
89*0a6a1f1dSLionel Sambuc // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1SIiE1fEv(
90*0a6a1f1dSLionel Sambuc // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1fIiEEiv(
91*0a6a1f1dSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc namespace PR21718 {
94*0a6a1f1dSLionel Sambuc // The linkage of a used constexpr member function can change from linkonce_odr
95*0a6a1f1dSLionel Sambuc // to weak_odr after explicit instantiation without errors about defining the
96*0a6a1f1dSLionel Sambuc // same function twice.
97*0a6a1f1dSLionel Sambuc template <typename T>
98*0a6a1f1dSLionel Sambuc struct S {
99*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define weak_odr i32 @_ZN7PR217181SIiE1fEv
fPR21718::S100*0a6a1f1dSLionel Sambuc __attribute__((used)) constexpr int f() { return 0; }
101*0a6a1f1dSLionel Sambuc };
g()102*0a6a1f1dSLionel Sambuc int g() { return S<int>().f(); }
103*0a6a1f1dSLionel Sambuc template struct S<int>;
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc
106f4a2713aSLionel Sambuc // Check that we emit definitions from explicit instantiations even when they
107f4a2713aSLionel Sambuc // occur prior to the definition itself.
108f4a2713aSLionel Sambuc template <typename T> struct S {
109f4a2713aSLionel Sambuc void f();
110f4a2713aSLionel Sambuc static void g();
111f4a2713aSLionel Sambuc static int i;
112f4a2713aSLionel Sambuc struct S2 {
113f4a2713aSLionel Sambuc void h();
114f4a2713aSLionel Sambuc };
115f4a2713aSLionel Sambuc };
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv
118f4a2713aSLionel Sambuc template void S<int>::f();
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv
121f4a2713aSLionel Sambuc template void S<int>::g();
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // See the check line at the top of the file.
124f4a2713aSLionel Sambuc template int S<int>::i;
125f4a2713aSLionel Sambuc
126f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv
127f4a2713aSLionel Sambuc template void S<int>::S2::h();
128f4a2713aSLionel Sambuc
f()129f4a2713aSLionel Sambuc template <typename T> void S<T>::f() {}
g()130f4a2713aSLionel Sambuc template <typename T> void S<T>::g() {}
131f4a2713aSLionel Sambuc template <typename T> int S<T>::i;
h()132f4a2713aSLionel Sambuc template <typename T> void S<T>::S2::h() {}
133