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
7template<typename T, typename U, typename Result>
8struct plus {
9  Result operator()(const T& t, const U& u) const;
10};
11
12template<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
17// CHECK-LABEL: define weak_odr i32 @_ZNK4plusIillEclERKiRKl
18template struct plus<int, long, long>;
19
20namespace 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
93// Check that we emit definitions from explicit instantiations even when they
94// occur prior to the definition itself.
95template <typename T> struct S {
96  void f();
97  static void g();
98  static int i;
99  struct S2 {
100    void h();
101  };
102};
103
104// CHECK-LABEL: define weak_odr void @_ZN1SIiE1fEv
105template void S<int>::f();
106
107// CHECK-LABEL: define weak_odr void @_ZN1SIiE1gEv
108template void S<int>::g();
109
110// See the check line at the top of the file.
111template int S<int>::i;
112
113// CHECK-LABEL: define weak_odr void @_ZN1SIiE2S21hEv
114template void S<int>::S2::h();
115
116template <typename T> void S<T>::f() {}
117template <typename T> void S<T>::g() {}
118template <typename T> int S<T>::i;
119template <typename T> void S<T>::S2::h() {}
120