]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/CodeGenCXX/explicit-instantiation.cpp
Vendor import of clang trunk r290819:
[FreeBSD/FreeBSD.git] / test / CodeGenCXX / explicit-instantiation.cpp
1 // 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-passes -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT
3 // RUN: %clang_cc1 -emit-llvm -triple i686-pc-win32 -std=c++1y -o - %s | FileCheck %s --check-prefix=CHECK-MS
4
5 // This check logically is attached to 'template int S<int>::i;' below.
6 // CHECK: @_ZN1SIiE1iE = weak_odr global i32
7
8 template<typename T, typename U, typename Result>
9 struct plus {
10   Result operator()(const T& t, const U& u) const;
11 };
12
13 template<typename T, typename U, typename Result>
14 Result plus<T, U, Result>::operator()(const T& t, const U& u) const {
15   return t + u;
16 }
17
18 // CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl
19 template struct plus<int, long, long>;
20
21 namespace EarlyInstantiation {
22   // Check that we emit definitions if we instantiate a function definition before
23   // it gets explicitly instantiatied.
24   template<typename T> struct S {
25     constexpr int constexpr_function() { return 0; }
26     auto deduced_return_type() { return 0; }
27   };
28
29   // From an implicit instantiation.
30   constexpr int a = S<char>().constexpr_function();
31   int b = S<char>().deduced_return_type();
32
33   // From an explicit instantiation declaration.
34   extern template struct S<int>;
35   constexpr int c = S<int>().constexpr_function();
36   int d = S<int>().deduced_return_type();
37
38   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE18constexpr_functionEv(
39   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIcE19deduced_return_typeEv(
40   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE18constexpr_functionEv(
41   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation1SIiE19deduced_return_typeEv(
42   template struct S<char>;
43   template struct S<int>;
44
45   template<typename T> constexpr int constexpr_function() { return 0; }
46   template<typename T> auto deduced_return_type() { return 0; }
47
48   // From an implicit instantiation.
49   constexpr int e = constexpr_function<char>();
50   int f = deduced_return_type<char>();
51
52   // From an explicit instantiation declaration.
53   extern template int constexpr_function<int>();
54   extern template auto deduced_return_type<int>();
55   constexpr int g = constexpr_function<int>();
56   int h = deduced_return_type<int>();
57
58   // The FIXMEs below are for PR19551.
59   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIcEEiv(
60   // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIcEEiv(
61   // CHECK: define weak_odr i32 @_ZN18EarlyInstantiation18constexpr_functionIiEEiv(
62   // FIXME: define weak_odr i32 @_ZN18EarlyInstantiation19deduced_return_typeIiEEiv(
63   template int constexpr_function<char>();
64   // FIXME template auto deduced_return_type<char>();
65   template int constexpr_function<int>();
66   // FIXME template auto deduced_return_type<int>();
67 }
68
69 namespace LateInstantiation {
70   // Check that we downgrade the linkage to available_externally if we see an
71   // explicit instantiation declaration after the function template is
72   // instantiated.
73   template<typename T> struct S { constexpr int f() { return 0; } };
74   template<typename T> constexpr int f() { return 0; }
75
76   // Trigger eager instantiation of the function definitions.
77   int a, b = S<char>().f() + f<char>() + a;
78   int c, d = S<int>().f() + f<int>() + a;
79
80   // Don't allow some of those definitions to be emitted.
81   extern template struct S<int>;
82   extern template int f<int>();
83
84   // Check that we declare, define, or provide an available-externally
85   // definition as appropriate.
86   // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1SIcE1fEv(
87   // CHECK: define linkonce_odr i32 @_ZN17LateInstantiation1fIcEEiv(
88   // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1SIiE1fEv(
89   // CHECK-NO-OPT: declare i32 @_ZN17LateInstantiation1fIiEEiv(
90   // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1SIiE1fEv(
91   // CHECK-OPT: define available_externally i32 @_ZN17LateInstantiation1fIiEEiv(
92 }
93
94 namespace PR21718 {
95 // The linkage of a used constexpr member function can change from linkonce_odr
96 // to weak_odr after explicit instantiation without errors about defining the
97 // same function twice.
98 template <typename T>
99 struct S {
100 // CHECK-LABEL: define weak_odr i32 @_ZN7PR217181SIiE1fEv
101   __attribute__((used)) constexpr int f() { return 0; }
102 };
103 int g() { return S<int>().f(); }
104 template struct S<int>;
105 }
106
107 namespace NestedClasses {
108   // Check how explicit instantiation of an outer class affects the inner class.
109   template <typename T> struct Outer {
110     struct Inner {
111       void f() {}
112     };
113   };
114
115   // Explicit instantiation definition of Outer causes explicit instantiation
116   // definition of Inner.
117   template struct Outer<int>;
118   // CHECK: define weak_odr void @_ZN13NestedClasses5OuterIiE5Inner1fEv
119   // CHECK-MS: define weak_odr x86_thiscallcc void @"\01?f@Inner@?$Outer@H@NestedClasses@@QAEXXZ"
120
121   // Explicit instantiation declaration of Outer causes explicit instantiation
122   // declaration of Inner, but not in MSVC mode.
123   extern template struct Outer<char>;
124   auto use = &Outer<char>::Inner::f;
125   // CHECK: {{declare|define available_externally}} void @_ZN13NestedClasses5OuterIcE5Inner1fEv
126   // CHECK-MS: define linkonce_odr x86_thiscallcc void @"\01?f@Inner@?$Outer@D@NestedClasses@@QAEXXZ"
127 }
128
129 // Check that we emit definitions from explicit instantiations even when they
130 // occur prior to the definition itself.
131 template <typename T> struct S {
132   void f();
133   static void g();
134   static int i;
135   struct S2 {
136     void h();
137   };
138 };
139
140 // CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv
141 template void S<int>::f();
142
143 // CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv
144 template void S<int>::g();
145
146 // See the check line at the top of the file.
147 template int S<int>::i;
148
149 // CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv
150 template void S<int>::S2::h();
151
152 template <typename T> void S<T>::f() {}
153 template <typename T> void S<T>::g() {}
154 template <typename T> int S<T>::i;
155 template <typename T> void S<T>::S2::h() {}