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-optzns -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT 3 4 // This check logically is attached to 'template int S<int>::i;' below. 5 // CHECK: @_ZN1SIiE1iE = weak_odr global i32 6 7 template<typename T, typename U, typename Result> 8 struct plus { 9 Result operator()(const T& t, const U& u) const; 10 }; 11 12 template<typename T, typename U, typename Result> 13 Result plus<T, U, Result>::operator()(const T& t, const U& u) const { 14 return t + u; 15 } 16 17 // CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl 18 template struct plus<int, long, long>; 19 20 namespace 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 68 namespace 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 93 namespace 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. 97 template <typename T> 98 struct S { 99 // CHECK-LABEL: define weak_odr i32 @_ZN7PR217181SIiE1fEv 100 __attribute__((used)) constexpr int f() { return 0; } 101 }; 102 int g() { return S<int>().f(); } 103 template struct S<int>; 104 } 105 106 // Check that we emit definitions from explicit instantiations even when they 107 // occur prior to the definition itself. 108 template <typename T> struct S { 109 void f(); 110 static void g(); 111 static int i; 112 struct S2 { 113 void h(); 114 }; 115 }; 116 117 // CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv 118 template void S<int>::f(); 119 120 // CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv 121 template void S<int>::g(); 122 123 // See the check line at the top of the file. 124 template int S<int>::i; 125 126 // CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv 127 template void S<int>::S2::h(); 128 129 template <typename T> void S<T>::f() {} 130 template <typename T> void S<T>::g() {} 131 template <typename T> int S<T>::i; 132 template <typename T> void S<T>::S2::h() {} 133