1 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
2 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s
3 
4 // ALL: ; ModuleID
5 
6 extern "C" int foo();
7 
8 template<typename T> struct A { static int a; };
9 template<typename T> int A<T>::a = foo();
10 
11 // ALLK-NOT: @_ZN1AIcE1aE
12 template<> int A<char>::a;
13 
14 // ALL: @_ZN1AIbE1aE = global i32 10
15 template<> int A<bool>::a = 10;
16 
17 // ALL: @llvm.global_ctors = appending global [8 x { i32, void ()*, i8* }]
18 
19 // ELF: [{ i32, void ()*, i8* } { i32 65535, void ()* @[[unordered1:[^,]*]], i8* bitcast (i32* @_ZN1AIsE1aE to i8*) },
20 // MACHO: [{ i32, void ()*, i8* } { i32 65535, void ()* @[[unordered1:[^,]*]], i8* null },
21 
22 // ELF:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered2:[^,]*]], i8* bitcast (i16* @_Z1xIsE to i8*) },
23 // MACHO:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered2:[^,]*]], i8* null },
24 
25 // ELF:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered3:[^,]*]], i8* bitcast (i32* @_ZN2ns1aIiE1iE to i8*) },
26 // MACHO:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered3:[^,]*]], i8* null },
27 
28 // ELF:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered4:[^,]*]], i8* bitcast (i32* @_ZN2ns1b1iIiEE to i8*) },
29 // MACHO:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered4:[^,]*]], i8* null },
30 
31 // ELF:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered5:[^,]*]], i8* bitcast (i32* @_ZN1AIvE1aE to i8*) },
32 // MACHO:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered5:[^,]*]], i8* null },
33 
34 // ELF:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered6:[^,]*]], i8* @_Z1xIcE },
35 // MACHO:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered6:[^,]*]], i8* null },
36 
37 // ALL:  { i32, void ()*, i8* } { i32 65535, void ()* @[[unordered7:[^,]*]], i8* null },
38 
39 // ALL:  { i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp, i8* null }]
40 
41 template int A<short>::a;  // Unordered
42 int b = foo();
43 int c = foo();
44 int d = A<void>::a; // Unordered
45 
46 // An explicit specialization is ordered, and goes in __GLOBAL_sub_I_static_member_variable_explicit_specialization.cpp.
47 template<> struct A<int> { static int a; };
48 int A<int>::a = foo();
49 
50 template<typename T> struct S { static T x; static T y; };
51 template<> int S<int>::x = foo();
52 template<> int S<int>::y = S<int>::x;
53 
54 template<typename T> T x = foo();
55 template short x<short>;  // Unordered
56 template<> int x<int> = foo();
57 int e = x<char>; // Unordered
58 
59 namespace ns {
60 template <typename T> struct a {
61   static int i;
62 };
63 template<typename T> int a<T>::i = foo();
64 template struct a<int>;
65 
66 struct b {
67   template <typename T> static T i;
68 };
69 template<typename T> T b::i = foo();
70 template int b::i<int>;
71 }
72 
73 namespace {
74 template<typename T> struct Internal { static int a; };
75 template<typename T> int Internal<T>::a = foo();
76 }
77 int *use_internal_a = &Internal<int>::a;
78 
79 // ALL: define internal void @[[unordered1]](
80 // ALL: call i32 @foo()
81 // ALL: store {{.*}} @_ZN1AIsE1aE
82 // ALL: ret
83 
84 // ALL: define internal void @[[unordered2]](
85 // ALL: call i32 @foo()
86 // ALL: store {{.*}} @_Z1xIsE
87 // ALL: ret
88 
89 // ALL: define internal void @[[unordered3]](
90 // ALL: call i32 @foo()
91 // ALL: store {{.*}} @_ZN2ns1aIiE1iE
92 // ALL: ret
93 
94 // ALL: define internal void @[[unordered4]](
95 // ALL: call i32 @foo()
96 // ALL: store {{.*}} @_ZN2ns1b1iIiEE
97 // ALL: ret
98 
99 // ALL: define internal void @[[unordered5]](
100 // ALL: call i32 @foo()
101 // ALL: store {{.*}} @_ZN1AIvE1aE
102 // ALL: ret
103 
104 // ALL: define internal void @[[unordered6]](
105 // ALL: call i32 @foo()
106 // ALL: store {{.*}} @_Z1xIcE
107 // ALL: ret
108 
109 // ALL: define internal void @[[unordered7]](
110 // ALL: call i32 @foo()
111 // ALL: store {{.*}} @_ZN12_GLOBAL__N_18InternalIiE1aE
112 // ALL: ret
113 
114 // ALL: define internal void @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp()
115 //   We call unique stubs for every ordered dynamic initializer in the TU.
116 // ALL: call
117 // ALL: call
118 // ALL: call
119 // ALL: call
120 // ALL: call
121 // ALL: call
122 // ALL: call
123 // ALL: call
124 // ALL-NOT: call
125 // ALL: ret
126